Rev 361 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 361 | Rev 362 | ||
|---|---|---|---|
| 1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
| 2 | #include <avr/interrupt.h> |
2 | #include <avr/interrupt.h> |
| 3 | #include <stdio.h> |
3 | #include <stdio.h> |
| 4 | #include <string.h> |
4 | #include <string.h> |
| 5 | #include <config.h> // All configuration parameters |
5 | #include <config.h> // All configuration parameters |
| 6 | #include <bios.h> // BIOS interface declarations, including CAN structure and ID defines. |
6 | #include <bios.h> // BIOS interface declarations, including CAN structure and ID defines. |
| 7 | #include <drivers/uart/serial.h> |
7 | #include <drivers/uart/serial.h> |
| 8 | #include <drivers/timer/timebase.h> |
8 | #include <drivers/timer/timebase.h> |
| 9 | 9 | ||
| 10 | #define APP_TYPE 0xf001 |
10 | #define APP_TYPE 0xf001 |
| 11 | #define APP_VERSION 0x0001 |
11 | #define APP_VERSION 0x0001 |
| 12 | 12 | ||
| 13 | // A simple message "queue", with space for one message only. |
13 | // A simple message "queue", with space for one message only. |
| 14 | // These are declared volatile to tell the compiler not to optimize away accesses. |
14 | // These are declared volatile to tell the compiler not to optimize away accesses. |
| 15 | volatile Can_Message_t rxMsg; // Message storage |
15 | volatile Can_Message_t rxMsg; // Message storage |
| 16 | volatile uint8_t rxMsgFull; // Synchronization flag |
16 | volatile uint8_t rxMsgFull; // Synchronization flag |
| 17 | 17 | ||
| 18 | // CAN message reception callback. |
18 | // CAN message reception callback. |
| 19 | // This function runs with interrupts disabled, keep it as short as possible. |
19 | // This function runs with interrupts disabled, keep it as short as possible. |
| 20 | void can_receive(Can_Message_t *msg) { |
20 | void can_receive(Can_Message_t *msg) { |
| 21 | if (!rxMsgFull) { |
21 | if (!rxMsgFull) { |
| 22 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
22 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
| 23 | rxMsgFull = 1; |
23 | rxMsgFull = 1; |
| 24 | } |
24 | } |
| 25 | } |
25 | } |
| 26 | 26 | ||
| 27 | int main(void) |
27 | int main(void) |
| 28 | { |
28 | { |
| 29 | // Enable interrupts as early as possible |
29 | // Enable interrupts as early as possible |
| 30 | sei(); |
30 | sei(); |
| 31 | 31 | ||
| 32 | Timebase_Init(); |
32 | Timebase_Init(); |
| 33 | Serial_Init(); |
33 | Serial_Init(); |
| 34 | 34 | ||
| 35 | unsigned long time, time1 = Timebase_CurrentTime(); |
35 | unsigned long time, time1 = Timebase_CurrentTime(); |
| 36 | 36 | ||
| 37 | Can_Message_t txMsg; |
37 | Can_Message_t txMsg; |
| 38 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
38 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
| 39 | txMsg.DataLength = 4; |
39 | txMsg.DataLength = 4; |
| 40 | txMsg.RemoteFlag = 0; |
40 | txMsg.RemoteFlag = 0; |
| 41 | txMsg.ExtendedFlag = 1; |
41 | txMsg.ExtendedFlag = 1; |
| 42 | txMsg.Data.words[0] = APP_TYPE; |
42 | txMsg.Data.words[0] = APP_TYPE; |
| 43 | txMsg.Data.words[1] = APP_VERSION; |
43 | txMsg.Data.words[1] = APP_VERSION; |
| 44 | 44 | ||
| 45 | // Set up callback for CAN reception, this is optional if only sending is required. |
45 | // Set up callback for CAN reception, this is optional if only sending is required. |
| 46 | BIOS_CanCallback = &can_receive; |
46 | BIOS_CanCallback = &can_receive; |
| 47 | // Send CAN_NMT_APP_START |
47 | // Send CAN_NMT_APP_START |
| 48 | BIOS_CanSend(&txMsg); |
48 | BIOS_CanSend(&txMsg); |
| 49 | 49 | ||
| 50 | printf("AVR Test Application\n"); |
50 | printf("AVR Test Application\n"); |
| 51 | printf("Using AVR BIOS version %x\n", BIOS_VERSION); |
51 | printf("Using AVR BIOS version %x\n", BIOS_VERSION); |
| 52 | 52 | ||
| 53 | txMsg.Id = (CAN_TST << CAN_SHIFT_CLASS) | NODE_ID; |
53 | txMsg.Id = (CAN_TST << CAN_SHIFT_CLASS) | NODE_ID; |
| 54 | txMsg.Data.dwords[0] = 0x01020304; |
54 | txMsg.Data.dwords[0] = 0x01020304; |
| 55 | txMsg.DataLength = 8; |
55 | txMsg.DataLength = 8; |
| 56 | 56 | ||
| 57 | while (1) { |
57 | while (1) { |
| 58 | time = Timebase_CurrentTime(); |
58 | time = Timebase_CurrentTime(); |
| 59 | if ((time - time1) >= 2000) { |
59 | if ((time - time1) >= 2000) { |
| 60 | time1 = time; |
60 | time1 = time; |
| 61 | // Send a CAN message |
61 | // Send a CAN message |
| 62 | txMsg.Data.dwords[1] = time; |
62 | txMsg.Data.dwords[1] = time; |
| 63 | BIOS_CanSend(&txMsg); |
63 | BIOS_CanSend(&txMsg); |
| 64 | printf("Two seconds passed, time is now %ld.\n", time); |
64 | printf("Two seconds passed, time is now %ld.\n", time); |
| 65 | } |
65 | } |
| 66 | 66 | ||
| 67 | if (rxMsgFull) { |
67 | if (rxMsgFull) { |
| 68 | // Print the received message |
68 | // Print the received message |
| 69 | printf("RX: ID=%08lx, DLC=%u, EXT=%u, RTR=%u, data={ ", |
69 | printf("RX: ID=%08lx, DLC=%u, EXT=%u, RTR=%u, data={ ", |
| 70 | rxMsg.Id, |
70 | rxMsg.Id, |
| 71 | (uint16_t)rxMsg.DataLength, |
71 | (uint16_t)rxMsg.DataLength, |
| 72 | (uint16_t)rxMsg.ExtendedFlag, |
72 | (uint16_t)rxMsg.ExtendedFlag, |
| 73 | (uint16_t)rxMsg.RemoteFlag); |
73 | (uint16_t)rxMsg.RemoteFlag); |
| 74 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
74 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 75 | printf("%02x ", rxMsg.Data.bytes[i]); |
75 | printf("%02x ", rxMsg.Data.bytes[i]); |
| 76 | } |
76 | } |
| 77 | printf("}\n"); |
77 | printf("}\n"); |
| 78 | rxMsgFull = 0; // |
78 | rxMsgFull = 0; // |
| 79 | } |
79 | } |
| 80 | } |
80 | } |
| 81 | 81 | ||
| 82 | return 0; |
82 | return 0; |
| 83 | } |
83 | } |
| 84 | 84 | ||