Add FreeRTOS

This commit is contained in:
aixiao 2024-06-03 18:59:42 +08:00
parent d6007ab75e
commit db774bfd76
16 changed files with 248 additions and 169 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
build
SOFTWARE/build SOFTWARE/build
SOFTWARE-FreeRTOS/build SOFTWARE-FreeRTOS/build

4
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,4 @@
{
"cmake.sourceDirectory": "/mnt/c/Users/niuyuling/Desktop/raspberry-pico/Danger-alarm/SOFTWARE",
"C_Cpp.errorSquiggles": "disabled"
}

View File

@ -35,9 +35,11 @@ pico_generate_pio_header(main ${CMAKE_CURRENT_LIST_DIR}/Source/uart_tx.pio)
pico_generate_pio_header(main ${CMAKE_CURRENT_LIST_DIR}/Source/uart_rx.pio) pico_generate_pio_header(main ${CMAKE_CURRENT_LIST_DIR}/Source/uart_rx.pio)
target_Sources(main PRIVATE target_Sources(main PRIVATE
${CMAKE_CURRENT_LIST_DIR}/Source/common.c ${CMAKE_CURRENT_LIST_DIR}/Source/common.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/main.c ${CMAKE_CURRENT_LIST_DIR}/Source/main.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/DS18B20.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/ZC13.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/MHZ14B.cpp
) )
target_include_directories(main PRIVATE target_include_directories(main PRIVATE

View File

@ -0,0 +1,36 @@
#include "DS18B20.hpp"
#include "common.hpp"
QueueHandle_t xQueue;
// 温度传感器
void DS18B20(void *pvParameters)
{
float TEMPERATURE = -1;
One_wire one_wire(DS18B20_PIN);
one_wire.init();
rom_address_t address {
};
_printTaskStackHighWaterMark("DS18B20");
while (1)
{
one_wire.single_device_read_rom(address);
one_wire.convert_temperature(address, true, false);
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],
// address.rom[4], address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
// 发送数据到队列
xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(1000));
//_printTaskStackHighWaterMark("DS18B20");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
return ;
}

View File

@ -0,0 +1,22 @@
#ifndef DS18B20_H
#define DS18B20_H
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include <cstdio>
#include <stdbool.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "../../SOFTWARE/Lib/pico-onewire/api/one_wire.h"
#define DS18B20_PIN 15 // DS18B20 引脚
extern QueueHandle_t xQueue;
extern void DS18B20(void *pvParameters);
#endif

View File

@ -1,70 +0,0 @@
#include "DTH11.hpp"
typedef struct {
float humidity;
float temp_celsius;
} dht_reading;
void read_from_dht(dht_reading * result, int DHT_PIN)
{
int data[5] = { 0, 0, 0, 0, 0 };
uint last = 1;
uint j = 0;
gpio_set_dir(DHT_PIN, GPIO_OUT);
gpio_put(DHT_PIN, 0);
sleep_ms(20);
gpio_set_dir(DHT_PIN, GPIO_IN);
for (uint i = 0; i < MAX_TIMINGS; i++) {
uint count = 0;
while (gpio_get(DHT_PIN) == last) {
count++;
sleep_us(1);
if (count == 255)
break;
}
last = gpio_get(DHT_PIN);
if (count == 255)
break;
if ((i >= 4) && (i % 2 == 0)) {
data[j / 8] <<= 1;
if (count > 16)
data[j / 8] |= 1;
j++;
}
}
if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
result->humidity = (float)((data[0] << 8) + data[1]) / 10;
if (result->humidity > 100) {
result->humidity = data[0];
}
result->temp_celsius = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
if (result->temp_celsius > 125) {
result->temp_celsius = data[2];
}
if (data[2] & 0x80) {
result->temp_celsius = -result->temp_celsius;
}
} else {
printf("Bad data\n");
}
}
int DTH11_(int DHT_PIN)
{
stdio_init_all();
gpio_init(DHT_PIN);
dht_reading reading;
read_from_dht(&reading, DHT_PIN);
float fahrenheit = (reading.temp_celsius * 9 / 5) + 32;
printf("Humidity = %.1f%%, Temperature = %.1fC (%.1fF)\n", reading.humidity, reading.temp_celsius, fahrenheit);
sleep_ms(2000);
return 0;
}

View File

@ -1,11 +0,0 @@
#ifndef DTH11_H
#define DTH11_H
#include <stdio.h>
#include <math.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
const uint MAX_TIMINGS = 85;
#endif

View File

@ -0,0 +1,74 @@
#include "MHZ14B.hpp"
#include "common.hpp"
void MH_Z14B_INIT()
{
// 初始化UART
uart_init(UART1, BAUD_RATE);
gpio_set_function(UART1_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART1, false, false);
uart_set_format(UART1, DATA_BITS, STOP_BITS, PARITY);
}
// CO2
static uint16_t MH_Z14B(int *MH_Z14B_DATA_IS_OK)
{
// 0x86 读气体浓度值
uint8_t CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
uart_write_blocking(UART1, CMD, 9);
sleep_ms(200);
// 读取
uint8_t CO2_DATA[9] = { 0 };
uart_read_blocking(UART1, CO2_DATA, 9);
// CO2 浓度
uint16_t CO2_CONC = (256 * CO2_DATA[2]) + CO2_DATA[3];
// 校验
uint8_t CHECKSUM = (0xFF - (CO2_DATA[1] + CO2_DATA[2] + CO2_DATA[3] + CO2_DATA[4] + CO2_DATA[5] + CO2_DATA[6] + CO2_DATA[7])) + 1;
if (CO2_DATA[8] == CHECKSUM && CO2_DATA[1] == 0x86) {
//printf("CHECKSUM: %X = %X\n", CO2_DATA[8], CHECKSUM);
//printf("CO2 Concentration: %d ppm\n", CO2_CONC);
*MH_Z14B_DATA_IS_OK = 1;
} else {
// 校准传感器 零点 ZERO
uint8_t ZERO[] = { 0XFF, 0X01, 0X87, 0X00, 0X00, 0X00, 0X00, 0X00, 0X78 };
uart_write_blocking(UART1, ZERO, 9);
sleep_ms(200);
// 校准传感器 跨度点 SPAN
uint8_t SPAN[] = { 0XFF, 0X01, 0X88, 0X07, 0XD0, 0X00, 0X00, 0X00, 0XA0 };
uart_write_blocking(UART1, SPAN, 9);
sleep_ms(200);
*MH_Z14B_DATA_IS_OK = 0;
printf("CO2 concentration reading failed!\n");
}
return CO2_CONC;
}
void CO2(void *pvParameters)
{
uint16_t CO2_DATA = -1;
int MH_Z14B_DATA_IS_OK = 0;
MH_Z14B_INIT();
_printTaskStackHighWaterMark("MH_Z14B");
while(1)
{
CO2_DATA = MH_Z14B(&MH_Z14B_DATA_IS_OK);
if (CO2_DATA != -1 && MH_Z14B_DATA_IS_OK == 1) {
printf("CO2 Concentration: %d ppm\n", CO2_DATA);
}
_printTaskStackHighWaterMark("MH_Z14B");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
}

View File

@ -0,0 +1,30 @@
#ifndef MHZ14B_H
#define MHZ14B_H
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include "pico/stdlib.h"
#include "hardware/clocks.h"
#include "hardware/watchdog.h"
#include "pico/multicore.h"
#include "hardware/i2c.h"
#include "pico/binary_info.h"
#include "hardware/uart.h"
#include "hardware/pwm.h"
#include "hardware/adc.h"
#define UART1 uart1
#define BAUD_RATE 9600
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
#define UART1_TX_PIN 5 // MH-Z14B T
#define UART1_RX_PIN 4 // MH-Z14B R
extern void CO2(void *pvParameters);
#endif

View File

@ -1,5 +1,5 @@
#include "ZC13.hpp" #include "ZC13.hpp"
#include "common.h" #include "common.hpp"
int ZC13_INIT() int ZC13_INIT()
{ {
@ -100,72 +100,15 @@ int ZC13(const char *model)
return -1; return -1;
} }
/* void CH4(void *pvParameters)
int ZC13(const char *model) {
ZC13_INIT();
_printTaskStackHighWaterMark("ZC13");
while(1)
{ {
char CH4_DATA[9] = { 0 };
int CH4_DATA_index = 0;
// 发送指令 ZC13("ZC05");
char CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; _printTaskStackHighWaterMark("ZC13");
for (int i = 0; i < 9; i++) { vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
uart_tx_program_putc(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD[i]);
sleep_ms(1);
}
sleep_ms(200);
// 接收数据,直到收到换行符或达到缓冲区大小限制
char c = '\0';
int timeout_ms = 100; // 设置超时时间为100ms
int received_count = 0;
while (received_count < 9 && timeout_ms > 0) {
if (uart_rx_program_available(ZC13_PIO, ZC13_PIO_SM_RX)) {
c = uart_rx_program_getc(ZC13_PIO, ZC13_PIO_SM_RX);
CH4_DATA[CH4_DATA_index++] = c;
received_count++;
timeout_ms = 100; // 重置超时时间
} else {
sleep_ms(1);
timeout_ms--; // 减少超时时间
} }
} }
// 处理接收到的数据
for (int i = 0; i < 9; i++) {
printf("0x%X ", CH4_DATA[i]);
}
printf("\n");
uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节
// 判断最高位是否为 1
if (highByte & 0x80) {
// 最高位是 1表示传感器故障
printf("CH4 sensor malfunction!\n");
} else {
if (CH4_DATA[1] == 0X86) {
if ( 0 == strcasecmp(model, "ZC05")) {
// 计算气体浓度值
uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte;
// 输出气体浓度值
printf("CH4 Concentration: %uppm\n", gasConcentration);
return gasConcentration;
}
if ( 0 == strcasecmp(model, "ZC13")) {
// 计算气体浓度值
uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte;
// 输出气体浓度值
printf("CH4 Concentration: %uppm\n", gasConcentration);
return gasConcentration;
}
}
}
return -1;
}
*/

View File

@ -1,5 +1,11 @@
#ifndef ZC13_HPP #ifndef ZC13_H
#define ZC13_HPP #define ZC13_H
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include <stdio.h> #include <stdio.h>
#include <strings.h> #include <strings.h>
@ -15,7 +21,6 @@
#define ZC13_PIO_SM_RX 1 #define ZC13_PIO_SM_RX 1
#define ZC13_PIO_SERIAL_BAUD 9600 #define ZC13_PIO_SERIAL_BAUD 9600
extern int ZC13_INIT(); extern void CH4(void *pvParameters);
extern int ZC13(const char *model);
#endif #endif

View File

@ -1,4 +1,4 @@
#include "common.h" #include "common.hpp"
void vApplicationMallocFailedHook(void) void vApplicationMallocFailedHook(void)
{ {
@ -67,12 +67,12 @@ void vApplicationTickHook(void)
} }
void _printTaskStackHighWaterMark(void) void _printTaskStackHighWaterMark(const char *task_name)
{ {
TaskHandle_t currentTask = xTaskGetCurrentTaskHandle(); TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
if (currentTask != NULL) if (currentTask != NULL)
{ {
printf("TASK STACK HIGH WATER MARK: %ld\n", uxTaskGetStackHighWaterMark(currentTask)); printf("%s TASK STACK HIGH WATER MARK: %ld\n", task_name, uxTaskGetStackHighWaterMark(currentTask));
} }
else else
{ {

View File

@ -5,6 +5,7 @@
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "semphr.h" #include "semphr.h"
#include "queue.h"
#include <stdio.h> #include <stdio.h>
#include "hardware/pio.h" #include "hardware/pio.h"
@ -17,6 +18,6 @@ static inline bool uart_rx_program_available(PIO pio, uint sm)
return !pio_sm_is_rx_fifo_empty(pio, sm); return !pio_sm_is_rx_fifo_empty(pio, sm);
} }
extern void _printTaskStackHighWaterMark(void); extern void _printTaskStackHighWaterMark(const char *task_name);
#endif #endif

View File

@ -2,6 +2,7 @@
#include "FreeRTOS.h" #include "FreeRTOS.h"
#include "task.h" #include "task.h"
#include "semphr.h" #include "semphr.h"
#include "queue.h"
/* Standard demo includes. */ /* Standard demo includes. */
#include "TimerDemo.h" #include "TimerDemo.h"
@ -10,9 +11,12 @@
#include "IntSemTest.h" #include "IntSemTest.h"
#include "TaskNotify.h" #include "TaskNotify.h"
#include "MAIN.h" #include "main.hpp"
#include "lwipopts.h" #include "lwipopts.h"
#include "common.h" #include "common.hpp"
#include "ds18b20.hpp"
#include "ZC13.hpp"
#include "MHZ14B.hpp"
#ifndef PICO_DEFAULT_LED_PIN #ifndef PICO_DEFAULT_LED_PIN
#warning pio/hello_pio example requires a board with a regular LED #warning pio/hello_pio example requires a board with a regular LED
@ -27,7 +31,9 @@ void Led_Blinky(void *pvParameters)
gpio_init(LED_PIN); gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT); gpio_set_dir(LED_PIN, GPIO_OUT);
_printTaskStackHighWaterMark(); _printTaskStackHighWaterMark("Led_Blinky");
float TEMPERATURE;
while (1) { while (1) {
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
@ -36,7 +42,14 @@ void Led_Blinky(void *pvParameters)
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
gpio_put(LED_PIN, 0); gpio_put(LED_PIN, 0);
_printTaskStackHighWaterMark(); // 从队列接收数据
if (xQueueReceive(xQueue, &TEMPERATURE, portMAX_DELAY) == pdPASS) {
// 处理接收到的数据
printf("%f\n", TEMPERATURE);
}
//_printTaskStackHighWaterMark("Led_Blinky");
} }
} }
@ -48,7 +61,7 @@ void Read_Onboard_Temperature(void *pvParameters)
adc_set_temp_sensor_enabled(true); adc_set_temp_sensor_enabled(true);
adc_select_input(4); // Input 4 is the onboard temperature sensor. adc_select_input(4); // Input 4 is the onboard temperature sensor.
//_printTaskStackHighWaterMark(); //_printTaskStackHighWaterMark("Read_Onboard_Temperature");
while (1) { while (1) {
const float conversionFactor = 3.3f / (1 << 12); const float conversionFactor = 3.3f / (1 << 12);
@ -58,7 +71,7 @@ void Read_Onboard_Temperature(void *pvParameters)
printf("Onboard temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32)); printf("Onboard temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
//_printTaskStackHighWaterMark(); //_printTaskStackHighWaterMark("Read_Onboard_Temperature");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时 vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
} }
@ -67,11 +80,20 @@ void Read_Onboard_Temperature(void *pvParameters)
int main(void) int main(void)
{ {
stdio_init_all(); stdio_init_all();
sleep_ms(1000); sleep_ms(3000);
//set_sys_clock_khz(250000, true); //set_sys_clock_khz(250000, true);
// 创建队列
xQueue = xQueueCreate(10, sizeof(long));
// 创建任务
BaseType_t xReturned; BaseType_t xReturned;
TaskHandle_t Led_Blinky_xHandle = NULL; TaskHandle_t Led_Blinky_xHandle = NULL;
TaskHandle_t DS18B20_xHandle = NULL;
TaskHandle_t CH4_xHandle = NULL;
TaskHandle_t CO2_xHandle = NULL;
// 板载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);
@ -80,6 +102,26 @@ int main(void)
printf("Blinky Task Error!"); printf("Blinky Task Error!");
} }
// DS18B20
xReturned = xTaskCreate(DS18B20, "DS18B20 task", 1024, NULL, tskIDLE_PRIORITY, &DS18B20_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
{
printf("DS18B20() Task Error!");
}
// CH4
xReturned = xTaskCreate(CH4, "CH4 task", 1024, NULL, tskIDLE_PRIORITY, &CH4_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
{
printf("CH4() Task Error!");
}
// CO2
xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
{
printf("CO2() Task Error!");
}
vTaskStartScheduler(); vTaskStartScheduler();
return 0; return 0;