diff --git a/APP/peripheral.c b/APP/peripheral.c index b5d0f30..01cafbc 100644 --- a/APP/peripheral.c +++ b/APP/peripheral.c @@ -21,14 +21,17 @@ #include "bsp_valve.h" +#include "bsp_uart.h" #include "log.h" +#include "bsp_i2c.h" +#include "bsp_adc.h" + #undef LOG_ENABLE #define LOG_ENABLE 1 #undef LOG_TAG -#define LOG_TAG "BSP_VALVE" - +#define LOG_TAG "peripheral" /********************************************************************* @@ -760,14 +763,61 @@ static void performPeriodicTask(void) // // peripheralChar4Notify(notiData, SIMPLEPROFILE_CHAR4_LEN); // peripheralChar4Notify(notiData, 5); // counter++; - TsRawFrameData RawData; - static uint8_t humi; - humi = 0; - BSP_VALVE_Generate_Data(&RawData, 30, 25, humi++); - if (humi > 99) + + #if 1 + // VBAT + uint32_t CountBat = 0; + uint16_t adcBuff[40]; + uint8_t i = 0; + // uint32_t countadc = 0; + uint16_t min_number = 0; + uint16_t max_number = 0; + + ADC_InterBATSampInit(); + for (i = 0; i < 20; i++) { - humi = 0; + adcBuff[i] = ADC_ExcutSingleConver(); // 连续采样20次 } + for (i = 0; i < 20; i++) + { + CountBat += adcBuff[i]; + if (i == 0) + { + min_number = adcBuff[i]; + max_number = adcBuff[i]; + } + min_number = ((min_number > adcBuff[i]) ? adcBuff[i] : min_number); // 软件滤波 + max_number = ((max_number < adcBuff[i]) ? adcBuff[i] : max_number); + } + logDebug("min_number = %d, max_number = %d", min_number, max_number); + CountBat = (CountBat - min_number - max_number) / 18; // 删除最小与最大值 + logDebug("AverageCountBat = %d", CountBat); +#endif + + int ret; + + GXHTC3C_Wakeup(); + DelayMs(20); + + GXHTC3C_GetStart(); + DelayMs(20); + float humi, temp; + ret = GXHTC3C_GetTempHumi(&humi, &temp); + if (ret == 0) + { + logDebug("humi %.2f %, temp %.2f C", humi, temp); + } + + GXHTC3C_Sleep(); + + TsRawFrameData RawData; +// static uint8_t humi; +// humi++; + BSP_VALVE_Generate_Data(&RawData, 0, CountBat/100, temp, humi); +// if (humi > 99) +// { +// humi = 0; +// } peripheralChar4Notify(&RawData.buf[0], RawData.len); } diff --git a/BSP/inc/bsp_uart.h b/BSP/inc/bsp_uart.h index 34acb6d..938cfd1 100644 --- a/BSP/inc/bsp_uart.h +++ b/BSP/inc/bsp_uart.h @@ -4,8 +4,8 @@ #include "CONFIG.h" -#define UART1_RX_BUFFER_LENGTH 64U -#define UART1_TX_BUFFER_LENGTH 1024U +#define UART1_RX_BUFFER_LENGTH 24 +#define UART1_TX_BUFFER_LENGTH 256 void BSP_UART1_Init(uint32_t baudrate); @@ -22,14 +22,5 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len); - - - - - - - - - #endif diff --git a/BSP/inc/bsp_valve.h b/BSP/inc/bsp_valve.h index 2b58587..76ed40d 100644 --- a/BSP/inc/bsp_valve.h +++ b/BSP/inc/bsp_valve.h @@ -2,7 +2,7 @@ * @Author : stark1898y 1658608470@qq.com * @Date : 2024-12-12 15:55:38 * @LastEditors : stark1898y 1658608470@qq.com - * @LastEditTime : 2024-12-12 19:08:06 + * @LastEditTime : 2024-12-15 10:40:07 * @FilePath : \BLE_TYQ_CH592F\BSP\inc\bsp_valve.h * @Description : * @@ -13,6 +13,15 @@ #include "CONFIG.h" +#define VAVLE_RX_DATA_EVT (0x0001 << 0) +#define VAVLE_TX_DATA_EVT (0x0001 << 1) + +#define VAVLE_CLOSE_START_EVT (0x0001 << 2) // 鍏抽榾锛屽紑濮嬪厖鐢 +#define VAVLE_CLOSE_END_EVT (0x0001 << 2) // 鍏抽榾锛屽紑濮嬪厖鐢 + +#define CHARGE_TIME_MS 1000 + + #define FRAME_HEADER 0xAA #define FRAME_TAIL 0x55 @@ -55,19 +64,18 @@ typedef enum typedef struct __attribute__((packed)) { - uint8_t type; // 闃闂ㄧ被鍨 - - uint8_t bat; // 1B 鐢垫睜鐢靛帇 30=3V,18=1.8V - int8_t temp; // 1B 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩 - uint8_t hum; // 1B 闃闂ㄦ箍搴 %RH - // uint8_t status; // 1B 闃闂ㄥ伐浣滅姸鎬 瓒呮瑺鍘嬨佽繃娴 // uint8_t connct_status; // 1B 闃闂ㄨ繛鎺ョ姸鎬 锛堢數纾侀榾BLE鎺у埗鐩掞級 0=鏈繛鎺 1=宸茶繛鎺 - // uint8_t switch_status; // 1B 闃闂ㄥ紑鍏崇姸鎬 0=鏈煡 1=鍏抽棴 2=鍏抽棴杩囷紝鏈寜鎸夐敭鎭㈠ 3=鍏抽棴杩囷紝鎸変笅浜嗘仮澶嶉敭 4=鎵撳紑 + uint8_t switch_status; // 1B 闃闂ㄥ紑鍏崇姸鎬 0=鏈煡 1=鍏抽棴 2=鍏抽棴杩囷紝鏈寜鎸夐敭鎭㈠ 3=鍏抽棴杩囷紝鎸変笅浜嗘仮澶嶉敭 4=鎵撳紑 + int8_t temp; // 1B 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩 - // uint32_t in_pressure; // 4B 闃闂ㄥ叆鍙e帇鍔 Pa - // uint32_t out_pressure; // 4B 闃闂ㄥ嚭鍙e帇鍔 Pa - // uint32_t atm_pressure; // 4B 闃闂ㄥぇ姘斿帇鍔 Pa + uint32_t in_pressure; // 4B 闃闂ㄥ叆鍙e帇鍔 Pa + uint32_t out_pressure; // 4B 闃闂ㄥ嚭鍙e帇鍔 Pa + uint32_t atm_pressure; // 4B 闃闂ㄥぇ姘斿帇鍔 Pa + uint8_t type; // 闃闂ㄧ被鍨 + uint8_t bat; // 1B 鐢垫睜鐢靛帇 30=3V,18=1.8V + uint8_t hum; // 1B 闃闂ㄦ箍搴 %RH + uint8_t rssi; } TsValveData; uint8_t CheckSum(const uint8_t *data, size_t len); @@ -76,7 +84,9 @@ void BSP_VAVLE_Init(void); TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len); -void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t bat, int8_t temp, uint8_t hum); +uint8_t GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *p_src, uint8_t src_len); + +void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t switch_status, uint8_t bat, int8_t temp, uint8_t hum); void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status); #endif // ! __BSP_VALVE_H__ diff --git a/BSP/src/bsp_uart.c b/BSP/src/bsp_uart.c index 457804c..283cd9e 100644 --- a/BSP/src/bsp_uart.c +++ b/BSP/src/bsp_uart.c @@ -22,8 +22,8 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len) unsigned int ret; ret = lwrb_write(&Uart1Tx, buf, len); - BSP_UART1_TxLoop(); // UART1_INTCfg(ENABLE, RB_IER_THR_EMPTY); + BSP_UART1_TxLoop(); return ret; } diff --git a/BSP/src/bsp_valve.c b/BSP/src/bsp_valve.c index 4d44407..fe34fb3 100644 --- a/BSP/src/bsp_valve.c +++ b/BSP/src/bsp_valve.c @@ -1,3 +1,13 @@ +/* + * @Author : stark1898y 1658608470@qq.com + * @Date : 2024-12-15 15:01:15 + * @LastEditors : stark1898y 1658608470@qq.com + * @LastEditTime : 2024-12-15 16:31:46 + * @FilePath : \BLE_TYQ_CH592F - 鍓湰\BSP\src\bsp_valve.c + * @Description : + * + * Copyright (c) 2024 by yzy, All Rights Reserved. + */ /* * @Author: mbw * @Date: 2024-12-09 11:40:10 @@ -10,20 +20,21 @@ */ #include "bsp_valve.h" #include "bsp_flash.h" -#include "bsp_uart.h" #include "bsp_beep_led_emv.h" // 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55 - +#include "bsp_uart.h" #include "log.h" -#undef LOG_ENABLE -#define LOG_ENABLE 0 +#undef LOG_ENABLE +#define LOG_ENABLE 0 #undef LOG_TAG #define LOG_TAG "BSP_VALVE" +//瀛樺偍 褰撳墠task id 鐨勫叏灞鍙橀噺 +tmosTaskID vavle_task_id = INVALID_TASK_ID; /** @@ -110,8 +121,9 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len) if (len < 4) { // 鑷冲皯闇瑕 4 涓瓧鑺傦細璧峰鐮併侀暱搴︺佹牎楠岀爜銆佺粨鏉熺爜 logError("鏁版嵁甯ч暱搴︿笉瓒"); - logHexDumpAll(data, len); + // logHexDumpAll(data, len); // return 1; + return NULL; } while (index < len && data[index] != FRAME_HEADER) // 瀵绘壘璧峰鐮 0xAA { @@ -120,14 +132,16 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len) if (index >= len - 3) // 涓嶅绌洪棿瀹圭撼闀垮害銆佹牎楠岀爜鍜岀粨鏉熺爜 { logError("鏁版嵁甯ц捣濮嬬爜閿欒"); - logHexDumpAll(data, len); + // logHexDumpAll(data, len); + return NULL; // return 2; } uint16_t data_len = data[index + 2]; // 璇诲彇鏁版嵁闀垮害 if (index + 3 + data_len + 1 >= len) // 妫鏌ユ暟鎹暱搴︽槸鍚﹀悎鐞 鏁版嵁闀垮害 + 鏍¢獙鐮 + 缁撴潫鐮 { logError("鏁版嵁甯ч暱搴﹂敊璇"); - logHexDumpAll(data, len); + // logHexDumpAll(data, len); + return NULL; // return 3; } @@ -136,13 +150,15 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len) if (check_sum != calculated_sum) { logError("鏁版嵁甯ф牎楠岀爜閿欒 check_sum = %02X, calculated_sum = %02X", check_sum, calculated_sum); - logHexDumpAll(data, len); + // logHexDumpAll(data, len); + return NULL; // return 4; } if (data[index + 3 + data_len + 1] != FRAME_TAIL) // 妫鏌ョ粨鏉熺爜 { logError("鏁版嵁甯х粨鏉熺爜閿欒"); - logHexDumpAll(data, len); + // logHexDumpAll(data, len); + return NULL; // return 5; } logDebug("鏁版嵁甯ф牎楠岄氳繃"); @@ -177,7 +193,7 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len) // } } -uint8_t _GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *p_src, uint8_t src_len) +uint8_t GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *p_src, uint8_t src_len) { // 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55 pRawData->len = src_len + 5; @@ -195,30 +211,38 @@ uint8_t _GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t * pRawData->buf[pRawData->len - 2] = CheckSum(&pRawData->buf[0], pRawData->len - 2); pRawData->buf[pRawData->len - 1] = FRAME_TAIL; - logHexDumpAll(&pRawData->buf[0], pRawData->len); + // logHexDumpAll(&pRawData->buf[0], pRawData->len); return 0; } -void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t bat, int8_t temp, uint8_t hum) +void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t switch_status, uint8_t bat, int8_t temp, uint8_t hum) { TsValveData ValveData; + ValveData.switch_status = 0; + ValveData.temp = temp; // 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩 + + ValveData.in_pressure = 1.6 * 1000000; + ValveData.out_pressure = 3000; + ValveData.atm_pressure = 96000; + ValveData.type = kTyq; // 闃闂ㄧ被鍨 ValveData.bat = bat; // 鐢垫睜鐢靛帇 30=3V,18=1.8V - ValveData.temp = temp; // 闃闂ㄦ俯搴 鏈夌鍙锋暣鏁 25 鈩 ValveData.hum = hum; // 闃闂ㄦ箍搴 %RH - _GenerateRawFrame(pRawData, kCmdData, (uint8_t*)&ValveData, sizeof(ValveData)); - logHexDumpAll(&pRawData->buf[0], pRawData->len); + ValveData.rssi = 0; + + GenerateRawFrame(pRawData, kCmdData, (uint8_t*)&ValveData, sizeof(ValveData)); + // logHexDumpAll(&pRawData->buf[0], pRawData->len); } void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status) { uint8_t data = 0; data = status; - _GenerateRawFrame(pRawData, cmd, &data, 1); - logHexDumpAll(&pRawData->buf[0], pRawData->len); + GenerateRawFrame(pRawData, cmd, &data, 1); + // logHexDumpAll(&pRawData->buf[0], pRawData->len); } void BSP_CloseValve(void) @@ -238,8 +262,66 @@ void BSP_CloseValve(void) DelayMs(100); } +static void VAVLE_Task_ProcessTmosMsg(tmos_event_hdr_t *pMsg) +{ + switch (pMsg->event) + { + default: + logDebug("pMsg->event %04x", pMsg->event); + break; + } +} + +//task鐨別vent澶勭悊鍥炶皟鍑芥暟,闇瑕佸湪娉ㄥ唽task鏃跺,浼犺繘鍘 +static uint16_t VAVLE_Task_ProcessEvent( uint8_t task_id, uint16_t events ) +{ + if (events & SYS_EVENT_MSG) + { + uint8_t *pMsg; + if ((pMsg = tmos_msg_receive(vavle_task_id)) != NULL) + { + VAVLE_Task_ProcessTmosMsg((tmos_event_hdr_t *)pMsg); + // Release the TMOS message + tmos_msg_deallocate(pMsg); + } + // return unprocessed events + return (events ^ SYS_EVENT_MSG); + } + if (events & VAVLE_RX_DATA_EVT) + { + logDebug("VAVLE_RX_DATA_EVT"); + + return (events ^ VAVLE_RX_DATA_EVT); + } + if (events & VAVLE_TX_DATA_EVT) + { + logDebug("VAVLE_TX_DATA_EVT"); + + return (events ^ VAVLE_TX_DATA_EVT); + } + if (events & VAVLE_CLOSE_START_EVT) + { + logDebug("VAVLE_CLOSE_START_EVT"); + + //寮濮嬩竴涓畾鏃秂vent,1s鍚庝骇鐢,褰撳墠璇彞鍙細浜х敓涓娆vent + //鍙互鍦╡vent浜х敓鍚庡幓寮鍚痚vent,鍙互鏄埆鐨則ask鐨,涔熷彲浠ユ槸褰撳墠task鐨別vent + tmos_start_task(vavle_task_id, VAVLE_CLOSE_END_EVT, MS1_TO_SYSTEM_TIME(CHARGE_TIME_MS)); + + return (events ^ VAVLE_CLOSE_START_EVT); + } + + + + // Discard unknown events + return 0; +} + void BSP_VAVLE_Init(void) { + vavle_task_id = TMOS_ProcessEventRegister(VAVLE_Task_ProcessEvent); + + // //绔嬪嵆寮濮嬩竴涓猠vent + // tmos_set_event(vavle_task_id, VAVLE_RX_DATA_EVT); logInfo("BSP_Valve_Init"); }