标准库
#include "stm32f10x.h"
#include"XCOSnTh.h"
#include "stdio.h"
XCDefinieFIFO(cmd_fifo,512);//定义一个512字节接收的FIFO缓冲区
RegisterItemDef(Root, CMD_FIFO_POP, "CMD Rx", cmd_fifo_pop);
void USART1_IRQHandler(void)
{//串口接收中断
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
uint16_t temp = USART_ReceiveData(USART1); //读取串口接收到的数据
cmd_fifo_push(temp); //将串口接收到的数据存入到FIFO
}
}
static int PrintPutC(int ch)
{
USART_SendData(USART1, ch);//发送一个字节的数据
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //等待串口发送完成
return ch;
}
RegisterItemDef(Root, CMD_PutC, "CMD Tx", PrintPutC);
int fputc(int ch, FILE *f)//串口函数重定向
{
ch=PrintPutC(ch);
return ch;
}
static int CmdProcess_AdaptaionInit(CmdObj obj, char* str, int len)
{
{//【串口配置】
{//1、串口的GPIO配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA的时钟
GPIO_InitTypeDef GPIO_InitStructure;
//PA9:Tx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化PA9:Tx
//PA10:Rx
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化PA10:Rx
}
{//2、串口配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能串口时钟
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200; // 波特率:115200
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 流控:无
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // 模式:发送和接收模式
USART_InitStructure.USART_Parity = USART_Parity_No; // 校验:无
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位:1位
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位宽度:8位
USART_Init(USART1, &USART_InitStructure);//串口初始化
{//3、串口的接收中断配置
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
}
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //打开串口接收中断
}
}
obj->printf=printf; //挂载printf
obj->printf("\r\nXShell Serial init ok");
return 0;
}
CmdDef(CmdPortInit, 0, CmdProcess_AdaptaionInit, "[systemCMD]");