//****************************************************************************** // 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 }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ /* USER CODE END USART1_MspInit 0 */ /* Peripheral clock enable */ __USART1_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_MspInit 1 */ } else if (huart->Instance == USART2) { __USART2_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } else if (huart->Instance == USART3) { __USART3_CLK_ENABLE(); GPIO_InitStruct.Pin = GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; 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 = USART_TX_Pin|USART_RX_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ } }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; 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 = USART_TX_Pin|USART_RX_Pin; 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); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_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) { __USART1_CLK_ENABLE(); obj->index = 0; } if (obj->uart == UART_2) { __USART2_CLK_ENABLE(); obj->index = 1; } if (obj->uart == UART_3) { __USART3_CLK_ENABLE(); obj->index = 2; } // 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 #if defined (MX_DEFAULT_SERIAL_BAUD) obj->baudrate = MX_DEFAULT_SERIAL_BAUD; #else obj->baudrate = 9600; #endif 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)); } }
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); if (obj->uart == (UARTName)NC) { error("Serial error: pinout mapping failed."); } // Enable USART clock if (obj->uart == UART_1) { __USART1_CLK_ENABLE(); obj->index = 0; } if (obj->uart == UART_2) { __USART2_CLK_ENABLE(); obj->index = 1; } if (obj->uart == UART_3) { __USART3_CLK_ENABLE(); obj->index = 2; } if (obj->uart == UART_4) { __USART4_CLK_ENABLE(); obj->index = 3; } // Configure the UART pins pinmap_pinout(tx, PinMap_UART_TX); pinmap_pinout(rx, PinMap_UART_RX); pin_mode(tx, PullUp); 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)); } }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ /* USER CODE END USART1_MspInit 0 */ /* Peripheral clock enable */ __USART1_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = GPIO_PIN_9|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_AF1_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_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_AF1_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ } }
void Uart2UARTInit(UART_HandleTypeDef* huart) { __USART2_CLK_ENABLE() ; huart->Instance = USART2; huart->Init.BaudRate = 115200; huart->Init.WordLength = UART_WORDLENGTH_8B; huart->Init.StopBits = UART_STOPBITS_1; huart->Init.Parity = UART_PARITY_NONE; huart->Init.Mode = UART_MODE_TX_RX; huart->Init.HwFlowCtl = UART_HWCONTROL_NONE; huart->Init.OverSampling = UART_OVERSAMPLING_16; MyHAL_UARTInit(huart); __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; 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_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral DMA init*/ hdma_usart2_rx.Instance = DMA1_Channel6; hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_rx.Init.Mode = DMA_CIRCULAR; hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW; HAL_DMA_Init(&hdma_usart2_rx); __HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ } }
void HAL_UART_MspInit(UART_HandleTypeDef* UART2) { if(UART2->Instance==USART2) { /* Peripheral clock enable */ __USART2_CLK_ENABLE(); /**USART2 GPIO Configuration * PA2 ------> USART2_TX * PA3 ------> USART2_RX * */ GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_3; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_LOW; GPIO_InitStructure.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); } }
void HAL_USART_MspInit(USART_HandleTypeDef *huart) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable GPIO TX/RX and USART clock */ __GPIOA_CLK_ENABLE(); __USART2_CLK_ENABLE(); /* USART TX/RX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* NVIC for USART */ HAL_NVIC_SetPriority(USART2_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USART2_IRQn); }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART2) { __USART2_CLK_ENABLE(); /**USART2 GPIO Configuration PD5 ------> USART2_TX PD6 ------> USART2_RX */ GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_Pin; 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(GPIOD, &GPIO_InitStruct); } }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART1) { __USART1_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } else if(huart->Instance==USART2) { __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_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } }
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 HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART1) { /* Peripheral clock enable */ __USART1_CLK_ENABLE(); __GPIOA_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral interrupt init*/ // /* Sets the priority grouping field */ HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); } else if(huart->Instance==USART2) { /* 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_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } else if(huart->Instance==USART3) { /* Peripheral clock enable */ __USART3_CLK_ENABLE(); /**USART3 GPIO Configuration PB10 ------> USART3_TX PB11 ------> USART3_RX */ GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART3; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } else if(huart->Instance==USART6) { /* Peripheral clock enable */ __USART6_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); /**USART6 GPIO Configuration PC6 ------> USART6_TX PC7 ------> USART6_RX */ GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF8_USART6; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* Peripheral interrupt init*/ // /* Sets the priority grouping field */ // HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0); // HAL_NVIC_SetPriority(USART6_IRQn, 0, 0); // HAL_NVIC_EnableIRQ(USART6_IRQn); } }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ /* USER CODE END USART1_MspInit 0 */ /* Peripheral clock enable */ __USART1_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(USART1_IRQn, 5, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_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_FAST; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral DMA init*/ hdma_usart2_tx.Instance = DMA1_Stream6; hdma_usart2_tx.Init.Channel = DMA_CHANNEL_4; hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_tx.Init.Mode = DMA_NORMAL; hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_usart2_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE; hdma_usart2_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL; hdma_usart2_tx.Init.MemBurst = DMA_MBURST_SINGLE; hdma_usart2_tx.Init.PeriphBurst = DMA_PBURST_SINGLE; HAL_DMA_Init(&hdma_usart2_tx); __HAL_LINKDMA(huart,hdmatx,hdma_usart2_tx); hdma_usart2_rx.Instance = DMA1_Stream5; hdma_usart2_rx.Init.Channel = DMA_CHANNEL_4; hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_rx.Init.Mode = DMA_NORMAL; hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW; hdma_usart2_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE; hdma_usart2_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL; hdma_usart2_rx.Init.MemBurst = DMA_MBURST_SINGLE; hdma_usart2_rx.Init.PeriphBurst = DMA_PBURST_SINGLE; HAL_DMA_Init(&hdma_usart2_rx); __HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(USART2_IRQn, 5, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ } }
/** * @brief UART MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * - NVIC configuration for UART interrupt request enable * @param huart: UART handle pointer * @retval None */ void HAL_UART_MspInit(UART_HandleTypeDef *huart) { GPIO_InitTypeDef GPIO_InitStruct; switch((uint32_t)huart->Instance) { case (uint32_t)USART1 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOA_CLK_ENABLE(); /* Enable USARTx clock */ __USART1_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_9 | 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_AF1_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART1_IRQn, 1, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); } break; case (uint32_t)USART2 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOD_CLK_ENABLE(); /* Enable USARTx clock */ __USART2_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_USART2; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART2_IRQn, 1, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); } break; case (uint32_t)USART3 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOD_CLK_ENABLE(); /* To allow the IT source identification */ __SYSCFG_CLK_ENABLE(); /* Enable USARTx clock */ __USART3_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF0_USART3; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART3_8_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_8_IRQn); } break; case (uint32_t)USART4 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOC_CLK_ENABLE(); /* To allow the IT source identification */ __SYSCFG_CLK_ENABLE(); /* Enable USARTx clock */ __USART4_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ 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_AF0_USART4; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART3_8_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_8_IRQn); } break; case (uint32_t)USART5 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOB_CLK_ENABLE(); /* To allow the IT source identification */ __SYSCFG_CLK_ENABLE(); /* Enable USARTx clock */ __USART5_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_USART5; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART3_8_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_8_IRQn); } break; case (uint32_t)USART6 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOF_CLK_ENABLE(); /* To allow the IT source identification */ __SYSCFG_CLK_ENABLE(); /* Enable USARTx clock */ __USART6_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_9 | 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_AF1_USART6; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART3_8_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_8_IRQn); } break; case (uint32_t)USART7 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOF_CLK_ENABLE(); /* To allow the IT source identification */ __SYSCFG_CLK_ENABLE(); /* Enable USARTx clock */ __USART7_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_3 | 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_AF1_USART7; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART3_8_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_8_IRQn); } break; case (uint32_t)USART8 : { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ __GPIOC_CLK_ENABLE(); /* To allow the IT source identification */ __SYSCFG_CLK_ENABLE(); /* Enable USARTx clock */ __USART8_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF1_USART8; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*##-3- Configure the NVIC for UART ########################################*/ /* NVIC for USART */ HAL_NVIC_SetPriority(USART3_8_IRQn, 1, 0); HAL_NVIC_EnableIRQ(USART3_8_IRQn); } break; } }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ /* USER CODE END USART1_MspInit 0 */ /* Peripheral clock enable */ __USART1_CLK_ENABLE(); /**USART1 GPIO Configuration PA10 ------> USART1_RX PA9 ------> USART1_TX */ GPIO_InitStruct.Pin = U1RX_Pin|U1TX_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_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 PA1 ------> USART2_RTS PA2 ------> USART2_TX PA3 ------> USART2_RX */ GPIO_InitStruct.Pin = RS_ON232_Pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(RS_ON232_GPIO_Port, &GPIO_InitStruct); GPIO_InitStruct.Pin = RS_TX_Pin|RS_RX_Pin; 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 PC11 ------> USART3_RX PC10 ------> USART3_TX */ GPIO_InitStruct.Pin = RS485_RX_Pin|RS485_TX_Pin; 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(GPIOC, &GPIO_InitStruct); /* USER CODE BEGIN USART3_MspInit 1 */ /* USER CODE END USART3_MspInit 1 */ } }
// 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; }
void serial_init(serial_t *obj, PinName tx, PinName rx) { struct serial_s *obj_s = SERIAL_S(obj); // 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_s->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); MBED_ASSERT(obj_s->uart != (UARTName)NC); // Enable USART clock + switch to SystemClock if (obj_s->uart == UART_1) { __USART1_FORCE_RESET(); __USART1_RELEASE_RESET(); __USART1_CLK_ENABLE(); #if defined(RCC_USART1CLKSOURCE_SYSCLK) __HAL_RCC_USART1_CONFIG(RCC_USART1CLKSOURCE_SYSCLK); #endif obj_s->index = 0; } #if defined(USART2_BASE) if (obj_s->uart == UART_2) { __USART2_FORCE_RESET(); __USART2_RELEASE_RESET(); __USART2_CLK_ENABLE(); #if defined(RCC_USART2CLKSOURCE_SYSCLK) __HAL_RCC_USART2_CONFIG(RCC_USART2CLKSOURCE_SYSCLK); #endif obj_s->index = 1; } #endif #if defined(USART3_BASE) if (obj_s->uart == UART_3) { __USART3_FORCE_RESET(); __USART3_RELEASE_RESET(); __USART3_CLK_ENABLE(); #if defined(RCC_USART3CLKSOURCE_SYSCLK) __HAL_RCC_USART3_CONFIG(RCC_USART3CLKSOURCE_SYSCLK); #endif obj_s->index = 2; } #endif #if defined(UART4_BASE) if (obj_s->uart == UART_4) { __UART4_FORCE_RESET(); __UART4_RELEASE_RESET(); __UART4_CLK_ENABLE(); #if defined(RCC_UART4CLKSOURCE_SYSCLK) __HAL_RCC_UART4_CONFIG(RCC_UART4CLKSOURCE_SYSCLK); #endif obj_s->index = 3; } #endif #if defined(UART5_BASE) if (obj_s->uart == UART_5) { __HAL_RCC_UART5_FORCE_RESET(); __HAL_RCC_UART5_RELEASE_RESET(); __UART5_CLK_ENABLE(); #if defined(RCC_UART5CLKSOURCE_SYSCLK) __HAL_RCC_UART5_CONFIG(RCC_UART5CLKSOURCE_SYSCLK); #endif obj_s->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_s->baudrate = 9600; obj_s->databits = UART_WORDLENGTH_8B; obj_s->stopbits = UART_STOPBITS_1; obj_s->parity = UART_PARITY_NONE; #if DEVICE_SERIAL_FC obj_s->hw_flow_ctl = UART_HWCONTROL_NONE; #endif obj_s->pin_tx = tx; obj_s->pin_rx = rx; init_uart(obj); // For stdio management if (obj_s->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; 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; }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ /* USER CODE END USART1_MspInit 0 */ /* Peripheral clock enable */ __USART1_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_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_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ } }
// 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; }
// 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 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)); } }
void usart_init(pyb_usart_obj_t *usart_obj, uint32_t baudrate) { USART_TypeDef *USARTx=NULL; uint32_t GPIO_Pin=0; uint8_t GPIO_AF_USARTx=0; GPIO_TypeDef* GPIO_Port=NULL; switch (usart_obj->usart_id) { case PYB_USART_NONE: return; case PYB_USART_1: USARTx = USART1; GPIO_Port = GPIOA; GPIO_AF_USARTx = GPIO_AF7_USART1; GPIO_Pin = GPIO_PIN_9 | GPIO_PIN_10; __USART1_CLK_ENABLE(); break; case PYB_USART_2: USARTx = USART2; GPIO_Port = GPIOD; GPIO_AF_USARTx = GPIO_AF7_USART2; GPIO_Pin = GPIO_PIN_5 | GPIO_PIN_6; __USART2_CLK_ENABLE(); break; case PYB_USART_3: USARTx = USART3; #if defined(PYBV3) || defined(PYBV4) GPIO_Port = GPIOB; GPIO_AF_USARTx = GPIO_AF7_USART3; GPIO_Pin = GPIO_PIN_10 | GPIO_PIN_11; #else GPIO_Port = GPIOD; GPIO_AF_USARTx = GPIO_AF7_USART3; GPIO_Pin = GPIO_PIN_8 | GPIO_PIN_9; #endif __USART3_CLK_ENABLE(); break; case PYB_USART_6: USARTx = USART6; GPIO_Port = GPIOC; GPIO_AF_USARTx = GPIO_AF8_USART6; GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7; __USART6_CLK_ENABLE(); break; } // Initialize USARTx 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_USARTx; HAL_GPIO_Init(GPIO_Port, &GPIO_InitStructure); UART_HandleTypeDef *uh = &usart_obj->handle; memset(uh, 0, sizeof(*uh)); uh->Instance = USARTx; uh->Init.BaudRate = baudrate; uh->Init.WordLength = USART_WORDLENGTH_8B; uh->Init.StopBits = USART_STOPBITS_1; uh->Init.Parity = USART_PARITY_NONE; uh->Init.Mode = USART_MODE_TX_RX; uh->Init.HwFlowCtl = UART_HWCONTROL_NONE; uh->Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(uh); }
/** * @brief UART MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param huart: UART handle pointer * @retval None */ void HAL_UART_MspInit(UART_HandleTypeDef *huart) { if (huart->Instance == USART2) { GPIO_InitTypeDef GPIO_InitStruct; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable GPIO TX/RX clock */ USART2_TX_GPIO_CLK_ENABLE(); USART2_RX_GPIO_CLK_ENABLE(); /* Enable USART2 clock */ __USART2_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* UART TX GPIO pin configuration */ GPIO_InitStruct.Pin = USART2_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = USART2_TX_AF; HAL_GPIO_Init(USART2_TX_PORT, &GPIO_InitStruct); /* UART RX GPIO pin configuration */ GPIO_InitStruct.Pin = USART2_RX_PIN; GPIO_InitStruct.Alternate = USART2_RX_AF; HAL_GPIO_Init(USART2_RX_PORT, &GPIO_InitStruct); /* Enable RXNE interrupt */ __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); NVIC_SetPriority(USART2_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 3); NVIC_EnableIRQ(USART2_IRQn); } else if (huart->Instance==USART6) { GPIO_InitTypeDef GPIO_InitStruct; /* Peripheral clock enable */ __USART6_CLK_ENABLE(); USART6_TX_GPIO_CLK_ENABLE(); USART6_RX_GPIO_CLK_ENABLE(); /* USART6 GPIO Configuration */ /* TODO: Temproray fix to disable USART6 TX pin. I made a mistake to swap the TX and RX pin for the MPU6050 module... */ GPIO_InitStruct.Pin = USART6_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; //GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; //GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = 0; //USART6_TX_AF; HAL_GPIO_Init(USART6_TX_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = USART6_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = USART6_RX_AF; HAL_GPIO_Init(USART6_RX_PORT, &GPIO_InitStruct); } else if (huart->Instance==USART1) { GPIO_InitTypeDef GPIO_InitStruct; /* Peripheral clock enable */ __USART1_CLK_ENABLE(); USART1_TX_GPIO_CLK_ENABLE(); USART1_RX_GPIO_CLK_ENABLE(); /* USART1 GPIO Configuration */ GPIO_InitStruct.Pin = USART1_TX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = USART1_TX_AF; HAL_GPIO_Init(USART1_TX_PORT, &GPIO_InitStruct); GPIO_InitStruct.Pin = USART1_RX_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = USART1_RX_AF; HAL_GPIO_Init(USART1_RX_PORT, &GPIO_InitStruct); } }
void HAL_UART_MspInit(UART_HandleTypeDef* huart) { GPIO_InitTypeDef GPIO_InitStruct; 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_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral DMA init*/ hdma_usart2_rx.Instance = DMA1_Stream5; hdma_usart2_rx.Init.Channel = DMA_CHANNEL_4; hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_rx.Init.Mode = DMA_NORMAL; hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW; hdma_usart2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_usart2_rx); __HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx); hdma_usart2_tx.Instance = DMA1_Stream6; hdma_usart2_tx.Init.Channel = DMA_CHANNEL_4; hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart2_tx.Init.Mode = DMA_NORMAL; hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_usart2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_usart2_tx); __HAL_LINKDMA(huart,hdmatx,hdma_usart2_tx); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(USART2_IRQn, 1, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */ __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); //|UART_IT_IDLE /* 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 PD8 ------> USART3_TX PD9 ------> USART3_RX */ GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF7_USART3; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); /* Peripheral DMA init*/ hdma_usart3_rx.Instance = DMA1_Stream1; hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4; hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart3_rx.Init.Mode = DMA_CIRCULAR; hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW; hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_usart3_rx); __HAL_LINKDMA(huart,hdmarx,hdma_usart3_rx); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(USART3_IRQn, 2, 0); HAL_NVIC_EnableIRQ(USART3_IRQn); /* USER CODE BEGIN USART3_MspInit 1 */ /* USER CODE END USART3_MspInit 1 */ } else if(huart->Instance==USART6) { /* USER CODE BEGIN USART6_MspInit 0 */ /* USER CODE END USART6_MspInit 0 */ /* Peripheral clock enable */ __USART6_CLK_ENABLE(); /**USART6 GPIO Configuration PC6 ------> USART6_TX PC7 ------> USART6_RX */ GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF8_USART6; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* USER CODE BEGIN USART6_MspInit 1 */ /* USER CODE END USART6_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 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); }