Skip to content

Commit

Permalink
timer wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyGrosser committed Nov 16, 2024
1 parent e2e3397 commit db44af2
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- SPDX-License-Identifier: BSD-3-Clause
--
with RP2040_SVD.Interrupts;
with RP2040_SVD.TIMER; use RP2040_SVD.TIMER;
with RP2040_SVD.TIMER; use RP2040_SVD.TIMER;
with Cortex_M.Hints;
with RP_Interrupts;
with System;
Expand Down
6 changes: 4 additions & 2 deletions src/devices/rp2350/rp-clock.adb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--
with RP2350_SVD.CLOCKS; use RP2350_SVD.CLOCKS;
with RP2350_SVD.PLL; use RP2350_SVD.PLL;
with RP2350_SVD.TICKS;
with RP2350_SVD.TICKS; use RP2350_SVD.TICKS;
with RP2350_SVD.XOSC;
with RP2350_SVD.ROSC;
with RP.Reset;
Expand Down Expand Up @@ -147,7 +147,9 @@ package body RP.Clock is
end if;

CLOCKS_Periph.FC0_REF_KHZ.FC0_REF_KHZ := FC0_REF_KHZ_FC0_REF_KHZ_Field (Reference / 1_000);
RP2350_SVD.TICKS.TICKS_Periph.TIMER0_CTRL.ENABLE := True;
TICKS_Periph.TIMER0_CTRL.ENABLE := False;
TICKS_Periph.TIMER0_CYCLES.TIMER0_CYCLES := UInt9 (Reference / 1_000_000);
TICKS_Periph.TIMER0_CTRL.ENABLE := True;
end Initialize;

procedure Enable
Expand Down
99 changes: 99 additions & 0 deletions src/devices/rp2350/rp-timer-interrupts.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
--
-- Copyright 2021 (C) Jeremy Grosser
--
-- SPDX-License-Identifier: BSD-3-Clause
--
with RP2350_SVD.Interrupts;
with RP2350_SVD.TIMER0; use RP2350_SVD.TIMER0;
with Cortex_M.Hints;
with RP_Interrupts;
with System;

package body RP.Timer.Interrupts is

procedure IRQ_Handler is
begin
TIMER0_Periph.INTR.ALARM_2 := True;
end IRQ_Handler;

procedure Enable
(This : in out Delays)
is
use RP2350_SVD.Interrupts;
use System;
begin
TIMER0_Periph.INTE.ALARM_2 := True;

RP_Interrupts.Attach_Handler
(Handler => IRQ_Handler'Access,
Id => TIMER0_IRQ_2_Interrupt,
Prio => Interrupt_Priority'First);

RP.Timer.Set_Debug_Pause (False, False);
end Enable;

procedure Disable
(This : in out Delays)
is
begin
TIMER0_Periph.INTE.ALARM_2 := False;
end Disable;

function Enabled
(This : Delays)
return Boolean
is (TIMER0_Periph.INTE.ALARM_2);

procedure Delay_Until
(This : in out Delays;
T : Time)
is
begin
if T <= Clock then
return;
end if;
TIMER0_Periph.ALARM2 := UInt32 (T and 16#FFFFFFFF#);
loop
Cortex_M.Hints.Wait_For_Interrupt;
if Clock >= T then
return;
elsif TIMER0_Periph.INTS.ALARM_2 then
-- If the high byte of the timer rolled over but we still haven't
-- reached the target time, reset the alarm and go again
TIMER0_Periph.ALARM2 := UInt32 (T and 16#FFFFFFFF#);
end if;
end loop;
end Delay_Until;

overriding
procedure Delay_Microseconds
(This : in out Delays;
Us : Integer)
is
Deadline : constant UInt64 := UInt64 (Clock) + UInt64 (Us);
begin
if Us > 0 then
while UInt64 (Clock) < Deadline loop
null;
end loop;
end if;
end Delay_Microseconds;

overriding
procedure Delay_Milliseconds
(This : in out Delays;
Ms : Integer)
is
begin
Delay_Until (This, Clock + (1_000 * Time (Ms)));
end Delay_Milliseconds;

overriding
procedure Delay_Seconds
(This : in out Delays;
S : Integer)
is
begin
Delay_Until (This, Clock + (1_000_000 * Time (S)));
end Delay_Seconds;
end RP.Timer.Interrupts;
Loading

0 comments on commit db44af2

Please sign in to comment.