Skip to content

Commit 72e36aa

Browse files
committed
Fix ILI9341 display driver with SPI connector
and provide a demo for it.
1 parent dba3ece commit 72e36aa

File tree

10 files changed

+528
-6
lines changed

10 files changed

+528
-6
lines changed

components/src/screen/ili9341/ili9341-device.adb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
------------------------------------------------------------------------------
22
-- --
3-
-- Copyright (C) 2015-2023, AdaCore --
3+
-- Copyright (C) 2015-2024, AdaCore --
44
-- --
55
-- Redistribution and use in source and binary forms, with or without --
66
-- modification, are permitted provided that the following conditions are --
@@ -117,7 +117,13 @@ package body ILI9341.Device is
117117
Time.Delay_Milliseconds (150);
118118

119119
Send_Command (Data, ILI9341_DISPLAY_ON, []);
120-
Send_Command (Data, ILI9341_GRAM, []);
120+
121+
case Connection is
122+
when RGB =>
123+
Send_Command (Data, ILI9341_GRAM, []);
124+
when Serial | Parallel =>
125+
null;
126+
end case;
121127
end Initialize;
122128

123129
---------------------

components/src/screen/ili9341/ili9341-spi_connector.adb

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
------------------------------------------------------------------------------
22
-- --
3-
-- Copyright (C) 2015-2023, AdaCore --
3+
-- Copyright (C) 2015-2024, AdaCore --
44
-- --
55
-- Redistribution and use in source and binary forms, with or without --
66
-- modification, are permitted provided that the following conditions are --
@@ -29,8 +29,45 @@
2929
-- --
3030
------------------------------------------------------------------------------
3131

32+
with Bitmap_Color_Conversion;
33+
34+
with ILI9341.Regs;
35+
3236
package body ILI9341.SPI_Connector is
3337

38+
-----------------
39+
-- Read_Pixels --
40+
-----------------
41+
42+
procedure Read_Pixels
43+
(This : ILI9341_Connector;
44+
Cmd : HAL.UInt8;
45+
Address : System.Address;
46+
Count : Positive)
47+
is
48+
use all type HAL.SPI.SPI_Status;
49+
50+
subtype Raw_Buffer is HAL.SPI.SPI_Data_8b (1 .. 3 * Count);
51+
52+
Raw_Pixels : Raw_Buffer
53+
with Import, Address => Address;
54+
55+
Status : HAL.SPI.SPI_Status;
56+
begin
57+
This.WRX.Clear;
58+
This.Chip_Select.Clear;
59+
This.Port.Transmit (HAL.SPI.SPI_Data_8b'(1 => Cmd), Status);
60+
61+
if Status /= Ok then
62+
raise Program_Error;
63+
end if;
64+
65+
This.WRX.Set;
66+
This.Port.Receive (Raw_Pixels, Status);
67+
68+
This.Chip_Select.Set;
69+
end Read_Pixels;
70+
3471
------------------
3572
-- Send_Command --
3673
------------------
@@ -41,6 +78,7 @@ package body ILI9341.SPI_Connector is
4178
Data : HAL.UInt8_Array)
4279
is
4380
use HAL.SPI;
81+
4482
Status : SPI_Status;
4583
begin
4684
This.WRX.Clear;
@@ -60,7 +98,126 @@ package body ILI9341.SPI_Connector is
6098
end if;
6199
end if;
62100

63-
This.Chip_Select.Set;
101+
if Cmd not in ILI9341.Regs.ILI9341_GRAM then
102+
This.Chip_Select.Set;
103+
end if;
64104
end Send_Command;
65105

106+
------------------
107+
-- Write_Pixels --
108+
------------------
109+
110+
procedure Write_Pixels
111+
(This : ILI9341_Connector;
112+
Mode : HAL.Bitmap.Bitmap_Color_Mode;
113+
Address : System.Address;
114+
Count : Positive;
115+
Repeat : Positive)
116+
is
117+
118+
procedure Transmit (Color : HAL.Bitmap.Bitmap_Color);
119+
-- Transmit RGB on a single pixel
120+
121+
--------------
122+
-- Transmit --
123+
--------------
124+
125+
procedure Transmit (Color : HAL.Bitmap.Bitmap_Color) is
126+
Status : HAL.SPI.SPI_Status;
127+
128+
Pixel : HAL.SPI.SPI_Data_8b (1 .. 4)
129+
with Import, Address => Color'Address;
130+
begin
131+
This.Port.Transmit (Pixel (1 .. 3), Status);
132+
end Transmit;
133+
134+
begin
135+
This.WRX.Set;
136+
137+
case Mode is
138+
when HAL.Bitmap.RGB_888 =>
139+
-- Native format for SPI interface
140+
declare
141+
subtype Raw_Buffer is HAL.SPI.SPI_Data_8b (1 .. 3 * Count);
142+
143+
Raw_Pixels : Raw_Buffer
144+
with Import, Address => Address;
145+
146+
Status : HAL.SPI.SPI_Status;
147+
begin
148+
for Step in 1 .. Repeat loop
149+
This.Port.Transmit (Raw_Pixels, Status);
150+
end loop;
151+
end;
152+
153+
when HAL.Bitmap.ARGB_8888 =>
154+
declare
155+
subtype Raw_Buffer is HAL.UInt32_Array (1 .. Count);
156+
157+
Raw_Pixels : Raw_Buffer
158+
with Import, Address => Address;
159+
begin
160+
for Step in 1 .. Repeat loop
161+
for Raw of Raw_Pixels loop
162+
declare
163+
Color : constant HAL.Bitmap.Bitmap_Color :=
164+
Bitmap_Color_Conversion.Word_To_Bitmap_Color
165+
(Mode, Raw);
166+
begin
167+
Transmit (Color);
168+
end;
169+
end loop;
170+
end loop;
171+
end;
172+
173+
when HAL.Bitmap.RGB_565 |
174+
HAL.Bitmap.ARGB_1555 |
175+
HAL.Bitmap.ARGB_4444 |
176+
HAL.Bitmap.AL_88 =>
177+
declare
178+
subtype Raw_Buffer is HAL.UInt16_Array (1 .. Count);
179+
180+
Raw_Pixels : Raw_Buffer
181+
with Import, Address => Address;
182+
begin
183+
for Step in 1 .. Repeat loop
184+
for Raw of Raw_Pixels loop
185+
declare
186+
Color : constant HAL.Bitmap.Bitmap_Color :=
187+
Bitmap_Color_Conversion.Word_To_Bitmap_Color
188+
(Mode, HAL.UInt32 (Raw));
189+
begin
190+
Transmit (Color);
191+
end;
192+
end loop;
193+
end loop;
194+
end;
195+
196+
when HAL.Bitmap.L_8 | HAL.Bitmap.AL_44 =>
197+
declare
198+
subtype Raw_Buffer is HAL.UInt8_Array (1 .. Count);
199+
200+
Raw_Pixels : Raw_Buffer
201+
with Import, Address => Address;
202+
begin
203+
for Step in 1 .. Repeat loop
204+
for Raw of Raw_Pixels loop
205+
declare
206+
Color : constant HAL.Bitmap.Bitmap_Color :=
207+
Bitmap_Color_Conversion.Word_To_Bitmap_Color
208+
(Mode, HAL.UInt32 (Raw));
209+
begin
210+
Transmit (Color);
211+
end;
212+
end loop;
213+
end loop;
214+
end;
215+
216+
when others =>
217+
raise Program_Error;
218+
end case;
219+
220+
This.Chip_Select.Set;
221+
end Write_Pixels;
222+
66223
end ILI9341.SPI_Connector;

components/src/screen/ili9341/ili9341-spi_connector.ads

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
------------------------------------------------------------------------------
22
-- --
3-
-- Copyright (C) 2015-2023, AdaCore --
3+
-- Copyright (C) 2015-2024, AdaCore --
44
-- --
55
-- Redistribution and use in source and binary forms, with or without --
66
-- modification, are permitted provided that the following conditions are --
@@ -29,8 +29,11 @@
2929
-- --
3030
------------------------------------------------------------------------------
3131

32-
with HAL.SPI;
32+
with System;
33+
34+
with HAL.Bitmap;
3335
with HAL.GPIO;
36+
with HAL.SPI;
3437

3538
package ILI9341.SPI_Connector is
3639

@@ -47,4 +50,17 @@ package ILI9341.SPI_Connector is
4750
Data : HAL.UInt8_Array);
4851
-- Send ILI9341 command over SPI
4952

53+
procedure Write_Pixels
54+
(This : ILI9341_Connector;
55+
Mode : HAL.Bitmap.Bitmap_Color_Mode;
56+
Address : System.Address;
57+
Count : Positive;
58+
Repeat : Positive);
59+
60+
procedure Read_Pixels
61+
(This : ILI9341_Connector;
62+
Cmd : HAL.UInt8;
63+
Address : System.Address;
64+
Count : Positive);
65+
5066
end ILI9341.SPI_Connector;

examples/stm32_f4ve/lcd_spi/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# LCD demo
2+
3+
This example demonstrates working with the LCD using the
4+
HAL.Bitmap.Bitmap_Buffer interface.
5+
Colorful rectangles are displayed on the screen, and their
6+
colors change based on the X and Y coordinates.
7+
8+
For this demo, you need an LCD, (which is usually sold as
9+
part of the STM32 F4VE board kit) connected as SPI:
10+
11+
* PA1 LCD RESET
12+
* PA2 LCD D/C
13+
* PA3 LCD BKL
14+
* PA4 SPI1 NSS
15+
* PA5 SPI1 SCK
16+
* PA6 SPI1 MISO
17+
* PA7 SPI1 MOSI
18+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
with "stm32_f4ve_full_spi.gpr";
2+
3+
project LCD_SPI is
4+
5+
for Runtime ("Ada") use STM32_f4VE_Full'Runtime ("Ada");
6+
for Target use "arm-eabi";
7+
for Main use ("main.adb");
8+
for Languages use ("Ada");
9+
for Source_Dirs use (".");
10+
for Object_Dir use "obj/";
11+
for Create_Missing_Dirs use "True";
12+
13+
package Compiler renames STM32_F4VE_Full.Compiler;
14+
15+
package Ide is
16+
for Program_Host use "localhost:4242";
17+
for Communication_Protocol use "remote";
18+
for Connection_Tool use "st-util";
19+
end Ide;
20+
end LCD_SPI;

examples/stm32_f4ve/lcd_spi/main.adb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2024, AdaCore --
4+
-- --
5+
-- Redistribution and use in source and binary forms, with or without --
6+
-- modification, are permitted provided that the following conditions are --
7+
-- met: --
8+
-- 1. Redistributions of source code must retain the above copyright --
9+
-- notice, this list of conditions and the following disclaimer. --
10+
-- 2. Redistributions in binary form must reproduce the above copyright --
11+
-- notice, this list of conditions and the following disclaimer in --
12+
-- the documentation and/or other materials provided with the --
13+
-- distribution. --
14+
-- 3. Neither the name of the copyright holder nor the names of its --
15+
-- contributors may be used to endorse or promote products derived --
16+
-- from this software without specific prior written permission. --
17+
-- --
18+
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
19+
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
20+
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
21+
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
22+
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
23+
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
24+
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
25+
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
26+
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
27+
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
28+
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
29+
-- --
30+
------------------------------------------------------------------------------
31+
32+
with Ada.Real_Time;
33+
34+
with HAL.Bitmap;
35+
36+
with STM32.Board;
37+
with Display_ILI9341;
38+
39+
procedure Main is
40+
use type Ada.Real_Time.Time;
41+
42+
Next : Ada.Real_Time.Time := Ada.Real_Time.Clock;
43+
44+
Bitmap : Display_ILI9341.Bitmap_Buffer renames STM32.Board.TFT_Bitmap;
45+
46+
begin
47+
-- STM32.Board.Initialize_LEDs;
48+
STM32.Board.Display.Initialize;
49+
50+
Bitmap.Set_Source (HAL.Bitmap.Black);
51+
Bitmap.Fill;
52+
53+
for X in 0 .. 31 loop
54+
for Y in 0 .. 31 loop
55+
Bitmap.Set_Source
56+
((Alpha => 255,
57+
Green => 128,
58+
Red => HAL.UInt8 (X * 8),
59+
Blue => HAL.UInt8 (Y * 8)));
60+
61+
Bitmap.Fill_Rect (((X * 7, Y * 10), 6, 9));
62+
end loop;
63+
end loop;
64+
65+
loop
66+
-- STM32.Board.Toggle (STM32.Board.D1_LED);
67+
68+
Next := Next + Ada.Real_Time.Milliseconds (500);
69+
delay until Next;
70+
end loop;
71+
end Main;

0 commit comments

Comments
 (0)