STM32F0xx芯片的USART配置
备份一下关于UASRT的相关函数,USART1的寄存器用法:
/* USART1 init function */ static void MX_USART1_UART_Init(void) { /* Enable the peripheral clock of GPIOA */ RCC->AHBENR |= RCC_AHBENR_GPIOAEN; /* GPIO configuration for USART1 signals */ /* (1) Select AF mode (10) on PA9 and PA10 */ /* (2) AF1 for USART1 signals */ GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER9|GPIO_MODER_MODER10))\ | (GPIO_MODER_MODER9_1 | GPIO_MODER_MODER10_1); /* (1) */ GPIOA->AFR[1] = (GPIOA->AFR[1] &~ (GPIO_AFRH_AFRH1 | GPIO_AFRH_AFRH2))\ | (1 << (1 * 4)) | (1 << (2 * 4)); /* (2) */ /* Enable the peripheral clock USART1 */ RCC->APB2ENR |= RCC_APB2ENR_USART1EN; /* Configure USART1 */ /* (1) oversampling by 16, 38400 baud */ /* (2) 8 data bit, 1 start bit, 1 stop bit, no parity, reception mode */ USART1->BRR = 48000000 / 38400; USART1->CR1 = USART_CR1_RXNEIE | USART_CR1_RE | USART_CR1_TE | USART_CR1_UE; /* (2) */ /* Configure IT */ /* (3) Set priority for USART1_IRQn */ /* (4) Enable USART1_IRQn */ NVIC_SetPriority(USART1_IRQn, 3); /* (3) */ //NVIC_EnableIRQ(USART1_IRQn); /* (4) */ }
LL库的函数用法:
/* USART1 init function */ static void MX_USART1_UART_Init(void) { LL_USART_InitTypeDef USART_InitStruct; LL_GPIO_InitTypeDef GPIO_InitStruct; /* Peripheral clock enable */ LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_USART1); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = COM_TX_Pin; GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; GPIO_InitStruct.Alternate = LL_GPIO_AF_1; LL_GPIO_Init(COM_TX_GPIO_Port, &GPIO_InitStruct); GPIO_InitStruct.Pin = COM_RX_Pin; GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; GPIO_InitStruct.Pull = LL_GPIO_PULL_UP; GPIO_InitStruct.Alternate = LL_GPIO_AF_1; LL_GPIO_Init(COM_RX_GPIO_Port, &GPIO_InitStruct); USART_InitStruct.BaudRate = 38400; USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B; USART_InitStruct.StopBits = LL_USART_STOPBITS_1; USART_InitStruct.Parity = LL_USART_PARITY_NONE; USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX; USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE; USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16; LL_USART_Init(USART1, &USART_InitStruct); LL_USART_DisableIT_CTS(USART1); LL_USART_DisableOverrunDetect(USART1); LL_USART_ConfigAsyncMode(USART1); LL_USART_Enable(USART1); }
中断函数,数据存到队列中:
/* Irq */ void USART1_IRQHandler(void) { static uint8 tmp; if((USART1->ISR & USART_ISR_PE) == USART_ISR_PE) { tmp = (uint8)(USART1->RDR); USART1->ICR |= USART_ICR_PECF; } if((USART1->ISR & USART_ISR_FE) == USART_ISR_FE) { tmp = (uint8)(USART1->RDR); USART1->ICR |= USART_ICR_FECF; } if((USART1->ISR & USART_ISR_ORE) == USART_ISR_ORE) { tmp = (uint8)(USART1->RDR); USART1->ICR |= USART_ICR_ORECF; } if((USART1->ISR & USART_ISR_RXNE) == USART_ISR_RXNE) { tmp = (uint8_t)(USART1->RDR); xQueueSendToBackFromISR(xQueueRxFromHost, &tmp, NULL); } }