|
| 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; |
0 commit comments