Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "sns_heatPower.h"
  3.  
  4. const char sns_heatPower_request[sns_heatPower_REQ_STRING_LEN] = sns_heatPower_REQ_STRING;
  5.  
  6. typedef enum {
  7.     PARITY_NONE = 0,
  8.     PARITY_EVEN = 1,
  9.     PARITY_ODD = 2,
  10. } parityMode_t;
  11.  
  12. void sns_heatPower_Init(void)
  13. {
  14.     uart_setDatabits(sns_heatPower_CNF_DATABITS);
  15.     uart_setStopbits(sns_heatPower_CNF_STOPBITS);
  16.     uart_setParity(sns_heatPower_CNF_PARITY!=PARITY_NONE, sns_heatPower_CNF_PARITY==PARITY_ODD);
  17.     uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(sns_heatPower_BAUD_RX, F_CPU));
  18.  
  19.     Timer_SetTimeout(sns_heatPower_REQ_TIMER, sns_heatPower_REQ_INTERVAL_S*1000 , TimerTypeFreeRunning, 0);
  20. }
  21.  
  22. void sns_heatPower_Process(void)
  23. {
  24.     /* At timer timeout send a new request */
  25.     if (Timer_Expired(sns_heatPower_REQ_TIMER))
  26.     {
  27. printf("TX\n");
  28.         /* Set baudrate to transmit baudrate */
  29.         uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(sns_heatPower_BAUD_TX, F_CPU));
  30.        
  31.         /* Send request string */
  32.         uart_puts(sns_heatPower_request);
  33.        
  34.         /* TX buffer must be empty before we change buadrate */
  35.         //while(uart_txbufempty()) {;}
  36.        
  37.         Timer_SetTimeout(sns_heatPower_REQ_TIMER2, 300 , TimerTypeOneShot, 0);     
  38.     }
  39.  
  40.     /* At timer timeout send a new request */
  41.     if (Timer_Expired(sns_heatPower_REQ_TIMER))
  42.     {
  43.         /* Set baudrate to receive baudrate */
  44.         uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(sns_heatPower_BAUD_RX, F_CPU));
  45.     }
  46.    
  47.     unsigned char status;
  48.     do
  49.     {
  50.         /* ask uart driver for more data */
  51.         unsigned int data = uart_getc();
  52.         /* character is contained in LSB */
  53.         unsigned char c = (unsigned char)(data & 0x00FF);
  54.  
  55.         /* status is contained in MSB */
  56.         status = (unsigned char)((data & 0xFF00) >> 8);
  57.        
  58.         /* status == 0 means we just received a new char */
  59.         if (status == 0)
  60.         {
  61.             printf("RX %c\n", c);
  62.             // TODO c contains data, use it! add a counter which reset at TX then filter with config.inc parameters and send can package
  63.         }
  64.         else if (data != UART_NO_DATA)
  65.         {
  66.             printf("RX %u\n", data);
  67.         }
  68.     /* keep going until uart RXBUF is empty */
  69.     } while (status == 0);
  70. }
  71.  
  72. void sns_heatPower_HandleMessage(StdCan_Msg_t *rxMsg)
  73. {
  74.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  75.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  76.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_HEATPOWER &&
  77.         rxMsg->Header.ModuleId == sns_heatPower_ID)
  78.     {
  79.         switch (rxMsg->Header.Command)
  80.         {
  81.         //case CAN_CMD_MODULE_DUMMY:
  82.         ///TODO: Do something dummy
  83.         break;
  84.         }
  85.     }
  86. }
  87.  
  88. void sns_heatPower_List(uint8_t ModuleSequenceNumber)
  89. {
  90.     StdCan_Msg_t txMsg;
  91.  
  92.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  93.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  94.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_HEATPOWER;
  95.     txMsg.Header.ModuleId = sns_heatPower_ID;
  96.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  97.     txMsg.Length = 6;
  98.  
  99.     uint32_t HwId=BIOS_GetHwId();
  100.     txMsg.Data[0] = HwId&0xff;
  101.     txMsg.Data[1] = (HwId>>8)&0xff;
  102.     txMsg.Data[2] = (HwId>>16)&0xff;
  103.     txMsg.Data[3] = (HwId>>24)&0xff;
  104.  
  105.     txMsg.Data[4] = NUMBER_OF_MODULES;
  106.     txMsg.Data[5] = ModuleSequenceNumber;
  107.  
  108.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  109. }
  110.