Subversion Repositories HomeAutomation

Rev

Rev 1459 | Rev 1555 | 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. // internal variables
  18. static uint32_t baudRate = 9600;
  19. static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK;
  20.  
  21. void sns_Serial_Init(void)
  22. {
  23. #ifdef sns_Serial_USEEEPROM
  24.     if (EEDATA_OK)
  25.     {
  26.       ///TODO: Use stored data to set initial values for the module
  27.       blablaX = eeprom_read_byte(EEDATA.x);
  28.       blablaY = eeprom_read_word(EEDATA16.y);
  29.     } else
  30.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  31.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  32.       eeprom_write_word_crc(EEDATA16.y, 0x1234, WITHOUT_CRC);
  33.       EEDATA_UPDATE_CRC;
  34.     }
  35. #endif
  36.     // default baudrate
  37.     uart_init(UART_BAUD_SELECT(baudRate, F_CPU));
  38.     // configure control pins
  39.     gpio_set_out(sns_Serial_RXEN);
  40.     gpio_set_out(sns_Serial_TXEN);
  41.     gpio_set_out(sns_Serial_ON);
  42.     gpio_set_out(sns_Serial_485_232);
  43.     gpio_set_out(sns_Serial_TERM_EN);
  44.     gpio_set_out(sns_Serial_485_CONNECT);
  45.     //default to loopback mode until config command received
  46.     gpio_clr_pin(sns_Serial_ON);
  47.     gpio_clr_pin(sns_Serial_485_232);
  48.     gpio_clr_pin(sns_Serial_485_CONNECT);   // disable RS485_CONNECT
  49.     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  50.     gpio_set_pin(sns_Serial_485_232);       // RS232 mode
  51.     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  52.     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  53.  
  54.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  55.     // Pcint_SetCallbackPin(sns_Serial_PCINT, EXP_C , &sns_Serial_pcint_callback);
  56.  
  57.     printf("Serial started!\n");
  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.             printf("RX\n");
  86.             // insert it into the message
  87.             msg.Data[(uint8_t)msg.Length] = c;
  88.             msg.Length++;
  89.         }
  90.     // keep going until max data length reached, or until uart RXBUF is empty
  91.     } while (status == 0 && msg.Length < 8);
  92.    
  93.     // did we fill any data into the message?
  94.     if (msg.Length > 0)
  95.     {
  96.         // transmit this chunk of data (max 8 chars)
  97.         while(StdCan_Put(&msg) != StdCan_Ret_OK);
  98.     }
  99. }
  100.  
  101. void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg)
  102. {
  103.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  104.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  105.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL &&
  106.         rxMsg->Header.ModuleId == sns_Serial_ID)
  107.     {
  108.         switch (rxMsg->Header.Command)
  109.         {
  110.             case CAN_MODULE_CMD_SERIAL_SERIALDATA:
  111.                 gpio_set_pin(sns_Serial_TXEN);          // enable TX
  112.                 for (uint8_t i=0; i<rxMsg->Length; i++)
  113.                 {
  114.                     uart_putc(rxMsg->Data[i]);
  115.                 }
  116.                 gpio_clr_pin(sns_Serial_TXEN);          // disable TX
  117.                 break;
  118.             case CAN_MODULE_CMD_SERIAL_SERIALCONFIG:
  119.                 baudRate = 10 * (((uint32_t)rxMsg->Data[1] << 0) | ((uint32_t)rxMsg->Data[0] << 8));
  120.                 format = (uint16_t)rxMsg->Data[2];
  121.                 uart_init(UART_BAUD_SELECT(baudRate, F_CPU));
  122.                 if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232)
  123.                 {
  124.                     printf("RS232\n");
  125.                     gpio_clr_pin(sns_Serial_485_CONNECT);   // disable RS485_CONNECT
  126.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  127.                     gpio_clr_pin(sns_Serial_485_232);       // RS232 mode
  128.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  129.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  130.                 }
  131.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485FULLDUPLEX)
  132.                 {
  133.                     printf("RS485FD\n");
  134.                     gpio_clr_pin(sns_Serial_485_CONNECT);   // disable RS485_CONNECT
  135.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  136.                     gpio_set_pin(sns_Serial_485_232);       // RS485 mode
  137.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  138.                     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  139.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  140.                 }
  141.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEX)
  142.                 {
  143.                     printf("RS485HD\n");
  144.                     gpio_set_pin(sns_Serial_485_CONNECT);   // enable RS485_CONNECT
  145.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  146.                     gpio_set_pin(sns_Serial_485_232);       // RS485 mode
  147.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  148.                     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  149.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  150.                 }
  151.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS485HALFDUPLEXWITHTERMINATION)
  152.                 {
  153.                     printf("485HDT\n");
  154.                     gpio_set_pin(sns_Serial_485_CONNECT);   // enable RS485_CONNECT
  155.                     gpio_set_pin(sns_Serial_TERM_EN);       // enable termination
  156.                     gpio_set_pin(sns_Serial_485_232);       // RS485 mode
  157.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  158.                     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  159.                     gpio_set_pin(sns_Serial_ON);            // enable charge pump
  160.                 }
  161.                 else if (format == CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK)
  162.                 {
  163.                     printf("LPMODE\n");
  164.                     gpio_clr_pin(sns_Serial_485_CONNECT);   // disable RS485_CONNECT
  165.                     gpio_clr_pin(sns_Serial_TERM_EN);       // disable termination
  166.                     gpio_set_pin(sns_Serial_485_232);       // RS232 mode
  167.                     gpio_set_pin(sns_Serial_RXEN);          // enable RX
  168.                     gpio_set_pin(sns_Serial_TXEN);          // enable TX
  169.                     gpio_clr_pin(sns_Serial_ON);            // disable charge pump (i.e. enable loopback)
  170.                 }
  171.                 else
  172.                 {
  173.                     //invalid format
  174.                     printf("ERROR!\n");
  175.                 }
  176.                 break;
  177.         }
  178.     }
  179. }
  180.  
  181. void sns_Serial_List(uint8_t ModuleSequenceNumber)
  182. {
  183.     StdCan_Msg_t txMsg;
  184.  
  185.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  186.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  187.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  188.     txMsg.Header.ModuleId = sns_Serial_ID;
  189.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  190.     txMsg.Length = 6;
  191.  
  192.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  193.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  194.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  195.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  196.  
  197.     txMsg.Data[4] = NUMBER_OF_MODULES;
  198.     txMsg.Data[5] = ModuleSequenceNumber;
  199.  
  200.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  201. }
  202.  
  203.