stm32f207 UART驱动
2015-03-07 18:29
696 查看
这两天又调了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);
}
}
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);
}
}
相关文章推荐
- mega328p-ADC,PWM,UART驱动
- S3C2440 UART串口驱动
- Hi3515 UART2和UART3驱动加载
- Linux TTY驱动--Uart_driver底层
- S3C2440 UART串口驱动
- 基于Yocto项目UART驱动开发
- 工业级wifi模块。普通单片机可以驱动。SPI UART SDIO接口。内置协议,天线。
- Linux tty驱动学习 - UART驱动的read操作流程
- Linux设备模型之tty&&uart驱动架构分析
- tty初探—uart驱动框架分析
- linux UART 驱动学习笔记——用户如何操作硬件
- Linux TTY驱动--Uart_driver底层
- 嵌入式linux UART驱动
- pl-2303hx usb-ttl uart usb转串口设备 检测 驱动安装 win8.1 64bit
- 【引用】Linux-2.6.32.2内核在mini2440上的移植(十八)---将UART2更成普通串口驱动
- UART驱动分析
- 基于S3C2440的Linux-3.6.6移植——UART驱动
- UART控制器驱动流程
- KitProg USB-UART驱动 安装失败终结贴
- Zigbee网关 Zstack增加串口功能1-修改底层驱动MT/MT_UART.c