Gradually migrate to FreeRTOS

This commit is contained in:
aixiao 2024-06-04 09:54:09 +08:00
parent db774bfd76
commit 92c0ee6bd8
15 changed files with 143 additions and 161 deletions

View File

@ -1,4 +1,5 @@
# 基于 Raspberry Pico / Pico W 的厨房危险(火灾)报警 Pico W WiFi传输暂时不玩 # 基于 Raspberry Pico / Pico W 的厨房危险(火灾)报警 Pico W WiFi传输暂时不玩
* 使用 DS18B20温度传感器 * 使用 DS18B20温度传感器
* 使用 CH4 N55A甲烷气体传感器(进口) (弃用) * 使用 CH4 N55A甲烷气体传感器(进口) (弃用)
* 使用 PASCO2V01 CO2二氧化碳传感器模块(进口模块暂时买不到!) * 使用 PASCO2V01 CO2二氧化碳传感器模块(进口模块暂时买不到!)
@ -7,8 +8,9 @@
* 使用 HC-12 433MHZ传感器模块(国产) * 使用 HC-12 433MHZ传感器模块(国产)
* 使用 ZC05/ZC13 可燃气体(天然气 CH4)传感器模块(国产) * 使用 ZC05/ZC13 可燃气体(天然气 CH4)传感器模块(国产)
## Build ## 源码构建
```
```bash
# 使用WSL Debian GNU/Linux 12 (bookworm) 构建 # 使用WSL Debian GNU/Linux 12 (bookworm) 构建
# 确保Pico-SDK环境变量 # 确保Pico-SDK环境变量
export PICO_SDK_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry-pico/SDK/pico-sdk export PICO_SDK_PATH=/mnt/c/Users/niuyuling/Desktop/raspberry-pico/SDK/pico-sdk
@ -39,7 +41,8 @@
./hc-12 ./hc-12
``` ```
## display
## 展示
![brief](HARDWARE/IMG/db.jpg) ![brief](HARDWARE/IMG/db.jpg)
![brief](HARDWARE/IMG/display.png) ![brief](HARDWARE/IMG/display.png)

View File

@ -15,14 +15,12 @@ void DS18B20(void *pvParameters)
_printTaskStackHighWaterMark("DS18B20"); _printTaskStackHighWaterMark("DS18B20");
while (1) while (1) {
{
one_wire.single_device_read_rom(address); one_wire.single_device_read_rom(address);
one_wire.convert_temperature(address, true, false); one_wire.convert_temperature(address, true, false);
TEMPERATURE = one_wire.temperature(address); TEMPERATURE = one_wire.temperature(address);
//printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x Temperature: %3.1f°C\n", address.rom[0], address.rom[1], address.rom[2], address.rom[3], printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x Temperature: %3.1f°C\n", address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
// address.rom[4], address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
// 发送数据到队列 // 发送数据到队列
xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY); xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY);

View File

@ -15,7 +15,6 @@
#define DS18B20_PIN 15 // DS18B20 引脚 #define DS18B20_PIN 15 // DS18B20 引脚
extern QueueHandle_t xQueue; extern QueueHandle_t xQueue;
extern void DS18B20(void *pvParameters); extern void DS18B20(void *pvParameters);

View File

@ -1,7 +1,6 @@
#include "MHZ14B.hpp" #include "MHZ14B.hpp"
#include "common.hpp" #include "common.hpp"
void MH_Z14B_INIT() void MH_Z14B_INIT()
{ {
// 初始化UART // 初始化UART
@ -16,7 +15,6 @@ void MH_Z14B_INIT()
static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK) static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
{ {
// 0x86 读气体浓度值 // 0x86 读气体浓度值
uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
uart_write_blocking(UART1, CMD, 9); uart_write_blocking(UART1, CMD, 9);
@ -61,14 +59,12 @@ void CO2(void *pvParameters)
MH_Z14B_INIT(); MH_Z14B_INIT();
_printTaskStackHighWaterMark("MH_Z14B"); _printTaskStackHighWaterMark("MH_Z14B");
while(1) while (1) {
{
CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK); CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK);
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) { if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
printf("CO2 Concentration: %d ppm\n", CO2_DATA); printf("CO2 Concentration: %d ppm\n", CO2_DATA);
} }
//_printTaskStackHighWaterMark("MH_Z14B");
_printTaskStackHighWaterMark("MH_Z14B");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时 vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
} }
} }

View File

@ -12,7 +12,8 @@ int ZC13_INIT()
return 0; return 0;
} }
int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
for (int i = 0; i < DATA_LEN; i++) { for (int i = 0; i < DATA_LEN; i++) {
uart_tx_program_putc(pio, sm, DATA[i]); uart_tx_program_putc(pio, sm, DATA[i]);
@ -22,7 +23,8 @@ int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) {
return 0; return 0;
} }
int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
char c = '\0'; char c = '\0';
int received_count = 0; int received_count = 0;
int timeout_ms = 100; // 设置较长的超时时间 int timeout_ms = 100; // 设置较长的超时时间
@ -31,7 +33,7 @@ int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) {
if (uart_rx_program_available(pio, sm)) { if (uart_rx_program_available(pio, sm)) {
c = uart_rx_program_getc(pio, sm); c = uart_rx_program_getc(pio, sm);
DATA[received_count++] = c; DATA[received_count++] = c;
printf("0x%X ", c); //printf("0x%X ", c);
if (c == '\n') { if (c == '\n') {
// 接收到换行符,停止接收数据 // 接收到换行符,停止接收数据
break; break;
@ -64,9 +66,8 @@ int ZC13(const char *model)
// 接收指令 // 接收指令
CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9); CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);
//printf("\n");
printf("\n"); // printf("%d\n", CH4_DATA_LEN);
printf("%d\n", CH4_DATA_LEN);
uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节 uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节 uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节
@ -104,11 +105,11 @@ void CH4(void *pvParameters)
{ {
ZC13_INIT(); ZC13_INIT();
_printTaskStackHighWaterMark("ZC13"); _printTaskStackHighWaterMark("ZC13");
while(1)
{ while (1) {
ZC13("ZC05"); ZC13("ZC05");
_printTaskStackHighWaterMark("ZC13"); //_printTaskStackHighWaterMark("ZC13");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时 vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
} }
} }

View File

@ -66,16 +66,12 @@ void vApplicationTickHook(void)
#endif #endif
} }
void _printTaskStackHighWaterMark(const char *task_name) void _printTaskStackHighWaterMark(const char *task_name)
{ {
TaskHandle_t currentTask = xTaskGetCurrentTaskHandle(); TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
if (currentTask != NULL) if (currentTask != NULL) {
{
printf("%s TASK STACK HIGH WATER MARK: %ld\n", task_name, uxTaskGetStackHighWaterMark(currentTask)); printf("%s TASK STACK HIGH WATER MARK: %ld\n", task_name, uxTaskGetStackHighWaterMark(currentTask));
} } else {
else
{
printf("FAILED TO GET CURRENT TASK HANDLE.\n"); printf("FAILED TO GET CURRENT TASK HANDLE.\n");
} }
} }

View File

@ -10,7 +10,6 @@
#include <stdio.h> #include <stdio.h>
#include "hardware/pio.h" #include "hardware/pio.h"
#define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0) #define BUILD(fmt...) do { fprintf(stderr,"%s %s ",__DATE__,__TIME__); fprintf(stderr, ##fmt); } while(0)
static inline bool uart_rx_program_available(PIO pio, uint sm) static inline bool uart_rx_program_available(PIO pio, uint sm)

View File

@ -47,7 +47,6 @@ void Led_Blinky(void *pvParameters)
// 处理接收到的数据 // 处理接收到的数据
printf("%f\n", TEMPERATURE); printf("%f\n", TEMPERATURE);
} }
//_printTaskStackHighWaterMark("Led_Blinky"); //_printTaskStackHighWaterMark("Led_Blinky");
} }
@ -83,11 +82,9 @@ int main(void)
sleep_ms(3000); sleep_ms(3000);
//set_sys_clock_khz(250000, true); //set_sys_clock_khz(250000, true);
// 创建队列 // 创建队列
xQueue = xQueueCreate(10, sizeof(long)); xQueue = xQueueCreate(10, sizeof(long));
// 创建任务 // 创建任务
BaseType_t xReturned; BaseType_t xReturned;
TaskHandle_t Led_Blinky_xHandle = NULL; TaskHandle_t Led_Blinky_xHandle = NULL;
@ -97,31 +94,25 @@ int main(void)
// 板载LED闪烁 // 板载LED闪烁
xReturned = xTaskCreate(Led_Blinky, "Blinky task", 512, NULL, tskIDLE_PRIORITY, &Led_Blinky_xHandle); xReturned = xTaskCreate(Led_Blinky, "Blinky task", 512, NULL, tskIDLE_PRIORITY, &Led_Blinky_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("Blinky Task Error!"); printf("Blinky Task Error!");
} }
// DS18B20 // DS18B20
xReturned = xTaskCreate(DS18B20, "DS18B20 task", 1024, NULL, tskIDLE_PRIORITY, &DS18B20_xHandle); xReturned = xTaskCreate(DS18B20, "DS18B20 task", 1024, NULL, tskIDLE_PRIORITY, &DS18B20_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("DS18B20() Task Error!"); printf("DS18B20() Task Error!");
} }
// CH4 // CH4
xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle); xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("CH4() Task Error!"); printf("CH4() Task Error!");
} }
// CO2 // CO2
xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle); xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
{
printf("CO2() Task Error!"); printf("CO2() Task Error!");
} }
vTaskStartScheduler(); vTaskStartScheduler();
return 0; return 0;

View File

@ -297,8 +297,6 @@ int main()
} }
watchdog_update(); // 喂狗 watchdog_update(); // 喂狗
// CH4 // CH4
FIFO_ = multicore_fifo_pop_blocking(); FIFO_ = multicore_fifo_pop_blocking();
if (FIFO_ == 1) { if (FIFO_ == 1) {

View File

@ -12,7 +12,8 @@ int ZC13_INIT()
return 0; return 0;
} }
int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
for (int i = 0; i < DATA_LEN; i++) { for (int i = 0; i < DATA_LEN; i++) {
uart_tx_program_putc(pio, sm, DATA[i]); uart_tx_program_putc(pio, sm, DATA[i]);
@ -22,7 +23,8 @@ int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) {
return 0; return 0;
} }
int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) { int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
char c = '\0'; char c = '\0';
int received_count = 0; int received_count = 0;
int timeout_ms = 100; // 设置较长的超时时间 int timeout_ms = 100; // 设置较长的超时时间
@ -64,7 +66,6 @@ int ZC13(const char *model)
// 接收指令 // 接收指令
CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9); CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);
printf("\n"); printf("\n");
printf("%d\n", CH4_DATA_LEN); printf("%d\n", CH4_DATA_LEN);