-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathstm32f072.ino
443 lines (377 loc) · 15.5 KB
/
stm32f072.ino
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
#define DEBUG 1
#define AF4 0x04
#define AF0 0x00
/* Symbolic names for bit rate of CAN message */
typedef enum {CAN_50KBPS, CAN_100KBPS, CAN_125KBPS, CAN_250KBPS, CAN_500KBPS, CAN_1000KBPS} BITRATE;
/* Symbolic names for formats of CAN message */
typedef enum {STANDARD_FORMAT = 0, EXTENDED_FORMAT} CAN_FORMAT;
/* Symbolic names for type of CAN message */
typedef enum {DATA_FRAME = 0, REMOTE_FRAME} CAN_FRAME;
typedef struct
{
uint32_t id; /* 29 bit identifier */
uint8_t data[8]; /* Data field */
uint8_t len; /* Length of data field in bytes */
uint8_t ch; /* Object channel(Not use) */
uint8_t format; /* 0 - STANDARD, 1- EXTENDED IDENTIFIER */
uint8_t type; /* 0 - DATA FRAME, 1 - REMOTE FRAME */
} CAN_msg_t;
typedef const struct
{
uint8_t TS2;
uint8_t TS1;
uint8_t BRP;
} CAN_bit_timing_config_t;
CAN_bit_timing_config_t can_configs[6] = {{2, 13, 60}, {2, 13, 30}, {2, 13, 24}, {2, 13, 12}, {2, 13, 6}, {2, 13, 3}};
/**
* Print registers.
*/
void printRegister(char * buf, uint32_t reg) {
if (DEBUG == 0) return;
Serial.print(buf);
Serial.print("0x");
Serial.print(reg, HEX);
Serial.println();
}
/**
* Initializes the CAN GPIO registers.
*
* @params: addr - Specified GPIO register address.
* @params: index - Specified GPIO index.
* @params: afry - Specified Alternative function selection AF0-AF15.
* @params: speed - Specified OSPEEDR register value.(Optional)
*
*/
void CANSetGpio(GPIO_TypeDef * addr, uint8_t index, uint8_t afry, uint8_t speed = 3) {
uint8_t _index2 = index * 2;
uint8_t _index4 = index * 4;
uint8_t ofs = 0;
uint8_t setting;
if (index > 7) {
_index4 = (index - 8) * 4;
ofs = 1;
}
uint32_t mask;
printRegister("GPIO_AFR(b)=", addr->AFR[1]);
mask = 0xF << _index4;
addr->AFR[ofs] &= ~mask; // Reset alternate function
//setting = 0x9; // AF9
setting = afry; // Alternative function selection
mask = setting << _index4;
addr->AFR[ofs] |= mask; // Set alternate function
printRegister("GPIO_AFR(a)=", addr->AFR[1]);
printRegister("GPIO_MODER(b)=", addr->MODER);
mask = 0x3 << _index2;
addr->MODER &= ~mask; // Reset mode
setting = 0x2; // Alternate function mode
mask = setting << _index2;
addr->MODER |= mask; // Set mode
printRegister("GPIO_MODER(a)=", addr->MODER);
printRegister("GPIO_OSPEEDR(b)=", addr->OSPEEDR);
mask = 0x3 << _index2;
addr->OSPEEDR &= ~mask; // Reset speed
setting = speed;
mask = setting << _index2;
addr->OSPEEDR |= mask; // Set speed
printRegister("GPIO_OSPEEDR(a)=", addr->OSPEEDR);
printRegister("GPIO_OTYPER(b)=", addr->OTYPER);
mask = 0x1 << index;
addr->OTYPER &= ~mask; // Reset Output push-pull
printRegister("GPIO_OTYPER(a)=", addr->OTYPER);
printRegister("GPIO_PUPDR(b)=", addr->PUPDR);
mask = 0x3 << _index2;
addr->PUPDR &= ~mask; // Reset port pull-up/pull-down
printRegister("GPIO_PUPDR(a)=", addr->PUPDR);
}
/**
* Initializes the CAN filter registers.
*
* The bxCAN provides up to 14 scalable/configurable identifier filter banks for selecting the incoming messages the software needs and discarding the others.
*
* @preconditions - This register can be written only when the filter initialization mode is set (FINIT=1) in the CAN_FMR register.
* @params: index - Specified filter index. index 27:14 are available in connectivity line devices only.
* @params: scale - Select filter scale.
* 0: Dual 16-bit scale configuration
* 1: Single 32-bit scale configuration
* @params: mode - Select filter mode.
* 0: Two 32-bit registers of filter bank x are in Identifier Mask mode
* 1: Two 32-bit registers of filter bank x are in Identifier List mode
* @params: fifo - Select filter assigned.
* 0: Filter assigned to FIFO 0
* 1: Filter assigned to FIFO 1
* @params: bank1 - Filter bank register 1
* @params: bank2 - Filter bank register 2
*
*/
void CANSetFilter(uint8_t index, uint8_t scale, uint8_t mode, uint8_t fifo, uint32_t bank1, uint32_t bank2) {
if (index > 13) return;
CAN->FA1R &= ~(0x1UL<<index); // Deactivate filter
if (scale == 0) {
CAN->FS1R &= ~(0x1UL<<index); // Set filter to Dual 16-bit scale configuration
} else {
CAN->FS1R |= (0x1UL<<index); // Set filter to single 32 bit configuration
}
if (mode == 0) {
CAN->FM1R &= ~(0x1UL<<index); // Set filter to Mask mode
} else {
CAN->FM1R |= (0x1UL<<index); // Set filter to List mode
}
if (fifo == 0) {
CAN->FFA1R &= ~(0x1UL<<index); // Set filter assigned to FIFO 0
} else {
CAN->FFA1R |= (0x1UL<<index); // Set filter assigned to FIFO 1
}
CAN->sFilterRegister[index].FR1 = bank1; // Set filter bank registers1
CAN->sFilterRegister[index].FR2 = bank2; // Set filter bank registers2
CAN->FA1R |= (0x1UL<<index); // Activate filter
}
/**
* Initializes the CAN controller with specified bit rate.
*
* @params: bitrate - Specified bitrate. If this value is not one of the defined constants, bit rate will be defaulted to 125KBS
* @params: remap - Select CAN port.
* =0:CAN_RX mapped to PA11, CAN_TX mapped to PA12
* =1:Not used
* =2:CAN_RX mapped to PB8, CAN_TX mapped to PB9 (not available on 36-pin package)
* =3:CAN_RX mapped to PD0, CAN_TX mapped to PD1 (available on 100-pin and 144-pin package)
*
*/
bool CANInit(BITRATE bitrate, int remap)
{
// Reference manual
// https://www.st.com/resource/en/reference_manual/dm00031936-stm32f0x1stm32f0x2stm32f0x8-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
RCC->APB1ENR |= 0x2000000UL; // Enable CAN clock
if (remap == 0) {
RCC->AHBENR |= 0x20000UL; // Enable GPIOA clock
CANSetGpio(GPIOA, 11, AF4); // Set PA11 to AF4
CANSetGpio(GPIOA, 12, AF4); // Set PA12 to AF4
}
if (remap == 2) {
RCC->AHBENR |= 0x40000UL; // Enable GPIOB clock
CANSetGpio(GPIOB, 8, AF4); // Set PB8 to AF4
CANSetGpio(GPIOB, 9, AF4); // Set PB9 to AF4
}
if (remap == 3) {
RCC->AHBENR |= 0x100000UL; // Enable GPIOD clock
CANSetGpio(GPIOD, 0, AF0); // Set PD0 to AF0
CANSetGpio(GPIOD, 1, AF0); // Set PD1 to AF0
}
CAN->MCR |= 0x1UL; // Set CAN to Initialization mode
while (!(CAN->MSR & 0x1UL)); // Wait for Initialization mode
//CAN->MCR = 0x51UL; // Hardware initialization(No automatic retransmission)
CAN->MCR = 0x41UL; // Hardware initialization(With automatic retransmission)
// Set bit rates
//CAN1->BTR &= ~(((0x03) << 24) | ((0x07) << 20) | ((0x0F) << 16) | (0x1FF));
//CAN1->BTR |= (((can_configs[bitrate].TS2-1) & 0x07) << 20) | (((can_configs[bitrate].TS1-1) & 0x0F) << 16) | ((can_configs[bitrate].BRP-1) & 0x1FF);
CAN->BTR &= ~(((0x03) << 24) | ((0x07) << 20) | ((0x0F) << 16) | (0x3FF));
CAN->BTR |= (((can_configs[bitrate].TS2-1) & 0x07) << 20) | (((can_configs[bitrate].TS1-1) & 0x0F) << 16) | ((can_configs[bitrate].BRP-1) & 0x3FF);
printRegister("CAN->BTR=", CAN->BTR);
// Configure Filters to default values
CAN->FMR |= 0x1UL; // Set to filter initialization mode
// Set fileter 0
// Single 32-bit scale configuration
// Two 32-bit registers of filter bank x are in Identifier Mask mode
// Filter assigned to FIFO 0
// Filter bank register to all 0
CANSetFilter(0, 1, 0, 0, 0x0UL, 0x0UL);
CAN->FMR &= ~(0x1UL); // Deactivate initialization mode
uint16_t TimeoutMilliseconds = 1000;
bool can1 = false;
CAN->MCR &= ~(0x1UL); // Require CAN1 to normal mode
// Wait for normal mode
// If the connection is not correct, it will not return to normal mode.
for (uint16_t wait_ack = 0; wait_ack < TimeoutMilliseconds; wait_ack++) {
if ((CAN->MSR & 0x1UL) == 0) {
can1 = true;
break;
}
delayMicroseconds(1000);
}
//Serial.print("can1=");
//Serial.println(can1);
if (can1) {
Serial.println("CAN1 initialize ok");
} else {
Serial.println("CAN1 initialize fail!!");
return false;
}
return true;
}
#define STM32_CAN_TIR_TXRQ (1U << 0U) // Bit 0: Transmit Mailbox Request
#define STM32_CAN_RIR_RTR (1U << 1U) // Bit 1: Remote Transmission Request
#define STM32_CAN_RIR_IDE (1U << 2U) // Bit 2: Identifier Extension
#define STM32_CAN_TIR_RTR (1U << 1U) // Bit 1: Remote Transmission Request
#define STM32_CAN_TIR_IDE (1U << 2U) // Bit 2: Identifier Extension
#define CAN_EXT_ID_MASK 0x1FFFFFFFU
#define CAN_STD_ID_MASK 0x000007FFU
/**
* Decodes CAN messages from the data registers and populates a
* CAN message struct with the data fields.
*
* @preconditions - A valid CAN message is received
* @params CAN_rx_msg - CAN message structure for reception
*
*/
void CANReceive(CAN_msg_t* CAN_rx_msg)
{
uint32_t id = CAN->sFIFOMailBox[0].RIR;
if ((id & STM32_CAN_RIR_IDE) == 0) { // Standard frame format
CAN_rx_msg->format = STANDARD_FORMAT;;
CAN_rx_msg->id = (CAN_STD_ID_MASK & (id >> 21U));
}
else { // Extended frame format
CAN_rx_msg->format = EXTENDED_FORMAT;;
CAN_rx_msg->id = (CAN_EXT_ID_MASK & (id >> 3U));
}
if ((id & STM32_CAN_RIR_RTR) == 0) { // Data frame
CAN_rx_msg->type = DATA_FRAME;
}
else { // Remote frame
CAN_rx_msg->type = REMOTE_FRAME;
}
CAN_rx_msg->len = (CAN->sFIFOMailBox[0].RDTR) & 0xFUL;
CAN_rx_msg->data[0] = 0xFFUL & CAN->sFIFOMailBox[0].RDLR;
CAN_rx_msg->data[1] = 0xFFUL & (CAN->sFIFOMailBox[0].RDLR >> 8);
CAN_rx_msg->data[2] = 0xFFUL & (CAN->sFIFOMailBox[0].RDLR >> 16);
CAN_rx_msg->data[3] = 0xFFUL & (CAN->sFIFOMailBox[0].RDLR >> 24);
CAN_rx_msg->data[4] = 0xFFUL & CAN->sFIFOMailBox[0].RDHR;
CAN_rx_msg->data[5] = 0xFFUL & (CAN->sFIFOMailBox[0].RDHR >> 8);
CAN_rx_msg->data[6] = 0xFFUL & (CAN->sFIFOMailBox[0].RDHR >> 16);
CAN_rx_msg->data[7] = 0xFFUL & (CAN->sFIFOMailBox[0].RDHR >> 24);
// Release FIFO 0 output mailbox.
// Make the next incoming message available.
CAN->RF0R |= 0x20UL;
}
/**
* Encodes CAN messages using the CAN message struct and populates the
* data registers with the sent.
*
* @params CAN_tx_msg - CAN message structure for transmission
*
*/
void CANSend(CAN_msg_t* CAN_tx_msg)
{
volatile int count = 0;
uint32_t out = 0;
if (CAN_tx_msg->format == EXTENDED_FORMAT) { // Extended frame format
out = ((CAN_tx_msg->id & CAN_EXT_ID_MASK) << 3U) | STM32_CAN_TIR_IDE;
}
else { // Standard frame format
out = ((CAN_tx_msg->id & CAN_STD_ID_MASK) << 21U);
}
// Remote frame
if (CAN_tx_msg->type == REMOTE_FRAME) {
out |= STM32_CAN_TIR_RTR;
}
CAN->sTxMailBox[0].TDTR &= ~(0xF);
CAN->sTxMailBox[0].TDTR |= CAN_tx_msg->len & 0xFUL;
CAN->sTxMailBox[0].TDLR = (((uint32_t) CAN_tx_msg->data[3] << 24) |
((uint32_t) CAN_tx_msg->data[2] << 16) |
((uint32_t) CAN_tx_msg->data[1] << 8) |
((uint32_t) CAN_tx_msg->data[0] ));
CAN->sTxMailBox[0].TDHR = (((uint32_t) CAN_tx_msg->data[7] << 24) |
((uint32_t) CAN_tx_msg->data[6] << 16) |
((uint32_t) CAN_tx_msg->data[5] << 8) |
((uint32_t) CAN_tx_msg->data[4] ));
// Send Go
CAN->sTxMailBox[0].TIR = out | STM32_CAN_TIR_TXRQ;
// Wait until the mailbox is empty
while(CAN->sTxMailBox[0].TIR & 0x1UL && count++ < 1000000);
// The mailbox don't becomes empty while loop
if (CAN->sTxMailBox[0].TIR & 0x1UL) {
Serial.println("Send Fail");
Serial.println(CAN->ESR);
Serial.println(CAN->MSR);
Serial.println(CAN->TSR);
}
}
/**
* Returns whether there are CAN messages available.
*
* @returns If pending CAN messages are in the CAN controller
*
*/
uint8_t CANMsgAvail(void)
{
// Check for pending FIFO 0 messages
return CAN->RF0R & 0x3UL;
}
uint8_t counter = 0;
uint8_t frameLength = 0;
unsigned long previousMillis = 0; // stores last time output was updated
const long interval = 1000; // transmission interval (milliseconds)
void setup() {
Serial.begin(115200);
bool ret = CANInit(CAN_500KBPS, 0); // CAN_RX mapped to PA11, CAN_TX mapped to PA12
//bool ret = CANInit(CAN_500KBPS, 2); // CAN_RX mapped to PB8, CAN_TX mapped to PB9
//bool ret = CANInit(CAN_500KBPS, 3); // CAN_RX mapped to PD0, CAN_TX mapped to PD1
//bool ret = CANInit(CAN_1000KBPS, 0); // CAN_RX mapped to PA11, CAN_TX mapped to PA12
//bool ret = CANInit(CAN_1000KBPS, 2); // CAN_RX mapped to PB8, CAN_TX mapped to PB9
//bool ret = CANInit(CAN_1000KBPS, 3); // CAN_RX mapped to PD0, CAN_TX mapped to PD1
if (!ret) while(true);
}
void loop() {
CAN_msg_t CAN_TX_msg;
CAN_msg_t CAN_RX_msg;
CAN_TX_msg.data[0] = 0x00;
CAN_TX_msg.data[1] = 0x01;
CAN_TX_msg.data[2] = 0x02;
CAN_TX_msg.data[3] = 0x03;
CAN_TX_msg.data[4] = 0x04;
CAN_TX_msg.data[5] = 0x05;
CAN_TX_msg.data[6] = 0x06;
CAN_TX_msg.data[7] = 0x07;
CAN_TX_msg.len = frameLength;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if ( ( counter % 2) == 0) {
CAN_TX_msg.type = DATA_FRAME;
if (CAN_TX_msg.len == 0) CAN_TX_msg.type = REMOTE_FRAME;
CAN_TX_msg.format = EXTENDED_FORMAT;
CAN_TX_msg.id = 0x32F072;
} else {
CAN_TX_msg.type = DATA_FRAME;
if (CAN_TX_msg.len == 0) CAN_TX_msg.type = REMOTE_FRAME;
CAN_TX_msg.format = STANDARD_FORMAT;
CAN_TX_msg.id = 0x072;
}
CANSend(&CAN_TX_msg);
frameLength++;
if (frameLength == 9) frameLength = 0;
counter++;
}
if(CANMsgAvail()) {
CANReceive(&CAN_RX_msg);
if (CAN_RX_msg.format == EXTENDED_FORMAT) {
Serial.print("Extended ID: 0x");
if (CAN_RX_msg.id < 0x10000000) Serial.print("0");
if (CAN_RX_msg.id < 0x1000000) Serial.print("0");
if (CAN_RX_msg.id < 0x100000) Serial.print("0");
if (CAN_RX_msg.id < 0x10000) Serial.print("0");
if (CAN_RX_msg.id < 0x1000) Serial.print("0");
if (CAN_RX_msg.id < 0x100) Serial.print("0");
if (CAN_RX_msg.id < 0x10) Serial.print("0");
Serial.print(CAN_RX_msg.id, HEX);
} else {
Serial.print("Standard ID: 0x");
if (CAN_RX_msg.id < 0x100) Serial.print("0");
if (CAN_RX_msg.id < 0x10) Serial.print("0");
Serial.print(CAN_RX_msg.id, HEX);
Serial.print(" ");
}
Serial.print(" DLC: ");
Serial.print(CAN_RX_msg.len);
if (CAN_RX_msg.type == DATA_FRAME) {
Serial.print(" Data: ");
for(int i=0; i<CAN_RX_msg.len; i++) {
Serial.print("0x");
Serial.print(CAN_RX_msg.data[i], HEX);
if (i != (CAN_RX_msg.len-1)) Serial.print(" ");
}
Serial.println();
} else {
Serial.println(" Data: REMOTE REQUEST FRAME");
}
}
}