text
stringlengths
0
234
Timer_AF : constant STM32.GPIO_Alternate_Function := GPIO_AF_TIM4_2;
-- Note that this value MUST match the corresponding timer selected!
Output_Channel : constant Timer_Channel := Channel_2; -- arbitrary
-- The LED driven by this example is determined by the channel selected.
-- That is so because each channel of Timer_4 is connected to a specific
-- LED in the alternate function configuration on this board. We will
-- initialize all of the LEDs to be in the AF mode. The
-- particular channel selected is completely arbitrary, as long as the
-- selected GPIO port/pin for the LED matches the selected channel.
--
-- Channel_1 is connected to the green LED.
-- Channel_2 is connected to the orange LED.
-- Channel_3 is connected to the red LED.
-- Channel_4 is connected to the blue LED.
LED_For : constant array (Timer_Channel) of User_LED :=
(Channel_1 => Green_LED,
Channel_2 => Orange_LED,
Channel_3 => Red_LED,
Channel_4 => Blue_LED);
Requested_Frequency : constant Hertz := 30_000; -- arbitrary
Power_Control : PWM_Modulator;
-- 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_PWM_Timer (Selected_Timer'Access, Requested_Frequency);
Power_Control.Attach_PWM_Channel
(Selected_Timer'Access,
Output_Channel,
LED_For (Output_Channel),
Timer_AF);
Power_Control.Enable_Output;
declare
Arg : Long_Float := 0.0;
Value : Percentage;
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 compiler optimization is observable.
begin
loop
Value := Percentage (50.0 * (1.0 + Sine (Arg)));
Power_Control.Set_Duty_Cycle (Value);
Arg := Arg + Increment;
end loop;
Function Definition: procedure Demo_DAC_Basic is
Function Body: Output_Channel : constant DAC_Channel := Channel_1; -- arbitrary
procedure Configure_DAC_GPIO (Output_Channel : DAC_Channel);
-- Once the channel is enabled, the corresponding GPIO pin is automatically
-- connected to the analog converter output. However, in order to avoid
-- parasitic consumption, the PA4 pin (Channel_1) or PA5 pin (Channel_2)
-- should first be configured to analog mode. See the note in the RM, page
-- 431.
procedure Print (Value : UInt32);
-- Prints the image of the arg at a fixed location
procedure Await_Button;
-- Wait for the user to press and then release the blue user button