Rev 658 | Rev 671 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 658 | Rev 667 | ||
|---|---|---|---|
| Line 3... | Line 3... | ||
| 3 | * Replaces CanTestPeriodic and CanTestEcho. |
3 | * Replaces CanTestPeriodic and CanTestEcho. |
| 4 | * |
4 | * |
| 5 | * CanTest sends messages periodicly, dumps incomming messages on uart |
5 | * CanTest sends messages periodicly, dumps incomming messages on uart |
| 6 | * and echoes on packages with ID== ECHO_RECEIVE_ID; |
6 | * and echoes on packages with ID== ECHO_RECEIVE_ID; |
| 7 | * |
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. |
- | |
| 10 | * Probably the wire between AVR and MCP, so check it twice. |
- | |
| 11 | * |
8 | * |
| 12 | * @date 2006-11-21 |
9 | * @date 2006-11-21 |
| 13 | * @author Jimmy Myhrman, Erik Larsson (2007-05-07) |
10 | * @author Jimmy Myhrman, Erik Larsson (2007-05-07) |
| 14 | * |
11 | * |
| 15 | */ |
12 | */ |
| Line 20... | Line 17... | ||
| 20 | /* system files */ |
17 | /* system files */ |
| 21 | #include <avr/io.h> |
18 | #include <avr/io.h> |
| 22 | #include <avr/interrupt.h> |
19 | #include <avr/interrupt.h> |
| 23 | #include <avr/wdt.h> |
20 | #include <avr/wdt.h> |
| 24 | #include <stdio.h> |
21 | #include <stdio.h> |
| - | 22 | #include <string.h> |
|
| 25 | /* lib files */ |
23 | /* lib files */ |
| 26 | #include < |
24 | #include <config.h> |
| - | 25 | #include <mcp2515.h> |
|
| 27 | #include <serial.h> |
26 | #include <serial.h> |
| 28 | #include <timebase.h> |
27 | #include <timebase.h> |
| 29 | - | ||
| 30 | #define UART_OUTPUT /* If you got the node connected to a computer enable this */ |
- | |
| 31 | //#define LED_OUTPUT /* If you havent got any connection to a computer but still got some IOs free */ |
- | |
| 32 | - | ||
| 33 | #define SENDING_ID 0x1600000UL // FIXME vilka idn kan vara lämpliga? |
- | |
| 34 | #define ECHO_RECEIVE_ID 0x1700000UL |
- | |
| 35 | |
28 | #include <mcu.h> |
| 36 | 29 | ||
| 37 | /* LED 1 blinks |
30 | /* LED 1 blinks |
| 38 | #define LED1_PORT PORTC |
31 | #define LED1_PORT PORTC |
| 39 | #define LED1_DDR DDRC |
32 | #define LED1_DDR DDRC |
| 40 | #define LED2_PORT PORTC |
33 | #define LED2_PORT PORTC |
| 41 | #define LED2_DDR PORTC |
34 | #define LED2_DDR PORTC |
| - | 35 | */ |
|
| - | 36 | ||
| - | 37 | volatile Can_Message_t rxMsg; // Message storage |
|
| - | 38 | volatile uint8_t rxMsgFull; // Synchronization flag |
|
| - | 39 | ||
| - | 40 | void Can_Process(Can_Message_t* msg) { |
|
| - | 41 | if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames. |
|
| 42 | 42 | ||
| - | 43 | if (!rxMsgFull) { |
|
| - | 44 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
|
| - | 45 | rxMsgFull = 1; |
|
| - | 46 | } |
|
| - | 47 | } |
|
| 43 | 48 | ||
| - | 49 | ISR(MCP_INT_VECTOR) { |
|
| - | 50 | // Get first available message from controller and pass it to |
|
| - | 51 | // application handler. If both RX buffers contain messages |
|
| - | 52 | // we will get another interrupt as soon as this one returns. |
|
| - | 53 | if (Can_Receive(&rxMsg) == CAN_OK) { |
|
| - | 54 | // Callbacks are run with global interrupts disabled but |
|
| - | 55 | // with controller flag cleared so another msg can be |
|
| - | 56 | // received while this one is processed. |
|
| - | 57 | Can_Process(&rxMsg); |
|
| - | 58 | } |
|
| - | 59 | } |
|
| 44 | 60 | ||
| 45 | /*----------------------------------------------------------------------------- |
61 | /*----------------------------------------------------------------------------- |
| 46 | * Main Program |
62 | * Main Program |
| 47 | *---------------------------------------------------------------------------*/ |
63 | *---------------------------------------------------------------------------*/ |
| 48 | int main(void) { |
64 | int main(void) { |
| Line 50... | Line 66... | ||
| 50 | Timebase_Init(); |
66 | Timebase_Init(); |
| 51 | sei(); |
67 | sei(); |
| 52 | #if defined(UART_OUTPUT) |
68 | #if defined(UART_OUTPUT) |
| 53 | Serial_Init(); |
69 | Serial_Init(); |
| 54 | 70 | ||
| 55 |
|
71 | // printf("\n------------------------------------------------------------\n"); |
| 56 |
|
72 | // printf( " CAN Test:\n"); |
| 57 |
|
73 | // printf( " Periodic Transmission on CAN\n"); |
| 58 |
|
74 | // printf( " CAN Dump\n"); |
| 59 |
|
75 | // printf( " CAN Echo\n"); |
| 60 |
|
76 | // printf( "------------------------------------------------------------\n"); |
| 61 | 77 | ||
| 62 | printf("CanInit..."); |
78 | printf("CanInit..."); |
| 63 | if (Can_Init() != CAN_OK) { |
79 | if (Can_Init() != CAN_OK) { |
| 64 | printf("FAILED!\n"); |
80 | printf("FAILED!\n"); |
| 65 |
|
81 | // printf("Check wires between AVR and MCP2515 and the MCP speed (xtal)."); |
| 66 | } |
82 | } |
| 67 | else { |
83 | else { |
| 68 | printf("OK!\n"); |
84 | printf("OK!\n"); |
| 69 | printf("MCP2515 working fine\n"); |
85 | printf("MCP2515 working fine\n"); |
| 70 | } |
86 | } |
| 71 | #elif defined(LED_OUTPUT) |
87 | #elif defined(LED_OUTPUT) |
| 72 | Can_Init(); |
88 | Can_Init(); |
| 73 | #else |
89 | #else |
| 74 | Can_Init(); |
90 | Can_Init(); |
| 75 | #endif |
91 | #endif |
| 76 | 92 | ||
| 77 | uint32_t timeStamp = 0; |
93 | uint32_t timeStamp = 0; |
| 78 | 94 | ||
| 79 | Can_Message_t txMsg; |
95 | Can_Message_t txMsg; |
| 80 | Can_Message_t rxMsg; |
96 | Can_Message_t rxMsg; |
| 81 | txMsg.RemoteFlag = 0; |
97 | txMsg.RemoteFlag = 0; |
| 82 | txMsg.ExtendedFlag = 1; |
98 | txMsg.ExtendedFlag = 1; |
| 83 | txMsg.Id = SENDING_ID; |
99 | txMsg.Id = SENDING_ID; |
| Line 85... | Line 101... | ||
| 85 | txMsg.Data.bytes[0] = 0x12; |
101 | txMsg.Data.bytes[0] = 0x12; |
| 86 | txMsg.Data.bytes[1] = 0x34; |
102 | txMsg.Data.bytes[1] = 0x34; |
| 87 | 103 | ||
| 88 | /* main loop */ |
104 | /* main loop */ |
| 89 | while (1) { |
105 | while (1) { |
| 90 | /* service the CAN routines */ |
- | |
| 91 | Can_Service(); |
- | |
| 92 | - | ||
| 93 | /* send CAN message and check for CAN errors once every second */ |
106 | /* send CAN message and check for CAN errors once every second */ |
| 94 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
107 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 95 | timeStamp = Timebase_CurrentTime(); |
108 | timeStamp = Timebase_CurrentTime(); |
| 96 | /* send txMsg */ |
109 | /* send txMsg */ |
| 97 | txMsg.Id = SENDING_ID; |
110 | txMsg.Id = SENDING_ID; |
| 98 | Can_Send(&txMsg); |
111 | Can_Send(&txMsg); |
| 99 | } |
112 | } |
| 100 | 113 | ||
| 101 | /* check if any messages have been received */ |
114 | /* check if any messages have been received */ |
| 102 |
|
115 | if (rxMsgFull) { |
| 103 | #if defined(UART_OUTPUT) |
116 | #if defined(UART_OUTPUT) |
| 104 | /* Dump message data on uart */ |
117 | /* Dump message data on uart */ |
| 105 | printf("\nPKT %#lx %u %u", rxMsg.Id, (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
118 | printf("\nPKT %#lx %u %u", rxMsg.Id, (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
| 106 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
119 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 107 | printf(" %#x", rxMsg.Data.bytes[i]); |
120 | printf(" %#x", rxMsg.Data.bytes[i]); |
| Line 119... | Line 132... | ||
| 119 | txMsg.Id = ECHO_SENDING_ID; |
132 | txMsg.Id = ECHO_SENDING_ID; |
| 120 | /* Send reply */ |
133 | /* Send reply */ |
| 121 | Can_Send(&txMsg); |
134 | Can_Send(&txMsg); |
| 122 | #endif |
135 | #endif |
| 123 | } |
136 | } |
| - | 137 | rxMsgFull = 0; |
|
| 124 | } |
138 | } |
| 125 | } |
139 | } |
| 126 | 140 | ||
| 127 | return 0; |
141 | return 0; |
| 128 | } |
142 | } |