Skip to content

Commit 404b55a

Browse files
committed
Initialize ADC and obtain samples.
1 parent 40be1b7 commit 404b55a

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed

source/configuration.adb

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
--
66

7+
with A0B.STM32F401.SVD.ADC;
78
with A0B.STM32F401.SVD.RCC;
89
with A0B.STM32F401.SVD.TIM;
910
with A0B.STM32F401.TIM_Lines;
@@ -21,6 +22,8 @@ is
2122

2223
procedure Initialize_GPIO;
2324

25+
procedure Initialize_ADC1;
26+
2427
procedure Initialize_TIM3;
2528
-- Configure TIM3. Timer is disabled. It generates TRGO on CEN set.
2629

@@ -52,12 +55,168 @@ is
5255
begin
5356
Initialize_GPIO;
5457
Initialize_UART;
58+
Initialize_ADC1;
5559
Initialize_TIM3;
5660
Initialize_TIM4;
5761

5862
Enable_Timers;
5963
end Initialize;
6064

65+
---------------------
66+
-- Initialize_ADC1 --
67+
---------------------
68+
69+
procedure Initialize_ADC1 is
70+
use A0B.STM32F401.SVD.ADC;
71+
72+
begin
73+
A0B.STM32F401.SVD.RCC.RCC_Periph.APB2ENR.ADC1EN := True;
74+
75+
-- Clear SR - Not needed
76+
77+
-- Configure CR1
78+
79+
declare
80+
Aux : A0B.STM32F401.SVD.ADC.CR1_Register := ADC1_Periph.CR1;
81+
82+
begin
83+
-- Aux.AWDCH := <>; -- Not used
84+
Aux.EOCIE := False; -- 0: EOC interrupt disabled
85+
Aux.AWDIE := False; -- 0: Analog watchdog interrupt disabled
86+
Aux.JEOCIE := False; -- 0: JEOC interrupt disabled
87+
Aux.SCAN := True; -- 1: Scan mode enabled
88+
-- Aux.AWDSGL := <>; -- Not used
89+
Aux.JAUTO := False;
90+
-- 0: Automatic injected group conversion disabled
91+
Aux.DISCEN := False;
92+
-- 0: Discontinuous mode on regular channels disabled
93+
Aux.JDISCEN := False;
94+
-- 0: Discontinuous mode on injected channels disabled
95+
-- Aux.DISCNUM := <>; -- Not used
96+
Aux.JAWDEN := False;
97+
-- 0: Analog watchdog disabled on injected channels
98+
Aux.AWDIE := False;
99+
-- 0: Analog watchdog disabled on regular channels
100+
Aux.RES := 2#00#; -- 00: 12-bit (15 ADCCLK cycles)
101+
Aux.OVRIE := False; -- 0: Overrun interrupt disabled
102+
103+
ADC1_Periph.CR1 := Aux;
104+
end;
105+
106+
-- Configure CR2
107+
108+
declare
109+
Aux : A0B.STM32F401.SVD.ADC.CR2_Register := ADC1_Periph.CR2;
110+
111+
begin
112+
Aux.ADON := False;
113+
-- 0: Disable ADC conversion and go to power down mode
114+
Aux.CONT := False; -- 0: Single conversion mode
115+
Aux.DMA := False; -- 0: DMA mode disabled
116+
Aux.DDS := True;
117+
-- 1: DMA requests are issued as long as data are converted and DMA=1
118+
Aux.EOCS := True;
119+
-- 1: The EOC bit is set at the end of each regular conversion.
120+
-- Overrun detection is enabled.
121+
Aux.ALIGN := False; -- 0: Right alignment
122+
-- Aux.JEXTSEL := <>; -- Not used
123+
Aux.JEXTEN := 2#00#; -- 00: Trigger detection disabled
124+
Aux.JSWSTART := False; -- 0: Reset state
125+
Aux.EXTSEL := 2#1010#; -- 1010: Timer 5 CC1 event
126+
Aux.EXTEN := 2#01#; -- 01: Trigger detection on the rising edge
127+
Aux.SWSTART := False; -- 0: Reset state
128+
129+
ADC1_Periph.CR2 := Aux;
130+
end;
131+
132+
-- Configure SMPR1
133+
134+
ADC1_Periph.SMPR1 := 0; -- 000: 3 cycles, for channels 10..18
135+
136+
-- Configure SMPR2
137+
138+
ADC1_Periph.SMPR2 := 0; -- 000: 3 cycles, for channels 0..9
139+
140+
-- Configure JOFR1 - Not used
141+
142+
-- Configure JOFR2 - Not used
143+
144+
-- Configure JOFR3 - Not used
145+
146+
-- Configure JOFR4 - Not used
147+
148+
-- Configure HTR - Not used
149+
150+
-- Configure LTR - Not used
151+
152+
-- Configure SQR1
153+
154+
declare
155+
Aux : A0B.STM32F401.SVD.ADC.SQR1_Register := ADC1_Periph.SQR1;
156+
157+
begin
158+
-- Aux.SQ.Arr (16) := <>; -- Not used
159+
-- Aux.SQ.Arr (15) := <>; -- Not used
160+
-- Aux.SQ.Arr (14) := <>; -- Not used
161+
-- Aux.SQ.Arr (13) := <>; -- Not used
162+
Aux.L := 2#1000#; -- 1000: 9 conversions
163+
164+
ADC1_Periph.SQR1 := Aux;
165+
end;
166+
167+
-- Configure SQR2
168+
169+
declare
170+
Aux : A0B.STM32F401.SVD.ADC.SQR2_Register := ADC1_Periph.SQR2;
171+
172+
begin
173+
-- Aux.SQ.Arr (12) := <>; -- Not used
174+
-- Aux.SQ.Arr (11) := <>; -- Not used
175+
-- Aux.SQ.Arr (10) := <>; -- Not used
176+
Aux.SQ.Arr (9) := 7; -- IN7
177+
Aux.SQ.Arr (8) := 6; -- IN6
178+
Aux.SQ.Arr (7) := 5; -- IN5
179+
180+
ADC1_Periph.SQR2 := Aux;
181+
end;
182+
183+
-- Configure SQR3
184+
185+
declare
186+
Aux : A0B.STM32F401.SVD.ADC.SQR3_Register := ADC1_Periph.SQR3;
187+
188+
begin
189+
Aux.SQ.Arr (6) := 4; -- IN4
190+
Aux.SQ.Arr (5) := 3; -- IN3
191+
Aux.SQ.Arr (4) := 2; -- IN2
192+
Aux.SQ.Arr (3) := 1; -- IN1
193+
Aux.SQ.Arr (2) := 0; -- IN0
194+
Aux.SQ.Arr (1) := 17; -- IN17 VREFINT
195+
196+
ADC1_Periph.SQR3 := Aux;
197+
end;
198+
199+
-- Configure JSQR - Not used
200+
201+
-- Configure CCR
202+
203+
declare
204+
Aux : CCR_Register := ADC_Common_Periph.CCR;
205+
206+
begin
207+
Aux.ADCPRE := 2#01#; -- PCLK2 divided by 4
208+
Aux.VBATE := False; -- 0: VBAT channel disabled
209+
Aux.TSVREFE := True;
210+
-- 1: Temperature sensor and VREFINT channel enabled
211+
212+
ADC_Common_Periph.CCR := Aux;
213+
end;
214+
215+
-- Enable ADC
216+
217+
ADC1_Periph.CR2.ADON := True;
218+
end Initialize_ADC1;
219+
61220
---------------------
62221
-- Initialize_GPIO --
63222
---------------------

source/driver.adb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
with Configuration;
88
with Console;
99
with Motor_Drivers;
10+
with Sensors;
1011

1112
procedure Driver is
1213
Motor : Motor_Drivers.Motor_Index := 1;
@@ -56,6 +57,11 @@ begin
5657
Console.Put_Line ("off");
5758
Motor_Drivers.Set_Off (Motor);
5859

60+
when 'a' | 'A' =>
61+
Console.Put_Line ("collect sensors data");
62+
Sensors.Collect_Data;
63+
Sensors.Dump;
64+
5965
when others =>
6066
Console.Put_Line ("unknown command");
6167
end case;

source/sensors.adb

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
--
2+
-- Copyright (C) 2025, Vadim Godunko <vgodunko@gmail.com>
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
--
6+
7+
with A0B.STM32F401.SVD.ADC;
8+
with A0B.Types;
9+
10+
with Console;
11+
12+
package body Sensors is
13+
14+
type Sensors_Data is record
15+
Vref : A0B.Types.Unsigned_16;
16+
M1_Current : A0B.Types.Unsigned_16;
17+
M1_Position : A0B.Types.Unsigned_16;
18+
M2_Current : A0B.Types.Unsigned_16;
19+
M2_Position : A0B.Types.Unsigned_16;
20+
M3_Current : A0B.Types.Unsigned_16;
21+
M3_Position : A0B.Types.Unsigned_16;
22+
M4_Current : A0B.Types.Unsigned_16;
23+
M4_Position : A0B.Types.Unsigned_16;
24+
end record;
25+
26+
Data : array (1 .. 1_000) of Sensors_Data with Export;
27+
28+
------------------
29+
-- Collect_Data --
30+
------------------
31+
32+
procedure Collect_Data is
33+
use A0B.STM32F401.SVD.ADC;
34+
35+
function Get return A0B.Types.Unsigned_16;
36+
37+
---------
38+
-- Get --
39+
---------
40+
41+
function Get return A0B.Types.Unsigned_16 is
42+
begin
43+
while not ADC1_Periph.SR.EOC loop
44+
null;
45+
end loop;
46+
47+
return ADC1_Periph.DR.DATA;
48+
end Get;
49+
50+
begin
51+
for Item of Data loop
52+
ADC1_Periph.CR2.SWSTART := True;
53+
Item.Vref := Get;
54+
Item.M1_Current := Get;
55+
Item.M1_Position := Get;
56+
Item.M2_Current := Get;
57+
Item.M2_Position := Get;
58+
Item.M3_Current := Get;
59+
Item.M3_Position := Get;
60+
Item.M4_Current := Get;
61+
Item.M4_Position := Get;
62+
end loop;
63+
end Collect_Data;
64+
65+
----------
66+
-- Dump --
67+
----------
68+
69+
procedure Dump is
70+
begin
71+
Console.New_Line;
72+
Console.Put_Line ("Vref");
73+
74+
for J in Data'Range loop
75+
if (J - 1) mod 20 = 0 then
76+
Console.New_Line;
77+
end if;
78+
79+
Console.Put (A0B.Types.Unsigned_16'Image (Data (J).Vref));
80+
end loop;
81+
82+
Console.New_Line;
83+
84+
Console.New_Line;
85+
Console.Put_Line ("M1 current");
86+
87+
for J in Data'Range loop
88+
if (J - 1) mod 20 = 0 then
89+
Console.New_Line;
90+
end if;
91+
92+
Console.Put (A0B.Types.Unsigned_16'Image (Data (J).M1_Current));
93+
end loop;
94+
95+
Console.New_Line;
96+
97+
Console.New_Line;
98+
Console.Put_Line ("M1 position");
99+
100+
for J in Data'Range loop
101+
if (J - 1) mod 20 = 0 then
102+
Console.New_Line;
103+
end if;
104+
105+
Console.Put (A0B.Types.Unsigned_16'Image (Data (J).M1_Position));
106+
end loop;
107+
108+
Console.New_Line;
109+
end Dump;
110+
111+
end Sensors;

source/sensors.ads

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--
2+
-- Copyright (C) 2025, Vadim Godunko <vgodunko@gmail.com>
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
--
6+
7+
package Sensors is
8+
9+
procedure Collect_Data;
10+
11+
procedure Dump;
12+
13+
end Sensors;

0 commit comments

Comments
 (0)