Subversion Repositories HomeAutomation

Rev

Rev 1957 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #include "sns_ds18x20.h"
  2.  
  3. #ifdef CHN_LOADED
  4. #include "../chn_ChnMaster/chn_ChnMaster.h"
  5.  
  6. uint16_t sns_ds18s20_sensorids[DS18x20_MAXSENSORS] = {sns_ds18x20_chn_sensors};
  7. #endif
  8.  
  9. void ConvertTemperature(void);
  10. void ConvertTemperature_callback(uint8_t timer);
  11. void ReadTemperature(void);
  12. void ReadTemperature_callback(uint8_t timer);
  13.  
  14. uint8_t sns_ds18x20_ReportInterval = (uint8_t)sns_ds18x20_SEND_PERIOD;
  15. uint8_t NumberOfSensors = 0;
  16. uint16_t SensorIds[DS18x20_MAXSENSORS];
  17. uint8_t FlagReadTemperature = 0, FlagConvertTemperature = 0, FlagSearchSensors = 0;
  18.  
  19. void ReadTemperature_callback(uint8_t timer)
  20. {
  21.     FlagReadTemperature = 1;
  22. }
  23.  
  24.  
  25. void ReadTemperature(void)
  26. {
  27.     static uint8_t SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  28.     static uint8_t CurrentSensor = 0;
  29.     uint8_t subzero, cel, cel_frac_bits = 0;
  30.     StdCan_Msg_t txMsg;
  31.  
  32.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  33.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  34.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  35.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  36.     txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_TEMPERATURE_CELSIUS;
  37.     txMsg.Length = 3;
  38.  
  39.     txMsg.Data[0] = SensorIds[CurrentSensor];
  40.  
  41.     if (DS18X20_read_meas(&gSensorIDs[CurrentSensor][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK)
  42.     {
  43.         int16_t temp = cel;
  44.  
  45.         temp = temp << 6;
  46.  
  47.         temp += (cel_frac_bits << 2);
  48.  
  49.         if (subzero)
  50.         {
  51.             temp = -temp;
  52.         }
  53.  
  54.         txMsg.Data[1] = (uint8_t)(temp >> 8);
  55.         txMsg.Data[2] = (uint8_t)(temp & 0xff);
  56.  
  57. #ifdef CHN_LOADED
  58.         for( uint8_t i=0; i<DS18x20_MAXSENSORS; i++) {
  59.             if( sns_ds18s20_sensorids[i] == SensorIds[CurrentSensor] ) {
  60.                     chn_ChnMaster_UpdateChannel( sns_ds18x20_CHN_CHANNEL + i, (temp<<2) + 25600 );
  61.             }
  62.         }
  63. #endif
  64.     }
  65.    
  66.     StdCan_Put(&txMsg);
  67.  
  68.     CurrentSensor++;
  69.     if (CurrentSensor < NumberOfSensors)
  70.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_SEND_DELAY, TimerTypeOneShot, &ReadTemperature_callback);
  71.     else
  72.     {
  73.         SearchSensorsTimeout--;
  74.         if (SearchSensorsTimeout == 0)
  75.         {
  76.             SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  77.             FlagSearchSensors = 1;
  78.         }
  79.        
  80.         CurrentSensor = 0;
  81.  
  82.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_ReportInterval*1000, TimerTypeOneShot, &ConvertTemperature_callback);
  83.     }
  84. }
  85.  
  86. void ConvertTemperature_callback(uint8_t timer)
  87. {
  88.     FlagConvertTemperature = 1;
  89. }
  90.  
  91. void ConvertTemperature(void)
  92. {
  93.     if (DS18X20_start_meas(DS18X20_POWER_PARASITE, NULL) == DS18X20_OK)
  94.     {
  95.         Timer_SetTimeout(sns_ds18x20_TIMER, DS18B20_TCONV_12BIT, TimerTypeOneShot, &ReadTemperature_callback);
  96.     }
  97. }
  98.  
  99. void sns_ds18x20_Init(void)
  100. {
  101.     /* Make sure there is no more then 4 sensors. */
  102.     FlagSearchSensors = 1;
  103.     NumberOfSensors = 0;
  104.     uint8_t i;
  105.     for (i=0; i < DS18x20_MAXSENSORS;i++) {
  106.       SensorIds[i] = 0;
  107.     }
  108.     Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_ReportInterval*1000, TimerTypeFreeRunning, &ConvertTemperature_callback);
  109. }
  110.  
  111. void sns_ds18x20_Process(void)
  112. {
  113.     if (FlagConvertTemperature)
  114.     {
  115.         ConvertTemperature();
  116.         FlagConvertTemperature = 0;
  117.     }
  118.  
  119.     if (FlagSearchSensors)
  120.     {
  121.         NumberOfSensors = search_sensors();
  122.         uint8_t i, j;
  123.  
  124.         for(i = 0; i < NumberOfSensors; i++)
  125.         {
  126.             SensorIds[i] = 0;
  127.             for (j = 0; j < 7; j++)
  128.             {
  129.                 SensorIds[i] ^= gSensorIDs[i][j];  
  130.             }
  131.         }
  132.         FlagSearchSensors = 0;
  133.     }
  134.  
  135.     if (FlagReadTemperature)
  136.     {
  137.         ReadTemperature();
  138.         FlagReadTemperature = 0;
  139.     }
  140. }
  141.  
  142. void sns_ds18x20_HandleMessage(StdCan_Msg_t *rxMsg)
  143. {
  144.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  145.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  146.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_DS18X20 &&
  147.         rxMsg->Header.ModuleId == sns_ds18x20_ID)
  148.     {
  149.         switch (rxMsg->Header.Command)
  150.         {
  151.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  152.         if (rxMsg->Length > 0)
  153.             sns_ds18x20_ReportInterval = rxMsg->Data[0];
  154.  
  155.         StdCan_Msg_t txMsg;
  156.  
  157.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  158.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  159.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  160.         txMsg.Header.ModuleId = sns_ds18x20_ID;
  161.         txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  162.         txMsg.Length = 1;
  163.  
  164.         txMsg.Data[0] = sns_ds18x20_ReportInterval;
  165.  
  166.         StdCan_Put(&txMsg);
  167.         break;
  168.         }
  169.     }
  170. }
  171.  
  172. void sns_ds18x20_List(uint8_t ModuleSequenceNumber)
  173. {
  174.     StdCan_Msg_t txMsg;
  175.    
  176.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  177.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  178.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  179.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  180.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  181.     txMsg.Length = 6;
  182.  
  183.     uint32_t HwId=BIOS_GetHwId();
  184.     txMsg.Data[0] = HwId&0xff;
  185.     txMsg.Data[1] = (HwId>>8)&0xff;
  186.     txMsg.Data[2] = (HwId>>16)&0xff;
  187.     txMsg.Data[3] = (HwId>>24)&0xff;
  188.    
  189.     txMsg.Data[4] = NUMBER_OF_MODULES;
  190.     txMsg.Data[5] = ModuleSequenceNumber;
  191.    
  192.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  193. }
  194.