-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp2517fd.cpp
308 lines (284 loc) · 9.59 KB
/
mcp2517fd.cpp
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
#include "mcp2517fd.hpp"
mcp2517fdError_t mcp2517fd::init(const mcp2517fdConfig_t* const config) {
//if ((!spi_exchange) || (!spi_select_slave) || (!spi_deselect_slave) || !(delayMs)) {
//if (debugLog) {
debugLog("Function pointer in null.\n");
//
//}
//return enMCP2517FD_ErrorFpIsNull;
//}
if (config == NULL) {
//if (debugLog) {
debugLog("Null configuration passed.\n");
//}
return enMCP2517FD_ErrorNullArg;
}
if (isMCP2517FDConfigured) {
//if (debugLog) {
debugLog("Already configured.\n");
//}
return enMCP2517FD_ErrorAlreadyConfigured;
}
// Set MCP2517FD in configuration mode..
mcp2517fdError_t error = setMode(enMCP2517FD_ModeConfiguration);
if (error != enMCP2517FD_ErrorNoError) {
return error;
}
//if (debugLog) {
debugLog("Device is in cofiguration mode.\n");
//}
mcp2517fd_reset();
//if (debugLog) {
debugLog("Reset done.\n");
//}
uint8_t temp[255];
uint8_t read[255];
memset(temp,0xAA,sizeof(temp));
memset(read,0,sizeof(read));
for (uint16_t offset = 0 ; offset < 0x800 ; offset += 255) {
mcp2517fd_write(RAM_OFFSET + offset, temp, sizeof(temp), false) ;
mcp2517fd_read(RAM_OFFSET + offset, read, sizeof(read), false) ;
if (memcmp(temp, read, sizeof(temp)) != 0) {
//if (debugLog) {
debugLog("RAM test failed.\n");
//}
}
}
//if (debugLog) {
debugLog("RAM reset done.\n");
//}
// mcp2517fd is in configuration mode ...
mcp2517fdMode_t mode;
getMode(&mode);
if (mode != enMCP2517FD_ModeConfiguration)
while(1);
// do clock setup
uint8_t setup = 0; // PLL bit : 1 to Enable 0 to diable..
//! Enable PLL is not working...
uint16_t retry = 0;
mcp2517fd_write(OSC_ADDRESS, &setup, 1, false); // Enable PLL // TODO clock setup based on user input
// wait till pll is ready
setup = 0;
do {
mcp2517fd_read(OSC_ADDRESS + 0x1, &setup, 1, false);
/*if ((setup & PLLRDY) == 1) {
//if (debugLog) {
debugLog("PLL is ready.\n");
//}
break;
}*/
if ((setup >> 2) == 1) {
//if (debugLog) {
debugLog("Clock is stable and ready.\n");
//}
break;
}
} while (retry++ <= RETRY_COUNT);
if (retry >= RETRY_COUNT) {
/*//if (debugLog) {
debugLog("PLL timeout.\n");
}*/
//if (debugLog) {
debugLog("Clock is not stable and ready.\n");
//}
return enMCP2517FD_ErrorTimeout;
}
// Reset RAM
memset(temp,0,sizeof(temp));
for (uint16_t offset = 0 ; offset < 0x800 ; offset += 255) {
mcp2517fd_write(0x400 + offset, temp, sizeof(temp), false) ;
}
//if (debugLog) {
debugLog("RAM reset done.\n");
//}
// Configure ISO CRC Enable bit
setup = 1 << 6 ; // PXEDIS
if (config->crc) {
setup |= 1 << 5 ; // Enable ISO CRC in CAN FD Frames bit
}
mcp2517fd_write(CONFIG_ADDRESS, &setup, 1, false);
// Configure TXQ and TXAT
setup = config->retransmission ? 2 << 6 : ~(3 << 6);
mcp2517fd_write(TXQCON_ADDRESS + 0x2, &setup, 1, false);
setup = config->payload << 5 | 0x1F << 0; //? Fifo message deep...
mcp2517fd_write(TXQCON_ADDRESS + 0x3, &setup, 1, false);
setup = config->retransmission ? 1 : 0;
setup |= 1 << 4;
mcp2517fd_write(CONFIG_ADDRESS + 0x2, &setup, 1, false);
// Set FIFO size based on user input...
// TODO There are total 32 fifo handler
// lets 24 fifo be configured as RX fifo and remaining will be tx
// Configure RX FIFO
setup = 1 << 2;
mcp2517fd_write(TXQCON_ADDRESS + 1, &setup, 1, false);
setup = config->rxFifo << 5 | 0x1F << 0;
mcp2517fd_write(FIFO_BASE_ADDRESS + RX_FIFO + 0x3, &setup, 1, false);
setup = 1 << 3 | 1 << 2 | 1 << 0; // TFNRFNIE : rxFifo not empty , TFNRFFIE : rxFifo full , RXOVIE : overflow enable
mcp2517fd_write(FIFO_BASE_ADDRESS + RX_FIFO, &setup, 1, false);
// Configure TX FIFO
setup = config->txFifo << 5 | 0x1F << 0;
mcp2517fd_write(FIFO_BASE_ADDRESS + TX_FIFO + 0x3, &setup, 1, false);
/*setup = 1 << 4; // TXATIE
mcp2517fd_write(FIFO_BASE_ADDRESS + TX_FIFO, &setup, 1, false);*/
setup = 1 << 7 | 1 << 4; // TXEN : Tx fifo , TXATIE : enable interrupt
mcp2517fd_write(FIFO_BASE_ADDRESS + TX_FIFO, &setup, 1, false);
//if (debugLog) {
debugLog("Fifo config done.\n");
//}
// Configure receive filters
// set allow all filter user can set by calling setFilter api...
// Activate interrupts
setup = 1 << 1 | 1 << 0; // TXIF : Tx interrupt Enable , RXIF : Rx interrupt Enable
mcp2517fd_write(INT_ADDRESS + 0x2, &setup, 1, false);
setup = 1 << 2; // TXATIE : Transmit Attempt Interrupt Enable bit
mcp2517fd_write(INT_ADDRESS + 0x3, &setup, 1, false);
//if (debugLog) {
debugLog("Enable interrupts.\n");
//}
// Program nominal bit rate
// mcp2517fd_write(NBTCFG_ADDRESS, &setup, 1, false);
// Program data bit rate
isMCP2517FDConfigured = true;
//if (debugLog) {
debugLog("Configuration done.\n");
//}
return enMCP2517FD_ErrorNoError;
}
mcp2517fdError_t mcp2517fd::setGpioMode(const int pin, const bool isInput) {
if (!isMCP2517FDConfigured) {
//if (debugLog) {
debugLog("Not configured.\n");
//}
return enMCP2517FD_ErrorNotConfigured;
}
if (pin <= GPIO1) {
uint8_t mode = 0;
mcp2517fd_read(IOCON_ADDRESS, &mode, 1, false);
if (isInput) {
SET_BIT(mode, pin);
} else {
CLEAR_BIT(mode, pin);
}
mcp2517fd_write(IOCON_ADDRESS, &mode, 1, false);
return enMCP2517FD_ErrorNoError;
} else {
return enMCP2517FD_ErrorInvalidArg;
}
}
mcp2517fdError_t mcp2517fd::getGpioMode(const int pin, uint8_t* const mode) {
if (!isMCP2517FDConfigured) {
//if (debugLog) {
debugLog("Not configured.\n");
//}
return enMCP2517FD_ErrorNotConfigured;
}
if (pin <= GPIO1) {
uint8_t gmode = 0;
mcp2517fd_read(IOCON_ADDRESS, &gmode, 1, false);
if (READ_BIT(gmode, pin)) {
*mode = INPUT_MODE;
} else {
*mode = OUTPUT_MODE;
}
return enMCP2517FD_ErrorNoError;
} else {
return enMCP2517FD_ErrorInvalidArg;
}
}
mcp2517fdError_t mcp2517fd::writeGpio(const int pin, const bool enable) {
if (!isMCP2517FDConfigured) {
//if (debugLog) {
debugLog("Not configured.\n");
//}
return enMCP2517FD_ErrorNotConfigured;
}
if (pin <= GPIO1) {
uint8_t state = 0;
mcp2517fd_read(IOCON_ADDRESS + 0x1, &state, 1, false);
if (enable) {
SET_BIT(state, pin);
} else {
CLEAR_BIT(state, pin);
}
mcp2517fd_write(IOCON_ADDRESS + 0x1, &state, 1, false);
return enMCP2517FD_ErrorNoError;
} else {
return enMCP2517FD_ErrorInvalidArg;
}
}
mcp2517fdError_t mcp2517fd::readGpio(const int pin, bool* const state) {
if (!isMCP2517FDConfigured) {
//if (debugLog) {
debugLog("Not configured.\n");
//}
return enMCP2517FD_ErrorNotConfigured;
}
if (pin <= GPIO1) {
uint8_t gstate = 0;
mcp2517fd_read(IOCON_ADDRESS + 0x2, &gstate, 1, false);
if (READ_BIT(gstate, pin)) {
*state = true;
} else {
*state = false;
}
return enMCP2517FD_ErrorNoError;
} else {
return enMCP2517FD_ErrorInvalidArg;
}
}
bool mcp2517fd::isBusy() {
if (!isMCP2517FDConfigured) {
//if (debugLog) {
debugLog("Not configured.\n");
//}
return true;
}
uint8_t isbusy = 0;
mcp2517fd_read(IOCON_ADDRESS + 0x1, &isbusy, 1, false);
return (READ_BIT(isbusy, 3)) ? true : false;
}
mcp2517fdError_t mcp2517fd::setMode(const mcp2517fdMode_t mode) {
// Set MCP2517FD in user specified mode..
uint8_t config_set_value = (mode | 1 << 3);
uint8_t config_bit = 0;
uint16_t retry = 0;
mcp2517fd_write(CONFIG_ADDRESS + 0x3, &config_set_value, 1, false); // Abort all transmission and set to user specific mode..
do {
mcp2517fd_read(CONFIG_ADDRESS + 0x2, &config_bit, 1, false); // checking for config mode bit
if ((config_bit >> 5) == mode) {
break;
}
} while (retry++ != RETRY_COUNT);
if (retry >= RETRY_COUNT) {
//if (debugLog) {
debugLog("Timeout error.\n");
//}
return enMCP2517FD_ErrorTimeout;
}
return enMCP2517FD_ErrorNoError;
}
mcp2517fdError_t mcp2517fd::getMode(mcp2517fdMode_t* mode) {
// Get MCP2517FD in user specified mode..
uint8_t config_bit = 0;
mcp2517fd_read(CONFIG_ADDRESS + 0x2, &config_bit, 1, false); // checking for config mode bit
*mode = (mcp2517fdMode_t)(config_bit >> 5);
return enMCP2517FD_ErrorNoError;
}
mcp2517fdError_t mcp2517fd::write(CANFDMessage_t* const message) {
if (message == NULL) {
//if (debugLog) {
debugLog("Null configuration passed.\n");
//}
return enMCP2517FD_ErrorNullArg;
}
return mcp2517fd_writeMessage(message, false);
}
mcp2517fdError_t mcp2517fd::read(CANFDMessage_t* const message) {
if (message == NULL) {
//if (debugLog) {
debugLog("Null configuration passed.\n");
//}
return enMCP2517FD_ErrorNullArg;
}
return mcp2517fd_readMessage(message, false);
}