ISKBoard example project
guowenxue
5 days ago d517a825c688a35bb0d4441726fc42a2cef118ae
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
/**********************************************************************
 *   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
 *
 ***********************************************************************/
 
#ifndef __I2C_BITBANG_H_
#define __I2C_BITBANG_H_
 
#include "stm32l4xx_hal.h"
 
#define I2C_CLK_STRETCH_TIMEOUT   6000
 
/*
 *+---------------------------------+
 *|  I2C GPIO port Level functions  |
 *+---------------------------------+
 */
 
/* GPIO port for I2C typedef */
typedef struct i2c_gpio_s
{
    GPIO_TypeDef        *group; /* GPIO group */
    uint16_t             pin;   /* GPIO pin   */
} i2c_gpio_t;
 
/* I2C bus typedef */
typedef struct i2c_bus_s
{
    uint8_t             addr; /* 7-bits slave address */
    i2c_gpio_t          scl;  /* SCL GPIO pin */
    i2c_gpio_t          sda;  /* SDA GPIO pin */
} i2c_bus_t;
 
 
/*
 *+--------------------------------+
 *|  I2C High Level API functions  |
 *+--------------------------------+
 */
 
/* I2C bus error number */
enum
{
  ERROR_NONE     = 0x00, // no error
  ERROR_PARM     = 0x01, // parameter out of range error
  ERROR_ACK      = 0x02, // no acknowledgment error
  ERROR_TIMEOUT  = 0x03, // timeout error
  ERROR_CHECKSUM = 0x04, // checksum mismatch error
};
 
/* Set I2C slave device 7-bits address in the bus to lock it */
extern int i2c_lock(uint8_t addr);
 
/* Clear I2C slave device 7-bits address in the bus to free it */
extern void i2c_free(void);
 
/* write data by to slave device */
extern int i2c_write(uint8_t *data, int bytes);
 
/* read data from slave device */
extern int i2c_read(uint8_t *buf, int size);
 
/*
 *+--------------------------------+
 *|  I2C Low Level API functions   |
 *+--------------------------------+
 */
 
/* Send I2C start condition */
extern void I2C_StartCondition(void);
 
/* Send I2C stop condition */
extern void I2C_StopCondition(void);
 
/* Send I2C slave device 8-bits address */
#define W_ADDR(x)     ((x)<<1 )    /* I2C address bit[0]=0 means write */
#define R_ADDR(x)     ((x)<<1 | 1) /* I2C address bit[0]=1 means read  */
enum
{
    I2C_WR,  /* Direction for write */
    I2C_RD,  /* Direction for read  */
};
extern int I2C_SendAddress(uint8_t dir);
 
/* Write a byte data to I2C slave device */
extern int I2C_WriteByte(uint8_t txByte);
 
/* Read a byte data from I2C slave device */
enum
{
    ACK_NONE, /* ACK None */
    ACK,      /* ACK */
    NAK,      /* NAK */
};
extern int I2C_ReadByte(uint8_t *rxByte, uint8_t ack, uint32_t timeout);
 
#endif /* __I2C_BITBANG_H_ */