#define USARTz USART2
#define USARTz_GPIO GPIOA
#define USARTz_CLK RCC_APB1_PERIPH_USART2
#define USARTz_GPIO_CLK RCC_APB2_PERIPH_GPIOB
#define USARTz_RxPin GPIO_PIN_3
#define USARTz_TxPin GPIO_PIN_2
#define USARTz_Rx_GPIO_AF GPIO_AF3_USART2
#define USARTz_Tx_GPIO_AF GPIO_AF1_USART2
#define USARTz_APBxClkCmd RCC_EnableAPB1PeriphClk
#define USARTz_IRQn USART1_2_IRQn
#define USARTz_IRQHandler USART1_2_IRQHandler
#define RS485_DIR_RECCEIVE (0)
#define RS485_DIR_TRANSMIT (1)
void bsp_usart2_init(uint32_t baudrate)
{
usart2_rcc_init();
usart2_nvic_config();
usart2_gpio_init();
usart2_config(baudrate);
usart2_set_dir(RS485_DIR_RECCEIVE);
//usart2_set_dir(RS485_DIR_TRANSMIT);
}
void bsp_usart2_send_buf(const uint8_t *buf, uint16_t len)
{
uint16_t i;
for (i = 0; i < len; i++) {
USART_SendData(USART2, (uint8_t)(*(buf + i)));
while (USART_GetFlagStatus(USART2, USART_FLAG_TXC) == RESET);
}
}
void usart2_gpio_init(void)
{
GPIO_InitType GPIO_InitStructure;
/* Initialize GPIO_InitStructure */
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure USART2 Tx as alternate function push-pull */
GPIO_InitStructure.Pin = USARTz_TxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Alternate = USARTz_Tx_GPIO_AF;
GPIO_InitPeripheral(USARTz_GPIO, &GPIO_InitStructure);
/* Configure USART2 Rx as alternate function */
GPIO_InitStructure.Pin = USARTz_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.GPIO_Alternate = USARTz_Rx_GPIO_AF;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
GPIO_InitPeripheral(USARTz_GPIO, &GPIO_InitStructure);
log_func();
}
void usart2_rcc_init(void)
{
/* Enable GPIO clock */
RCC_EnableAPB2PeriphClk(USARTz_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE);
/* Enable USARTy and USARTz Clock */
USARTz_APBxClkCmd(USARTz_CLK, ENABLE);
log_func();
}
void usart2_config(uint32_t baudrate)
{
USART_InitType USART2_InitStructure;
USART_DeInit(USARTz);
/* USART2 configuration ------------------------------------------------------*/
USART2_InitStructure.BaudRate = baudrate;
USART2_InitStructure.WordLength = USART_WL_8B;
USART2_InitStructure.StopBits = USART_STPB_1;
USART2_InitStructure.Parity = USART_PE_NO;
USART2_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART2_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
/* Configure USART2 */
USART_Init(USARTz, &USART2_InitStructure);
/* Enable USART2 Receive and Transmit interrupts */
USART_ConfigInt(USARTz, USART_INT_RXDNE, ENABLE);
//USART_ConfigInt(USARTz, USART_INT_TXDE, ENABLE);
/* Enable the USART2*/
USART_Enable(USARTz, ENABLE);
log_func();
}
void usart2_nvic_config(void)
{
NVIC_InitType NVIC_InitStructure;
/* Enable the USARTz Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USARTz_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
log_func();
}
void usart2_set_dir(uint8_t dir)
{
if (dir == RS485_DIR_TRANSMIT){
GPIO_SetBits(GPIOA, GPIO_PIN_7);
} else {
GPIO_ResetBits(GPIOA, GPIO_PIN_7);
}
}
void USART1_2_IRQHandler(void)
{
log_func();
if (USART_GetFlagStatus(USART1, USART_FLAG_RXDNE) != RESET) {
uint8_t data = USART_ReceiveData(USART1);
log_debug("USART1 data: 0x%02x\r\n", data);
}
if (USART_GetFlagStatus(USART2, USART_FLAG_RXDNE) != RESET) {
uint8_t data = USART_ReceiveData(USART2);
log_debug("USART2 data: 0x%02x\r\n", data);
}
}
上面是我USART2初始化代码,示波器测量接收引脚上的波形和数据解码和我发送数据一致,但是就是无法触发接收中断,开始设置接收引脚IO模式为GPIO_MODE_AF_PP会拉低输入电压,导致引脚数据电压异常,这个模式也是官方给的代码,后自己改成input模式了,有没有大佬能看看上面可能有什么问题导致没法触发接收中断啊 |