PlatformIO: libopencm3 + FreeRTOS

以下步骤基于常见的 Bluepill STM32F103C8T6, 也适用于其它 libopencm3 支持的MCU型号

方案一: 只复制需要的文件

  1. 在 PlatformIO 中, Board 选择 Bluepill F103C8, Framework 选择 libopencm3, 创建项目
  2. 在项目的lib下新建目录 FreeRTOS
  3. 解压缩最新的 FreeRTOS
    1. 复制 FreeRTOS/Source/ 目录下, 除 portable 目录以外其它全部文件和目录, 至 lib/FreeRTOS 下
    2. 复制 FreeRTOS/Source/portable/GCC/ARM_CM3 目录下所有文件(port.c, portmacro.h), 至 lib/FreeRTOS 下
    3. 复制 FreeRTOS/Source/portable/Common 目录下所有文件(mpu_wrappers.c), 至 lib/FreeRTOS 下
    4. 复制 FreeRTOS/Source/portable/MemMang 目录下 heap_4.c, 至 lib/FreeRTOS 下
  4. 复制 FreeRTOSConfig.h, 至 lib/FreeRTOS/include 下
  5. 编写 src/main.c

完成后目录结构应当为

├── include
│   └── README
├── lib
│   ├── FreeRTOS
│   │   ├── croutine.c
│   │   ├── event_groups.c
│   │   ├── heap_4.c
│   │   ├── include
│   │   │   ├── atomic.h
│   │   │   ├── croutine.h
│   │   │   ├── deprecated_definitions.h
│   │   │   ├── event_groups.h
│   │   │   ├── FreeRTOSConfig.h
│   │   │   ├── FreeRTOS.h
│   │   │   ├── list.h
│   │   │   ├── message_buffer.h
│   │   │   ├── mpu_prototypes.h
│   │   │   ├── mpu_wrappers.h
│   │   │   ├── portable.h
│   │   │   ├── projdefs.h
│   │   │   ├── queue.h
│   │   │   ├── semphr.h
│   │   │   ├── stack_macros.h
│   │   │   ├── StackMacros.h
│   │   │   ├── stdint.readme
│   │   │   ├── stream_buffer.h
│   │   │   ├── task.h
│   │   │   └── timers.h
│   │   ├── list.c
│   │   ├── mpu_wrappers.c
│   │   ├── port.c
│   │   ├── portmacro.h
│   │   ├── queue.c
│   │   ├── stream_buffer.c
│   │   ├── tasks.c
│   │   └── timers.c
│   └── README
├── platformio.ini
├── README.md
├── src
│   └── main.c
└── test
└── README

方案二: 完整的FreeRTOS, 使用library.json

  1. 在 PlatformIO 中, Board 选择 Bluepill F103C8, Framework 选择 libopencm3, 创建项目
  2. 在项目的lib下新建目录 FreeRTOS
  3. 解压缩最新的 FreeRTOS
  4. 复制 FreeRTOS/Source/ 目录下所有文件至 lib/FreeRTOS 下
  5. 复制 FreeRTOS/Source/portable/GCC/ARM_CM3 目录下 portmacro.h 至 lib/FreeRTOS 下, 因为 library.json 还不支持多个 include 路径
  6. 复制 FreeRTOSConfig.h, 至 lib/FreeRTOS 下
  7. lib/FreeRTOS 下添加 library.json
  8. 编写 src/main.c

library.json 内容如下, 只包含需要的 c 文件

{
"name": "FreeRTOS",
"version": "10.4.6",
"build": {
"srcFilter": [
"+<*.c>",
"+<portable/GCC/ARM_CM3/port.c>",
"+<portable/MemMang/heap_4.c>"
]
}
}

如果是多核MCU, 需要再包含 mpu_wrappers.c, 对于 F103C8 就不需要了

"+<portable/Common/mpu_wrappers.c>",

完成后目录结构为

├── include
│   └── README
├── lib
│   ├── FreeRTOS
│   │   ├── croutine.c
│   │   ├── event_groups.c
│   │   ├── FreeRTOSConfig.h
│   │   ├── include
│   │   ├── library.json
│   │   ├── list.c
│   │   ├── miniprintf.c
│   │   ├── miniprintf.h
│   │   ├── portable
│   │   ├── portmacro.h
│   │   ├── queue.c
│   │   ├── stream_buffer.c
│   │   ├── tasks.c
│   │   └── timers.c
│   └── README
├── platformio.ini
├── README.md
├── src
│   └── main.c
└── test
└── README

示例代码

使用Queue的UART收发

/* Task based UART demo, using queued communication.
*
* TX: A9 ====> RX of TTL serial
* RX: A10 <==== TX of TTL serial (not used)
*/
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h> #include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h> static QueueHandle_t uart_txq; // TX queue for UART /*********************************************************************
* Configure and initialize USART1:
*********************************************************************/
static void
uart_setup(void) { rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_USART1); // UART TX on PA9 (GPIO_USART1_TX)
gpio_set_mode(GPIOA,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
GPIO_USART1_TX); usart_set_baudrate(USART1,115200);
usart_set_databits(USART1,8);
usart_set_stopbits(USART1,USART_STOPBITS_1);
usart_set_mode(USART1,USART_MODE_TX);
usart_set_parity(USART1,USART_PARITY_NONE);
usart_set_flow_control(USART1,USART_FLOWCONTROL_NONE);
usart_enable(USART1); // Create a queue for data to transmit from UART
uart_txq = xQueueCreate(256,sizeof(char));
} /*********************************************************************
* USART Task:
*********************************************************************/
static void
uart_task(void *args __attribute__((unused))) {
char ch; for (;;) {
// Receive char to be TX
if ( xQueueReceive(uart_txq,&ch,500) == pdPASS ) {
// if not tx data buffer empty
while ( !usart_get_flag(USART1,USART_SR_TXE) )
taskYIELD(); // Yield until ready
usart_send(USART1,ch);
}
// Toggle LED to show signs of life
gpio_toggle(GPIOB,GPIO12);
gpio_toggle(GPIOC,GPIO13);
}
} /*********************************************************************
* Queue a string of characters to be TX
*********************************************************************/
static void
uart_puts(const char *s) { for ( ; *s; ++s ) {
// blocks when queue is full
xQueueSend(uart_txq,s,portMAX_DELAY);
}
} /*********************************************************************
* Demo Task:
* Simply queues up two line messages to be TX, one second
* apart.
*********************************************************************/
static void
demo_task(void *args __attribute__((unused))) { for (;;) {
uart_puts("Now this is a message..\n\r");
uart_puts(" sent via FreeRTOS queues.\n\n\r");
vTaskDelay(pdMS_TO_TICKS(1000));
}
} /*********************************************************************
* Main program & scheduler:
*********************************************************************/
int
main(void) { rcc_clock_setup_in_hse_8mhz_out_72mhz(); // CPU clock is 72 MHz // GPIO PB12,PC13:
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_GPIOC);
gpio_set_mode(
GPIOC,
GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
GPIO13);
gpio_set_mode(
GPIOB,
GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
GPIO12);
// Turn LED off
gpio_set(GPIOB, GPIO12);
gpio_set(GPIOC, GPIO13); uart_setup(); xTaskCreate(uart_task,"UART",100,NULL,configMAX_PRIORITIES-1,NULL);
xTaskCreate(demo_task,"DEMO",100,NULL,configMAX_PRIORITIES-1,NULL); vTaskStartScheduler();
for (;;);
return 0;
}

最新文章

  1. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
  2. HTML5播放暂停音乐
  3. 【JAVA 其它流对象】
  4. BI之SSAS完整实战教程3 -- 创建第一个多维数据集
  5. zend studio一些常用配置
  6. Dom对象的方法应用一getElementById技巧、getElementsByName() IE,firefox兼容
  7. 现代浏览器原生js获取id号方法
  8. 《编写高质量代码——Web前端开发修炼之道》读后随笔
  9. 《python基础教程》笔记之 字符串
  10. javascript 中 arguments.callee属性
  11. selenium获取元素后用click()点击没有作用,用Keys.ENTER就可以成功
  12. TextView设置不同字段不同点击事件
  13. TTL和COMS电平匹配以及电平转换的方法
  14. mysql常规巡检
  15. HTML5中input标签有用的新属性
  16. Mac同时安装python2和python3
  17. docker 第一次学习(一)--安装以及相关命令
  18. appium简明教程(3)——appium的安装windows版
  19. grub 启动错误 &quot;file not found&quot;
  20. nginx的MainLine version、Stable version、Legacy versions

热门文章

  1. Swoole 中使用 WebSocket 异步服务器、WebSocket 协程服务器
  2. unittest_测试报告(6)
  3. 单元测试 报错 org.junit.runners.model.InvalidTestClassError: Invalid test class &#39;com.example.xxx&#39; 解决
  4. POJ2115C Looooops
  5. Boost下载安装
  6. 2月1日 体温APP开发记录
  7. linux下编译支持opencl的opencv for android
  8. manjora20不小心卸载,重新安装terminal,软件商店/软件中心linux类似
  9. 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)
  10. qt 简单登录界面(一)