FreeRTOS Add Boot Time

This commit is contained in:
aixiao 2024-12-29 17:24:29 +08:00
parent dbfdce8ec9
commit f4f1772db6
8 changed files with 85 additions and 6 deletions

View File

@ -4,7 +4,9 @@
"files.associations": { "files.associations": {
"cstring": "cpp", "cstring": "cpp",
"random": "cpp", "random": "cpp",
"system_error": "c" "system_error": "c",
"*.tcc": "cpp",
"streambuf": "cpp"
}, },
"cmake.ignoreCMakeListsMissing": true "cmake.ignoreCMakeListsMissing": true
} }

View File

@ -189,6 +189,7 @@ int loop(int fd, CONF *conf, DATA *DATA)
} }
} }
// CO // CO
if (strstr(receivedString, "CO")) { if (strstr(receivedString, "CO")) {
@ -246,6 +247,14 @@ int loop(int fd, CONF *conf, DATA *DATA)
printf("\n"); printf("\n");
} }
// Boot Time
if (strstr(receivedString, "Boot Time")) {
if (1 == (sscanf(receivedString, "Boot Time: %llu seconds\n", &DATA->boot_time_))) {
printf("Boot Time: %llu seconds\n", DATA->boot_time_);
}
}
} }
receivedString[0] = '\0'; receivedString[0] = '\0';
@ -360,6 +369,7 @@ int main()
data->co_1st = 1; data->co_1st = 1;
data->co2_1st = 1; data->co2_1st = 1;
data->_time = 180; data->_time = 180;
data->boot_time_ = 0;
redirect_stdout_to_file(logfd, "hc-12.log"); redirect_stdout_to_file(logfd, "hc-12.log");
pthread_mutex_init(&mutex, NULL); // 初始化互斥锁 pthread_mutex_init(&mutex, NULL); // 初始化互斥锁

View File

@ -48,6 +48,9 @@ typedef struct DATA {
float co2; float co2;
float ch4_; float ch4_;
// boot time
long long unsigned int boot_time_;
// 超过阈值次数 // 超过阈值次数
int ds18b20_num; int ds18b20_num;
int ch4_num; int ch4_num;

View File

@ -42,6 +42,7 @@ target_Sources(main PRIVATE
${CMAKE_CURRENT_LIST_DIR}/Source/ZE07CO.cpp ${CMAKE_CURRENT_LIST_DIR}/Source/ZE07CO.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/MHZ14B.cpp ${CMAKE_CURRENT_LIST_DIR}/Source/MHZ14B.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/HC-12.cpp ${CMAKE_CURRENT_LIST_DIR}/Source/HC-12.cpp
${CMAKE_CURRENT_LIST_DIR}/Source/boot_time.cpp
) )
target_include_directories(main PRIVATE target_include_directories(main PRIVATE
@ -76,6 +77,7 @@ target_link_libraries(
hardware_pwm hardware_pwm
hardware_adc hardware_adc
hardware_pio hardware_pio
hardware_timer
) )
pico_enable_stdio_uart(main 1) pico_enable_stdio_uart(main 1)

View File

@ -0,0 +1,39 @@
#include "common.hpp"
#include "HC-12.hpp"
#include "boot_time.hpp"
void BOOT_TIME(void *pvParameters)
{
(void)pvParameters;
_printTaskStackHighWaterMark("BOOT_TIME");
int _HC_12_SEND_NUM = 0;
char BOOT_TIME_TEMP[BUFER] = { 0 };
while (1) {
// 获取自开机以来的微秒数
uint64_t uptime_us = time_us_64();
uint64_t total_uptime_sec = uptime_us / 1000000;
// 打印开机秒数
printf("Boot Time: %llu seconds\n", total_uptime_sec);
{
_HC_12_SEND_NUM++;
if (_HC_12_SEND_NUM > 5) {
sprintf(BOOT_TIME_TEMP, "Boot Time: %llu seconds\n", total_uptime_sec);
_HC_12(BOOT_TIME_TEMP);
memset(BOOT_TIME_TEMP, 0, BUFER);
_HC_12_SEND_NUM = 0;
}
}
_printTaskStackHighWaterMark("BOOT_TIME");
vTaskDelay(pdMS_TO_TICKS(1000)); // 每秒延迟
}
}

View File

@ -0,0 +1,17 @@
#ifndef BOOT_TIME_H
#define BOOT_TIME_H
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include <cstring>
#include "pico/stdlib.h"
#define BUFER 256
extern void BOOT_TIME(void *pvParameters);
#endif

View File

@ -19,6 +19,7 @@
#include "ZE07CO.hpp" #include "ZE07CO.hpp"
#include "MHZ14B.hpp" #include "MHZ14B.hpp"
#include "HC-12.hpp" #include "HC-12.hpp"
#include "boot_time.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
@ -109,6 +110,7 @@ int main(int argc, char *argv[])
TaskHandle_t CH4_xHandle = NULL; TaskHandle_t CH4_xHandle = NULL;
TaskHandle_t CO_xHandle = NULL; TaskHandle_t CO_xHandle = NULL;
TaskHandle_t CO2_xHandle = NULL; TaskHandle_t CO2_xHandle = NULL;
TaskHandle_t BOOT_TIME_xHandle = NULL;
// 板载CPU温度 // 板载CPU温度
@ -146,6 +148,12 @@ int main(int argc, char *argv[])
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) { if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
printf("CO2() Task Error!"); printf("CO2() Task Error!");
} }
// Boot Time
xReturned = xTaskCreate(BOOT_TIME, "BOOT_TIME task", 512, NULL, tskIDLE_PRIORITY, &BOOT_TIME_xHandle);
if (xReturned == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY) {
printf("CO2() Task Error!");
}
vTaskStartScheduler(); vTaskStartScheduler();

View File

@ -304,6 +304,7 @@ int wifi_connect() {
} }
} }
/*
// 主程序 // 主程序
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
stdio_init_all(); stdio_init_all();
@ -349,10 +350,8 @@ int main(int argc, char *argv[]) {
cyw43_arch_deinit(); cyw43_arch_deinit();
return 0; return 0;
} }
*/
/*
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
stdio_init_all(); stdio_init_all();
@ -469,5 +468,4 @@ int main(int argc, char *argv[])
} }
return 0; return 0;
} }
*/