Subversion Repositories HomeAutomation

Rev

Rev 694 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #include <inttypes.h>
  2. #include <avr/interrupt.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <config.h> // All configuration parameters
  6. #include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
  7. #include <drivers/uart/serial.h>
  8. #include <drivers/DTMF/mt8870/mt8870.h>
  9.  
  10. #define STATE_IDLE          0
  11. #define STATE_GET_DATA      1
  12. #define STATE_START_WAIT    2
  13. #define STATE_WAIT          3
  14. #define STATE_STOP          4
  15.  
  16. #define APP_TYPE    0xf001
  17. #define APP_VERSION 0x0002
  18.  
  19. // A simple message "queue", with space for one message only.
  20. // These are declared volatile to tell the compiler not to optimize away accesses.
  21. volatile Can_Message_t rxMsg; // Message storage
  22. volatile uint8_t rxMsgFull;   // Synchronization flag
  23.  
  24. // CAN message reception callback.
  25. // This function runs with interrupts disabled, keep it as short as possible.
  26. void can_receive(Can_Message_t *msg) {
  27.     if (!rxMsgFull) {
  28.         memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
  29.         rxMsgFull = 1;
  30.     }
  31. }
  32.  
  33. int main(void)
  34. {
  35.     // Enable interrupts as early as possible
  36.     sei();
  37.    
  38.     Serial_Init();
  39.    
  40.     Can_Message_t txMsg;
  41.     txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
  42.     txMsg.DataLength = 4;
  43.     txMsg.RemoteFlag = 0;
  44.     txMsg.ExtendedFlag = 1;
  45.     txMsg.Data.words[0] = APP_TYPE;
  46.     txMsg.Data.words[1] = APP_VERSION;
  47.    
  48.     // Set up callback for CAN reception, this is optional if only sending is required.
  49.     BIOS_CanCallback = &can_receive;
  50.     // Send CAN_NMT_APP_START
  51.     BIOS_CanSend(&txMsg);
  52.    
  53.     DTMFin_Init();
  54.  
  55.     uint8_t rxbuffer[MAX_TONES];
  56.     uint8_t state=0;
  57.     uint8_t rxlen=0;
  58.  
  59.     while (1) {
  60.        
  61.         if (state == STATE_IDLE) {
  62.             DTMFin_Start(rxbuffer);
  63.             state = STATE_START_WAIT;
  64.         } else if (state == STATE_START_WAIT) {
  65.             state = STATE_WAIT;
  66.         } else if (state == STATE_WAIT) {
  67.             uint8_t retval = DTMFin_Poll(&rxlen);
  68.             if (retval == RET_FINISHED) {
  69.                 state = STATE_STOP;
  70.             } else if (retval == RET_OVERFLOW) {
  71.                 state = STATE_IDLE;
  72.             }
  73.         } else if (state == STATE_STOP) {
  74.             //if ((rxbuffer[0] == 0xa || rxbuffer[0] == 0xb) && rxbuffer[rxlen-1] == 0xc) {
  75.                 for (uint8_t i = 0; i < 16; i=i+2) {
  76.                     txMsg.Data.bytes[i>>1] = rxbuffer[i]<<4;
  77.                     if (i+1 < rxlen) {
  78.                         txMsg.Data.bytes[i>>1] |= rxbuffer[i+1];
  79.                     }
  80.                 }
  81.                 txMsg.DataLength = (rxlen>>1)+(rxlen&1);
  82.                 txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_PHONENR << CAN_SHIFT_SNS_TYPE) | (0<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
  83.                 BIOS_CanSend(&txMsg);
  84.    
  85.             //}
  86.             state = STATE_IDLE;
  87.         }
  88.     }
  89.    
  90.     return 0;
  91. }
  92.