forked from logzhan/RobotHardware-UESTC
163 lines
4.7 KiB
C
163 lines
4.7 KiB
C
|
/*
|
|||
|
********************************************************************************************************
|
|||
|
<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bsp_usart.c
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>https://shop60670850.taobao.com
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD>ߣ<EFBFBD>lilianhe
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: 2017-2-8
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
|
|||
|
/*
|
|||
|
********************************************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><EFBFBD>
|
|||
|
1.2017-2-8<EFBFBD><EFBFBD><EFBFBD><EFBFBD>дuart1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
#include "bsp_usart.h"
|
|||
|
#include <stdio.h>
|
|||
|
|
|||
|
volatile unsigned char *rx_address;
|
|||
|
volatile unsigned int rx_count;
|
|||
|
volatile unsigned int rx_length;
|
|||
|
|
|||
|
/*
|
|||
|
********************************************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>void USART1_Init(u32 bound)
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>PA9----TXD<EFBFBD><EFBFBD>PA10----RXD
|
|||
|
<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD>boundΪ<EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: 2017-2-8
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void USART1_Init(u32 bound)
|
|||
|
{
|
|||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|||
|
USART_InitTypeDef USART_InitStructure;
|
|||
|
|
|||
|
/* config USART1 clock */
|
|||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
|
|||
|
|
|||
|
/* USART1 GPIO config */
|
|||
|
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
|
|||
|
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);
|
|||
|
/* Configure USART1 Rx (PA.10) as input floating */
|
|||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|||
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|||
|
|
|||
|
/* USART1 mode config */
|
|||
|
USART_InitStructure.USART_BaudRate = bound;
|
|||
|
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(USART1, &USART_InitStructure);
|
|||
|
|
|||
|
/* ʹ<>ܴ<EFBFBD><DCB4><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> */
|
|||
|
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
|
|||
|
|
|||
|
USART_Cmd(USART1, ENABLE);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <20>ض<EFBFBD><D8B6><EFBFBD>c<EFBFBD>⺯<EFBFBD><E2BAAF>printf<74><66>USART1
|
|||
|
int fputc(int ch, FILE *f)
|
|||
|
{
|
|||
|
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ݵ<EFBFBD>USART1 */
|
|||
|
USART_SendData(USART1, (uint8_t) ch);
|
|||
|
|
|||
|
/* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
|
|||
|
|
|||
|
return (ch);
|
|||
|
}
|
|||
|
|
|||
|
/// <20>ض<EFBFBD><D8B6><EFBFBD>c<EFBFBD>⺯<EFBFBD><E2BAAF>scanf<6E><66>USART1
|
|||
|
int fgetc(FILE *f)
|
|||
|
{
|
|||
|
/* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
|
|||
|
|
|||
|
return (int)USART_ReceiveData(USART1);
|
|||
|
}
|
|||
|
/*
|
|||
|
********************************************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>void USART1_Send(unsigned char *tx_buf, int len)
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>ͺ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>PA9----TXD<EFBFBD><EFBFBD>PA10----RXD
|
|||
|
<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: 2017-2-8
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void USART1_Send(unsigned char *tx_buf, int len)
|
|||
|
{
|
|||
|
USART_ClearFlag(USART1, USART_FLAG_TC);
|
|||
|
USART_ClearITPendingBit(USART1, USART_FLAG_TXE);
|
|||
|
while(len--)
|
|||
|
{
|
|||
|
USART_SendData(USART1, *tx_buf);
|
|||
|
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) != 1);
|
|||
|
USART_ClearFlag(USART1, USART_FLAG_TC);
|
|||
|
USART_ClearITPendingBit(USART1, USART_FLAG_TXE);
|
|||
|
tx_buf++;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
********************************************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>void USART1_Receive(unsigned char *rx_buf, int len)
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>պ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>PA9----TXD<EFBFBD><EFBFBD>PA10----RXD
|
|||
|
<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: 2017-2-8
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void USART1_Receive(unsigned char *rx_buf, int len)
|
|||
|
{
|
|||
|
rx_count = 0;
|
|||
|
rx_length = len;
|
|||
|
rx_address = rx_buf;
|
|||
|
}
|
|||
|
/*
|
|||
|
********************************************************************************************************
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>void USART1_IRQHandler(void)
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>PA9----TXD<EFBFBD><EFBFBD>PA10----RXD
|
|||
|
<EFBFBD><EFBFBD> ע<EFBFBD><EFBFBD>
|
|||
|
<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: 2017-2-8
|
|||
|
*********************************************************************************************************
|
|||
|
*/
|
|||
|
void USART1_IRQHandler(void)
|
|||
|
{
|
|||
|
unsigned char Res;
|
|||
|
//u8 Res;
|
|||
|
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>(<28><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><EFBFBD><EFBFBD>0x0d 0x0a<30><61>β)
|
|||
|
{
|
|||
|
|
|||
|
Res = USART_ReceiveData(USART1);
|
|||
|
// printf("[USART1_IRQHandler],Rec_data = %x\r\n", Res);
|
|||
|
|
|||
|
if(rx_length > rx_count)
|
|||
|
{
|
|||
|
*rx_address = Res;
|
|||
|
rx_address++;
|
|||
|
rx_count++;
|
|||
|
}
|
|||
|
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/*********************************************END OF FILE**********************/
|