text
stringlengths
0
234
--------------------
procedure Configure_LEDs;
procedure Configure_LEDs is
begin
Enable_Clock (GPIO_D);
Configure_IO
(All_LEDs,
(Mode_AF,
AF => GPIO_AF_TIM4_2,
AF_Speed => Speed_50MHz,
AF_Output_Type => Push_Pull,
Resistors => Floating));
end Configure_LEDs;
-- The SFP run-time library for these boards is intended for certified
-- environments and so does not contain the full set of facilities defined
-- by the Ada language. The elementary functions are not included, for
-- example. In contrast, the Ravenscar "full" run-times do have these
-- functions.
function Sine (Input : Long_Float) return Long_Float;
-- Therefore there are four choices: 1) use the "ravescar-full-stm32f4"
-- runtime library, 2) pull the sources for the language-defined elementary
-- function package into the board's run-time library and rebuild the
-- run-time, 3) pull the sources for those packages into the source
-- directory of your application and rebuild your application, or 4) roll
-- your own approximation to the functions required by your application.
-- In this demonstration we roll our own approximation to the sine function
-- so that it doesn't matter which runtime library is used.
function Sine (Input : Long_Float) return Long_Float is
Pi : constant Long_Float := 3.14159_26535_89793_23846;
X : constant Long_Float := Long_Float'Remainder (Input, Pi * 2.0);
B : constant Long_Float := 4.0 / Pi;
C : constant Long_Float := (-4.0) / (Pi * Pi);
Y : constant Long_Float := B * X + C * X * abs (X);
P : constant Long_Float := 0.225;
begin
return P * (Y * abs (Y) - Y) + Y;
end Sine;
-- We use the sine function to drive the power applied to the LED, thereby
-- making the LED increase and decrease in brightness. We attach the timer
-- to the LED and then control how much power is supplied by changing the
-- value of the timer's output compare register. The sine function drives
-- that value, thus the waxing/waning effect.
begin
Configure_LEDs;
Enable_Clock (Timer_4);
Reset (Timer_4);
Configure
(Timer_4,
Prescaler => 1,
Period => Period,
Clock_Divisor => Div1,
Counter_Mode => Up);
Configure_Channel_Output
(Timer_4,
Channel => Output_Channel,
Mode => PWM1,
State => Enable,
Pulse => 0,
Polarity => High);
Set_Autoreload_Preload (Timer_4, True);
Enable_Channel (Timer_4, Output_Channel);
Enable (Timer_4);
declare
Arg : Long_Float := 0.0;
Pulse : UInt16;
Increment : constant Long_Float := 0.00003;
-- The Increment value controls the rate at which the brightness
-- increases and decreases. The value is more or less arbitrary, but
-- note that the effect of optimization is observable.
begin
loop
Pulse := UInt16 (Long_Float (Period / 2) * (1.0 + Sine (Arg)));
Set_Compare_Value (Timer_4, Output_Channel, Pulse);
Arg := Arg + Increment;
end loop;
Function Definition: procedure Demo_PWM_ADT is -- demo the higher-level PWM abstract data type
Function Body: Selected_Timer : STM32.Timers.Timer renames Timer_4;
-- NOT arbitrary! We drive the on-board LEDs that are tied to the channels
-- of Timer_4 on some boards. Not all boards have this association. If you
-- use a different board, select a GPIO point connected to your selected
-- timer and drive that instead.