Rev 149 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 149 | Rev 151 | ||
|---|---|---|---|
| 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 | Timebase_Init(); |
28 | Timebase_Init(); |
| 29 | Serial_Init(); |
29 | Serial_Init(); |
| 30 | sei(); |
30 | sei(); |
| 31 | 31 | ||
| 32 | printf("\n------------------------------------------------------------\n"); |
32 | printf("\n------------------------------------------------------------\n"); |
| 33 | printf( " CAN Test: Periodic Transmission\n"); |
33 | printf( " CAN Test: Periodic Transmission\n"); |
| 34 | printf( "------------------------------------------------------------\n"); |
34 | printf( "------------------------------------------------------------\n"); |
| 35 | 35 | ||
| 36 | printf("CanInit..."); |
36 | printf("CanInit..."); |
| 37 | if (Can_Init() != CAN_OK) { |
37 | if (Can_Init() != CAN_OK) { |
| 38 | printf("FAILED!\n"); |
38 | printf("FAILED!\n"); |
| 39 | } |
39 | } |
| 40 | else { |
40 | else { |
| 41 | printf("OK!\n"); |
41 | printf("OK!\n"); |
| 42 | } |
42 | } |
| 43 | 43 | ||
| 44 | uint32_t timeStamp = 0; |
44 | uint32_t timeStamp = 0; |
| 45 | 45 | ||
| 46 | Can_Message_t txMsg; |
46 | Can_Message_t txMsg; |
| 47 | Can_Message_t rxMsg; |
47 | Can_Message_t rxMsg; |
| 48 | txMsg.Id = 0; |
48 | txMsg.Id = 0; |
| 49 | txMsg.RemoteFlag = 0; |
49 | txMsg.RemoteFlag = 0; |
| 50 | txMsg.ExtendedFlag = 1; |
50 | txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set. |
| 51 | 51 | ||
| 52 | /* main loop */ |
52 | /* main loop */ |
| 53 | while (1) { |
53 | while (1) { |
| 54 | /* service the CAN routines */ |
54 | /* service the CAN routines */ |
| 55 | Can_Service(); |
55 | Can_Service(); |
| 56 | 56 | ||
| 57 | /* send CAN message and check for CAN errors once every second */ |
57 | /* send CAN message and check for CAN errors once every second */ |
| 58 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
58 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 59 | timeStamp = Timebase_CurrentTime(); |
59 | timeStamp = Timebase_CurrentTime(); |
| 60 | /* send txMsg */ |
60 | /* send txMsg */ |
| 61 | txMsg.Id++; |
61 | txMsg.Id++; |
| 62 | Can_Send(&txMsg); |
62 | Can_Send(&txMsg); |
| 63 | } |
63 | } |
| 64 | 64 | ||
| 65 | /* check if any messages have been received */ |
65 | /* check if any messages have been received */ |
| 66 | while (Can_Receive(&rxMsg) == CAN_OK) { |
66 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 67 | 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)); |
67 | 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)); |
| 68 | printf("data={ "); |
68 | printf("data={ "); |
| 69 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
69 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 70 | printf("%x ", rxMsg.Data.bytes[i]); |
70 | printf("%x ", rxMsg.Data.bytes[i]); |
| 71 | } |
71 | } |
| 72 | printf("}\n"); |
72 | printf("}\n"); |
| 73 | } |
73 | } |
| 74 | } |
74 | } |
| 75 | 75 | ||
| 76 | return 0; |
76 | return 0; |
| 77 | } |
77 | } |
| 78 | 78 | ||