@@ -15,10 +15,12 @@ package body Configuration
1515  with  Preelaborate
1616is 
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
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 --
6165      Initialize_ADC1;
6266      Initialize_TIM3;
6367      Initialize_TIM4;
64- 
65-       Enable_Timers;
68+       Initialize_TIM5;
6669   end  Initialize ;
6770
6871   -- -------------------
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
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
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
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   -- -------------------
0 commit comments