Subversion Repositories HomeAutomation

Rev

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

  1. /**
  2.  * CAN Test. This program sends a CAN message once every second. The ID of the
  3.  * message is increased each time for testing purposes.
  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. #include <irreceiver.h>
  23.  
  24. #define STATE_IDLE      0
  25. #define STATE_IR_REPEAT 1
  26.  
  27. /*-----------------------------------------------------------------------------
  28.  * Main Program
  29.  *---------------------------------------------------------------------------*/
  30. int main(void) {
  31.     Mcu_Init();
  32.     Timebase_Init();
  33.     Serial_Init();
  34.    
  35.     sei();
  36.    
  37.     IRDDR &= ~(1<<IRBIT);
  38.    
  39.     printf("\n------------------------------------------------------------\n");
  40.     printf(  "   CAN Test: Periodic Transmission\n");
  41.     printf(  "------------------------------------------------------------\n");
  42.    
  43.     printf("CanInit...");
  44.     if (Can_Init() != CAN_OK) {
  45.         printf("FAILED!\n");
  46.     }
  47.     else {
  48.         printf("OK!\n");
  49.     }
  50.    
  51.     uint32_t timeStamp = 0;
  52.     uint32_t irTimeoutTimeStamp = 0;
  53.    
  54.     Can_Message_t txMsg;
  55.     Can_Message_t rxMsg;
  56.     txMsg.Id = 1;
  57.    
  58.     uint8_t ir_protocol, ir_address, ir_command;
  59.     uint8_t state = STATE_IDLE;
  60.    
  61.     /* main loop */
  62.     while (1) {
  63.         /* service the CAN routines */
  64.         Can_Service();
  65.        
  66.         if (state == STATE_IDLE) {
  67.             if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) {
  68.                 txMsg.Data.bytes[0] = 0x00;
  69.                 txMsg.Data.bytes[1] = ir_protocol;
  70.                 txMsg.Data.bytes[2] = ir_address;
  71.                 txMsg.Data.bytes[3] = ir_command;
  72.                 txMsg.DataLength = 4;
  73.                 Can_Send(&txMsg);
  74.                 irTimeoutTimeStamp = Timebase_CurrentTime();
  75.                 state = STATE_IR_REPEAT;
  76.             }
  77.         } else if (state == STATE_IR_REPEAT) {
  78.             //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE
  79.             if (Timebase_PassedTimeMillis(irTimeoutTimeStamp) >= 105) {     //getLastProtoTimeout()
  80.                 state = STATE_IDLE;
  81.                 txMsg.Data.bytes[0] = 0x0f;
  82.                 Can_Send(&txMsg);
  83.             }
  84.             if (checkIrIdle() == IR_OK) {       //funktion som borde finnas i irreceiver.c
  85.                 irTimeoutTimeStamp = Timebase_CurrentTime();
  86.             }
  87.         }
  88.        
  89.    
  90.         /* check if any messages have been received */
  91.         while (Can_Receive(&rxMsg) == CAN_OK) {
  92.             /*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));
  93.             printf("data={ ");
  94.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  95.                 printf("%x ", rxMsg.Data.bytes[i]);
  96.             }
  97.             printf("}\n");*/
  98.         }
  99.     }
  100.    
  101.     return 0;
  102. }
  103.