Optimization of RP2040 C/C++SDK combined with FreeRTOS

This commit is contained in:
aixiao 2024-06-04 15:45:40 +08:00
parent 92c0ee6bd8
commit 16014ec70b
10 changed files with 267 additions and 11 deletions

View File

@ -39,6 +39,7 @@ target_Sources(main PRIVATE
${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/ZE07CO.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/MHZ14B.cpp
)

View File

@ -3,9 +3,103 @@
QueueHandle_t xQueue;
#define MAX_TIMINGS 85
void ds18b20_init() {
gpio_init(DS18B20_PIN);
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 1); // 上拉电阻
}
void ds18b20_write_bit(uint8_t bit) {
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 0);
sleep_us(bit ? 10 : 60);
gpio_put(DS18B20_PIN, 1);
sleep_us(bit ? 55 : 5);
}
uint8_t ds18b20_read_bit() {
uint8_t bit = 0;
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 0);
sleep_us(3);
gpio_set_dir(DS18B20_PIN, GPIO_IN);
sleep_us(10);
bit = gpio_get(DS18B20_PIN);
sleep_us(50);
return bit;
}
void ds18b20_write_byte(uint8_t byte) {
for (int i = 0; i < 8; i++) {
ds18b20_write_bit(byte & 0x01);
byte >>= 1;
}
}
uint8_t ds18b20_read_byte() {
uint8_t byte = 0;
for (int i = 0; i < 8; i++) {
byte >>= 1;
if (ds18b20_read_bit()) {
byte |= 0x80;
}
}
return byte;
}
int ds18b20_reset() {
gpio_set_dir(DS18B20_PIN, GPIO_OUT);
gpio_put(DS18B20_PIN, 0);
sleep_us(480);
gpio_put(DS18B20_PIN, 1);
gpio_set_dir(DS18B20_PIN, GPIO_IN);
sleep_us(70);
int presence = !gpio_get(DS18B20_PIN);
sleep_us(410);
return presence;
}
float ds18b20_read_temperature() {
if (!ds18b20_reset()) {
printf("DS18B20 not found\n");
return -1;
}
ds18b20_write_byte(0xCC); // 跳过ROM指令
ds18b20_write_byte(0x44); // 启动温度转换
sleep_ms(750); // 温度转换时间,具体时间请参考传感器数据手册
if (!ds18b20_reset()) {
printf("DS18B20 not found\n");
return -1;
}
ds18b20_write_byte(0xCC); // 跳过ROM指令
ds18b20_write_byte(0xBE); // 读取暂存器
uint8_t lsb = ds18b20_read_byte();
uint8_t msb = ds18b20_read_byte();
int16_t temp = (msb << 8) | lsb;
// 检查传感器数据有效性
if (temp == 0xFFFF) {
printf("Invalid temperature data\n");
return -1;
}
return temp / 16.0; // 转换为摄氏度
}
// 温度传感器
void DS18B20(void *pvParameters)
{
// One_wire 第三方库方法
float TEMPERATURE = -1;
One_wire one_wire(DS18B20_PIN);
@ -20,15 +114,31 @@ void DS18B20(void *pvParameters)
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));
printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x DS18B20 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");
watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
/*
ds18b20_init();
while (1) {
float temperature = ds18b20_read_temperature();
if (temperature != -1) {
printf("DS18B20 Temperature: %.2f°C\n", temperature);
} else {
printf("Failed to read temperature\n");
}
watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
*/
return;
}

View File

@ -11,6 +11,7 @@
#include <stdbool.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/watchdog.h"
#include "../../SOFTWARE/Lib/pico-onewire/api/one_wire.h"
#define DS18B20_PIN 15 // DS18B20 引脚

View File

@ -57,14 +57,17 @@ void CO2(void *pvParameters)
uint16_t CO2_DATA = -1;
int MH_Z14B_DATA_IS_OK = 0;
MH_Z14B_INIT();
_printTaskStackHighWaterMark("MH_Z14B");
_printTaskStackHighWaterMark("CO2");
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");
//_printTaskStackHighWaterMark("CO2");
watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
printf("\n");
}
}

View File

@ -17,6 +17,7 @@
#include "hardware/pwm.h"
#include "hardware/adc.h"
#define UART1 uart1
#define BAUD_RATE 9600
#define DATA_BITS 8

View File

@ -110,6 +110,7 @@ void CH4(void *pvParameters)
ZC13("ZC05");
//_printTaskStackHighWaterMark("ZC13");
watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
}

View File

@ -10,6 +10,7 @@
#include <stdio.h>
#include <strings.h>
#include "pico/stdlib.h"
#include "hardware/watchdog.h"
#include "hardware/pio.h"
#include "uart_tx.pio.h"
#include "uart_rx.pio.h"

View File

@ -0,0 +1,81 @@
#include "ZE07CO.hpp"
#include "common.hpp"
void ZE07CO_INIT()
{
// 初始化UART
uart_init(UART0, BAUD_RATE);
gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART0_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART0, false, false);
uart_set_format(UART0, DATA_BITS, STOP_BITS, PARITY);
}
// CO
static uint16_t ZE07CO(int ANSWER, int *ZE07_CO_DATA_IS_OK)
{
if (ANSWER == 1) {
// 应答模式
static uint8_t _ANSWER[9] = { 0xFF, 0x01, 0x78, 0x41, 0x00, 0x00, 0x00, 0x00, 0x46 };
uart_write_blocking(UART0, _ANSWER, 9);
sleep_ms(100);
}
if (ANSWER == 2) {
// 主动上传模式
static uint8_t _ANSWER[9] = { 0xFF, 0x01, 0x78, 0x40, 0x00, 0x00, 0x00, 0x00, 0x47 };
uart_write_blocking(UART0, _ANSWER, 9);
sleep_ms(100);
}
// 读取
uint8_t CO_DATA[9] = { 0 };
uart_read_blocking(UART0, CO_DATA, 9);
sleep_ms(100);
/*
for(int i=0; i<9; i++) {
printf("0X%X ", CO_DATA[i]);
}
printf("\n");
*/
// CO 浓度
uint16_t CO_CONC = (256 * CO_DATA[4]) + CO_DATA[5];
// 校验
uint8_t CHECKSUM = (0xFF - (CO_DATA[1] + CO_DATA[2] + CO_DATA[3] + CO_DATA[4] + CO_DATA[5] + CO_DATA[6] + CO_DATA[7])) + 1;
if (CO_DATA[8] == CHECKSUM && CO_DATA[1] == 0x04) {
//printf("CHECKSUM: %X = %X\n", CO_DATA[8], CHECKSUM);
//printf("CO Concentration: %d ppm\n", CO_CONC);
*ZE07_CO_DATA_IS_OK = 1;
} else {
*ZE07_CO_DATA_IS_OK = 0;
}
return CO_CONC;
}
void CO(void *pvParameters)
{
uint16_t CO_DATA = -1;
int ZE07_CO_DATA_IS_OK = 0;
_printTaskStackHighWaterMark("CO");
while(1)
{
ZE07CO_INIT();
CO_DATA = ZE07CO(2, &ZE07_CO_DATA_IS_OK);
if (CO_DATA != -1 && ZE07_CO_DATA_IS_OK == 1) {
printf("CO Concentration: %d ppm\n", CO_DATA);
}
//_printTaskStackHighWaterMark("CO");
watchdog_update(); // 喂狗
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
return;
}

View File

@ -0,0 +1,31 @@
#ifndef ZE07CO_H
#define ZE07CO_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 UART0 uart0
#define BAUD_RATE 9600
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
#define UART0_TX_PIN 1 // T
#define UART0_RX_PIN 0 // R
extern void CO(void *pvParameters);
#endif

View File

@ -16,6 +16,7 @@
#include "common.hpp"
#include "ds18b20.hpp"
#include "ZC13.hpp"
#include "ZE07CO.hpp"
#include "MHZ14B.hpp"
#ifndef PICO_DEFAULT_LED_PIN
@ -48,11 +49,10 @@ void Led_Blinky(void *pvParameters)
printf("%f\n", TEMPERATURE);
}
//_printTaskStackHighWaterMark("Led_Blinky");
}
}
void Read_Onboard_Temperature(void *pvParameters)
void CPU(void *pvParameters)
{
(void)pvParameters;
@ -68,7 +68,7 @@ void Read_Onboard_Temperature(void *pvParameters)
float adc = (float)adc_read() * conversionFactor;
float tempC = 27.0f - (adc - 0.706f) / 0.001721f;
printf("Onboard temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
printf("CPU temperature %.02f°C %.02f°F\n", tempC, (tempC * 9 / 5 + 32));
//_printTaskStackHighWaterMark("Read_Onboard_Temperature");
@ -82,31 +82,57 @@ int main(void)
sleep_ms(3000);
//set_sys_clock_khz(250000, true);
if (watchdog_caused_reboot()) { // 判断是否从看门狗启动或者正常启动
printf("Rebooted by Watchdog!\n");
} else {
printf("Clean boot\n");
}
watchdog_enable(8300, 1); // 8秒检测是否重新加载看门狗计数器. (不更新计数器则重启硬件, 最高8秒)
watchdog_start_tick(12);
// 创建队列
xQueue = xQueueCreate(10, sizeof(long));
// 创建任务
BaseType_t xReturned;
TaskHandle_t CPU_xHandle = NULL;
TaskHandle_t Led_Blinky_xHandle = NULL;
TaskHandle_t DS18B20_xHandle = NULL;
TaskHandle_t CH4_xHandle = NULL;
TaskHandle_t CO_xHandle = NULL;
TaskHandle_t CO2_xHandle = NULL;
// 板载CPU温度
xReturned = xTaskCreate(CPU, "CPU task", 512, NULL, tskIDLE_PRIORITY, &CPU_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
printf("CPU() Task Error!");
}
// 板载LED闪烁
xReturned = xTaskCreate(Led_Blinky, "Blinky task", 512, NULL, tskIDLE_PRIORITY, &Led_Blinky_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
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!");
}
// CO
xReturned = xTaskCreate(CO, "CO task", 2048, NULL, tskIDLE_PRIORITY, &CO_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
printf("CO() Task Error!");
}
// CO2
xReturned = xTaskCreate(CO2, "CO2 task", 1024, NULL, tskIDLE_PRIORITY, &CO2_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
@ -114,6 +140,6 @@ int main(void)
}
vTaskStartScheduler();
while (1) {};
return 0;
}