]> git.sur5r.net Git - freertos/blob - Demo/_Cortex_STM32L152_IAR/system_and_ST_code/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_gpio.c
First phase of changing the directory that starts Cortex, with one that starts CORTEX.
[freertos] / Demo / _Cortex_STM32L152_IAR / system_and_ST_code / STM32L1xx_StdPeriph_Driver / src / stm32l1xx_gpio.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_gpio.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    07/02/2010\r
7   * @brief   This file provides all the GPIO firmware functions.\r
8   ******************************************************************************\r
9   * @copy\r
10   *\r
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
17   *\r
18   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>\r
19   */ \r
20 \r
21 /* Includes ------------------------------------------------------------------*/\r
22 #include "stm32l1xx_gpio.h"\r
23 #include "stm32l1xx_rcc.h"\r
24 \r
25 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
26   * @{\r
27   */\r
28 \r
29 /** @defgroup GPIO \r
30   * @brief GPIO driver modules\r
31   * @{\r
32   */\r
33 \r
34 /** @defgroup GPIO_Private_TypesDefinitions\r
35   * @{\r
36   */\r
37 \r
38 /**\r
39   * @}\r
40   */\r
41 \r
42 /** @defgroup GPIO_Private_Defines\r
43   * @{\r
44   */ \r
45 \r
46 /**\r
47   * @}\r
48   */\r
49 \r
50 /** @defgroup GPIO_Private_Macros\r
51   * @{\r
52   */\r
53 /**\r
54   * @}\r
55   */\r
56 \r
57 /** @defgroup GPIO_Private_Variables\r
58   * @{\r
59   */\r
60 /**\r
61   * @}\r
62   */\r
63 \r
64 /** @defgroup GPIO_Private_FunctionPrototypes\r
65   * @{\r
66   */\r
67 /**\r
68   * @}\r
69   */\r
70 \r
71 /** @defgroup GPIO_Private_Functions\r
72   * @{\r
73   */\r
74 \r
75 /**\r
76   * @brief  Deinitializes the GPIOx peripheral registers to their default reset \r
77   *         values.\r
78   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
79   * @retval None\r
80   */\r
81 void GPIO_DeInit(GPIO_TypeDef* GPIOx)\r
82 {\r
83   /* Check the parameters */\r
84   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
85 \r
86   if(GPIOx == GPIOA)\r
87   {\r
88     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, ENABLE);\r
89     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOA, DISABLE);  \r
90   }\r
91   else if(GPIOx == GPIOB)\r
92   {\r
93     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);\r
94     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, DISABLE);\r
95   }\r
96   else if(GPIOx == GPIOC)\r
97   {\r
98     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, ENABLE);\r
99     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOC, DISABLE);\r
100   }\r
101   else if(GPIOx == GPIOD)\r
102   {\r
103     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, ENABLE);\r
104     RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOD, DISABLE);\r
105   }\r
106   else if(GPIOx == GPIOE)\r
107   {\r
108       RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, ENABLE);\r
109       RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOE, DISABLE);\r
110   }\r
111   else\r
112   {\r
113     if(GPIOx == GPIOH)\r
114     {\r
115       RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, ENABLE);\r
116       RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOH, DISABLE);\r
117     }\r
118   }\r
119 }\r
120 \r
121 /**\r
122   * @brief  Initializes the GPIOx peripheral according to the specified \r
123   *         parameters in the GPIO_InitStruct.\r
124   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
125   * @param  GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that \r
126   *         contains the configuration information for the specified GPIO\r
127   *         peripheral.\r
128   * @retval None\r
129   */\r
130 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)\r
131 {\r
132   uint32_t pinpos = 0x00, pos = 0x00 , currentpin = 0x00;\r
133   \r
134   /* Check the parameters */\r
135   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
136   assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));\r
137   assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));\r
138   assert_param(IS_GPIO_PUPD(GPIO_InitStruct->GPIO_PuPd));\r
139 \r
140   /* -------------------------Configure the port pins---------------- */\r
141   /*-- GPIO Mode Configuration --*/\r
142   for (pinpos = 0x00; pinpos < 0x10; pinpos++)\r
143   {\r
144     pos = ((uint32_t)0x01) << pinpos;\r
145 \r
146     /* Get the port pins position */\r
147     currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;\r
148 \r
149     if (currentpin == pos)\r
150     {\r
151       GPIOx->MODER  &= ~(GPIO_MODER_MODER0 << (pinpos * 2));\r
152 \r
153       GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));\r
154 \r
155       if ((GPIO_InitStruct->GPIO_Mode == GPIO_Mode_OUT) || (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_AF))\r
156       {\r
157         /*Check Speed mode parameters */\r
158         assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));\r
159 \r
160         /*Speed mode configuration */\r
161         GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pinpos * 2));\r
162         GPIOx->OSPEEDR |= ((uint32_t)(GPIO_InitStruct->GPIO_Speed) << (pinpos * 2));\r
163 \r
164         /*Check Output mode parameters */\r
165         assert_param(IS_GPIO_OTYPE(GPIO_InitStruct->GPIO_OType));\r
166 \r
167         /* Output mode configuartion*/\r
168         GPIOx->OTYPER  &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pinpos)) ;\r
169         GPIOx->OTYPER |= (uint16_t)(((uint16_t)GPIO_InitStruct->GPIO_OType) << ((uint16_t)pinpos));\r
170       }\r
171 \r
172       /*Pull-up Pull down resistor configuration*/\r
173       GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pinpos * 2));\r
174       GPIOx->PUPDR |= (((uint32_t)GPIO_InitStruct->GPIO_PuPd) << (pinpos * 2));\r
175     }\r
176   }\r
177 }\r
178 \r
179 /**\r
180   * @brief  Fills each GPIO_InitStruct member with its default value.\r
181   * @param  GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will \r
182   *         be initialized.\r
183   * @retval None\r
184   */\r
185 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)\r
186 {\r
187   /* Reset GPIO init structure parameters values */\r
188   GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;\r
189   GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN;\r
190   GPIO_InitStruct->GPIO_Speed = GPIO_Speed_400KHz;\r
191   GPIO_InitStruct->GPIO_OType = GPIO_OType_PP;\r
192   GPIO_InitStruct->GPIO_PuPd = GPIO_PuPd_NOPULL;\r
193 }\r
194 \r
195 /**\r
196   * @brief  Reads the specified input port pin.\r
197   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
198   * @param  GPIO_Pin: specifies the port bit to read.\r
199   *   This parameter can be GPIO_Pin_x where x can be (0..15).\r
200   * @retval The input port pin value.\r
201   */\r
202 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
203 {\r
204   uint8_t bitstatus = 0x00;\r
205   \r
206   /* Check the parameters */\r
207   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
208   assert_param(IS_GET_GPIO_PIN(GPIO_Pin));\r
209 \r
210   if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET)\r
211   {\r
212     bitstatus = (uint8_t)Bit_SET;\r
213   }\r
214   else\r
215   {\r
216     bitstatus = (uint8_t)Bit_RESET;\r
217   }\r
218   return bitstatus;\r
219 }\r
220 \r
221 /**\r
222   * @brief  Reads the specified GPIO input data port.\r
223   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
224   * @retval GPIO input data port value.\r
225   */\r
226 uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx)\r
227 {\r
228   /* Check the parameters */\r
229   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
230   \r
231   return ((uint16_t)GPIOx->IDR);\r
232 }\r
233 \r
234 /**\r
235   * @brief  Reads the specified output data port bit.\r
236   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
237   * @param  GPIO_Pin: Specifies the port bit to read.\r
238   *   This parameter can be GPIO_Pin_x where x can be (0..15).\r
239   * @retval The output port pin value.\r
240   */\r
241 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
242 {\r
243   uint8_t bitstatus = 0x00;\r
244 \r
245   /* Check the parameters */\r
246   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
247   assert_param(IS_GET_GPIO_PIN(GPIO_Pin));\r
248   \r
249   if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)\r
250   {\r
251     bitstatus = (uint8_t)Bit_SET;\r
252   }\r
253   else\r
254   {\r
255     bitstatus = (uint8_t)Bit_RESET;\r
256   }\r
257   return bitstatus;\r
258 }\r
259 \r
260 /**\r
261   * @brief  Reads the specified GPIO output data port.\r
262   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
263   * @retval GPIO output data port value.\r
264   */\r
265 uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx)\r
266 {\r
267   /* Check the parameters */\r
268   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
269   \r
270   return ((uint16_t)GPIOx->ODR);\r
271 }\r
272 \r
273 /**\r
274   * @brief  Sets the selected data port bits.\r
275   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
276   * @param  GPIO_Pin: specifies the port bits to be written.\r
277   *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).\r
278   * @retval None\r
279   */\r
280 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
281 {\r
282   /* Check the parameters */\r
283   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
284   assert_param(IS_GPIO_PIN(GPIO_Pin));\r
285   \r
286   GPIOx->BSRRL = GPIO_Pin;\r
287 }\r
288 \r
289 /**\r
290   * @brief  Clears the selected data port bits.\r
291   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
292   * @param  GPIO_Pin: specifies the port bits to be written.\r
293   *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).\r
294   * @retval None\r
295   */\r
296 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
297 {\r
298   /* Check the parameters */\r
299   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
300   assert_param(IS_GPIO_PIN(GPIO_Pin));\r
301   \r
302   GPIOx->BSRRH = GPIO_Pin;\r
303 }\r
304 \r
305 /**\r
306   * @brief  Sets or clears the selected data port bit.\r
307   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
308   * @param  GPIO_Pin: specifies the port bit to be written.\r
309   *   This parameter can be one of GPIO_Pin_x where x can be (0..15).\r
310   * @param  BitVal: specifies the value to be written to the selected bit.\r
311   *   This parameter can be one of the BitAction enum values:\r
312   *     @arg Bit_RESET: to clear the port pin\r
313   *     @arg Bit_SET: to set the port pin\r
314   * @retval None\r
315   */\r
316 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)\r
317 {\r
318   /* Check the parameters */\r
319   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
320   assert_param(IS_GET_GPIO_PIN(GPIO_Pin));\r
321   assert_param(IS_GPIO_BIT_ACTION(BitVal));\r
322   \r
323   if (BitVal != Bit_RESET)\r
324   {\r
325     GPIOx->BSRRL = GPIO_Pin;\r
326   }\r
327   else\r
328   {\r
329     GPIOx->BSRRH = GPIO_Pin ;\r
330   }\r
331 }\r
332 \r
333 /**\r
334   * @brief  Writes data to the specified GPIO data port.\r
335   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
336   * @param  PortVal: specifies the value to be written to the port output data \r
337   *                  register.\r
338   * @retval None\r
339   */\r
340 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)\r
341 {\r
342   /* Check the parameters */\r
343   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
344   \r
345   GPIOx->ODR = PortVal;\r
346 }\r
347 \r
348 /**\r
349   * @brief  Locks GPIO Pins configuration registers.\r
350   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
351   * @param  GPIO_Pin: specifies the port bit to be written.\r
352   *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).\r
353   * @retval None\r
354   */\r
355 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\r
356 {\r
357   uint32_t tmp = 0x00010000;\r
358   \r
359   /* Check the parameters */\r
360   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
361   assert_param(IS_GPIO_PIN(GPIO_Pin));\r
362   \r
363   tmp |= GPIO_Pin;\r
364   /* Set LCKK bit */\r
365   GPIOx->LCKR = tmp;\r
366   /* Reset LCKK bit */\r
367   GPIOx->LCKR =  GPIO_Pin;\r
368   /* Set LCKK bit */\r
369   GPIOx->LCKR = tmp;\r
370   /* Read LCKK bit*/\r
371   tmp = GPIOx->LCKR;\r
372   /* Read LCKK bit*/\r
373   tmp = GPIOx->LCKR;\r
374 }\r
375 \r
376 /**\r
377   * @brief  Changes the mapping of the specified pin.\r
378   * @param  GPIOx: where x can be (A, B, C, D, E or H) to select the GPIO peripheral.\r
379   * @param  GPIO_PinSource: specifies the pin for the Alternate function.\r
380   *   This parameter can be GPIO_PinSourcex where x can be (0..15).\r
381   * @param  GPIO_AFSelection: selects the pin to used as Alternat function.\r
382   *   This parameter can be one of the following values:\r
383   *     @arg GPIO_AF_RTC_50Hz\r
384   *     @arg GPIO_AF_MCO\r
385   *     @arg GPIO_AF_TAMPER\r
386   *     @arg GPIO_AF_WKUP\r
387   *     @arg GPIO_AF_SWJ\r
388   *     @arg GPIO_AF_TRACE\r
389   *     @arg GPIO_AF_TIMESTAMP\r
390   *     @arg GPIO_AF_CALIB\r
391   *     @arg GPIO_AF_TIM2\r
392   *     @arg GPIO_AF_TIM3\r
393   *     @arg GPIO_AF_TIM4\r
394   *     @arg GPIO_AF_TIM9\r
395   *     @arg GPIO_AF_TIM10\r
396   *     @arg GPIO_AF_TIM11\r
397   *     @arg GPIO_AF_I2C1\r
398   *     @arg GPIO_AF_I2C2\r
399   *     @arg GPIO_AF_SPI1\r
400   *     @arg GPIO_AF_SPI2\r
401   *     @arg GPIO_AF_USART1\r
402   *     @arg GPIO_AF_USART2\r
403   *     @arg GPIO_AF_USART3\r
404   *     @arg GPIO_AF_USB\r
405   *     @arg GPIO_AF_LCD\r
406   *     @arg GPIO_AF_RI\r
407   *     @arg GPIO_AF_EVENTOUT\r
408   * @retval None\r
409   */\r
410 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)\r
411 {\r
412   uint32_t temp = 0x00;\r
413   uint32_t temp_2 = 0x00;\r
414   \r
415   /* Check the parameters */\r
416   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));\r
417   assert_param(IS_GPIO_PIN_SOURCE(GPIO_PinSource));\r
418   assert_param(IS_GPIO_AF(GPIO_AF));\r
419   \r
420   temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;\r
421   GPIOx->AFR[GPIO_PinSource >> 0x03] &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4)) ;\r
422   temp_2 = GPIOx->AFR[GPIO_PinSource >> 0x03] | temp;\r
423   GPIOx->AFR[GPIO_PinSource >> 0x03] = temp_2;\r
424 }\r
425 \r
426 /**\r
427   * @}\r
428   */\r
429 \r
430 /**\r
431   * @}\r
432   */\r
433 \r
434 /**\r
435   * @}\r
436   */\r
437 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r