Skip to content

Commit 532c5e6

Browse files
committed
Configure TIM4 to generate PWM.
1 parent 5534c3e commit 532c5e6

File tree

1 file changed

+260
-3
lines changed

1 file changed

+260
-3
lines changed

source/configuration.adb

Lines changed: 260 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,28 @@ is
2020
-- 1/1/3_360: PWM 25kHz CPU @84MHz (nominal for L298)
2121

2222
procedure Initialize_TIM3;
23+
-- Configure TIM3. Timer is disabled. It generates TRGO on CEN set.
24+
25+
procedure Initialize_TIM4;
26+
-- Configure TIM4. Timer is disabled. It is configured to be triggered by
27+
-- set of CEN of TIM3.
2328

2429
procedure Initialize_UART;
2530

31+
procedure Enable_Timers;
32+
-- Enable all timers.
33+
34+
-------------------
35+
-- Enable_Timers --
36+
-------------------
37+
38+
procedure Enable_Timers is
39+
begin
40+
A0B.STM32F401.SVD.TIM.TIM3_Periph.CR1.CEN := True;
41+
-- Timers are configured in master-slave mode, so enable of TIM3 will
42+
-- enable other timers too.
43+
end Enable_Timers;
44+
2645
----------------
2746
-- Initialize --
2847
----------------
@@ -31,6 +50,9 @@ is
3150
begin
3251
Initialize_UART;
3352
Initialize_TIM3;
53+
Initialize_TIM4;
54+
55+
Enable_Timers;
3456
end Initialize;
3557

3658
---------------------
@@ -279,11 +301,246 @@ is
279301
Mode => A0B.STM32F401.GPIO.Push_Pull,
280302
Speed => A0B.STM32F401.GPIO.Very_High,
281303
Pull => A0B.STM32F401.GPIO.Pull_Up);
304+
end Initialize_TIM3;
282305

283-
-- Enable timer
306+
---------------------
307+
-- Initialize_TIM4 --
308+
---------------------
284309

285-
TIM.CR1.CEN := True;
286-
end Initialize_TIM3;
310+
procedure Initialize_TIM4 is
311+
use A0B.STM32F401.SVD.TIM;
312+
use type A0B.Types.Unsigned_16;
313+
314+
TIM : TIM3_Peripheral renames TIM4_Periph;
315+
316+
begin
317+
A0B.STM32F401.SVD.RCC.RCC_Periph.APB1ENR.TIM4EN := True;
318+
319+
-- Configure CR1
320+
321+
declare
322+
Aux : A0B.STM32F401.SVD.TIM.CR1_Register := TIM.CR1;
323+
324+
begin
325+
Aux.CEN := False; -- 0: Counter disabled
326+
Aux.UDIS := False; -- 0: UEV enabled
327+
Aux.URS := False;
328+
-- 0: Any of the following events generate an update interrupt or DMA
329+
-- request if enabled.
330+
--
331+
-- These events can be:
332+
-- – Counter overflow/underflow
333+
-- – Setting the UG bit
334+
-- – Update generation through the slave mode controller
335+
Aux.OPM := False; -- 0: Counter is not stopped at update event
336+
Aux.DIR := False; -- 0: Counter used as upcounter
337+
Aux.CMS := 2#00#;
338+
-- 00: Edge-aligned mode. The counter counts up or down depending on
339+
-- the direction bit (DIR).
340+
Aux.ARPE := True; -- 1: TIMx_ARR register is buffered
341+
Aux.CKD := Divider;
342+
343+
TIM.CR1 := Aux;
344+
end;
345+
346+
-- Configure CR2
347+
348+
declare
349+
Aux : CR2_Register_1 := TIM.CR2;
350+
351+
begin
352+
-- Aux.CCDS := <>; -- Not needed
353+
-- Aux.MMS := <>; -- Not needed
354+
Aux.TI1S := False; -- 0: The TIMx_CH1 pin is connected to TI1 input
355+
356+
TIM.CR2 := Aux;
357+
end;
358+
359+
-- Configure SMCR
360+
361+
declare
362+
Aux : SMCR_Register := TIM.SMCR;
363+
364+
begin
365+
Aux.SMS := 2#110#;
366+
-- 110: Trigger Mode - The counter starts at a rising edge of the
367+
-- trigger TRGI (but it is not reset). Only the start of the counter
368+
-- is controlled.
369+
Aux.TS := 2#010#; -- 010: Internal Trigger 2 (ITR2).
370+
Aux.MSM := True;
371+
-- 1: The effect of an event on the trigger input (TRGI) is delayed
372+
-- to allow a perfect synchronization between the current timer and
373+
-- its slaves (through TRGO). It is useful if we want to synchronize
374+
-- several timers on a single external event.
375+
-- Aux.ETF := <>; -- Not used
376+
-- Aux.ETPS := <>; -- Not used
377+
-- Aux.ECE := <>; -- Not used
378+
-- Aux.ETP := <>; -- Not used
379+
380+
TIM.SMCR := Aux;
381+
end;
382+
383+
-- Configure DIER - Not used
384+
385+
-- XXX Reset SR by write 0?
386+
387+
-- Set EGR - Not used
388+
389+
-- Configure CCMR1
390+
391+
declare
392+
Aux : CCMR1_Output_Register := TIM.CCMR1_Output;
393+
394+
begin
395+
Aux.CC1S := 2#00#; -- 00: CC1 channel is configured as output.
396+
Aux.OC1FE := False;
397+
-- 0: CC1 behaves normally depending on counter and CCR1 values even
398+
-- when the trigger is ON. The minimum delay to activate CC1 output
399+
-- when an edge occurs on the trigger input is 5 clock cycles.
400+
Aux.OC1PE := True;
401+
-- 1: Preload register on TIMx_CCR1 enabled. Read/Write operations
402+
-- access the preload register. TIMx_CCR1 preload value is loaded
403+
-- in the active register at each update event.
404+
Aux.OC1M := 2#110#;
405+
-- 110: PWM mode 1 - In upcounting, channel 1 is active as long as
406+
-- TIMx_CNT<TIMx_CCR1 else inactive. In downcounting, channel 1 is
407+
-- inactive (OC1REF=‘0) as long as TIMx_CNT>TIMx_CCR1 else active
408+
-- (OC1REF=1).
409+
Aux.OC1CE := False; -- 0: OC1Ref is not affected by the ETRF input
410+
Aux.CC2S := 2#00#; -- 00: CC2 channel is configured as output
411+
Aux.OC2FE := False;
412+
-- 0: CC2 behaves normally depending on counter and CCR2 values even
413+
-- when the trigger is ON. The minimum delay to activate CC2 output
414+
-- when an edge occurs on the trigger input is 5 clock cycles.
415+
Aux.OC2PE := True;
416+
-- 1: Preload register on TIMx_CCR2 enabled. Read/Write operations
417+
-- access the preload register. TIMx_CCR2 preload value is loaded
418+
-- in the active register at each update event.
419+
Aux.OC2M := 2#110#;
420+
-- 110: PWM mode 1 - In upcounting, channel 2 is active as long as
421+
-- TIMx_CNT<TIMx_CCR2 else inactive. In downcounting, channel 2 is
422+
-- inactive (OC2REF=‘0) as long as TIMx_CNT>TIMx_CCR2 else active
423+
-- (OC2REF=1).
424+
Aux.OC2CE := False; -- 0: OC2Ref is not affected by the ETRF input
425+
426+
TIM.CCMR1_Output := Aux;
427+
end;
428+
429+
-- Configure CCMR2
430+
431+
declare
432+
Aux : CCMR2_Output_Register := TIM.CCMR2_Output;
433+
434+
begin
435+
Aux.CC3S := 2#00#; -- 00: CC3 channel is configured as output.
436+
Aux.OC3FE := False;
437+
-- 0: CC3 behaves normally depending on counter and CCR2 values even
438+
-- when the trigger is ON. The minimum delay to activate CC3 output
439+
-- when an edge occurs on the trigger input is 5 clock cycles.
440+
Aux.OC3PE := True;
441+
-- 1: Preload register on TIMx_CCR3 enabled. Read/Write operations
442+
-- access the preload register. TIMx_CCR3 preload value is loaded
443+
-- in the active register at each update event.
444+
Aux.OC3M := 2#110#;
445+
-- 110: PWM mode 1 - In upcounting, channel 3 is active as long as
446+
-- TIMx_CNT<TIMx_CCR3 else inactive. In downcounting, channel 1 is
447+
-- inactive (OC3REF=‘0) as long as TIMx_CNT>TIMx_CCR3 else active
448+
-- (OC3REF=1).
449+
Aux.OC3CE := False; -- 0: OC3Ref is not affected by the ETRF input
450+
Aux.CC4S := 2#00#; -- 00: CC4 channel is configured as output
451+
Aux.OC4FE := False;
452+
-- 0: CC4 behaves normally depending on counter and CCR4 values even
453+
-- when the trigger is ON. The minimum delay to activate CC4 output
454+
-- when an edge occurs on the trigger input is 5 clock cycles.
455+
Aux.OC4PE := True;
456+
-- 1: Preload register on TIMx_CCR4 enabled. Read/Write operations
457+
-- access the preload register. TIMx_CCR4 preload value is loaded
458+
-- in the active register at each update event.
459+
Aux.OC4M := 2#110#;
460+
-- 110: PWM mode 1 - In upcounting, channel 4 is active as long as
461+
-- TIMx_CNT<TIMx_CCR4 else inactive. In downcounting, channel 4 is
462+
-- inactive (OC4REF=‘0) as long as TIMx_CNT>TIMx_CCR4 else active
463+
-- (OC4REF=1).
464+
Aux.OC4CE := False; -- 0: OC4Ref is not affected by the ETRF input
465+
466+
TIM.CCMR2_Output := Aux;
467+
end;
468+
469+
-- Configure CCER
470+
471+
declare
472+
Aux : CCER_Register_1 := TIM.CCER;
473+
474+
begin
475+
Aux.CC1E := True;
476+
-- 1: On - OC1 signal is output on the corresponding output pin
477+
Aux.CC1P := False; -- 0: OC1 active high
478+
Aux.CC1NP := False;
479+
-- CC1 channel configured as output: CC1NP must be kept cleared in
480+
-- this case.
481+
Aux.CC2E := True;
482+
-- 1: On - OC2 signal is output on the corresponding output pin
483+
Aux.CC2P := False; -- 0: OC2 active high
484+
Aux.CC2NP := False;
485+
-- CC2 channel configured as output: CC2NP must be kept cleared in
486+
-- this case.
487+
Aux.CC3E := True;
488+
-- 1: On - OC3 signal is output on the corresponding output pin
489+
Aux.CC3P := False; -- 0: OC3 active high
490+
Aux.CC3NP := False;
491+
-- CC3 channel configured as output: CC3NP must be kept cleared in
492+
-- this case.
493+
Aux.CC4E := True;
494+
-- 1: On - OC4 signal is output on the corresponding output pin
495+
Aux.CC4P := False; -- 0: OC4 active high
496+
Aux.CC4NP := False;
497+
-- CC4 channel configured as output: CC4NP must be kept cleared in
498+
-- this case.
499+
500+
TIM.CCER := Aux;
501+
end;
502+
503+
-- Set CNT to zero (TIM3/TIM4 support low part only)
504+
505+
TIM.CNT.CNT_L := 0;
506+
507+
-- Set PSC
508+
509+
TIM.PSC.PSC := Prescale;
510+
511+
-- Set ARR (TIM3/TIM4 support low part only)
512+
513+
TIM.ARR.ARR_L := Cycle - 1;
514+
515+
-- Set CCR1/CCR2/CCR3/CCR4 later
516+
517+
-- Configure DCR - Not used
518+
519+
-- Configure DMAR - Not used
520+
521+
-- Configure GPIO
522+
523+
M3_IN1_Pin.Configure_Alternative_Function
524+
(Line => A0B.STM32F401.TIM_Lines.TIM4_CH1,
525+
Mode => A0B.STM32F401.GPIO.Push_Pull,
526+
Speed => A0B.STM32F401.GPIO.Very_High,
527+
Pull => A0B.STM32F401.GPIO.Pull_Up);
528+
M3_IN2_Pin.Configure_Alternative_Function
529+
(Line => A0B.STM32F401.TIM_Lines.TIM4_CH2,
530+
Mode => A0B.STM32F401.GPIO.Push_Pull,
531+
Speed => A0B.STM32F401.GPIO.Very_High,
532+
Pull => A0B.STM32F401.GPIO.Pull_Up);
533+
M4_IN1_Pin.Configure_Alternative_Function
534+
(Line => A0B.STM32F401.TIM_Lines.TIM4_CH3,
535+
Mode => A0B.STM32F401.GPIO.Push_Pull,
536+
Speed => A0B.STM32F401.GPIO.Very_High,
537+
Pull => A0B.STM32F401.GPIO.Pull_Up);
538+
M4_IN2_Pin.Configure_Alternative_Function
539+
(Line => A0B.STM32F401.TIM_Lines.TIM4_CH4,
540+
Mode => A0B.STM32F401.GPIO.Push_Pull,
541+
Speed => A0B.STM32F401.GPIO.Very_High,
542+
Pull => A0B.STM32F401.GPIO.Pull_Up);
543+
end Initialize_TIM4;
287544

288545
---------------------
289546
-- Initialize_UART --

0 commit comments

Comments
 (0)