void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==UART4) { /* USER CODE BEGIN UART4_MspInit 0 */ /* USER CODE END UART4_MspInit 0 */ /* Peripheral clock enable */ __UART4_CLK_ENABLE(); /**UART4 GPIO Configuration PA0-WKUP ------> UART4_TX PA1 ------> UART4_RX */ GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF8_UART4; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(UART4_IRQn, 0, 0); HAL_NVIC_EnableIRQ(UART4_IRQn); /* USER CODE BEGIN UART4_MspInit 1 */ /* USER CODE END UART4_MspInit 1 */ } }
//****************************************************************************** // board_uart_enable_clock // // Ensures that the specified UART's clock is turned on. //****************************************************************************** int board_uart_enable_clock (int module_id) { //-------------------------------------------------------- // Turn on clock for the associated SPIx module. //-------------------------------------------------------- switch (module_id) { case 0: // USARTMD / VCP __USART2_CLK_ENABLE(); break; case 1: // USART1 __USART1_CLK_ENABLE(); break; case 2: // USART2 __USART2_CLK_ENABLE(); break; #if defined(STM32F446xx) case 3: // USART3 __USART3_CLK_ENABLE(); break; case 4: // UART4 __UART4_CLK_ENABLE(); break; case 5: // UART5 __UART5_CLK_ENABLE(); break; #endif case 6: // USART6 __USART6_CLK_ENABLE(); break; } return (0); // denote everything worked OK }
/** * @brief Enables the RS232 interface with the current settings * @param None * @retval None */ static void prvEnableRs232Interface() { /* Enable UART clock */ __UART4_CLK_ENABLE(); /* Configure priority and enable interrupt */ HAL_NVIC_SetPriority(UART4_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0); HAL_NVIC_EnableIRQ(UART4_IRQn); /* Enable the UART */ HAL_UART_Init(&UART_Handle); /* If we are in RX mode we should start receiving data */ if (UART_Handle.Init.Mode == UARTMode_RX || UART_Handle.Init.Mode == UARTMode_TX_RX) HAL_UART_Receive_IT(&UART_Handle, &prvReceivedByte, 1); }
/** * @brief Initializes the Global MSP. * @param None * @retval None */ void HAL_MspInit(void) { /* NOTE : This function is generated automatically by MicroXplorer and eventually modified by the user */ /* Enable UART 4 clock */ __UART4_CLK_ENABLE(); /* RNG Periph clock enable */ __RNG_CLK_ENABLE(); #ifdef USE_STM32F4XX_HW_CRYPTO /* Enable CRYP clock */ __CRYP_CLK_ENABLE(); /* Enable Hash clock */ __HASH_CLK_ENABLE(); #endif }
void serial_init(serial_t *obj, PinName tx, PinName rx) { // Determine the UART to use (UART_1, UART_2, ...) UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); MBED_ASSERT(obj->uart != (UARTName)NC); // Enable USART clock switch (obj->uart) { case UART_1: __USART1_CLK_ENABLE(); obj->index = 0; break; case UART_2: __USART2_CLK_ENABLE(); obj->index = 1; break; case UART_3: __USART3_CLK_ENABLE(); obj->index = 2; break; case UART_4: __UART4_CLK_ENABLE(); obj->index = 3; break; case UART_5: __UART5_CLK_ENABLE(); obj->index = 4; break; case UART_6: __USART6_CLK_ENABLE(); obj->index = 5; break; } // Configure the UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); if (tx != NC) { pin_mode(tx, PullUp); } if (rx != NC) { pin_mode(rx, PullUp); } // Configure UART obj->baudrate = 9600; obj->databits = UART_WORDLENGTH_8B; obj->stopbits = UART_STOPBITS_1; obj->parity = UART_PARITY_NONE; obj->pin_tx = tx; obj->pin_rx = rx; init_uart(obj); // For stdio management if (obj->uart == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
// assumes Init parameters have been set up correctly STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { USART_TypeDef *UARTx; IRQn_Type irqn; uint32_t GPIO_Pin, GPIO_Pin2 = 0; uint8_t GPIO_AF_UARTx = 0; GPIO_TypeDef* GPIO_Port = NULL; GPIO_TypeDef* GPIO_Port2 = NULL; switch (uart_obj->uart_id) { #if defined(MICROPY_HW_UART1_PORT) && defined(MICROPY_HW_UART1_PINS) // USART1 is on PA9/PA10 (CK on PA8), PB6/PB7 case PYB_UART_1: UARTx = USART1; irqn = USART1_IRQn; GPIO_AF_UARTx = GPIO_AF7_USART1; GPIO_Port = MICROPY_HW_UART1_PORT; GPIO_Pin = MICROPY_HW_UART1_PINS; __USART1_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART1_TX_PORT) && \ defined(MICROPY_HW_UART1_TX_PIN) && \ defined(MICROPY_HW_UART1_RX_PORT) && \ defined(MICROPY_HW_UART1_RX_PIN) case PYB_UART_1: UARTx = USART1; irqn = USART1_IRQn; GPIO_AF_UARTx = GPIO_AF7_USART1; GPIO_Port = MICROPY_HW_UART1_TX_PORT; GPIO_Pin = MICROPY_HW_UART1_TX_PIN; GPIO_Port2 = MICROPY_HW_UART1_RX_PORT; GPIO_Pin2 = MICROPY_HW_UART1_RX_PIN; __USART1_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART2_PORT) && defined(MICROPY_HW_UART2_PINS) // USART2 is on PA2/PA3 (CTS,RTS,CK on PA0,PA1,PA4), PD5/PD6 (CK on PD7) case PYB_UART_2: UARTx = USART2; irqn = USART2_IRQn; GPIO_AF_UARTx = GPIO_AF7_USART2; GPIO_Port = MICROPY_HW_UART2_PORT; GPIO_Pin = MICROPY_HW_UART2_PINS; #if defined(MICROPY_HW_UART2_RTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { GPIO_Pin |= MICROPY_HW_UART2_RTS; } #endif #if defined(MICROPY_HW_UART2_CTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { GPIO_Pin |= MICROPY_HW_UART2_CTS; } #endif __USART2_CLK_ENABLE(); break; #endif #if defined(USART3) && defined(MICROPY_HW_UART3_PORT) && defined(MICROPY_HW_UART3_PINS) // USART3 is on PB10/PB11 (CK,CTS,RTS on PB12,PB13,PB14), PC10/PC11 (CK on PC12), PD8/PD9 (CK on PD10) case PYB_UART_3: UARTx = USART3; irqn = USART3_IRQn; GPIO_AF_UARTx = GPIO_AF7_USART3; GPIO_Port = MICROPY_HW_UART3_PORT; GPIO_Pin = MICROPY_HW_UART3_PINS; #if defined(MICROPY_HW_UART3_RTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { GPIO_Pin |= MICROPY_HW_UART3_RTS; } #endif #if defined(MICROPY_HW_UART3_CTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { GPIO_Pin |= MICROPY_HW_UART3_CTS; } #endif __USART3_CLK_ENABLE(); break; #endif #if defined(UART4) && defined(MICROPY_HW_UART4_PORT) && defined(MICROPY_HW_UART4_PINS) // UART4 is on PA0/PA1, PC10/PC11 case PYB_UART_4: UARTx = UART4; irqn = UART4_IRQn; GPIO_AF_UARTx = GPIO_AF8_UART4; GPIO_Port = MICROPY_HW_UART4_PORT; GPIO_Pin = MICROPY_HW_UART4_PINS; __UART4_CLK_ENABLE(); break; #endif #if defined(UART5) && \ defined(MICROPY_HW_UART5_TX_PORT) && \ defined(MICROPY_HW_UART5_TX_PIN) && \ defined(MICROPY_HW_UART5_RX_PORT) && \ defined(MICROPY_HW_UART5_RX_PIN) case PYB_UART_5: UARTx = UART5; irqn = UART5_IRQn; GPIO_AF_UARTx = GPIO_AF8_UART5; GPIO_Port = MICROPY_HW_UART5_TX_PORT; GPIO_Port2 = MICROPY_HW_UART5_RX_PORT; GPIO_Pin = MICROPY_HW_UART5_TX_PIN; GPIO_Pin2 = MICROPY_HW_UART5_RX_PIN; __UART5_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART6_PORT) && defined(MICROPY_HW_UART6_PINS) // USART6 is on PC6/PC7 (CK on PC8) case PYB_UART_6: UARTx = USART6; irqn = USART6_IRQn; GPIO_AF_UARTx = GPIO_AF8_USART6; GPIO_Port = MICROPY_HW_UART6_PORT; GPIO_Pin = MICROPY_HW_UART6_PINS; __USART6_CLK_ENABLE(); break; #endif default: // UART does not exist or is not configured for this board return false; } uart_obj->irqn = irqn; uart_obj->uart.Instance = UARTx; // init GPIO mp_hal_gpio_clock_enable(GPIO_Port); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = GPIO_Pin; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Alternate = GPIO_AF_UARTx; HAL_GPIO_Init(GPIO_Port, &GPIO_InitStructure); // init GPIO for second pin if needed if (GPIO_Port2 != NULL) { mp_hal_gpio_clock_enable(GPIO_Port2); GPIO_InitStructure.Pin = GPIO_Pin2; HAL_GPIO_Init(GPIO_Port2, &GPIO_InitStructure); } // init UARTx HAL_UART_Init(&uart_obj->uart); uart_obj->is_enabled = true; return true; }
// assumes Init parameters have been set up correctly STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { USART_TypeDef *UARTx; IRQn_Type irqn; int uart_unit; const pin_obj_t *pins[4] = {0}; switch (uart_obj->uart_id) { #if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX) case PYB_UART_1: uart_unit = 1; UARTx = USART1; irqn = USART1_IRQn; pins[0] = &MICROPY_HW_UART1_TX; pins[1] = &MICROPY_HW_UART1_RX; __USART1_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX) case PYB_UART_2: uart_unit = 2; UARTx = USART2; irqn = USART2_IRQn; pins[0] = &MICROPY_HW_UART2_TX; pins[1] = &MICROPY_HW_UART2_RX; #if defined(MICROPY_HW_UART2_RTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { pins[2] = &MICROPY_HW_UART2_RTS; } #endif #if defined(MICROPY_HW_UART2_CTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { pins[3] = &MICROPY_HW_UART2_CTS; } #endif __USART2_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX) case PYB_UART_3: uart_unit = 3; UARTx = USART3; irqn = USART3_IRQn; pins[0] = &MICROPY_HW_UART3_TX; pins[1] = &MICROPY_HW_UART3_RX; #if defined(MICROPY_HW_UART3_RTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { pins[2] = &MICROPY_HW_UART3_RTS; } #endif #if defined(MICROPY_HW_UART3_CTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { pins[3] = &MICROPY_HW_UART3_CTS; } #endif __USART3_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX) case PYB_UART_4: uart_unit = 4; UARTx = UART4; irqn = UART4_IRQn; pins[0] = &MICROPY_HW_UART4_TX; pins[1] = &MICROPY_HW_UART4_RX; __UART4_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX) case PYB_UART_5: uart_unit = 5; UARTx = UART5; irqn = UART5_IRQn; pins[0] = &MICROPY_HW_UART5_TX; pins[1] = &MICROPY_HW_UART5_RX; __UART5_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX) case PYB_UART_6: uart_unit = 6; UARTx = USART6; irqn = USART6_IRQn; pins[0] = &MICROPY_HW_UART6_TX; pins[1] = &MICROPY_HW_UART6_RX; #if defined(MICROPY_HW_UART6_RTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { pins[2] = &MICROPY_HW_UART6_RTS; } #endif #if defined(MICROPY_HW_UART6_CTS) if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { pins[3] = &MICROPY_HW_UART6_CTS; } #endif __USART6_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX) case PYB_UART_7: uart_unit = 7; UARTx = UART7; irqn = UART7_IRQn; pins[0] = &MICROPY_HW_UART7_TX; pins[1] = &MICROPY_HW_UART7_RX; __UART7_CLK_ENABLE(); break; #endif #if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX) case PYB_UART_8: uart_unit = 8; UARTx = UART8; irqn = UART8_IRQn; pins[0] = &MICROPY_HW_UART8_TX; pins[1] = &MICROPY_HW_UART8_RX; __UART8_CLK_ENABLE(); break; #endif default: // UART does not exist or is not configured for this board return false; } uint32_t mode = MP_HAL_PIN_MODE_ALT; uint32_t pull = MP_HAL_PIN_PULL_UP; for (uint i = 0; i < 4; i++) { if (pins[i] != NULL) { bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_UART, uart_unit); if (!ret) { return false; } } } uart_obj->irqn = irqn; uart_obj->uart.Instance = UARTx; // init UARTx HAL_UART_Init(&uart_obj->uart); uart_obj->is_enabled = true; return true; }
// assumes Init parameters have been set up correctly STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) { USART_TypeDef *UARTx; IRQn_Type irqn; uint32_t GPIO_Pin; uint8_t GPIO_AF_UARTx = 0; GPIO_TypeDef* GPIO_Port = NULL; switch (uart_obj->uart_id) { // USART1 is on PA9/PA10 (CK on PA8), PB6/PB7 case PYB_UART_1: UARTx = USART1; irqn = USART1_IRQn; GPIO_AF_UARTx = GPIO_AF7_USART1; #if defined (PYBV4) || defined(PYBV10) GPIO_Port = GPIOB; GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7; #else GPIO_Port = GPIOA; GPIO_Pin = GPIO_PIN_9 | GPIO_PIN_10; #endif __USART1_CLK_ENABLE(); break; // USART2 is on PA2/PA3 (CTS,RTS,CK on PA0,PA1,PA4), PD5/PD6 (CK on PD7) case PYB_UART_2: UARTx = USART2; irqn = USART2_IRQn; GPIO_AF_UARTx = GPIO_AF7_USART2; GPIO_Port = GPIOA; GPIO_Pin = GPIO_PIN_2 | GPIO_PIN_3; if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { GPIO_Pin |= GPIO_PIN_1; } if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { GPIO_Pin |= GPIO_PIN_0; } __USART2_CLK_ENABLE(); break; // USART3 is on PB10/PB11 (CK,CTS,RTS on PB12,PB13,PB14), PC10/PC11 (CK on PC12), PD8/PD9 (CK on PD10) case PYB_UART_3: UARTx = USART3; irqn = USART3_IRQn; GPIO_AF_UARTx = GPIO_AF7_USART3; #if defined(PYBV3) || defined(PYBV4) | defined(PYBV10) GPIO_Port = GPIOB; GPIO_Pin = GPIO_PIN_10 | GPIO_PIN_11; if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) { GPIO_Pin |= GPIO_PIN_14; } if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) { GPIO_Pin |= GPIO_PIN_13; } #else GPIO_Port = GPIOD; GPIO_Pin = GPIO_PIN_8 | GPIO_PIN_9; #endif __USART3_CLK_ENABLE(); break; // UART4 is on PA0/PA1, PC10/PC11 case PYB_UART_4: UARTx = UART4; irqn = UART4_IRQn; GPIO_AF_UARTx = GPIO_AF8_UART4; GPIO_Port = GPIOA; GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1; __UART4_CLK_ENABLE(); break; // USART6 is on PC6/PC7 (CK on PC8) case PYB_UART_6: UARTx = USART6; irqn = USART6_IRQn; GPIO_AF_UARTx = GPIO_AF8_USART6; GPIO_Port = GPIOC; GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7; __USART6_CLK_ENABLE(); break; default: return false; } uart_obj->irqn = irqn; uart_obj->uart.Instance = UARTx; // init GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = GPIO_Pin; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Alternate = GPIO_AF_UARTx; HAL_GPIO_Init(GPIO_Port, &GPIO_InitStructure); // init UARTx HAL_UART_Init(&uart_obj->uart); uart_obj->is_enabled = true; return true; }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==UART4) { /* USER CODE BEGIN UART4_MspInit 0 */ /* USER CODE END UART4_MspInit 0 */ /* Peripheral clock enable */ __UART4_CLK_ENABLE(); /**UART4 GPIO Configuration PC10 ------> UART4_TX PC11 ------> UART4_RX */ GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF5_UART4; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* USER CODE BEGIN UART4_MspInit 1 */ /* USER CODE END UART4_MspInit 1 */ } else if(huart->Instance==UART5) { /* USER CODE BEGIN UART5_MspInit 0 */ /* USER CODE END UART5_MspInit 0 */ /* Peripheral clock enable */ __UART5_CLK_ENABLE(); /**UART5 GPIO Configuration PC12 ------> UART5_TX PD2 ------> UART5_RX */ GPIO_InitStruct.Pin = GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF5_UART5; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF5_UART5; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /* USER CODE BEGIN UART5_MspInit 1 */ /* USER CODE END UART5_MspInit 1 */ } else if(huart->Instance==USART2) { /* USER CODE BEGIN USART2_MspInit 0 */ /* USER CODE END USART2_MspInit 0 */ /* Peripheral clock enable */ __USART2_CLK_ENABLE(); /**USART2 GPIO Configuration PA2 ------> USART2_TX PA3 ------> USART2_RX */ GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ } else if(huart->Instance==USART3) { /* USER CODE BEGIN USART3_MspInit 0 */ /* USER CODE END USART3_MspInit 0 */ /* Peripheral clock enable */ __USART3_CLK_ENABLE(); /**USART3 GPIO Configuration PE15 ------> USART3_RX PB10 ------> USART3_TX */ GPIO_InitStruct.Pin = GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART3; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART3; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN USART3_MspInit 1 */ /* USER CODE END USART3_MspInit 1 */ } }
void serial_init(serial_t *obj, PinName tx, PinName rx) { // Determine the UART to use (UART_1, UART_2, ...) UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); MBED_ASSERT(obj->uart != (UARTName)NC); // Enable UART clock if (obj->uart == UART_1) { __HAL_RCC_USART1_FORCE_RESET(); __HAL_RCC_USART1_RELEASE_RESET(); __USART1_CLK_ENABLE(); obj->index = 0; } if (obj->uart == UART_2) { __HAL_RCC_USART2_FORCE_RESET(); __HAL_RCC_USART2_RELEASE_RESET(); __USART2_CLK_ENABLE(); obj->index = 1; } if (obj->uart == UART_3) { __HAL_RCC_USART3_FORCE_RESET(); __HAL_RCC_USART3_RELEASE_RESET(); __USART3_CLK_ENABLE(); obj->index = 2; } #if defined(UART4_BASE) if (obj->uart == UART_4) { __HAL_RCC_UART4_FORCE_RESET(); __HAL_RCC_UART4_RELEASE_RESET(); __UART4_CLK_ENABLE(); obj->index = 3; } #endif #if defined(UART5_BASE) if (obj->uart == UART_5) { __HAL_RCC_UART5_FORCE_RESET(); __HAL_RCC_UART5_RELEASE_RESET(); __UART5_CLK_ENABLE(); obj->index = 4; } #endif // Configure the UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); if (tx != NC) { pin_mode(tx, PullUp); } if (rx != NC) { pin_mode(rx, PullUp); } // Configure UART obj->baudrate = 9600; obj->databits = UART_WORDLENGTH_8B; obj->stopbits = UART_STOPBITS_1; obj->parity = UART_PARITY_NONE; obj->pin_tx = tx; obj->pin_rx = rx; init_uart(obj); // For stdio management if (obj->uart == STDIO_UART) { stdio_uart_inited = 1; memcpy(&stdio_uart, obj, sizeof(serial_t)); } }
// assumes Init parameters have been set up correctly bool uart_init2(pyb_uart_obj_t *uart_obj) { #if 0 USART_TypeDef *UARTx = NULL; uint32_t GPIO_Pin = 0; uint8_t GPIO_AF_UARTx = 0; GPIO_TypeDef* GPIO_Port = NULL; switch (uart_obj->uart_id) { // USART1 is on PA9/PA10 (CK on PA8), PB6/PB7 case PYB_UART_1: UARTx = USART1; GPIO_AF_UARTx = GPIO_AF7_USART1; #if defined (PYBV4) || defined(PYBV10) GPIO_Port = GPIOB; GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7; #else GPIO_Port = GPIOA; GPIO_Pin = GPIO_PIN_9 | GPIO_PIN_10; #endif __USART1_CLK_ENABLE(); break; // USART2 is on PA2/PA3 (CK on PA4), PD5/PD6 (CK on PD7) case PYB_UART_2: UARTx = USART2; GPIO_AF_UARTx = GPIO_AF7_USART2; GPIO_Port = GPIOA; GPIO_Pin = GPIO_PIN_2 | GPIO_PIN_3; __USART2_CLK_ENABLE(); break; // USART3 is on PB10/PB11 (CK on PB12), PC10/PC11 (CK on PC12), PD8/PD9 (CK on PD10) case PYB_UART_3: UARTx = USART3; GPIO_AF_UARTx = GPIO_AF7_USART3; #if defined(PYBV3) || defined(PYBV4) | defined(PYBV10) GPIO_Port = GPIOB; GPIO_Pin = GPIO_PIN_10 | GPIO_PIN_11; #else GPIO_Port = GPIOD; GPIO_Pin = GPIO_PIN_8 | GPIO_PIN_9; #endif __USART3_CLK_ENABLE(); break; // UART4 is on PA0/PA1, PC10/PC11 case PYB_UART_4: UARTx = UART4; GPIO_AF_UARTx = GPIO_AF8_UART4; GPIO_Port = GPIOA; GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1; __UART4_CLK_ENABLE(); break; // USART6 is on PC6/PC7 (CK on PC8) case PYB_UART_6: UARTx = USART6; GPIO_AF_UARTx = GPIO_AF8_USART6; GPIO_Port = GPIOC; GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7; __USART6_CLK_ENABLE(); break; default: return false; } // init GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = GPIO_Pin; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_PULLUP; GPIO_InitStructure.Alternate = GPIO_AF_UARTx; HAL_GPIO_Init(GPIO_Port, &GPIO_InitStructure); // init UARTx uart_obj->uart.Instance = UARTx; HAL_UART_Init(&uart_obj->uart); uart_obj->is_enabled = true; #endif return true; }
void serial_init(serial_t *obj, PinName tx, PinName rx) { // Determine the UART to use (UART_1, UART_2, ...) UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object UARTName instance = (UARTName)pinmap_merge(uart_tx, uart_rx); MBED_ASSERT(instance != (UARTName)NC); // Enable USART clock switch (instance) { case UART_1: __USART1_CLK_ENABLE(); obj->serial.module = 0; break; case UART_2: __USART2_CLK_ENABLE(); obj->serial.module = 1; break; #if defined(USART3_BASE) case UART_3: __USART3_CLK_ENABLE(); obj->serial.module = 2; break; #endif #if defined(UART4_BASE) case UART_4: __UART4_CLK_ENABLE(); obj->serial.module = 3; break; #endif #if defined(UART5_BASE) case UART_5: __UART5_CLK_ENABLE(); obj->serial.module = 4; break; #endif case UART_6: __USART6_CLK_ENABLE(); obj->serial.module = 5; break; #if defined(UART7_BASE) case UART_7: __UART7_CLK_ENABLE(); obj->serial.module = 6; break; #endif #if defined(UART8_BASE) case UART_8: __UART8_CLK_ENABLE(); obj->serial.module = 7; break; #endif } // Configure the UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); if (tx != NC) pin_mode(tx, PullUp); if (rx != NC) pin_mode(rx, PullUp); obj->serial.pin_tx = tx; obj->serial.pin_rx = rx; // initialize the handle for this master! UART_HandleTypeDef *handle = &UartHandle[obj->serial.module]; handle->Instance = (USART_TypeDef *)instance; handle->Init.BaudRate = 9600; handle->Init.WordLength = UART_WORDLENGTH_8B; handle->Init.StopBits = UART_STOPBITS_1; handle->Init.Parity = UART_PARITY_NONE; if (rx == NC) { handle->Init.Mode = UART_MODE_TX; } else if (tx == NC) { handle->Init.Mode = UART_MODE_RX; } else { handle->Init.Mode = UART_MODE_TX_RX; } handle->Init.HwFlowCtl = UART_HWCONTROL_NONE; handle->Init.OverSampling = UART_OVERSAMPLING_16; handle->TxXferCount = 0; handle->RxXferCount = 0; if (tx == STDIO_UART_TX && rx == STDIO_UART_RX) { handle->Init.BaudRate = YOTTA_CFG_MBED_OS_STDIO_DEFAULT_BAUD; } HAL_UART_Init(handle); // DEBUG_PRINTF("UART%u: Init\n", obj->serial.module+1); }