afiskon / stm32-st7735

STM32 HAL-based library for ST7735 displays
https://eax.me/stm32-st7735/
MIT License
235 stars 65 forks source link

Out of buffer reading on STM32F042F6 and STM32F051K6 #3

Open vshumilov opened 5 years ago

vshumilov commented 5 years ago

Hello, I'm try to start library on STM32F042F6 with SPI 0.96" ST7735S. But hangup on ST7735_Init() -> ST7735_ExecuteCommandList(init_cmds1) -> ST7735_WriteData() -> HardFault_Handler()

Project created in STM32CubeIDE

JakubKajzer commented 4 years ago

I can confirm that this problem still exists, I'm trying to run code generated in STM32CubeIDE on STM32F051K6

JakubKajzer commented 4 years ago

The solution I found is to change in function _ST7735_ExecuteCommandList(init_cmds1)_ line:

ST7735_WriteData((uint8_t*)addr, numArgs);

to

ST7735_WriteData((uint8_t)addr, sizeof(addr));

It HardFault due to SPI reading out of buffer.

afiskon commented 4 years ago

Many thanks for reporting this issue!

Unfortunately I can't reproduce it because I don't have STM32F042F6 or STM32F051K6. I re-checked the code and init_cmds1 and pretty sure both are correct. The patch proposed by @JakubKajzer can't be used, because sizeof(*addr) is always 1. Also I don't see how it prevents reading out of buffer if the following code is addr += numArgs;

I strongly suspect that something may be wrong with HAL code for these particular MCUs, or maybe the hardware itself. For instance if HAL_SPI_Transmit reads one extra byte despite of arguments (but not necessary transmits it) this could explain the issue.

At the moment I can only suggest to A) make sure the code of HAL_SPI_Transmit doesn't do anything weird B) Try placing extra 0x00's in the end of init_cmds1.

JakubKajzer commented 4 years ago

Thanks for your reply! :)

I don't know how it prevents reading out of buffer too. But this patch worked for me. I can give additional info, that the hard fault occurs exactly at this line in _HAL_SPITRANSMIT()

hspi->Instance->DR = ((uint16_t )hspi->pTxBuffPtr);

When uC tries to run this line, it's hard fault.