void uart4_init(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USART Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_8_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0; /* 最高优先级*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Configure USART Tx as alternate function push-pull */
vGpioInit( RCC_AHBPeriph_GPIOA,
GPIOA,
GPIO_Pin_0,
GPIO_Mode_AF,
GPIO_Speed_50MHz,
GPIO_OType_PP,
GPIO_PuPd_UP);
/* Configure USART Rx as alternate function push-pull */
vGpioInit( RCC_AHBPeriph_GPIOA,
GPIOA,
GPIO_Pin_1,
GPIO_Mode_AF,
GPIO_Speed_50MHz,
GPIO_OType_OD,
GPIO_PuPd_UP);
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_4);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_4);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART4, ENABLE);
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 configuration */
USART_Init(USART4, &USART_InitStructure);
USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
/* Enable USART */
USART_Cmd(USART4, ENABLE);
}
bool uart4_send_byte(UINT8 data)
{
UINT16 UartTimeOutCount=0xFFFF;
/* Loop until transmit data register is empty */
while (USART_GetFlagStatus(USART4, USART_FLAG_TXE) == RESET)
{
UartTimeOutCount--;
if(UartTimeOutCount == 0)
{
//break;
return FALSE;
}
}
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART4, (UINT8 ) data);
return TRUE;
}
void uart4_send_string(UINT8 *data, UINT16 len)
{
UINT16 i;
for(i=0;i<len;i++)
{
bt_uart_send_byte((UINT8 )(*(data+i)));
}
}
void USART3_8_IRQHandler(void)
{
if(USART_GetITStatus(UART_BT, USART_IT_RXNE) != RESET)
{
uart4_send_byte(USART_ReceiveData(USART4));
}
}
|