guowenxue
2021-08-29 77ddd4a0943e2f9935bec2e00fffacec370cc1aa
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**********************************************************************
*   Copyright: (C)2021 LingYun IoT System Studio <www.weike-iot.com>
*      Author: GuoWenxue<guowenxue@gmail.com> QQ: 281143292
* Description: BearKE1 NB-IoT Board GPIO simulate I2C bus source code
*
*      Notice: Must implement delay_us() by timer in tim.h first.
*
*   ChangeLog:
*        Version    Date       Author            Description
*        V1.0.0  2021.08.10  GuoWenxue      Release initial version
***********************************************************************/
 
#include <stdio.h>
#include "stm32l4xx_hal.h"
#include "tim.h"   /* delay_us() implement */
#include "gpio.h"
#include "gpio_i2c.h"
 
 
/* comment follow macro will disable I2C bus clock stretching support
 * reference: http://www.i2c-bus.org/clock-stretching/
 */
#define I2C_CLK_STRETCH_TIMEOUT      50
 
/* uncomment follow macro will enable debug print */
//#define CONFIG_GPIO_I2C_DEBUG
 
#ifdef CONFIG_GPIO_I2C_DEBUG
#define i2c_print(format,args...) printf(format, ##args)
#else
#define i2c_print(format,args...) do{} while(0)
#endif
 
/* GPIO Simulate I2C Bus pins */
typedef struct i2c_gpio_s
{
    GPIO_TypeDef        *group;
    uint16_t             scl; /* SCL */
    uint16_t             sda; /* SDA */
} i2c_gpio_t;
 
static i2c_gpio_t   i2c_pins = { GPIOB, GPIO_PIN_13/*SCL*/, GPIO_PIN_14/*SDA*/ };
 
 
#define SDA_IN()    do{    GPIO_InitTypeDef GPIO_InitStruct = {0}; \
                        GPIO_InitStruct.Pin = i2c_pins.sda; \
                        GPIO_InitStruct.Mode = GPIO_MODE_INPUT; \
                        GPIO_InitStruct.Pull = GPIO_PULLUP; \
                        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; \
                        HAL_GPIO_Init(i2c_pins.group, &GPIO_InitStruct); \
                    }while(0)
 
 
#define SDA_OUT()    do{    GPIO_InitTypeDef GPIO_InitStruct = {0}; \
                        GPIO_InitStruct.Pin = i2c_pins.sda; \
                        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; \
                        GPIO_InitStruct.Pull = GPIO_PULLUP; \
                        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; \
                        HAL_GPIO_Init(i2c_pins.group, &GPIO_InitStruct); \
                    }while(0)
 
#define SCL_OUT()    do{    GPIO_InitTypeDef GPIO_InitStruct = {0}; \
                        GPIO_InitStruct.Pin = i2c_pins.scl; \
                        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; \
                        GPIO_InitStruct.Pull = GPIO_PULLUP; \
                        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; \
                        HAL_GPIO_Init(i2c_pins.group, &GPIO_InitStruct); \
                    }while(0)
 
 
#define SCL_H()        HAL_GPIO_WritePin(i2c_pins.group, i2c_pins.scl, GPIO_PIN_SET)
#define SCL_L()        HAL_GPIO_WritePin(i2c_pins.group, i2c_pins.scl, GPIO_PIN_RESET)
#define SDA_H()        HAL_GPIO_WritePin(i2c_pins.group, i2c_pins.sda, GPIO_PIN_SET)
#define SDA_L()        HAL_GPIO_WritePin(i2c_pins.group, i2c_pins.sda, GPIO_PIN_RESET)
 
#define READ_SDA()    HAL_GPIO_ReadPin(i2c_pins.group, i2c_pins.sda)
#define READ_SCL()    HAL_GPIO_ReadPin(i2c_pins.group, i2c_pins.scl)
 
 
static inline uint8_t I2c_WaitWhileClockStretching(uint16_t timeout)
{
    while( timeout-- >  0 )
    {
        if( READ_SCL() )
            break;
 
        delay_us(1);
    }
 
    return timeout ? NO_ERROR : BUS_ERROR;
}
 
/* StartCondition(S) */
uint8_t I2c_StartCondition()
{
    uint8_t           rv = NO_ERROR;
 
    SDA_OUT();
    SCL_OUT();
 
    /* StartCondition(S): A high to low transition on the SDA line while SCL is high.
            _______
     SCL:          |___
            _____
     SDA:        |_____
    */
    SDA_H();
    delay_us(1);
    SCL_H();
    delay_us(1);
 
#ifdef I2C_CLK_STRETCH_TIMEOUT
    rv = I2c_WaitWhileClockStretching(I2C_CLK_STRETCH_TIMEOUT);
    if( rv  )
    {
        i2c_print("ERROR: %s() I2C bus busy\n", __func__);
        return rv;
    }
#endif
 
    SDA_L();
    delay_us(2);
 
    SCL_L();
    delay_us(2);
 
    return rv;
}
 
 
/* StopCondition(P) */
uint8_t I2c_StopCondition(void)
{
    uint8_t           rv = NO_ERROR;
 
    SDA_OUT();
 
    /* StopCondition(P): A low to high transition on the SDA line while SCL is high.
                _____
      SCL:   ___|
                  _____
      SDA:   _____|
    */
    SCL_L();
    SDA_L();
    delay_us(2);
 
    SCL_H();
    delay_us(2);
 
#ifdef I2C_CLK_STRETCH_TIMEOUT
    rv = I2c_WaitWhileClockStretching(I2C_CLK_STRETCH_TIMEOUT);
    if( rv  )
    {
        i2c_print("ERROR: %s() I2C bus busy\n", __func__);
    }
#endif
 
    SDA_H();
    delay_us(2);
 
    return rv;
}
 
 
 
uint8_t I2c_WriteByte(uint8_t byte)
{
    uint8_t rv = NO_ERROR;
    uint8_t mask;
 
    /* Data line changes must happened when SCL is low */
    SDA_OUT();
    SCL_L();
 
    /* 1Byte=8bit, MSB send: bit[7]-->bit[0] */
    for(mask=0x80; mask>0; mask>>=1)
    {
        if((mask & byte) == 0)
            SDA_L();
        else
            SDA_H();
 
        delay_us(1);    // data set-up time (t_SU;DAT)
 
        SCL_H();
        delay_us(5);    // SCL high time (t_HIGH)
 
#ifdef I2C_CLK_STRETCH_TIMEOUT
        rv = I2c_WaitWhileClockStretching(I2C_CLK_STRETCH_TIMEOUT);
        if( rv  )
        {
            i2c_print("ERROR: %s() I2C bus busy\n", __func__);
            goto OUT;
        }
#endif
 
        SCL_L();
        delay_us(1);    // data hold time(t_HD;DAT)
    }
 
    /* clk #9 wait ACK/NAK from slave */
    SDA_IN();
    SCL_H();     // clk #9 for ack
    delay_us(1); // data set-up time (t_SU;DAT)
 
#ifdef I2C_CLK_STRETCH_TIMEOUT
    rv = I2c_WaitWhileClockStretching(I2C_CLK_STRETCH_TIMEOUT);
    if( rv  )
    {
        i2c_print("ERROR: %s() I2C bus busy\n", __func__);
        goto OUT;
    }
#endif
 
    /* High level means NAK */
    if( READ_SDA() )
        rv = ACK_ERROR;
 
OUT:
    SCL_L();
    delay_us(20);
 
    return rv;
}
 
 
 
uint8_t I2c_ReadByte(uint8_t *byte, uint8_t ack)
{
    uint8_t rv = NO_ERROR;
    uint8_t mask;
 
    *byte = 0x00;
 
    SDA_IN();
 
    /* 1Byte=8bit, MSB send: bit[7]-->bit[0] */
    for(mask = 0x80; mask > 0; mask >>= 1)
    {
        SCL_H();     // start clock on SCL-line
        delay_us(1); // clock set-up time (t_SU;CLK)
 
#ifdef I2C_CLK_STRETCH_TIMEOUT
        rv = I2c_WaitWhileClockStretching(I2C_CLK_STRETCH_TIMEOUT);
        if( rv  )
        {
            i2c_print("ERROR: %s() I2C bus busy\n", __func__);
            goto OUT;
        }
#endif
 
        if(READ_SDA())
            *byte |= mask; // read bit
 
        SCL_L();
        delay_us(1); // data hold time(t_HD;DAT)
    }
 
    /* clk #9 send ACK/NAK to slave */
 
    if(ack == ACK)
    {
        SDA_OUT();
        SDA_L(); // send Acknowledge if necessary
    }
    else if( ack == NAK )
    {
        SDA_OUT();
        SDA_H(); // send NotAcknowledge if necessary
    }
 
    delay_us(1); // data set-up time (t_SU;DAT)
    SCL_H();     // clk #9 for ack
    delay_us(2); // SCL high time (t_HIGH)
 
#ifdef I2C_CLK_STRETCH_TIMEOUT
    rv = I2c_WaitWhileClockStretching(I2C_CLK_STRETCH_TIMEOUT);
    if( rv  )
    {
        i2c_print("ERROR: %s() I2C bus busy\n", __func__);
    }
#endif
 
OUT:
    SCL_L();
    delay_us(2);  // wait to see byte package on scope
 
  return rv;
}
 
 
uint8_t I2c_SendAddress(uint8_t addr)
{
    return I2c_WriteByte(addr);
}
 
 
int I2C_Master_Receive(uint8_t addr, uint8_t *buf, int len)
{
  int       i;
  int       rv = NO_ERROR;
  uint8_t   byte;
 
  I2c_StartCondition();
 
  rv = I2c_SendAddress(addr);
  if( rv )
  {
      i2c_print("Send I2C read address failure,  rv=%d\n", rv);
      goto OUT;
  }
 
#ifdef I2C_CLK_STRETCH_TIMEOUT
  /* wait while clock streching */
  rv = I2c_WaitWhileClockStretching(I2C_CLK_STRETCH_TIMEOUT);
  if( rv )
  {
      i2c_print("ERROR: %s() I2C wait clock stretching failure, rv=%d\n", __func__, rv);
      return rv;
  }
#endif
 
  for (i=0; i<len; i++)
  {
     if( !I2c_ReadByte(&byte, ACK) )
         buf[i] = byte;
     else
         goto OUT;
  }
 
OUT:
  I2c_StopCondition();
  return rv;
}
 
 
int I2C_Master_Transmit(uint8_t addr, uint8_t *data, int bytes)
{
 
    int       i;
    int       rv = NO_ERROR;
 
    if(!data)
    {
        return PARM_ERROR;
    }
 
    i2c_print("I2C Mastr start transimit [%d] bytes data to addr [0x%02x]\n", bytes, addr);
    I2c_StartCondition();
 
    rv = I2c_SendAddress(addr);
    if( rv )
    {
        goto OUT;
    }
 
    for (i=0; i<bytes; i++)
    {
        if( NO_ERROR != (rv=I2c_WriteByte(data[i])) )
        {
            break;
        }
    }
 
OUT:
    I2c_StopCondition();
    return rv;
}