2023-05-16 03:24 AM
Uart communication did not work in the existing project file.
There may be other interference, so I created a new project and wrote only Uart, but it is the same.
The input of the signal to the RX pin is confirmed by the oscilloscope, but it does not work.
Also, the TX pin remains high.
It doesn't seem to have anything special, but it doesn't work at all, so I'm at a loss as to what to do.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Main.c
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
unsigned char uartTxbuf[1024];
unsigned int uartTxbuf_cnt;
unsigned char uartRxData;
unsigned char uartRxbuf[256];
int uartBufCount = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) // Receive Interrupt
{
if(huart->Instance == USART2)
{
uartRxbuf[uartBufCount] = uartRxData;
uartBufCount++;
if(uartBufCount >= 256)
{
uartBufCount = 0;
}
}
HAL_UART_Receive_IT(&huart2, uartRxbuf, 8);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
huart->gState = HAL_UART_STATE_READY;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart2, uartRxbuf, 8);
sprintf((char *)uartTxbuf, "0123");
uartTxbuf_cnt = strlen(uartTxbuf);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_WritePin(GPIOB, UART_FLOW_CTRL_Pin, GPIO_PIN_SET); // Write mode
HAL_UART_Transmit_IT(&huart2, uartTxbuf, uartTxbuf_cnt); // Data Trans
HAL_GPIO_WritePin(GPIOB, UART_FLOW_CTRL_Pin, GPIO_PIN_RESET); // Read Mode
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/////////////////////////////////////////////////////////////////////////////////////////stm32f4xx_hal_msp.c
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_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_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART2 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 */
}
}
////////////////////////////////////////////////////////////////static void MX_USART2_UART_Init(void)
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
Solved! Go to Solution.
2023-05-16 06:40 AM
Hello @wkim.4 ,
Try to put this code between user code begin 4 and user code end 4 :
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) // Receive Interrupt
{
if(huart->Instance == USART2)
{
uartRxbuf[uartBufCount] = uartRxData;
uartBufCount++;
if(uartBufCount >= 256)
{
uartBufCount = 0;
}
}
HAL_UART_Receive_IT(&huart2, uartRxbuf, 8);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
huart->gState = HAL_UART_STATE_READY;
}
/* USER CODE END 4 */
Foued
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2023-05-16 06:40 AM
Hello @wkim.4 ,
Try to put this code between user code begin 4 and user code end 4 :
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) // Receive Interrupt
{
if(huart->Instance == USART2)
{
uartRxbuf[uartBufCount] = uartRxData;
uartBufCount++;
if(uartBufCount >= 256)
{
uartBufCount = 0;
}
}
HAL_UART_Receive_IT(&huart2, uartRxbuf, 8);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
huart->gState = HAL_UART_STATE_READY;
}
/* USER CODE END 4 */
Foued
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.