cancel
Showing results for 
Search instead for 
Did you mean: 

Why does my UART gives a cannot access memory error

Jvan .10
Associate III

Hey everyone,

When using the default project generated my UART works fine and I can see the output on my Putty terminal. Now for my application I'm trying to move the UART object to another file.

UART.cpp:

class UART {
public:
	UART();
private:
	UART_HandleTypeDef huart3;
};

UART::UART(){
	huart3.Instance = USART3;
	huart3.Init.BaudRate = 115200;
	huart3.Init.WordLength = UART_WORDLENGTH_8B;
	huart3.Init.StopBits = UART_STOPBITS_1;
	huart3.Init.Parity = UART_PARITY_NONE;
	huart3.Init.Mode = UART_MODE_TX_RX;
	huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart3.Init.OverSampling = UART_OVERSAMPLING_16;
	huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
	huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
	huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
	HAL_UART_Init(&huart3);
        HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8);
	HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8);
	HAL_UARTEx_DisableFifoMode(&huart3);
}

A function within this class sends a message using the HAL transmit function:

void UART::printMenu(){
	uint8_t buff[4] = {3, 4, 5, 6};
	HAL_UART_Transmit(&huart3, buff, sizeof(buff), HAL_MAX_DELAY);
	char const *another = "Hello, world!\r\n";
	HAL_UART_Transmit(&huart3, (uint8_t*)another, 17, HAL_MAX_DELAY);
}

Using the debugger everything is going fine, except there is nohing to be seen on the Putty terminal anymore. I think I found the issue because it can't access the memory of the buffer;

0693W000008wUyXQAU.png 

Anyone knows how to fix this issue?

Edit: probably caused by the INIT function not working as expected

/*-------------------------- USART CR1 Configuration -----------------------*/
  /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  *  the UART Word Length, Parity, Mode and oversampling:
  *  set the M bits according to huart->Init.WordLength value
  *  set PCE and PS bits according to huart->Init.Parity value
  *  set TE and RE bits according to huart->Init.Mode value
  *  set OVER8 bit according to huart->Init.OverSampling value */
  tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);

While debugging I can see the CR1 is set to 12 for a fraction of a second. Only to be set to 0 again when it goes to the next line. Anyone knows why?

3 REPLIES 3

Check that structure is cleared properly.

Looks like you have invalid data, or it subsequently gets corrupted.​

Check startup.s clearing statics and running constructors properly.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I don't know how to check this. The file looks pretty advanced.

0693W000008wVGIQA2.pngThis does also look pretty strange

Jvan .10
Associate III

I found the problem.

Seems it's better to call GPIO init AFTER the UART init haha. /facepalm