这两天又调了F207的uart接口,把板上的uart5做成打印和输入处理接口。
uart5.c:
#include "stm32f2xx.h"
#include <stdio.h>
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
void UART5_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);//USART1,6ÓÃRCC_APB2PeriphClockCmd
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(UART5, &USART_InitStructure);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //½ÓÊÕÖжÏʹÄÜ
USART_Cmd(UART5, ENABLE);
}
void UART5_SendByte(unsigned short Data)
{
while (!(UART5->SR & USART_FLAG_TXE));
UART5->DR = (Data & (uint16_t)0x01FF);
}
void UART5_Write(u8* data, unsigned short len)
{
unsigned short i;
for (i=0; i<len; i++)
{
UART5_SendByte(data[i]);
}
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(UART5, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET)
{}
return ch;
}
stm32f2xx_it.c中的中断处理,做了个echo功能:
void UART5_IRQHandler(void)
{
if(USART_GetITStatus(EVAL_COM1, USART_IT_RXNE) != RESET)
{
unsigned char tmp = USART_ReceiveData(EVAL_COM1);
USART_SendData(EVAL_COM1, tmp);
}
}