Subversion Repositories HomeAutomation

Rev

Rev 1453 | Rev 1475 | 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.     // default baudrate
  45.     uart_init(UART_BAUD_SELECT(baudRate, F_CPU));
  46.     // configure control pins
  47.     gpio_set_out(sns_Serial_RXEN);
  48.     gpio_set_out(sns_Serial_TXEN);
  49.     gpio_set_out(sns_Serial_ON);
  50.     gpio_set_out(sns_Serial_485_232);
  51.     //default to loopback mode until config command received
  52.     gpio_clr_pin(sns_Serial_ON);
  53.     gpio_clr_pin(sns_Serial_485_232);
  54.  
  55.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  56.     // Pcint_SetCallbackPin(sns_Serial_PCINT, EXP_C , &sns_Serial_pcint_callback);
  57.  
  58. }
  59.  
  60. void sns_Serial_Process(void)
  61. {
  62.     // prepare a new message, in case new data is available
  63.     StdCan_Msg_t msg;
  64.     msg.Length = 0; // no data so far
  65.     StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  66.     StdCan_Set_direction(msg.Header, DIRECTIONFLAG_TO_OWNER);
  67.     msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  68.     msg.Header.ModuleId = sns_Serial_ID;
  69.     msg.Header.Command = CAN_MODULE_CMD_SERIAL_SERIALDATA;
  70.    
  71.     unsigned char status;
  72.    
  73.     do
  74.     {
  75.         // ask uart driver for more data
  76.         unsigned int data = uart_getc();
  77.         // character is contained in LSB
  78.         unsigned char c = (unsigned char)(data & 0x00FF);
  79.         // status is contained in MSB
  80.         status = (unsigned char)((data & 0xFF00) >> 8);
  81.        
  82.         // status == 0 means we just received a new char
  83.         if (status == 0)
  84.         {
  85.             // insert it into the message
  86.             msg.Data[(uint8_t)msg.Length] = c;
  87.             msg.Length++;
  88.         }
  89.     // keep going until max data length reached, or until uart RXBUF is empty
  90.     } while (status == 0 && msg.Length < 8);
  91.    
  92.     // did we fill any data into the message?
  93.     if (msg.Length > 0)
  94.     {
  95.         // transmit this chunk of data (max 8 chars)
  96.         while(StdCan_Put(&msg) != StdCan_Ret_OK);
  97.     }
  98. }
  99.  
  100. void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg)
  101. {
  102.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  103.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  104.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL &&
  105.         rxMsg->Header.ModuleId == sns_Serial_ID)
  106.     {
  107.         switch (rxMsg->Header.Command)
  108.         {
  109.             case CAN_MODULE_CMD_SERIAL_SERIALDATA:
  110.                 for (uint8_t i=0; i<rxMsg->Length; i++)
  111.                 {
  112.                     uart_putc(rxMsg->Data[i]);
  113.                 }
  114.                 break;
  115.             case CAN_MODULE_CMD_SERIAL_SERIALCONFIG:
  116.                 baudRate = ((uint16_t)rxMsg->Data[0] << 0) | ((uint16_t)rxMsg->Data[1] << 8);
  117.                 format = (snsSerialPhyFormat_t)rxMsg->Data[2];
  118.                 uart_init(UART_BAUD_SELECT(baudRate, F_CPU));
  119.                 if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232)
  120.                 {
  121.                     gpio_clr_pin(sns_Serial_485_CONNECT);   // disable RS485_CONNECT
  122.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  123.                     gpio_clr_pin(sns_Serial_485_232);       // RS232 mode
  124.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  125.                     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  126.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  127.                 }
  128.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485FULLDUPLEX)
  129.                 {
  130.                     gpio_clr_pin(sns_Serial_485_CONNECT);   // disable RS485_CONNECT
  131.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  132.                     gpio_set_pin(sns_Serial_485_232);       // RS485 mode
  133.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  134.                     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  135.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  136.                 }
  137.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEX)
  138.                 {
  139.                     gpio_set_pin(sns_Serial_485_CONNECT);   // enable RS485_CONNECT
  140.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  141.                     gpio_set_pin(sns_Serial_485_232);       // RS485 mode
  142.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  143.                     gpio_clr_pin(sns_Serial_TXEN);          // disable TX
  144.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  145.                 }
  146.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEXWITHTERMINATION)
  147.                 {
  148.                     gpio_set_pin(sns_Serial_485_CONNECT);   // enable RS485_CONNECT
  149.                     gpio_set_pin(sns_Serial_TERM_EN);       // enable termination
  150.                     gpio_set_pin(sns_Serial_485_232);       // RS485 mode
  151.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  152.                     gpio_clr_pin(sns_Serial_TXEN);          // disable TX
  153.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  154.                 }
  155.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK)
  156.                 {
  157.                     gpio_clr_pin(sns_Serial_485_CONNECT);   // disable RS485_CONNECT
  158.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  159.                     gpio_set_pin(sns_Serial_485_232);       // RS232 mode
  160.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  161.                     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  162.                     gpio_clr_pin(sns_Serial_ON);            // disable charge pump (i.e. enable loopback)
  163.                 }
  164.                 else
  165.                 {
  166.                     //invalid format
  167.                 }
  168.                 break;
  169.         }
  170.     }
  171. }
  172.  
  173. void sns_Serial_List(uint8_t ModuleSequenceNumber)
  174. {
  175.     StdCan_Msg_t txMsg;
  176.  
  177.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  178.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  179.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  180.     txMsg.Header.ModuleId = sns_Serial_ID;
  181.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  182.     txMsg.Length = 6;
  183.  
  184.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  185.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  186.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  187.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  188.  
  189.     txMsg.Data[4] = NUMBER_OF_MODULES;
  190.     txMsg.Data[5] = ModuleSequenceNumber;
  191.  
  192.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  193. }
  194.