From c9094c7d7b49e21bc53d273288463723beec6601 Mon Sep 17 00:00:00 2001 From: Marc Poulhiès Date: Thu, 7 Jul 2022 22:36:29 +0200 Subject: Initial Ada firmware Missing doc, comment. --- firmware/ada/.gitignore | 4 ++ firmware/ada/alire.toml | 34 +++++++++++ firmware/ada/pouetpouet.gpr | 32 +++++++++++ firmware/ada/src/logging.adb | 61 ++++++++++++++++++++ firmware/ada/src/logging.ads | 16 ++++++ firmware/ada/src/pouetpouet.adb | 124 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 271 insertions(+) create mode 100644 firmware/ada/.gitignore create mode 100644 firmware/ada/alire.toml create mode 100644 firmware/ada/pouetpouet.gpr create mode 100644 firmware/ada/src/logging.adb create mode 100644 firmware/ada/src/logging.ads create mode 100644 firmware/ada/src/pouetpouet.adb (limited to 'firmware') diff --git a/firmware/ada/.gitignore b/firmware/ada/.gitignore new file mode 100644 index 0000000..5866d7b --- /dev/null +++ b/firmware/ada/.gitignore @@ -0,0 +1,4 @@ +/obj/ +/bin/ +/alire/ +/config/ diff --git a/firmware/ada/alire.toml b/firmware/ada/alire.toml new file mode 100644 index 0000000..70b31fd --- /dev/null +++ b/firmware/ada/alire.toml @@ -0,0 +1,34 @@ +name = "pouetpouet" +description = "PouetPouet firmware" +version = "0.1.0-dev" + +authors = ["Marc Poulhiès"] +maintainers = ["Marc Poulhiès "] +maintainers-logins = ["dkm"] + +executables = ["pouetpouet"] + + +# [[pins]] # Added by alr +# usb_embedded = { url='https://github.com/Fabien-Chouteau/usb_embedded' } + +##/mnt/barryallen/dkm/git/ada-embedded/usb_embedded' } +[[depends-on]] +click = "~0.1.0-dev" +[[pins]] +click = { url='https://github.com/dkm/click' } + +[configuration.values] +atomic.backend = "armv6m" +cortex_m.core = "m0" + +[[depends-on]] +usb_embedded = "~0.3.0-dev" +[[pins]] +usb_embedded = { url='https://github.com/Fabien-Chouteau/usb_embedded' } +[[depends-on]] +gnat_arm_elf = "^11.2" +[[depends-on]] +stm32f0x2_hal = "~0.0.0" +[[pins]] +stm32f0x2_hal = { url='https://github.com/dkm/stm32f0x2_hal-ada' } diff --git a/firmware/ada/pouetpouet.gpr b/firmware/ada/pouetpouet.gpr new file mode 100644 index 0000000..78bd5ad --- /dev/null +++ b/firmware/ada/pouetpouet.gpr @@ -0,0 +1,32 @@ +with "config/pouetpouet_config.gpr"; +with "stm32f0x2_hal.gpr"; + +project Pouetpouet is + + for Target use STM32F0X2_HAL'Target; + for Runtime ("Ada") use STM32F0X2_HAL'Runtime ("Ada"); + + for Source_Dirs use ("src/", "config/"); + for Object_Dir use "obj/" & Pouetpouet_Config.Build_Profile; + for Create_Missing_Dirs use "True"; + for Exec_Dir use "bin"; + for Main use ("pouetpouet.adb"); + + package Compiler is + for Default_Switches ("Ada") use Pouetpouet_Config.Ada_Compiler_Switches & ("-gnatX"); + end Compiler; + + package Binder is + for Switches ("Ada") use ("-Es"); -- Symbolic traceback + end Binder; + + package Linker is + for Default_Switches ("Ada") use + STM32F0X2_HAL.Linker_Switches; + end Linker; + + package Install is + for Artifacts (".") use ("share"); + end Install; + +end Pouetpouet; diff --git a/firmware/ada/src/logging.adb b/firmware/ada/src/logging.adb new file mode 100644 index 0000000..f6a159f --- /dev/null +++ b/firmware/ada/src/logging.adb @@ -0,0 +1,61 @@ +with HAL; use HAL; +with STM32.Device; use STM32.Device; +with STM32.GPIO; use STM32.GPIO; +with STM32.USARTs; use STM32.USARTs; + +package body Logging is + + TX_Pin : constant GPIO_Point := PB6; + RX_Pin : constant GPIO_Point := PB7; + + procedure Init is + begin + Enable_Clock (USART_1); + Enable_Clock (RX_Pin & TX_Pin); + + Configure_IO + (RX_Pin & TX_Pin, + (Mode => Mode_AF, + AF => GPIO_B_AF_USART1_0, + Resistors => Pull_Up, + AF_Speed => Speed_50MHz, + AF_Output_Type => Push_Pull)); + + Disable (USART_1); + + Set_Oversampling_Mode (USART_1, Oversampling_By_16); + Set_Baud_Rate (USART_1, 115200); + Set_Mode (USART_1, Tx_Rx_Mode); + Set_Stop_Bits (USART_1, Stopbits_1); + Set_Word_Length (USART_1, Word_Length_8); + Set_Parity (USART_1, No_Parity); + Set_Flow_Control (USART_1, No_Flow_Control); + + Enable (USART_1); + + end Init; + + procedure Await_Send_Ready (This : USART) is + begin + loop + exit when Tx_Ready (This); + end loop; + end Await_Send_Ready; + + procedure Put_Blocking (This : in out USART; Data : UInt16) is + begin + Await_Send_Ready (This); + Transmit (This, UInt9 (Data)); + end Put_Blocking; + + procedure Log (S : String; L : Integer := 1; Deindent : Integer := 0) is + begin + + for C of S loop + Put_Blocking (USART_1, Character'Pos (C)); + end loop; + + Put_Blocking (USART_1, UInt16 (13)); -- CR + Put_Blocking (USART_1, UInt16 (10)); -- LF + end Log; +end Logging; diff --git a/firmware/ada/src/logging.ads b/firmware/ada/src/logging.ads new file mode 100644 index 0000000..32eaa2a --- /dev/null +++ b/firmware/ada/src/logging.ads @@ -0,0 +1,16 @@ +with HAL; use HAL; +with STM32.Device; use STM32.Device; +with STM32.GPIO; use STM32.GPIO; +with STM32.USARTs; use STM32.USARTs; + +package Logging is + + procedure Init; + + procedure Await_Send_Ready (This : USART); + + procedure Put_Blocking (This : in out USART; Data : UInt16); + + procedure Log (S : String; L : Integer := 1; Deindent : Integer := 0); + +end Logging; diff --git a/firmware/ada/src/pouetpouet.adb b/firmware/ada/src/pouetpouet.adb new file mode 100644 index 0000000..a201bc6 --- /dev/null +++ b/firmware/ada/src/pouetpouet.adb @@ -0,0 +1,124 @@ +with STM32.Device; use STM32.Device; +with STM32.GPIO; use STM32.GPIO; + +-- Not yet used. +-- with STM32.Timers; use STM32.Timers; + +-- with USB.HAL.Device; +-- with USB.Device.Serial; + +with USB; use USB; +with USB.Device.HID.Keyboard; +with USB.Device; use USB.Device; + +with Click; + +with Logging; use Logging; + +procedure Pouetpouet is + + Fatal_Error : exception; + + type ColR is range 1 .. 12; + type RowR is range 1 .. 5; + + type Rt is array (RowR) of GPIO_Point; + type Ct is array (ColR) of GPIO_Point; + + Rows : constant Rt + := [PB0, PB1, PB2, PB10, PB11]; + Cols : constant Ct + := [PA0, PA1, PB13, PB12, PB14, PB15, PA15, PB3, PB4, PB5, PB8, PB9]; + + package TestClick is new Click (5 ,ColR, RowR, GPIO_Point, + Ct, Rt, Cols, Rows); + use TestClick; + + BepoLayout : constant Layout := + [ + [Kb1, Kb2, Kb3, Kb4, Kb5, Grave, Kb6, Kb7, Kb8, Kb9, Kb0, Minus], + [Q, W, E, R, T, Tab, Y, U, I, O, P, LBracket], + [A, S, D, F, G, BSpace, H, J, K, L, SColon, Quote], + [Z, X, C, V, B, Enter, N, M, Comma, Dot, Slash, Bslash ], + [LCtrl, X, LGui, LShift, LAlt, Space, RAlt, RBracket, Equal, Delete, RShift, RCtrl] + ]; + + Max_Packet_Size : constant := 64; + USB_Stack : USB.Device.USB_Device_Stack (Max_Classes => 1); + HID_Class : aliased USB.Device.HID.Keyboard.Instance; + + use type USB.Device.Init_Result; + USB_Status : USB.Device.Init_Result; + +begin + Init; + + if not USB_Stack.Register_Class (HID_Class'Unchecked_Access) then + raise Fatal_Error with "Failed to register USB Serial device class"; + end if; + + USB_Status := USB_Stack.Initialize + (Controller => STM32.Device.UDC'Access, + Manufacturer => USB.To_USB_String ("Kataplop"), + Product => USB.To_USB_String ("Some buggy Keyboard"), + Serial_Number => USB.To_USB_String ("DEADBEEF"), + Max_Packet_Size => Max_Packet_Size); + + if USB_Status /= USB.Device.Ok then + raise Fatal_Error with "USB stack initialization failed: " & USB_Status'Image; + end if; + + USB_Stack.Start; + + -- Enable_Clock (Timer_1); + -- Reset (Timer_1); + + -- Configure (Timer_1, Prescaler => 13999, Period => 5999); + + -- Enable_Interrupt (Timer_1, Timer_Update_Interrupt); + + -- Enable (Timer_1); + Log ("STARTING"); + + for Row of Keys.Rows loop + Enable_Clock (Row); + Configure_IO + (Row, + (Mode => Mode_Out, + Resistors => Floating, + Speed => Speed_Medium, + Output_Type => Push_Pull)); + end loop; + + for Col of Keys.Cols loop + Enable_Clock (Col); + Configure_IO + (Col, + (Mode => Mode_In, + Resistors => Pull_Up)); + end loop; + + loop + USB_Stack.Poll; + + declare + Evts : constant Events := Get_Events (Get_Matrix); + begin + for Evt of Evts loop + Log ("(" & Evt.Evt'Image & ", " & Evt.Col'Image & ", " + & Evt.Row'Image & ") = " + & KeyCode'Enum_Rep (BepoLayout (Evt.Row, Evt.Col))'Image); + + if HID_Class.Ready then + if Evt.Evt = Press then + HID_Class.Push_Key_Code + (KeyCode'Enum_Rep (BepoLayout (Evt.Row, Evt.Col))); + end if; + + HID_Class.Send_Report (UDC); + end if; + end loop; + end; + -- Delay_Cycles (72); + end loop; +end Pouetpouet; -- cgit v1.2.3