|
|
@@ -37,7 +37,7 @@ struct
|
|
|
uint32_t timestamp;
|
|
|
uint32_t tsk_timer;
|
|
|
int retry;
|
|
|
- int rx_len;
|
|
|
+ volatile int rx_len;
|
|
|
uint32_t rx_timer;
|
|
|
uint8_t rx_buf[128];
|
|
|
} sensor_ctx;
|
|
|
@@ -132,58 +132,73 @@ void sensor_task(void)
|
|
|
sensor_ctx.tsk_sta = SENSOR_STA_WAIT_PWR;
|
|
|
break;
|
|
|
case SENSOR_STA_WAIT_PWR: // 等待传感器上电完成
|
|
|
- if (HAL_GetTick() - sensor_ctx.tsk_timer > 1000)
|
|
|
+ if (HAL_GetTick() - sensor_ctx.tsk_timer > 3000)
|
|
|
{
|
|
|
sensor_ctx.tsk_sta = SENSOR_STA_TX;
|
|
|
}
|
|
|
break;
|
|
|
case SENSOR_STA_TX:
|
|
|
{
|
|
|
- const uint8_t read_cmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};
|
|
|
+ const uint8_t read_cmd[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
|
|
|
|
|
|
sensor_set_bus_sta(BUS_STA_TX);
|
|
|
sensor_write((uint8_t *)read_cmd, sizeof(read_cmd));
|
|
|
sensor_set_bus_sta(BUS_STA_RX);
|
|
|
- sensor_ctx.tsk_sta = SENSOR_STA_WAIT_ACK;
|
|
|
+
|
|
|
+ sensor_ctx.rx_len = 0;
|
|
|
sensor_ctx.tsk_timer = HAL_GetTick();
|
|
|
sensor_ctx.retry++;
|
|
|
+
|
|
|
+ sensor_ctx.tsk_sta = SENSOR_STA_WAIT_ACK;
|
|
|
}
|
|
|
break;
|
|
|
case SENSOR_STA_WAIT_ACK: // 等待传感器应答
|
|
|
{
|
|
|
int need_retry = 0;
|
|
|
|
|
|
- if (sensor_ctx.rx_len >= 19)
|
|
|
+ if (sensor_ctx.rx_len >= 9)
|
|
|
{
|
|
|
- if (CRC16(sensor_ctx.rx_buf, 19) == 0)
|
|
|
+ if(CRC16(sensor_ctx.rx_buf, 9) == 0)
|
|
|
{
|
|
|
- sensor_data.temperature = ((uint16_t)sensor_ctx.rx_buf[7] << 8) + sensor_ctx.rx_buf[8];
|
|
|
- sensor_data.humidity = (uint16_t)(sensor_ctx.rx_buf[9] << 8) + sensor_ctx.rx_buf[10];
|
|
|
- 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]);
|
|
|
+ int16_t temperature = (int16_t)(((uint16_t)sensor_ctx.rx_buf[3] << 8) | sensor_ctx.rx_buf[4]);
|
|
|
+ int16_t humidity = (int16_t)(((uint16_t)sensor_ctx.rx_buf[5] << 8) | sensor_ctx.rx_buf[6]);
|
|
|
+ if (temperature == 0x7FFF || humidity == 0x7FFF)
|
|
|
+ { // 传感器还没准备好
|
|
|
+ dbg_log("read sensor invalid: temperature=0x%04X, humidity=0x%04X", temperature, humidity);
|
|
|
+ sensor_ctx.rx_len = 0; sensor_ctx.tsk_timer = HAL_GetTick();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ { // 传感器读到正确值
|
|
|
+ sensor_data.temperature = temperature;
|
|
|
+ sensor_data.humidity = humidity;
|
|
|
+ sensor_data.illuminance = 0;
|
|
|
+ sensor_ctx.retry = 0;
|
|
|
+ dbg_log("read sensor success: temperature=0x%04X, humidity=0x%04X", temperature, humidity);
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
need_retry = 1;
|
|
|
}
|
|
|
}
|
|
|
- else if (HAL_GetTick() - sensor_ctx.tsk_timer < 500)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
else
|
|
|
{
|
|
|
- need_retry = 1;
|
|
|
+ if (HAL_GetTick() - sensor_ctx.tsk_timer < 1000)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ need_retry = 1;
|
|
|
}
|
|
|
+
|
|
|
if (need_retry)
|
|
|
{
|
|
|
- if (sensor_ctx.retry < 3)
|
|
|
+ if (sensor_ctx.retry < 5)
|
|
|
{
|
|
|
- sensor_ctx.rx_len = 0;
|
|
|
- sensor_ctx.tsk_timer = 0;
|
|
|
sensor_ctx.tsk_sta = SENSOR_STA_TX;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
// 成功采集到数据或达到最大重试次数
|
|
|
// 保存数据
|
|
|
rec_data_t rec_data;
|
|
|
@@ -203,9 +218,10 @@ void sensor_task(void)
|
|
|
else
|
|
|
{
|
|
|
rec_data.timestamp = sensor_ctx.timestamp;
|
|
|
- rec_write(&rec_data);
|
|
|
}
|
|
|
|
|
|
+ rec_write(&rec_data);
|
|
|
+
|
|
|
sensor_ctx.tsk_sta = SENSOR_STA_STOP;
|
|
|
HAL_GPIO_WritePin(PWR_RS485_GPIO_Port, PWR_RS485_Pin, GPIO_PIN_RESET);
|
|
|
|