Subversion Repositories HomeAutomation

Rev

Rev 671 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  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.  *
  9.  * @date    2006-11-21
  10.  * @author  Jimmy Myhrman, Erik Larsson (2007-05-07)
  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>
  22. #include <string.h>
  23. /* lib files */
  24. #include <config.h>
  25. #include <mcp2515.h>
  26. #include <serial.h>
  27. #include <timebase.h>
  28. #include <mcu.h>
  29.  
  30. /* LED 1 blinks
  31. #define LED1_PORT   PORTC
  32. #define LED1_DDR    DDRC
  33. #define LED2_PORT   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.  
  43.     if (!rxMsgFull) {
  44.         memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
  45.         rxMsgFull = 1;
  46.     }
  47. }
  48.  
  49. /*-----------------------------------------------------------------------------
  50.  * Main Program
  51.  *---------------------------------------------------------------------------*/
  52. int main(void) {
  53.     Mcu_Init();
  54.     Timebase_Init();
  55.     sei();
  56. #if defined(UART_OUTPUT)
  57.     Serial_Init();
  58.  
  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");
  65.    
  66.     printf("CanInit...");
  67.     if (Can_Init() != CAN_OK) {
  68.         printf("FAILED!\n");
  69. //      printf("Check wires between AVR and MCP2515 and the MCP speed (xtal).");
  70.     }
  71.     else {
  72.         printf("OK!\n");
  73.         printf("MCP2515 working fine\n");
  74.     }
  75. #elif defined(LED_OUTPUT)
  76.     Can_Init();
  77. #else
  78.     Can_Init();
  79. #endif
  80.    
  81.     uint32_t timeStamp = 0;
  82.    
  83.     Can_Message_t txMsg;
  84.     txMsg.RemoteFlag = 0;
  85.     txMsg.ExtendedFlag = 1;
  86.     txMsg.Id = SENDING_ID;
  87.     txMsg.DataLength = 2;
  88.     txMsg.Data.bytes[0] = 0x12;
  89.     txMsg.Data.bytes[1] = 0x34;
  90.  
  91.     /* main loop */
  92.     while (1) {
  93.         /* send CAN message and check for CAN errors once every second */
  94.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  95.             timeStamp = Timebase_CurrentTime();
  96.             /* send txMsg */
  97.             txMsg.Id = SENDING_ID;
  98.             Can_Send(&txMsg);
  99. #if defined(UART_OUTPUT)
  100.             printf("\nSending: %#lx", txMsg.Id);
  101. #endif
  102.         }
  103.        
  104.         /* check if any messages have been received */
  105.         if (rxMsgFull) {
  106. #if defined(UART_OUTPUT)
  107.             /* Dump message data on uart */
  108.             printf("\nPKT %#lx %u %u", rxMsg.Id, (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
  109.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  110.                 printf(" %#x", rxMsg.Data.bytes[i]);
  111.             }
  112. #endif
  113.             /* Echo function */
  114.             if(rxMsg.Id == ECHO_RECEIVE_ID){
  115. #if defined(UART_OUTPUT)
  116.                 printf("\n\"ping\" received: %#lx", rxMsg.Id);
  117.                 txMsg.Id = ECHO_SENDING_ID;
  118.                 /* Send reply */
  119.                 Can_Send(&txMsg);
  120.                 printf("\nreply sent, ID: %#lx", txMsg.Id);
  121. #else
  122.                 txMsg.Id = ECHO_SENDING_ID;
  123.                 /* Send reply */
  124.                 Can_Send(&txMsg);
  125. #endif
  126.             }
  127.             rxMsgFull = 0;
  128.         }
  129.     }
  130.    
  131.     return 0;
  132. }
  133.