Skip to main content
RCres.1
Associate III
March 20, 2020
Solved

Hello, I'm new on STM32. I'm using STM32F769-Discovery for product prototiping and I am trying to use USART6 with interrupts. That's what I've:

  • March 20, 2020
  • 1 reply
  • 742 views
#define UART_PORTID USART6//USART1
 
 
#define USARTx_CLK_ENABLE() __HAL_RCC_USART6_CLK_ENABLE();
#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
 
#define USARTx_FORCE_RESET() __HAL_RCC_USART6_FORCE_RESET()
#define USARTx_RELEASE_RESET() __HAL_RCC_USART6_RELEASE_RESET()
 
#define USARTx_TX_PIN GPIO_PIN_6
#define USARTx_TX_GPIO_PORT GPIOC
#define USARTx_TX_AF GPIO_AF8_USART6
#define USARTx_RX_PIN GPIO_PIN_7
#define USARTx_RX_GPIO_PORT GPIOC
#define USARTx_RX_AF GPIO_AF8_USART6
 
UART_HandleTypeDef UART_Handle;
 
void USART6_IRQHandler()
{
 HAL_UART_IRQHandler(&UART_Handle);
}
 
 /*##-1- Enable peripherals and GPIO Clocks #################################*/
 /* Enable GPIO TX/RX clock */
 USARTx_TX_GPIO_CLK_ENABLE();
 USARTx_RX_GPIO_CLK_ENABLE();
 
 /* Enable USART clock */
 USARTx_CLK_ENABLE();
 
 /*##-2- Configure peripheral GPIO ##########################################*/
 /* UART TX GPIO pin configuration */
 GPIO_InitStruct.Pin = USARTx_TX_PIN;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
 GPIO_InitStruct.Alternate = USARTx_TX_AF;
 
 HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
 
 /* UART RX GPIO pin configuration */
 GPIO_InitStruct.Pin = USARTx_RX_PIN;
 GPIO_InitStruct.Alternate = USARTx_RX_AF;
 
 HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
 
 UART_Handle.Instance = UART_PORTID;
 UART_Handle.Init.BaudRate = 9600;
 UART_Handle.Init.WordLength = UART_WORDLENGTH_8B;
 UART_Handle.Init.StopBits = UART_STOPBITS_1;
 UART_Handle.Init.Parity = UART_PARITY_NONE;
 UART_Handle.Init.Mode = UART_MODE_TX_RX;
 UART_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 UART_Handle.Init.OverSampling = UART_OVERSAMPLING_16;
 
 // Init USART
 HAL_UART_Init( &UART_Handle);
 
 // Enable USART IRQn
 NVIC_EnableIRQ(USART6_IRQn);
 
void EwBspConsolePutCharacter( const char* aMessage, unsigned int size ){
	HAL_UART_Transmit_IT( &UART_Handle, (uint8_t*)aMessage,(uint16_t)size);
}

And EwBspConsolePutCharacter is called from:

void EwConsoleOutput( const char* aMessage)
{
	char buffer[] = "HelloWorld\n\r";
	EwBspConsolePutCharacter(&buffer,sizeof(buffer));
}

The PC that receives the messages has serial config: 9600,8bits,1stopbit, non parity, none hw flowcontrol

The issue is sometimes message arrives incomplete:

0693W000000UN7UQAW.jpg

Where could be the mistake?

Kind Regards,

Rafa

This topic has been closed for replies.
Best answer by RCres.1

FIXED:

As said in: https://stackoverflow.com/questions/55624428/can-we-edit-callback-function-hal-uart-txcpltcallback-for-our-convenience

HAL_UART_Transmit_IT stores a pointer and length of the data buffer you provide. It doesn't perform a copy, so your buffer you passed needs to remain valid until callback gets called. For example it cannot be a buffer you'll perform delete [] / free on before callbacks happen or a buffer that's local in a function you're going to return from before a callback call.

So I have modified EwBspConsolePutCharacter to:

void EwBspConsolePutCharacter(char* aFrame,unsigned int aSize){
	static char buffer[256];
	
	// Assign to buffer the frame to be sent
	for(unsigned int i = 0; i<aSize;i++){
		buffer[i] = *(aFrame+i);
	}
	
	HAL_UART_Transmit_IT( &UART_Handle, (uint8_t*)&buffer,(uint16_t)aSize);
 
}

Now it's working as expected

1 reply

RCres.1
RCres.1AuthorBest answer
Associate III
March 20, 2020

FIXED:

As said in: https://stackoverflow.com/questions/55624428/can-we-edit-callback-function-hal-uart-txcpltcallback-for-our-convenience

HAL_UART_Transmit_IT stores a pointer and length of the data buffer you provide. It doesn't perform a copy, so your buffer you passed needs to remain valid until callback gets called. For example it cannot be a buffer you'll perform delete [] / free on before callbacks happen or a buffer that's local in a function you're going to return from before a callback call.

So I have modified EwBspConsolePutCharacter to:

void EwBspConsolePutCharacter(char* aFrame,unsigned int aSize){
	static char buffer[256];
	
	// Assign to buffer the frame to be sent
	for(unsigned int i = 0; i<aSize;i++){
		buffer[i] = *(aFrame+i);
	}
	
	HAL_UART_Transmit_IT( &UART_Handle, (uint8_t*)&buffer,(uint16_t)aSize);
 
}

Now it's working as expected