Subversion Repositories HomeAutomation

Rev

Rev 658 | 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. 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. }
  60.  
  61. /*-----------------------------------------------------------------------------
  62.  * Main Program
  63.  *---------------------------------------------------------------------------*/
  64. int main(void) {
  65.     Mcu_Init();
  66.     Timebase_Init();
  67.     sei();
  68. #if defined(UART_OUTPUT)
  69.     Serial_Init();
  70.  
  71. //  printf("\n------------------------------------------------------------\n");
  72. //  printf(  "   CAN Test:\n");
  73. //  printf(  "   Periodic Transmission on CAN\n");
  74. //  printf(  "   CAN Dump\n");
  75. //  printf(  "   CAN Echo\n");
  76. //  printf(  "------------------------------------------------------------\n");
  77.    
  78.     printf("CanInit...");
  79.     if (Can_Init() != CAN_OK) {
  80.         printf("FAILED!\n");
  81. //      printf("Check wires between AVR and MCP2515 and the MCP speed (xtal).");
  82.     }
  83.     else {
  84.         printf("OK!\n");
  85.         printf("MCP2515 working fine\n");
  86.     }
  87. #elif defined(LED_OUTPUT)
  88.     Can_Init();
  89. #else
  90.     Can_Init();
  91. #endif
  92.    
  93.     uint32_t timeStamp = 0;
  94.    
  95.     Can_Message_t txMsg;
  96.     Can_Message_t rxMsg;
  97.     txMsg.RemoteFlag = 0;
  98.     txMsg.ExtendedFlag = 1;
  99.     txMsg.Id = SENDING_ID;
  100.     txMsg.DataLength = 2;
  101.     txMsg.Data.bytes[0] = 0x12;
  102.     txMsg.Data.bytes[1] = 0x34;
  103.  
  104.     /* main loop */
  105.     while (1) {
  106.         /* send CAN message and check for CAN errors once every second */
  107.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  108.             timeStamp = Timebase_CurrentTime();
  109.             /* send txMsg */
  110.             txMsg.Id = SENDING_ID;
  111.             Can_Send(&txMsg);
  112.         }
  113.        
  114.         /* check if any messages have been received */
  115.         if (rxMsgFull) {
  116. #if defined(UART_OUTPUT)
  117.             /* Dump message data on uart */
  118.             printf("\nPKT %#lx %u %u", rxMsg.Id, (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
  119.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  120.                 printf(" %#x", rxMsg.Data.bytes[i]);
  121.             }
  122. #endif
  123.             /* Echo function */
  124.             if(rxMsg.Id == ECHO_RECEIVE_ID){
  125. #if defined(UART_OUTPUT)
  126.                 printf("\n\"ping\" received");
  127.                 txMsg.Id = ECHO_SENDING_ID;
  128.                 /* Send reply */
  129.                 Can_Send(&txMsg);
  130.                 printf("\nreply sent");
  131. #else
  132.                 txMsg.Id = ECHO_SENDING_ID;
  133.                 /* Send reply */
  134.                 Can_Send(&txMsg);
  135. #endif
  136.             }
  137.             rxMsgFull = 0;
  138.         }
  139.     }
  140.    
  141.     return 0;
  142. }
  143.