Skip to content

Commit cc205b1

Browse files
pat-rogersjklmnn
authored andcommitted
Add support for dual channel conversion
1 parent 6ab6b1a commit cc205b1

2 files changed

Lines changed: 96 additions & 25 deletions

File tree

arch/ARM/STM32/drivers/stm32-dac.adb

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
------------------------------------------------------------------------------
22
-- --
3-
-- Copyright (C) 2015, AdaCore --
3+
-- Copyright (C) 2015-2026, AdaCore --
44
-- --
55
-- Redistribution and use in source and binary forms, with or without --
66
-- modification, are permitted provided that the following conditions are --
@@ -115,11 +115,9 @@ package body STM32.DAC is
115115
when DAC_Resolution_12_Bits =>
116116
case Alignment is
117117
when Left_Aligned =>
118-
This.DHR12L1.DACC1DHR :=
119-
UInt12 (Value and Max_12bit_Resolution);
118+
This.DHR12L1.DACC1DHR := UInt12 (Value and Max_12bit_Resolution);
120119
when Right_Aligned =>
121-
This.DHR12R1.DACC1DHR :=
122-
UInt12 (Value and Max_12bit_Resolution);
120+
This.DHR12R1.DACC1DHR := UInt12 (Value and Max_12bit_Resolution);
123121
end case;
124122
when DAC_Resolution_8_Bits =>
125123
This.DHR8R1.DACC1DHR := UInt8 (Value and Max_8bit_Resolution);
@@ -130,11 +128,9 @@ package body STM32.DAC is
130128
when DAC_Resolution_12_Bits =>
131129
case Alignment is
132130
when Left_Aligned =>
133-
This.DHR12L2.DACC2DHR :=
134-
UInt12 (Value and Max_12bit_Resolution);
131+
This.DHR12L2.DACC2DHR := UInt12 (Value and Max_12bit_Resolution);
135132
when Right_Aligned =>
136-
This.DHR12R2.DACC2DHR :=
137-
UInt12 (Value and Max_12bit_Resolution);
133+
This.DHR12R2.DACC2DHR := UInt12 (Value and Max_12bit_Resolution);
138134
end case;
139135
when DAC_Resolution_8_Bits =>
140136
This.DHR8R2.DACC2DHR := UInt8 (Value and Max_8bit_Resolution);
@@ -143,6 +139,35 @@ package body STM32.DAC is
143139
end case;
144140
end Set_Output;
145141

142+
-----------------------------
143+
-- Set_Dual_Channel_Output --
144+
-----------------------------
145+
146+
procedure Set_Dual_Channel_Output
147+
(This : in out Digital_To_Analog_Converter;
148+
Channel_2_Data : UInt16;
149+
Channel_1_Data : UInt16;
150+
Resolution : DAC_Resolution;
151+
Alignment : Data_Alignment)
152+
is
153+
begin
154+
-- See RM0385 Rev 8, sections 16.5.9 .. 16.5.11 for these registers
155+
case Resolution is
156+
when DAC_Resolution_12_Bits =>
157+
case Alignment is
158+
when Left_Aligned =>
159+
This.DHR12LD.DACC2DHR := UInt12 (Channel_2_Data and Max_12bit_Resolution);
160+
This.DHR12LD.DACC1DHR := UInt12 (Channel_1_Data and Max_12bit_Resolution);
161+
when Right_Aligned =>
162+
This.DHR12RD.DACC2DHR := UInt12 (Channel_2_Data and Max_12bit_Resolution);
163+
This.DHR12RD.DACC1DHR := UInt12 (Channel_1_Data and Max_12bit_Resolution);
164+
end case;
165+
when DAC_Resolution_8_Bits =>
166+
This.DHR8RD.DACC2DHR := UInt8 (Channel_2_Data and Max_8bit_Resolution);
167+
This.DHR8RD.DACC1DHR := UInt8 (Channel_1_Data and Max_8bit_Resolution);
168+
end case;
169+
end Set_Dual_Channel_Output;
170+
146171
------------------------------------
147172
-- Trigger_Conversion_By_Software --
148173
------------------------------------
@@ -194,21 +219,15 @@ package body STM32.DAC is
194219
when DAC_Resolution_12_Bits =>
195220
case Alignment is
196221
when Left_Aligned =>
197-
This.DHR12LD.DACC1DHR :=
198-
UInt12 (Channel_1_Value and Max_12bit_Resolution);
199-
This.DHR12LD.DACC2DHR :=
200-
UInt12 (Channel_2_Value and Max_12bit_Resolution);
222+
This.DHR12LD.DACC1DHR := UInt12 (Channel_1_Value and Max_12bit_Resolution);
223+
This.DHR12LD.DACC2DHR := UInt12 (Channel_2_Value and Max_12bit_Resolution);
201224
when Right_Aligned =>
202-
This.DHR12RD.DACC1DHR :=
203-
UInt12 (Channel_1_Value and Max_12bit_Resolution);
204-
This.DHR12RD.DACC2DHR :=
205-
UInt12 (Channel_2_Value and Max_12bit_Resolution);
225+
This.DHR12RD.DACC1DHR := UInt12 (Channel_1_Value and Max_12bit_Resolution);
226+
This.DHR12RD.DACC2DHR := UInt12 (Channel_2_Value and Max_12bit_Resolution);
206227
end case;
207228
when DAC_Resolution_8_Bits =>
208-
This.DHR8RD.DACC1DHR :=
209-
UInt8 (Channel_1_Value and Max_8bit_Resolution);
210-
This.DHR8RD.DACC2DHR :=
211-
UInt8 (Channel_2_Value and Max_8bit_Resolution);
229+
This.DHR8RD.DACC1DHR := UInt8 (Channel_1_Value and Max_8bit_Resolution);
230+
This.DHR8RD.DACC2DHR := UInt8 (Channel_2_Value and Max_8bit_Resolution);
212231
end case;
213232
end Set_Dual_Output_Voltages;
214233

@@ -682,10 +701,36 @@ package body STM32.DAC is
682701
when DAC_Resolution_8_Bits =>
683702
Result := This.DHR8R2'Address;
684703
end case;
685-
686704
end case;
687705

688706
return Result;
689707
end Data_Address;
690708

709+
----------------------------------
710+
-- Data_Address_Dual_Conversion --
711+
----------------------------------
712+
713+
function Data_Address_Dual_Conversion
714+
(This : Digital_To_Analog_Converter;
715+
Resolution : DAC_Resolution;
716+
Alignment : Data_Alignment)
717+
return Address
718+
is
719+
Result : Address;
720+
begin
721+
case Resolution is
722+
when DAC_Resolution_12_Bits =>
723+
case Alignment is
724+
when Left_Aligned =>
725+
Result := This.DHR12LD'Address;
726+
when Right_Aligned =>
727+
Result := This.DHR12RD'Address;
728+
end case;
729+
when DAC_Resolution_8_Bits =>
730+
Result := This.DHR8RD'Address;
731+
end case;
732+
733+
return Result;
734+
end Data_Address_Dual_Conversion;
735+
691736
end STM32.DAC;

arch/ARM/STM32/drivers/stm32-dac.ads

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
------------------------------------------------------------------------------
22
-- --
3-
-- Copyright (C) 2015, AdaCore --
3+
-- Copyright (C) 2015-2026, AdaCore --
44
-- --
55
-- Redistribution and use in source and binary forms, with or without --
66
-- modification, are permitted provided that the following conditions are --
@@ -51,7 +51,9 @@ package STM32.DAC is
5151

5252
type DAC_Channel is (Channel_1, Channel_2);
5353

54-
-- Note that Channel 1 is tied to GPIO pin PA4, and Channel 2 to PA5
54+
-- Note that Channel 1 is tied to GPIO pin PA4, and Channel 2 to PA5.
55+
-- Note that Channel 1 is mapped to DMA1 Stream 5 channel 7.
56+
-- Note that Channel 2 is mapped on DMA1 Stream 6 channel 7.
5557

5658
procedure Enable
5759
(This : in out Digital_To_Analog_Converter;
@@ -88,6 +90,8 @@ package STM32.DAC is
8890
Max_8bit_Resolution : constant := 16#00FF#;
8991

9092
type Data_Alignment is (Left_Aligned, Right_Aligned);
93+
-- These only apply when using 12-bit resolution. For 8-bit resolution the
94+
-- alignment is always right-aligned.
9195

9296
procedure Set_Output
9397
(This : in out Digital_To_Analog_Converter;
@@ -102,6 +106,18 @@ package STM32.DAC is
102106
-- the reference input voltage and the 'n' of Max_nbit_Counts is either 12
103107
-- or 8.
104108

109+
procedure Set_Dual_Channel_Output
110+
(This : in out Digital_To_Analog_Converter;
111+
Channel_2_Data : UInt16;
112+
Channel_1_Data : UInt16;
113+
Resolution : DAC_Resolution;
114+
Alignment : Data_Alignment);
115+
-- This routine writes the 32-bit value, composed from the two 16-bit
116+
-- values, with the necessary layout (8/12left-right alignment) to
117+
-- the appropriate output register for conversion on both channels
118+
-- simultaneously. DMA is the alternative to software calling this
119+
-- procedure.
120+
105121
procedure Trigger_Conversion_By_Software
106122
(This : in out Digital_To_Analog_Converter;
107123
Channel : DAC_Channel)
@@ -328,7 +344,17 @@ package STM32.DAC is
328344
Alignment : Data_Alignment)
329345
return Address;
330346
-- Returns the address of the Data Holding register within This, for the
331-
-- specified Channel, at the specified Resolution and Alignment.
347+
-- specified Channel, given the specified Resolution and Alignment.
348+
--
349+
-- This function is stricly for use with DMA, all others use the API above.
350+
351+
function Data_Address_Dual_Conversion
352+
(This : Digital_To_Analog_Converter;
353+
Resolution : DAC_Resolution;
354+
Alignment : Data_Alignment)
355+
return Address;
356+
-- Returns the address of the Data Holding register within This, for the
357+
-- dual conversion case, given the specified Resolution and Alignment.
332358
--
333359
-- This function is stricly for use with DMA, all others use the API above.
334360

0 commit comments

Comments
 (0)