2026-02-26 17:20:03 +08:00
|
|
|
|
/* Private includes -----------------------------------------------------------*/
|
|
|
|
|
|
//includes
|
2026-03-15 11:20:09 +08:00
|
|
|
|
#include "adc.h"
|
2026-02-26 17:20:03 +08:00
|
|
|
|
#include "user_TasksInit.h"
|
|
|
|
|
|
#include "user_MessageTask.h"
|
|
|
|
|
|
#include "usbd_cdc_if.h"
|
|
|
|
|
|
#include "usb_device.h"
|
|
|
|
|
|
#include "BL24C02.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-15 11:20:09 +08:00
|
|
|
|
* @brief task for receiving messages, such as USB update requests.
|
2026-02-26 17:20:03 +08:00
|
|
|
|
* @param argument: Not used
|
|
|
|
|
|
* @retval None
|
|
|
|
|
|
*/
|
2026-03-15 11:20:09 +08:00
|
|
|
|
void MessageReceiveTask(void *argument)
|
2026-02-26 17:20:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
while(1)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(get_update_request()) {
|
|
|
|
|
|
|
|
|
|
|
|
// clear the flag
|
|
|
|
|
|
set_update_request(false);
|
|
|
|
|
|
// set the EEPROM flag
|
|
|
|
|
|
EEPROM_UpdateCommand_Write(true);
|
|
|
|
|
|
HAL_Delay(100);
|
|
|
|
|
|
USER_USB_DEVICE_DeInit();
|
|
|
|
|
|
// 给予PC足够的时间来识别设备断开
|
|
|
|
|
|
HAL_Delay(500);
|
|
|
|
|
|
// reset
|
|
|
|
|
|
NVIC_SystemReset();
|
|
|
|
|
|
}
|
|
|
|
|
|
osDelay(500);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-15 11:20:09 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief send adc data to PC via USB,
|
|
|
|
|
|
* @param argument: Not used
|
|
|
|
|
|
*/
|
|
|
|
|
|
void MessageSendTask(void *argument)
|
|
|
|
|
|
{
|
|
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
while (1)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. 等待两个标志中的任意一个 (osFlagsWaitAny)
|
|
|
|
|
|
flags = osThreadFlagsWait(FLAG_ADC_HALF_READY | FLAG_ADC_FULL_READY,
|
|
|
|
|
|
osFlagsWaitAny,
|
|
|
|
|
|
osWaitForever);
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 判断是不是出错了 (比如超时或者传参错误,通常返回值最高位会置1)
|
|
|
|
|
|
if (flags & 0x80000000) {
|
|
|
|
|
|
continue; // 错误处理
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 检查具体是哪个标志位被置位了,然后处理对应的数据
|
|
|
|
|
|
if (flags & FLAG_ADC_HALF_READY)
|
|
|
|
|
|
{
|
|
|
|
|
|
Process_ADC_Chunk(&adc_raw_buffer[0][0], 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (flags & FLAG_ADC_FULL_READY)
|
|
|
|
|
|
{
|
|
|
|
|
|
Process_ADC_Chunk(&adc_raw_buffer[ADC_TIMES][0], 1);
|
|
|
|
|
|
}
|
2026-03-19 11:18:06 +08:00
|
|
|
|
|
|
|
|
|
|
// 发送数据给UI层进行显示
|
|
|
|
|
|
PowerData_t newData;
|
|
|
|
|
|
Data_Monitor_Get_Values(&newData.voltage, &newData.current);
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试发送,如果不等待直接返回错误,说明队列可能满了
|
|
|
|
|
|
osStatus_t status = osMessageQueuePut(PowerDataQueue, &newData, 0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
if (status == osErrorResource) {
|
|
|
|
|
|
PowerData_t dummy;
|
|
|
|
|
|
// 1. 强行从队列头部取出一个旧数据(丢弃)
|
|
|
|
|
|
osMessageQueueGet(PowerDataQueue, &dummy, NULL, 0);
|
|
|
|
|
|
// 2. 再次存入新数据
|
|
|
|
|
|
osMessageQueuePut(PowerDataQueue, &newData, 0, 0);
|
|
|
|
|
|
}
|
2026-03-15 11:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|