Skip to content

Commit 5c667c1

Browse files
committed
Copy sensors' data from the buffers into data structure.
1 parent bef33f0 commit 5c667c1

File tree

1 file changed

+150
-54
lines changed

1 file changed

+150
-54
lines changed

source/sensors.adb

Lines changed: 150 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,45 @@ package body Sensors is
4040

4141
type Buffer_2_Array is array (Positive range <>) of ADC2_Sensors_Data;
4242

43-
Buffer_1 : Buffer_1_Array (1 .. 120) with Export;
44-
Buffer_2 : Buffer_2_Array (1 .. 120) with Export;
43+
ADC1_Buffer : Buffer_1_Array (1 .. 120) with Export;
44+
ADC2_Buffer : Buffer_2_Array (1 .. 120) with Export;
4545
-- Buffers to receive data with DNA.
4646

47-
-- Data : Buffer_Array (1 .. 2_400) with Export;
48-
Last : Natural := 0;
47+
type Sensors_Data is record
48+
M1_Position : A0B.Types.Unsigned_16;
49+
M1_Current : A0B.Types.Unsigned_16;
50+
M2_Position : A0B.Types.Unsigned_16;
51+
M2_Current : A0B.Types.Unsigned_16;
52+
M3_Position : A0B.Types.Unsigned_16;
53+
M3_Current : A0B.Types.Unsigned_16;
54+
end record;
55+
56+
Data : array (1 .. 2_400) of Sensors_Data with Export;
57+
ADC1_Last : Natural := 0;
58+
ADC2_Last : Natural := 0;
4959
-- Buffer to accumulate data.
5060

5161
-- Current : Sensors_Data;
5262
Average_Position : A0B.Types.Unsigned_16;
5363

54-
procedure On_Conversion_Done;
64+
procedure On_ADC1_DMA;
65+
66+
procedure On_ADC2_DMA;
67+
68+
package On_ADC1_DMA_Callbacks is
69+
new A0B.Callbacks.Generic_Parameterless (On_ADC1_DMA);
5570

56-
package On_Conversion_Done_Callbacks is
57-
new A0B.Callbacks.Generic_Parameterless (On_Conversion_Done);
71+
package On_ADC2_DMA_Callbacks is
72+
new A0B.Callbacks.Generic_Parameterless (On_ADC2_DMA);
5873

5974
------------------
6075
-- Collect_Data --
6176
------------------
6277

6378
procedure Collect_Data is
6479
begin
65-
Last := 0;
80+
ADC1_Last := 0;
81+
ADC2_Last := 0;
6682
end Collect_Data;
6783

6884
----------
@@ -145,73 +161,154 @@ package body Sensors is
145161
use type A0B.Types.Unsigned_16;
146162

147163
begin
148-
Configuration.ADC1_DMA_CH.DMA_CH.Set_Interrupt_Callback
149-
(On_Conversion_Done_Callbacks.Create_Callback);
150164
Configuration.ADC1_DMA_CH.DMA_CH.Set_Memory
151-
(Memory_Address => Buffer_1'Address,
152-
Number_Of_Items => Buffer_1'Length * ADC1_Channels);
165+
(Memory_Address => ADC1_Buffer'Address,
166+
Number_Of_Items => ADC1_Buffer'Length * ADC1_Channels);
167+
Configuration.ADC1_DMA_CH.DMA_CH.Set_Interrupt_Callback
168+
(On_ADC1_DMA_Callbacks.Create_Callback);
169+
Configuration.ADC1_DMA_CH.DMA_CH.Enable_Transfer_Completed_Interrupt;
170+
Configuration.ADC1_DMA_CH.DMA_CH.Enable_Half_Transfer_Interrupt;
171+
Configuration.ADC1_DMA_CH.DMA_CH.Enable;
153172

154-
Configuration.ADC2_DMA_CH.DMA_CH.Set_Interrupt_Callback
155-
(On_Conversion_Done_Callbacks.Create_Callback);
156173
Configuration.ADC2_DMA_CH.DMA_CH.Set_Memory
157-
(Memory_Address => Buffer_2'Address,
158-
Number_Of_Items => Buffer_2'Length * ADC2_Channels);
159-
160-
null;
161-
162-
Configuration.ADC1_DMA_CH.DMA_CH.Enable;
174+
(Memory_Address => ADC2_Buffer'Address,
175+
Number_Of_Items => ADC2_Buffer'Length * ADC2_Channels);
176+
Configuration.ADC2_DMA_CH.DMA_CH.Set_Interrupt_Callback
177+
(On_ADC2_DMA_Callbacks.Create_Callback);
178+
Configuration.ADC2_DMA_CH.DMA_CH.Enable_Transfer_Completed_Interrupt;
179+
Configuration.ADC2_DMA_CH.DMA_CH.Enable_Half_Transfer_Interrupt;
163180
Configuration.ADC2_DMA_CH.DMA_CH.Enable;
164-
165-
-- Configuration.ADC1_DMA_Stream.Set_Memory_Buffer
166-
-- (Memory => Buffer'Address,
167-
-- Count => Buffer'Length * 9,
168-
-- Increment => True);
169-
-- Configuration.ADC1_DMA_Stream.Enable;
170-
-- Configuration.ADC1_DMA_Stream.Set_Interrupt_Callback
171-
-- (On_Conversion_Done_Callbacks.Create_Callback);
172-
-- Configuration.ADC1_DMA_Stream.Enable_Half_Transfer_Interrupt;
173-
-- Configuration.ADC1_DMA_Stream.Enable_Transfer_Complete_Interrupt;
174181
end Initialize;
175182

176-
------------------------
177-
-- On_Conversion_Done --
178-
------------------------
179-
180-
procedure On_Conversion_Done is
181-
-- F : Positive;
182-
-- L : Positive;
183+
-----------------
184+
-- On_ADC1_DMA --
185+
-----------------
183186

187+
procedure On_ADC1_DMA is
184188
begin
185-
Configuration.ADC1_DMA_CH.DMA_CH.Disable;
186-
Configuration.ADC2_DMA_CH.DMA_CH.Disable;
189+
-- Configuration.ADC1_DMA_CH.DMA_CH.Disable;
187190

188-
raise Program_Error;
189-
-- if Configuration.ADC1_DMA_Stream.Get_Masked_And_Clear_Half_Transfer then
190-
-- if Last <= Data'Last - Buffer'Length / 2 then
191+
if Configuration.ADC1_DMA_CH.DMA_CH.Get_Masked_And_Clear_Half_Transfer
192+
then
193+
if ADC1_Last <= Data'Last - ADC1_Buffer'Length / 2 then
191194
-- F := Buffer'First;
192195
-- L := Buffer'Length / 2;
196+
197+
for J in ADC1_Buffer'First
198+
.. ADC1_Buffer'First + ADC1_Buffer'Length / 2 - 1
199+
loop
200+
ADC1_Last := @ + 1;
201+
Data (ADC1_Last).M1_Current := ADC1_Buffer (J).M1_Current;
202+
Data (ADC1_Last).M1_Position := ADC1_Buffer (J).M1_Position;
203+
Data (ADC1_Last).M2_Position := ADC1_Buffer (J).M2_Position;
204+
end loop;
205+
206+
else
207+
raise Program_Error;
208+
end if;
209+
210+
-- Current := Buffer (Buffer'Length / 2);
211+
end if;
212+
213+
if Configuration.ADC1_DMA_CH.DMA_CH
214+
.Get_Masked_And_Clear_Transfer_Completed
215+
then
216+
if ADC1_Last <= Data'Last - ADC1_Buffer'Length / 2 then
217+
-- F := Buffer'First;
218+
-- L := Buffer'Length / 2;
219+
220+
for J in ADC1_Buffer'First + ADC1_Buffer'Length / 2
221+
.. ADC1_Buffer'Last
222+
loop
223+
ADC1_Last := @ + 1;
224+
Data (ADC1_Last).M1_Current := ADC1_Buffer (J).M1_Current;
225+
Data (ADC1_Last).M1_Position := ADC1_Buffer (J).M1_Position;
226+
Data (ADC1_Last).M2_Position := ADC1_Buffer (J).M2_Position;
227+
end loop;
228+
-- Data (Last + 1 .. Last + Buffer'Length / 2) :=
229+
-- Buffer (Buffer'First + Buffer'Length / 2 .. Buffer'Last);
230+
-- Last := @ + Buffer'Length / 2;
231+
232+
else
233+
raise Program_Error;
234+
end if;
193235
--
236+
-- Current := Buffer (Buffer'Last);
237+
end if;
238+
239+
-- declare
240+
-- use type A0B.Types.Unsigned_32;
241+
--
242+
-- A : A0B.Types.Unsigned_32 := 0;
243+
--
244+
-- begin
245+
-- for J in F .. L loop
246+
-- A := @ + A0B.Types.Unsigned_32 (Buffer (J).M1_Position);
247+
-- end loop;
248+
--
249+
-- Average_Position :=
250+
-- A0B.Types.Unsigned_16 (A / A0B.Types.Unsigned_32 (L - F + 1));
251+
-- end;
252+
--
253+
-- Control.Iteration;
254+
null;
255+
end On_ADC1_DMA;
256+
257+
-----------------
258+
-- On_ADC2_DMA --
259+
-----------------
260+
261+
procedure On_ADC2_DMA is
262+
begin
263+
if Configuration.ADC2_DMA_CH.DMA_CH.Get_Masked_And_Clear_Half_Transfer
264+
then
265+
if ADC2_Last <= Data'Last - ADC2_Buffer'Length / 2 then
266+
-- F := Buffer'First;
267+
-- L := Buffer'Length / 2;
268+
269+
for J in ADC2_Buffer'First
270+
.. ADC2_Buffer'First + ADC2_Buffer'Length / 2 - 1
271+
loop
272+
ADC2_Last := @ + 1;
273+
Data (ADC2_Last).M2_Current := ADC2_Buffer (J).M2_Current;
274+
Data (ADC2_Last).M3_Current := ADC2_Buffer (J).M3_Current;
275+
Data (ADC2_Last).M3_Position := ADC2_Buffer (J).M3_Position;
276+
end loop;
194277
-- Data (Last + 1 .. Last + Buffer'Length / 2) :=
195278
-- Buffer (Buffer'First .. Buffer'Length / 2);
196279
-- Last := @ + Buffer'Length / 2;
197-
-- end if;
280+
281+
else
282+
raise Program_Error;
283+
end if;
198284
--
199285
-- Current := Buffer (Buffer'Length / 2);
200-
-- end if;
201-
--
202-
-- if Configuration.ADC1_DMA_Stream.Get_Masked_And_Clear_Transfer_Completed
203-
-- then
286+
end if;
287+
288+
if Configuration.ADC2_DMA_CH.DMA_CH
289+
.Get_Masked_And_Clear_Transfer_Completed
290+
then
204291
-- F := Buffer'First;
205292
-- L := Buffer'Length / 2;
206-
--
207-
-- if Last <= Data'Last - Buffer'Length / 2 then
293+
294+
if ADC2_Last <= Data'Last - ADC2_Buffer'Length / 2 then
295+
for J in ADC2_Buffer'First + ADC2_Buffer'Length / 2
296+
.. ADC2_Buffer'Last
297+
loop
298+
ADC2_Last := @ + 1;
299+
Data (ADC2_Last).M2_Current := ADC2_Buffer (J).M2_Current;
300+
Data (ADC2_Last).M3_Current := ADC2_Buffer (J).M3_Current;
301+
Data (ADC2_Last).M3_Position := ADC2_Buffer (J).M3_Position;
302+
end loop;
208303
-- Data (Last + 1 .. Last + Buffer'Length / 2) :=
209304
-- Buffer (Buffer'First + Buffer'Length / 2 .. Buffer'Last);
210305
-- Last := @ + Buffer'Length / 2;
211-
-- end if;
212-
--
306+
else
307+
raise Program_Error;
308+
end if;
309+
213310
-- Current := Buffer (Buffer'Last);
214-
-- end if;
311+
end if;
215312
--
216313
-- declare
217314
-- use type A0B.Types.Unsigned_32;
@@ -228,7 +325,6 @@ package body Sensors is
228325
-- end;
229326
--
230327
-- Control.Iteration;
231-
null;
232-
end On_Conversion_Done;
328+
end On_ADC2_DMA;
233329

234330
end Sensors;

0 commit comments

Comments
 (0)