1 | C-Code
|
2 | #include <stdio.h>
|
3 | #include <string.h>
|
4 | #include "board.h"
|
5 | #include "sysclk.h"
|
6 | #include "exceptions.h"
|
7 | #include "uart.h"
|
8 | #include "pmc.h"
|
9 | #include "gpio.h"
|
10 | #include "can.h"
|
11 | #include "stdio_serial.h"
|
12 | #include "sn65hvd234.h"
|
13 | #include "conf_board.h"
|
14 | #include "conf_clock.h"
|
15 |
|
16 | //prototypes
|
17 | void showRegister(void);
|
18 |
|
19 |
|
20 | #define STRING_EOL "\r"
|
21 |
|
22 | #define STRING_HEADER "-- CAN Example --\r\n" \
|
23 | "-- "BOARD_NAME" --\r\n" \
|
24 | "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL
|
25 |
|
26 | #define CAN_MSG_TOGGLE_LED_0 0x11223344
|
27 | #define CAN_MSG_TOGGLE_LED_1 0xAABBCCDD
|
28 | #define CAN_MSG_DUMMY_DATA 0x55AAAA55
|
29 |
|
30 | /** CAN frame max data length */
|
31 | #define MAX_CAN_FRAME_DATA_LEN 8
|
32 |
|
33 | /** CAN0 Transceiver */
|
34 | sn65hvd234_ctrl_t can0_transceiver;
|
35 |
|
36 | /** CAN1 Transceiver */
|
37 | sn65hvd234_ctrl_t can1_transceiver;
|
38 |
|
39 | /** CAN0 Transfer mailbox structure */
|
40 | can_mb_conf_t can0_mailbox;
|
41 |
|
42 | /** CAN1 Transfer mailbox structure */
|
43 | can_mb_conf_t can1_mailbox;
|
44 |
|
45 | /** Receive status */
|
46 | volatile uint32_t g_ul_recv_status = 0;
|
47 | ]
|
48 |
|
49 | /**
|
50 | * \brief Default interrupt handler for CAN 1.
|
51 | */
|
52 |
|
53 | /*- - - - - - - - - - - - - - - - - - - - - - */
|
54 |
|
55 | void CAN1_Handler(void) // stellt sich tot
|
56 | {
|
57 | uint32_t ul_status;
|
58 | printf("\rDEBUG: \(1a\) in CAN1_Handler\r");
|
59 |
|
60 | ul_status = can_get_status(CAN1);
|
61 | printf("\rDEBUG: \(1a_a\) in CAN1_Handler\r");
|
62 | if (ul_status & GLOBAL_MAILBOX_MASK) {
|
63 | printf("\rDEBUG: \(1b\) in CAN1_Handler ul_status \& GLOBAL_MAILBOX_MASK\r");
|
64 | for (uint8_t i = 0; i < CANMB_NUMBER; i++) {
|
65 | ul_status = can_mailbox_get_status(CAN1, i);
|
66 | if ((ul_status & CAN_MSR_MRDY) == CAN_MSR_MRDY) {
|
67 | printf("\rDEBUG: \(1c\) in CAN1_Handler ul_status \& CAN_MSR_MRDY\r");
|
68 | can1_mailbox.ul_mb_idx = i;
|
69 | can1_mailbox.ul_status = ul_status;
|
70 | can_mailbox_read(CAN1, &can1_mailbox);
|
71 | g_ul_recv_status = 1;
|
72 | break;
|
73 | }
|
74 | }
|
75 | }
|
76 | }
|
77 | /*- - - - - - - - - - - - - - - - - - - - - - */
|
78 |
|
79 | /**
|
80 | * \brief Decode CAN messages.
|
81 | *
|
82 | * \param p_mailbox Pointer to CAN Mailbox structure.
|
83 | */
|
84 | static void decode_can_msg(can_mb_conf_t *p_mailbox)
|
85 | {
|
86 | int a;
|
87 | uint32_t ul_led_Ctrl = p_mailbox->ul_datal;
|
88 | if (p_mailbox->ul_datal == CAN_MSG_TOGGLE_LED_0) a=1;
|
89 |
|
90 |
|
91 | puts("CAN message:" STRING_EOL);
|
92 | if (ul_led_Ctrl == CAN_MSG_TOGGLE_LED_0) {
|
93 | puts(" Toggle LED 0" STRING_EOL);
|
94 | gpio_toggle_pin(LED0_GPIO); //PB27 PWM13 Arduino::13
|
95 | } else if (ul_led_Ctrl == CAN_MSG_TOGGLE_LED_1) {
|
96 | puts(" Toggle LED 1" STRING_EOL);
|
97 | gpio_toggle_pin(LED1_GPIO); //PC21 PWM9 Arduino::9
|
98 | }
|
99 | }
|
100 |
|
101 | /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
102 | /**
|
103 | * \brief Reset mailbox configure structure.
|
104 | *
|
105 | * \param p_mailbox Pointer to mailbox configure structure.
|
106 | */
|
107 | static void reset_mailbox_conf(can_mb_conf_t *p_mailbox)
|
108 | {
|
109 | p_mailbox->ul_mb_idx = 0;
|
110 | p_mailbox->uc_obj_type = 0;
|
111 | p_mailbox->uc_id_ver = 0;
|
112 | p_mailbox->uc_length = 0;
|
113 | p_mailbox->uc_tx_prio = 0;
|
114 | p_mailbox->ul_status = 0;
|
115 | p_mailbox->ul_id_msk = 0;
|
116 | p_mailbox->ul_id = 0;
|
117 | p_mailbox->ul_fid = 0;
|
118 | p_mailbox->ul_datal = 0;
|
119 | p_mailbox->ul_datah = 0;
|
120 | }
|
121 |
|
122 | #define TEST1_CAN_COMM_MB_IDX 0
|
123 | #define TEST1_CAN_TRANSFER_ID 0x07
|
124 | #define TEST1_CAN0_TX_PRIO 15
|
125 |
|
126 | /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
127 | /**
|
128 | * \brief Test the transmission from CAN0 Mailbox 0 to CAN1 Mailbox 0.
|
129 | */
|
130 | static void test_1(void)
|
131 | {
|
132 | can_reset_all_mailbox(CAN0);
|
133 | can_reset_all_mailbox(CAN1);
|
134 |
|
135 | printf("In Test1: CAN0 Mailbox 0 transmitting to CAN1 Mailbox 0\r");
|
136 |
|
137 |
|
138 |
|
139 | /* Init CAN1 Mailbox 0 to Reception Mailbox. */
|
140 | reset_mailbox_conf(&can1_mailbox);
|
141 | can1_mailbox.ul_mb_idx = TEST1_CAN_COMM_MB_IDX;
|
142 | can1_mailbox.uc_obj_type = CAN_MB_RX_MODE;
|
143 | can1_mailbox.ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk;
|
144 | can1_mailbox.ul_id = CAN_MID_MIDvA(TEST1_CAN_TRANSFER_ID);
|
145 | can_mailbox_init(CAN1, &can1_mailbox);
|
146 |
|
147 | /* Init CAN0 Mailbox 0 to Transmit Mailbox. */
|
148 | reset_mailbox_conf(&can0_mailbox);
|
149 | can0_mailbox.ul_mb_idx = TEST1_CAN_COMM_MB_IDX;
|
150 | can0_mailbox.uc_obj_type = CAN_MB_TX_MODE;
|
151 | can0_mailbox.uc_tx_prio = TEST1_CAN0_TX_PRIO;
|
152 | can0_mailbox.uc_id_ver = 0;
|
153 | can0_mailbox.ul_id_msk = 0;
|
154 | can_mailbox_init(CAN0, &can0_mailbox);
|
155 |
|
156 | /* Write transmit information into mailbox. */
|
157 | can0_mailbox.ul_id = CAN_MID_MIDvA(TEST1_CAN_TRANSFER_ID);
|
158 | can0_mailbox.ul_datal = CAN_MSG_TOGGLE_LED_0;
|
159 | can0_mailbox.ul_datah = CAN_MSG_DUMMY_DATA;
|
160 | can0_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN;
|
161 | can_mailbox_write(CAN0, &can0_mailbox);
|
162 |
|
163 | printf("DEBUG: Transmit information written into mailbox\r");
|
164 | //showRegister();
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | /* Enable CAN1 mailbox 0 interrupt. */
|
170 | can_enable_interrupt(CAN1, CAN_IER_MB0);
|
171 |
|
172 | /* Send out the information in the mailbox. */
|
173 | can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0);
|
174 |
|
175 | printf("\rDEBUG: \(1\) g_ul_recv_status = %d\r",g_ul_recv_status);
|
176 |
|
177 | //Ab hier passiert nix mehr weil Abarbeitung in Empfänger-ISR:
|
178 | while (!g_ul_recv_status); /////////////////////////////////////////////////////////////
|
179 |
|
180 | printf("DEBUG: \(2\) g_ul_recv_status = %d\r\r",g_ul_recv_status);
|
181 | printf("DEBUG: still in Test 1\r\r");
|
182 | if ((can1_mailbox.ul_datal == CAN_MSG_TOGGLE_LED_0) &&
|
183 | (can1_mailbox.uc_length == MAX_CAN_FRAME_DATA_LEN)) {
|
184 | puts("Test1 passed" STRING_EOL);
|
185 | decode_can_msg(&can1_mailbox);
|
186 | } else {
|
187 | puts("Test1 ERROR" STRING_EOL);
|
188 | }
|
189 | }
|
190 |
|
191 |
|
192 |
|
193 | /**
|
194 | * Configure UART for debug message output.
|
195 | */
|
196 | static void configure_console(void)
|
197 | {
|
198 | const usart_serial_options_t uart_serial_options = {
|
199 | .baudrate = CONF_UART_BAUDRATE,
|
200 | .paritytype = CONF_UART_PARITY
|
201 | };
|
202 |
|
203 | /* Configure console UART. */
|
204 | sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
|
205 | stdio_serial_init(CONF_UART, &uart_serial_options);
|
206 | }
|
207 |
|
208 |
|
209 |
|
210 | /**
|
211 | * \brief can_example application entry point.
|
212 | *
|
213 | * \return Unused (ANSI-C compatibility).
|
214 | */
|
215 | int main(void)
|
216 | {
|
217 | uint32_t ul_sysclk;
|
218 | uint8_t uc_char;
|
219 |
|
220 | /* Initialize the SAM system. */
|
221 | sysclk_init();
|
222 | board_init();
|
223 |
|
224 | #ifdef CONF_BOARD_CAN0
|
225 | /* Configure the CAN0 TX and RX pins. */
|
226 | gpio_configure_pin(PIN_CAN0_RX_IDX, PIN_CAN0_RX_FLAGS);
|
227 | gpio_configure_pin(PIN_CAN0_TX_IDX, PIN_CAN0_TX_FLAGS);
|
228 | /* Configure the transceiver0 RS & EN pins. */
|
229 | gpio_configure_pin(PIN_CAN0_TR_RS_IDX, PIN_CAN0_TR_RS_FLAGS);
|
230 | gpio_configure_pin(PIN_CAN0_TR_EN_IDX, PIN_CAN0_TR_EN_FLAGS);
|
231 | #endif
|
232 |
|
233 | #ifdef CONF_BOARD_CAN1
|
234 | /* Configure the CAN1 TX and RX pin. */
|
235 | gpio_configure_pin(PIN_CAN1_RX_IDX, PIN_CAN1_RX_FLAGS);
|
236 | gpio_configure_pin(PIN_CAN1_TX_IDX, PIN_CAN1_TX_FLAGS);
|
237 | /* Configure the transceiver1 RS & EN pins. */
|
238 | gpio_configure_pin(PIN_CAN1_TR_RS_IDX, PIN_CAN1_TR_RS_FLAGS);
|
239 | gpio_configure_pin(PIN_CAN1_TR_EN_IDX, PIN_CAN1_TR_EN_FLAGS);
|
240 | #endif
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | /* Configure UART for debug message output. */
|
247 | configure_console();
|
248 |
|
249 | /* Output example information. */
|
250 | puts(STRING_HEADER);
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | //Test PB15 CAN1 RX
|
256 |
|
257 | while (0){
|
258 | // - - - - - - - - - - - - - - - - - - - - -
|
259 | //eine LED mit Taster einschalten, 01.03.2013
|
260 |
|
261 | pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID);
|
262 | pio_pull_up(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, PIO_PULLUP); //pmc_enable_periph_clk nicht vergessen!
|
263 |
|
264 | while (1) {
|
265 |
|
266 | if (!( pio_get(PIN_PUSHBUTTON_1_PIO, PIO_INPUT, PIN_PUSHBUTTON_1_MASK) ))
|
267 | gpio_set_pin_high(LED0_GPIO);
|
268 | else gpio_set_pin_low(LED0_GPIO);
|
269 | }
|
270 |
|
271 |
|
272 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
273 | }
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | /* Initialize CAN0 Transceiver. */
|
282 | sn65hvd234_set_rs(&can0_transceiver, PIN_CAN0_TR_RS_IDX);
|
283 | sn65hvd234_set_en(&can0_transceiver, PIN_CAN0_TR_EN_IDX);
|
284 | /* Enable CAN0 Transceiver. */
|
285 | sn65hvd234_disable_low_power(&can0_transceiver);
|
286 | sn65hvd234_enable(&can0_transceiver);
|
287 |
|
288 | /* Initialize CAN1 Transceiver. */
|
289 | sn65hvd234_set_rs(&can1_transceiver, PIN_CAN1_TR_RS_IDX);
|
290 | sn65hvd234_set_en(&can1_transceiver, PIN_CAN1_TR_EN_IDX);
|
291 | /* Enable CAN1 Transceiver. */
|
292 | sn65hvd234_disable_low_power(&can1_transceiver);
|
293 | sn65hvd234_enable(&can1_transceiver);
|
294 |
|
295 | /* Enable CAN0 & CAN1 clock. */
|
296 | pmc_enable_periph_clk(ID_CAN0);
|
297 | pmc_enable_periph_clk(ID_CAN1);
|
298 |
|
299 | ul_sysclk = sysclk_get_cpu_hz();
|
300 | if (can_init(CAN0, ul_sysclk, CAN_BPS_1000K) && //1000K
|
301 | can_init(CAN1, ul_sysclk, CAN_BPS_1000K)) { //1000K
|
302 | puts("CAN initialization is completed." STRING_EOL);
|
303 |
|
304 | /* Disable all CAN0 & CAN1 interrupts. */
|
305 | can_disable_interrupt(CAN0, CAN_DISABLE_ALL_INTERRUPT_MASK);
|
306 | can_disable_interrupt(CAN1, CAN_DISABLE_ALL_INTERRUPT_MASK);
|
307 |
|
308 | /* Configure and enable interrupt of CAN1, as the tests will use receiver interrupt. */
|
309 | NVIC_EnableIRQ(CAN1_IRQn);
|
310 |
|
311 |
|
312 |
|
313 | /* Run tests. */
|
314 | //while (1) {
|
315 | // puts("Press any key to start test" STRING_EOL);
|
316 | // while (uart_read(CONSOLE_UART, &uc_char)) { };
|
317 | //}
|
318 |
|
319 | puts("Test1 beginnt" STRING_EOL);
|
320 | test_1();
|
321 | g_ul_recv_status = 0;
|
322 |
|
323 | puts("Test2 beginnt: Press any key to continue..."
|
324 | //...
|
325 | //... weitere Tests
|
326 |
|
327 | }
|