cancel
Showing results for 
Search instead for 
Did you mean: 

problem with using ili9341 TFT LCD with stm32f103RE

salar1991
Associate II

I successfully used a HAL code I found on Github for driving an ili9341 TFT LCD with SPI then I rewrote the code with registers. Part of my code works and I am able to fill pixels with color but when I try to use a function named ILI9341_Draw_Colour_Burst the code doesn't work. I tried to use the Keil debugger to see what happens and it showed a hard fault occurred. The function code is shown below

void ILI9341_Draw_Colour_Burst(uint16_t Colour, uint32_t Size)
{
//SENDS COLOUR
uint32_t Buffer_Size = 0;
if((Size*2) < BURST_MAX_SIZE)
{
    Buffer_Size = Size;
}
else
{
    Buffer_Size = BURST_MAX_SIZE;
}
    
LCD_DC_PORT->BSRR |= LCD_DC_PIN;
LCD_CS_PORT->BRR |= LCD_CS_PIN;
 
unsigned char chifted =     Colour>>8;;
unsigned char burst_buffer[Buffer_Size];
for(uint32_t j = 0; j < Buffer_Size; j+=2)
    {
        burst_buffer[j] =   chifted;
        burst_buffer[j+1] = Colour;
    }
 
uint32_t Sending_Size = Size*2;
uint32_t Sending_in_Block = Sending_Size/Buffer_Size;
uint32_t Remainder_from_block = Sending_Size%Buffer_Size;
 
if(Sending_in_Block != 0)
{
    for(uint32_t j = 0; j < (Sending_in_Block); j++)
        {
        write_buffer32((unsigned char *)burst_buffer,Buffer_Size)   ;
        }
}
 
//REMAINDER!
    
write_buffer32((unsigned char *)burst_buffer,Remainder_from_block)  ;
    
LCD_CS_PORT->BSRR |= LCD_CS_PIN;
}

and the code I wrote for SPI is as follows:

    #define RXNE    0x01
#define TXE     0x02
#define BSY     0x80
 
 
void  SPIInit(void)
{ 
     
   RCC->APB2ENR |=  (1<<12);      /* Enable SPI1 clock   */
     GPIOA->CRL&=~0xFFF00000;
   GPIOA->CRL |=0xB4B00000;
     
  
  /* Enable SPI in Master Mode, CPOL=0, CPHA=0. */
  /* Clock speed = fPCLK1 / 256 = 280 kHz at 72 MHz PCLK1 clk. */
    SPI1->CR1  = (0 << 0) | // Clock phase
                 (0 << 1) | // Clock polarity 
               (1 << 2) | // Master selection
             (1 << 3) | // Baud rate control clock/2
                             (1 << 6) | // SPI enable
                             (0 << 7) | // Frame format : 0: MSB transmitted first
                             (1 << 8) | // Internal slave select
                             (1 << 9) | // Software slave management enabled
                             (0 << 11); // Data frame format :  0: 8-bit
  SPI1->CR2  = 0x0000;  
}
 
u8 SPI1_WriteByte(uint8_t TxData)
{       
  /* Loop while DR register in not emplty , 1: Tx buffer empty*/
   while ((SPI1->SR & TXE) == 0);
   /* Send byte through the SPI2 peripheral */
   SPI1->DR = TxData;
   /* Wait to receive a byte ,1: Rx buffer not empty*/
   while ((SPI1->SR & RXNE) == 0);
   /* Return the byte read from the SPI bus */
   return SPI1->DR;             
 
                
}
 
void write_buffer(unsigned char *data,u8 size)
{
  u8 i;
 
  for(i=0;i<size;i++) 
  SPI1_WriteByte(data[i]);
 
}
 
void write_buffer32(unsigned char *data,int size)
{
  int i;
 
  for(i=0;i<size;i++) 
  SPI1_WriteByte(data[i]);
 
}

what might be the problem?

1 ACCEPTED SOLUTION

Accepted Solutions
salar1991
Associate II

my problem is solved! I changed the BURST_MAX_SIZE value from 500 to 100 and the function worked! I'm still curious that what was the reason behind the hard fault

i attached an image of the hard fault error of the debugger.

0693W000007Zm7dQAC.png

View solution in original post

2 REPLIES 2
Ozone
Lead

I suppose you are aware of performance limits with a serial connection (SPI) and busy-waits.

I can't see an immediate problem, so I would tackle the problem from the other end.

What do the SCB fault registers say when the hardfault occurs ?

Can you switch to instruction step mode, and step to the critical sections ?

Register values will usually quickly reveal the problem.

salar1991
Associate II

my problem is solved! I changed the BURST_MAX_SIZE value from 500 to 100 and the function worked! I'm still curious that what was the reason behind the hard fault

i attached an image of the hard fault error of the debugger.

0693W000007Zm7dQAC.png