Rev 362 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 362 | Rev 544 | ||
|---|---|---|---|
| Line 3... | Line 3... | ||
| 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/ |
8 | #include <drivers/timer/timer.h> |
| 9 | 9 | ||
| 10 | #define APP_TYPE 0xf001 |
10 | #define APP_TYPE 0xf001 |
| 11 | #define APP_VERSION |
11 | #define APP_VERSION 0x0002 |
| 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 |
| Line 20... | Line 20... | ||
| 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 | } |
|
| - | 26 | ||
| - | 27 | // Timer callback function used for some timer tests |
|
| - | 28 | void timer_callback(uint8_t timer) { |
|
| - | 29 | Can_Message_t msg; |
|
| - | 30 | ||
| - | 31 | msg.ExtendedFlag = 1; |
|
| - | 32 | msg.Id = (CAN_TST << CAN_SHIFT_CLASS) | NODE_ID; |
|
| - | 33 | msg.RemoteFlag = 0; |
|
| - | 34 | msg.DataLength = 1; |
|
| - | 35 | msg.Data.bytes[0] = timer; |
|
| - | 36 | ||
| - | 37 | BIOS_CanSend(&msg); |
|
| 25 | } |
38 | } |
| 26 | 39 | ||
| 27 | int main(void) |
40 | int main(void) |
| 28 | { |
41 | { |
| 29 | // Enable interrupts as early as possible |
42 | // Enable interrupts as early as possible |
| 30 | sei(); |
43 | sei(); |
| 31 | 44 | ||
| 32 |
|
45 | Timer_Init(); |
| 33 | Serial_Init(); |
46 | Serial_Init(); |
| 34 | 47 | ||
| 35 | unsigned long time |
48 | unsigned long time; |
| 36 | 49 | ||
| 37 | Can_Message_t txMsg; |
50 | Can_Message_t txMsg; |
| 38 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
51 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
| 39 | txMsg.DataLength = 4; |
52 | txMsg.DataLength = 4; |
| 40 | txMsg.RemoteFlag = 0; |
53 | txMsg.RemoteFlag = 0; |
| 41 | txMsg.ExtendedFlag = 1; |
54 | txMsg.ExtendedFlag = 1; |
| Line 44... | Line 57... | ||
| 44 | 57 | ||
| 45 | // Set up callback for CAN reception, this is optional if only sending is required. |
58 | // Set up callback for CAN reception, this is optional if only sending is required. |
| 46 | BIOS_CanCallback = &can_receive; |
59 | BIOS_CanCallback = &can_receive; |
| 47 | // Send CAN_NMT_APP_START |
60 | // Send CAN_NMT_APP_START |
| 48 | BIOS_CanSend(&txMsg); |
61 | BIOS_CanSend(&txMsg); |
| 49 | 62 | ||
| 50 | printf("AVR Test Application\n"); |
63 | printf("AVR Test Application\n"); |
| 51 | printf("Using AVR BIOS version %x\n", BIOS_VERSION); |
64 | printf("Using AVR BIOS version %x\n", BIOS_VERSION); |
| 52 | 65 | ||
| 53 | txMsg.Id = (CAN_TST << CAN_SHIFT_CLASS) | NODE_ID; |
66 | txMsg.Id = (CAN_TST << CAN_SHIFT_CLASS) | NODE_ID; |
| 54 | txMsg.Data.dwords[0] = 0x01020304; |
67 | txMsg.Data.dwords[0] = 0x01020304; |
| 55 | txMsg.DataLength = 8; |
68 | txMsg.DataLength = 8; |
| - | 69 | ||
| - | 70 | // Set up three timers (assume at least three has been defined) |
|
| - | 71 | // The timeout is specified in ticks, which is equal to ms if |
|
| - | 72 | // the tick frequency is set to 1000. |
|
| - | 73 | Timer_SetTimeout(0, 2000, TimerTypeFreeRunning, 0); |
|
| - | 74 | Timer_SetTimeout(1, 10768, TimerTypeOneShot, &timer_callback); |
|
| - | 75 | Timer_SetTimeout(2, 3141, TimerTypeFreeRunning, &timer_callback); |
|
| 56 | 76 | ||
| 57 | while (1) { |
77 | while (1) { |
| 58 | time = Timebase_CurrentTime(); |
- | |
| 59 | if (( |
78 | if (Timer_Expired(0)) { |
| 60 | time1 = time; |
- | |
| 61 | // Send a CAN message |
79 | // Send a CAN message |
| - | 80 | time = Timer_GetTicks(); |
|
| 62 | txMsg.Data.dwords[1] = time; |
81 | txMsg.Data.dwords[1] = time; |
| 63 | BIOS_CanSend(&txMsg); |
82 | BIOS_CanSend(&txMsg); |
| 64 | printf("Two seconds passed, time is now %ld.\n", time); |
83 | printf("Two seconds passed, time is now %ld.\n", time); |
| 65 | } |
84 | } |
| 66 | 85 | ||