Subversion Repositories HomeAutomation

Rev

Rev 148 | Blame | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CAN Test. This program immediately echoes back all received CAN messages to
  3.  * the bus, but increases ID with 1 before doing so.
  4.  *
  5.  * @date    2006-11-21
  6.  * @author  Jimmy Myhrman
  7.  *  
  8.  */
  9.  
  10. /*-----------------------------------------------------------------------------
  11.  * Includes
  12.  *---------------------------------------------------------------------------*/
  13. /* system files */
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <avr/wdt.h>
  17. #include <stdio.h>
  18. /* lib files */
  19. #include <can.h>
  20. #include <serial.h>
  21. #include <timebase.h>
  22.  
  23.  
  24. /*-----------------------------------------------------------------------------
  25.  * Main Program
  26.  *---------------------------------------------------------------------------*/
  27. int main(void) {
  28.     Timebase_Init();
  29.     Serial_Init();
  30.     sei();
  31.    
  32.     printf("\n------------------------------------------------------------\n");
  33.     printf(  "   CAN Test: Echo\n");
  34.     printf(  "------------------------------------------------------------\n");
  35.    
  36.     printf("CanInit...");
  37.     if (Can_Init() != CAN_OK) {
  38.         printf("FAILED!\n");
  39.     }
  40.     else {
  41.         printf("OK!\n");
  42.     }
  43.    
  44.     uint32_t timeStamp = 0;
  45.    
  46.     Can_Message_t txMsg;
  47.     Can_Message_t rxMsg;
  48.     txMsg.DataLength = 8;
  49.     txMsg.Data.bytes[0] = 7;
  50.     txMsg.Id = 0;
  51.     txMsg.RemoteFlag = 0;
  52.     txMsg.ExtendedFlag = 1;
  53.    
  54.     /* main loop */
  55.     while (1) {
  56.         /* service the CAN routines */
  57.         Can_Service();
  58.        
  59.         /* send CAN message and check for CAN errors once every second */
  60.         if (Timebase_PassedTimeMillis(timeStamp) >= 1000) {
  61.             timeStamp = Timebase_CurrentTime();
  62.         }
  63.        
  64.         /* check if any messages have been received */
  65.         while (Can_Receive(&rxMsg) == CAN_OK) {
  66.             /* echo back with increased Id */
  67.             rxMsg.Id++;
  68.             while (Can_Send(&rxMsg) != CAN_OK);
  69.         }
  70.     }
  71.    
  72.     return 0;
  73. }
  74.