Skip to content

Commit 5809455

Browse files
committed
Add touch panel to stm32_f4ve board
1 parent 3d584b8 commit 5809455

File tree

7 files changed

+388
-21
lines changed

7 files changed

+388
-21
lines changed

boards/stm32_f4ve/src/stm32-board.adb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
------------------------------------------------------------------------------
3131

3232
with HAL.SPI;
33+
with STM32.SPI;
3334

3435
package body STM32.Board is
3536

boards/stm32_f4ve/src/stm32-board.ads

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ with STM32.Device; use STM32.Device;
4040
with STM32.DMA; use STM32.DMA;
4141
with STM32.DMA.Interrupts; use STM32.DMA.Interrupts;
4242
with STM32.GPIO; use STM32.GPIO;
43-
with STM32.SPI; use STM32.SPI;
4443

4544
with SDCard;
4645
with W25Q16;
4746
with Display_ILI9341;
47+
with Touch_Panel_XPT2046;
4848

4949
package STM32.Board is
5050
pragma Elaborate_Body;
@@ -95,10 +95,20 @@ package STM32.Board is
9595
-- TFT --
9696
---------
9797

98+
SPI2_SCK : GPIO_Point renames PB13;
99+
SPI2_MISO : GPIO_Point renames PB14;
100+
SPI2_MOSI : GPIO_Point renames PB15;
101+
102+
TFT_RS : GPIO_Point renames PC5; -- PEN IRQ
103+
TFT_BLK : GPIO_Point renames PB1; -- LCD backlight
104+
TFT_CS : GPIO_Point renames PB12;
105+
98106
Display : Display_ILI9341.Display;
99107

100108
TFT_Bitmap : Display_ILI9341.Bitmap_Buffer := Display.Buffer;
101109

110+
Touch_Panel : Touch_Panel_XPT2046.Touch_Panel;
111+
102112
--------------------------
103113
-- micro SD card reader --
104114
--------------------------
@@ -134,26 +144,6 @@ package STM32.Board is
134144

135145
SDCard_Device : aliased SDCard.SDCard_Controller (SDIO'Access);
136146

137-
---------------
138-
-- SPI2 Pins --
139-
---------------
140-
141-
SPI2_SCK : GPIO_Point renames PB13;
142-
SPI2_MISO : GPIO_Point renames PB14;
143-
SPI2_MOSI : GPIO_Point renames PB15;
144-
145-
-- External TFT connector
146-
147-
TFT_RS : GPIO_Point renames PC5;
148-
TFT_BLK : GPIO_Point renames PB1; -- LCD backlight
149-
TFT_CS : GPIO_Point renames PB12;
150-
151-
-----------------
152-
-- Touch Panel --
153-
-----------------
154-
155-
TFT_SPI : SPI_Port renames SPI_2;
156-
157147
------------------
158148
-- User buttons --
159149
------------------
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2023, 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 STMicroelectronics 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 HAL.SPI;
33+
with STM32.Board;
34+
with STM32.GPIO;
35+
with STM32.SPI;
36+
37+
package body Touch_Panel_XPT2046 is
38+
39+
----------------
40+
-- Initialize --
41+
----------------
42+
43+
procedure Initialize
44+
(This : in out Touch_Panel;
45+
Orientation : HAL.Framebuffer.Display_Orientation :=
46+
HAL.Framebuffer.Default)
47+
is
48+
SPI_Pins : constant STM32.GPIO.GPIO_Points :=
49+
(STM32.Board.SPI2_SCK,
50+
STM32.Board.SPI2_MISO,
51+
STM32.Board.SPI2_MOSI);
52+
begin
53+
STM32.Device.Enable_Clock (STM32.Board.TFT_RS);
54+
STM32.Device.Enable_Clock (STM32.Board.TFT_CS);
55+
STM32.Device.Enable_Clock (SPI_Pins);
56+
STM32.Device.Enable_Clock (STM32.Device.SPI_2);
57+
58+
STM32.Board.TFT_RS.Configure_IO -- Pen IRQ pin
59+
((Mode => STM32.GPIO.Mode_In,
60+
Resistors => STM32.GPIO.Floating));
61+
62+
STM32.Board.TFT_CS.Configure_IO
63+
((Mode => STM32.GPIO.Mode_Out,
64+
Resistors => STM32.GPIO.Floating,
65+
Output_Type => STM32.GPIO.Push_Pull,
66+
Speed => STM32.GPIO.Speed_100MHz));
67+
68+
STM32.GPIO.Configure_IO
69+
(SPI_Pins,
70+
(Mode => STM32.GPIO.Mode_AF,
71+
Resistors => STM32.GPIO.Pull_Up,
72+
AF_Output_Type => STM32.GPIO.Push_Pull,
73+
AF_Speed => STM32.GPIO.Speed_100MHz,
74+
AF => STM32.Device.GPIO_AF_SPI2_5));
75+
76+
STM32.SPI.Configure
77+
(STM32.Device.SPI_2,
78+
(Direction => STM32.SPI.D2Lines_FullDuplex,
79+
Mode => STM32.SPI.Master,
80+
Data_Size => HAL.SPI.Data_Size_8b,
81+
Clock_Polarity => STM32.SPI.Low, -- Mode 0
82+
Clock_Phase => STM32.SPI.P1Edge,
83+
Slave_Management => STM32.SPI.Software_Managed,
84+
Baud_Rate_Prescaler => STM32.SPI.BRP_32,
85+
First_Bit => STM32.SPI.MSB,
86+
CRC_Poly => 0));
87+
-- SPI2 sits on APB1, which is 42MHz, so SPI rate in 42/32=1.3MHz
88+
89+
This.Set_Orientation (Orientation);
90+
91+
This.Calibrate
92+
(Min_X => 300,
93+
Max_X => 3768,
94+
Min_Y => 140,
95+
Max_Y => 3813);
96+
end Initialize;
97+
98+
---------------------
99+
-- Set_Orientation --
100+
---------------------
101+
102+
procedure Set_Orientation
103+
(This : in out Touch_Panel;
104+
Orientation : HAL.Framebuffer.Display_Orientation) is
105+
begin
106+
case Orientation is
107+
when HAL.Framebuffer.Default | HAL.Framebuffer.Portrait =>
108+
This.Set_Bounds (240, 320, HAL.Touch_Panel.Invert_Y);
109+
when HAL.Framebuffer.Landscape =>
110+
This.Set_Bounds (240, 320, HAL.Touch_Panel.Swap_XY);
111+
end case;
112+
end Set_Orientation;
113+
114+
end Touch_Panel_XPT2046;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
------------------------------------------------------------------------------
2+
-- --
3+
-- Copyright (C) 2023, 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 HAL.Touch_Panel;
33+
with HAL.Framebuffer;
34+
35+
private with XPT2046;
36+
private with STM32.Device;
37+
38+
package Touch_Panel_XPT2046 is
39+
40+
type Touch_Panel is limited new HAL.Touch_Panel.Touch_Panel_Device
41+
with private;
42+
43+
procedure Initialize
44+
(This : in out Touch_Panel;
45+
Orientation : HAL.Framebuffer.Display_Orientation :=
46+
HAL.Framebuffer.Default);
47+
48+
procedure Set_Orientation
49+
(This : in out Touch_Panel;
50+
Orientation : HAL.Framebuffer.Display_Orientation);
51+
52+
private
53+
54+
type Touch_Panel is limited new XPT2046.XPT2046_Device
55+
(SPI => STM32.Device.SPI_2'Access,
56+
CS => STM32.Device.PB12'Access) with null record;
57+
58+
end Touch_Panel_XPT2046;

0 commit comments

Comments
 (0)