-
Notifications
You must be signed in to change notification settings - Fork 0
/
arm_hardi2c.c
executable file
·485 lines (391 loc) · 10.6 KB
/
arm_hardi2c.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* STM32 Controller for direct drive truck
* Copyright (C) 2012-2021 Adam Williams <broadcast at earthling dot net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "arm_hardi2c.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_rcc.h"
#include "uart.h"
#include "misc.h"
#define I2C_TIMEOUT_MAX 100000
// wait for the bits to be set
#define WAIT_STATUS_TRUE(verbose) \
{ \
i2c->status1 |= i2c->regs->SR1; \
i2c->status1 |= (i2c->regs->SR2 << 16); \
if((i2c->status1 & i2c->want_event) != i2c->want_event) \
{ \
if ((i2c->timeout--) > 0) \
{ \
return; \
} \
else \
if(verbose) \
{ \
TRACE2 \
print_text("WAIT_STATUS_TRUE flags="); \
print_hex(i2c->status1); \
init_hardi2c(i2c, i2c->regs, i2c->pin_combo); \
i2c->error = 1; \
} \
} \
}
// Wait for the bits to not be set
#define WAIT_STATUS_FALSE(verbose) \
{ \
i2c->status1 = i2c->regs->SR1; \
i2c->status1 |= (i2c->regs->SR2 << 16); \
if((i2c->status1 & i2c->want_event) == i2c->want_event) \
{ \
if ((i2c->timeout--) > 0) \
{ \
return; \
} \
else \
if(verbose) \
{ \
TRACE2 \
print_text("WAIT_STATUS_FALSE flags="); \
print_hex(i2c->status1); \
init_hardi2c(i2c, i2c->regs, i2c->pin_combo); \
} \
} \
}
void i2c_idle(void *i2c)
{
}
static void i2c_write_device5(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_FALSE(1)
i2c->state = i2c_idle;
}
static void i2c_write_device4(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
I2C_GenerateSTOP(i2c->regs, ENABLE);
#ifdef I2C_INTERRUPTS
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_FLAG_BUSY;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_write_device5;
#else
i2c->state = i2c_idle;
#endif
}
static void i2c_write_device3(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_BYTE_TRANSMITTED;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_write_device4;
I2C_SendData(i2c->regs, i2c->value);
}
static void i2c_write_device2(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_BYTE_TRANSMITTED;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_write_device3;
I2C_SendData(i2c->regs, i2c->reg_address);
}
static void i2c_write_device1(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_write_device2;
I2C_Send7bitAddress(i2c->regs, i2c->dev_address, I2C_Direction_Transmitter);
}
static void i2c_write_device0(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_FALSE(1)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_write_device1;
i2c->want_event = I2C_EVENT_MASTER_MODE_SELECT;
I2C_GenerateSTART(i2c->regs, ENABLE);
}
void i2c_write_device(i2c_t *i2c,
unsigned char dev_address,
unsigned char reg_address,
unsigned char value)
{
i2c->dev_address = dev_address;
i2c->reg_address = reg_address;
i2c->value = value;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->got_event = 0;
i2c->status1 = 0;
#ifdef I2C_INTERRUPTS
i2c->want_event = I2C_EVENT_MASTER_MODE_SELECT;
I2C_GenerateSTART(i2c->regs, ENABLE);
i2c->state = i2c_write_device1;
#else
i2c->want_event = I2C_FLAG_BUSY;
i2c->state = i2c_write_device0;
#endif
}
static void i2c_read_device7(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_FALSE(1)
i2c->state = i2c_idle;
}
static void i2c_read_device6(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
i2c->value = I2C_ReceiveData(i2c->regs);
if(i2c->bytes > 1)
{
i2c->burst[i2c->bytes_read] = i2c->value;
if(i2c->bytes_read < i2c->bytes - 1)
{
I2C_AcknowledgeConfig(i2c->regs, ENABLE);
}
else
{
I2C_AcknowledgeConfig(i2c->regs, DISABLE);
}
}
i2c->bytes_read++;
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_BYTE_RECEIVED;
if(i2c->bytes_read >= i2c->bytes)
{
I2C_GenerateSTOP(i2c->regs, ENABLE);
#ifdef I2C_INTERRUPTS
i2c->got_event = 0;
i2c->status1 = 0;
i2c->state = i2c_read_device7;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->want_event = I2C_FLAG_BUSY;
#else
i2c->state = i2c_idle;
#endif
}
i2c->timeout = I2C_TIMEOUT_MAX;
}
static void i2c_read_device5(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
if(i2c->bytes > 1)
{
I2C_AcknowledgeConfig(i2c->regs, ENABLE);
}
else
{
I2C_AcknowledgeConfig(i2c->regs, DISABLE);
}
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_BYTE_RECEIVED;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_read_device6;
}
static void i2c_read_device4(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_read_device5;
I2C_Send7bitAddress(i2c->regs, i2c->dev_address, I2C_Direction_Receiver);
}
static void i2c_read_device3(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(2)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_MODE_SELECT;
i2c->regs->SR1 |= (uint16_t)0x0400;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_read_device4;
I2C_GenerateSTART(i2c->regs, ENABLE);
}
static void i2c_read_device2(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(2)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_BYTE_TRANSMITTED;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_read_device3;
I2C_SendData(i2c->regs, (uint8_t)i2c->reg_address);
}
static void i2c_read_device1(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_TRUE(1)
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->state = i2c_read_device2;
I2C_Send7bitAddress(i2c->regs, i2c->dev_address, I2C_Direction_Transmitter);
}
static void i2c_read_device0(void *ptr)
{
i2c_t *i2c = (i2c_t*)ptr;
WAIT_STATUS_FALSE(1)
i2c->state = i2c_read_device1;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->got_event = 0;
i2c->status1 = 0;
i2c->want_event = I2C_EVENT_MASTER_MODE_SELECT;
I2C_GenerateSTART(i2c->regs, ENABLE);
}
void i2c_read_device(i2c_t *i2c,
unsigned char dev_address,
unsigned char reg_address)
{
i2c->dev_address = dev_address;
i2c->reg_address = reg_address;
i2c->value = 0;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->bytes = 1;
i2c->bytes_read = 0;
i2c->got_event = 0;
i2c->status1 = 0;
#ifdef I2C_INTERRUPTS
i2c->state = i2c_read_device1;
i2c->want_event = I2C_EVENT_MASTER_MODE_SELECT;
I2C_GenerateSTART(i2c->regs, ENABLE);
#else
i2c->want_event = I2C_FLAG_BUSY;
i2c->state = i2c_read_device0;
#endif
}
void i2c_read_burst(i2c_t *i2c,
unsigned char dev_address,
unsigned char reg_address,
int bytes)
{
i2c->dev_address = dev_address;
i2c->reg_address = reg_address;
i2c->value = 0;
i2c->timeout = I2C_TIMEOUT_MAX;
i2c->bytes = bytes;
i2c->bytes_read = 0;
i2c->got_event = 0;
i2c->status1 = 0;
int i;
for(i = 0; i < bytes; i++)
i2c->burst[i] = 0;
#ifdef I2C_INTERRUPTS
i2c->state = i2c_read_device1;
i2c->want_event = I2C_EVENT_MASTER_MODE_SELECT;
I2C_GenerateSTART(i2c->regs, ENABLE);
#else
i2c->state = i2c_read_device0;
i2c->want_event = I2C_FLAG_BUSY;
#endif
}
#ifdef I2C_INTERRUPTS
void I2C3_EV_IRQHandler()
{
//TRACE2
//print_hex(global_i2c->state);
//flush_uart();
global_i2c->state(global_i2c);
}
#endif
void init_hardi2c(i2c_t *i2c, I2C_TypeDef *regs, int pin_combo)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
i2c->error = 0;
i2c->regs = regs;
i2c->pin_combo = pin_combo;
if(regs == I2C1)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
if(pin_combo == 0)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
}
else
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
}
}
I2C_Cmd(i2c->regs, DISABLE);
I2C_DeInit(i2c->regs);
i2c->regs->SR1 = 0;
i2c->regs->SR2 = 0;
I2C_Cmd(i2c->regs, ENABLE);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
// I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0xFE;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
// I2C_InitStructure.I2C_ClockSpeed = 100000;
// I2C_InitStructure.I2C_ClockSpeed = 150000;
// I2C_InitStructure.I2C_ClockSpeed = 200000;
// I2C_InitStructure.I2C_ClockSpeed = 400000;
/* Apply i2c->regs configuration after enabling it */
I2C_Init(i2c->regs, &I2C_InitStructure);
i2c->state = i2c_idle;
#ifdef I2C_INTERRUPTS
NVIC_SetVectorTable(NVIC_VectTab_FLASH, PROGRAM_START - 0x08000000);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = I2C3_EV_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
I2C_ITConfig(i2c->regs, I2C_IT_EVT, ENABLE);
#endif
}