凌云物联网实验室ISKBoard(IoT Starter Kits Board)开发板项目源码
guowenxue
2023-04-03 32806da6f5647ac637fa7d48aa9c221b091ab35e
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
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * i2c_bitbang.c
 *
 *  Created on: Jan 19, 2023
 *      Author: Wenxue
 */
 
 
#include "i2c_bitbang.h"
 
i2c_bus_t i2c_bus[I2CBUSMAX] =
{
    /* Bus   ChipAddr       SCL_Pin              SDA_Pin      */
    {I2CBUS0, 0x00, {GPIOB, GPIO_PIN_6 }, {GPIOB, GPIO_PIN_7 } }, /* SHT20, RTC, OLED */
    {I2CBUS1, 0x00, {GPIOB, GPIO_PIN_10}, {GPIOB, GPIO_PIN_11} }, /* MikroBUS I2C interface */
};
 
 
enum
{
    LOW,
    HIGH,
};
 
 
/*+--------------------------+
 *|    Low level GPIO API    |
 *+--------------------------+*/
 
static inline void SDA_IN(uint8_t bus)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
 
    GPIO_InitStruct.Pin = i2c_bus[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[bus].sda.group, &GPIO_InitStruct);
}
 
static inline void SDA_OUT(uint8_t bus)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
 
    GPIO_InitStruct.Pin = i2c_bus[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[bus].sda.group, &GPIO_InitStruct);
}
 
static inline void SCL_OUT(uint8_t bus)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
 
    GPIO_InitStruct.Pin = i2c_bus[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[bus].scl.group, &GPIO_InitStruct);
}
 
static inline void SCL_SET(uint8_t bus, int level)
{
    HAL_GPIO_WritePin(i2c_bus[bus].scl.group, i2c_bus[bus].scl.pin, level?GPIO_PIN_SET:GPIO_PIN_RESET);
}
 
static inline void SDA_SET(uint8_t bus, int level)
{
    HAL_GPIO_WritePin(i2c_bus[bus].sda.group, i2c_bus[bus].sda.pin, level?GPIO_PIN_SET:GPIO_PIN_RESET);
}
 
static inline GPIO_PinState SCL_GET(uint8_t bus)
{
    return HAL_GPIO_ReadPin(i2c_bus[bus].scl.group, i2c_bus[bus].scl.pin);
}
 
static inline GPIO_PinState SDA_GET(uint8_t bus)
{
    return HAL_GPIO_ReadPin(i2c_bus[bus].sda.group, i2c_bus[bus].sda.pin);
}
 
 
/*+--------------------------+
 *|  I2C Clock Sequence API  |
 *+--------------------------+*/
 
/* */
#if 1
#include "miscdev.h"  /* delay_us() implement by TIM6 */
#else
static inline void delay_us(uint32_t us)
{
    uint32_t      i, j;
    for(j=0; j<450; j++)// 450 ~500
        for(i = 0; i < us; i++)
        {
            __NOP();
        }
}
#endif
 
 
void I2C_StartCondition(uint8_t bus)
{
    SDA_OUT(bus);
    SCL_OUT(bus);
 
    /* Start Condition Clock Sequence:
              ______
     SCL:           |___
            _____
     SDA:        |_____
    */
    SDA_SET(bus, HIGH);
    SCL_SET(bus, HIGH);
    delay_us(1);
 
    SDA_SET(bus, LOW);
    delay_us(1);  // hold time start condition (t_HD;STA)
    SCL_SET(bus, LOW);
    delay_us(1);
}
 
void I2C_StopCondition(uint8_t bus)
{
    SDA_OUT(bus);
 
    /* SCL Stop Condition Clock Sequence:
                _______
      SCL:   ___|
                   _____
      SDA:   _____|
    */
    SCL_SET(bus, LOW);
    SDA_SET(bus, LOW);
    delay_us(1);
 
    SCL_SET(bus, HIGH);
    SDA_SET(bus, HIGH);
    delay_us(1);
}
 
void I2C_Ack(uint8_t bus, uint8_t ack)
{
    if(ACK_NONE == ack)
        return ;
 
    SCL_SET(bus, LOW);
    SDA_OUT(bus);
 
    if( ACK==ack )
        SDA_SET(bus, LOW);
    else if( NAK==ack )
        SDA_SET(bus, HIGH);
 
    delay_us(1);
 
    SCL_SET(bus, HIGH);
    delay_us(1);
    SCL_SET(bus, LOW);
}
 
uint8_t I2C_WriteByte(uint8_t bus, uint8_t txByte)
{
    uint8_t error = NO_ERROR;
    uint8_t mask;
 
    SDA_OUT(bus);
    SCL_SET(bus, LOW);
 
    /* start send 8 bits data */
    for(mask=0x80; mask>0; mask>>=1)
    {
        if((mask & txByte) == 0)
            SDA_SET(bus, LOW);
        else
            SDA_SET(bus, HIGH);
 
        delay_us(1);    // data set-up time (t_SU;DAT)
        SCL_SET(bus, HIGH);
        delay_us(1);    // SCL high time (t_HIGH)
        SCL_SET(bus, LOW);
        delay_us(1);    // data hold time(t_HD;DAT)
    }
 
    /* Wait for ACK/NAK */
    SDA_IN(bus);
    SCL_SET(bus, HIGH); // clk #9 for ack
    delay_us(1); // data set-up time (t_SU;DAT)
 
    /* High level means NAK */
    if( SDA_GET(bus) )
        error = ACK_ERROR;
 
    SCL_SET(bus, LOW);
 
    delay_us(1);
 
    return error;
}
 
static inline uint8_t I2C_WaitWhileClockStreching(uint8_t bus, uint8_t timeout)
{
    uint8_t error = NO_ERROR;
 
    while( SCL_GET(bus)== 0)
    {
        if(timeout-- == 0)
            return TIMEOUT_ERROR;
 
        delay_us(10);
    }
    return error;
}
 
uint8_t I2C_ReadByte(uint8_t bus, uint8_t *rxByte, uint8_t ack, uint32_t timeout)
{
//==============================================================================
    uint8_t error = NO_ERROR;
    uint8_t mask;
 
    *rxByte = 0x00;
    SDA_IN(bus);
 
    /* start read 8 bits data */
    for(mask = 0x80; mask > 0; mask >>= 1)
    {
        SCL_SET(bus, HIGH);  // start clock on SCL-line
        delay_us(1);         // clock set-up time (t_SU;CLK)
 
        // wait while clock streching
        error = I2C_WaitWhileClockStreching(bus, timeout);
 
        delay_us(1); // SCL high time (t_HIGH)
 
        if( SDA_GET(bus) )
            *rxByte |= mask; // read bit
 
        SCL_SET(bus, LOW);
        delay_us(1); // data hold time(t_HD;DAT)
    }
 
    I2C_Ack(bus, ack);
  return error;
}
 
 
uint8_t I2C_SendAddress(uint8_t bus, uint8_t dir)
{
    uint8_t       addr;
    uint8_t       rv;
 
    if( I2C_WR == dir )
    {
        addr = W_ADDR(i2c_bus[bus].addr);
    }
    else
    {
        addr = R_ADDR(i2c_bus[bus].addr);
    }
 
    rv=I2C_WriteByte(bus, addr);
 
    return rv;
}
 
int i2c_init(uint8_t bus, uint8_t addr)
{
    if( bus >= I2CBUSMAX )
    {
        return -1;
    }
 
#if 0
    /* I2C bus used already */
    if( 0x00 != i2c_bus[bus].addr )
    {
        return -2;
    }
#endif
 
    /* Set I2C slave address */
    i2c_bus[bus].addr = addr;
 
    return 0;
}
 
int i2c_term(uint8_t bus)
{
    if( bus >= I2CBUSMAX )
    {
        return -1;
    }
 
    /* Set I2C slave address */
    i2c_bus[bus].addr = 0x0;
 
    return 0;
}
 
int i2c_read(uint8_t bus, uint8_t *buf, int size)
{
  int       i;
  int       rv = NO_ERROR;
    uint8_t   byte;
 
  I2C_StartCondition(bus);
 
  if( NO_ERROR != (rv=I2C_SendAddress(bus, I2C_RD) ) )
  {
    goto OUT;
  }
 
  for (i=0; i<size; i++)
  {
     if( !I2C_ReadByte(bus, &byte, ACK, I2C_CLK_STRETCH_TIMEOUT) )
             buf[i] = byte;
         else
             goto OUT;
  }
 
OUT:
  I2C_StopCondition(bus);
  return rv;
}
 
int i2c_write(uint8_t bus, uint8_t *data, int bytes)
{
  int       i;
  int       rv = NO_ERROR;
 
    if(!data)
        return PARM_ERROR;
 
  I2C_StartCondition(bus);
 
  if( NO_ERROR != (rv=I2C_SendAddress(bus, I2C_WR)) )
  {
    goto OUT;
  }
 
  for (i=0; i<bytes; i++)
  {
    if( NO_ERROR != (rv=I2C_WriteByte(bus, data[i])) )
    {
      break;
    }
  }
 
OUT:
  I2C_StopCondition(bus);
  return rv;
}