2014-07-14 06:44 AM
Hello there, I am trying to read in serial data from UART2 Rx and retransmit out to UART3 Tx. I am using the STM32CubeMx code generator with the F4 discovery board. Both UART are configure to the same baud rate.
I would like to ask if the method I am using is efficient: In the while loop in main, I have 1 function:while (1)
{
HAL_UART_Receive_IT(&huart2, &rx_char, 1);
}
and in the receive callback:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
if (UartHandle->Instance == USART2) {
HAL_UART_Transmit_IT(&huart3, &rx_char, 1);
}
}
2014-10-09 01:40 AM
Hiphua.eugene,
In fact, your implementation is only correct to receive just one character. But if you need to receive in continous mode you need to start the receive process under the RX callback, as shown below:void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{ if(UartHandle->Instance == USART2) { HAL_UART_Transmit_IT(&huart3, &rx_char, 1); HAL_UART_Receive_IT(&huart2, &rx_char, 1); } } Hope that can help you. Regards, Heisenberg.2015-07-02 06:28 AM
Hi Heisenberg,
and how can I stop this process? I mean, I would like to receive continuously but under some conditions I want to stop the reception, take appropriate actions and then start again.Thank you.2015-07-03 03:32 AM
Hi,
If I understand, you want to stop the reception during the transmission? isn't it? if so, you should know that UART is asynchronous, the transmission of data should not be interupted. Otherwise the data is lost. If you aim to implement another scenario, give me more details, so maybe I can help. Regards2015-07-03 05:24 AM
Hi!
Sorry, I've been ambiguous.I want to stop the reception. Because whenever you callHAL_UART_Receive_IT() within the callback then the reception process keeps going on. But I wonder if it is possible to stop it (for example if a given string has been received). One option could be to define a flag which if false then prevents to call againHAL_UART_Receive_IT() within the callback. Other options are welcome! Further my solution:void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
bool knownMess = 0;
if(UartHandle->Instance == UART_GSM)
{
RB_push(&ringBuffer, singleCharRx);
detectMess(&ringBuffer, &knownMess);
if(!knownMess)
HAL_UART_Receive_IT(UartHandle, &singleCharRx, 1);
else
{
/* do something */
}
}
}
2015-07-07 04:14 AM
Hi,
1-The functionHAL_UART_RxCpltCallback
is called when the tranfer is finished. 2-If you want to stop the reception, the transmitter will continue to send data and this can cause an overrun. Regards2015-07-13 02:04 AM
Hi,
1- Yes I know that. The callback is called when a data of size sizeof(singleCharRx
) (that is 1) is transferred from the data register to singleCharRx.2- Well I guessed that if I stop the receive process, then I am telling the lower layer I don't want to receive data anymore so overrun makes no sense or, at least it should be managed accordingly. However in case I get overrun I think there are macros that allow me to manage this occurrence.2015-07-29 03:53 AM
Found the answer
. Hope this can help somebodyelse.2015-08-11 04:49 PM
Hi, I am trying to create a basic echo to use for hyperterminal. I can not seem to get it working properly. When I copy and paste a string for example '1234567890' into hyperterminal I want an echo back '1234567890'. The uart seems to crash whenever i try sending data to the uart faster than typing speed. I need to connect it to a GSM modem, so I need a reliable uart connection.
Please see what is wrong here:#include ''stm32l0xx_hal.h''
#include <
stdio.h
>
#include <
string.h
>
UART_HandleTypeDef huart2;
char msgbuff[1000];
uint8_t incoming_uart2_buf[1000];
__IO ITStatus UartReady = RESET;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart2);
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
strcpy(msgbuff,''
START'');
HAL_UART_Transmit_IT(&huart2, (uint8_t*)msgbuff, strlen(msgbuff));
HAL_UART_Receive_IT(&huart2, (uint8_t *)incoming_uart2_buf, 1);
while (1)
{
}
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart2)
{
UartReady = SET;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart2)
{
HAL_UART_Transmit_IT(&huart2, (uint8_t *)incoming_uart2_buf, 1);
HAL_UART_Receive_IT(&huart2, (uint8_t *)incoming_uart2_buf, 1);
}
void USART2_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart2);
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
while(1)
{
}
}
/* USART2 init function */
void MX_USART2_UART_Init(void)
{
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;
huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
HAL_UART_Init(&huart2);
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
|RCC_PERIPHCLK_RTC;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
}
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__GPIOC_CLK_ENABLE();
__GPIOH_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PC13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : PA5 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}