2020-07-12 10:54 PM
Hey,
I'm using STM32F303RET6 and need to implement UART communication via USART1 (PA9 as Tx and PA10 as Rx).
I managed to print "Hello World" on my console,Which means Tx is working fine and my console is configured correctly, but couldn't receive bytes from the console.
This is how I set up the USART using MXCube
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 38400;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
and this is what i'm trying to do in order to get the data from the console
uint8_t myRxData[1];
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_UART_Receive(&huart1, myRxData, 10,1000);
//HAL_UART_Receive_IT(&huart1, myRxData, 10);
//HAL_UART_Receive_DMA(&huart1, myRxData, 10);
}
I also added this in order to echo while trying the IT method and even tried the DMA with no luck.
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE : This function should not be modified, when the callback is needed,
the HAL_UART_RxCpltCallback can be implemented in the user file.
*/
HAL_UART_Transmit(&huart1, myRxData, 1, 1000);
}
What am I doing wrong??
Thanks in advance,
Rony.
Solved! Go to Solution.
2020-07-14 03:02 AM
This shouldn't change anything. I tried anyway..
At the end it was a design problem which has nothing to do with the STM32...
Thanks anyway!
2020-07-12 11:24 PM
Hi. Your data buffer is 1 byte?
uint8_t myRxData[1];
If you want use IT or DMA methods, properly interrupts must be enabled.
2020-07-12 11:36 PM
Hi, just to be sure I do not understand your code wrongly. In the HAL_UART_Rx....Callback function, should it not be HAL_UART_ReceiveIT instead of HAL_UART_Transmit if you want to use UART in DMA or INTERRUPT mode ?
Also the HAL_UART_Receive_IT or DMA should not be in the while loop if you wanna use the INTERRUPT or DMA mode.
Now, for using the UART in Polling mode which is what you are doing in your while loop, everything seems right.
Try increasing the timeout from 1000 to something bigger or even use use the maximum timeout which is HAL_MAX_DELAY.
Also set your terminal to transmit continuously if this is possible so that there is no doubt the terminal has something been transmitted.
I do not know how your hardware setup is but One more things is to check that indeed you are using the PA10 as RX and the connection is okay.
Also try a smaller baud rate, like 9600 and see what happens
P.S: I would advice you test just for the polling mode first till it works before using DMA or INTERRUPT.
Let me know if this helps =)
2020-07-12 11:50 PM
Hey,
Yes my buffer is 1 byte.
It doesn't really matter the size.. I just need to perform a sanity check.
I don't really have to use the IT or DMA.. I simply need it to work no matter the method.
2020-07-13 01:42 AM
@oleksandr.karbivsky
I've tried the IT approach. now I manged to receive data one time and then i don't seem to be able to receive anymore data
uint8_t myRxData[10];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Receive_IT(&huart1, myRxData, 4);
}
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_DMA_Init();
MX_I2C1_Init();
MX_SPI2_Init();
MX_SPI3_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
/*
* @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
* @param GPIO_Pin specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* @param PinState specifies the value to be written to the selected bit.
* This parameter can be one of the GPIO_PinState enum values:
* @arg GPIO_PIN_RESET: to clear the port pin
* @arg GPIO_PIN_SET: to set the port pin
* @retval None
*/
HAL_UART_Receive_IT(&huart1, myRxData, 4);
while (1)
{
}
Then I've tried
while(1)
{
HAL_UART_Receive_IT(&huart1, myRxData, 4);
}
and it seems to work (Even though it shouldn't be like that...)
I worked in debug and I know the first time it does enter HAL_UART_RxCpltCallback so I'm not sure why the IT is enabled only once..
Do you have any insight??
Rony.
2020-07-13 07:47 AM
When you call this
HAL_UART_Receive_IT(&huart1, myRxData, 4);
HAL library receives 4 bytes to myRxData buffer and then rise HAL_UART_RxCpltCallback. After that, data acquisition stops.
If you want continuously receive data, try this:
uint8_t myRxData[10];
volatile uint8_t RxReady = 0;
volatile uint8_t RxByte = 0;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
RxReady = 1;
RxByte = myRxData[0];
HAL_UART_Receive_IT(&huart1, myRxData, 1);
}
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_DMA_Init();
MX_I2C1_Init();
MX_SPI2_Init();
MX_SPI3_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */
/*
* @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F3 family
* @param GPIO_Pin specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* @param PinState specifies the value to be written to the selected bit.
* This parameter can be one of the GPIO_PinState enum values:
* @arg GPIO_PIN_RESET: to clear the port pin
* @arg GPIO_PIN_SET: to set the port pin
* @retval None
*/
HAL_UART_Receive_IT(&huart1, myRxData, 1);
while (1)
{
if(RxReady){
RxReady = 0;
// Here handle received data byte (RxByte)
}
}
2020-07-14 03:02 AM
This shouldn't change anything. I tried anyway..
At the end it was a design problem which has nothing to do with the STM32...
Thanks anyway!
2024-05-19 10:48 AM
Hello, Respected Author, Might I inquire as to how you resolved your issue and why the above mentioned solution turned out to be of no use.
Yours Sincerely
ShahJ