Subversion Repositories HomeAutomation

Rev

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. void sns_Serial_Init(void)
  18. {
  19. #ifdef sns_Serial_USEEEPROM
  20.     if (EEDATA_OK)
  21.     {
  22.       ///TODO: Use stored data to set initial values for the module
  23.       blablaX = eeprom_read_byte(EEDATA.x);
  24.       blablaY = eeprom_read_word(EEDATA16.y);
  25.     } else
  26.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  27.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  28.       eeprom_write_word_crc(EEDATA16.y, 0x1234, WITHOUT_CRC);
  29.       EEDATA_UPDATE_CRC;
  30.     }
  31. #endif
  32.     ///TODO: Initialize hardware etc here
  33.     uart_init(UART_BAUD_SELECT(9600, F_CPU));
  34.     gpio_set_out(sns_Serial_RXEN);
  35.     gpio_set_out(sns_Serial_TXEN);
  36.     gpio_set_pin(sns_Serial_RXEN);
  37.     gpio_set_pin(sns_Serial_TXEN);
  38.  
  39.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  40.     // Pcint_SetCallbackPin(sns_Serial_PCINT, EXP_C , &sns_Serial_pcint_callback);
  41.  
  42. }
  43.  
  44. void sns_Serial_Process(void)
  45. {
  46.     ///TODO: Stuff that needs doing is done here
  47.     unsigned int data = uart_getc();
  48.     unsigned char c = (unsigned char)(data & 0x00FF);
  49.     unsigned char status = (unsigned char)((data & 0xFF00) >> 8);
  50.    
  51.     if (status == 0)
  52.     {
  53.         // data received from uart
  54.         StdCan_Msg_t msg;
  55.         msg.Length = 1;
  56.         msg.Data[0] = c;
  57.         StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
  58.         StdCan_Set_direction(msg.Header, DIRECTIONFLAG_TO_OWNER);
  59.         msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  60.         msg.Header.ModuleId = sns_Serial_ID;
  61.         msg.Header.Command = CAN_MODULE_CMD_SERIAL_SERIALDATA;
  62.         // transmit
  63.         while(StdCan_Put(&msg) != StdCan_Ret_OK);
  64.     }
  65. }
  66.  
  67. void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg)
  68. {
  69.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  70.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  71.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL &&
  72.         rxMsg->Header.ModuleId == sns_Serial_ID)
  73.     {
  74.         switch (rxMsg->Header.Command)
  75.         {
  76.         case CAN_MODULE_CMD_SERIAL_SERIALDATA:
  77.             for (uint8_t i=0; i<rxMsg->Length; i++)
  78.             {
  79.                 uart_putc(rxMsg->Data[i]);
  80.             }
  81.             break;
  82.         }
  83.     }
  84. }
  85.  
  86. void sns_Serial_List(uint8_t ModuleSequenceNumber)
  87. {
  88.     StdCan_Msg_t txMsg;
  89.  
  90.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  91.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  92.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_SERIAL;
  93.     txMsg.Header.ModuleId = sns_Serial_ID;
  94.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  95.     txMsg.Length = 6;
  96.  
  97.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  98.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  99.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  100.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  101.  
  102.     txMsg.Data[4] = NUMBER_OF_MODULES;
  103.     txMsg.Data[5] = ModuleSequenceNumber;
  104.  
  105.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  106. }
  107.