Rev 197 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 197 | Rev 208 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CAN Test. This program sends a CAN message once every second. The ID of the |
2 | * CAN Test. This program sends a CAN message once every second. The ID of the |
| 3 | * message is increased each time for testing purposes. |
3 | * message is increased each time for testing purposes. |
| 4 | * |
4 | * |
| 5 | * @date 2006-11-21 |
5 | * @date 2006-11-21 |
| 6 | * @author Jimmy Myhrman |
6 | * @author Jimmy Myhrman |
| 7 | * |
7 | * |
| 8 | */ |
8 | */ |
| 9 | 9 | ||
| 10 | /*----------------------------------------------------------------------------- |
10 | /*----------------------------------------------------------------------------- |
| 11 | * Includes |
11 | * Includes |
| 12 | *---------------------------------------------------------------------------*/ |
12 | *---------------------------------------------------------------------------*/ |
| 13 | /* system files */ |
13 | /* system files */ |
| 14 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 15 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 17 | #include <stdio.h> |
17 | #include <stdio.h> |
| 18 | /* lib files */ |
18 | /* lib files */ |
| 19 | #include <can.h> |
19 | #include <can.h> |
| 20 | #include <serial.h> |
20 | #include <serial.h> |
| 21 | #include <timebase.h> |
21 | #include <timebase.h> |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | /*----------------------------------------------------------------------------- |
24 | /*----------------------------------------------------------------------------- |
| 25 | * Main Program |
25 | * Main Program |
| 26 | *---------------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------------*/ |
| 27 | int main(void) { |
27 | int main(void) { |
| - | 28 | Mcu_Init(); |
|
| 28 | Timebase_Init(); |
29 | Timebase_Init(); |
| 29 | Serial_Init(); |
30 | Serial_Init(); |
| - | 31 | ||
| 30 | sei(); |
32 | sei(); |
| 31 | 33 | ||
| 32 | printf("\n------------------------------------------------------------\n"); |
34 | printf("\n------------------------------------------------------------\n"); |
| 33 | printf( " CAN Test: Periodic Transmission\n"); |
35 | printf( " CAN Test: Periodic Transmission\n"); |
| 34 | printf( "------------------------------------------------------------\n"); |
36 | printf( "------------------------------------------------------------\n"); |
| 35 | 37 | ||
| 36 | printf("CanInit..."); |
38 | printf("CanInit..."); |
| 37 | if (Can_Init() != CAN_OK) { |
39 | if (Can_Init() != CAN_OK) { |
| 38 | printf("FAILED!\n"); |
40 | printf("FAILED!\n"); |
| 39 | } |
41 | } |
| 40 | else { |
42 | else { |
| 41 | printf("OK!\n"); |
43 | printf("OK!\n"); |
| 42 | } |
44 | } |
| 43 | 45 | ||
| 44 | uint32_t timeStamp = 0; |
46 | uint32_t timeStamp = 0; |
| 45 | 47 | ||
| 46 | Can_Message_t txMsg; |
48 | Can_Message_t txMsg; |
| 47 | Can_Message_t rxMsg; |
49 | Can_Message_t rxMsg; |
| 48 | //The databytes are just what happens to be in the memory. They are never set. |
50 | //The databytes are just what happens to be in the memory. They are never set. |
| 49 | txMsg.RemoteFlag = 0; |
51 | txMsg.RemoteFlag = 0; |
| 50 | txMsg.ExtendedFlag = 1; |
52 | txMsg.ExtendedFlag = 1; |
| 51 | #define NODENUMBER 1 |
53 | #define NODENUMBER 1 |
| 52 | #if NODENUMBER == 1 |
54 | #if NODENUMBER == 1 |
| 53 | txMsg.Id = 16; |
55 | txMsg.Id = 16; |
| 54 | txMsg.DataLength = 2; |
56 | txMsg.DataLength = 2; |
| 55 | #elif NODENUMBER == 2 |
57 | #elif NODENUMBER == 2 |
| 56 | txMsg.Id = 32; |
58 | txMsg.Id = 32; |
| 57 | txMsg.DataLength = 5; |
59 | txMsg.DataLength = 5; |
| 58 | #else |
60 | #else |
| 59 | # error NODENUMBER not set! |
61 | # error NODENUMBER not set! |
| 60 | #endif |
62 | #endif |
| 61 | 63 | ||
| 62 | /* main loop */ |
64 | /* main loop */ |
| 63 | while (1) { |
65 | while (1) { |
| 64 | /* service the CAN routines */ |
66 | /* service the CAN routines */ |
| 65 | Can_Service(); |
67 | Can_Service(); |
| 66 | 68 | ||
| 67 | /* send CAN message and check for CAN errors once every second */ |
69 | /* send CAN message and check for CAN errors once every second */ |
| 68 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
70 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 69 | timeStamp = Timebase_CurrentTime(); |
71 | timeStamp = Timebase_CurrentTime(); |
| 70 | /* send txMsg */ |
72 | /* send txMsg */ |
| 71 | txMsg.Id++; |
73 | txMsg.Id++; |
| 72 | Can_Send(&txMsg); |
74 | Can_Send(&txMsg); |
| 73 | } |
75 | } |
| 74 | 76 | ||
| 75 | /* check if any messages have been received */ |
77 | /* check if any messages have been received */ |
| 76 | while (Can_Receive(&rxMsg) == CAN_OK) { |
78 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 77 | printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
79 | printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
| 78 | printf("data={ "); |
80 | printf("data={ "); |
| 79 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
81 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 80 | printf("%x ", rxMsg.Data.bytes[i]); |
82 | printf("%x ", rxMsg.Data.bytes[i]); |
| 81 | } |
83 | } |
| 82 | printf("}\n"); |
84 | printf("}\n"); |
| 83 | } |
85 | } |
| 84 | } |
86 | } |
| 85 | 87 | ||
| 86 | return 0; |
88 | return 0; |
| 87 | } |
89 | } |
| 88 | 90 | ||