最近開始Study FreeRTOS,買了一塊STM32F103開發板。參考 FreeRTOS on STM32F103C8T6 (Many thanks to Aurelian Ioan) 的步驟在STM32F103C8T6開發板上把FreeRTOS跑起來。這裡做個筆記紀綠下來。
1. 需要的軟硬體
-
STM32F103C8T6 開發板Blue Pill
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
電路圖
-
USB to USART converter (eg. FTDI或PL2303)
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
我手上的是PL2303。
PL2303HXA PHASED OUT SINCE 2012, PLEASE CONTACT YOUR SUPPLIER問題解決辦法
- ST-Link V2 for STM8 & STM32
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
驅動與安裝
使用說明
- ARM Keil MDK uVision software
我安裝的版本是v5.38
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
2. Keil MDK需要先裝Pack
-
點選Pack Installer Icon
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
Search STM32F103C8
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
然後把下面這些Pack裝起來
- Keil::STM32F1xx_DFP
- ARM::CMSIS
- ARM::CMSIS-Driver
- ARM::CMSIS-FreeRTOS
- Keil::ARM_Compiler
3. Create MDK Project
-
Create a new project click on the Project -> "New uVision Project".
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
指定好Project Name之後 Search stm32f103c8 點選後按下OK
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
選取 CMSIS -> CORE
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
選取 CMSIS Driver -> USART(API) -> USART ,如果出現黃色的方塊的話,按下面的 Resolve Button
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
選取 Compiler -> I/O -> STDOUT選取User以及 Device -> DMA, GPIO及Startup
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
RTOS 選取設定 Config 選取[FreeRTOS], Heap, Timers
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
Project Create完成後,如下圖所示。基本上project已經create完成。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
4. Enable USART1 並指定Tx/Rx Pin
-
點選Target 1 –> Device –> RTE_Device.h
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
修改下面的#define,Enabe USART1, 指定Tx pin到PA9, Rx pin到PA10
5. 把printf輸出導向到USART
-
Target 1右鍵 Add New Item to Group..
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
User Code Template –> Compier –> STDOUT via USART 按Add
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
修改stdout_USART.c 把 USART_DRV_NUM 改成 1, USART_BAUDRATE 改成 115200
6. 加入main.c 測試code
main.c主要執行如下:
- MCU使用 SystemInit() 初始化,Clock使用 SystemCoreClockUpdate() 和 stdout_init() 初始化 USART1 設置。
- 創建了 2 個Task和一個Binary Semaphore
- FreeRTOS Task Scheduler 啟動
- 這兩個Task將以相同的優先級運行,因此它們將具有相同的執行時間。 Semaphore用於防止兩個Task同時競爭輸出相應的字符串。
- 當Task 1 處於“運行”狀態時,它將獲取Semaphore,使用 printf() 並將Semaphore返回給另一個Task使用。 如果Task 2 在Task 1 使用Semaphore時嘗試獲取Semaphore,它將進入“阻塞”狀態並等待Semaphore可用。
7. 編譯問題解決
8. 接線
9. 燒錄image
11. Task問題解決
-
燒錄完image後,按下板子的reset key,發現只有 Initialization is done. 的打印。
Task1, Task2的打印沒有出來。

-
使用 Debug-> Start/Stop Debug Session (Ctrl+F5) 用Download的方式定位問題

-
問題#1 : Assert在 task.c vTaskStartScheduler() 2066行。

解決方式:
Target 1 –> RTOS –> FreeRTOSConfig.h 修改 configMINIMAL_STACK_SIZE 為 128
-
問題#2 : Assert在 port.c xPortStartScheduler() 320行。

解決方式:
Target 1 –> RTOS –> FreeRTOSConfig.h 修改 configPRIO_BITS 為 4
12. 輸出結果
解決問題後,就可以看到輸出結果的打印了。

Project commit to github
https://github.com/lucas0514/freertos-stm32f103c8t6