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 "usb_device.h"
|
|
|
|
|
|
#include "BL24C02.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
|
|
|
2026-03-25 16:00:47 +08:00
|
|
|
|
/* 10kHz / 100 / 10 = 10Hz UI data refresh rate */
|
|
|
|
|
|
#define UI_UPDATE_DIV 10
|
|
|
|
|
|
|
2026-02-26 17:20:03 +08:00
|
|
|
|
/* 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
|
|
|
|
{
|
2026-03-25 16:00:47 +08:00
|
|
|
|
uint32_t flags;
|
2026-02-26 17:20:03 +08:00
|
|
|
|
while(1)
|
|
|
|
|
|
{
|
2026-03-25 16:00:47 +08:00
|
|
|
|
flags = osThreadFlagsWait(FLAG_USB_UPDATE_REQ,
|
|
|
|
|
|
osFlagsWaitAny,
|
|
|
|
|
|
osWaitForever);
|
2026-02-26 17:20:03 +08:00
|
|
|
|
|
2026-03-25 16:00:47 +08:00
|
|
|
|
if ((flags & 0x80000000U) == 0U) {
|
2026-02-26 17:20:03 +08:00
|
|
|
|
// set the EEPROM flag
|
|
|
|
|
|
EEPROM_UpdateCommand_Write(true);
|
|
|
|
|
|
HAL_Delay(100);
|
|
|
|
|
|
USER_USB_DEVICE_DeInit();
|
|
|
|
|
|
// 给予PC足够的时间来识别设备断开
|
|
|
|
|
|
HAL_Delay(500);
|
|
|
|
|
|
// reset
|
|
|
|
|
|
NVIC_SystemReset();
|
|
|
|
|
|
}
|
2026-03-25 16:00:47 +08:00
|
|
|
|
HAL_Delay(500);
|
2026-02-26 17:20:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
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;
|
2026-03-25 16:00:47 +08:00
|
|
|
|
uint32_t ui_div_cnt = 0;
|
2026-03-15 11:20:09 +08:00
|
|
|
|
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
|
|
|
|
|
2026-03-25 16:00:47 +08:00
|
|
|
|
// 4. 限频:每 UI_UPDATE_DIV 次才往队列写一次
|
|
|
|
|
|
if (++ui_div_cnt >= UI_UPDATE_DIV)
|
|
|
|
|
|
{
|
|
|
|
|
|
ui_div_cnt = 0;
|
2026-03-19 11:18:06 +08:00
|
|
|
|
|
2026-03-25 16:00:47 +08:00
|
|
|
|
PowerData_t newData;
|
|
|
|
|
|
Data_Monitor_Get_Values(&newData.voltage, &newData.current);
|
2026-03-19 11:18:06 +08:00
|
|
|
|
|
2026-03-25 16:00:47 +08:00
|
|
|
|
osStatus_t status = osMessageQueuePut(PowerDataQueue, &newData, 0, 0);
|
|
|
|
|
|
if (status == osErrorResource) {
|
|
|
|
|
|
PowerData_t dummy;
|
|
|
|
|
|
// 队列满:丢弃旧数据,写入新数据
|
|
|
|
|
|
osMessageQueueGet(PowerDataQueue, &dummy, NULL, 0);
|
|
|
|
|
|
osMessageQueuePut(PowerDataQueue, &newData, 0, 0);
|
|
|
|
|
|
}
|
2026-03-19 11:18:06 +08:00
|
|
|
|
}
|
2026-03-15 11:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|