用的外设是PA9 PA10
DMA选择的是DMA2_Channel6
代码中还用了DMA传输完成中断,用来配置ADM2483的管脚。
直接上代码了:
void UART_Init(u32 pclk2, u32 bound, u8 checkMode)
{
__GPIOA_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
__GPIOC_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_DMA2_CLK_ENABLE();
MODIFY_REG(USART1->CR1, USART_CR1_UE_Msk, 0); // 失能串口
while (READ_BIT(USART1->CR1, USART_CR1_UE_Msk) != 0);
MODIFY_REG(DMA2_Channel6->CCR, DMA_CCR_EN_Msk, 0); //dis: Channel disable
while(DMA2_Channel6->CCR & DMA_CCR_EN_Msk); //确保 DMA 可以被设置
GPIO_Set(GPIOA, GPIO_PIN_9 | GPIO_PIN_10, GPIO_MODE_AF, GPIO_OTYPE_PP, GPIO_SPEED_FREQ_LOW, GPIO_NOPULL); // PB14设置
GPIO_AF_Set(GPIOA, 9, GPIO_AF7_USART1); // PA9, AF7
GPIO_AF_Set(GPIOA, 10, GPIO_AF7_USART1); // PA10, AF7
u32 USARTDiv = 2 * pclk2 * 1000000 / bound;
USART1->BRR = (USARTDiv & 0xFFFFFFF0) | ((USARTDiv & 0x0F) >> 1);
// MeterConfig.s.rsCheckMode
if (checkMode == checkMode_NONE) {
MODIFY_REG(USART1->CR1, USART_CR1_PCE_Msk, 0); // 0: Parity control disabled
MODIFY_REG(USART1->CR1, USART_CR1_M_Msk, 0); // M[1:0] = 00: 1 Start bit, 8 data bits, n stop bits
} else if