Repository navigation
Expand file tree
/
Copy pathI2C.c
More file actions
252 lines (215 loc) · 5.31 KB
/
Copy pathI2C.c
File metadata and controls
252 lines (215 loc) · 5.31 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
/*
* I2C.cpp
*
* Created on: Nov 25, 2018
* Author: Jasper
*/
#include "I2C.h"
void I2C_init(i2cBASE_t *i)
{
if(I2C_initialized)
return;
// TODO: Figure out if mutex can be instantiated with a
// static member function instead
if(I2CMutex == NULL) {
I2CMutex = xSemaphoreCreateMutex();
}
i2c = i;
I2C_num_resets = 0;
I2C_initialized = true;
}
int16_t I2C_send(uint32_t length, uint8_t *data, uint32_t addr)
{
if(I2C_set_slave_addr(addr) != I2C_OK)
return I2C_MUTEX_FAIL;
int16_t err;
if(!I2C_get_mutex())
return I2C_MUTEX_FAIL;
i2cSetDirection(i2c, I2C_TRANSMITTER);
i2cSetCount(i2c, length);
i2cSetMode(i2c, I2C_MASTER);
i2cSetStop(i2c);
i2cSetStart(i2c);
err = _I2C_send(length, data);
if(err != I2C_OK) {
xSemaphoreGive(I2CMutex);
return err;
}
i2cSetStop(i2c);
i2cClearSCD(i2c);
err = I2C_is_bus_busy();
if(err != I2C_OK) {
xSemaphoreGive(I2CMutex);
return err;
}
err = I2C_ok_transmit();
if(err != I2C_OK) {
xSemaphoreGive(I2CMutex);
return err;
}
xSemaphoreGive(I2CMutex);
return I2C_OK;
}
int16_t _I2C_send(uint32_t length, uint8_t *data)
{
uint32_t timeout = 0;
while(length > 0U){
// Wait while transmit (I2C_TX_INT) and access
// (I2C_ARDY_INT) are not ready
while(((i2c->STR & (uint32_t) I2C_TX_INT
| (uint32_t) I2C_ARDY_INT)
== 0U)
&& timeout < I2C_TIMEOUT_MAX)
{
timeout++;
}
if(timeout >= I2C_TIMEOUT_MAX)
return I2C_TIMEOUT_FAIL;
if(i2c->STR & (uint32_t) I2C_NACK_INT) {
i2cSetStop(i2c);
i2c->STR = I2C_NACK_INT;
return I2C_ERR_NACK;
}
i2c->DXR = (uint32_t) (*data);
data++;
length--;
}
return I2C_OK;
}
int16_t I2C_receive(uint32_t clength,
uint8_t* cmd,
uint32_t dlength,
uint8_t *data,
uint32_t addr)
{
if(I2C_set_slave_addr(addr) != I2C_OK)
return I2C_MUTEX_FAIL;
int16_t err;
if(!I2C_get_mutex())
return I2C_MUTEX_FAIL;
i2cSetDirection(i2c, I2C_TRANSMITTER);
i2cSetCount(i2c, clength);
i2cSetMode(i2c, I2C_MASTER);
i2cSetStop(i2c);
i2cSetStart(i2c);
err = _I2C_send(clength, cmd);
if(err != I2C_OK) {
xSemaphoreGive(I2CMutex);
return err;
}
i2cSetDirection(i2c, I2C_RECEIVER);
i2cSetCount(i2c, dlength);
i2cSetMode(i2c, I2C_MASTER);
err = _I2C_receive(dlength, data);
if(err != I2C_OK) {
xSemaphoreGive(I2CMutex);
return err;
}
i2cSetStop(i2c);
i2cClearSCD(i2c);
err = I2C_is_bus_busy();
if(err != I2C_OK) {
xSemaphoreGive(I2CMutex);
return err;
}
err = I2C_ok_transmit();
if(err != I2C_OK) {
xSemaphoreGive(I2CMutex);
return err;
}
xSemaphoreGive(I2CMutex);
return I2C_OK;
}
int16_t _I2C_receive(uint32_t length, uint8_t *data)
{
uint32_t timeout = 0;
while(length > 0U){
// Wait while receive (I2C_RX_INT) is not ready
while(((i2c->STR & (uint32_t) I2C_RX_INT)
== 0U)
&& timeout < I2C_TIMEOUT_MAX)
{
timeout++;
}
if(timeout >= I2C_TIMEOUT_MAX)
return I2C_TIMEOUT_FAIL;
if(i2c->STR & (uint32_t) I2C_NACK_INT) {
i2cSetStop(i2c);
i2c->STR = I2C_NACK_INT;
return I2C_ERR_NACK;
}
*data = ((uint8_t) i2c->DRR);
data++;
length--;
}
return I2C_OK;
}
int16_t I2C_is_bus_busy()
{
uint32_t timeout = 0;
while((i2c->STR & I2C_BUSBUSY)
&& timeout < I2C_TIMEOUT_MAX)
{
timeout++;
}
if(timeout >= I2C_TIMEOUT_MAX)
return I2C_BUSBUSY_FAIL;
return I2C_OK;
}
int16_t I2C_ok_transmit()
{
uint32_t timeout = 0;
while((i2c->MDR & I2C_MASTER)
&& timeout < I2C_TIMEOUT_MAX)
{
timeout++;
}
if(timeout >= I2C_TIMEOUT_MAX)
return I2C_TIMEOUT_FAIL;
return I2C_OK;
}
int16_t I2C_reset()
{
if(!I2C_get_mutex())
return I2C_MUTEX_FAIL;
uint8_t i,j;
vTaskSuspendAll();
do {
i2c->MDR = (uint32_t)((uint32_t)0U << 5U);
i2c->PFNC = (0x01);
i2c->DIR = (0x01);
for(i = 0; i < 10; i++) {
i2c->DOUT = i2c->DOUT | 0x01;
for(j=0;j<300;j--);
i2c->DOUT ^= i2c->DOUT;
for(j=0;j<300;j--);
}
I2C_num_resets++;
} while(i2c->DIN & 0x02 != 0x02 && I2C_num_resets < I2C_MAX_RESETS);
if(I2C_num_resets >= 5) {
xSemaphoreGive(I2CMutex);
while(1);
}
I2C_init(i2c);
xTaskResumeAll();
return I2C_OK;
}
bool I2C_get_mutex()
{
bool got_mutex = false;
uint8_t mutex_tries = 0;
while(!got_mutex && mutex_tries < I2C_MUTEX_ATTEMPTS) {
got_mutex = xSemaphoreTake(I2CMutex, I2C_MUTEX_WAIT);
mutex_tries++;
if(!got_mutex)
vTaskDelay(10);
}
return got_mutex;
}
int16_t I2C_set_slave_addr(uint32_t addr) {
if(!I2C_get_mutex())
return I2C_MUTEX_FAIL;
i2cSetSlaveAdd(i2c, addr);
xSemaphoreGive(I2CMutex);
return I2C_OK;
}