44--   SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55-- 
66
7- --   with A0B.STM32F401.SVD.ADC;
7+ pragma  Ada_2022;
8+ 
9+ with  A0B.Callbacks.Generic_Parameterless ;
810with  A0B.Types ;
911
1012with  Configuration ;
@@ -24,52 +26,44 @@ package body Sensors is
2426      M4_Position : A0B.Types.Unsigned_16;
2527   end  record ;
2628
27-    Data : array  (1  .. 1_000 ) of  Sensors_Data with  Export;
29+    type  Buffer_Array is  array  (Positive range  <>) of  Sensors_Data;
30+ 
31+    Buffer : Buffer_Array (1  .. 120 ) with  Export;
32+    --   Buffer to receive data with DNA.
33+ 
34+    Data   : Buffer_Array (1  .. 1_000 ) with  Export;
35+    Last   : Natural := 0 ;
36+    --   Buffer to accumulate data.
37+ 
38+    procedure  On_Conversion_Done ;
39+ 
40+    package  On_Conversion_Done_Callbacks  is 
41+       new  A0B.Callbacks.Generic_Parameterless (On_Conversion_Done);
2842
2943   -- ----------------
3044   --  Collect_Data --
3145   -- ----------------
3246
3347   procedure  Collect_Data  is 
34-       --   use A0B.STM32F401.SVD.ADC;
35- 
36-       --   function Get return A0B.Types.Unsigned_16;
37-       -- 
38-       --   ---------
39-       --   -- Get --
40-       --   ---------
41-       -- 
42-       --   function Get return A0B.Types.Unsigned_16 is
43-       --   begin
44-       --      while not ADC1_Periph.SR.EOC loop
45-       --         null;
46-       --      end loop;
47-       -- 
48-       --      return ADC1_Periph.DR.DATA;
49-       --   end Get;
50- 
5148   begin 
52-       null ;
53- 
54-       --   for Item of Data loop
55-       --   ADC1_Periph.CR2.SWSTART := True;
56-          --   Item.Vref        := Get;
57-          --   Item.M1_Current  := Get;
58-          --   Item.M1_Position := Get;
59-          --   Item.M2_Current  := Get;
60-          --   Item.M2_Position := Get;
61-          --   Item.M3_Current  := Get;
62-          --   Item.M3_Position := Get;
63-          --   Item.M4_Current  := Get;
64-          --   Item.M4_Position := Get;
65-       --   end loop;
49+       Last := 0 ;
6650   end  Collect_Data ;
6751
6852   -- --------
6953   --  Dump --
7054   -- --------
7155
7256   procedure  Dump  is 
57+ 
58+       procedure  Put  (Item : A0B.Types.Unsigned_16) is 
59+          Image  : constant  String := A0B.Types.Unsigned_16'Image (Item);
60+          Buffer : String (1  .. 5 ) := (others  => '  '  );
61+ 
62+       begin 
63+          Buffer (Buffer'Last - Image'Length + 1  .. Buffer'Last) := Image;
64+          Console.Put (Buffer);
65+       end  Put ;
66+ 
7367   begin 
7468      Console.New_Line;
7569      Console.Put_Line (" Vref"  );
@@ -88,11 +82,13 @@ package body Sensors is
8882      Console.Put_Line (" M1 current"  );
8983
9084      for  J in  Data'Range  loop 
91-          if  (J - 1 ) mod  20  = 0  then 
85+          if  (J - 1 ) mod  30  = 0  then 
9286            Console.New_Line;
87+          elsif  (J - 1 ) mod  6  = 0  then 
88+             Console.Put ("   "  );
9389         end  if ;
9490
95-          Console. Put (A0B.Types.Unsigned_16'Image ( Data (J).M1_Current) );
91+          Put (Data (J).M1_Current);
9692      end  loop ;
9793
9894      Console.New_Line;
@@ -116,12 +112,42 @@ package body Sensors is
116112   -- --------------
117113
118114   procedure  Initialize  is 
115+       use  type  A0B.Types.Unsigned_16;
116+ 
119117   begin 
120118      Configuration.ADC1_DMA_Stream.Set_Memory_Buffer
121-         (Memory    => Data 'Address,
122-          Count     => 9_000 ,
119+         (Memory    => Buffer 'Address,
120+          Count     => Buffer'Length *  9 ,
123121         Increment => True);
124122      Configuration.ADC1_DMA_Stream.Enable;
123+       Configuration.ADC1_DMA_Stream.Set_Interrupt_Callback
124+         (On_Conversion_Done_Callbacks.Create_Callback);
125+       Configuration.ADC1_DMA_Stream.Enable_Half_Transfer_Interrupt;
126+       Configuration.ADC1_DMA_Stream.Enable_Transfer_Complete_Interrupt;
125127   end  Initialize ;
126128
129+    -- ----------------------
130+    --  On_Conversion_Done --
131+    -- ----------------------
132+ 
133+    procedure  On_Conversion_Done  is 
134+    begin 
135+       if  Configuration.ADC1_DMA_Stream.Get_Masked_And_Clear_Half_Transfer then 
136+          if  Last < Data'Last - Buffer'Length then 
137+             Data (Last + 1  .. Last + Buffer'Length / 2 ) :=
138+               Buffer (Buffer'First .. Buffer'Length / 2 );
139+             Last := @ + Buffer'Length / 2 ;
140+          end  if ;
141+       end  if ;
142+ 
143+       if  Configuration.ADC1_DMA_Stream.Get_Masked_And_Clear_Transfer_Completed
144+       then 
145+          if  Last < Data'Last - Buffer'Length then 
146+             Data (Last + 1  .. Last + Buffer'Length / 2 ) :=
147+               Buffer (Buffer'First + Buffer'Length / 2  .. Buffer'Last);
148+             Last := @ + Buffer'Length / 2 ;
149+          end  if ;
150+       end  if ;
151+    end  On_Conversion_Done ;
152+ 
127153end  Sensors ;
0 commit comments