cancel
Showing results for 
Search instead for 
Did you mean: 

USART2 : Communication RS232

edwin marcellin
Associate II
Posted on May 17, 2018 at 14:40

Hello,

I have a prototype where a sensor send information in RS232 to my STM32152RE board through a MAX232 to convert the signal. I plugged in my board with RX/TX on USART 2 (+5V et GND).

I used CUBE MX and i only modify the code as following but i receive no signal.

Is someone can help me, please?

/* Includes ------------------------------------------------------------------*/

&sharpinclude 'main.h'

&sharpinclude 'stm32l1xx_hal.h'

&sharpinclude <stdio.h>

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart2;

/* Private variables ---------------------------------------------------------*/

int i=0;

uint8_t buffer[100];

int len;

uint8_t Rx_indx, Rx_data[2], Rx_Buffer[100], Transfer_cplt;

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

/* Private function prototypes -----------------------------------------------*/

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

uint8_t i;

if (huart->Instance == USART2) //current UART

{

if (Rx_indx==0) {for (i=0;i<100;i++) Rx_Buffer[i]=0;} //clear Rx_Buffer before receiving new data

if (Rx_data[0]!=13) //if received data different from ascii 13 (enter)

{

Rx_Buffer[Rx_indx++]=Rx_data[0]; //add data to Rx_Buffer

}

else //if received data = 13

{

Rx_indx=0;

Transfer_cplt=1;//transfer complete, data is ready to read

}

HAL_UART_Receive_IT(&huart2, Rx_data, 1); //activate UART receive interrupt every time

}

}

int main(void)

{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_USART2_UART_Init();

/* USER CODE BEGIN 2 */

HAL_UART_Receive_IT(&huart2,Rx_data,1); //activate uart rx interrupt every time receiving 1 byte

/* USER CODE END 2 */

while (1)

{

if(Transfer_cplt)

{

sprintf(buffer,'%s\r\n',Rx_Buffer);

len=strlen(buffer);

HAL_UART_Transmit(&huart2,buffer, len,1000);

Transfer_cplt=0;

HAL_Delay(500);

i++;

}

}

}

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

/**Configure the main internal regulator output voltage

*/

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;

RCC_OscInitStruct.MSIState = RCC_MSI_ON;

RCC_OscInitStruct.MSICalibrationValue = 0;

RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Configure the Systick interrupt time

*/

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick

*/

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* USART2 init function */

static 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_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

/** Pinout Configuration

*/

static void MX_GPIO_Init(void)

{

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

* @brief This function is executed in case of error occurrence.

* @param file: The file name as string.

* @param line: The line in file as a number.

* @retval None

*/

void _Error_Handler(char *file, int line)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

while(1)

{

}

/* USER CODE END Error_Handler_Debug */

}

&sharpifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

&sharpendif /* USE_FULL_ASSERT */

#usart #rs232 #usart-rs232 #usart2
9 REPLIES 9
T J
Lead
Posted on May 18, 2018 at 01:50

Can you set a breakpoint in here: HAL_UART_RxCpltCallback

and single step from there.

Posted on May 18, 2018 at 03:40

Review signalling with a scope.

Try sending a 'U' data pattern, confirm levels and bit timing.

Break-point the IRQ handler, confirm reception of any data.

Check from overrun or framing errors flagged by the USART.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Pevy.Andy
Associate III
Posted on May 18, 2018 at 12:38

Have you enabled the NVIC for the UART in the Cube tool ?.

Posted on May 18, 2018 at 12:30

Hello,

Thank you for your help.

Without launching my code, when i open an hyperterminal (Tera term) i receive the data with the good format. so i think hardware part and configuration of uart is ok

When i launch the code with a 

breakpoint in here: HAL_UART_RxCpltCallback, i have the message ''One or more breakpoints could not be set and have been disabled''. So it diactivate my break point and when i launch the code, i never go into this function 

HAL_UART_RxCpltCallback.

When i do the HAL_Init(), i have HAL_OK

My configuration is:

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_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

In my hyperterminal, i still see the data.

But if i check my variables buffer, RX_data... are fixed to 0. Is it normal that i don't have StopBits?

0690X0000060KqiQAE.png

I'm a bit lost...

Thank you for your help

Posted on May 18, 2018 at 13:25

I think so.

0690X0000060Kn6QAE.png
edwin marcellin
Associate II
Posted on May 24, 2018 at 14:28

Anyone could help me? I think the pb come from the reception of data. I think i don't receive something... Do i have to use a specific configuration with jumper to use usart2? or do you have any supplementary clues?

T J
Lead
Posted on May 25, 2018 at 02:33

Are you using the DMA and the Uart Rx interrupt ?

if the DMA is configured the Bytes should start appearing in the predefined buffer.

The DMA functionality in these processors seems flawless, you really should be using it.

edwin marcellin
Associate II
Posted on May 28, 2018 at 09:29

No i don't use DMA.

I tried with another code (ST example) and i only receive the 2 first bytes.

But anyway, without DMA, i should still receive something, no?

AvaTar
Lead
Posted on May 28, 2018 at 09:36

Using a debugger with 'live watch' and viewing peripheral register data is a 'deadly' combination.

Several registers have flag bits that get reset upon read - like interrupt flags, for instance.