forked from letssteam/DAPLink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteami_i2c.c
More file actions
326 lines (254 loc) · 8.35 KB
/
Copy pathsteami_i2c.c
File metadata and controls
326 lines (254 loc) · 8.35 KB
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
#include "steami_i2c.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_rcc.h"
#include "stm32f1xx_hal_gpio.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "steami_uart.h"
#include "steami_i2c_dma.h"
static uint32_t time_master_ask_data = 0;
static bool is_master_wait_data = false;
static bool is_listen_I2C = false;
static I2C_HandleTypeDef hi2c2;
static steami_cmd_callback on_cmd_recv = NULL;
static uint8_t rx_i2c_command = 0x00;
static uint8_t rx_i2c_argument[STEAMI_I2C_RX_BUFFER_SIZE] = {0};
static uint16_t rx_i2c_len_argument = 0;
static uint8_t tx_i2c_data[STEAMI_I2C_TX_BUFFER_SIZE] = {0};
static uint16_t tx_i2c_len_data = 0;
static bool is_command_valid(uint8_t cmd){
switch (cmd)
{
case WHO_AM_I:
case CLEAR_FLASH:
case SET_FILENAME:
case GET_FILENAME:
case WRITE_DATA:
case READ_SECTOR:
case WRITE_CONFIG:
case READ_CONFIG:
case CLEAR_CONFIG:
case STATUS:
case ERROR_STATUS:
return true;
default:
return false;
}
}
static uint16_t get_argument_byte_number(uint8_t cmd){
switch (cmd)
{
case WHO_AM_I:
return 0;
case CLEAR_FLASH:
return 0;
case SET_FILENAME:
return 11;
case GET_FILENAME:
return 0;
case WRITE_DATA:
return 31;
case READ_SECTOR:
return 2;
case WRITE_CONFIG:
return 31; /* fixed frame: [offset_hi, offset_lo, len, data(28 bytes max)] */
case READ_CONFIG:
return 2;
case CLEAR_CONFIG:
return 0;
case STATUS:
return 0;
case ERROR_STATUS:
return 0;
default:
return 0;
}
}
static void execute_command(){
if( is_command_valid(rx_i2c_command) && on_cmd_recv != NULL ){
on_cmd_recv( (steami_i2c_command)rx_i2c_command, rx_i2c_argument, rx_i2c_len_argument);
rx_i2c_command = 0;
}
}
void I2C2_EV_IRQHandler(void){
HAL_I2C_EV_IRQHandler(&hi2c2);
}
void I2C2_ER_IRQHandler(void){
HAL_I2C_ER_IRQHandler(&hi2c2);
}
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c){
__HAL_RCC_I2C2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_AFIO_CLK_ENABLE();
GPIO_InitTypeDef i2c_gpio = {0};
i2c_gpio.Pin = GPIO_PIN_10 | GPIO_PIN_11;
i2c_gpio.Mode = GPIO_MODE_AF_OD;
i2c_gpio.Pull = GPIO_NOPULL;
i2c_gpio.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &i2c_gpio);
HAL_NVIC_SetPriority(I2C2_EV_IRQn, 0, 0);
HAL_NVIC_SetPriority(I2C2_ER_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c){
(void)hi2c;
HAL_NVIC_DisableIRQ(I2C2_EV_IRQn);
HAL_NVIC_DisableIRQ(I2C2_ER_IRQn);
__HAL_RCC_I2C2_CLK_DISABLE();
}
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
{
if(TransferDirection == I2C_DIRECTION_TRANSMIT)
{
tx_i2c_len_data = 0;
rx_i2c_len_argument = 0;
HAL_I2C_Slave_Seq_Receive_IT(hi2c, &rx_i2c_command, 1, I2C_FIRST_FRAME);
}
else if(TransferDirection == I2C_DIRECTION_RECEIVE)
{
is_master_wait_data = true;
time_master_ask_data = HAL_GetTick();
}
}
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
if( rx_i2c_len_argument > 0 ){
execute_command();
}
else{
rx_i2c_len_argument = get_argument_byte_number(rx_i2c_command);
if( rx_i2c_len_argument > 0 ){
HAL_I2C_Slave_Seq_Receive_IT(hi2c, rx_i2c_argument, rx_i2c_len_argument, I2C_NEXT_FRAME);
}
else{
execute_command();
}
}
}
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c){
if( is_listen_I2C ) HAL_I2C_EnableListen_IT(hi2c);
}
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c){
if( is_listen_I2C ) HAL_I2C_EnableListen_IT(hi2c);
}
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
steami_uart_write_string("[I2C ERROR] ");
steami_uart_write_number(hi2c->ErrorCode, HEX);
switch( hi2c->ErrorCode ){
case HAL_I2C_ERROR_NONE :
steami_uart_write_string(" (HAL_I2C_ERROR_NONE)\n");
break;
case HAL_I2C_ERROR_BERR :
steami_uart_write_string(" (HAL_I2C_ERROR_BERR)\n");
steami_uart_write_string("Re-init I2C...\n");
if( is_master_wait_data ){
tx_i2c_data[0] = 0xFF;
HAL_I2C_Slave_Seq_Transmit_IT(&hi2c2, tx_i2c_data, 1, I2C_LAST_FRAME);
is_master_wait_data = false;
}
steami_i2c_deinit();
steami_i2c_init();
break;
case HAL_I2C_ERROR_ARLO :
steami_uart_write_string(" (HAL_I2C_ERROR_ARLO)\n");
break;
case HAL_I2C_ERROR_AF :
steami_uart_write_string(" (HAL_I2C_ERROR_AF)\n");
break;
case HAL_I2C_ERROR_OVR :
steami_uart_write_string(" (HAL_I2C_ERROR_OVR)\n");
break;
case HAL_I2C_ERROR_DMA :
steami_uart_write_string(" (HAL_I2C_ERROR_DMA)\n");
break;
case HAL_I2C_ERROR_TIMEOUT :
steami_uart_write_string(" (HAL_I2C_ERROR_TIMEOUT)\n");
break;
case HAL_I2C_ERROR_SIZE :
steami_uart_write_string(" (HAL_I2C_ERROR_SIZE)\n");
break;
case HAL_I2C_ERROR_DMA_PARAM:
steami_uart_write_string(" (HAL_I2C_ERROR_DMA_PARAM)\n");
break;
case HAL_I2C_WRONG_START :
steami_uart_write_string(" (HAL_I2C_WRONG_START)\n");
break;
default:
steami_uart_write_string(" (UNKNOWN)\n");
steami_i2c_deinit();
steami_i2c_init();
break;
}
}
void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c){
steami_uart_write_string("[I2C ABORT]\n");
}
bool steami_i2c_init(){
hi2c2.Instance = I2C2;
hi2c2.Init.ClockSpeed = 100000;
hi2c2.Init.OwnAddress1 = I2C_STEAMI_ADDRESS;
hi2c2.Init.OwnAddress2 = 0x00;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if( HAL_I2C_Init(&hi2c2) != HAL_OK ){
return false;
}
if( HAL_I2C_EnableListen_IT(&hi2c2) != HAL_OK ){
return false;
}
is_listen_I2C = true;
return steami_i2c_dma_init(&hi2c2);
}
void steami_i2c_deinit(){
is_listen_I2C = false;
HAL_I2C_DisableListen_IT(&hi2c2);
HAL_I2C_DeInit(&hi2c2);
}
void steami_i2c_on_receive_command( steami_cmd_callback callback ){
on_cmd_recv = callback;
}
void steami_i2c_set_tx_data(uint8_t* data, uint16_t len){
tx_i2c_len_data = (len > STEAMI_I2C_TX_BUFFER_SIZE) ? STEAMI_I2C_TX_BUFFER_SIZE : len;
memcpy(tx_i2c_data, data, tx_i2c_len_data);
}
void steami_i2c_process(){
if( is_listen_I2C && READ_BIT(hi2c2.Instance->CR1, I2C_CR1_PE) == 0){
steami_i2c_init();
return;
}
if( is_master_wait_data && (HAL_GetTick() - time_master_ask_data) >= I2C_STEAMI_MASTER_READ_TIMEOUT_MS){
tx_i2c_data[0] = 0xFF;
HAL_I2C_Slave_Seq_Transmit_IT(&hi2c2, tx_i2c_data, 1, I2C_LAST_FRAME);
is_master_wait_data = false;
tx_i2c_len_data = 0;
steami_uart_write_string("[I2C] Send data timeout...\n");
}
else if( is_master_wait_data && tx_i2c_len_data > 0 ){
HAL_I2C_DisableListen_IT(&hi2c2);
switch( HAL_I2C_Slave_Transmit_DMA(&hi2c2, tx_i2c_data, tx_i2c_len_data) ){
case HAL_OK:
// steami_uart_write_string("I2C DMA Success\n");
break;
case HAL_ERROR:
steami_uart_write_string("I2C DMA ERROR\n");
break;
case HAL_BUSY:
steami_uart_write_string("I2C DMA BUSY\n");
break;
case HAL_TIMEOUT:
steami_uart_write_string("I2C DMA TIMEOUT\n");
break;
default:
steami_uart_write_string("I2C DMA DEFAULT ??\n");
break;
}
is_master_wait_data = false;
tx_i2c_len_data = 0;
}
}