- 開發環境:Window 10 64bit
- 開發工具:IAR Embedded Workbench
- 硬件:stm32f103c8t6
準備工作:
下載一份IAR的stm32裸機工程(包含標準庫),參考博客,下載鏈接。
下載RT-Thread Nano源碼,下載鏈接。
添加RT-Thread Nano源碼到IAR的工程:
在STM32-IAR-Demo目錄下創建文件夾RT-Thread,並將以下文件添加到該文件夾裏:
-
Nano 源碼中的 include、libcpu、src 文件夾。
-
配置文件:源碼代碼 rtthread/bsp 文件夾中的兩個文件:board.c與rtconfig.c。
打開STM32-IAR-Demo/Project下的IAR工程STM32-IAR-Demo.eww,添加Rtthread分組,並將以下文件添加到該分組:
- 添加工程下RT-Thread/src/文件夾中所有的文件;
- 添加工程下RT-Thread//libcpu/ 文件夾中相應內核的 CPU 移植文件及上下文切換文件:cpuport.c以及context_iar.S;
- 添加工程下RT-Thread/文件夾下的board.c
添加頭文件路徑,點擊Project->Options->C/C++Compiler->Preprocessor。
(提供複製)
$PROJ_DIR$\..\RT-Thread
$PROJ_DIR$\..\RT-Thread\include
適配 RT-Thread Nano
修改User組下的stm32f10x_it.c中斷函數文件:
- 去掉void HardFault_Handler(void)函數
- 去掉void PendSV_Handler(void)函數
- 去掉void SysTick_Handler(void)函數
以上三個函數由RT-Thread系統實現,避免重複定義。
實現rt_kprintf串口打印功能,rt_kprintf函數系統已經實現,我們只要實現它裏面的rt_hw_console_output函數和串口的初始化。
- rtconfig.h文件裏打開宏#define RT_USING_CONSOLE
- 在board.c文件末尾加入一下代碼:
// USART1 PA9-TX PA10-RX
static int uart_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//配置串口1 (USART1) 時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
//配置串口1接收終端的優先級
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//配置串口1 發送引腳(PA.09)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置串口1 接收引腳 (PA.10)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//串口1工作模式(USART1 mode)配置
USART_InitStructure.USART_BaudRate = 115200;//一般設置爲9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數據位爲8個字節
USART_InitStructure.USART_StopBits = USART_StopBits_1; //一位停止位
USART_InitStructure.USART_Parity = USART_Parity_No ; //無校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不需要流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //接收發送模式
USART_Init(USART1, &USART_InitStructure);
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啓中斷
USART_Cmd(USART1, ENABLE);//使能串口
return 0;
}
INIT_BOARD_EXPORT(uart_init);
void rt_hw_console_output(const char *str)
{
rt_size_t i = 0, size = 0;
char a = '\r';
size = rt_strlen(str);
for (i = 0; i < size; i++)
{
if (*(str + i) == '\n')
{
USART_SendData(USART1, a);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
}
USART_SendData(USART1, *(str + i));
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
}
}
int rt_hw_console_getchar(void)
{
int ch = -1;
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
{
ch = USART_ReceiveData(USART1);
USART_ClearFlag(USART1, USART_FLAG_RXNE);
}
else
{
//rt_thread_mdelay(10);
}
return ch;
}
uart_init函數不需要添加到初始化函數調用,使用INIT_BOARD_EXPORT宏之後再系統初始化時自動調用。
編寫第一個應用:
- 在main文件首部增加RT-Thread的相關頭文件<rtthread.h>
- 在main()函數中實現PC13引腳的LED指示等閃爍,並使用串口打印信息
- 調用rt_thread_mdelay(500)延時函數時,切換到其他線程運行,體現了線程實時性的特點。
#include "stm32f10x.h"
#include <rtthread.h>
#define LD2_GPIO_PORT GPIOC
#define LD2_PIN GPIO_Pin_13
static void MX_GPIO_Init(void);
int main(void)
{
MX_GPIO_Init();
while (1)
{
GPIO_SetBits(LD2_GPIO_PORT, LD2_PIN);
rt_thread_mdelay(500);
GPIO_ResetBits(LD2_GPIO_PORT, LD2_PIN);
rt_thread_mdelay(500);
rt_kprintf("main runing.\r\n");
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_ResetBits(LD2_GPIO_PORT, LD2_PIN);
GPIO_InitStruct.GPIO_Pin = LD2_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LD2_GPIO_PORT, &GPIO_InitStruct);
}
編譯下載到開發板,程序運行,led正常閃爍,接上串口助手,可看到打印信息。
已移植的完整工程代碼,下載鏈接。
如若有誤,還望指出,謝謝。