/** @brief: set registers acc to info in huart * @details: used in HAL_UART3Debug_Init, private ****************************************************************/ static void MyUARTSetConfig(UART_HandleTypeDef *huart) { uint32_t tmpreg = 0x00; /* Check the parameters */ assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate)); assert_param(IS_UART_STOPBITS(huart->Init.StopBits)); assert_param(IS_UART_PARITY(huart->Init.Parity)); assert_param(IS_UART_MODE(huart->Init.Mode)); /*-------------------------- USART CR2 Configuration -----------------------*/ tmpreg = huart->Instance->CR2; /* Clear STOP[13:12] bits */ tmpreg &= (uint32_t) ~((uint32_t) USART_CR2_STOP); /* Configure the UART Stop Bits: Set STOP[13:12] bits according to huart->Init.StopBits value */ tmpreg |= (uint32_t) huart->Init.StopBits; /* Write to USART CR2 */ huart->Instance->CR2 = (uint32_t) tmpreg; /*-------------------------- USART CR1 Configuration -----------------------*/ tmpreg = huart->Instance->CR1; /* Clear M, PCE, PS, TE and RE bits */ tmpreg &= (uint32_t) ~((uint32_t) (USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)); /* Configure the UART Word Length, Parity and mode: Set the M bits according to huart->Init.WordLength value Set PCE and PS bits according to huart->Init.Parity value Set TE and RE bits according to huart->Init.Mode value Set OVER8 bit according to huart->Init.OverSampling value */ tmpreg |= (uint32_t) huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling; /* Write to USART CR1 */ huart->Instance->CR1 = (uint32_t) tmpreg; /*-------------------------- USART CR3 Configuration -----------------------*/ tmpreg = huart->Instance->CR3; /* Clear CTSE and RTSE bits */ tmpreg &= (uint32_t) ~((uint32_t) (USART_CR3_RTSE | USART_CR3_CTSE)); /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */ tmpreg |= huart->Init.HwFlowCtl; /* Write to USART CR3 */ huart->Instance->CR3 = (uint32_t) tmpreg; /* Check the Over Sampling */ if (huart->Init.OverSampling == UART_OVERSAMPLING_8) { /*-------------------------- USART BRR Configuration ---------------------*/ if ((huart->Instance == USART1) || (huart->Instance == USART6)) { huart->Instance->BRR = UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate); } else { huart->Instance->BRR = UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate); } } else { /*-------------------------- USART BRR Configuration ---------------------*/ if ((huart->Instance == USART1) || (huart->Instance == USART6)) { huart->Instance->BRR = UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate); } else { huart->Instance->BRR = UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate); } } }
int hal_uart_config(int port, int32_t baudrate, uint8_t databits, uint8_t stopbits, enum hal_uart_parity parity, enum hal_uart_flow_ctl flow_ctl) { struct hal_uart *u; const struct stm32f3_uart_cfg *cfg; uint32_t cr1, cr2, cr3; if (port >= UART_CNT) { return -1; } u = &uarts[port]; if (u->u_open) { return -1; } cfg = u->u_cfg; assert(cfg); /* * RCC * pin config * UART config * nvic config * enable uart */ cr1 = cfg->suc_uart->CR1; cr2 = cfg->suc_uart->CR2; cr3 = cfg->suc_uart->CR3; cr1 &= ~(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_RE | USART_CR1_OVER8); cr2 &= ~(USART_CR2_STOP); cr3 &= ~(USART_CR3_RTSE | USART_CR3_CTSE); switch (databits) { case 8: cr1 |= UART_WORDLENGTH_8B; break; case 9: cr1 |= UART_WORDLENGTH_9B; break; default: assert(0); return -1; } switch (stopbits) { case 1: cr2 |= UART_STOPBITS_1; break; case 2: cr2 |= UART_STOPBITS_2; break; default: return -1; } switch (parity) { case HAL_UART_PARITY_NONE: cr1 |= UART_PARITY_NONE; break; case HAL_UART_PARITY_ODD: cr1 |= UART_PARITY_ODD; break; case HAL_UART_PARITY_EVEN: cr1 |= UART_PARITY_EVEN; break; } switch (flow_ctl) { case HAL_UART_FLOW_CTL_NONE: cr3 |= UART_HWCONTROL_NONE; break; case HAL_UART_FLOW_CTL_RTS_CTS: cr3 |= UART_HWCONTROL_RTS_CTS; if (cfg->suc_pin_rts < 0 || cfg->suc_pin_cts < 0) { /* * Can't turn on HW flow control if pins to do that are not * defined. */ assert(0); return -1; } break; } cr1 |= (UART_MODE_RX | UART_MODE_TX | UART_OVERSAMPLING_16); *cfg->suc_rcc_reg |= cfg->suc_rcc_dev; hal_gpio_init_af(cfg->suc_pin_tx, cfg->suc_pin_af, 0, 0); hal_gpio_init_af(cfg->suc_pin_rx, cfg->suc_pin_af, 0, 0); if (flow_ctl == HAL_UART_FLOW_CTL_RTS_CTS) { hal_gpio_init_af(cfg->suc_pin_rts, cfg->suc_pin_af, 0, 0); hal_gpio_init_af(cfg->suc_pin_cts, cfg->suc_pin_af, 0, 0); } u->u_regs = cfg->suc_uart; u->u_regs->CR3 = cr3; u->u_regs->CR2 = cr2; u->u_regs->CR1 = cr1; if (cfg->suc_uart == USART1) { u->u_regs->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), baudrate)); } else { u->u_regs->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), baudrate)); } (void)u->u_regs->RDR; (void)u->u_regs->ISR; hal_uart_set_nvic(cfg->suc_irqn, u); u->u_regs->CR1 |= (USART_CR1_RXNEIE | USART_CR1_UE); u->u_open = 1; return 0; }