Subversion Repositories HomeAutomation

Rev

Rev 426 | Rev 658 | 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 SENDING_ID  0x1600000UL // FIXME vilka idn kan vara lämpliga?
  31. #define ECHO_RECEIVE_ID     0x1700000UL
  32. #define ECHO_SENDING_ID     0x17000FFUL
  33.  
  34.  
  35.  
  36. /*-----------------------------------------------------------------------------
  37.  * Main Program
  38.  *---------------------------------------------------------------------------*/
  39. int main(void) {
  40.     Mcu_Init();
  41.     Timebase_Init();
  42.     Serial_Init();
  43.  
  44.     sei();
  45.  
  46.     printf("\n------------------------------------------------------------\n");
  47.     printf(  "   CAN Test:\n");
  48.     printf(  "   Periodic Transmission on CAN\n");
  49.     printf(  "   CAN Dump\n");
  50.     printf(  "   CAN Echo\n");
  51.     printf(  "------------------------------------------------------------\n");
  52.    
  53.     printf("CanInit...");
  54.     if (Can_Init() != CAN_OK) {
  55.         printf("FAILED!\n");
  56.         printf("Check wires between AVR and MCP2515 and the MCP speed (xtal).");
  57.     }
  58.     else {
  59.         printf("OK!\n");
  60.         printf("MCP2515 working fine\n");
  61.     }
  62.    
  63.     uint32_t timeStamp = 0;
  64.    
  65.     Can_Message_t txMsg;
  66.     Can_Message_t rxMsg;
  67.     txMsg.RemoteFlag = 0;
  68.     txMsg.ExtendedFlag = 1;
  69.     txMsg.Id = SENDING_ID;
  70.     txMsg.DataLength = 2;
  71.     txMsg.Data.bytes[0] = 0x12;
  72.     txMsg.Data.bytes[1] = 0x34;
  73.  
  74.     /* main loop */
  75.     while (1) {
  76.         /* service the CAN routines */
  77.         Can_Service();
  78.        
  79.         /* send CAN message and check for CAN errors once every second */
  80.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  81.             timeStamp = Timebase_CurrentTime();
  82.             /* send txMsg */
  83.             txMsg.Id = SENDING_ID;
  84.             Can_Send(&txMsg);
  85.         }
  86.        
  87.         /* check if any messages have been received */
  88.         while (Can_Receive(&rxMsg) == CAN_OK) {
  89.             /* Dump message data on uart */
  90.             printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
  91.             printf("data={ ");
  92.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  93.                 printf("%x ", rxMsg.Data.bytes[i]);
  94.             }
  95.             printf("}\n");
  96.  
  97.             /* Echo function */
  98.             if(rxMsg.Id == ECHO_RECEIVE_ID){
  99.                 printf("\"ping\" received\n");
  100.                 txMsg.Id = ECHO_SENDING_ID;
  101.                 /* Send reply */
  102.                 Can_Send(&txMsg);
  103.                 printf("reply sent\n");
  104.             }
  105.         }
  106.     }
  107.    
  108.     return 0;
  109. }
  110.