-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
544 lines (489 loc) · 16.5 KB
/
main.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
This code was originally written for the SAMA5D2, but has been modified to work with the SAMC21.
*/
/* ----------------------------------------------------------------------------
* SAM Software Package License
* ----------------------------------------------------------------------------
* Copyright (c) 2015, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/**
* \page can Controller Area Network with Flexible Data-rate.
* An example using the MCAN peripheral of SAMA5D2x Microcontrollers.
*
* \section Purpose
*
* The Controller Area Network example will help new users get familiar
* with the MCAN peripheral used in Atmel's SAMA5D2x microcontrollers.
*
* \section Requirements
*
* This package is compatible with the evaluation boards listed below:
* - SAMC21-XPRO
*
* \section Description
*
* The demonstration program transmits several different CAN messages. Since it
* also enables the CAN loopback mode, the transmitted messages are reflected
* back as received messages too.
* Both standard CAN and CAN FD operation can be tested.
*
* \section Usage
*
* -# Build the program and download it inside the evaluation board. Please
* refer to the
* <a href="http://www.atmel.com/dyn/resources/prod_documents/6421B.pdf">
* SAM-BA User Guide</a>, the
* <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6310.pdf">
* GNU-Based Software Development</a>
* application note or to the
* <a href="ftp://ftp.iar.se/WWWfiles/arm/Guides/EWARM_UserGuide.ENU.pdf">
* IAR EWARM User Guide</a>,
* depending on your chosen solution.
* -# On the computer, open and configure a terminal application
* (e.g. HyperTerminal on Microsoft Windows) with these settings:
* - 115200 bauds
* - 8 bits of data
* - No parity
* - 1 stop bit
* - No flow control
* -# In the terminal window, the
* following text should appear (values depend on the board and chip used):
* \code
* -- Controller Area Network (CAN) Example xxx --
* -- xxxxxx-xx
* -- Compiled: xxx xx xxxx xx:xx:xx --
*
* CAN menu:
* Press [c|f|s] to set CAN or CAN FD mode
* Press [l] to toggle the integrated MCAN loop-back on/off
* p: Send sample messages
* h: Display this menu
* \endcode
* -# Input command according to the menu.
*
* \section References
* - mcan/main.c
* - mcan.h
* - mcan.h
*/
/** \file
*
* This file contains all the specific code for the CAN example.
*/
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include <stdint.h>
#include "chip.h"
#include "compiler.h"
#include "mcan.h"
#include "console.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
/*----------------------------------------------------------------------------
* Local definitions
*----------------------------------------------------------------------------*/
/* size of our custom Rx and Tx Buffer Elements, in words */
#define RAM_BUF_SIZE (MCAN_RAM_BUF_HDR_SIZE + 64u / 4)
#define RAM_ARRAY_SIZE_FILT_STD (8u)
#define RAM_ARRAY_SIZE_FILT_EXT (8u)
#define RAM_FIFO_SIZE_RX0 (12u)
/* no Rx FIFO 1 in our Message RAM */
#define RAM_ARRAY_SIZE_RX (4u)
/* no Tx Event FIFO in our Message RAM */
#define RAM_ARRAY_SIZE_TX (4u)
#define RAM_FIFO_SIZE_TX (4u)
/* size of our custom Message RAM, in words */
#define MSG_RAM_SIZE ( \
RAM_ARRAY_SIZE_FILT_STD * MCAN_RAM_FILT_STD_SIZE \
+ RAM_ARRAY_SIZE_FILT_EXT * MCAN_RAM_FILT_EXT_SIZE \
+ RAM_FIFO_SIZE_RX0 * RAM_BUF_SIZE \
+ RAM_ARRAY_SIZE_RX * RAM_BUF_SIZE \
+ RAM_ARRAY_SIZE_TX * RAM_BUF_SIZE \
+ RAM_FIFO_SIZE_TX * RAM_BUF_SIZE )
#define MSG_LEN_1_CAN 8
#define MSG_LEN_1_CAN_FD 64
#define MSG_LEN_2_CAN 7
#define MSG_LEN_2_CAN_FD 48
#define MSG_ID_2 (CAN_STD_MSG_ID | 0x444)
#define MSG_ID_2_MASK 0x7FCul /* bits 0 & 1 are don't care */
#define RX_BUFFER_0 0
#define RX_BUFFER_1 1
#define FILTER_0 0
#define FILTER_1 1
struct frame_desc
{
uint32_t id;
uint8_t data[64];
uint8_t len;
uint8_t buf_idx;
};
/*----------------------------------------------------------------------------
* Local variables
*----------------------------------------------------------------------------*/
/* the Message RAM is allocated from within the SAMC21's RAM
*/
static uint32_t mcan_msg_ram[MSG_RAM_SIZE] __attribute__ ((aligned (4)));
struct frame_desc samples[3] = {
{
.id = CAN_STD_MSG_ID | 0x555,
.buf_idx = 0,
.len = 1,
.data = { 0x81 },
},
{
.id = CAN_EXT_MSG_ID | 0x11111111,
.buf_idx = 1,
.len = MSG_LEN_1_CAN_FD,
.data = { 0x55, 0x55, 0x55, 0x55, 0xff, 0x00, 0xff, 0x00,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
},
},
{
.id = MSG_ID_2,
.len = MSG_LEN_2_CAN_FD,
.data = { 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24,
0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32,
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
},
},
};
static struct mcan_set mcan;
static enum mcan_can_mode can_mode;
static bool loop_back, bus_off;
static volatile bool rx_ded_buffer_data = false;
uint8_t * txMailbox0;
uint8_t * txMailbox1;
/*----------------------------------------------------------------------------
* Local functions
*----------------------------------------------------------------------------*/
/**
* \brief Trace buffer contents in hex.
*/
static void print_buffer(uint32_t len, const uint8_t *data)
{
const uint8_t * const data_nxt = data + len;
for ( ; data < data_nxt; data++)
printf(" %02x", *data);
printf("\n\r");
}
/**
* \brief Handler for interrupt line 1 of MCANx.
*/
void CAN0_Handler(void)
{
if (mcan_rx_array_data(&mcan)) {
mcan_clear_rx_array_flag(&mcan);
rx_ded_buffer_data = true;
}
}
void CAN1_Handler(void)
{
if (mcan_rx_array_data(&mcan)) {
mcan_clear_rx_array_flag(&mcan);
rx_ded_buffer_data = true;
}
}
/**
* \brief Transmit sample messages.
*/
static void send_message(void)
{
/* Send standard ID from a dedicated buffer */
/* Write data into CAN mailbox */
memcpy(txMailbox0, samples[0].data, samples[0].len);
/* Send data */
mcan_send_tx_buffer(&mcan, samples[0].buf_idx);
/* Send extended ID from a dedicated buffer */
if (can_mode > MCAN_MODE_CAN)
samples[1].len = MSG_LEN_1_CAN_FD;
else
samples[1].len = MSG_LEN_1_CAN;
txMailbox1 = mcan_prepare_tx_buffer(&mcan, samples[1].buf_idx,
samples[1].id, samples[1].len);
/* Write data into CAN mailbox */
memcpy(txMailbox1, samples[1].data, samples[1].len);
/* Send data */
mcan_send_tx_buffer(&mcan, samples[1].buf_idx);
/* Send from TX FIFO */
if (can_mode > MCAN_MODE_CAN)
samples[2].len = MSG_LEN_2_CAN_FD;
else
samples[2].len = MSG_LEN_2_CAN;
samples[2].buf_idx = mcan_enqueue_outgoing_msg(&mcan, samples[2].id,
samples[2].len, samples[2].data);
printf("-I- Sending messages from transmit buffers %u, %u and %u.\n\r",
samples[0].buf_idx, samples[1].buf_idx, samples[2].buf_idx);
}
/**
* \brief Print potential MCAN events out.
*/
static void watch_mcan(void)
{
uint8_t msg_data[64];
struct mcan_msg_info msg = { .data = msg_data };
struct frame_desc *sample = NULL;
uint8_t fifo_entries;
bool need_to_service_rx;
/* Expect TX'd message complete */
if (mcan_is_tx_complete(&mcan)) {
mcan_clear_tx_flag(&mcan);
for (sample = samples; sample < samples + ARRAY_SIZE(samples);
sample++) {
if (!mcan_is_buffer_sent(&mcan, sample->buf_idx))
continue;
printf("-I- Transmit buffer %u sent: ID %lx, data",
sample->buf_idx, mcan_get_id(sample->id));
print_buffer(sample->len, sample->data);
}
}
/* check if ISR has fired */
__disable_irq();
if ((need_to_service_rx = rx_ded_buffer_data))
rx_ded_buffer_data = false;
__enable_irq();
if (need_to_service_rx) {
if (mcan_rx_buffer_data(&mcan, RX_BUFFER_0)) {
msg.data_len = sizeof(msg_data);
mcan_read_rx_buffer(&mcan, RX_BUFFER_0, &msg);
printf("-I- Message in receive buffer 0: ID %lx"
", data", mcan_get_id(msg.id));
print_buffer(msg.data_len, msg.data);
}
if (mcan_rx_buffer_data(&mcan, RX_BUFFER_1)) {
msg.data_len = sizeof(msg_data);
mcan_read_rx_buffer(&mcan, RX_BUFFER_1, &msg);
printf("-I- Message in receive buffer 1: ID %lx"
", data", mcan_get_id(msg.id));
print_buffer(msg.data_len, msg.data);
}
}
/* Poll for new CAN messages in RX FIFO */
do {
msg.data_len = sizeof(msg_data);
fifo_entries = mcan_dequeue_received_msg(&mcan, 0, &msg);
if (fifo_entries > 0) {
printf("-I- Message from receive FIFO 0: ID %lx"
", data", mcan_get_id(msg.id));
print_buffer(msg.data_len, msg.data);
}
} while (fifo_entries > 1);
/* Warn if the MCAN is not in its Normal Operation state anymore */
if (!bus_off && !mcan_is_enabled(&mcan)) {
printf("-W- MCAN has gone Bus_Off.\n\r");
/* Warn only once */
bus_off = true;
}
}
/**
* \brief Display main menu.
*/
static void display_menu(void)
{
uint8_t chk_box[3];
printf("\n\rCAN menu:\n\r");
printf("Press [c|f|s] to set CAN or CAN FD mode\n\r");
chk_box[0] = (can_mode == MCAN_MODE_CAN) ? 'X' : ' ';
chk_box[1] = (can_mode == MCAN_MODE_EXT_LEN_CONST_RATE) ? 'X' : ' ';
chk_box[2] = (can_mode == MCAN_MODE_EXT_LEN_DUAL_RATE) ? 'X' : ' ';
printf(" c: [%c] ISO 11898-1 CAN\n\r", chk_box[0]);
printf(" f: [%c] ISO 11898-7 CAN FD, 64-byte data\n\r", chk_box[1]);
printf(" s: [%c] ISO 11898-7 CAN FD, 64-byte data, 1 Mbps data bit"
" rate\n\r", chk_box[2]);
printf("Press [l] to toggle the integrated MCAN loop-back on/off\n\r");
chk_box[0] = loop_back ? 'X' : ' ';
printf(" l: [%c] Integrated CANTX->CANRX loop-back\n\r", chk_box[0]);
printf(" p: Send sample messages\n\r");
printf(" h: Display this menu\n\r");
printf("\n\r");
}
/*----------------------------------------------------------------------------
* Exported functions
*----------------------------------------------------------------------------*/
/**
* \brief CAN Application entry point.
*
* \return Unused (ANSI-C compatibility).
*/
int main(void)
{
const struct mcan_config mcan_cfg = {
#if 1
.id = ID_CAN0,
.regs = CAN0,
#else
.id = ID_CAN1,
.regs = CAN1,
#endif
.msg_ram = mcan_msg_ram,
.array_size_filt_std = RAM_ARRAY_SIZE_FILT_STD,
.array_size_filt_ext = RAM_ARRAY_SIZE_FILT_EXT,
.fifo_size_rx0 = RAM_FIFO_SIZE_RX0,
.fifo_size_rx1 = 0,
.array_size_rx = RAM_ARRAY_SIZE_RX,
.fifo_size_tx_evt = 0,
.array_size_tx = RAM_ARRAY_SIZE_TX,
.fifo_size_tx = RAM_FIFO_SIZE_TX,
.buf_size_rx_fifo0 = 64,
.buf_size_rx_fifo1 = 0,
.buf_size_rx = 64,
.buf_size_tx = 64,
/*
using values from AT6493 (SAMC21 app note); the plus values are to add on what the MCAN driver subtracts back off
*/
.bit_rate = 500000,
.quanta_before_sp = 10 + 2,
.quanta_after_sp = 3 + 1,
.quanta_sync_jump = 3 + 1,
/*
AT6493 (SAMC21 app note) 'fast' values were unhelpfully the same as normal speed; these are for double (1MBit)
the maximum peripheral clock of 48MHz on the SAMC21 does restrict us from very high rates
*/
.bit_rate_fd = 1000000,
.quanta_before_sp_fd = 10 + 2,
.quanta_after_sp_fd = 3 + 1,
.quanta_sync_jump_fd = 3 + 1,
};
uint32_t mcan_msg_ram_size = ARRAY_SIZE(mcan_msg_ram);
uint8_t user_key;
/* Output example information */
console_example_info("CAN Example");
/*
instead of using Atmel's convoluted and bloated Atmel Software Framework to configure pins, we just efficiently write the needed registers
*/
switch (mcan_cfg.id)
{
case ID_CAN0:
PORT->Group[0].DIRSET.reg = PORT_PA24;
PORT->Group[0].DIRCLR.reg = PORT_PA25;
PORT->Group[0].PINCFG[24].reg = PORT_PINCFG_INEN | PORT_PINCFG_PMUXEN;
PORT->Group[0].PINCFG[25].reg = PORT_PINCFG_INEN | PORT_PINCFG_PMUXEN;
PORT->Group[0].PMUX[24 / 2].reg = PORT_PMUX_PMUXE(6 /* CAN0 G */) | PORT_PMUX_PMUXO(6 /* CAN0 G */); /* have to write odd and even at once */
GCLK->PCHCTRL[CAN0_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0;
MCLK->AHBMASK.reg |= MCLK_AHBMASK_CAN0;
NVIC_EnableIRQ(CAN0_IRQn);
break;
case ID_CAN1:
PORT->Group[1].DIRSET.reg = PORT_PB14;
PORT->Group[1].DIRCLR.reg = PORT_PB15;
PORT->Group[1].PINCFG[14].reg = PORT_PINCFG_INEN | PORT_PINCFG_PMUXEN;
PORT->Group[1].PINCFG[15].reg = PORT_PINCFG_INEN | PORT_PINCFG_PMUXEN;
PORT->Group[1].PMUX[14 / 2].reg = PORT_PMUX_PMUXE(6 /* CAN0 G */) | PORT_PMUX_PMUXO(6 /* CAN0 G */); /* have to write odd and even at once */
GCLK->PCHCTRL[CAN1_GCLK_ID].reg = GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN_GCLK0;
MCLK->AHBMASK.reg |= MCLK_AHBMASK_CAN1;
NVIC_EnableIRQ(CAN1_IRQn);
break;
}
__enable_irq();
if (!mcan_configure_msg_ram(&mcan_cfg, &mcan_msg_ram_size))
goto Error;
if (mcan_msg_ram_size > ARRAY_SIZE(mcan_msg_ram)) {
printf("-E- Message RAM too small. Please verify its configuration.\n\r");
goto Error;
}
else if (mcan_msg_ram_size != ARRAY_SIZE(mcan_msg_ram))
printf("-I- The Message RAM is larger than required.\n\r");
/* Display menu */
can_mode = MCAN_MODE_CAN;
loop_back = true;
display_menu();
if (!mcan_initialize(&mcan, &mcan_cfg))
goto Error;
mcan_set_mode(&mcan, can_mode);
mcan_init_loopback(&mcan);
mcan_enable(&mcan);
bus_off = false;
mcan_enable_rx_array_flag(&mcan, 1);
txMailbox0 = mcan_prepare_tx_buffer(&mcan, samples[0].buf_idx,
samples[0].id, samples[0].len);
mcan_filter_single_id(&mcan, RX_BUFFER_0, FILTER_0, samples[0].id);
mcan_filter_single_id(&mcan, RX_BUFFER_1, FILTER_0, samples[1].id);
mcan_filter_id_mask(&mcan, 0, FILTER_1, samples[2].id, MSG_ID_2_MASK);
while (true) {
watch_mcan();
if (!console_is_rx_ready())
continue;
user_key = tolower(console_get_char());
switch (user_key) {
case 'c':
case 'f':
case 's':
can_mode = user_key == 'f' ? MCAN_MODE_EXT_LEN_CONST_RATE :
(user_key == 's' ? MCAN_MODE_EXT_LEN_DUAL_RATE :
MCAN_MODE_CAN);
mcan_disable(&mcan);
mcan_reconfigure(&mcan);
mcan_set_mode(&mcan, can_mode);
mcan_enable(&mcan);
bus_off = false;
display_menu();
break;
case 'l':
loop_back = loop_back ? false : true;
mcan_disable(&mcan);
if (loop_back)
mcan_loopback_on(&mcan);
else
mcan_loopback_off(&mcan);
mcan_enable(&mcan);
bus_off = false;
display_menu();
break;
case 'h':
display_menu();
break;
case 'p':
if (!mcan_is_enabled(&mcan)) {
mcan_enable(&mcan);
bus_off = false;
if (!mcan_is_enabled(&mcan))
printf("-E- Failed to re-enable MCAN.\n\r");
}
/* Demonstrate ID filtering */
samples[2].id = MSG_ID_2
+ ((samples[2].id - MSG_ID_2 + 1) & 0x007);
/* Also, transmit each time different data. */
samples[2].data[0]++;
send_message();
break;
}
}
Error:
printf("-E- Fatal error encountered. Program halted.\n\r");
while (true) ;
/* This code is never reached */
}