Skip to content

Commit

Permalink
Update arduino demos.
Browse files Browse the repository at this point in the history
  * demos-arduino-due/debounce_hardware.adb: added explanatory comment.
    (Handler): ISR_Register's name has changed (to PIOA_ISR_Register :-)
  * demos-arduino-due/debounce_software.adb: likewise.
    (Button'Spec): added Outstanding_Interrupts, Suppress_Interrupts_Until.
    (Button.Wait): only clear Trigger when there are no outstanding
      interrupts left.
    (Button.Handler): rewrite.
    (T): flash the LED each time Button.Wait is released.
  * demos-arduino-due/.gdbinit: copied from ../test-arduino-due.
  • Loading branch information
simonjwright committed Mar 14, 2020
1 parent 1b130b8 commit 71cee11
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
2 changes: 2 additions & 0 deletions demos-arduino-due/.gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target remote :2331
load
12 changes: 9 additions & 3 deletions demos-arduino-due/debounce_hardware.adb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright (C) 2016, 2019 Free Software Foundation, Inc.
-- Copyright (C) 2016, 2019, 2020 Free Software Foundation, Inc.

-- This file is part of the Cortex GNAT RTS package.
--
Expand All @@ -18,6 +18,12 @@

-- This package, in file debounce_hardware.adb, is the
-- hardware-implemented version of debouncing.
--
-- To run, connect a pushbutton between GND and pin 53. On button-up,
-- the LED will flash a number of times indicating how many button-up
-- interrupts were received,
--
-- To check, comment out the line where debounce is enabled (:132).

with Ada.Interrupts.Names;
with Ada.Real_Time;
Expand Down Expand Up @@ -63,7 +69,7 @@ package body Debounce_Impl is
end Wait;

procedure Handler is
Status : constant ISR_Register := PIOB_Periph.ISR;
Status : constant PIOA_ISR_Register := PIOB_Periph.ISR;
begin
if Status.Arr (Input_Pin) /= 0 then
Interrupts := Interrupts + 1;
Expand Down Expand Up @@ -122,7 +128,7 @@ begin
-- .. debounce vs glitch ..
PIOB_Periph.DIFSR.Arr := (Input_Pin => 1, others => 0);

-- .. enable debounce ..
-- .. enable debounce (comment-out to disable) ..
PIOB_Periph.IFER.Arr := (Input_Pin => 1, others => 0);

-- .. enable interrupts.
Expand Down
41 changes: 28 additions & 13 deletions demos-arduino-due/debounce_software.adb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright (C) 2016, 2019 Free Software Foundation, Inc.
-- Copyright (C) 2016, 2019, 2020 Free Software Foundation, Inc.

-- This file is part of the Cortex GNAT RTS package.
--
Expand All @@ -16,8 +16,12 @@
-- along with this program; see the file COPYING3. If not, see
-- <http://www.gnu.org/licenses/>.

-- This package, in file debounce_hardware.adb, is the
-- This package, in file debounce_software.adb, is the
-- software-implemented version of debouncing.
--
-- To run, connect a pushbutton between GND and pin 53. On button-up,
-- the LED will flash a number of times indicating how many button-up
-- interrupts were received,

with Ada.Interrupts.Names;
with Ada.Real_Time;
Expand Down Expand Up @@ -45,6 +49,9 @@ package body Debounce_Impl is
entry Wait;
private
Triggered : Boolean := False;
Outstanding_Interrupts : Natural := 0;
Suppress_Interrupts_Until : Ada.Real_Time.Time
:= Ada.Real_Time.Time_First;
procedure Handler
with
Attach_Handler => Ada.Interrupts.Names.PIOB_IRQ,
Expand All @@ -56,14 +63,25 @@ package body Debounce_Impl is
protected body Button is
entry Wait when Triggered is
begin
Triggered := False;
Outstanding_Interrupts := Outstanding_Interrupts - 1;
if Outstanding_Interrupts = 0 then
Triggered := False;
end if;
end Wait;

procedure Handler is
Status : constant ISR_Register := PIOB_Periph.ISR;
Status : constant PIOA_ISR_Register := PIOB_Periph.ISR;
Now : constant Ada.Real_Time.Time := Ada.Real_Time.Clock;
use type Ada.Real_Time.Time;
begin
if Status.Arr (Input_Pin) /= 0 then
if Status.Arr (Input_Pin) /= 0
-- Pull-up is enabled, so the pin is 0 if the button is pressed
and then PIOB_Periph.PDSR.Arr (Input_Pin) /= 0
and then Now >= Suppress_Interrupts_until
then
Triggered := True;
Outstanding_Interrupts := Outstanding_Interrupts + 1;
Suppress_Interrupts_Until := Now + Ada.Real_Time.Milliseconds (5);
end if;
end Handler;
end Button;
Expand All @@ -77,15 +95,12 @@ package body Debounce_Impl is
loop
Button.Wait;

PIOB_Periph.SODR.Arr := (Output_Pin => 1, others => 0);
delay until
Ada.Real_Time.Clock + Ada.Real_Time.Milliseconds (5);

-- Pull-up is enabled, so the pin is 0 if the button is pressed
if PIOB_Periph.PDSR.Arr (Input_Pin) /= 0 then
PIOB_Periph.CODR.Arr := (Output_Pin => 1, others => 0);
else
PIOB_Periph.SODR.Arr := (Output_Pin => 1, others => 0);
end if;
Ada.Real_Time.Clock + Ada.Real_Time.Milliseconds (100);
PIOB_Periph.CODR.Arr := (Output_Pin => 1, others => 0);
delay until
Ada.Real_Time.Clock + Ada.Real_Time.Milliseconds (100);
end loop;
end T;

Expand Down

0 comments on commit 71cee11

Please sign in to comment.