- 准备freeRTOS源码和一个简单的工程
freeRTOS源码下载链接:
链接:https://pan.baidu.com/s/1hgyQqoDqDuETEHr_I80M8Q 提取码:e890
另外还需要准备一个简单的工程,这里使用led闪烁的例子来完成移植已移植好的stm32f103ve例程下载链接:
https://download.csdn.net/download/mygod2008ok/12234264
- 开始移植
- 在led闪烁工程中新建一个文件夹,并命名为freeRTOS
#define INCLUDE_xTaskGetCurrentTaskHandle 1
/**
* @file main.c
* @author 仙剑情缘
* @version V1.0
* @data 20-Oct-2019
* @brief 串口USART2实验
*
* 介绍配置USART2串口收发功能
*
*
*
*/
#include "stm32f10x_usart.h"
#include "stm32f10x.h"
#include
#include "task.h"
/* 加入以下代码,支持printf函数,而不需要选择use MicroLIB */
#ifdef DEBUG
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART2->SR&0X40)==0);
USART2->DR = (u8) ch;
return ch;
}
#endif
uint8_t rxBuf[40];
uint8_t rxLen;
/**
* @brief USART2串口初时化
*
*
* @param uint32_t baudrate[in]:设置波特率
* @retval None
*/
void Usart2_Init(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 初时化串口前,先将相应的时钟打开 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
/* 初时化USART2 TX PA2引脚设为复用推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 初时化USART2 RX PA3引脚为浮空输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);/*!< 复位串口2 */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);/*!< 停止复位 */
/* USART2 NVIC初时化 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /*!< 设置NVIC中断分组2:2位抢占优先级,2位响应优先级 0-3; */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; /*!< USART2中断通道USART1_IRQn */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; /*!< 抢占优先级3 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; /*!< 子优先级3 */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /*!< IRQ使能通道 */
NVIC_Init(&NVIC_InitStructure); /*!< 初时化NVIC寄存器 */
/* USART2初时化设置 */
USART_InitStructure.USART_BaudRate = baudrate; /*!< 设置波特率 */
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*!< 8位数据*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*!< 1位停止位 */
USART_InitStructure.USART_Parity = USART_Parity_No; /*!< 无校验位 */
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*!< 收发模式*/
USART_Init(USART2,&USART_InitStructure); /*!< 初时化串口2 */
/* 开启串口2接收中断 */
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
/* 使能串口2 */
USART_Cmd(USART2,ENABLE);
}
/**
* @brief USART2串口发送函数
*
*
* @param uint8_t *buf[in]:待发送的数据buffer
* @param uint16_t len:发送的数据长度
* @retval None
*/
void USART2_Send_Data(uint8_t *buf,uint16_t len)
{
for(uint16_t i = 0; i= 20)
{
USART2_Send_Data(rxBuf,20);
rxLen = 0;
}
}
}
#if USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
printf("\r\nfile path is %s,error line is %d\r\n",file,line);
while(1);
}
#endif
编译运行
- protues仿真代码下载,注意protues仿真STM32串口会乱码https://download.csdn.net/download/mygod2008ok/12234385