/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file usart.c * @brief This file provides code for the configuration * of the USART instances. ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "usart.h" /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ UART_HandleTypeDef huart3; DMA_HandleTypeDef hdma_usart3_rx; /* USART3 init function */ void MX_USART3_UART_Init(void) { /* USER CODE BEGIN USART3_Init 0 */ /* USER CODE END USART3_Init 0 */ /* USER CODE BEGIN USART3_Init 1 */ //256000*4 /* USER CODE END USART3_Init 1 */ huart3.Instance = USART3; huart3.Init.BaudRate = 9600*2; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1; huart3.Init.Parity = UART_PARITY_NONE; huart3.Init.Mode = UART_MODE_TX_RX; huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart3.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart3) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART3_Init 2 */ /* USER CODE END USART3_Init 2 */ } void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(uartHandle->Instance==USART3) { /* USER CODE BEGIN USART3_MspInit 0 */ /* USER CODE END USART3_MspInit 0 */ /* USART3 clock enable */ __HAL_RCC_USART3_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /**USART3 GPIO Configuration PB10 ------> USART3_TX PB11 ------> USART3_RX */ GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USART3 DMA Init */ /* USART3_RX Init */ hdma_usart3_rx.Instance = DMA1_Channel3; 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; if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK) { Error_Handler(); } __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx); /* USART3 interrupt Init */ HAL_NVIC_SetPriority(USART3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_IRQn); /* USER CODE BEGIN USART3_MspInit 1 */ /* USER CODE END USART3_MspInit 1 */ } } void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) { if(uartHandle->Instance==USART3) { /* USER CODE BEGIN USART3_MspDeInit 0 */ /* USER CODE END USART3_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART3_CLK_DISABLE(); /**USART3 GPIO Configuration PB10 ------> USART3_TX PB11 ------> USART3_RX */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11); /* USART3 DMA DeInit */ HAL_DMA_DeInit(uartHandle->hdmarx); /* USART3 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART3_IRQn); /* USER CODE BEGIN USART3_MspDeInit 1 */ /* USER CODE END USART3_MspDeInit 1 */ } } /* USER CODE BEGIN 1 */ // CUSTOM UART INIT //void User_UART_Init(UART_HandleTypeDef* huart, DMA_HandleTypeDef *DMAhuart, struct UARTSettings *uuart) //{ // function takes uart handler, dma handler and setting structure for init // // // get setting for uart from UARTSettings structure // huart->Instance = uuart->UARTx; // huart->Init.BaudRate = uuart->UART_Speed*2; // // // everything else is default (for now, maybe this settings would be added in UARTSettings structure later) // 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; // if (HAL_UART_Init(huart) != HAL_OK) // { // Error_Handler(); // } // // // init gpio from UARTSettings structure // UART_GPIO_Init(uuart->GPIOx, uuart->GPIO_PIN_RX, uuart->GPIO_PIN_TX); // // init dma from UARTSettings structure if need // if (uuart->DMAChannel != 0) // UART_DMA_Init(huart, DMAhuart, uuart->DMAChannel); //} //void UART_GPIO_Init(GPIO_TypeDef *GPIOx, uint16_t GPIO_PIN_RX, uint16_t GPIO_PIN_TX) //{ // function takes port and pins (for rx and tx) // GPIO_InitTypeDef GPIO_InitStruct = {0}; // // // choose port for enable clock // if (GPIOx==GPIOA) // __HAL_RCC_GPIOA_CLK_ENABLE(); // else if (GPIOx==GPIOB) // __HAL_RCC_GPIOB_CLK_ENABLE(); // else if (GPIOx==GPIOC) // __HAL_RCC_GPIOC_CLK_ENABLE(); // else if (GPIOx==GPIOD) // __HAL_RCC_GPIOD_CLK_ENABLE(); // //// USART3 GPIO Configuration ////GPIO_PIN_TX ------> USART_TX // GPIO_InitStruct.Pin = GPIO_PIN_TX; // GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // HAL_GPIO_Init(GPIOx, &GPIO_InitStruct); //// GPIO_PIN_RX ------> USART_RX // GPIO_InitStruct.Pin = GPIO_PIN_RX; // GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // GPIO_InitStruct.Pull = GPIO_NOPULL; // HAL_GPIO_Init(GPIOx, &GPIO_InitStruct); //} //void UART_DMA_Init(UART_HandleTypeDef *huart, DMA_HandleTypeDef *hdma_rx, DMA_Channel_TypeDef *DMAhuart) //{ // function takes uart and dma handlers and dmachannel for uart // // for now only dma rx is supported, tx maybe later if needed // // /* USART3 DMA Init */ // /* USART3_RX Init */ // hdma_rx->Instance = DMAhuart; // hdma_rx->Init.Direction = DMA_PERIPH_TO_MEMORY; // hdma_rx->Init.PeriphInc = DMA_PINC_DISABLE; // hdma_rx->Init.MemInc = DMA_MINC_ENABLE; // hdma_rx->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; // hdma_rx->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; // hdma_rx->Init.Mode = DMA_CIRCULAR; // hdma_rx->Init.Priority = DMA_PRIORITY_LOW; // if (HAL_DMA_Init(hdma_rx) != HAL_OK) // { // Error_Handler(); // } // __USER_LINKDMA(huart,hdmarx,hdma_rx); // // __USER_LINKDMA is need because __HAL_LINKDMA is written for global defined hdma_rx // // so you get error because hal uses . insted of -> //} //void HAL_UART_MspInit(UART_HandleTypeDef* huart) // redefine hal function //{ // left only rcc and interrupt init for USART_1,2,3 (maybe UART_4,5 need to be added) // // GPIO and DMA init was move to their own functions // if(huart->Instance==USART3) // { // /* USART3 clock enable */ // __HAL_RCC_USART3_CLK_ENABLE(); // // /* USART3 interrupt Init */ // HAL_NVIC_SetPriority(USART3_IRQn, 0, 0); // HAL_NVIC_EnableIRQ(USART3_IRQn); // } // else if(huart->Instance==USART2) // { // /* USART3 clock enable */ // __HAL_RCC_USART2_CLK_ENABLE(); // // /* USART3 interrupt Init */ // HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); // HAL_NVIC_EnableIRQ(USART2_IRQn); // } // else if(huart->Instance==USART1) // { // /* USART3 clock enable */ // __HAL_RCC_USART1_CLK_ENABLE(); // // /* USART3 interrupt Init */ // HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); // HAL_NVIC_EnableIRQ(USART1_IRQn); // } //} /* USER CODE END 1 */