SPI for LCD controller, RS problem maybe

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

SPI for LCD controller, RS problem maybe

Post by Rick Burnett »

I'm trying to get an SPI LCD working on SPI2. I've used most of the information provided in the DEMO and I think I am getting closer. However, I am perplexed by this problem. I have 2 functions that writes to the SPI bus, the difference being whether a certain signal is asserted or not to tell the LCD if it's receiving a command or data. I've used PA8 for this and looking in the manual for the STM32F4, I do not see anything else using this port. These are my two functions for sending the data:

Code: Select all

void writeLCDCommand(uint8_t c)
{   palClearPad(GPIOA,8);
   chSysLockFromIsr();
   spiSelectI(&SPID2);
   // size is data size and samples are the samples pointer
   spiStartSendI(&SPID2, sizeof(c), &c);
   chSysUnlockFromIsr();
}

void writeLCDData(uint8_t c)
{   palSetPad(GPIOA,8);
   chSysLockFromIsr();
   spiSelectI(&SPID2);
   // size is data size and samples are the samples pointer
   spiStartSendI(&SPID2, sizeof(c), &c);
   chSysUnlockFromIsr();
}


They are identical other than what palSetPad is being set to. In my main function, I am setting up this port as follows:

Code: Select all

palSetPadMode(GPIOA, 8,  PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);


Not sure if I need the second flag or not, but I also set up GPIOA 15 to control the _RST line of the LCD, and it works no problem.

When I run my code, here are two sections:

Code: Select all

   // Software Reset
   writeLCDCommand(ST7735_SWRESET);
   chThdSleepMilliseconds(50);
   writeLCDCommand(ST7735_SLPOUT);
   chThdSleepMilliseconds(500);

  writeLCDCommand(ST7735_COLMOD);  // set color mode
  writeLCDData(0x05);        // 16-bit color
  chThdSleepMilliseconds(10);
  palSetPad(GPIOD, GPIOD_LED_RED); //Red


writeLCDCommand runs and passes through, originally I had the RED LED turn on after the first part of the code to make sure. However, in the second part writeLCDData does not return. If I comment out that line, it of course passes through and turns on the RED LED.

Clearly I am missing something here. Given how similar both commands are internally, I am at a loss.
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: SPI for LCD controller, RS problem maybe

Post by Rick Burnett »

So an interesting thing I noticed, if I put a chThreadSleepMilliseconds(1) between the writeLCDCommand and writeLCDData that it passes through the writeLCDData. Am I supposed to run something that waits for the SPI transfer to complete?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: SPI for LCD controller, RS problem maybe

Post by Giovanni »

Not sure, you should verify the timings specification on the controller datasheet. Probably Tectu knows more about LCD controllers, may be he can answer this.

Giovanni
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: SPI for LCD controller, RS problem maybe

Post by Rick Burnett »

I figured that might be part of the problem. When communicating over SPI, does it do some sort of handshake with the LCD? In that, if it's not getting the right information back then it can lock up that part of the code? I was trying to understand how to use gdb to know what is happening there but I really don't see a way.

I'll look at the LCD specs and see. Where do you set the speed of the SPI bus? I looked in the mcuconf.h and didn't see anything. I also looked through the documentation. No one I've found has made the ST7735R controller work with the STM32F4 AND ChibiOS that I have found. I did find some code for an older board, but not ChibiOS.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: SPI for LCD controller, RS problem maybe

Post by Giovanni »

The SPI speed is set in the driver configuration structure you pass to spiStart(), look at the examples under ./testhal. The dividers are described on the reference manual.

Giovanni
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: SPI for LCD controller, RS problem maybe

Post by Tectu »

This is a timing problem for sure (In case of your hardware wiring is correct). I don't know your LCD controller, but you should check the timings diagram as Giovanni suggested.
Also, if you know that your LCD controller does make some kind of handshaking (which I am pretty sure it does not) you might want to use spiExchange() instead.
You should do two things:
  • Lower your SPI speed
  • Make a delay after you set or cleared the RS line. I'd suggest 1ms for starting.

Also, are you going to port the driver to ChibiOS/GFX and contribute it? :D


~ Tectu
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: SPI for LCD controller, RS problem maybe

Post by Rick Burnett »

I wouldn't be opposed to porting it once I got it working, although my coding is not the cleanest! Like anything, jumping in to this is a bit overwhelming at points so I've got to just buckle down and get deeper.

The controller is the ST7735R, which is from this purchase:

http://www.ebay.com/itm/1-8-TFT-LCD-mod ... 3f10eb6dbd

I've been trying to read up on SPI as well.
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: SPI for LCD controller, RS problem maybe

Post by Rick Burnett »

Okay, going to work on lowering the SPI speed and adding the delay after the RS. Will report back with what I find.
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: SPI for LCD controller, RS problem maybe

Post by Tectu »

For contributing the driver to ChibiOS/GFX, just copy an existing one and clean everything up. If you're not into this stuff, I'd also be open to receive your source and port it myself. You would just need to test it afterwards.

~ Tectu
Rick Burnett
Posts: 150
Joined: Tue Jan 08, 2013 2:20 pm

Re: SPI for LCD controller, RS problem maybe

Post by Rick Burnett »

Still not working. One thing I notice with an Arudino driver is that SPI is set to mode 3. What is the default mode? Is this something I set also in the config. Digging through the files more.
Post Reply