Thursday, October 25, 2012

STM32 LCD touch screen demo

[Update: Torrent available for downloading the Virtualbox image ]

You see many hobby projects use the good old 16x2 LCD.


Pretty easy to use. But limiting. No graphics. Limited text. Hard to have a very complicated UI. You need some other device for input - push buttons usually. Unintuitive.

In a word: ugly.

Most consumer devices these days include a graphical LCD and touch screen interface. People understand how to use them. People expect them.

So I started to get away from 16x2 LCDs in my projects.

Fortunately 320x240 LCDs with touch screen and micro controller can be had cheaply on ebay. So I have been using them for my projects.

I have a some friends who have started using these boards or expressed interest in using them. Now they are ARM based and can be tougher to bring up that an Arduino. So I have decided to make a downloadable VirtualBox image that has the toolchain and environment setup, with a demo project, that includes a simple menu system. This should help people get started with these boards.

Which boards?

ebay auctions expire all the time, so it's hard to link to specific boards. Instead take a look at a search:

http://www.ebay.com/sch/i.html?_nkw=STM32+tft+lcd

Look for the ones that are clearly 2 boards - the LCD board on top. CPU is usually a STM32F103.

The first sort of board has a 2.4" LCD:





The second has a 3.2" LCD:

Both LCDs are the same resolution, but I prefer the 3.2", it's substantially bigger (and so buttons will be bigger for fingers to hit).


However the board with the smaller LCDs have other goodies, like 16MB flash, speaker and RS232.


You can take a look at the schematics for each:





Either way, LCD + touch screen + STM32 CPU for less than some Arduinos cost.


STM32 CPUs are significantly more powerful than the trusty old Atmels (Arduinos).
 Your code may have a main task to accomplish, and then the LCD+touch UI to look after. Programming to bare metal, this can be tricky to pull off. Why not use a multi-tasking OS to help?

FreeRTOS to the rescue. Go read the 5 or so pages here for a good overview of how it works:

http://www.freertos.org/implementation/a00002.html


So then how to get started?

I have put together a VirtualBox image you can download that contains:

  • Ubuntu desktop
  • ARM toolchain (gcc + newlib)
  • Eclipse C++ IDE
  • stm32loader.py - for loading code into the board
  • openocd - for loading code and debugging
  • Example FreeRTOS project with all source code, include LCD+touch drivers and a simple menuing system to get you started quickly



 Demo





Getting started


Download the virtual box image: https://www.dropbox.com/s/rkpiq3wa1jfnof9/ubuntu_stm32.7z

https://thepiratebay.se/torrent/7803003

(~800MB download, ~3GB disk space uncompressed)

Login/password: ubuntu/reverse

Install virtualbox if you don't already have it: https://www.virtualbox.org/

You will need 7-zip to uncompress the image: http://www.7-zip.org/

Create a new virtual machine in the Virtual box manager. Give it a gig or ram or so. Select "Use existing hard disk", and choose the ubuntu image. Boot it up.

Building and running the demo


Demo menu code walk through



Loading code

The ARM Cortex M3 chips have a built in bootloader that allows you to load code over the serial interface.

http://www.micromouseonline.com/2009/05/08/stm32-arm-cortex-bootloader/

You need to boot the device with the boot0 pin grounded. Most dev boards have a switch or jumper.

This can become pretty tedious so it's worthwhile to get a JTAG programmer - you can then load your code faster, without touching the board. You can also run openocd and gdb to help debug your code.

I use and recommend a bus blaster:

http://dangerousprototypes.com/docs/Bus_Blaster



OpenOCD config files are included in the JTAG directory in the Virtalbox Image.

You will also probably need a 20 pin JTAG cable.

printf debugging


gdb & JTAG debugging

Boot, FreeRTOS and the LCD - how it comes together

50 comments:

  1. great post! found you from the dangerous prototypes page, bookmarked and I'll be trying out your image when I can get one of these dev boards..

    ReplyDelete
  2. How much more power do the touch screens use? And do they have an interrupt when someone touches it to wake it up so they can be powered down when not in use?

    ReplyDelete
    Replies
    1. Probably 100ma or so for the LCD backlight.

      I don't think it is on a separate circuit on these boards - but you could probably do a hack and put a transistor in to control it.

      Delete
  3. Really cool! But what about the Graphics Library to use?

    ReplyDelete
    Replies
    1. No library, just some pretty simple drawing primitives for boxes and fonts.

      Delete
  4. After reading some nice stuff in your article I really feel speechless,i want to learn about digital jukeboxes. Thanks for giving me such type of useful information..

    ReplyDelete
  5. Could you post/reply with the dimensions of the 2.4" LCD's PCB please?

    ReplyDelete
    Replies
    1. The LCD board itself is 65mm x 70mm. The CPU board is 103mm x 78mm

      Delete
  6. This is a great tutorial!
    Could you also post the code separately from the VirtualBox image, so we don't have to install VirtualBox to look at the code (I already have a toolchain set up for the STM32F103)....

    ReplyDelete
    Replies
    1. https://github.com/Zizzle/stm32_freertos_example

      Delete
  7. You guys might probably want to check out ChibiOS/GFX:

    http://chibios-gfx.com

    ReplyDelete
  8. If you are interested to build the toolchain in Linux from scratch there is a good tutorial for doing it http://kernel.ozandroid.info/?p=125

    ReplyDelete
  9. Hey can you post another link for the virtual box image. The dropbox link does not work anymore.

    ReplyDelete
  10. https://thepiratebay.se/torrent/7803003

    ReplyDelete
  11. Have you see ST Application Note AN3128

    http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/APPLICATION_NOTE/CD00259585.pdf

    It talks about a nice Graphic Object/Touchscreen Library. Do you think its possible to integrate that with this toolchain? It looks very interesting for providing advanced GUI design tools with minimal efforts.

    ReplyDelete
    Replies
    1. Not seen that. I expect you should be able to link it in with gcc.

      I tend to stay away from the closed source libraries.

      Delete
  12. I am on a MAC , when you say set up the virtual box with the existing HD it doesn't give an option for that. Plus I am understanding you want me to install ubuntu on the HD on my Mac then use the virtual box to run it? How does that work.
    I have never used either software before, so it's unfamiliar. Can you clarify please?
    Thanks.

    ReplyDelete
    Replies
    1. https://www.virtualbox.org/wiki/Downloads

      Once you install virtualbox you should be able to load the Ubuntu image in it.

      Then you boot Ubuntu in virtual box (i.e. ubuntu run in a window on your Mac, no need to install ubuntu).

      Delete
  13. Oh and is the bus pirate the same idea as the bus blaster? IS either better or worse?

    ReplyDelete
    Replies
    1. Bus pirate is more a swiss army knife designed to talk a lot of protocols.

      Bus blaster has more of a focus on JTAG only.

      The bus blaster is much faster at JTAG than the bus pirate.

      Delete
  14. Terrific article! I'm looking forward to trying it (I'm waiting for a 3.2" LCD ARM board to arrive from China).

    I have limited experience (Arduino & PCs) so much of what you're doing with JTAG, gdb, FreeRTOS etc. is new to me. I'm looking forward to learning. :) In the meantime, I have a question: I have some other ARM/LCD/camera boards from China bought some time ago. They don't have a JTAG connection. They do have a large 20-pin connector labelled GPIO. I'm also mildly familiar with FTDI which is used for chip access in the Arduino world.

    What I'm wondering is if you can offer any pointers for something to look at in gaining code access to boards like this? I realize my question is vague and don't expect a precise answer. But any advice from you as to directions I might look in would be valuable & I'd be grateful. Thanks.

    ReplyDelete
  15. Thanks for the info.

    As an FYI, it seems the 3.2" and 2.4" have the LCD wired up different, so your demo code will not run on the 3.2" version.

    ReplyDelete
  16. Thanks for your info on these china made boards.
    I have this board that I bought from ebay (http://www.soliddepot.com/index.php?main_page=product_info&cPath=45_3&products_id=1)
    and have tried to use your code but I am not having any luck I can upload the code but the LCD does not work and I have tried the printf ("Hello World"); and just get a garbled mess in my terminal.
    Could you tell me if this board should work with your code ?
    I am new to programming and eclipse and would appreciate any advice that you might have.

    ReplyDelete
  17. I am BRAND NEW to this but want to learn. SOme Arduino learning but fairly new as well. Can I upload your demo to the board? I don't have a cable for my JTAG programmer yet. (Bus Blaster) But can you upload the code using the usb cable? Please give me step y step. I am trying to follow the videos you made but I get lost right now. Thank you for all your work as well.

    ReplyDelete
    Replies
    1. Ok so after some screwing around I got it. It kept disconnecting. That demo is written in python?

      Delete
  18. Hi, I was simply checking out this blog and I really admire the premise of the article and this is really informative. I will for sure refer my friends the same. Thanks
    Touch Screen Jukeboxes

    ReplyDelete
  19. I have the ILI9331 Screen which came with a unit basically identical to the one shown in the video (with full RS232 etc) and 2.8" screen. Unfortunately this screen is not supported out of the box with this code, but I was able to make it work. I was able to load the code, however with a blank screen (but the LEDs would still toggle when pushing the correct part as per demo code). I eventually got the screen going, but it was upside down.

    From memory I only needed to modify power_SET and gamma_SET methods based on the code from the chinese CD that was provided with my board, you can find these functions linked to pastebin below.

    I would be interested if anyone else had a similar issue and found a work around...

    http://pastebin.com/XmpaT4JR

    ReplyDelete
  20. I have the 3.2" board, precisely as illustrated in the above picture, running the example code shown in the picture, but I have not been able to get the example code to run. I can download it ok, but the display is just completely blank.

    What might be necessary to do ? I have the CD with a mass of chinese documentation, but I have not been able to make sense of it without being able to experiment.

    ReplyDelete
  21. Does anyone else get a hard fault exception when attempting to create a second task? Even if the new task doesn't do anything (it's blank) the exception still occurs and crashes the board. Any suggestions?

    ReplyDelete
    Replies
    1. Are you using the newlib in the VM image?

      Delete
    2. I am using your image and basically uncommented the new task that you had added and then changed labelled to suit my task and added appropriate methods, basically copying the layout of touch task. I traced it to a hard fault by making a different LED toggle since most of the faults toggle the same LED. Im sure its something quite simple but i have no experience with these kind of boards, only AVR.

      Delete
    3. seems to work for me. In main.c I commented out the KEG task and changed it to vADCTask, also changed to xAdcTaskHandle at the top of prvSetupHardware( void ) also in main.c. in drivers/adc.c I added created vADCTask routine as this:
      void vADCTask( void *pvParameters ) {
      printf("ADC start\r\n");
      adc_init();
      for( ;; ) {
      int x,y;
      x = read_adc(ADC_Channel_0);
      y = read_adc(ADC_Channel_1);
      printf("x %d y %d\r\n", x, y);
      vTaskDelay( 500 );
      }
      }

      then added "void vADCTask( void *pvParameters ) ;" to the top of drivers/adc.h
      Compile, load, reset and watch the serial output show floating adc values. The ADC ports much be near the JTAG connector as putting fingers on the pins show ADC values jumping.

      Maybe something else causing problems and not task creation or scheduling?

      Delete
    4. Just figured out I need to read ADC channels 11 and 10 to see VR1 and VR2 POTs values. But the code still shows the running multiple tasks does work.

      Delete
  22. This comment has been removed by the author.

    ReplyDelete
  23. thanks for posting this blog. it got me to purchase a STM32 2.4" LCD board a while ago and I already had TI Stellaris Launchpads. I finally got working on it and I'm starting to learn FreeRTOS. The TI Stellaris Launchpad boards require a later version of the arm gcc compiler so I upgraded to arm-gcc v4.6_20121016. I now have both your demo(FreeRTOSv6.0.1) and the TI demo(FreeRTOSv7.02) building, loading and running on these boards under Ubuntu 12.04 via cmdline. Still no JTAG yet so no debugging but the Olimex unit is due in a few days.

    I looked at porting your example to a 7.x.x version of FreeRTOS but there are lots changed and files are all over the place so that'll have to wait until I learn more about FreeRTOS and these boards. The LCD/Touch screen runs less than $25 so maybe a Stellaris port is in my future. Thanks again for getting the wheels moving.

    ReplyDelete
  24. nice software i have tried this and its really good thnx for posing


    Kiosk solution | Lockdown Browser

    ReplyDelete
  25. Nice tutorial. I've tried it and I'm able to compile, flash and debug under Fedora16.

    But what about debugging code from RAM?
    Are you able to avoid flashing before debug?

    I tried to change some compiler defines and the linker script but the error 'region `SRAM' overflowed by 133684 bytes' occurs.

    Any idea?

    Thanks a lot

    Best,
    SM

    ReplyDelete
  26. Can you point me in the direction of a basic example of using the GPIO please? Ive watched all your videos and have decided to make the leap from 8 bit to 32 bit but need a tutorial which will show me basic useage of the GPIO, reading state, setting high etc.
    If you know also where I can see a tutorial on pwm, etc

    Lastly, I have a lot of code written in C++. What do i need to do to use RTOS under C++? If I just make .cpp files will everything compile correctly?
    Thanks

    ReplyDelete
  27. has this been tested with both 3.2 inch lcd and 2.8 inch? A comment earlier suggests it doesnt work with 3.2 inch?

    ReplyDelete
  28. Hi Matt,

    I really like the blog you made showing how to use your image for developing under an STM32. I purchased exactly the same board and i've been developing under your image for about two months now. I also purchased the Bus Blaster as you recommended, but i haven't been able to debug using it... I am following exaclty the same steps that you show in your videos but without any luck... I can upload code with Bus Blaster , but when i try to debug, the gdb opens , but instead of showing the code it says "No source Available"... Any ideas? Why didn't you use gdb with eclipse? It looks like a lot more user-friendly ? Is there a reason , or it's just a matter of taste? Thanks for your help!! :)

    Albert

    ReplyDelete
  29. Hi!
    I've tried this code, but it works in kinda' strange way. If I step through the code - even if just step over calls to display init - everything works and when I "step over" scheduler start everything is ok.
    But when I hit run straight at start or just have a breakpoint on menu_set_root(main_menu); - I get hardfault error ): Any ideas, what might be wrong?

    ReplyDelete
  30. This comment has been removed by the author.

    ReplyDelete
  31. Hi
    what is ubuntu virtual box image root password?
    thanks

    ReplyDelete
  32. I realise this is a bit long in the tooth now. Used this image a while back, had no issues. Gotten a slightly different board (Claims to be the same) but when doing make install0 it gets to Wriet 256 bytes at 0x8020000 and then fails NACK 0x31 Programming failed.

    ReplyDelete