Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyGrosser committed Sep 4, 2024
1 parent d99c713 commit 1aec542
Show file tree
Hide file tree
Showing 63 changed files with 95,638 additions and 51 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions src/devices/rp2350/rp-device.ads
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,60 @@
--
-- SPDX-License-Identifier: BSD-3-Clause
--
with RP2040_SVD.SPI;
with RP2040_SVD.I2C;
with RP2040_SVD.UART;
with RP2350_SVD;
with RP.I2C;
with RP.I2C_Master;
with RP.SPI;
with RP.UART;
with RP.Timer.Interrupts;
with RP.PIO;

package RP.Device is
SPI0_Periph : aliased RP2040_SVD.SPI.SPI_Peripheral
with Import, Address => RP2350_SVD.SPI0_Base;
SPI1_Periph : aliased RP2040_SVD.SPI.SPI_Peripheral
with Import, Address => RP2350_SVD.SPI1_Base;

SPI_0 : aliased RP.SPI.SPI_Port (0, SPI0_Periph'Access);
SPI_1 : aliased RP.SPI.SPI_Port (1, SPI1_Periph'Access);

I2C0_Periph : aliased RP2040_SVD.I2C.I2C_Peripheral
with Import, Address => RP2350_SVD.I2C0_Base;
I2C1_Periph : aliased RP2040_SVD.I2C.I2C_Peripheral
with Import, Address => RP2350_SVD.I2C1_Base;

I2C_0 : aliased RP.I2C.I2C_Port (0, I2C0_Periph'Access);
I2C_1 : aliased RP.I2C.I2C_Port (1, I2C1_Periph'Access);

I2CM_0 : aliased RP.I2C_Master.I2C_Master_Port (0, I2C0_Periph'Access);
I2CM_1 : aliased RP.I2C_Master.I2C_Master_Port (1, I2C1_Periph'Access);

UART0_Periph : aliased RP2040_SVD.UART.UART_Peripheral
with Import, Address => RP2350_SVD.UART0_Base;
UART1_Periph : aliased RP2040_SVD.UART.UART_Peripheral
with Import, Address => RP2350_SVD.UART1_Base;

UART_0 : aliased RP.UART.UART_Port (0, UART0_Periph'Access);
UART_1 : aliased RP.UART.UART_Port (1, UART1_Periph'Access);

Timer_0 : aliased RP.Timer.Interrupts.Delays;
Timer_1 : aliased RP.Timer.Interrupts.Delays;
Timer : RP.Timer.Interrupts.Delays renames Timer_0;
-- RP2040 only had one timer, rename it to Timer_0 here. RP2350 timers have
-- new registers too, but we don't do anything with that yet.

PIO0_Periph : aliased RP.PIO.PIO_Peripheral
with Import, Address => RP2350_SVD.PIO0_Base;
PIO1_Periph : aliased RP.PIO.PIO_Peripheral
with Import, Address => RP2350_SVD.PIO1_Base;
PIO2_Periph : aliased RP.PIO.PIO_Peripheral
with Import, Address => RP2350_SVD.PIO2_Base;

PIO_0 : aliased RP.PIO.PIO_Device (0, PIO0_Periph'Access);
PIO_1 : aliased RP.PIO.PIO_Device (1, PIO1_Periph'Access);
PIO_2 : aliased RP.PIO.PIO_Device (2, PIO2_Periph'Access);

end RP.Device;
6 changes: 3 additions & 3 deletions src/drivers/rp-gpio.adb → src/devices/rp2350/rp-gpio.adb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ package body RP.GPIO is
-- on these pads. This is unlikely to cause a problem as the digital
-- inputs have hysteresis enabled by default.

for Pin in ADC_Pin'Range loop
PADS_BANK_Periph.GPIO (Pin).IE := False;
PADS_BANK_Periph.GPIO (Pin).OD := True;
for Pin in 26 .. 29 loop
PADS_BANK_Periph.GPIO (GPIO_Pin (Pin)).IE := False;
PADS_BANK_Periph.GPIO (GPIO_Pin (Pin)).OD := True;
end loop;

-- Mask all pin interrupts
Expand Down
56 changes: 36 additions & 20 deletions src/drivers/rp-gpio.ads → src/devices/rp2350/rp-gpio.ads
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
-- SPDX-License-Identifier: BSD-3-Clause
--

with RP2040_SVD.IO_BANK0; use RP2040_SVD.IO_BANK0;
with RP2040_SVD.PADS_BANK0; use RP2040_SVD.PADS_BANK0;
with RP2040_SVD; use RP2040_SVD;
with RP2350_SVD.IO_BANK0; use RP2350_SVD.IO_BANK0;
with RP2350_SVD.PADS_BANK0; use RP2350_SVD.PADS_BANK0;
with RP2350_SVD; use RP2350_SVD;
with HAL; use HAL;
with HAL.GPIO; use HAL.GPIO;

package RP.GPIO
with Preelaborate
is
type GPIO_Pin is range 0 .. 29;
subtype ADC_Pin is GPIO_Pin range 26 .. 29;
type GPIO_Pin is range 0 .. 47;
subtype ADC_Pin is GPIO_Pin
with Static_Predicate => ADC_Pin in 26 .. 29 | 40 .. 47;

type GPIO_Point is new HAL.GPIO.GPIO_Point with
record
Expand All @@ -23,24 +24,39 @@ is

type GPIO_Config_Mode is (Input, Output, Analog);

type GPIO_Function is
(SPI, UART, I2C, PWM, SIO, PIO0, PIO1, CLOCK, USB, HI_Z);

type GPIO_Pull_Mode is (Floating, Pull_Up, Pull_Down, Pull_Both);

type GPIO_Drive is (Drive_2mA, Drive_4mA, Drive_8mA, Drive_12mA);

type GPIO_Function is
(HSTX,
SPI,
UART,
I2C,
PWM,
SIO,
PIO0,
PIO1,
PIO2,
CLOCK,
USB,
UART_ALT,
HI_Z);

for GPIO_Function use
(SPI => 1,
UART => 2,
I2C => 3,
PWM => 4,
SIO => 5,
PIO0 => 6,
PIO1 => 7,
CLOCK => 8,
USB => 9,
HI_Z => 31);
(HSTX => 0,
SPI => 1,
UART => 2,
I2C => 3,
PWM => 4,
SIO => 5,
PIO0 => 6,
PIO1 => 7,
PIO2 => 8,
CLOCK => 9,
USB => 10,
UART_ALT => 11,
HI_Z => 31);

type Interrupt_Triggers is (Low_Level, High_Level, Falling_Edge, Rising_Edge)
with Size => 4;
Expand Down Expand Up @@ -187,10 +203,10 @@ private
end record
with Volatile;
for IO_BANK use record
GPIO at 16#0000# range 0 .. 1919;
GPIO at 16#0000# range 0 .. 3071;
end record;

type PADS_BANK_GPIO_Registers is array (GPIO_Pin) of RP2040_SVD.PADS_BANK0.GPIO_Register;
type PADS_BANK_GPIO_Registers is array (GPIO_Pin) of RP2350_SVD.PADS_BANK0.GPIO_Register;

type PADS_BANK is record
VOLTAGE_SELECT : VOLTAGE_SELECT_Register;
Expand Down
61 changes: 61 additions & 0 deletions src/devices/rp2350/rp-reset.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--
-- Copyright 2021 (C) Jeremy Grosser <[email protected]>
--
-- SPDX-License-Identifier: BSD-3-Clause
--
with RP2040_SVD;
with RP.Timer;

package body RP.Reset is

type RESET_Register is array (Reset_Id) of Boolean
with Component_Size => 1,
Size => 32;

type RESETS_Peripheral is record
RESET : aliased RESET_Register;
WDSEL : aliased RESET_Register;
RESET_DONE : aliased RESET_Register;
end record
with Volatile;

for RESETS_Peripheral use record
RESET at 0 range 0 .. 31;
WDSEL at 4 range 0 .. 31;
RESET_DONE at 8 range 0 .. 31;
end record;

RESETS_Periph : aliased RESETS_Peripheral
with Import, Address => RP2040_SVD.RESETS_Base;

procedure Reset_Peripheral
(Peripheral : Reset_Id)
is
begin
RESETS_Periph.RESET (Peripheral) := True;
RESETS_Periph.RESET (Peripheral) := False;
while not RESETS_Periph.RESET_DONE (Peripheral) loop
null;
end loop;
end Reset_Peripheral;

procedure Reset_Peripheral
(Peripheral : Reset_Id;
Status : out Reset_Status;
Timeout : Natural := 100)
is
use RP.Timer;
Deadline : constant Time := Clock + Milliseconds (Timeout);
begin
RESETS_Periph.RESET (Peripheral) := True;
RESETS_Periph.RESET (Peripheral) := False;
while not RESETS_Periph.RESET_DONE (Peripheral) loop
if Timeout > 0 and then Clock >= Deadline then
Status := Reset_Timeout;
return;
end if;
end loop;
Status := Reset_Ok;
end Reset_Peripheral;

end RP.Reset;
57 changes: 57 additions & 0 deletions src/devices/rp2350/rp-reset.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--
-- Copyright 2021 (C) Jeremy Grosser <[email protected]>
--
-- SPDX-License-Identifier: BSD-3-Clause
--
package RP.Reset
with Preelaborate
is

type Reset_Id is
(Reset_ADC,
Reset_BUSCTRL,
Reset_DMA,
Reset_HSTX,
Reset_I2C0,
Reset_I2C1,
Reset_IO_BANK0,
Reset_QSPI,
Reset_JTAG,
Reset_PADS_BANK0,
Reset_PADS_QSPI,
Reset_PIO0,
Reset_PIO1,
Reset_PIO2,
Reset_PLL_SYS,
Reset_PLL_USB,
Reset_PWM,
Reset_SHA256,
Reset_SPI0,
Reset_SPI1,
Reset_SYSCFG,
Reset_SYSINFO,
Reset_TBMAN,
Reset_TIMER0,
Reset_TIMER1,
Reset_TRNG,
Reset_UART0,
Reset_UART1,
Reset_USBCTRL);

type Reset_Status is
(Reset_Ok,
Reset_Timeout);

-- This will hang if the reset fails. Most drivers use this version.
procedure Reset_Peripheral
(Peripheral : Reset_Id);

-- Timeout is specified in Milliseconds. No peripheral should take more
-- than a few cycles to reset.
procedure Reset_Peripheral
(Peripheral : Reset_Id;
Status : out Reset_Status;
Timeout : Natural := 100)
with Pre => Peripheral /= Reset_TIMER0;

end RP.Reset;
2 changes: 1 addition & 1 deletion src/drivers/rp-pio-audio_i2s.adb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ package body RP.PIO.Audio_I2S is
Set_Enabled (This.PIO.all, This.SM, True);

DMA_Config.Trigger := DMA_Request_Trigger'Val
(DMA_Request_Trigger'Pos (PIO0_TX0) + (This.PIO.Num * 8) + Natural (This.SM));
(DMA_Request_Trigger'Pos (PIO0_TX0) + (Natural (This.PIO.Num) * 8) + Natural (This.SM));
if Channels = 1 then
DMA_Config.Data_Size := Transfer_16;
else
Expand Down
24 changes: 5 additions & 19 deletions src/drivers/rp-pio.adb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
with Ada.Unchecked_Conversion;
with RP.Clock;
with RP.Reset;
with RP2040_SVD.Interrupts;

package body RP.PIO is
procedure Enable
Expand All @@ -17,6 +16,7 @@ package body RP.PIO is
case This.Num is
when 0 => Reset_Peripheral (Reset_PIO0);
when 1 => Reset_Peripheral (Reset_PIO1);
when 2 => Reset_Peripheral (Reset_PIO2);
end case;
end Enable;

Expand Down Expand Up @@ -254,6 +254,7 @@ package body RP.PIO is
case PIO.Num is
when 0 => return RP.GPIO.PIO0;
when 1 => return RP.GPIO.PIO1;
when 2 => return RP.GPIO.PIO2;
end case;
end GPIO_Function;

Expand Down Expand Up @@ -513,22 +514,7 @@ package body RP.PIO is
(This : PIO_Device;
IRQ : PIO_IRQ_ID)
return Cortex_M.NVIC.Interrupt_ID
is
use RP2040_SVD.Interrupts;
begin
case This.Num is
when 0 =>
case IRQ is
when 0 => return PIO0_IRQ_0_Interrupt;
when 1 => return PIO0_IRQ_1_Interrupt;
end case;
when 1 =>
case IRQ is
when 0 => return PIO1_IRQ_0_Interrupt;
when 1 => return PIO1_IRQ_1_Interrupt;
end case;
end case;
end NVIC_IRQ_Line;
is (Cortex_M.NVIC.Interrupt_ID (This.Interrupt_Base + (Natural (This.Num) * 2) + Natural (IRQ)));

procedure Enable_IRQ (This : in out PIO_Device;
IRQ : PIO_IRQ_ID)
Expand Down Expand Up @@ -666,12 +652,12 @@ package body RP.PIO is
(This : PIO_Device;
SM : PIO_SM)
return RP.DMA.DMA_Request_Trigger
is (RP.DMA.DMA_Request_Trigger'Val (This.Num * 8 + Natural (SM)));
is (RP.DMA.DMA_Request_Trigger'Val (Natural (This.Num) * 8 + Natural (SM)));

function DMA_RX_Trigger
(This : PIO_Device;
SM : PIO_SM)
return RP.DMA.DMA_Request_Trigger
is (RP.DMA.DMA_Request_Trigger'Val (This.Num * 8 + Natural (SM) + 4));
is (RP.DMA.DMA_Request_Trigger'Val (Natural (This.Num) * 8 + Natural (SM) + 4));

end RP.PIO;
6 changes: 4 additions & 2 deletions src/drivers/rp-pio.ads
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ package RP.PIO
with Preelaborate
is
type PIO_Peripheral is private;
subtype PIO_Number is Natural range 0 .. 1;
type PIO_Number is range 0 .. 2;
type PIO_Device
(Num : PIO_Number;
Periph : not null access PIO_Peripheral)
is tagged null record;
is tagged record
Interrupt_Base : Natural := 7;
end record;

type PIO_SM is range 0 .. 3;
type PIO_SM_Mask is array (PIO_SM) of Boolean
Expand Down
Loading

0 comments on commit 1aec542

Please sign in to comment.