OpenMCU(三):STM32F103 FreeRTOS移植
发布日期:2025-04-29 02:22:17 浏览次数:16 分类:精选文章

本文共 3737 字,大约阅读时间需要 12 分钟。

STM32F103 FreeRTOS ???????????

??????????????? STM32F103 ??????? FreeRTOS ???????????????????? LED ???UART ?????

1. ?? Keil ???

????????? Keil C51 ????Keil ? FreeRTOS ?????????????????????????

2. STM32F103 ????

?? STM32F103 ???????????????????? Keil ????? USB ?????????????? Baud????? 115200?????

3. ?? FreeRTOS ??

? OpenMCU_RTOS ??????? STM32F1XX ??????HAL??????????

OpenMCU_RTOS\arch\STM32\STM32F1XX\CMSIS\Device\ST\STM32F1xx\Source\Templates

4. ????

? Keil ??????????????? STM32F103 ??????????????

  • ?????startup_stm32f103x6.s
  • HAL ???stm32f1xx_hal.c

???????? CPU ????? 48 MHz???????????? FreeRTOS ??????

5. ?? FreeRTOS ????

?? FreeRTOSConfig.h ??????????????????????????????????????????????

6. ????????

?????????????? LED ???UART ?????????????

/* ????? HAL ??? */#include "stm32f1xx_hal.h"#include "stm32f1xx_hal_uart.h"#include "stm32f1xx_hal_GPIO.c"/* ??????? */#define TASK_LED_PRIORITY 1#define TASK_UART_PRIORITY 2/* ????? */#define LED_TASK_STACK_SIZE 100#define UART_TASK_STACK_SIZE 100/* ?? ID */uint32_t led_task_id;uint32_t uart_task_id;/* LED ?? */bool led_state = false;/* ???? prototypes */void led_task(void *args);void uart_task(void *args);/* ????? */void init_GPIO_and_UART(void);/* ????? */UART_HandleTypeDef uart_handle;/* ?????? */void sys_init(void);/* ????????? */void read_UART_data(void);/* ??????? */TaskDescriptor *tasks;/* ???? */uint32_t task_create TaskDescriptor *taskDescriptor, uint32_t priority, uint32_t stackSize, uint32_t *taskID);/* ???? */void vTaskStart(void *taskDescriptor);/* ????? */void init_UART(void) {    uart_handle.Instance = UART_1;    uart_handle.BaudRate = 115200;    HAL_UART_Init(&uart_handle);}/* GPIO ??? */void init_GPIO_and_UART(void) {    GPIO_InitTypeDef GPIO_InitStruct;    // ??? GPIO ??    GPIO_InitStruct.Pin = GPIO_PIN_x;  // ?????????????    GPIO_InitStruct.Pull = GPIO_PULLUP;    GPIO_Init(GPIO_UART_IRQn, &GPIO_InitStruct);    // ??? UART    init_UART();}/* LED ???? */void led_task(void *args) {    while(1) {        // ?????? LED        if(led_state) {            GPIO_WriteBit(GPIO_LED, 1 << LED_OUTPUT_PIN);            led_state = 0;        } else {            GPIO_WriteBit(GPIO_LED, 0);            led_state = 1;        }        vTaskDelay(1000);  // ?? 1 ?    }}/* ???????? */void uart_task(void *args) {    while(1) {        if(UART_GetIT(&uart_handle) != 0) {            char data = UART_ReadData(&uart_handle);            printf("?????%c\r\n", data);            vTaskDelay(500);  // ?? 0.5 ?        }    }}/* ????? */int main() {    // ?????    sys_init();    // ???????    tasks = malloc( sizeof(TaskDescriptor) );    tasks->taskName = "LED";    tasks->priority = TASK_LED_PRIORITY;    tasks->stackSize = LED_TASK_STACK_SIZE;    led_task_id = task_create( tasks, TASK_LED_PRIORITY, LED_TASK_STACK_SIZE, &led_task_id );    vTaskStart( tasks );    tasks->taskName = "UART";    tasks->priority = TASK_UART_PRIORITY;    tasks->stackSize = UART_TASK_STACK_SIZE;    uart_task_id = task_create( tasks, TASK_UART_PRIORITY, UART_TASK_STACK_SIZE, &uart_task_id );    vTaskStart( tasks );    for( ;; );  // ??????}

7. ???????

? Keil ????????????????????????????

// ????? Baudrate ??BDOPATH "COM3"BAUD 115200// USB ??????? Baudrate// ???? USB ??????????// (???????????? Baudrate)// #define   UART_DEVICE   UART_1// #define   UART_BAUD    115200// ???????# define   UART_IRQ    14  // ???????????// ?????# define   UART_CLK     48000000  // ????????????

8. ???????

? Keil ???????????????????????????????????????????????? STM32F103 ??

9. ?????

??????? USB ???????? Keil ??????? UART ??? LED ??????????????????????? LED ????????????

10. ?????

????????? LED ?UART ??????????????????????????????????????

?????????????? STM32F103 ? FreeRTOS ??????????????????????????????????

上一篇:OpenMCU(三):STM32F103 FreeRTOS移植
下一篇:OpenMCU(一):STM32F407 FreeRTOS移植

发表评论

最新留言

表示我来过!
[***.240.166.169]2026年06月21日 17时25分13秒