Subversion Repositories HomeAutomation

Rev

Rev 1449 | Rev 1453 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_Serial.h"
  3.  
  4. #ifdef sns_Serial_USEEEPROM
  5. #include "sns_Serial_eeprom.h"
  6. struct eeprom_sns_Serial EEMEM eeprom_sns_Serial =
  7. {
  8.     {
  9.         ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
  10.         0xAB,   // x
  11.         0x1234  // y
  12.     },
  13.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  14. };
  15. #endif
  16.  
  17. // types
  18. typedef enum
  19. {
  20.     SNS_SERIAL_PHYFORMAT_RS232 = 0,
  21.     SNS_SERIAL_PHYFORMAT_RS485 = 1,
  22.     SNS_SERIAL_PHYFORMAT_LOOPBACK = 2
  23. } snsSerialPhyFormat_t;
  24.  
  25. // internal variables
  26. static uint16_t baudRate = 9600;
  27. static snsSerialPhyFormat_t format = SNS_SERIAL_PHYFORMAT_RS232;
  28.  
  29. void sns_Serial_Init(void)
  30. {
  31. #ifdef sns_Serial_USEEEPROM
  32.     if (EEDATA_OK)
  33.     {
  34.       ///TODO: Use stored data to set initial values for the module
  35.       blablaX = eeprom_read_byte(EEDATA.x);
  36.       blablaY = eeprom_read_word(EEDATA16.y);
  37.     } else
  38.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  39.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  40.       eeprom_write_word_crc(EEDATA16.y, 0x1234, WITHOUT_CRC);
  41.       EEDATA_UPDATE_CRC;
  42.     }
  43. #endif
  44.     ///TODO: Initialize hardware etc here
  45.     uart_init(UART_BAUD_SELECT(baudRate, F_CPU));
  46.     gpio_set_out(sns_Serial_RXEN);
  47.     gpio_set_out(sns_Serial_TXEN);
  48.     gpio_set_pin(sns_Serial_RXEN);
  49.     gpio_set_pin(sns_Serial_TXEN);
  50.  
  51.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  52.     // Pcint_SetCallbackPin(sns_Serial_PCINT, EXP_C , &sns_Serial_pcint_callback);
  53.  
  54. }
  55.  
  56. void sns_Serial_Process(void)
  57. {
  58.     ///TODO: Stuff that needs doing is done here
  59.     unsigned int data = uart_getc();
  60.     unsigned char c = (unsigned char)(data & 0x00FF);
  61.     unsigned char status = (unsigned char)((data & 0xFF00) >> 8);
  62.    
  63.     if (status == 0)
  64.     {
  65.         // data received from uart
  66.         StdCan_Msg_t msg;
  67.         msg.Length = 1;
  68.         msg.Data[0] = c;
  69.         StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  70.         StdCan_Set_direction(msg.Header, DIRECTIONFLAG_TO_OWNER);
  71.         msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  72.         msg.Header.ModuleId = sns_Serial_ID;
  73.         msg.Header.Command = CAN_MODULE_CMD_SERIAL_SERIALDATA;
  74.         // transmit
  75.         while(StdCan_Put(&msg) != StdCan_Ret_OK);
  76.     }
  77. }
  78.  
  79. void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg)
  80. {
  81.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  82.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  83.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL &&
  84.         rxMsg->Header.ModuleId == sns_Serial_ID)
  85.     {
  86.         switch (rxMsg->Header.Command)
  87.         {
  88.             case CAN_MODULE_CMD_SERIAL_SERIALDATA:
  89.                 for (uint8_t i=0; i<rxMsg->Length; i++)
  90.                 {
  91.                     uart_putc(rxMsg->Data[i]);
  92.                 }
  93.                 break;
  94.             case CAN_MODULE_CMD_SERIAL_SERIALCONFIG:
  95.                 baudRate = ((uint16_t)rxMsg->Data[0] << 0) | ((uint16_t)rxMsg->Data[1] << 8);
  96.                 format = (snsSerialPhyFormat_t)rxMsg->Data[2];
  97.                 uart_init(UART_BAUD_SELECT(baudRate, F_CPU));
  98.                 break;
  99.         }
  100.     }
  101. }
  102.  
  103. void sns_Serial_List(uint8_t ModuleSequenceNumber)
  104. {
  105.     StdCan_Msg_t txMsg;
  106.  
  107.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  108.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  109.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  110.     txMsg.Header.ModuleId = sns_Serial_ID;
  111.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  112.     txMsg.Length = 6;
  113.  
  114.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  115.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  116.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  117.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  118.  
  119.     txMsg.Data[4] = NUMBER_OF_MODULES;
  120.     txMsg.Data[5] = ModuleSequenceNumber;
  121.  
  122.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  123. }
  124.