Skip to content

Fix ILI9341 display driver with SPI connector #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions components/src/screen/ili9341/ili9341-device.adb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
------------------------------------------------------------------------------
-- --
-- Copyright (C) 2015-2023, AdaCore --
-- Copyright (C) 2015-2024, AdaCore --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
Expand Down Expand Up @@ -117,7 +117,13 @@ package body ILI9341.Device is
Time.Delay_Milliseconds (150);

Send_Command (Data, ILI9341_DISPLAY_ON, []);
Send_Command (Data, ILI9341_GRAM, []);

case Connection is
when RGB =>
Send_Command (Data, ILI9341_GRAM, []);
when Serial | Parallel =>
null;
end case;
end Initialize;

---------------------
Expand Down
161 changes: 159 additions & 2 deletions components/src/screen/ili9341/ili9341-spi_connector.adb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
------------------------------------------------------------------------------
-- --
-- Copyright (C) 2015-2023, AdaCore --
-- Copyright (C) 2015-2024, AdaCore --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
Expand Down Expand Up @@ -29,8 +29,45 @@
-- --
------------------------------------------------------------------------------

with Bitmap_Color_Conversion;

with ILI9341.Regs;

package body ILI9341.SPI_Connector is

-----------------
-- Read_Pixels --
-----------------

procedure Read_Pixels
(This : ILI9341_Connector;
Cmd : HAL.UInt8;
Address : System.Address;
Count : Positive)
is
use all type HAL.SPI.SPI_Status;

subtype Raw_Buffer is HAL.SPI.SPI_Data_8b (1 .. 3 * Count);

Raw_Pixels : Raw_Buffer
with Import, Address => Address;

Status : HAL.SPI.SPI_Status;
begin
This.WRX.Clear;
This.Chip_Select.Clear;
This.Port.Transmit (HAL.SPI.SPI_Data_8b'(1 => Cmd), Status);

if Status /= Ok then
raise Program_Error;
end if;

This.WRX.Set;
This.Port.Receive (Raw_Pixels, Status);

This.Chip_Select.Set;
end Read_Pixels;

------------------
-- Send_Command --
------------------
Expand All @@ -41,6 +78,7 @@ package body ILI9341.SPI_Connector is
Data : HAL.UInt8_Array)
is
use HAL.SPI;

Status : SPI_Status;
begin
This.WRX.Clear;
Expand All @@ -60,7 +98,126 @@ package body ILI9341.SPI_Connector is
end if;
end if;

This.Chip_Select.Set;
if Cmd not in ILI9341.Regs.ILI9341_GRAM then
This.Chip_Select.Set;
end if;
end Send_Command;

------------------
-- Write_Pixels --
------------------

procedure Write_Pixels
(This : ILI9341_Connector;
Mode : HAL.Bitmap.Bitmap_Color_Mode;
Address : System.Address;
Count : Positive;
Repeat : Positive)
is

procedure Transmit (Color : HAL.Bitmap.Bitmap_Color);
-- Transmit RGB on a single pixel

--------------
-- Transmit --
--------------

procedure Transmit (Color : HAL.Bitmap.Bitmap_Color) is
Status : HAL.SPI.SPI_Status;

Pixel : HAL.SPI.SPI_Data_8b (1 .. 4)
with Import, Address => Color'Address;
begin
This.Port.Transmit (Pixel (1 .. 3), Status);
end Transmit;

begin
This.WRX.Set;

case Mode is
when HAL.Bitmap.RGB_888 =>
-- Native format for SPI interface
declare
subtype Raw_Buffer is HAL.SPI.SPI_Data_8b (1 .. 3 * Count);

Raw_Pixels : Raw_Buffer
with Import, Address => Address;

Status : HAL.SPI.SPI_Status;
begin
for Step in 1 .. Repeat loop
This.Port.Transmit (Raw_Pixels, Status);
end loop;
end;

when HAL.Bitmap.ARGB_8888 =>
declare
subtype Raw_Buffer is HAL.UInt32_Array (1 .. Count);

Raw_Pixels : Raw_Buffer
with Import, Address => Address;
begin
for Step in 1 .. Repeat loop
for Raw of Raw_Pixels loop
declare
Color : constant HAL.Bitmap.Bitmap_Color :=
Bitmap_Color_Conversion.Word_To_Bitmap_Color
(Mode, Raw);
begin
Transmit (Color);
end;
end loop;
end loop;
end;

when HAL.Bitmap.RGB_565 |
HAL.Bitmap.ARGB_1555 |
HAL.Bitmap.ARGB_4444 |
HAL.Bitmap.AL_88 =>
declare
subtype Raw_Buffer is HAL.UInt16_Array (1 .. Count);

Raw_Pixels : Raw_Buffer
with Import, Address => Address;
begin
for Step in 1 .. Repeat loop
for Raw of Raw_Pixels loop
declare
Color : constant HAL.Bitmap.Bitmap_Color :=
Bitmap_Color_Conversion.Word_To_Bitmap_Color
(Mode, HAL.UInt32 (Raw));
begin
Transmit (Color);
end;
end loop;
end loop;
end;

when HAL.Bitmap.L_8 | HAL.Bitmap.AL_44 =>
declare
subtype Raw_Buffer is HAL.UInt8_Array (1 .. Count);

Raw_Pixels : Raw_Buffer
with Import, Address => Address;
begin
for Step in 1 .. Repeat loop
for Raw of Raw_Pixels loop
declare
Color : constant HAL.Bitmap.Bitmap_Color :=
Bitmap_Color_Conversion.Word_To_Bitmap_Color
(Mode, HAL.UInt32 (Raw));
begin
Transmit (Color);
end;
end loop;
end loop;
end;

when others =>
raise Program_Error;
end case;

This.Chip_Select.Set;
end Write_Pixels;

end ILI9341.SPI_Connector;
20 changes: 18 additions & 2 deletions components/src/screen/ili9341/ili9341-spi_connector.ads
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
------------------------------------------------------------------------------
-- --
-- Copyright (C) 2015-2023, AdaCore --
-- Copyright (C) 2015-2024, AdaCore --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
Expand Down Expand Up @@ -29,8 +29,11 @@
-- --
------------------------------------------------------------------------------

with HAL.SPI;
with System;

with HAL.Bitmap;
with HAL.GPIO;
with HAL.SPI;

package ILI9341.SPI_Connector is

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

procedure Write_Pixels
(This : ILI9341_Connector;
Mode : HAL.Bitmap.Bitmap_Color_Mode;
Address : System.Address;
Count : Positive;
Repeat : Positive);

procedure Read_Pixels
(This : ILI9341_Connector;
Cmd : HAL.UInt8;
Address : System.Address;
Count : Positive);

end ILI9341.SPI_Connector;
18 changes: 18 additions & 0 deletions examples/stm32_f4ve/lcd_spi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# LCD demo

This example demonstrates working with the LCD using the
HAL.Bitmap.Bitmap_Buffer interface.
Colorful rectangles are displayed on the screen, and their
colors change based on the X and Y coordinates.

For this demo, you need an LCD, (which is usually sold as
part of the STM32 F4VE board kit) connected as SPI:

* PA1 LCD RESET
* PA2 LCD D/C
* PA3 LCD BKL
* PA4 SPI1 NSS
* PA5 SPI1 SCK
* PA6 SPI1 MISO
* PA7 SPI1 MOSI

20 changes: 20 additions & 0 deletions examples/stm32_f4ve/lcd_spi/lcd_spi.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
with "stm32_f4ve_full_spi.gpr";

project LCD_SPI is

for Runtime ("Ada") use STM32_f4VE_Full'Runtime ("Ada");
for Target use "arm-eabi";
for Main use ("main.adb");
for Languages use ("Ada");
for Source_Dirs use (".");
for Object_Dir use "obj/";
for Create_Missing_Dirs use "True";

package Compiler renames STM32_F4VE_Full.Compiler;

package Ide is
for Program_Host use "localhost:4242";
for Communication_Protocol use "remote";
for Connection_Tool use "st-util";
end Ide;
end LCD_SPI;
71 changes: 71 additions & 0 deletions examples/stm32_f4ve/lcd_spi/main.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
------------------------------------------------------------------------------
-- --
-- Copyright (C) 2024, AdaCore --
-- --
-- Redistribution and use in source and binary forms, with or without --
-- modification, are permitted provided that the following conditions are --
-- met: --
-- 1. Redistributions of source code must retain the above copyright --
-- notice, this list of conditions and the following disclaimer. --
-- 2. Redistributions in binary form must reproduce the above copyright --
-- notice, this list of conditions and the following disclaimer in --
-- the documentation and/or other materials provided with the --
-- distribution. --
-- 3. Neither the name of the copyright holder nor the names of its --
-- contributors may be used to endorse or promote products derived --
-- from this software without specific prior written permission. --
-- --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT --
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, --
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY --
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT --
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE --
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --
-- --
------------------------------------------------------------------------------

with Ada.Real_Time;

with HAL.Bitmap;

with STM32.Board;
with Display_ILI9341;

procedure Main is
use type Ada.Real_Time.Time;

Next : Ada.Real_Time.Time := Ada.Real_Time.Clock;

Bitmap : Display_ILI9341.Bitmap_Buffer renames STM32.Board.TFT_Bitmap;

begin
-- STM32.Board.Initialize_LEDs;
STM32.Board.Display.Initialize;

Bitmap.Set_Source (HAL.Bitmap.Black);
Bitmap.Fill;

for X in 0 .. 31 loop
for Y in 0 .. 31 loop
Bitmap.Set_Source
((Alpha => 255,
Green => 128,
Red => HAL.UInt8 (X * 8),
Blue => HAL.UInt8 (Y * 8)));

Bitmap.Fill_Rect (((X * 7, Y * 10), 6, 9));
end loop;
end loop;

loop
-- STM32.Board.Toggle (STM32.Board.D1_LED);

Next := Next + Ada.Real_Time.Milliseconds (500);
delay until Next;
end loop;
end Main;
Loading
Loading