bsp_vavle ok

This commit is contained in:
stark1898y 2024-12-15 16:38:50 +08:00
parent 5a30a18ee5
commit 8f72577baa
5 changed files with 182 additions and 49 deletions

View File

@ -21,14 +21,17 @@
#include "bsp_valve.h" #include "bsp_valve.h"
#include "bsp_uart.h"
#include "log.h" #include "log.h"
#include "bsp_i2c.h"
#include "bsp_adc.h"
#undef LOG_ENABLE #undef LOG_ENABLE
#define LOG_ENABLE 1 #define LOG_ENABLE 1
#undef LOG_TAG #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, SIMPLEPROFILE_CHAR4_LEN);
// peripheralChar4Notify(notiData, 5); // peripheralChar4Notify(notiData, 5);
// counter++; // counter++;
TsRawFrameData RawData;
static uint8_t humi; #if 1
humi = 0; // VBAT
BSP_VALVE_Generate_Data(&RawData, 30, 25, humi++); uint32_t CountBat = 0;
if (humi > 99) 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); peripheralChar4Notify(&RawData.buf[0], RawData.len);
} }

View File

@ -4,8 +4,8 @@
#include "CONFIG.h" #include "CONFIG.h"
#define UART1_RX_BUFFER_LENGTH 64U #define UART1_RX_BUFFER_LENGTH 24
#define UART1_TX_BUFFER_LENGTH 1024U #define UART1_TX_BUFFER_LENGTH 256
void BSP_UART1_Init(uint32_t baudrate); void BSP_UART1_Init(uint32_t baudrate);
@ -22,14 +22,5 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len);
#endif #endif

View File

@ -2,7 +2,7 @@
* @Author : stark1898y 1658608470@qq.com * @Author : stark1898y 1658608470@qq.com
* @Date : 2024-12-12 15:55:38 * @Date : 2024-12-12 15:55:38
* @LastEditors : stark1898y 1658608470@qq.com * @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 * @FilePath : \BLE_TYQ_CH592F\BSP\inc\bsp_valve.h
* @Description : * @Description :
* *
@ -13,6 +13,15 @@
#include "CONFIG.h" #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_HEADER 0xAA
#define FRAME_TAIL 0x55 #define FRAME_TAIL 0x55
@ -55,19 +64,18 @@ typedef enum
typedef struct __attribute__((packed)) 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 status; // 1B 阀门工作状态 超欠压、过流
// uint8_t connct_status; // 1B 阀门连接状态 电磁阀BLE控制盒 0=未连接 1=已连接 // 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 阀门入口压力 Pa uint32_t in_pressure; // 4B 阀门入口压力 Pa
// uint32_t out_pressure; // 4B 阀门出口压力 Pa uint32_t out_pressure; // 4B 阀门出口压力 Pa
// uint32_t atm_pressure; // 4B 阀门大气压力 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; } TsValveData;
uint8_t CheckSum(const uint8_t *data, size_t len); 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); 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); void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status);
#endif // ! __BSP_VALVE_H__ #endif // ! __BSP_VALVE_H__

View File

@ -22,8 +22,8 @@ unsigned int BSP_Uart1_Send_Data(const void *buf, unsigned int len)
unsigned int ret; unsigned int ret;
ret = lwrb_write(&Uart1Tx, buf, len); ret = lwrb_write(&Uart1Tx, buf, len);
BSP_UART1_TxLoop();
// UART1_INTCfg(ENABLE, RB_IER_THR_EMPTY); // UART1_INTCfg(ENABLE, RB_IER_THR_EMPTY);
BSP_UART1_TxLoop();
return ret; return ret;
} }

View File

@ -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 * @Author: mbw
* @Date: 2024-12-09 11:40:10 * @Date: 2024-12-09 11:40:10
@ -10,20 +20,21 @@
*/ */
#include "bsp_valve.h" #include "bsp_valve.h"
#include "bsp_flash.h" #include "bsp_flash.h"
#include "bsp_uart.h"
#include "bsp_beep_led_emv.h" #include "bsp_beep_led_emv.h"
// 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55 // 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55
#include "bsp_uart.h"
#include "log.h" #include "log.h"
#undef LOG_ENABLE #undef LOG_ENABLE
#define LOG_ENABLE 0 #define LOG_ENABLE 0
#undef LOG_TAG #undef LOG_TAG
#define LOG_TAG "BSP_VALVE" #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) if (len < 4)
{ // 至少需要 4 个字节:起始码、长度、校验码、结束码 { // 至少需要 4 个字节:起始码、长度、校验码、结束码
logError("数据帧长度不足"); logError("数据帧长度不足");
logHexDumpAll(data, len); // logHexDumpAll(data, len);
// return 1; // return 1;
return NULL;
} }
while (index < len && data[index] != FRAME_HEADER) // 寻找起始码 0xAA 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) // 不够空间容纳长度、校验码和结束码 if (index >= len - 3) // 不够空间容纳长度、校验码和结束码
{ {
logError("数据帧起始码错误"); logError("数据帧起始码错误");
logHexDumpAll(data, len); // logHexDumpAll(data, len);
return NULL;
// return 2; // return 2;
} }
uint16_t data_len = data[index + 2]; // 读取数据长度 uint16_t data_len = data[index + 2]; // 读取数据长度
if (index + 3 + data_len + 1 >= len) // 检查数据长度是否合理 数据长度 + 校验码 + 结束码 if (index + 3 + data_len + 1 >= len) // 检查数据长度是否合理 数据长度 + 校验码 + 结束码
{ {
logError("数据帧长度错误"); logError("数据帧长度错误");
logHexDumpAll(data, len); // logHexDumpAll(data, len);
return NULL;
// return 3; // return 3;
} }
@ -136,13 +150,15 @@ TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint16_t len)
if (check_sum != calculated_sum) if (check_sum != calculated_sum)
{ {
logError("数据帧校验码错误 check_sum = %02X, calculated_sum = %02X", 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; // return 4;
} }
if (data[index + 3 + data_len + 1] != FRAME_TAIL) // 检查结束码 if (data[index + 3 + data_len + 1] != FRAME_TAIL) // 检查结束码
{ {
logError("数据帧结束码错误"); logError("数据帧结束码错误");
logHexDumpAll(data, len); // logHexDumpAll(data, len);
return NULL;
// return 5; // return 5;
} }
logDebug("数据帧校验通过"); 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 // 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55
pRawData->len = src_len + 5; 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 - 2] = CheckSum(&pRawData->buf[0], pRawData->len - 2);
pRawData->buf[pRawData->len - 1] = FRAME_TAIL; pRawData->buf[pRawData->len - 1] = FRAME_TAIL;
logHexDumpAll(&pRawData->buf[0], pRawData->len); // logHexDumpAll(&pRawData->buf[0], pRawData->len);
return 0; 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; 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.type = kTyq; // 阀门类型
ValveData.bat = bat; // 电池电压 30=3V,18=1.8V ValveData.bat = bat; // 电池电压 30=3V,18=1.8V
ValveData.temp = temp; // 阀门温度 有符号整数 25 ℃
ValveData.hum = hum; // 阀门湿度 %RH ValveData.hum = hum; // 阀门湿度 %RH
_GenerateRawFrame(pRawData, kCmdData, (uint8_t*)&ValveData, sizeof(ValveData)); ValveData.rssi = 0;
logHexDumpAll(&pRawData->buf[0], pRawData->len);
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) void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status)
{ {
uint8_t data = 0; uint8_t data = 0;
data = status; data = status;
_GenerateRawFrame(pRawData, cmd, &data, 1); GenerateRawFrame(pRawData, cmd, &data, 1);
logHexDumpAll(&pRawData->buf[0], pRawData->len); // logHexDumpAll(&pRawData->buf[0], pRawData->len);
} }
void BSP_CloseValve(void) void BSP_CloseValve(void)
@ -238,8 +262,66 @@ void BSP_CloseValve(void)
DelayMs(100); DelayMs(100);
} }
static void VAVLE_Task_ProcessTmosMsg(tmos_event_hdr_t *pMsg)
{
switch (pMsg->event)
{
default:
logDebug("pMsg->event %04x", pMsg->event);
break;
}
}
//task的event处理回调函数,需要在注册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");
//开始一个定时event,1s后产生,当前语句只会产生一次event
//可以在event产生后去开启event,可以是别的task的,也可以是当前task的event
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) void BSP_VAVLE_Init(void)
{ {
vavle_task_id = TMOS_ProcessEventRegister(VAVLE_Task_ProcessEvent);
// //立即开始一个event
// tmos_set_event(vavle_task_id, VAVLE_RX_DATA_EVT);
logInfo("BSP_Valve_Init"); logInfo("BSP_Valve_Init");
} }