2018-05-17 05:40 AM
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 dataif (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 #usart22018-05-17 04:50 PM
Can you set a breakpoint in here: HAL_UART_RxCpltCallback
and single step from there.
2018-05-17 06:40 PM
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.
2018-05-18 03:38 AM
Have you enabled the NVIC for the UART in the Cube tool ?.
2018-05-18 05:30 AM
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?
I'm a bit lost...
Thank you for your help
2018-05-18 06:25 AM
I think so.
2018-05-24 05:28 AM
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?
2018-05-24 05:33 PM
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.
2018-05-28 12:29 AM
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?
2018-05-28 12:36 AM
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.