Page 1 of 3

SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:00 pm
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.

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:17 pm
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?

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:19 pm
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

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:25 pm
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.

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:27 pm
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

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:34 pm
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

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:37 pm
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.

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:39 pm
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.

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 8:42 pm
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

Re: SPI for LCD controller, RS problem maybe

Posted: Sun Jan 13, 2013 9:58 pm
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.