Difference between revisions of "EBC Exercise 12a 2.4 TFT LCD display via SPI"

From eLinux.org
Jump to: navigation, search
m (The Sodtware: Added)
m (The Hardware: Added SPI 1)
(24 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
[[File:Ili9341.png|thumb|2.4" TFT LCD]]
 
[[File:Ili9341.png|thumb|2.4" TFT LCD]]
  
This is an exercise in interfacing the 2.4" TFT LCD display (ili9341) via a SPI bus.
+
This is an exercise in interfacing the 2.4" TFT LCD display (ili9341) via a SPI bus. The newer using the '''tinyDRM'''
 +
driver and the older uses '''fbtft'''.
  
 
== The Hardware ==
 
== The Hardware ==
  
Wire the LCD display as shown in the table.
+
This table shows how to wire the LCD display for SPI 0.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 34: Line 35:
 
|}
 
|}
  
== The Software ==
 
  
Clone the course repo.
+
This table shows how to wire the LCD display for SPI 1.
 +
 
 +
{| class="wikitable"
 +
|-
 +
! LCD !! Bone
 +
|-
 +
| MISO    || P9_29
 +
|-
 +
| LED    || P9_16
 +
|-
 +
| SCK    || P9_31
 +
|-
 +
| MOSI    || P9_30
 +
|-
 +
| D/C    || P9_27
 +
|-
 +
| RESET  || P9_25
 +
|-
 +
| CS      || P9_28
 +
|-
 +
| GND    || P9_2
 +
|-
 +
| VCC    || P9_4
 +
|}
 +
 
 +
== tinyDRM ==
 +
 
 +
The tinyDRM driver requires a 4.19 kernel or newer.  Run '''uname -a''' to see which kernel is being run.
 +
If it isn't new enough run:
  
  bone$ '''git clone https://github.com/MarkAYoder/BeagleBoard-exercises.git exercises'''
+
  bone$ '''cd /opt/scripts/tools'''
 +
bone$ '''sudo ./update_kernel.sh --lts-4_19'''
 +
bone$ '''reboot'''
  
and then
+
Find the device trees:
  
  bone$ '''cd exercises/display/ili9341/fb'''
+
  bone$ '''cd /opt/source/bb.org-overlays'''
 
  bone$ '''git pull'''
 
  bone$ '''git pull'''
  bone$ '''./on.sh'''
+
  bone$ '''ls src/arm/BB-LCD*'''
 +
src/arm/BB-LCD-ADAFRUIT-24-SPI0-00A0.dts src/arm/BB-LCD-ADAFRUIT-18-SPI1-00A0.dts 
 +
src/arm/BB-LCD-ADAFRUIT-24-SPI1-00A0.dts
 +
 
 +
'''BB-LCD-ADAFRUIT-24-SPI1-00A0.dts''' is the device driver for the LCD attached to SPI 1.
 +
 
 +
Compile it via Makefile.  All the device trees will be compiled.
 +
bone$ '''make'''
 +
...
 +
  DTC    src/arm/BEAGLEBOY-0013.dtbo
 +
  DTC    '''src/arm/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo'''
 +
  DTC    src/arm/PB-USB0-HOST.dtbo
 +
...
 +
bone$ '''sudo cp src/arm/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo /lib/firmware/'''
 +
 
 +
Add uboot_overlay_addr4=/lib/firmware/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo to /boot/uEnv.txt
 +
 
 +
Now edit '''/boot/uEnv.txt''' and find the line starting with
 +
#uboot_overlay_addr4=/lib/firmware
 +
Uncomment it and change it to:
 +
uboot_overlay_addr4=/lib/firmware/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo
 +
Then reboot and check of '''/dev/fb0'''
 +
bone$ '''sudo reboot'''
 +
After rebooting...
 +
bone$ ls -ls /dev/fb0
 +
0 crw-rw---- 1 root video 29, 0 Oct  3 10:07 /dev/fb0
 +
 
 +
The LCD should be showing some text.
 +
 
 +
You can control the backlight with:
 +
bone$ '''cd /sys/class/backlight/backlight_pwm'''
 +
Turn the display off, then on with: (Yup, it's backwards.)
 +
bone$ '''echo 1 > bl_power'''                       
 +
bone$ '''echo 0 > bl_power'''
 +
Change the brightness to 25% with:
 +
bone$ '''echo 25 > brightness'''
 +
 
 +
Now jump to the Using the Frame Buffer section to see how to display images, etc..
  
After a moment the display should turn dark.  Look in '''on.sh''' to see what it did.
+
== Using the framebuffer ==
  
 
=== Displaying Images ===
 
=== Displaying Images ===
 
Follow the instructions in '''install.sh''' to download '''fbi''' and some images.
 
Follow the instructions in '''install.sh''' to download '''fbi''' and some images.
 +
bone$ '''sudo apt update'''
 
  bone$ '''sudo apt install fbi'''
 
  bone$ '''sudo apt install fbi'''
 
Display them with:
 
Display them with:
  bone$ '''fbi -noverbose -T 1 -a boris.png'''
+
  bone$ '''sudo fbi -noverbose -T 1 -a boris.png'''
You should now see Boris the Beagle on your display
+
You should now see Boris the Beagle on your display.
Look in '''on.sh''' and figure out how to rotate the display.  Rotate Boris 90°.
 
  
 
=== Playing Movies ===
 
=== Playing Movies ===
 
Install '''mplayer''' and load a movie on the Bone (see install.sh).   
 
Install '''mplayer''' and load a movie on the Bone (see install.sh).   
 
  bone$ '''sudo apt install mplayer'''
 
  bone$ '''sudo apt install mplayer'''
 +
Get something to play
 +
bone$ '''wget http://hubblesource.stsci.edu/sources/video/clips/details/images/hst_1.mpg'''
 +
And play it
 +
bone$ '''mplayer -vo fbdev2 -nolirc -framedrop hst_1.mpg'''
 +
 +
If you get an error you might need:
 +
bone$ '''export SDL_VIDEODRIVER=fbcon'''
 +
bone$ '''export SDL_FBDEV=/dev/fb0'''
  
 
Play the movie.  Rotate the movie.
 
Play the movie.  Rotate the movie.
Line 70: Line 145:
 
Display an image and write some text on it.
 
Display an image and write some text on it.
  
 +
# Here's how to use imagemagick to display text
 +
# Make a blank image
 +
SIZE=320x240
 +
TMP_FILE=/tmp/frame.png
 +
 +
# From: http://www.imagemagick.org/Usage/text/
 +
convert -background lightblue -fill blue -font Times-Roman -pointsize 24 \
 +
      -size $SIZE \
 +
      label:'ImageMagick\nExamples\nby Anthony' \
 +
      -draw "text 0,200 'Bottom of Display'" \
 +
      $TMP_FILE
 +
 +
sudo fbi -noverbose -T 1 $TMP_FILE
 +
 +
=== pygame ===
 +
It's  not hard to get pygame to display on the LCD.
 +
 +
bone$ '''sudo apt install pygame'''
 +
bone$ '''sudo pip3 install requests pillow'''
 +
bone$ '''cd exercises/displays/ili9341/fb/pygame'''
 +
bone$ '''sudo ./clock.py'''
 +
 +
Look in '''clock.py''' to see how to direct the graphics to the LCD.
 +
 +
== fbtft ==
 +
 +
fbtft is no longer supported after the 5.4 kernel.
 +
(https://github.com/notro/fbtft/wiki#fbtft_device-and-flexfb-are-gone-in-54)
 +
 +
Clone the course repo.
 +
 +
bone$ '''git clone https://github.com/MarkAYoder/BeagleBoard-exercises.git exercises'''
 +
 +
and then
 +
 +
bone$ '''cd exercises/display/ili9341/fb'''
 +
bone$ '''git pull'''
 +
bone$ '''./on.sh'''
 +
 +
After a moment the display should turn dark.  Look in '''on.sh''' to see what it did.
 +
If the display doesn't turn on, look in '''on.sh''' and make sure it's set for the correct SPI bus.
  
 
{{YoderFoot}}
 
{{YoderFoot}}

Revision as of 06:07, 8 October 2021

thumb‎ Embedded Linux Class by Mark A. Yoder


2.4" TFT LCD

This is an exercise in interfacing the 2.4" TFT LCD display (ili9341) via a SPI bus. The newer using the tinyDRM driver and the older uses fbtft.

The Hardware

This table shows how to wire the LCD display for SPI 0.

LCD Bone
MISO P9_21
LED P9_16
SCK P9_22
MOSI P9_18
D/C P9_19
RESET P9_20
CS P9_17
GND P9_2
VCC P9_4


This table shows how to wire the LCD display for SPI 1.

LCD Bone
MISO P9_29
LED P9_16
SCK P9_31
MOSI P9_30
D/C P9_27
RESET P9_25
CS P9_28
GND P9_2
VCC P9_4

tinyDRM

The tinyDRM driver requires a 4.19 kernel or newer. Run uname -a to see which kernel is being run. If it isn't new enough run:

bone$ cd /opt/scripts/tools
bone$ sudo ./update_kernel.sh --lts-4_19
bone$ reboot

Find the device trees:

bone$ cd /opt/source/bb.org-overlays
bone$ git pull
bone$ ls src/arm/BB-LCD*
src/arm/BB-LCD-ADAFRUIT-24-SPI0-00A0.dts src/arm/BB-LCD-ADAFRUIT-18-SPI1-00A0.dts   
src/arm/BB-LCD-ADAFRUIT-24-SPI1-00A0.dts

BB-LCD-ADAFRUIT-24-SPI1-00A0.dts is the device driver for the LCD attached to SPI 1.

Compile it via Makefile. All the device trees will be compiled.

bone$ make
...
  DTC     src/arm/BEAGLEBOY-0013.dtbo
  DTC     src/arm/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo
  DTC     src/arm/PB-USB0-HOST.dtbo
...
bone$ sudo cp src/arm/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo /lib/firmware/

Add uboot_overlay_addr4=/lib/firmware/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo to /boot/uEnv.txt

Now edit /boot/uEnv.txt and find the line starting with

#uboot_overlay_addr4=/lib/firmware

Uncomment it and change it to:

uboot_overlay_addr4=/lib/firmware/BB-LCD-ADAFRUIT-24-SPI1-00A0.dtbo

Then reboot and check of /dev/fb0

bone$ sudo reboot

After rebooting...

bone$ ls -ls /dev/fb0
0 crw-rw---- 1 root video 29, 0 Oct  3 10:07 /dev/fb0

The LCD should be showing some text.

You can control the backlight with:

bone$ cd /sys/class/backlight/backlight_pwm

Turn the display off, then on with: (Yup, it's backwards.)

bone$ echo 1 > bl_power                        
bone$ echo 0 > bl_power

Change the brightness to 25% with:

bone$ echo 25 > brightness

Now jump to the Using the Frame Buffer section to see how to display images, etc..

Using the framebuffer

Displaying Images

Follow the instructions in install.sh to download fbi and some images.

bone$ sudo apt update
bone$ sudo apt install fbi

Display them with:

bone$ sudo fbi -noverbose -T 1 -a boris.png

You should now see Boris the Beagle on your display.

Playing Movies

Install mplayer and load a movie on the Bone (see install.sh).

bone$ sudo apt install mplayer

Get something to play

bone$ wget http://hubblesource.stsci.edu/sources/video/clips/details/images/hst_1.mpg

And play it

bone$ mplayer -vo fbdev2 -nolirc -framedrop hst_1.mpg

If you get an error you might need:

bone$ export SDL_VIDEODRIVER=fbcon 
bone$ export SDL_FBDEV=/dev/fb0

Play the movie. Rotate the movie.

Generate Text

Install imagemagick.

bone$ sudo apt install imagemagick

See text.sh for an example of using imagemagick to write text to the LCD display. Write your name on the LCD. Display an image and write some text on it.

# Here's how to use imagemagick to display text
# Make a blank image
SIZE=320x240
TMP_FILE=/tmp/frame.png

# From: http://www.imagemagick.org/Usage/text/
convert -background lightblue -fill blue -font Times-Roman -pointsize 24 \
     -size $SIZE \
     label:'ImageMagick\nExamples\nby Anthony' \
     -draw "text 0,200 'Bottom of Display'" \
     $TMP_FILE

sudo fbi -noverbose -T 1 $TMP_FILE

pygame

It's not hard to get pygame to display on the LCD.

bone$ sudo apt install pygame
bone$ sudo pip3 install requests pillow
bone$ cd exercises/displays/ili9341/fb/pygame
bone$ sudo ./clock.py

Look in clock.py to see how to direct the graphics to the LCD.

fbtft

fbtft is no longer supported after the 5.4 kernel. (https://github.com/notro/fbtft/wiki#fbtft_device-and-flexfb-are-gone-in-54)

Clone the course repo.

bone$ git clone https://github.com/MarkAYoder/BeagleBoard-exercises.git exercises

and then

bone$ cd exercises/display/ili9341/fb
bone$ git pull
bone$ ./on.sh

After a moment the display should turn dark. Look in on.sh to see what it did. If the display doesn't turn on, look in on.sh and make sure it's set for the correct SPI bus.




thumb‎ Embedded Linux Class by Mark A. Yoder