cancel
Showing results for 
Search instead for 
Did you mean: 

[stm32f411RE] uart communication between nucleo and PC, problem in the reception

fernando23
Associate
Posted on July 20, 2015 at 10:23

hello everyone, i'm trying to understand the uart communication with my stm32f411RE, on linux.

I was able to send a message from nucleo to PC, but the problem is the reverse. I can't send something, e.g 1 character, to nucleo and then forwarding it to PC. My idea is, i want send 1 or more character from PC to nucleo, and then forwarding them to PC to print them. thanks this is a snippet

/**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2015 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include ''stm32f4xx_hal.h''
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_tx;
DMA_HandleTypeDef hdma_usart2_rx;
/* Private function prototypes -----------------------------------------------*/
void
SystemClock_Config(
void
);
static
void
MX_GPIO_Init(
void
);
static
void
MX_DMA_Init(
void
);
static
void
MX_USART2_UART_Init(
void
);
char
*msg1;
int
main(
void
)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART2_UART_Init();
char
*msg = 
''Hello Nucleo Fun!\n\r''
;
HAL_UART_Transmit(&huart2, (uint8_t*)msg, 
strlen
(msg), 0xFFFF);
/* Infinite loop */
while
(1)
{
HAL_UART_Receive_IT(&huart2, msg1, 1);
}
}
void
HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
HAL_UART_StateTypeDef st = HAL_UART_Transmit_IT(&huart2, &msg1, 1);
while
(st == HAL_UART_STATE_BUSY_TX);
HAL_UART_Receive_IT(&huart2, &msg1, 1);
}
/** System Clock Configuration
*/
void
SystemClock_Config(
void
)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 4;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
}
/* USART2 init function */
void
MX_USART2_UART_Init(
void
)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
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;
HAL_UART_Init(&huart2);
}
/** 
* Enable DMA controller clock
*/
void
MX_DMA_Init(
void
) 
{
/* DMA controller clock enable */
__DMA1_CLK_ENABLE();
/* DMA interrupt init */
HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);
}
/** Configure pins as 
* Analog 
* Input 
* Output
* EVENT_OUT
* EXTI
*/
void
MX_GPIO_Init(
void
)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__GPIOC_CLK_ENABLE();
__GPIOH_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
__GPIOB_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);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

6 REPLIES 6
Posted on July 20, 2015 at 19:30 So what happens here if the functions returns something else?
while
(st == HAL_UART_STATE_BUSY_TX);
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
fernando23
Associate
Posted on July 22, 2015 at 22:22

exit.

But you right, i done some fixes but i think that i miss the correct method to make a serial connection, i mean transmitt and receive.

Can you help me ??

I don't understand how to receive data from computer. The ''HAL_UART_RxCpltCallback'' is never call.

Posted on July 22, 2015 at 22:33

Can you help me ??

HAL is a train-wreck I'm avoiding. If you're not getting a call back you need to verify that the STM32 is actually receiving any data, and if it is why the IRQHandler isn't being enabled/called. The IRQHandler needs to call back into the HAL, and it in turn calls your callback function. If it gets stuck in your function, this will seriously limit it's ability to function beyond one character.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
eng
Associate II
Posted on September 15, 2015 at 22:28

I don't understand, why are you using char *msg1?

This pointer don't have with a address. it is NULL (?), so is probably that will happen a Hard Fault! (I guess).

In my opinion you should declare how 'char msg' and send him with a pointer for HAL_UART_Receive_IT(&huart2, &msg1, 1).

Obs: Sorry for my english, but I am still studing this language. Then, I need improve. And I only improve if I try write and talk more.

Posted on September 15, 2015 at 23:52

More realistically

char msg1[1];

HAL_UART_Receive_IT(&huart2, msg1, 1).

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
eng
Associate II
Posted on September 17, 2015 at 14:35

I'm have seriou problems with the library HAL, specially with UART. Try do the following for verify if you have the same problem with me:

In file usart.c put the function below.

uint16_t errorUART2=0;

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart){

  if(huart->Instance == USART2)

  {

    errorUART2++;

    HAL_UART_Receive_IT(&huart2, (uint8_t *) &ui16characterUSART2, 1 );

  }

}

Insert a breakpoint in variable error UART2 or leave running your program and after verify this variable. If errorUART2 is greater than 0, so you have problems. =)