Subversion Repositories HomeAutomation

Rev

Rev 451 | Rev 667 | 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.  * 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.  *
  12.  * @date    2006-11-21
  13.  * @author  Jimmy Myhrman, Erik Larsson (2007-05-07)
  14.  *  
  15.  */
  16.  
  17. /*-----------------------------------------------------------------------------
  18.  * Includes
  19.  *---------------------------------------------------------------------------*/
  20. /* system files */
  21. #include <avr/io.h>
  22. #include <avr/interrupt.h>
  23. #include <avr/wdt.h>
  24. #include <stdio.h>
  25. /* lib files */
  26. #include <can.h>
  27. #include <serial.h>
  28. #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. #define ECHO_SENDING_ID     0x17000FFUL
  36.  
  37. /* LED 1 blinks
  38. #define LED1_PORT   PORTC
  39. #define LED1_DDR    DDRC
  40. #define LED2_PORT   PORTC
  41. #define LED2_DDR    PORTC
  42.  
  43.  
  44.  
  45. /*-----------------------------------------------------------------------------
  46.  * Main Program
  47.  *---------------------------------------------------------------------------*/
  48. int main(void) {
  49.     Mcu_Init();
  50.     Timebase_Init();
  51.     sei();
  52. #if defined(UART_OUTPUT)
  53.     Serial_Init();
  54.  
  55.     printf("\n------------------------------------------------------------\n");
  56.     printf(  "   CAN Test:\n");
  57.     printf(  "   Periodic Transmission on CAN\n");
  58.     printf(  "   CAN Dump\n");
  59.     printf(  "   CAN Echo\n");
  60.     printf(  "------------------------------------------------------------\n");
  61.    
  62.     printf("CanInit...");
  63.     if (Can_Init() != CAN_OK) {
  64.         printf("FAILED!\n");
  65.         printf("Check wires between AVR and MCP2515 and the MCP speed (xtal).");
  66.     }
  67.     else {
  68.         printf("OK!\n");
  69.         printf("MCP2515 working fine\n");
  70.     }
  71. #elif defined(LED_OUTPUT)
  72.     Can_Init();
  73. #else
  74.     Can_Init();
  75. #endif
  76.    
  77.     uint32_t timeStamp = 0;
  78.    
  79.     Can_Message_t txMsg;
  80.     Can_Message_t rxMsg;
  81.     txMsg.RemoteFlag = 0;
  82.     txMsg.ExtendedFlag = 1;
  83.     txMsg.Id = SENDING_ID;
  84.     txMsg.DataLength = 2;
  85.     txMsg.Data.bytes[0] = 0x12;
  86.     txMsg.Data.bytes[1] = 0x34;
  87.  
  88.     /* main loop */
  89.     while (1) {
  90.         /* service the CAN routines */
  91.         Can_Service();
  92.        
  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.         }
  100.        
  101.         /* check if any messages have been received */
  102.         while (Can_Receive(&rxMsg) == CAN_OK) {
  103. #if defined(UART_OUTPUT)
  104.             /* Dump message data on uart */
  105.             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++) {
  107.                 printf(" %#x", rxMsg.Data.bytes[i]);
  108.             }
  109. #endif
  110.             /* Echo function */
  111.             if(rxMsg.Id == ECHO_RECEIVE_ID){
  112. #if defined(UART_OUTPUT)
  113.                 printf("\n\"ping\" received");
  114.                 txMsg.Id = ECHO_SENDING_ID;
  115.                 /* Send reply */
  116.                 Can_Send(&txMsg);
  117.                 printf("\nreply sent");
  118. #else
  119.                 txMsg.Id = ECHO_SENDING_ID;
  120.                 /* Send reply */
  121.                 Can_Send(&txMsg);
  122. #endif
  123.             }
  124.         }
  125.     }
  126.    
  127.     return 0;
  128. }
  129.