Files
SmartPetFeeder_STM32/Core/Src/freertos.c

207 lines
5.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* Copyright (c) 2026 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "dx_wf_24.h"
#include "elog.h"
#include "string.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TAG "Main"
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 256 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for wifi_mqtt */
osThreadId_t wifi_mqttHandle;
const osThreadAttr_t wifi_mqtt_attributes = {
.name = "wifi_mqtt",
.stack_size = 1024 * 4,
.priority = (osPriority_t) osPriorityHigh,
};
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void *argument);
void wifi_task_mqtt(void *argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/**
* @brief FreeRTOS initialization
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
easylogger_init(); // 初始化日志系统
__enable_irq(); // 开启全局中断
/* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* creation of defaultTask */
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* creation of wifi_mqtt */
wifi_mqttHandle = osThreadNew(wifi_task_mqtt, NULL, &wifi_mqtt_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_EVENTS */
/* add events, ... */
/* USER CODE END RTOS_EVENTS */
}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for (;;) {
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
HAL_GPIO_TogglePin(MOTOR_GPIO_Port, MOTOR_Pin);
osDelay(1000);
}
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_wifi_task_mqtt */
/**
* @brief Function implementing the wifi_mqtt thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_wifi_task_mqtt */
void wifi_task_mqtt(void *argument)
{
/* USER CODE BEGIN wifi_task_mqtt */
WIFI_RECV_DMA_Init(); // 初始化 WiFi DMA 接收
/* 等待模块启动 */
elog_i(TAG, "Waiting for WiFi module to start...");
osDelay(3000);
/* 连接MQTT服务器 */
uint8_t connect_success = WIFI_Connect_MQTT(
"ChinaNet_WBH", // WiFi名称
"88888888", // WiFi密码
"broker.emqx.io", // MQTT服务器
1883, // MQTT端口
"STM32_MQTT", // 客户端ID
NULL, // MQTT用户名NULL表示无需认证
NULL, // MQTT密码NULL表示无需认证
"pet/control" // 订阅主题
);
if (connect_success) {
elog_i(TAG, "MQTT connection successful!");
} else {
elog_e(TAG, "MQTT connection failed!");
// 可以在这里添加失败处理,如重启模块或重试
}
uint8_t rx_data[WIFI_RX_BUF_SIZE];
/* Infinite loop */
for (;;) {
/* 检查并处理接收数据 */
if (wifi.rx_flag) {
int len = WIFI_Get_Received_Data(rx_data, sizeof(rx_data));
if (len > 0) {
// 处理 WiFi 返回数据
elog_d(TAG, "WiFi response: %s", rx_data);
// 检查MQTT消息
if (strstr((char*)rx_data, "+MQTTPUBLISH")) {
elog_i(TAG, "Received MQTT message!");
// TODO: 解析并处理MQTT消息
}
}
}
osDelay(100);
}
/* USER CODE END wifi_task_mqtt */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE END Application */