Rev 426 | Rev 451 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 426 | Rev 427 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /** |
1 | /** |
| 2 | * CAN Test. This program |
2 | * CAN Test. This program is for testing purpose. |
| - | 3 | * Replaces CanTestPeriodic and CanTestEcho. |
|
| - | 4 | * |
|
| - | 5 | * CanTest sends messages periodicly, dumps incomming messages on uart |
|
| - | 6 | * and echoes on packages with ID== ECHO_RECEIVE_ID; |
|
| - | 7 | * |
|
| - | 8 | * This version uses drivers that polls the mcp2515. That means that if this program works fine, |
|
| - | 9 | * but bios+app doesnt. Something is wrong with the interruppt. |
|
| 3 | * |
10 | * Probably the wire between AVR and MCP, so check it twice. |
| 4 | * |
11 | * |
| 5 | * @date 2006-11-21 |
12 | * @date 2006-11-21 |
| 6 | * @author Jimmy |
13 | * @author Jimmy Myhrman, Erik Larsson (2007-05-07) |
| 7 | * |
14 | * |
| 8 | */ |
15 | */ |
| 9 | 16 | ||
| 10 | /*----------------------------------------------------------------------------- |
17 | /*----------------------------------------------------------------------------- |
| 11 | * Includes |
18 | * Includes |
| Line 18... | Line 25... | ||
| 18 | /* lib files */ |
25 | /* lib files */ |
| 19 | #include <can.h> |
26 | #include <can.h> |
| 20 | #include <serial.h> |
27 | #include <serial.h> |
| 21 | #include <timebase.h> |
28 | #include <timebase.h> |
| 22 | 29 | ||
| - | 30 | #define SENDING_ID 0x1600000UL // FIXME vilka idn kan vara lämpliga? |
|
| - | 31 | #define ECHO_RECEIVE_ID 0x1700000UL |
|
| - | 32 | #define ECHO_SENDING_ID 0x17000FFUL |
|
| 23 | 33 | ||
| - | 34 | ||
| - | 35 | ||
| 24 | /*----------------------------------------------------------------------------- |
36 | /*----------------------------------------------------------------------------- |
| 25 | * Main Program |
37 | * Main Program |
| 26 | *---------------------------------------------------------------------------*/ |
38 | *---------------------------------------------------------------------------*/ |
| 27 | int main(void) { |
39 | int main(void) { |
| 28 | Mcu_Init(); |
40 | Mcu_Init(); |
| 29 | Timebase_Init(); |
41 | Timebase_Init(); |
| 30 | Serial_Init(); |
42 | Serial_Init(); |
| 31 | 43 | ||
| 32 | sei(); |
44 | sei(); |
| 33 | 45 | ||
| 34 | printf("\n------------------------------------------------------------\n"); |
46 | printf("\n------------------------------------------------------------\n"); |
| - | 47 | printf( " CAN Test:\n"); |
|
| 35 | printf( " |
48 | printf( " Periodic Transmission on CAN\n"); |
| - | 49 | printf( " CAN Dump\n"); |
|
| - | 50 | printf( " CAN Echo\n"); |
|
| 36 | printf( "------------------------------------------------------------\n"); |
51 | printf( "------------------------------------------------------------\n"); |
| 37 | 52 | ||
| 38 | printf("CanInit..."); |
53 | printf("CanInit..."); |
| 39 | if (Can_Init() != CAN_OK) { |
54 | if (Can_Init() != CAN_OK) { |
| 40 | printf("FAILED!\n"); |
55 | printf("FAILED!\n"); |
| - | 56 | printf("Check wires between AVR and MCP2515 and the MCP speed (xtal)."); |
|
| 41 | } |
57 | } |
| 42 | else { |
58 | else { |
| 43 | printf("OK!\n"); |
59 | printf("OK!\n"); |
| - | 60 | printf("MCP2515 working fine\n"); |
|
| 44 | } |
61 | } |
| 45 | 62 | ||
| 46 | uint32_t timeStamp = 0; |
63 | uint32_t timeStamp = 0; |
| 47 | 64 | ||
| 48 | Can_Message_t txMsg; |
65 | Can_Message_t txMsg; |
| 49 | Can_Message_t rxMsg; |
66 | Can_Message_t rxMsg; |
| 50 | //The databytes are just what happens to be in the memory. They are never set. |
- | |
| 51 | txMsg.RemoteFlag = 0; |
67 | txMsg.RemoteFlag = 0; |
| 52 | txMsg.ExtendedFlag = 1; |
68 | txMsg.ExtendedFlag = 1; |
| 53 | txMsg.Id = |
69 | txMsg.Id = SENDING_ID; |
| 54 | txMsg.DataLength = 2; |
70 | txMsg.DataLength = 2; |
| - | 71 | txMsg.Data.bytes[0] = 0x12; |
|
| - | 72 | txMsg.Data.bytes[1] = 0x34; |
|
| 55 | 73 | ||
| 56 | /* main loop */ |
74 | /* main loop */ |
| 57 | while (1) { |
75 | while (1) { |
| 58 | /* service the CAN routines */ |
76 | /* service the CAN routines */ |
| 59 | Can_Service(); |
77 | Can_Service(); |
| 60 | 78 | ||
| 61 | /* send CAN message and check for CAN errors once every second */ |
79 | /* send CAN message and check for CAN errors once every second */ |
| 62 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
80 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 63 | timeStamp = Timebase_CurrentTime(); |
81 | timeStamp = Timebase_CurrentTime(); |
| 64 | /* send txMsg */ |
82 | /* send txMsg */ |
| 65 | txMsg.Id |
83 | txMsg.Id = SENDING_ID; |
| 66 | Can_Send(&txMsg); |
84 | Can_Send(&txMsg); |
| 67 | } |
85 | } |
| 68 | 86 | ||
| 69 | /* check if any messages have been received */ |
87 | /* check if any messages have been received */ |
| 70 | while (Can_Receive(&rxMsg) == CAN_OK) { |
88 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| - | 89 | /* Dump message data on uart */ |
|
| 71 | 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)); |
90 | 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)); |
| 72 | printf("data={ "); |
91 | printf("data={ "); |
| 73 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
92 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 74 | printf("%x ", rxMsg.Data.bytes[i]); |
93 | printf("%x ", rxMsg.Data.bytes[i]); |
| 75 | } |
94 | } |
| 76 | printf("}\n"); |
95 | printf("}\n"); |
| - | 96 | ||
| - | 97 | /* Echo function */ |
|
| - | 98 | if(rxMsg.Id == ECHO_RECEIVE_ID){ |
|
| - | 99 | printf("\"ping\" received\n"); |
|
| - | 100 | txMsg.Id = ECHO_SENDING_ID; |
|
| - | 101 | /* Send reply */ |
|
| - | 102 | Can_Send(&txMsg); |
|
| - | 103 | printf("reply sent\n"); |
|
| - | 104 | } |
|
| 77 | } |
105 | } |
| 78 | } |
106 | } |
| 79 | 107 | ||
| 80 | return 0; |
108 | return 0; |
| 81 | } |
109 | } |