ISKBoard example project
guowenxue
7 days ago 6c7b6c910be1dcdc0bb786e02be648b1a56faa5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**********************************************************************
 *   Copyright: (C)2024 LingYun IoT System Studio
 *      Author: GuoWenxue<guowenxue@gmail.com>
 *
 * Description: The purpose of this code is to provide a simple C library,
 *              which providing software bit-bang of the I2C protocol on
 *              any GPIO pins for ISKBoard.
 *
 *   ChangeLog:
 *        Version    Date       Author            Description
 *        V1.0.0  2024.08.29    GuoWenxue      Release initial version
 *
 ***********************************************************************/
 
#include "i2c_bitbang.h"
#include "miscdev.h"  /* udelay() implement by TIM6 */
 
/*
 *+---------------------------------+
 *|  I2C GPIO port Level functions  |
 *+---------------------------------+
 */
 
#define mdelay(ms) HAL_Delay(ms)
 
enum
{
    LOW,
    HIGH,
};
 
i2c_bus_t i2c_bus =
{
 /* Addr        SCL_Pin               SDA_Pin      */
    0x00,  {GPIOB, GPIO_PIN_6 }, {GPIOB, GPIO_PIN_7 }, /* SHT20, RTC, OLED */
};
 
static inline void SDA_IN(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
 
    GPIO_InitStruct.Pin = i2c_bus.sda.pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(i2c_bus.sda.group, &GPIO_InitStruct);
}
 
static inline void SDA_OUT(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
 
    GPIO_InitStruct.Pin = i2c_bus.sda.pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(i2c_bus.sda.group, &GPIO_InitStruct);
}
 
static inline void SCL_OUT(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
 
    GPIO_InitStruct.Pin = i2c_bus.scl.pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(i2c_bus.scl.group, &GPIO_InitStruct);
}
 
static inline void SCL_SET(int level)
{
    HAL_GPIO_WritePin(i2c_bus.scl.group, i2c_bus.scl.pin, level?GPIO_PIN_SET:GPIO_PIN_RESET);
}
 
static inline void SDA_SET(int level)
{
    HAL_GPIO_WritePin(i2c_bus.sda.group, i2c_bus.sda.pin, level?GPIO_PIN_SET:GPIO_PIN_RESET);
}
 
static inline GPIO_PinState SCL_GET(void)
{
    return HAL_GPIO_ReadPin(i2c_bus.scl.group, i2c_bus.scl.pin);
}
 
static inline GPIO_PinState SDA_GET(void)
{
    return HAL_GPIO_ReadPin(i2c_bus.sda.group, i2c_bus.sda.pin);
}
 
/*
 *+--------------------------------+
 *|  I2C Low Level API functions   |
 *+--------------------------------+
 */
 
void I2C_StartCondition(void)
{
    SDA_OUT();
    SCL_OUT();
 
    /* Start Condition Clock Sequence:
              ______
     SCL:           |___
            _____
     SDA:        |_____
    */
    SDA_SET(HIGH);
    SCL_SET( HIGH);
    udelay(1);
 
    SDA_SET(LOW);
    udelay(1);  // hold time start condition (t_HD;STA)
    SCL_SET(LOW);
    udelay(1);
}
 
void I2C_StopCondition(void)
{
    SDA_OUT();
 
    /* SCL Stop Condition Clock Sequence:
                _______
      SCL:   ___|
                   _____
      SDA:   _____|
    */
    SCL_SET(LOW);
    SDA_SET(LOW);
    udelay(1);
 
    SCL_SET(HIGH);
    SDA_SET(HIGH);
    udelay(1);
}
 
int I2C_SendAddress(uint8_t dir)
{
    uint8_t       addr;
    uint8_t       rv;
 
    if( I2C_WR == dir )
    {
        addr = W_ADDR(i2c_bus.addr);
    }
    else
    {
        addr = R_ADDR(i2c_bus.addr);
    }
 
    rv=I2C_WriteByte(addr);
 
    return rv;
}
 
void I2C_Ack(uint8_t ack)
{
    if(ACK_NONE == ack)
        return ;
 
    SCL_SET(LOW);
    SDA_OUT();
 
    if( ACK==ack )
        SDA_SET(LOW);
    else if( NAK==ack )
        SDA_SET(HIGH);
 
    udelay(1);
 
    SCL_SET( HIGH);
    udelay(1);
    SCL_SET(LOW);
}
 
int I2C_WriteByte(uint8_t txByte)
{
    int     error = 0;
    uint8_t mask;
 
    SDA_OUT();
    SCL_SET(LOW);
 
    /* start send 8 bits data, MSB */
    for(mask=0x80; mask>0; mask>>=1)
    {
        if((mask & txByte) == 0)
            SDA_SET(LOW);
        else
            SDA_SET(HIGH);
 
        udelay(1);  // data set-up time (t_SU;DAT)
        SCL_SET(HIGH);
        udelay(1);  // SCL high time (t_HIGH)
        SCL_SET(LOW);
        udelay(1);  // data hold time(t_HD;DAT)
    }
 
    /* Wait for ACK/NAK */
    SDA_IN();
    SCL_SET(HIGH); // clk #9 for ack
    udelay(1); // data set-up time (t_SU;DAT)
 
    /* High level means NAK */
    if( SDA_GET() )
        error = -ERROR_ACK;
 
    SCL_SET(LOW);
 
    udelay(1);
 
    return error;
}
 
static inline int I2C_WaitWhileClockStreching(uint8_t timeout)
{
    while( SCL_GET()== 0)
    {
        if(timeout-- == 0)
            return -ERROR_TIMEOUT;
 
        udelay(10);
    }
    return ERROR_NONE;
}
 
int I2C_ReadByte(uint8_t *rxByte, uint8_t ack, uint32_t timeout)
{
    uint8_t error = 0;
    uint8_t mask;
 
    *rxByte = 0x00;
    SDA_IN();
 
    /* start read 8 bits data, MSB */
    for(mask = 0x80; mask > 0; mask >>= 1)
    {
        SCL_SET(HIGH);  // start clock on SCL-line
        udelay(1);         // clock set-up time (t_SU;CLK)
 
        // wait while clock streching
        error = I2C_WaitWhileClockStreching(timeout);
 
        udelay(1); // SCL high time (t_HIGH)
 
        if( SDA_GET() )
            *rxByte |= mask; // read bit
 
        SCL_SET(LOW);
        udelay(1); // data hold time(t_HD;DAT)
    }
 
    I2C_Ack(ack);
 
    return error;
}
 
/*
 *+--------------------------------+
 *|  I2C High Level API functions  |
 *+--------------------------------+
 */
 
int i2c_lock(uint8_t addr)
{
    /* I2C bus already be used */
    while( i2c_bus.addr && i2c_bus.addr!=addr )
    {
        mdelay(10);
    }
 
    i2c_bus.addr = addr;
 
    return 0;
}
 
void i2c_free(void)
{
    i2c_bus.addr = 0x0;
}
 
int i2c_write(uint8_t *data, int bytes)
{
    int       i;
    int       rv = 0;
 
    if( !data )
    {
        return -ERROR_PARM;
    }
 
    /* send start condition  */
    I2C_StartCondition();
 
    /* send device address for write */
    if( (rv=I2C_SendAddress(I2C_WR)) < 0 )
        goto OUT;
 
    /* send data to slave device */
    for (i=0; i<bytes; i++)
    {
        if( (rv=I2C_WriteByte(data[i])) < 0 )
        {
            break;
        }
    }
 
OUT:
    /* send stop condition */
    I2C_StopCondition();
 
    return rv;
}
 
int i2c_read(uint8_t *buf, int size)
{
    int       i;
    int       rv = ERROR_NONE;
    uint8_t   byte;
 
    /* send start condition  */
    I2C_StartCondition();
 
    /* send device address for read */
    if( (rv=I2C_SendAddress(I2C_RD)) < 0 )
        goto OUT;
 
    /* read data from slave device */
    for (i=0; i<size; i++)
    {
        if( (rv=I2C_ReadByte(&byte, ACK, I2C_CLK_STRETCH_TIMEOUT)) < 0 )
            goto OUT;
 
        buf[i] = byte;
    }
 
OUT:
    /* send stop condition  */
    I2C_StopCondition();
    return rv;
}