Flask 配置文件config之from_object 篇

本文详细解析了Flask中`app.config.from_object()`的配置加载方式。从`Flask`类的初始化,到`make_config`函数,再到`Config`类的定义,探讨了配置文件的加载过程。`root_path`和`default_config`参数的作用,以及`from_object`如何根据字符串或类对象加载配置。强调了配置名需大写,并指出需要先导入配置模块,例如`from config import config`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flask Web开发看到第7章,硬着头皮感觉看不下去

回过头去补基础

这里讲到 app.config.from_object() 这个加载配置的方式


首先,app是通过app=Flask(__name__)来实现的

那么,app的config方法肯定是通过Flask类里面获得的


先看到app的config属性的定义



然后,我们再看到make_config(instance_relative_config)这个函数的定义内容



他最终返回的是config_class这样一个方法,而config_class是一个类对象,而这个类对象是从Config类而来

而里面的参数root_path和default_config的解释如下

如下图


另外我们来看一下

应用的root_path的定义,自动指向应用根目录

另外看一下instance_relative_config 的解释:默认为False,如果设

#include "gd32f4xx_gpio.h" #include "gd32f4xx_rcu.h" #include "gd32f4xx_sdio.h" #include "gd32f4xx_misc.h" #include "Serial.h" #include "sdcard.h" #include "Delay.h" #include "ff.h" #include <string.h> #include <stdio.h> uint32_t SD_Get_RCA(void) { //发送 CMD3(请求分配 RCA) sdio_command_response_config(3, 0, SDIO_RESPONSETYPE_SHORT); // uint32_t timeout = 1000; // while (!sdio_flag_get(SDIO_FLAG_CMDRECV)) { // if (timeout-- == 0) { // return 0; // } // } sdio_flag_clear(SDIO_FLAG_CMDRECV); //从 RESP1 寄存器提取 RCA(高16位) uint32_t RCA = (sdio_response_get(SDIO_RESPONSE0) >> 16); return RCA; } void TF_Init() { uint32_t RCA=SD_Get_RCA(); rcu_periph_clock_enable(RCU_SDIO); rcu_periph_clock_enable(RCU_GPIOC); gpio_mode_set(GPIOC,GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO_PIN_9); gpio_output_options_set(GPIOC,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9); sdio_clock_config(SDIO_SDIOCLKEDGE_RISING,SDIO_CLOCKBYPASS_DISABLE,SDIO_CLOCKPWRSAVE_DISABLE,256); sdio_bus_mode_set(SDIO_BUSMODE_4BIT); sdio_clock_enable(); sdio_power_state_set(SDIO_POWER_ON); Delay_ms(2); // sdio_wait_type_set(); sdio_command_response_config(0,0,SDIO_RESPONSETYPE_NO); //发送CMD0,复位卡 while(sdio_flag_get(SDIO_FLAG_CMDSEND)==SET); sdio_command_response_config(8,0x1AA,SDIO_RESPONSETYPE_SHORT); //发送CMD8,检查电压支持 uint32_t response = sdio_response_get(SDIO_RESP0); sdio_command_response_config(55,RCA<<16,SDIO_RESPONSETYPE_SHORT); //发送CMD55,下次命令时特定应用命令 sdio_command_response_config(41,(1UL << 30) | 0x00FF8000,SDIO_RESPONSETYPE_LONG); while((response&0x80000000)==0); sdio_csm_enable(); sdio_dsm_enable(); sdio_command_response_config(2,0,SDIO_RESPONSETYPE_LONG); sdio_command_response_config(7,RCA,SDIO_RESPONSETYPE_SHORT); //发送CMD7,选择卡 sdio_command_response_config(16,512,SDIO_RESPONSETYPE_SHORT); } void TF_WriteData(uint8_t *data,uint32_t Len,uint32_t timeout,uint32_t WritAddr) { uint32_t RCA=SD_Get_RCA(); sdio_command_response_config(24,WritAddr,SDIO_RESPONSETYPE_SHORT); //CMD24单块写,CMD25多块写 sdio_wait_type_set(SDIO_WAITTYPE_NO); while(sdio_flag_get(SDIO_FLAG_CMDRECV)); sdio_flag_clear(SDIO_FLAG_CMDRECV); sdio_data_config(timeout,Len,SDIO_DATABLOCKSIZE_512BYTES); sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK,SDIO_TRANSDIRECTION_TOCARD); sdio_dma_disable(); sdio_command_response_config(13,RCA<<16,SDIO_RESPONSETYPE_SHORT); for(uint16_t i=0;i<128;i++) { sdio_data_write(*(uint32_t*)data); data+=4; } while(sdio_flag_get(SDIO_FLAG_DTEND)); sdio_flag_clear(SDIO_FLAG_DTEND); nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2); nvic_irq_enable(SDIO_IRQn,2,2); sdio_interrupt_enable(SDIO_INT_DTCRCERR); sdio_interrupt_enable(SDIO_INT_FLAG_DTEND); } uint32_t TF_ReadData(uint8_t *buffer,uint32_t timeout) { uint32_t RCA=SD_Get_RCA(); sdio_command_response_config(17,RCA,SDIO_RESPONSETYPE_SHORT); //发送CMD17,读取单个块 sdio_data_config(timeout,512,SDIO_DATABLOCKSIZE_512BYTES); sdio_data_transfer_config(SDIO_TRANSMODE_BLOCK,SDIO_TRANSDIRECTION_TOSDIO); sdio_dma_disable(); //禁用DMA,使用轮询 while (sdio_flag_get(SDIO_FLAG_RXDTVAL)==SET) //等待数据可用 for(uint16_t i=0;i<128;i++) { buffer[i]=sdio_data_read(); } while (sdio_flag_get(SDIO_FLAG_DTEND)==SET) //等待数据可用 sdio_flag_clear(SDIO_FLAG_DTEND); return *buffer; } void SDIO_IRQHandler() { if(sdio_interrupt_flag_get(SDIO_INT_FLAG_DTCRCERR)==SET) { sdio_command_response_config(12,0,SDIO_RESPONSETYPE_SHORT); } sdio_interrupt_flag_clear(SDIO_INT_FLAG_DTCRCERR); if(sdio_interrupt_flag_get(SDIO_INT_FLAG_DTEND)==SET) { TF_Init(); } sdio_interrupt_flag_clear(SDIO_INT_FLAG_DTEND); } #define CONFIG_FILE "config.ini" #define FLASH_RATIO_ADDR 0x0000 // 变比存储地址 #define FLASH_LIMIT_ADDR 0x0004 // 阈值存储地址 FATFS fs; // FatFs文件系统对象 FIL fil; // 文件对象 FRESULT fr; // 文件操作结果 // 解析配置文件 int parse_config(const char* buffer, float* ratio, float* limit) { if (buffer == NULL || ratio == NULL || limit == NULL) return 0; char *p = (char*)buffer; *ratio = 0.0f; *limit = 0.0f; int found = 0; // 查找Ratio部分 while ((p = strstr(p, "[Ratio]")) != NULL) { p += strlen("[Ratio]"); char *ch0 = strstr(p, "Ch0"); if (ch0 && sscanf(ch0, "Ch0 = %f", ratio) == 1) { found++; break; } } p = (char*)buffer; // 查找Limit部分 while ((p = strstr(p, "[Limit]")) != NULL) { p += strlen("[Limit]"); char *ch0 = strstr(p, "Ch0"); if (ch0 && sscanf(ch0, "Ch0 = %f", limit) == 1) { found++; break; } } return (found == 2); } sd_error_enum sd_init_status = SD_ERROR; // SD卡初始化状态 void update_config_from_sd(void) { FIL config_file; UINT bytes_read; char buffer[256]; float ratio = 0.0f, limit = 0.0f; // SD卡重新初始化 if(sd_init_status != SD_OK) { printf("Reinitializing SD card...\r\n"); sd_init_status = sd_init(); if(sd_init_status != SD_OK) { printf("SD init failed: %d\r\n", sd_init_status); return; } } // 挂载文件系统(带超时) FRESULT fr; uint32_t mount_timeout = 5000; do { fr = f_mount(0,&fs); // 挂载到根目录 if(fr == FR_OK) break; delay_1ms(1); } while(mount_timeout-- > 0); if(fr != FR_OK) { printf("Mount error: %d\r\n", fr); return; } printf("Filesystem mounted\r\n"); // 检查文件是否存在 - 使用绝对路径 FILINFO fno; fr = f_stat("config.ini", &fno); // 关键修复:添加根目录前缀 if(fr != FR_OK) { printf("config.ini not found! Error: %d\r\n", fr); f_mount(0, NULL); return; } // 打开文件 f_mkdir("/"); // 确保根目录存在 fr = f_open(&config_file, "config.ini", FA_READ); // 使用绝对路径 if(fr != FR_OK) { printf("File open error: %d\r\n", fr); f_mount(0, NULL); return; } // 读取文件内容 fr = f_read(&config_file, buffer, sizeof(buffer)-1, &bytes_read); if(fr != FR_OK || bytes_read == 0) { printf("Read error: %d, Bytes: %d\r\n", fr, bytes_read); f_close(&config_file); f_mount(0, NULL); return; } buffer[bytes_read] = '\0'; f_close(&config_file); printf("File content:\r\n%s\r\n", buffer); // 打印文件内容 // 解析配置 if(parse_config(buffer, &ratio, &limit)) { printf("Parsed: Ratio=%.2f, Limit=%.2f\r\n", ratio, limit); // 保存到FLASH spi_flash_sector_erase(FLASH_RATIO_ADDR); spi_flash_buffer_write((uint8_t*)&ratio, FLASH_RATIO_ADDR, sizeof(ratio)); spi_flash_sector_erase(FLASH_LIMIT_ADDR); spi_flash_buffer_write((uint8_t*)&limit, FLASH_LIMIT_ADDR, sizeof(limit)); } else { printf("Config parse failed\r\n"); } // 卸载文件系统 f_mount(0, NULL); printf("Config update complete\r\n"); } // 命令处理函数 void process_command(const char* cmd) { if (strcmp(cmd, "conf") == 0) { USART0_SendData((uint16_t*)"Updating config...\r\n", 20); update_config_from_sd(); } else if (strcmp(cmd, "help") == 0) { USART0_SendData((uint16_t*)"Available commands:\r\n", 21); USART0_SendData((uint16_t*)" conf - Update config from SD card\r\n", 36); USART0_SendData((uint16_t*)" help - Show this help\r\n", 25); } else { char unknown_msg[64]; sprintf(unknown_msg, "Unknown command: %s\r\n", cmd); USART0_SendData((uint16_t*)unknown_msg, strlen(unknown_msg)); USART0_SendData((uint16_t*)"Type 'help' for available commands\r\n", 36); } } FATFS fs; FIL current_log_file, current_sample_file, current_overlimit_file, current_hidedata_file; uint8_t log_file_open = 0, sample_file_open = 0, overlimit_file_open = 0, hidedata_file_open = 0; uint32_t log_id = 0; // 辅助函数:获取日期时间字符串 void get_datetime_string(char* datetime_str) { // 需要实现RTC获取时间功能 // 格式: YYYYMMDDHHMMSS sprintf(datetime_str, "20250101120000"); // 示例 } // 辅助函数:从Flash获取下一个日志ID uint32_t get_next_log_id(void) { uint32_t id = 0; // 从Flash读取ID (地址0x2000) // spi_flash_buffer_read((uint8_t*)&id, 0x2000, 4); return id; } // 辅助函数:保存日志ID到Flash void save_log_id_to_flash(uint32_t id) { // spi_flash_sector_erase(0x2000); // spi_flash_buffer_write((uint8_t*)&id, 0x2000, 4); } // 1. 存储系统初始化 void storage_init(void) { FRESULT res; // 挂载文件系统 res = f_mount(1,&fs); if (res != FR_OK) { USART0_SendData((uint16_t*)"SD mount failed\r\n", 16); return; } // 创建存储目录 f_mkdir("sample"); f_mkdir("overLimit"); f_mkdir("log"); f_mkdir("hideData"); // 初始化日志ID log_id = get_next_log_id(); // 创建日志文件 char log_filename[32]; sprintf(log_filename, "log/log%u.txt", log_id); res = f_open(&current_log_file, log_filename, FA_CREATE_ALWAYS | FA_WRITE); if (res == FR_OK) { log_file_open = 1; // 更新日志ID并保存 save_log_id_to_flash(log_id + 1); } } void log_operation(const char* operation) { if (!log_file_open) return; char datetime_str[16]; get_datetime_string(datetime_str); char log_entry[128]; sprintf(log_entry, "[%s] %s\r\n", datetime_str, operation); UINT bytes_written; f_write(&current_log_file, log_entry, strlen(log_entry), &bytes_written); f_sync(&current_log_file); } void store_sample_data(float voltage) { if (!sample_file_open) { char datetime_str[16]; get_datetime_string(datetime_str); char filename[32]; sprintf(filename, "sample/sampleData%s.txt", datetime_str); if (f_open(&current_sample_file, filename, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) { sample_file_open = 1; } } if (sample_file_open) { char data_line[64]; sprintf(data_line, "%.1fV\r\n", voltage); UINT bytes_written; f_write(&current_sample_file, data_line, strlen(data_line), &bytes_written); f_sync(&current_sample_file); } } void store_overlimit_data(float voltage, float limit) { if (!overlimit_file_open) { char datetime_str[16]; get_datetime_string(datetime_str); char filename[32]; sprintf(filename, "overLimit/overLimit%s.txt", datetime_str); if (f_open(&current_overlimit_file, filename, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) { overlimit_file_open = 1; } } if (overlimit_file_open) { char data_line[64]; sprintf(data_line, "%.1fV > %.1fV\r\n", voltage, limit); UINT bytes_written; f_write(&current_overlimit_file, data_line, strlen(data_line), &bytes_written); f_sync(&current_overlimit_file); } } void store_hidedata(float voltage) { if (!hidedata_file_open) { char datetime_str[16]; get_datetime_string(datetime_str); char filename[32]; sprintf(filename, "hideData/hideData%s.txt", datetime_str); if (f_open(&current_hidedata_file, filename, FA_CREATE_ALWAYS | FA_WRITE) == FR_OK) { hidedata_file_open = 1; } } if (hidedata_file_open) { char data_line[64]; sprintf(data_line, "HIDE: %.1fV\r\n", voltage); UINT bytes_written; f_write(&current_hidedata_file, data_line, strlen(data_line), &bytes_written); f_sync(&current_hidedata_file); } } void save_config_to_file(float ratio, float limit) { FIL config_file; if (f_open(&config_file, "config.ini", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) { return; } char config_data[128]; sprintf(config_data, "[Ratio]\r\n" "Ch0 = %.1f\r\n" "\r\n" "[Limit]\r\n" "Ch0 = %.1f\r\n", ratio, limit); UINT bytes_written; f_write(&config_file, config_data, strlen(config_data), &bytes_written); f_close(&config_file); } // 写入Flash的辅助函数 void write_config_to_flash(float ratio, float limit) { spi_flash_sector_erase(FLASH_RATIO_ADDR); spi_flash_buffer_write((uint8_t*)&ratio, FLASH_RATIO_ADDR, sizeof(ratio)); spi_flash_sector_erase(FLASH_LIMIT_ADDR); spi_flash_buffer_write((uint8_t*)&limit, FLASH_LIMIT_ADDR, sizeof(limit)); } // 读取配置文件 void read_config_file(void) { UINT bytes_read; char buffer[256]; // 读取缓冲区 float ratio = 0.0f, limit = 0.0f; // 挂载文件系统 fr = f_mount(1,&fs); if (fr != FR_OK) { // 错误处理 return; } // 打开配置文件 fr = f_open(&fil, CONFIG_FILE, FA_READ); if (fr == FR_OK) { // 读取文件内容 fr = f_read(&fil, buffer, sizeof(buffer) - 1, &bytes_read); if (fr == FR_OK) { buffer[bytes_read] = '\0'; // 确保字符串结束 // 解析配置 if (parse_config(buffer, &ratio, &limit)) { // 写入Flash write_config_to_flash(ratio, limit); } } f_close(&fil); // 关闭文件 } } typedef enum { FR_OK = 0, /* (0) Succeeded */ FR_DISK_ERR, /* (1) A hard error occured in the low level disk I/O layer */ FR_INT_ERR, /* (2) Assertion failed */ FR_NOT_READY, /* (3) The physical drive cannot work */ FR_NO_FILE, /* (4) Could not find the file */ FR_NO_PATH, /* (5) Could not find the path */ FR_INVALID_NAME, /* (6) The path name format is invalid */ FR_DENIED, /* (7) Acces denied due to prohibited access or directory full */ FR_EXIST, /* (8) Acces denied due to prohibited access */ FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ FR_NOT_ENABLED, /* (12) The volume has no work area */ FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */ FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ FR_LOCKED, /* (16) The operation is rejected according to the file shareing policy */ FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */ FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ } FRESULT; ====system init==== conf Command received: conf Updating config from SD card... Filesystem mounted SD init success config.ini not found! Error: 3 > test Command received: test ====System selftest===== flash.......ok flash ID:0xC84013 TF card...........ok TF card memory: 30721024 KB RTC: Current time: 2025-05-01 : 08:32:47 ====system selftest==== TF卡在读卡器中识别到了相应的配置文件,优化代码,解决问题
最新发布
06-18
[1/9] Performing build step for 'bootloader' [1/1] cmd.exe /C "cd /D E:\esp32\project\caogao\lcd-3\build\bootloader\esp-idf\esptool_py && e:\esp32\ESP-IDF\5.2.3\Espressif\python_env\idf5.2_py3.11_env\Scripts\python.exe E:/esp32/ESP-IDF/5.2.3/Espressif/frameworks/esp-idf-v5.2.3/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x0 E:/esp32/project/caogao/lcd-3/build/bootloader/bootloader.bin" Bootloader binary size 0x5a30 bytes. 0x25d0 bytes (30%) free. [2/7] Building C object esp-idf/wifi_scan/CMakeFiles/__idf_wifi_scan.dir/wifiscan.c.obj In file included from E:/esp32/ESP-IDF/5.2.3/Espressif/frameworks/esp-idf-v5.2.3/components/nvs_flash/include/nvs.h:13, from E:/esp32/ESP-IDF/5.2.3/Espressif/frameworks/esp-idf-v5.2.3/components/nvs_flash/include/nvs_flash.h:13, from E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.h:8, from E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.c:1: E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.c: In function 'wifi_my_connect_task': E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.c:229:60: warning: passing argument 2 of 'esp_wifi_set_config' from incompatible pointer type [-Wincompatible-pointer-types] 229 | ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA,&wifi_config)); | ^~~~~~~~~~~~ | | | wifi_init_config_t * E:/esp32/ESP-IDF/5.2.3/Espressif/frameworks/esp-idf-v5.2.3/components/esp_common/include/esp_err.h:117:30: note: in definition of macro 'ESP_ERROR_CHECK' 117 | esp_err_t err_rc_ = (x); \ | ^ In file included from E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.h:9: E:/esp32/ESP-IDF/5.2.3/Espressif/frameworks/esp-idf-v5.2.3/components/esp_wifi/include/esp_wifi.h:918:74: note: expected 'wifi_config_t *' but argument is of type 'wifi_init_config_t *' 918 | esp_err_t esp_wifi_set_config(wifi_interface_t interface, wifi_config_t *conf); | ~~~~~~~~~~~~~~~^~~~ E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.c:237:20: warning: unused variable 'ux_wifi_start_BIT' [-Wunused-variable] 237 | EventBits_t ux_wifi_start_BIT = xEventGroupWaitBits( | ^~~~~~~~~~~~~~~~~ E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.c:196:17: warning: unused variable 'ux_wifi_init_BIT' [-Wunused-variable] 196 | EventBits_t ux_wifi_init_BIT = xEventGroupWaitBits( | ^~~~~~~~~~~~~~~~ E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.h: At top level: E:/esp32/project/caogao/lcd-3/components/wifi_scan/wifiscan.h:65:12: warning: 'wifi_connect_readystate' defined but not used [-Wunused-variable] 65 | static int wifi_connect_readystate=0; | ^~~~~~~~~~~~~~~~~~~~~~~
03-16
/* FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. All rights reserved VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. This file is part of the FreeRTOS distribution. FreeRTOS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (version 2) as published by the Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception. *************************************************************************** >>! NOTE: The modification to the GPL is included to allow you to !<< >>! distribute a combined work that includes FreeRTOS without being !<< >>! obliged to provide the source code for proprietary components !<< >>! outside of the FreeRTOS kernel. !<< *************************************************************************** FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Full license text is available on the following link: http://www.freertos.org/a00114.html *************************************************************************** * * * FreeRTOS provides completely free yet professionally developed, * * robust, strictly quality controlled, supported, and cross * * platform software that is more than just the market leader, it * * is the industry's de facto standard. * * * * Help yourself get started quickly while simultaneously helping * * to support the FreeRTOS project by purchasing a FreeRTOS * * tutorial book, reference manual, or both: * * http://www.FreeRTOS.org/Documentation * * * *************************************************************************** http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading the FAQ page "My application does not run, what could be wrong?". Have you defined configASSERT()? http://www.FreeRTOS.org/support - In return for receiving this top quality embedded software for free we request you assist our global community by participating in the support forum. http://www.FreeRTOS.org/training - Investing in training allows your team to be as productive as possible as early as possible. Now you can receive FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers Ltd, and the world's leading authority on the world's leading RTOS. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, including FreeRTOS+Trace - an indispensable productivity tool, a DOS compatible FAT file system, and our tiny thread aware UDP/IP stack. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS licenses offer ticketed support, indemnification and commercial middleware. http://www.SafeRTOS.com - High Integrity Systems also provide a safety engineered and independently SIL3 certified version for use in safety and mission critical applications that require provable dependability. 1 tab == 4 spaces! */ #ifndef FREERTOS_CONFIG_H #define FREERTOS_CONFIG_H #include "stm32f10x.h" // Device header //* 1:RTOS使用抢占式调度器;0:RTOS使用协作式调度器(时间片) #define configUSE_PREEMPTION 1 /*一些 FreeRTOS 移植有两种选择下一个执行任务的方法, *0:一种是通用方法, *1:另一种是移植特定的方法(依赖于一种或多种架构特定的汇编指令 (通常是前导零计数 [CLZ] 指令或同等指令) , *因此 仅适用于专为其编写该指令的架构。)。 */ //#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0 //设置为 1,使用 低功耗无 滴答 模式, 或设置为 0,保持 滴答 中断始终运行。 #define configUSE_TICKLESS_IDLE 1 //内核时钟 #define configCPU_CLOCK_HZ (SystemCoreClock) //SysTick 时钟频率 系统已定义,此处不用定义 //#define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ //RTOS系统节拍中断的频率 1ms #define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) //可使用的最大优先级 #define configMAX_PRIORITIES (32) //空闲任务使用的堆栈大小 #define configMINIMAL_STACK_SIZE ( ( unsigned short ) 128 ) //任务名字字符串长度 #define configMAX_TASK_NAME_LEN 16 //系统节拍计数器变量数据类型,1表示为16位无符号整形,0表示为32位无符号整形 #define configUSE_16_BIT_TICKS 0 // RTOS 内核启动后,滴答中断已经执行的次数。滴答数存放在 滴答Type_t 类型的变量中 //#define configTICK_TYPE_WIDTH_IN_BITS TICK_TYPE_WIDTH_32_BITS //空闲任务放弃CPU使用权给其他同优先级的用户任务 #define configIDLE_SHOULD_YIELD 1 //开启任务通知功能,1开启 //#define configUSE_TASK_NOTIFICATIONS 1 //每个 RTOS 任务都有一个任务通知数组。 configTASK_NOTIFICATION_ARRAY_ENTRIES 设置数组的索引数。 //#define configTASK_NOTIFICATION_ARRAY_ENTRIES 3 //1 使用互斥信号量 #define configUSE_MUTEXES 1 //1 使用递归互斥信号量 //#define configUSE_RECURSIVE_MUTEXES 0 //1 使用计数信号量 //#define configUSE_COUNTING_SEMAPHORES 0 //#define configUSE_ALTERNATIVE_API 0 /* Deprecated! */ //设置可以注册的信号量和消息队列个数 //#define configQUEUE_REGISTRY_SIZE 10 //启用队列 //#define configUSE_QUEUE_SETS 0 //1使能时间片调度 //#define configUSE_TIME_SLICING 1 //如果 configUSE_NEWLIB_REENTRANT 设置为 1,那么将为每个创建的任务分配一个newlib 重入结构体 。 //#define configUSE_NEWLIB_REENTRANT 0 //1 兼容8.0以前的版本 //#define configENABLE_BACKWARD_COMPATIBILITY 0 //设置每个任务的 线程本地存储数组 //#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5 //设置为 0 时, MiniListItem_t 和 ListItem_t 保持一致。设置为 1 时,MiniListItem_t 包含的字段比 ListItem_t 少 3 个 //#define configUSE_MINI_LIST_ITEM 1 //设置调用 xTaskCreate() 时用于指定堆栈深度的类型 //#define configSTACK_DEPTH_TYPE uint16_t //消息缓冲区使用 configMESSAGE_BUFFER_LENGTH_TYPE 类型的变量存储 每个消息的长度 //#define configMESSAGE_BUFFER_LENGTH_TYPE size_t // //#define configHEAP_CLEAR_MEMORY_ON_FREE 1 /***************************************************************** FreeRTOS与内存申请有关配置选项 *****************************************************************/ //支持静态内存 #define configSUPPORT_STATIC_ALLOCATION 0 //支持动态内存申请 #define configSUPPORT_DYNAMIC_ALLOCATION 1 //系统所有总的堆大小 #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 10 * 1024 ) ) /*默认情况下,FreeRTOS 堆由 FreeRTOS 声明 并由链接器放置在存储器中。 *将 configAPPLICATION_ALLOCATED_HEAP 设置为 1, *允许应用程序编写者声明堆,这使得 应用程序编写者可以将堆放置在内存的任意位置。 *如果使用 heap_1.c、heap_2.c 或 heap_4.c,并且将 configAPPLICATION_ALLOCATED_HEAP 设置为 1, *那么应用程序编写者必须提供一个 uint8_t 数组, 其确切的名称和大小如下所示。 *该数组将用作 FreeRTOS 堆。 数组如何放置在内存的特定位置取决于使用的编译器, 请参阅编译器的文档。 */ //#define configAPPLICATION_ALLOCATED_HEAP 1 /*设置为 1,那么对于任何 使用 xTaskCreate 或 xTaskCreateRestricted API 创建的任务, *其堆栈 使用 pvPortMallocStack 函数分配并使用 vPortFreeStack 函数释放。 *用户需要 实现线程安全的 pvPortMallocStack 和 vPortFreeStack 函数。 */ //#define configSTACK_ALLOCATION_FROM_SEPARATE_HEAP 1 /*************************************************************** FreeRTOS与钩子函数有关的配置选项 **************************************************************/ /* 置1:使用空闲钩子(Idle Hook类似于回调函数);置0:忽略空闲钩子 * * 空闲任务钩子是一个函数,这个函数由用户来实现, * FreeRTOS规定了函数的名字和参数:void vApplicationIdleHook(void ), * 这个函数在每个空闲任务周期都会被调用 * 对于已经删除的RTOS任务,空闲任务可以释放分配给它们的堆栈内存。 * 因此必须保证空闲任务可以被CPU执行 * 使用空闲钩子函数设置CPU进入省电模式是很常见的 * 不可以调用会引起空闲任务阻塞的API函数 */ #define configUSE_IDLE_HOOK 0 /* 置1:使用时间片钩子(Tick Hook);置0:忽略时间片钩子 * * * 时间片钩子是一个函数,这个函数由用户来实现, * FreeRTOS规定了函数的名字和参数:void vApplicationTickHook(void ) * 时间片中断可以周期性的调用 * 函数必须非常短小,不能大量使用堆栈, * 不能调用以”FromISR" 或 "FROM_ISR”结尾的API函数 */ /*xTaskIncrementTick函数是在xPortSysTickHandler中断函数中被调用的。因此,vApplicationTickHook()函数执行的时间必须很短才行*/ #define configUSE_TICK_HOOK 0 /* * 大于0时启用堆栈溢出检测功能,如果使用此功能 * 用户必须提供一个栈溢出钩子函数,如果使用的话 * 此值可以为1或者2,因为有两种栈溢出检测方法 */ //#define configCHECK_FOR_STACK_OVERFLOW 0 //使用内存申请失败钩子函数 #define configUSE_MALLOC_FAILED_HOOK 0 //#define configUSE_DAEMON_TASK_STARTUP_HOOK 0 //#define configUSE_SB_COMPLETED_CALLBACK 0 /******************************************************************** FreeRTOS与运行时间和任务状态收集有关的配置选项 **********************************************************************/ //启用运行时间统计功能 //#define configGENERATE_RUN_TIME_STATS 0 //启用可视化跟踪调试 //#define configUSE_TRACE_FACILITY 1 /* 与宏configUSE_TRACE_FACILITY同时为1时会编译下面3个函数 * prvWriteNameToBuffer() * vTaskList(), * vTaskGetRunTimeStats() */ //#define configUSE_STATS_FORMATTING_FUNCTIONS 1 /******************************************************************** FreeRTOS与协程有关的配置选项 *********************************************************************/ //启用协程,启用协程以后必须添加文件croutine.c #define configUSE_CO_ROUTINES 0 ////协程的有效优先级数目 #define configMAX_CO_ROUTINE_PRIORITIES 2 /*********************************************************************** FreeRTOS与软件定时器有关的配置选项 **********************************************************************/ //启用软件定时器 #define configUSE_TIMERS 0 //软件定时器优先级 #define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES-1) //软件定时器队列长度 #define configTIMER_QUEUE_LENGTH 10 //软件定时器任务堆栈大小 #define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE*2) /************************************************************ 中断嵌套行为配置 ************************************************************/ #ifdef __NVIC_PRIO_BITS #define configPRIO_BITS __NVIC_PRIO_BITS #else #define configPRIO_BITS 4 #endif //中断最低优先级 #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15 //系统可管理的最高中断优先级 #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 #define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) ) /* 240 */ #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) ) //#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application] /************************************************************ 断言配置 ************************************************************/ //#define configASSERT( ( x ) ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ ) /************************************************************ FreeRTOS MPU的具体定义 ************************************************************/ //#define configINCLUDE_APPLICATION_DEFINED_PRIVILEGED_FUNCTIONS 0 //#define configTOTAL_MPU_REGIONS 8 /* Default value. */ //#define configTEX_S_C_B_FLASH 0x07UL /* Default value. */ //#define configTEX_S_C_B_SRAM 0x07UL /* Default value. */ //#define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY 1 //#define configALLOW_UNPRIVILEGED_CRITICAL_SECTIONS 1 //#define configENABLE_ERRATA_837070_WORKAROUND 1 //#define configUSE_MPU_WRAPPERS_V1 0 //#define configPROTECTED_KERNEL_OBJECT_POOL_SIZE 10 //#define configSYSTEM_CALL_STACK_SIZE 128 ///* ARMv8-M secure side port related definitions. */ //#define secureconfigMAX_SECURE_CONTEXTS 5 /************************************************************ FreeRTOS可选函数配置选项 ************************************************************/ #define INCLUDE_vTaskPrioritySet 1 #define INCLUDE_uxTaskPriorityGet 1 #define INCLUDE_vTaskDelete 1 #define INCLUDE_vTaskSuspend 1 #define INCLUDE_xResumeFromISR 1 #define INCLUDE_vTaskDelayUntil 1 #define INCLUDE_vTaskDelay 1 #define INCLUDE_xTaskGetSchedulerState 1 #define INCLUDE_xTaskGetCurrentTaskHandle 1 #define INCLUDE_uxTaskGetStackHighWaterMark 1 #define INCLUDE_uxTaskGetStackHighWaterMark2 0 #define INCLUDE_xTaskGetIdleTaskHandle 0 #define INCLUDE_eTaskGetState 1 #define INCLUDE_xEventGroupSetBitFromISR 1 #define INCLUDE_xTimerPendFunctionCall 0 #define INCLUDE_xTaskAbortDelay 0 #define INCLUDE_xTaskGetHandle 0 #define INCLUDE_xTaskResumeFromISR 1 #define INCLUDE_vTaskCleanUpResources 0 /* A header file that defines trace macro can be included here. */ #define xPortPendSVHandler PendSV_Handler #define vPortSVCHandler SVC_Handler //#define xPortSysTickHandler SysTick_Handler #endif /* FREERTOS_CONFIG_H */ freertos用的是什么时钟?
05-27
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值