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

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

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,11 +68,11 @@ 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");
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
}
}
@@ -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;
}