tsensor_ll.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "drivers/i2c/i2c.h"
  2. #include "app/thermo/tsensor_ll.h"
  3. #include "stm32l1xx_hal.h"
  4. #include "stm32l1xx_hal_i2c.h"
  5. #include "stm32l1xx_hal_gpio.h"
  6. #include "core/main.h"
  7. #include "core/csect.h"
  8. #include "core/config_pins.h"
  9. #define pI2CHandle (&I2C_Handle)
  10. #define GENERAL_TIMEOUT 100 // Due to USB interrupt there is a latency
  11. #define WRITE_ADDRESS_TIMEOUT GENERAL_TIMEOUT
  12. #define WRITE_CONFIG_TIMEOUT GENERAL_TIMEOUT
  13. #define READ_CONFIG_TIMEOUT GENERAL_TIMEOUT
  14. #define READ_TEMP_TIMEOUT GENERAL_TIMEOUT
  15. static bool TSensor_Init();
  16. static void TSensor_DeInit();
  17. static bool TSensor_GetMomentaryTemp( tTSensorTemp * temp );
  18. // Typical startup time is 4us for AD74xx, let it be 10us
  19. #define AD74XX_POWERUP_TIME_us (10) // 4us typical
  20. // Typical conversion time for AD74xx, let it be 40us
  21. #define AD74XX_CONVERSION_TIME_us (40) // 25us typical
  22. #define AD74XX_REG_TEMPERATURE 0x0
  23. #define AD74XX_REG_CONFIGURATION 0x1
  24. #define AD7414_REG_THIGH 0x2
  25. #define AD7414_REG_TLOW 0x3
  26. // AD74xx sensor peroforms the single conversions
  27. // during 4ms startup time plus 29us conversion time
  28. #define AD74XX_ONESHOT_DELAY_us ((AD74XX_POWERUP_TIME_us)+(AD74XX_CONVERSION_TIME_us))
  29. #pragma pack(push,1)
  30. typedef union
  31. {
  32. #if THERMO_SENSOR_MODEL == AD7414
  33. struct ad7414
  34. {
  35. uint8_t test_mode: 2;
  36. uint8_t one_shot: 1;
  37. uint8_t alert_reset: 1;
  38. uint8_t alert_polarity: 1;
  39. uint8_t alert_disable: 1;
  40. uint8_t i2c_filter: 1;
  41. uint8_t power_down: 1;
  42. };
  43. #endif
  44. #if THERMO_SENSOR_MODEL == AD7415
  45. struct ad7415
  46. {
  47. uint8_t test_mode: 2;
  48. uint8_t one_shot: 1;
  49. uint8_t test_mode_2: 3;
  50. uint8_t i2c_filter: 1;
  51. uint8_t power_down: 1;
  52. };
  53. #endif
  54. uint8_t rawbyte;
  55. }
  56. sTSensorConfig;
  57. #pragma pack(pop)
  58. static bool AD74XX_ReadConfiguration( sTSensorConfig * pcfg );
  59. static bool AD74XX_WriteConfiguration( sTSensorConfig cfg );
  60. static bool AD74XX_ReadTemperature( uint8_t * pData, size_t nBytes );
  61. #if THERMO_SENSOR_POWERCONTROL_HW
  62. #if THERMO_SENSOR_POWERCONTROL_HW_KEEPPOWERED
  63. #if THERMO_SENSOR_POWERCONTROL_HW_LAZYCONFIG
  64. static bool Sensor_Configured = false;
  65. #endif
  66. #endif
  67. #endif
  68. const sI2CTempSensorHandle_t I2CTempSensor = {
  69. .Init = TSensor_Init,
  70. .DeInit = TSensor_DeInit,
  71. .GetTemp = TSensor_GetMomentaryTemp
  72. };
  73. static bool TSensor_Init()
  74. {
  75. #if THERMO_SENSOR_POWERCONTROL_HW
  76. // Deinitialize I2C with the lines pulled down
  77. pI2CHandle->DeInit( eI2CPinMode_KeepPullDown );
  78. // Enable sensor VCC power
  79. // Deliver the sensor power directly from the MCU Pin PB5
  80. { // Initialize POWER PIN
  81. GPIO_InitTypeDef GPIO_InitStruct = {0};
  82. GPIO_InitStruct.Pin = CONFIG_PIN__TSENS_PWR;
  83. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  84. GPIO_InitStruct.Pull = GPIO_NOPULL;
  85. HAL_GPIO_Init(CONFIG_PORT__TSENS_PWR, &GPIO_InitStruct);
  86. HAL_GPIO_WritePin( CONFIG_PORT__TSENS_PWR, CONFIG_PIN__TSENS_PWR, GPIO_PIN_SET );
  87. // It is required to wait for some time while sensor starting up
  88. HAL_Wait_us( AD74XX_POWERUP_TIME_us );
  89. }
  90. #endif
  91. #if (THERMO_SENSOR_POWERCONTROL_HW != 0) && (THERMO_SENSOR_POWERCONTROL_HW_KEEPPOWERED != 0)
  92. bool ret = pI2CHandle->Init( eI2CPinMode_KeepPullUp );
  93. #else
  94. bool ret = pI2CHandle->Init( eI2CPinMode_Default );
  95. #endif
  96. if( ret )
  97. {
  98. ret = false;
  99. // It is required to wait for some time while sensor starting up
  100. // Typical startup time is 4us for AD7414
  101. HAL_Wait_us( AD74XX_POWERUP_TIME_us );
  102. #if THERMO_SENSOR_POWERCONTROL_HW
  103. #if THERMO_SENSOR_POWERCONTROL_HW_KEEPPOWERED
  104. #if THERMO_SENSOR_POWERCONTROL_HW_LAZYCONFIG
  105. if( ! Sensor_Configured )
  106. #endif
  107. #endif
  108. #endif
  109. {
  110. sTSensorConfig sensor_cfg;
  111. // Read configuration register
  112. if( AD74XX_ReadConfiguration( &sensor_cfg ) )
  113. {
  114. sensor_cfg.one_shot = 0; // One-Shot conversion - disabled
  115. sensor_cfg.power_down = 1; // Enable POWER-DOWN mode (without periodical conversion)
  116. sensor_cfg.i2c_filter = 1; // Enable I2C-pins filtering
  117. #if THERMO_SENSOR_MODEL == AD7414
  118. sensor_cfg.alert_reset = 0; // Disable ALERT function
  119. sensor_cfg.alert_polarity = 0; // Disable ALERT function
  120. sensor_cfg.alert_disable = 1; // Disable ALERT function
  121. #endif
  122. // Write configuration register:
  123. if( AD74XX_WriteConfiguration( sensor_cfg ) )
  124. {
  125. // Now the sensor is power-down mode
  126. #if THERMO_SENSOR_POWERCONTROL_HW
  127. #if THERMO_SENSOR_POWERCONTROL_HW_KEEPPOWERED
  128. #if THERMO_SENSOR_POWERCONTROL_HW_LAZYCONFIG
  129. Sensor_Configured = true;
  130. #endif
  131. #endif
  132. #endif
  133. ret = true;
  134. }
  135. }
  136. }
  137. #if THERMO_SENSOR_POWERCONTROL_HW
  138. #if THERMO_SENSOR_POWERCONTROL_HW_KEEPPOWERED
  139. #if THERMO_SENSOR_POWERCONTROL_HW_LAZYCONFIG
  140. else
  141. ret = true;
  142. #endif
  143. #endif
  144. #endif
  145. }
  146. if( !ret )
  147. {
  148. TSensor_DeInit();
  149. }
  150. return ret;
  151. }
  152. static void TSensor_DeInit()
  153. {
  154. #if THERMO_SENSOR_POWERCONTROL_HW
  155. #if THERMO_SENSOR_POWERCONTROL_HW_KEEPPOWERED == 0
  156. // Disable sensor VCC power
  157. // The sensor power is delivered directly from the MCU Pin PB5
  158. {
  159. // Disable this pin
  160. GPIO_InitTypeDef GPIO_InitStruct = {0};
  161. GPIO_InitStruct.Pin = CONFIG_PIN__TSENS_PWR;
  162. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  163. GPIO_InitStruct.Pull = GPIO_NOPULL;
  164. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  165. HAL_GPIO_Init(CONFIG_PORT__TSENS_PWR, &GPIO_InitStruct);
  166. // #if THERMO_SENSOR_POWERCONTROL_HW_LAZYCONFIG
  167. // Sensor_Configured = false;
  168. // #endif
  169. }
  170. #endif
  171. #endif
  172. #if (THERMO_SENSOR_POWERCONTROL_HW != 0) && (THERMO_SENSOR_POWERCONTROL_HW_KEEPPOWERED != 0)
  173. pI2CHandle->DeInit(eI2CPinMode_KeepPullUp);
  174. #else
  175. pI2CHandle->DeInit(eI2CPinMode_Default);
  176. #endif
  177. }
  178. static bool AD74XX_WriteConfiguration( sTSensorConfig cfg )
  179. {
  180. #if THERMO_SENSOR_MODEL == AD7414
  181. cfg.test_mode = 0;
  182. #endif
  183. #if THERMO_SENSOR_MODEL == AD7415
  184. cfg.test_mode = 0;
  185. cfg.test_mode_2 = 0;
  186. #endif
  187. uint8_t write_transaction[2] = { [0] = AD74XX_REG_CONFIGURATION, [1] = cfg.rawbyte };
  188. if( HAL_OK == pI2CHandle->Transmit(
  189. THERMO_SENSOR_8bit_ADDRESS,
  190. write_transaction, sizeof(write_transaction),
  191. WRITE_CONFIG_TIMEOUT ) )
  192. {
  193. return true;
  194. }
  195. return false;
  196. }
  197. static bool AD74XX_ReadConfiguration( sTSensorConfig * pcfg )
  198. {
  199. uint8_t register_address = AD74XX_REG_CONFIGURATION;
  200. if( NULL != pcfg )
  201. {
  202. if( HAL_OK == pI2CHandle->Transmit(
  203. THERMO_SENSOR_8bit_ADDRESS,
  204. &register_address, 1,
  205. WRITE_ADDRESS_TIMEOUT ) )
  206. {
  207. if( HAL_OK == pI2CHandle->Receive(
  208. THERMO_SENSOR_8bit_ADDRESS,
  209. &pcfg->rawbyte, 1,
  210. READ_CONFIG_TIMEOUT ) )
  211. {
  212. return true;
  213. }
  214. }
  215. }
  216. return false;
  217. }
  218. static bool AD74XX_ReadTemperature( uint8_t * pData, size_t nBytes )
  219. {
  220. uint8_t register_address = AD74XX_REG_TEMPERATURE;
  221. if( NULL != pData && (nBytes>0) )
  222. {
  223. if( HAL_OK == pI2CHandle->Transmit(
  224. THERMO_SENSOR_8bit_ADDRESS,
  225. &register_address, 1,
  226. WRITE_ADDRESS_TIMEOUT ) )
  227. {
  228. if( nBytes > 2 ) nBytes = 2; // maximum 2 bytes
  229. if( HAL_OK == pI2CHandle->Receive(
  230. THERMO_SENSOR_8bit_ADDRESS,
  231. pData, nBytes,
  232. READ_TEMP_TIMEOUT ) )
  233. {
  234. return true;
  235. }
  236. }
  237. }
  238. return false;
  239. }
  240. static bool TSensor_GetMomentaryTemp( tTSensorTemp * temp )
  241. {
  242. sTSensorConfig sensor_cfg;
  243. // Read configuration register
  244. if( AD74XX_ReadConfiguration( &sensor_cfg ) )
  245. {
  246. sensor_cfg.power_down = 1; // set the POWER-DOWN mode (do not measure by itself)
  247. sensor_cfg.one_shot = 1; // set ONE-SHOT measuing mode
  248. if( AD74XX_WriteConfiguration( sensor_cfg ) )
  249. {
  250. HAL_Wait_us( AD74XX_ONESHOT_DELAY_us );
  251. uint8_t raw_temp[2];
  252. if( AD74XX_ReadTemperature( raw_temp, sizeof(raw_temp) ) )
  253. {
  254. *((uint16_t*)temp) = ((uint16_t)raw_temp[0] << 8) | (uint16_t)raw_temp[1];
  255. return true;
  256. }
  257. }
  258. }
  259. return false;
  260. }