Screen working...finally
Thursday, November 15th, 2012 11:25 amI received my Teensy 3.0 controllers in the mail last week, so I could finally start on version 2 of my programmable right-hand gaming keypad (designed to sit under my right hand because my mouse is on the left - my right shoulder doesn't tolerate mouse movements). I got the real time clock working, then the sd card...but for the life of me I simply couldn't get the Tft screen to work.
I was using the UTFT library for arduino by Henning Karlsen, and the tft was specified as using the Himax HX8347-A controller. Given the UTFT library was written for AVR and the Teensy 3 has an ARM chip, I went through the library and adjusted datatypes (e.g. Paul Stoffregen had warned on the Teensy 3 kickstarter that shift operations on datatypes of different sizes could produce different results to those on AVR) to try to prevent any odd results from the change from 8-bit AVR to 32-bit ARM. I completely hacked the pin read/write routines when it still wasn't working, replacing the arduino register/bitmask usage with Teensy's digitalWriteFast(). When it still wouldn't work, I added Serial.print() monitoring for EVERY pin value change so I could trace exactly what was being sent to the tft screen, pulled out a logic probe and checked that the values at the connectors matched, tried plugging in the second screen in case one was faulty...still no display.
Then I sat down with the Himax datasheet and stepped through the entire init sequence, adjusting delays between commands to match the reference implementation (on the basis that ARM is faster than AVR so it may have been a timing issue - though the Serial.print debugging should have addressed that anyway). There were 2 init commands in the library where the value set was different to the reference implementation - I tried adjusting them to the reference but the display still would not turn on (so I set them back to the library's values). All I was trying to do was set the whole screen blue with a red box on it - something that was visible and distinct, but it just wouldn't work. At that point, I decided that I either needed to get an actual register command listing for the HX8437-A controller so I could inspect exactly what the init commands were doing...or give up and replace the screens. I could understand if one was faulty...but 2? That was not happy-making.
Then this morning I finally tripped over this blog post: http://mbed.org/cookbook/TFT-LCD-with-HX8347
In particular, the comment by Mathew
"looks like this library is actually for the SSD1289, judging by the init code. Luckily my LCD is an SSD1289. Funny thing is that the eBay auction I got it from claimed it to be a HX8347."
If that was the case...maybe the reason the screen wouldn't respond to the HX8347 init code was because it wasn't actually an HX8347 chip...
So I tried changing the chip type in the initialization of UTFT to SSD1289...and BINGO! First try - screen turned on, blue background was drawn, and (very slowly due to the debugging statements) the red box was drawn on top. So I took out the debug statements, and screen draw dropped to milliseconds instead of 5-8 seconds...

All that frustration/hair tearing out was due to an incorrect specification of the controller on the tft. Just in case someone else comes across it, if you bought a 3.2" tft from ebay claiming to use the HX8347-A controller and it just won't work, try treating it as the SSD1289 controller instead - it seems that some ebay sellers have been listing their tfts as having the wrong controller chip!
no subject
Date: 2012-11-15 02:24 am (UTC)Would you be able to give negative feedback to the seller you bought it from?
(icon love)
no subject
Date: 2012-11-15 02:40 am (UTC)It actually doesn't make the screen any less useful - both chips are pretty much the same in performance terms (although there's more documentation available for the controller that it ACTUALLY is than for the one they said it was) - provided you work out that sending it Himax commands isn't going to make it work. *lol*
Re: teensy 3.0 utft library
Date: 2013-02-07 11:03 am (UTC)I've uploaded my modified lib as a tar.gz and as a zip file:
http://www.dawnmist.net/arduino/UTFT.tar.gz
http://www.dawnmist.net/arduino/UTFT.zip
This is based on UTFT 1.3 from 03 Jun 2012 - I notice that there is now a new release, so I'll have to update it :). If you wanted to try to update to the newer UTFT library, you'd need the HW_Teensy3.h and HW_Teensy3_defines.h files, and the "#if defined(__MK20DX128__)" parts of UTFT.h and UTFT.cpp.
I've added a function to swap the foreground and background pixel colours (because I was using that a lot) - "invertColors()"
Pins for the data lines are:
Teensy3 : TFT pin 0 = D8 pin 1 = D9 pin 2 = D10 pin 3 = D11 pin 4 = D12 pin 5 = D13 pin 6 = D14 pin 7 = D15 if (mode == 16) { pin 25 = D0 pin 26 = D1 pin 27 = D2 pin 28 = D3 pin 29 = D4 pin 30 = D5 pin 31 = D6 pin 32 = D7 }I haven't written the serial interface bit - at this stage only 8 bit/16 bit TFTs have the data write code implemented. If you want to change those pin numbers, you'll need to edit HW_Teensy3.h.
I've got the control pins set to:
but since these are initialised by that UTFT call you can move them to whatever pins you'd prefer to use.
Re: teensy 3.0 utft library
Date: 2013-02-18 04:09 am (UTC)http://www.dawnmist.net/arduino/UTFT_2.0.1.zip
This discussion also moved across to the Teensy forums here:
http://forum.pjrc.com/threads/18002-Teensy-3-0-driving-an-SSD1289-with-utft
Cool setup!
Date: 2013-04-22 12:51 pm (UTC)Cool setup! Is your LCD a Sain-Smart? It looks like the same one I have.
Have you been able to get the touchscreen working?
Any chance of getting a schematic of your rig?
Re: Cool setup!
Date: 2013-04-23 01:25 am (UTC)I can get the touch screen working - but not on the SPI ports with the SD card access working at the same time. There's something odd that the SDFat library is doing when setting up SPI communications that means that every time I try to connect both through SPI and use the chip select pins to differentiate between the SD Card and the Touch the Teensy hangs...but the touch interface works fine if it's accessed via bit-banging the communication on the non-spi pins of the teensy.
As for schematic, in the state above it's fairly simple - pretty much direct wiring as per the photo above. Pin numbers that I've used for the screen are as per my previous comments, and the SDCard is hooked up to the SPI pins using CS0 (pin 10) as the chip select. The microSD card reader board is the one from PJRC, though I've also since connected the screen's SD card reader as well to the SPI using CS1 (pin 9) for the chip select. Since everything is pretty much direct-wiring, I'm not sure what you're after with a schematic...
Re: Cool setup!
Date: 2013-04-23 04:56 pm (UTC)I was wondering about a schematic since it's not possible to determine the wiring from the photo- especially the orange bundle in the foreground that runs under the board.
That's interesting using non-spi pins for the touch!
In any case I have a specific application in mind that requires LCD that displays images in raw, bmp, .c, RGB or other format from the SD, functional touch and 1 or more UARTs. I think Teensy 3 is fast enough but haven't been able to try it yet. I've been looking at a variety of platforms but there always seems to be hardware deficiencies or library incompatibilities. You seem to have solved most of these problems. Have you been able to display images? If so do you have any benchmarks? I don't know what you do but I need to find someone knowledgeable to work with. Would you be interested?
Re: Cool setup!
Date: 2013-04-24 01:49 am (UTC)The next entry in the blog here: http://dawnmist.dreamwidth.org/935.html has a video showing graphics (bmp) loading (lower right corner). The delays that were there so I could capture it were in between swapping keymap displays - for the displays, the keys are all drawn manually and a bitmap is used for the logo in the bottom right corner. It's entirely possible that the display can be much better optimised - this is currently using fairly simple methods to write out to the screen which haven't had any optimisation done over them (but they were working fast enough for what I wanted, that I probably wouldn't bother trying to optimise them further). The graphics and touch libraries I'd used are by Henning Karlsen - UTFT and uTouch, with some minor modifications to UTFT to set/write out to the data pins I was using. The original libraries are at http://henningkarlsen.com/electronics/library.php?id=51, and the modifications can be seen on the thread at the Teensy 3 forums (along with other people's advice/experiences) at http://forum.pjrc.com/threads/18002-Teensy-3-0-driving-an-SSD1289-with-utft.
For displaying graphics, you need to keep in mind that the Teensy 3 does NOT have enough RAM on-board to load a complete image file into RAM before displaying it on the screen (especially for 24-bit images - that's 320x240x3 bytes ~= 230kB, and the Teensy 3 only has 16kB RAM). My current image loading is set to 24-bit bmp files only, and reads in one line at a time, writes that to the LCD controller's memory, then reads in the next line. I investigated reading in and drawing one pixel at a time just out of curiosity - and there's significant time in sending the commands for where to draw it (I think there's 9 control bytes written each time you communicate before you actually send the 2-bytes-per-pixel colour data), so you need to work out a balance between how much data you can load into RAM to send in one communication to the screen and how many writes to the screen you want to make for each image to be displayed.
I must admit that I haven't taken things much further than you can see in the video - I tested the touch out when asked about it on the Teensy 3 forums, and discovered that it won't work together with the SDFat library on the SPI pins at this point. The thread with the discussion of that is here: http://forum.pjrc.com/threads/18337-TFT-on-Teensy-3-0-%28LCD-Controller-SSD1289-Touch-Screen-Controller-ADS7843%29.
I haven't done any further work on my Teensy 3 since that last post to the Touch screen thread above - I've been busy with other things. However, I have found a thread about using SPI on the Teensy on the forums that I was thinking of going back through and seeing if I could patch both SDFat and uTouch using the information from this thread so that they could work together - http://forum.pjrc.com/threads/1156-Teensy-3-SPI-Basic-Clock-Questions. For what I want to do, the only way I can use touch is through SPI - I'll be one pin short if I have to use touch on a different set of pins. For me touch isn't necessary though, so if I can't get it working through SPI I just won't use it.
As for helping with what you're doing - I'm not that experienced with microcontrollers, and while I can read/modify/bug-fix/tweak in several languages the majority of my programming experience is in Java. This was a personal project and tinker-at-home thing for me driven by a need for a gaming keyboard that was designed to complement using a mouse left-handed (instead of the normal right-handed mouse use), which is why I've used the Teensy - good documentation, USB keyboard HID profile built-in, libraries to do what I needed readily available, etc, so that what I needed to write was the glue-code/specifics of what I wanted rather than the detail of driving the hardware...and to patch the details in a few places to add additional functionality such as N-KRO over USB instead of the usual 6-KRO limitation. As such, I'd probably not be much use to you for your project...and as can be seen with how long it's been since I did much work on it, my time available to tinker with it is pretty limited too.
Re: Cool setup!
Date: 2013-04-25 04:40 pm (UTC)I, like you, will continue plodding along looking for the answers and share them here if successful.
Good luck with your other endeavors and!
How many and which PINs free?
Date: 2015-03-18 09:18 am (UTC)the teensy could be a Solution for my project. How many and which PINs are free after connecting the Display and the SD card reader?
Re: How many and which PINs free?
Date: 2015-03-19 10:44 am (UTC)There's 16 data lines (digital) + 5 control lines for the screen (also digital - and a 6th line that is connected to 3.3V to keep the screen in write mode), and 4 lines for the SD card. If you want to use the touch, there's either another 2 lines if you can get the SPI to share between the SD and the touch interface (I couldn't with the version of SDFat I was using), or 5 lines to talk to the touch.
That means you'll have up to 30 digital lines in use for the screen, SD and touch. 25 if you're not using the touch, and 27 if you manage to get touch and SDCard sharing the SPI port properly.
There's 34 digital input/outputs in total, so that leaves you with 4 available if you're using the touch as well on separate pins. There's also 4 analogue input ports and 1 analogue input/output port (which has a DAC to generate a "true" analogue signal) free to use.
If that's not going to be enough free, you can get alternative screens that don't need the 16 data lines. I think Adafruit have one that uses 8 data lines instead (plus a few control lines), and there are some screens that use SPI to communicate to them too.
For the 16-data-line screen using the UTFT library, the required pins are the first 8 pins of Port D, and 2 sections of 4 pins from Port B (documented at: http://forum.pjrc.com/threads/18002-Teensy-3-0-driving-an-SSD1289-with-utft?p=34719&viewfull=1#post34719 and http://www.dawnmist.net/teensy/HW_Teensy3.h - note that there is no longer any need to patch the UTFT library as the Port B/D variant has become an official part of the library).
* Port D (0-7) = DB_8=2, DB_9=14, DB_10=7, DB_11=8, DB_12=6, DB_13=20, DB_14=21, DB_15=5
* Port B (0-3,16-19) = DB_0=16, DB_1=17, DB_2=19, DB_3=18, DB_4=0, DB_5=1, DB_6=32, DB_7=25
If you want to be able to adjust the brightness, LED_BL needs to be connected to a PWM pin (I used pin 9). In the end, I found I just wanted it to be full brightness though, so it didn't really matter that much.
The SD card needs to use SD_DOUT(MISO)=12, SD_CLOCK=13, SD_DIN(MOSI)=11 if using the Teensy's native spi port. These would be the pins that would be shared with the Touch interface *if* you can get SPI sharing working properly (I think the SD library - as opposed to the SDFat library - has been adjusted so that it may work with SPI sharing now). The SD_CS (chip select) pin can be any of the other digital pins - I used pin 10 (which was the default CS0 for the SPI port).
The other pins are all free to make your own choice with.
Pin diagrams: http://www.pjrc.com/teensy/pinout.html
Good luck, and I hope you have fun with your project. :-)
Re: How many and which PINs free?
Date: 2015-03-19 11:39 am (UTC)thank you very much for this really all-encompassing informations!!!
With your informations I can work on my project. Thank you((-: