Subversion Repositories HomeAutomation

Rev

Rev 1452 | Rev 1454 | 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.     // prepare a new message, in case new data is available
  59.     StdCan_Msg_t msg;
  60.     msg.Length = 0; // no data so far
  61.     StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  62.     StdCan_Set_direction(msg.Header, DIRECTIONFLAG_TO_OWNER);
  63.     msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  64.     msg.Header.ModuleId = sns_Serial_ID;
  65.     msg.Header.Command = CAN_MODULE_CMD_SERIAL_SERIALDATA;
  66.    
  67.     unsigned char status;
  68.    
  69.     do
  70.     {
  71.         // ask uart driver for more data
  72.         unsigned int data = uart_getc();
  73.         // character is contained in LSB
  74.         unsigned char c = (unsigned char)(data & 0x00FF);
  75.         // status is contained in MSB
  76.         status = (unsigned char)((data & 0xFF00) >> 8);
  77.        
  78.         // status == 0 means we just received a new char
  79.         if (status == 0)
  80.         {
  81.             // insert it into the message
  82.             msg.Data[(uint8_t)msg.Length] = c;
  83.             msg.Length++;
  84.         }
  85.     // keep going until max data length reached, or until uart RXBUF is empty
  86.     } while (status == 0 && msg.Length < 8);
  87.    
  88.     // did we fill any data into the message?
  89.     if (msg.Length > 0)
  90.     {
  91.         // transmit this chunk of data (max 8 chars)
  92.         while(StdCan_Put(&msg) != StdCan_Ret_OK);
  93.     }
  94. }
  95.  
  96. void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg)
  97. {
  98.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  99.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  100.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL &&
  101.         rxMsg->Header.ModuleId == sns_Serial_ID)
  102.     {
  103.         switch (rxMsg->Header.Command)
  104.         {
  105.             case CAN_MODULE_CMD_SERIAL_SERIALDATA:
  106.                 for (uint8_t i=0; i<rxMsg->Length; i++)
  107.                 {
  108.                     uart_putc(rxMsg->Data[i]);
  109.                 }
  110.                 break;
  111.             case CAN_MODULE_CMD_SERIAL_SERIALCONFIG:
  112.                 baudRate = ((uint16_t)rxMsg->Data[0] << 0) | ((uint16_t)rxMsg->Data[1] << 8);
  113.                 format = (snsSerialPhyFormat_t)rxMsg->Data[2];
  114.                 uart_init(UART_BAUD_SELECT(baudRate, F_CPU));
  115.                 break;
  116.         }
  117.     }
  118. }
  119.  
  120. void sns_Serial_List(uint8_t ModuleSequenceNumber)
  121. {
  122.     StdCan_Msg_t txMsg;
  123.  
  124.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  125.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  126.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  127.     txMsg.Header.ModuleId = sns_Serial_ID;
  128.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  129.     txMsg.Length = 6;
  130.  
  131.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  132.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  133.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  134.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  135.  
  136.     txMsg.Data[4] = NUMBER_OF_MODULES;
  137.     txMsg.Data[5] = ModuleSequenceNumber;
  138.  
  139.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  140. }
  141.