]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_STM32F103_Primer_GCC/ST_Code/led.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / CORTEX_STM32F103_Primer_GCC / ST_Code / led.c
1 /********************* (C) COPYRIGHT 2007 RAISONANCE S.A.S. *******************/
2 /**
3 *
4 * @file     led.c
5 * @brief    LED management.
6 * @author   IB
7 * @date     07/2007
8 *
9 **/
10 /******************************************************************************/
11
12 /* Includes ------------------------------------------------------------------*/
13 #include "circle.h"
14
15 /// @cond Internal
16
17 /* Private variables ---------------------------------------------------------*/
18
19 int            GreenLED_Counter  = 0;
20 int            RedLED_Counter    = 0;
21 enum LED_mode  GreenLED_mode     = LED_UNDEF;
22 enum LED_mode  RedLED_mode       = LED_UNDEF;
23 enum LED_mode  GreenLED_newmode  = LED_OFF;
24 enum LED_mode  RedLED_newmode    = LED_OFF;
25 const int      HalfPeriod_LF     = 200;
26 const int      HalfPeriod_HF     = 50;
27 const int      Period_LF         = 200 * 2;
28 const int      Period_HF         = 50 * 2;
29
30 /* Public functions for CircleOS ---------------------------------------------*/
31
32 /*******************************************************************************
33 *
34 *                                LED_Init
35 *
36 *******************************************************************************/
37 /**
38 *
39 *  Initialization of the GPIOs for the LEDs
40 *
41 *  @note    Is called by CircleOS startup.
42 *
43 **/
44 /******************************************************************************/
45 void LED_Init( void )
46    {
47    GPIO_InitTypeDef GPIO_InitStructure;
48
49    /* Enable LED GPIO clock */
50    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
51
52    /* Configure LED pins as output push-pull */
53    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9 ;
54    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
55    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
56
57    GPIO_Init( GPIOB, &GPIO_InitStructure );
58    }
59
60 /*******************************************************************************
61 *
62 *                                LED_Handler
63 *
64 *******************************************************************************/
65 /**
66 *
67 *  Called by the CircleOS scheduler to manage the states of the LEDs.
68 *  LEDs may be on, off or blinking according to their state.
69 *
70 **/
71 /******************************************************************************/
72 void LED_Handler( void )
73    {
74    LED_Handler_hw(LED_GREEN);
75    LED_Handler_hw(LED_RED);
76    }
77
78 /*******************************************************************************
79 *
80 *                                LED_Handler
81 *
82 *******************************************************************************/
83 /**
84 *
85 *  Called by the CircleOS scheduler to manage the states of the LEDs.
86 *  LEDs may be on, off or blinking according to their state.
87 *
88 *  @param[in]  id       A LED_id indicating the LED to take care of.
89 *
90 **/
91 /******************************************************************************/
92 void LED_Handler_hw( enum LED_id id )
93    {
94    int            counter;
95    enum LED_mode  mode;
96    
97    // Choose the right LED parameters.
98    if( id == LED_GREEN )
99       {
100       counter = GreenLED_Counter;
101       mode    = GreenLED_newmode;
102       }
103    else
104       {
105       counter = RedLED_Counter;
106       mode    = RedLED_newmode;
107       }
108
109    switch( mode )
110       {
111       case LED_OFF         :
112       case LED_ON          :
113          if( ( ( id == LED_GREEN ) && ( GreenLED_mode == mode ) ) ||
114              ( ( id == LED_RED   ) && ( RedLED_mode   == mode ) ) )
115             {
116             return;
117             }
118
119          if( id == LED_GREEN )
120             {
121             GPIO_WriteBit( GPIOB, GPIO_Pin_8, ( mode == LED_OFF ) ? Bit_RESET : Bit_SET );
122
123             GreenLED_mode = mode;
124             }
125          else if( id == LED_RED )
126             {
127             GPIO_WriteBit( GPIOB, GPIO_Pin_9, ( mode == LED_OFF ) ? Bit_RESET : Bit_SET );
128
129             RedLED_mode = mode;
130             }
131
132          counter = -1;
133          break;
134
135       case LED_BLINKING_HF :
136          counter++;
137
138          if( counter == HalfPeriod_HF )
139             {
140             GPIO_WriteBit( GPIOB, ( id == LED_RED ) ? GPIO_Pin_9 : GPIO_Pin_8, Bit_SET );
141             }
142          else if( ( counter < 0 ) || ( counter >= Period_HF ) )
143             {
144             GPIO_WriteBit( GPIOB, ( id == LED_RED ) ? GPIO_Pin_9 : GPIO_Pin_8, Bit_RESET );
145
146             counter = 0;
147             }
148          break;
149
150       case LED_BLINKING_LF :
151          counter++;
152
153          if( counter == HalfPeriod_LF )
154             {
155             GPIO_WriteBit( GPIOB, ( id == LED_RED ) ? GPIO_Pin_9 : GPIO_Pin_8, Bit_SET );
156             }
157
158          else if( ( counter < 0 ) || ( counter >= Period_LF ) )
159             {
160             GPIO_WriteBit( GPIOB, ( id == LED_RED ) ? GPIO_Pin_9 : GPIO_Pin_8, Bit_RESET );
161
162             counter = 0;
163             }
164          break;
165
166       default              :
167          break;
168       }
169
170    if( id == LED_GREEN )
171       {
172       GreenLED_Counter = counter;
173       GreenLED_mode    = mode;
174       }
175    else
176       {
177       RedLED_Counter = counter;
178       RedLED_mode    = mode;
179       }
180    }
181
182 /// @endcond
183
184 /* Public functions ----------------------------------------------------------*/
185
186 /*******************************************************************************
187 *
188 *                                LED_Set
189 *
190 *******************************************************************************/
191 /**
192 *
193 *  Set a specified LED in a specified mode.
194 *
195 *  @param[in]  id    A LED_id specifying the LED to change the mode.
196 *  @param[in]  mode  A LED_mode describing the new LED mode.
197 *
198 **/
199 /******************************************************************************/
200 void LED_Set( enum LED_id id, enum LED_mode mode )
201    {
202    if( id == LED_GREEN )
203       {
204       GreenLED_newmode = mode;
205       }
206    else if( id == LED_RED )
207       {
208       RedLED_newmode = mode;
209       }
210    }