Skip to content

Commit d365090

Browse files
committed
Setup TIM5 to start ADC sampling and conversions.
1 parent f01b1ad commit d365090

File tree

5 files changed

+200
-25
lines changed

5 files changed

+200
-25
lines changed

source/configuration.adb

Lines changed: 174 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ package body Configuration
1515
with Preelaborate
1616
is
1717

18-
Divider : constant := 2#00#; -- 00: tDTS = tCK_INT
19-
Prescale : constant := 1;
20-
Cycle : constant := 3_360;
18+
Divider : constant := 2#00#; -- 00: tDTS = tCK_INT
19+
Prescale : constant := 1;
20+
PWM_Cycle : constant := 3_360;
2121
-- 1/1/3_360: PWM 25kHz CPU @84MHz (nominal for L298)
22+
ADC_Cycle : constant := 560;
23+
-- 1/1/560: 6x ADC samples per PWM cycle
2224

2325
procedure Initialize_GPIO;
2426

@@ -27,16 +29,18 @@ is
2729
procedure Initialize_ADC1;
2830

2931
procedure Initialize_TIM3;
30-
-- Configure TIM3. Timer is disabled. It generates TRGO on CEN set.
32+
-- Configure TIM3 to generate PWM. Timer is disabled. It generates TRGO on
33+
-- CEN set.
3134

3235
procedure Initialize_TIM4;
33-
-- Configure TIM4. Timer is disabled. It is configured to be triggered by
34-
-- set of CEN of TIM3.
36+
-- Configure TIM4 to generate PWM. Timer is disabled. It is configured to
37+
-- be triggered by set of CEN of TIM3.
3538

36-
procedure Initialize_UART;
39+
procedure Initialize_TIM5;
40+
-- Configure TIM5 to initiate ADC sampling. Timer is disabled. It is
41+
-- configured to be triggered by set of CEN of TIM3.
3742

38-
procedure Enable_Timers;
39-
-- Enable all timers.
43+
procedure Initialize_UART;
4044

4145
-------------------
4246
-- Enable_Timers --
@@ -61,8 +65,7 @@ is
6165
Initialize_ADC1;
6266
Initialize_TIM3;
6367
Initialize_TIM4;
64-
65-
Enable_Timers;
68+
Initialize_TIM5;
6669
end Initialize;
6770

6871
---------------------
@@ -114,16 +117,18 @@ is
114117
begin
115118
Aux.ADON := False;
116119
-- 0: Disable ADC conversion and go to power down mode
117-
Aux.CONT := True; -- 1: Continuous conversion mode
118-
-- XXX Aux.CONT := False; -- 0: Single conversion mode
119-
Aux.DMA := True; -- 1: DMA mode enabled
120+
Aux.CONT := False; -- 0: Single conversion mode
121+
Aux.DMA := True; -- 1: DMA mode enabled
120122
Aux.DDS := False;
121123
-- 0: No new DMA request is issued after the last transfer (as
122124
-- configured in the DMA controller)
123125
-- XXX Aux.DDS := True;
124126
-- XXX 1: DMA requests are issued as long as data are converted and
125127
-- DMA=1
126-
Aux.EOCS := True;
128+
Aux.EOCS := False;
129+
-- 0: The EOC bit is set at the end of each sequence of regular
130+
-- conversions. Overrun detection is enabled only if DMA=1.
131+
-- XXX Aux.EOCS := True;
127132
-- 1: The EOC bit is set at the end of each regular conversion.
128133
-- Overrun detection is enabled.
129134
Aux.ALIGN := False; -- 0: Right alignment
@@ -520,7 +525,7 @@ is
520525

521526
-- Set ARR (TIM3/TIM4 support low part only)
522527

523-
TIM.ARR.ARR_L := Cycle - 1;
528+
TIM.ARR.ARR_L := PWM_Cycle - 1;
524529

525530
-- Set CCR1/CCR2/CCR3/CCR4 later
526531

@@ -741,7 +746,7 @@ is
741746

742747
-- Set ARR (TIM3/TIM4 support low part only)
743748

744-
TIM.ARR.ARR_L := Cycle - 1;
749+
TIM.ARR.ARR_L := PWM_Cycle - 1;
745750

746751
-- Set CCR1/CCR2/CCR3/CCR4 later
747752

@@ -755,6 +760,158 @@ is
755760
TIM.EGR.UG := True;
756761
end Initialize_TIM4;
757762

763+
---------------------
764+
-- Initialize_TIM5 --
765+
---------------------
766+
767+
procedure Initialize_TIM5 is
768+
use A0B.STM32F401.SVD.TIM;
769+
use type A0B.Types.Unsigned_16;
770+
771+
TIM : TIM5_Peripheral renames TIM5_Periph;
772+
773+
begin
774+
A0B.STM32F401.SVD.RCC.RCC_Periph.APB1ENR.TIM5EN := True;
775+
776+
-- Configure CR1
777+
778+
declare
779+
Aux : A0B.STM32F401.SVD.TIM.CR1_Register := TIM.CR1;
780+
781+
begin
782+
Aux.CEN := False; -- 0: Counter disabled
783+
Aux.UDIS := False; -- 0: UEV enabled
784+
Aux.URS := False;
785+
-- 0: Any of the following events generate an update interrupt or DMA
786+
-- request if enabled.
787+
--
788+
-- These events can be:
789+
-- – Counter overflow/underflow
790+
-- – Setting the UG bit
791+
-- – Update generation through the slave mode controller
792+
Aux.OPM := False; -- 0: Counter is not stopped at update event
793+
Aux.DIR := False; -- 0: Counter used as upcounter
794+
Aux.CMS := 2#00#;
795+
-- 00: Edge-aligned mode. The counter counts up or down depending on
796+
-- the direction bit (DIR).
797+
Aux.ARPE := True; -- 1: TIMx_ARR register is buffered
798+
Aux.CKD := Divider;
799+
800+
TIM.CR1 := Aux;
801+
end;
802+
803+
-- Configure CR2
804+
805+
declare
806+
Aux : CR2_Register_1 := TIM.CR2;
807+
808+
begin
809+
-- Aux.CCDS := <>; -- Not needed
810+
-- Aux.MMS := <>; -- Not needed
811+
Aux.TI1S := False; -- 0: The TIMx_CH1 pin is connected to TI1 input
812+
813+
TIM.CR2 := Aux;
814+
end;
815+
816+
-- Configure SMCR
817+
818+
declare
819+
Aux : SMCR_Register := TIM.SMCR;
820+
821+
begin
822+
Aux.SMS := 2#110#;
823+
-- 110: Trigger Mode - The counter starts at a rising edge of the
824+
-- trigger TRGI (but it is not reset). Only the start of the counter
825+
-- is controlled.
826+
Aux.TS := 2#001#; -- 001: Internal Trigger 1 (ITR1).
827+
Aux.MSM := True;
828+
-- 1: The effect of an event on the trigger input (TRGI) is delayed
829+
-- to allow a perfect synchronization between the current timer and
830+
-- its slaves (through TRGO). It is useful if we want to synchronize
831+
-- several timers on a single external event.
832+
-- Aux.ETF := <>; -- Not used
833+
-- Aux.ETPS := <>; -- Not used
834+
-- Aux.ECE := <>; -- Not used
835+
-- Aux.ETP := <>; -- Not used
836+
837+
TIM.SMCR := Aux;
838+
end;
839+
840+
-- Configure DIER - Not used
841+
842+
-- XXX Reset SR by write 0?
843+
844+
-- Set EGR - Not used
845+
846+
-- Configure CCMR1 only for CH1
847+
848+
declare
849+
Aux : CCMR1_Output_Register := TIM.CCMR1_Output;
850+
851+
begin
852+
Aux.CC1S := 2#00#; -- 00: CC1 channel is configured as output.
853+
Aux.OC1FE := False;
854+
-- 0: CC1 behaves normally depending on counter and CCR1 values even
855+
-- when the trigger is ON. The minimum delay to activate CC1 output
856+
-- when an edge occurs on the trigger input is 5 clock cycles.
857+
Aux.OC1PE := True;
858+
-- 1: Preload register on TIMx_CCR1 enabled. Read/Write operations
859+
-- access the preload register. TIMx_CCR1 preload value is loaded
860+
-- in the active register at each update event.
861+
Aux.OC1M := 2#110#;
862+
-- 110: PWM mode 1 - In upcounting, channel 1 is active as long as
863+
-- TIMx_CNT<TIMx_CCR1 else inactive. In downcounting, channel 1 is
864+
-- inactive (OC1REF=‘0) as long as TIMx_CNT>TIMx_CCR1 else active
865+
-- (OC1REF=1).
866+
Aux.OC1CE := False; -- 0: OC1Ref is not affected by the ETRF input
867+
868+
TIM.CCMR1_Output := Aux;
869+
end;
870+
871+
-- Configure CCMR2 - Not used
872+
873+
-- Configure CCER, only CH1
874+
875+
declare
876+
Aux : CCER_Register_1 := TIM.CCER;
877+
878+
begin
879+
Aux.CC1E := True;
880+
-- 1: On - OC1 signal is output on the corresponding output pin
881+
Aux.CC1P := False; -- 0: OC1 active high
882+
Aux.CC1NP := False;
883+
-- CC1 channel configured as output: CC1NP must be kept cleared in
884+
-- this case.
885+
886+
TIM.CCER := Aux;
887+
end;
888+
889+
-- Set CNT to zero
890+
891+
TIM.CNT := (CNT_L => 0, CNT_H => 0);
892+
893+
-- Set PSC
894+
895+
TIM.PSC.PSC := Prescale;
896+
897+
-- Set ARR
898+
899+
TIM.ARR := (ARR_L => ADC_Cycle - 1, ARR_H => 0);
900+
901+
-- Set CCR1/CCR2/CCR3/CCR4 later
902+
903+
TIM.CCR1 := (CCR1_L => ADC_Cycle / 2, CCR1_H => 0);
904+
905+
-- Configure DCR - Not used
906+
907+
-- Configure DMAR - Not used
908+
909+
-- Force update event to load configured values of ARR/PSC to shadow
910+
-- registers.
911+
912+
TIM.EGR.UG := True;
913+
end Initialize_TIM5;
914+
758915
---------------------
759916
-- Initialize_UART --
760917
---------------------

source/configuration.ads

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ is
3030
-- - ADC1
3131
-- - TIM3/TIM4 timer for PWM
3232
-- - UART1 for console
33+
--
34+
-- All timers are disabled to be able to complete setup of ADC data.
35+
36+
procedure Enable_Timers;
37+
-- Enable all timers.
3338

3439
private
3540

source/driver.adb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ procedure Driver is
1515

1616
begin
1717
Configuration.Initialize;
18+
Sensors.Initialize;
19+
Configuration.Enable_Timers;
1820

1921
Console.New_Line;
2022
Console.Put_Line ("RoboBDC Brushed DC Motors Controller");

source/sensors.adb

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

7-
with A0B.STM32F401.SVD.ADC;
7+
-- with A0B.STM32F401.SVD.ADC;
88
with A0B.Types;
99

1010
with Configuration;
@@ -31,7 +31,7 @@ package body Sensors is
3131
------------------
3232

3333
procedure Collect_Data is
34-
use A0B.STM32F401.SVD.ADC;
34+
-- use A0B.STM32F401.SVD.ADC;
3535

3636
-- function Get return A0B.Types.Unsigned_16;
3737
--
@@ -49,14 +49,10 @@ package body Sensors is
4949
-- end Get;
5050

5151
begin
52-
Configuration.ADC1_DMA_Stream.Set_Memory_Buffer
53-
(Memory => Data'Address,
54-
Count => 9_000,
55-
Increment => True);
56-
Configuration.ADC1_DMA_Stream.Enable;
52+
null;
5753

5854
-- for Item of Data loop
59-
ADC1_Periph.CR2.SWSTART := True;
55+
-- ADC1_Periph.CR2.SWSTART := True;
6056
-- Item.Vref := Get;
6157
-- Item.M1_Current := Get;
6258
-- Item.M1_Position := Get;
@@ -115,4 +111,17 @@ package body Sensors is
115111
Console.New_Line;
116112
end Dump;
117113

114+
----------------
115+
-- Initialize --
116+
----------------
117+
118+
procedure Initialize is
119+
begin
120+
Configuration.ADC1_DMA_Stream.Set_Memory_Buffer
121+
(Memory => Data'Address,
122+
Count => 9_000,
123+
Increment => True);
124+
Configuration.ADC1_DMA_Stream.Enable;
125+
end Initialize;
126+
118127
end Sensors;

source/sensors.ads

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ package Sensors is
1010

1111
procedure Dump;
1212

13+
procedure Initialize;
14+
1315
end Sensors;

0 commit comments

Comments
 (0)