__INLINE void Configure_GPIO_USART2(void)
{
/* Enable the peripheral clock of GPIOA */
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
/* GPIO configuration for USART1 signals */
/* (1) Select AF mode (10) on PA2 and PA3 */
/* (2) AF4 for USART2 signals */
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODE2|GPIO_MODER_MODE3))\
| (GPIO_MODER_MODE2_1 | GPIO_MODER_MODE3_1); /* (1) */
GPIOA->AFR[0] = (GPIOA->AFR[0] &~ (0x0000FF00))\
| (4 << (2 * 4)) | (4 << (3 * 4)); /* (2) */
}
/**
* Brief This function configures USART2.
* Param None
* Retval None
* add by ljy 171002
*/
__INLINE void Configure_USART2(void)
{
/* Enable the peripheral clock USART1 */
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
/* Configure USART2 */
/* (1) oversampling by 16, 9600 baud */
/* (2) Enable DMA in reception and transmission */
/* (3) 8 data bit, 1 start bit, 1 stop bit, no parity, reception and transmission enabled */
USART2->BRR = 160000 / 96; /* (1) */
//USART2->CR3 = USART_CR3_DMAT | USART_CR3_DMAR; /* (2) */ 暂时不使用DMA
USART2->CR1 = USART_CR1_TE | USART_CR1_RXNEIE| USART_CR1_RE | USART_CR1_UE; /* (3) */
while((USART2->ISR & USART_ISR_TC) != USART_ISR_TC)/* polling idle frame Transmission */
{
/* add time out here for a robust application */
}
USART2->ICR = USART_ICR_TCCF;/* Clear TC flag */
// interrupt mode 1002 ljy
USART2->CR1 |= USART_CR1_TCIE;/* enable TC interrupt */
/* Configure IT */
/* (4) Set priority for USART1_IRQn */
/* (5) Enable USART1_IRQn */
NVIC_SetPriority(USART2_IRQn, 0); /* (4) */
NVIC_EnableIRQ(USART2_IRQn); /* (5) */
}
以上是基于STM32L05X系统寄存器库直接配置USART2,通过测试发现没有数据发出,也没有数据接收,大家帮看下配置哪里有问题。 |