forked from liyanboy74/soft-i2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw_i2c_port_at32.c
280 lines (245 loc) · 8.24 KB
/
sw_i2c_port_at32.c
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
/***
* Software I2C port for Artery AT32 MCUs
*/
#include "sw_i2c.h"
#include <stdbool.h>
#include "at32f435_437_gpio.h"
#define TAG "I2Cport"
#include "log.h"
/**
* Macroses
*/
#define SW_I2C0_SCL_PORT GPIOA
#define SW_I2C0_SDA_PORT GPIOA
#define SW_I2C0_SCL_PIN GPIO_PINS_15
#define SW_I2C0_SDA_PIN GPIO_PINS_14
#define SW_I2C1_SCL_PORT GPIOA
#define SW_I2C1_SDA_PORT GPIOA
#define SW_I2C1_SCL_PIN GPIO_PINS_10
#define SW_I2C1_SDA_PIN GPIO_PINS_9
/**
* Static functions prototypes
*/
static bool is_line_busy(void * arg);
static void sda_in_mode(void * arg);
static void sda_out_mode(void * arg);
static void scl_in_mode(void * arg);
static void scl_out_mode(void * arg);
static int sw_i2c_port_initial(void * arg);
static int sw_i2c_port_deinit(void * arg);
static void sw_i2c_port_delay_us(uint32_t us);
static int sw_i2c_port_io_ctl(uint8_t opt, void * param);
/**
* Static functions
*/
/**
* Check if I2C line is busy (pulling SCL and SDA low)
* @param arg pointer to sw_i2c_t
* @return true if line is busy
*/
static bool is_line_busy(void * arg)
{
sw_i2c_t * bus = arg;
gpio_init_type GPIO_InitStruct =
{
.gpio_mode = GPIO_MODE_INPUT,
.gpio_pull = GPIO_PULL_UP,
};
GPIO_InitStruct.gpio_pins = bus->scl_pin | bus->sda_pin;
gpio_init(bus->scl_port, &GPIO_InitStruct);
gpio_pin_mux_config(bus->scl_port, bus->scl_pin, GPIO_MUX_0);
gpio_pin_mux_config(bus->sda_port, bus->sda_pin, GPIO_MUX_0);
for (volatile int i = 0; i < 10000; i++); // waiting for charging traces
return (gpio_input_data_bit_read(bus->scl_port, bus->scl_pin) & gpio_input_data_bit_read(bus->sda_port, bus->sda_pin)) ?
false : true;
}
static void sda_in_mode(void * arg)
{
sw_i2c_t * bus = arg;
static gpio_init_type GPIO_InitStruct =
{
.gpio_mode = GPIO_MODE_INPUT,
.gpio_pull = GPIO_PULL_UP,
};
GPIO_InitStruct.gpio_pins = bus->sda_pin;
gpio_init(bus->sda_port, &GPIO_InitStruct);
//logD("Port %d pin %d", bus->sda_port, bus->sda_pin);
}
static void sda_out_mode(void * arg)
{
sw_i2c_t * bus = arg;
static gpio_init_type GPIO_InitStruct =
{
.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN,
.gpio_mode = GPIO_MODE_OUTPUT,
.gpio_pull = GPIO_PULL_UP,
};
GPIO_InitStruct.gpio_pins = bus->sda_pin;
gpio_bits_set(bus->sda_port, bus->sda_pin); // remove possible low
gpio_init(bus->sda_port, &GPIO_InitStruct);
//logD("Port %d pin %d", bus->sda_port, bus->sda_pin);
}
static void scl_in_mode(void * arg)
{
sw_i2c_t * bus = arg;
static gpio_init_type GPIO_InitStruct =
{
.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
//.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN,
.gpio_mode = GPIO_MODE_INPUT,
.gpio_pull = GPIO_PULL_UP,
};
GPIO_InitStruct.gpio_pins = bus->scl_pin;
gpio_init(bus->scl_port, &GPIO_InitStruct);
//logD("Port %d pin %d", bus->sda_port, bus->sda_pin);
}
static void scl_out_mode(void * arg)
{
sw_i2c_t * bus = arg;
static gpio_init_type GPIO_InitStruct =
{
.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN,
.gpio_mode = GPIO_MODE_OUTPUT,
.gpio_pull = GPIO_PULL_UP,
};
GPIO_InitStruct.gpio_pins = bus->scl_pin;
gpio_bits_set(bus->scl_port, bus->scl_pin); // remove possible low
gpio_init(bus->scl_port, &GPIO_InitStruct);
//logD("Port %d pin %d", bus->sda_port, bus->sda_pin);
}
static int sw_i2c_port_initial(void * arg)
{
sw_i2c_t * bus = arg;
static gpio_init_type GPIO_InitStruct =
{
.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN,
.gpio_mode = GPIO_MODE_OUTPUT,
.gpio_pull = GPIO_PULL_UP
};
// i2c_sw SCL
GPIO_InitStruct.gpio_pins = bus->scl_pin;
gpio_init(bus->scl_port, &GPIO_InitStruct);
// i2c_sw SDA
GPIO_InitStruct.gpio_pins = bus->sda_pin;
gpio_init(bus->sda_port, &GPIO_InitStruct);
gpio_pin_mux_config(bus->scl_port, bus->scl_pin, GPIO_MUX_0);
gpio_pin_mux_config(bus->sda_port, bus->sda_pin, GPIO_MUX_0);
if(bus->i2c_sem == NULL)
bus->i2c_sem = xSemaphoreCreateMutex();
xSemaphoreGive(bus->i2c_sem);
return 0;
}
static int sw_i2c_port_deinit(void * arg)
{
sw_i2c_t * bus = arg;
gpio_init_type GPIO_InitStruct = {0};
// i2c_sw SCL
GPIO_InitStruct.gpio_pins = bus->scl_pin;
gpio_init(bus->scl_port, &GPIO_InitStruct);
// i2c_sw SDA
GPIO_InitStruct.gpio_pins = bus->sda_pin;
gpio_init(bus->sda_port, &GPIO_InitStruct);
if(bus->i2c_sem != NULL)
vSemaphoreDelete(bus->i2c_sem);
return 0;
}
#define DWT_BASE (0xE0001000UL) /**< Базовый адрес DWT */
#define DWT_CYCCNT (*(volatile uint32_t *)(DWT_BASE + 0x04)) /**< Регистр CYCCNT */
#define DWT_CONTROL (*(volatile uint32_t *)(DWT_BASE)) /**< Регистр управления DWT */
#define SCB_DEMCR (*(volatile uint32_t *)0xE000EDFC) /**< Регистр управления и отслеживания (Debug Exception and Monitor Control Register) */
static void sw_i2c_port_delay_us(uint32_t us)
{
if(us % 1000 == 0)
{
u32 ms = us / 1000;
vTaskDelay(ms);
}
else
{
uint32_t cpu_freq_mhz = SystemCoreClock / 1000000;
// Включаем DWT и CYCCNT
SCB_DEMCR |= 0x01000000; // Разрешаем использование DWT
DWT_CONTROL |= 1; // Включаем CYCCNT
uint32_t start_ticks = DWT_CYCCNT;
uint32_t delay_ticks = us * cpu_freq_mhz;
// Ждем, пока не пройдет нужное количество тактов
while ((DWT_CYCCNT - start_ticks) < delay_ticks);
// Отключаем CYCCNT для экономии энергии (по желанию)
DWT_CONTROL &= ~1;
}
}
static int sw_i2c_port_io_ctl(uint8_t opt, void * arg)
{
sw_i2c_t * bus = arg;
int ret = -1;
switch (opt)
{
case HAL_IO_OPT_SET_SDA_HIGH:
gpio_bits_set(bus->sda_port, bus->sda_pin);
//logD("SDA HI, Port %d pin %d", bus->sda_port, bus->sda_pin);
break;
case HAL_IO_OPT_SET_SDA_LOW:
gpio_bits_reset(bus->sda_port, bus->sda_pin);
//logD("SDA LO, Port %d pin %d", bus->sda_port, bus->sda_pin);
break;
case HAL_IO_OPT_GET_SDA_LEVEL:
ret = gpio_input_data_bit_read(bus->sda_port, bus->sda_pin);
break;
case HAL_IO_OPT_SET_SDA_INPUT:
sda_in_mode(bus);
break;
case HAL_IO_OPT_SET_SDA_OUTPUT:
sda_out_mode(bus);
break;
case HAL_IO_OPT_SET_SCL_HIGH:
gpio_bits_set(bus->scl_port, bus->scl_pin);
//logD("SCL HI, Port %d pin %d", bus->scl_port, bus->scl_pin);
break;
case HAL_IO_OPT_SET_SCL_LOW:
gpio_bits_reset(bus->scl_port, bus->scl_pin);
//logD("SCL LO, Port %d pin %d", bus->scl_port, bus->scl_pin);
break;
case HAL_IO_OPT_GET_SCL_LEVEL:
ret = gpio_input_data_bit_read(bus->scl_port, bus->scl_pin);
break;
case HAL_IO_OPT_SET_SCL_INPUT:
scl_in_mode(bus);
break;
case HAL_IO_OPT_SET_SCL_OUTPUT:
scl_out_mode(bus);
break;
case HAL_IO_OPT_IS_LINE_BUSY:
ret = is_line_busy(bus);
break;
default:
break;
}
return ret;
}
sw_i2c_t i2c_bus0 =
{
.hal_init = sw_i2c_port_initial,
.hal_deinit = sw_i2c_port_deinit,
.hal_io_ctl = sw_i2c_port_io_ctl,
.hal_delay_us = sw_i2c_port_delay_us,
.scl_pin = SW_I2C0_SCL_PIN,
.scl_port = SW_I2C0_SCL_PORT,
.sda_pin = SW_I2C0_SDA_PIN,
.sda_port = SW_I2C0_SDA_PORT,
.i2c_sem = NULL
},
i2c_bus1 =
{
.hal_init = sw_i2c_port_initial,
.hal_deinit = sw_i2c_port_deinit,
.hal_io_ctl = sw_i2c_port_io_ctl,
.hal_delay_us = sw_i2c_port_delay_us,
.scl_pin = SW_I2C1_SCL_PIN,
.scl_port = SW_I2C1_SCL_PORT,
.sda_pin = SW_I2C1_SDA_PIN,
.sda_port = SW_I2C1_SDA_PORT,
.i2c_sem = NULL
};