Rev 673 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 61 | jimmy | 1 | /** |
| 427 | eqlazer | 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 | * |
||
| 61 | jimmy | 8 | * |
| 144 | jimmy | 9 | * @date 2006-11-21 |
| 427 | eqlazer | 10 | * @author Jimmy Myhrman, Erik Larsson (2007-05-07) |
| 61 | jimmy | 11 | * |
| 12 | */ |
||
| 13 | |||
| 14 | /*----------------------------------------------------------------------------- |
||
| 15 | * Includes |
||
| 16 | *---------------------------------------------------------------------------*/ |
||
| 17 | /* system files */ |
||
| 18 | #include <avr/io.h> |
||
| 19 | #include <avr/interrupt.h> |
||
| 20 | #include <avr/wdt.h> |
||
| 21 | #include <stdio.h> |
||
| 667 | arune | 22 | #include <string.h> |
| 61 | jimmy | 23 | /* lib files */ |
| 667 | arune | 24 | #include <config.h> |
| 25 | #include <mcp2515.h> |
||
| 149 | jimmy | 26 | #include <serial.h> |
| 61 | jimmy | 27 | #include <timebase.h> |
| 667 | arune | 28 | #include <mcu.h> |
| 61 | jimmy | 29 | |
| 451 | eqlazer | 30 | /* LED 1 blinks |
| 31 | #define LED1_PORT PORTC |
||
| 32 | #define LED1_DDR DDRC |
||
| 33 | #define LED2_PORT PORTC |
||
| 34 | #define LED2_DDR PORTC |
||
| 667 | arune | 35 | */ |
| 427 | eqlazer | 36 | |
| 667 | arune | 37 | volatile Can_Message_t rxMsg; // Message storage |
| 38 | volatile uint8_t rxMsgFull; // Synchronization flag |
||
| 427 | eqlazer | 39 | |
| 667 | arune | 40 | void Can_Process(Can_Message_t* msg) { |
| 41 | if (!(msg->ExtendedFlag)) return; // We don't care about standard CAN frames. |
||
| 451 | eqlazer | 42 | |
| 667 | arune | 43 | if (!rxMsgFull) { |
| 44 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
||
| 45 | rxMsgFull = 1; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 61 | jimmy | 49 | /*----------------------------------------------------------------------------- |
| 50 | * Main Program |
||
| 51 | *---------------------------------------------------------------------------*/ |
||
| 52 | int main(void) { |
||
| 208 | jimmy | 53 | Mcu_Init(); |
| 127 | jimmy | 54 | Timebase_Init(); |
| 451 | eqlazer | 55 | sei(); |
| 56 | #if defined(UART_OUTPUT) |
||
| 149 | jimmy | 57 | Serial_Init(); |
| 427 | eqlazer | 58 | |
| 667 | arune | 59 | // printf("\n------------------------------------------------------------\n"); |
| 60 | // printf( " CAN Test:\n"); |
||
| 61 | // printf( " Periodic Transmission on CAN\n"); |
||
| 62 | // printf( " CAN Dump\n"); |
||
| 63 | // printf( " CAN Echo\n"); |
||
| 64 | // printf( "------------------------------------------------------------\n"); |
||
| 61 | jimmy | 65 | |
| 66 | printf("CanInit..."); |
||
| 127 | jimmy | 67 | if (Can_Init() != CAN_OK) { |
| 61 | jimmy | 68 | printf("FAILED!\n"); |
| 667 | arune | 69 | // printf("Check wires between AVR and MCP2515 and the MCP speed (xtal)."); |
| 61 | jimmy | 70 | } |
| 71 | else { |
||
| 72 | printf("OK!\n"); |
||
| 427 | eqlazer | 73 | printf("MCP2515 working fine\n"); |
| 61 | jimmy | 74 | } |
| 451 | eqlazer | 75 | #elif defined(LED_OUTPUT) |
| 76 | Can_Init(); |
||
| 77 | #else |
||
| 78 | Can_Init(); |
||
| 79 | #endif |
||
| 61 | jimmy | 80 | |
| 81 | uint32_t timeStamp = 0; |
||
| 82 | |||
| 127 | jimmy | 83 | Can_Message_t txMsg; |
| 61 | jimmy | 84 | txMsg.RemoteFlag = 0; |
| 427 | eqlazer | 85 | txMsg.ExtendedFlag = 1; |
| 86 | txMsg.Id = SENDING_ID; |
||
| 197 | andfri | 87 | txMsg.DataLength = 2; |
| 427 | eqlazer | 88 | txMsg.Data.bytes[0] = 0x12; |
| 89 | txMsg.Data.bytes[1] = 0x34; |
||
| 90 | |||
| 61 | jimmy | 91 | /* main loop */ |
| 92 | while (1) { |
||
| 93 | /* send CAN message and check for CAN errors once every second */ |
||
| 127 | jimmy | 94 | if (Timebase_PassedTimeMillis(timeStamp) >= 1000) { |
| 142 | jimmy | 95 | timeStamp = Timebase_CurrentTime(); |
| 127 | jimmy | 96 | /* send txMsg */ |
| 427 | eqlazer | 97 | txMsg.Id = SENDING_ID; |
| 144 | jimmy | 98 | Can_Send(&txMsg); |
| 673 | eqlazer | 99 | #if defined(UART_OUTPUT) |
| 100 | printf("\nSending: %#lx", txMsg.Id); |
||
| 101 | #endif |
||
| 142 | jimmy | 102 | } |
| 103 | |||
| 104 | /* check if any messages have been received */ |
||
| 667 | arune | 105 | if (rxMsgFull) { |
| 451 | eqlazer | 106 | #if defined(UART_OUTPUT) |
| 427 | eqlazer | 107 | /* Dump message data on uart */ |
| 451 | eqlazer | 108 | printf("\nPKT %#lx %u %u", rxMsg.Id, (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
| 427 | eqlazer | 109 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 451 | eqlazer | 110 | printf(" %#x", rxMsg.Data.bytes[i]); |
| 427 | eqlazer | 111 | } |
| 451 | eqlazer | 112 | #endif |
| 427 | eqlazer | 113 | /* Echo function */ |
| 114 | if(rxMsg.Id == ECHO_RECEIVE_ID){ |
||
| 451 | eqlazer | 115 | #if defined(UART_OUTPUT) |
| 673 | eqlazer | 116 | printf("\n\"ping\" received: %#lx", rxMsg.Id); |
| 427 | eqlazer | 117 | txMsg.Id = ECHO_SENDING_ID; |
| 118 | /* Send reply */ |
||
| 119 | Can_Send(&txMsg); |
||
| 673 | eqlazer | 120 | printf("\nreply sent, ID: %#lx", txMsg.Id); |
| 451 | eqlazer | 121 | #else |
| 122 | txMsg.Id = ECHO_SENDING_ID; |
||
| 123 | /* Send reply */ |
||
| 124 | Can_Send(&txMsg); |
||
| 125 | #endif |
||
| 427 | eqlazer | 126 | } |
| 667 | arune | 127 | rxMsgFull = 0; |
| 61 | jimmy | 128 | } |
| 129 | } |
||
| 130 | |||
| 131 | return 0; |
||
| 132 | } |