sensor.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "stdint.h"
  2. #include "string.h"
  3. #include "main.h"
  4. #include "sensor.h"
  5. #include "usart.h"
  6. #include "stdio.h"
  7. #include "crc.h"
  8. #include "pm.h"
  9. #include "..\Core\Inc\adc.h"
  10. #include "dma.h"
  11. #include "rec.h"
  12. #include "app_rtc.h"
  13. #include "spi.h"
  14. #define ADC_BUF_SZ (50ul)
  15. typedef enum
  16. {
  17. BUS_STA_DISABLE = 0,
  18. BUS_STA_TX = 1,
  19. BUS_STA_RX = 2,
  20. } bus_sta_t;
  21. typedef enum
  22. {
  23. SENSOR_STA_IDLE = 0,
  24. SENSOR_STA_START,
  25. SENSOR_STA_WAIT_PWR,
  26. SENSOR_STA_TX,
  27. SENSOR_STA_WAIT_ACK,
  28. SENSOR_STA_STOP,
  29. } sensor_sta_t;
  30. struct
  31. {
  32. sensor_sta_t tsk_sta;
  33. uint32_t timestamp;
  34. uint32_t tsk_timer;
  35. int retry;
  36. int rx_len;
  37. uint32_t rx_timer;
  38. uint8_t rx_buf[128];
  39. } sensor_ctx;
  40. volatile uint16_t adc_buf[ADC_BUF_SZ][2];
  41. sensor_data_t sensor_data;
  42. void sensor_set_bus_sta(bus_sta_t sta)
  43. {
  44. switch (sta)
  45. {
  46. default:
  47. case BUS_STA_DISABLE:
  48. HAL_GPIO_WritePin(RS485_DE_GPIO_Port, RS485_DE_Pin, GPIO_PIN_RESET);
  49. HAL_GPIO_WritePin(RS485_RE_GPIO_Port, RS485_RE_Pin, GPIO_PIN_SET);
  50. break;
  51. case BUS_STA_TX:
  52. HAL_GPIO_WritePin(RS485_DE_GPIO_Port, RS485_DE_Pin, GPIO_PIN_SET);
  53. HAL_GPIO_WritePin(RS485_RE_GPIO_Port, RS485_RE_Pin, GPIO_PIN_SET);
  54. break;
  55. case BUS_STA_RX:
  56. HAL_GPIO_WritePin(RS485_DE_GPIO_Port, RS485_DE_Pin, GPIO_PIN_RESET);
  57. HAL_GPIO_WritePin(RS485_RE_GPIO_Port, RS485_RE_Pin, GPIO_PIN_RESET);
  58. break;
  59. }
  60. }
  61. static void sensor_write(uint8_t *buf, int sz)
  62. {
  63. if (sz)
  64. {
  65. for (int i = 0; i < sz; i++)
  66. {
  67. LL_USART_TransmitData8(COM_RS485, buf[i]);
  68. while (!LL_USART_IsActiveFlag_TC(COM_RS485))
  69. ;
  70. }
  71. }
  72. }
  73. void sensor_init(uint32_t timestamp)
  74. {
  75. if (pm_get_lock("sensor") < 0)
  76. {
  77. return;
  78. }
  79. MX_SPI2_Init();
  80. MX_USART3_UART_Init();
  81. memset(&sensor_ctx, 0, sizeof(sensor_ctx));
  82. memset(&sensor_data, 0, sizeof(sensor_data));
  83. LL_USART_Enable(COM_RS485);
  84. LL_USART_EnableIT_RXNE(COM_RS485);
  85. MX_DMA_Init();
  86. MX_ADC1_Init();
  87. HAL_ADCEx_Calibration_Start(&hadc1);
  88. sensor_ctx.tsk_sta = SENSOR_STA_START;
  89. sensor_ctx.timestamp = timestamp;
  90. }
  91. void sensor_deinit(void)
  92. {
  93. LL_USART_DisableIT_RXNE(COM_RS485);
  94. LL_USART_Disable(COM_RS485);
  95. LL_USART_DeInit(COM_RS485);
  96. LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_USART3);
  97. extern DMA_HandleTypeDef hdma_adc1;
  98. HAL_ADC_Stop(&hadc1);
  99. HAL_ADC_Stop_DMA(&hadc1);
  100. HAL_ADC_DeInit(&hadc1);
  101. HAL_DMA_DeInit(&hdma_adc1);
  102. ADC_Disable(&hadc1);
  103. }
  104. void sensor_task(void)
  105. {
  106. switch (sensor_ctx.tsk_sta)
  107. {
  108. default:
  109. case SENSOR_STA_IDLE:
  110. break;
  111. case SENSOR_STA_START: // 传感器上电,等待一段时间后发送查询命令
  112. // 使能传感器电源
  113. HAL_GPIO_WritePin(PWR_RS485_GPIO_Port, PWR_RS485_Pin, GPIO_PIN_SET);
  114. // 250930 增加ADC分压电阻电源控制
  115. HAL_GPIO_WritePin(PWR_ADC_GPIO_Port, PWR_ADC_Pin, GPIO_PIN_SET);
  116. HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_buf, sizeof(adc_buf) / sizeof(uint16_t));
  117. sensor_ctx.tsk_timer = HAL_GetTick();
  118. sensor_ctx.tsk_sta = SENSOR_STA_WAIT_PWR;
  119. break;
  120. case SENSOR_STA_WAIT_PWR: // 等待传感器上电完成
  121. if (HAL_GetTick() - sensor_ctx.tsk_timer > 1000)
  122. {
  123. sensor_ctx.tsk_sta = SENSOR_STA_TX;
  124. }
  125. break;
  126. case SENSOR_STA_TX:
  127. {
  128. const uint8_t read_cmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};
  129. sensor_set_bus_sta(BUS_STA_TX);
  130. sensor_write((uint8_t *)read_cmd, sizeof(read_cmd));
  131. sensor_set_bus_sta(BUS_STA_RX);
  132. sensor_ctx.tsk_sta = SENSOR_STA_WAIT_ACK;
  133. sensor_ctx.tsk_timer = HAL_GetTick();
  134. sensor_ctx.retry++;
  135. }
  136. break;
  137. case SENSOR_STA_WAIT_ACK: // 等待传感器应答
  138. {
  139. int need_retry = 0;
  140. if (sensor_ctx.rx_len >= 19)
  141. {
  142. if (CRC16(sensor_ctx.rx_buf, 19) == 0)
  143. {
  144. sensor_data.temperature = ((uint16_t)sensor_ctx.rx_buf[7] << 8) + sensor_ctx.rx_buf[8];
  145. sensor_data.humidity = (uint16_t)(sensor_ctx.rx_buf[9] << 8) + sensor_ctx.rx_buf[10];
  146. sensor_data.illuminance = ((uint32_t)(sensor_ctx.rx_buf[13] << 8) + sensor_ctx.rx_buf[14]) * 10 + ((uint32_t)(sensor_ctx.rx_buf[15] << 8) + sensor_ctx.rx_buf[16]);
  147. }
  148. else
  149. {
  150. need_retry = 1;
  151. }
  152. }
  153. else if (HAL_GetTick() - sensor_ctx.tsk_timer < 500)
  154. {
  155. break;
  156. }
  157. else
  158. {
  159. need_retry = 1;
  160. }
  161. if (need_retry)
  162. {
  163. if (sensor_ctx.retry < 3)
  164. {
  165. sensor_ctx.rx_len = 0;
  166. sensor_ctx.tsk_timer = 0;
  167. sensor_ctx.tsk_sta = SENSOR_STA_TX;
  168. break;
  169. }
  170. }
  171. // 成功采集到数据或达到最大重试次数
  172. // 保存数据
  173. rec_data_t rec_data;
  174. rec_data.humidity = sensor_data.humidity;
  175. rec_data.illuminance = sensor_data.illuminance;
  176. rec_data.temperature = sensor_data.temperature;
  177. rec_data.voltage = sensor_data.voltage;
  178. if (sensor_ctx.timestamp == 0)
  179. {
  180. rtc_time_t time;
  181. rtc_get_time(&time);
  182. rec_data.timestamp = rtc_mktime(&time);
  183. }
  184. else
  185. {
  186. rec_data.timestamp = sensor_ctx.timestamp;
  187. rec_write(&rec_data);
  188. }
  189. sensor_ctx.tsk_sta = SENSOR_STA_STOP;
  190. HAL_GPIO_WritePin(PWR_RS485_GPIO_Port, PWR_RS485_Pin, GPIO_PIN_RESET);
  191. // 250930 增加ADC分压电阻电源控制
  192. HAL_GPIO_WritePin(PWR_ADC_GPIO_Port, PWR_ADC_Pin, GPIO_PIN_RESET);
  193. sensor_set_bus_sta(BUS_STA_DISABLE);
  194. }
  195. break;
  196. case SENSOR_STA_STOP: // 读取完成或达到最大重试次数
  197. sensor_deinit();
  198. sensor_ctx.tsk_sta = SENSOR_STA_IDLE;
  199. pm_release_lock("sensor");
  200. break;
  201. }
  202. }
  203. int sensor_is_busy(void)
  204. {
  205. return sensor_ctx.tsk_sta != SENSOR_STA_IDLE;
  206. }
  207. void sensor_irq_handle(void)
  208. {
  209. if (LL_USART_IsEnabledIT_RXNE(COM_RS485) && LL_USART_IsActiveFlag_RXNE(COM_RS485))
  210. {
  211. uint8_t tmp;
  212. tmp = LL_USART_ReceiveData8(COM_RS485);
  213. if (sensor_ctx.rx_len < sizeof(sensor_ctx.rx_buf))
  214. {
  215. sensor_ctx.rx_buf[sensor_ctx.rx_len] = tmp;
  216. sensor_ctx.rx_len++;
  217. }
  218. sensor_ctx.rx_timer = HAL_GetTick();
  219. }
  220. if (LL_USART_IsActiveFlag_ORE(COM_RS485))
  221. {
  222. LL_USART_ClearFlag_ORE(COM_RS485);
  223. LL_USART_DisableIT_ERROR(COM_RS485);
  224. }
  225. if (LL_USART_IsEnabledIT_TC(COM_RS485) && LL_USART_IsActiveFlag_TC(COM_RS485))
  226. {
  227. LL_USART_ClearFlag_TC(COM_RS485);
  228. LL_USART_DisableIT_TC(COM_RS485);
  229. }
  230. }
  231. /*
  232. 分压电阻R1=47K,R2=10K
  233. VCC------|||||------|||||------GND
  234. | | |
  235. R1 V R2
  236. */
  237. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  238. {
  239. uint32_t sum = 0;
  240. uint32_t vref = 0;
  241. for (int i = 0; i < ADC_BUF_SZ; i++)
  242. {
  243. vref += adc_buf[i][0];
  244. sum += adc_buf[i][1];
  245. }
  246. sum = sum / ADC_BUF_SZ;
  247. vref = __HAL_ADC_CALC_VREFANALOG_VOLTAGE(vref/ADC_BUF_SZ, ADC_RESOLUTION_12B);
  248. sensor_data.voltage = __HAL_ADC_CALC_DATA_TO_VOLTAGE(vref, sum, ADC_RESOLUTION_12B)*57/10ul;
  249. // 250927 电压单位转换以及防反接二极管的电压补偿统一在这里操作
  250. sensor_data.voltage = (sensor_data.voltage + 360) / 100;
  251. }
  252. void sensor_get_data(sensor_data_t *data)
  253. {
  254. if(data)
  255. {
  256. memcpy(data, &sensor_data, sizeof(sensor_data));
  257. }
  258. }