STM8L15x Standard Peripherals Drivers
|
stm8l15x_gpio.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8l15x_gpio.c 00004 * @author MCD Application Team 00005 * @version V1.5.0 00006 * @date 13-May-2011 00007 * @brief This file provides firmware functions to manage the following 00008 * functionalities of the GPIO peripheral: 00009 * - Initialization and Configuration 00010 * - GPIO Read and Write 00011 * 00012 * =================================================================== 00013 * How to use this driver 00014 * =================================================================== 00015 * 1. Configure the GPIO pin(s) using GPIO_Init() 00016 * Two main configuration are available for each pin: 00017 * - Input: Floating 00018 * Pull-up. 00019 * In Input mode, external interrupt can be enabled or disabled 00020 * - Output: Push-Pull 00021 * Open Drain. 00022 * In output mode, the GPIO pin speed is configurable: 00023 * Slow (2 MHz) or Fast (10MHz). 00024 * 00025 * 2. To get the level of a pin configured in input mode use GPIO_ReadInputDataBit() 00026 * 00027 * 3. To set/reset GPIO pins sharing the same GPIO port use 00028 * GPIO_SetBits() / GPIO_ResetBits() 00029 * 00030 * 4. To enable external interrupt, the GPIO pin must be configured 00031 * in input mode with interrupt enabled. Interrupt sensitivity 00032 * (rising, falling...) is configurable using 00033 * EXTI_SetPinSensitivity() in the EXTI peripheral driver "stm8l15x_exti.c" 00034 * 00035 ****************************************************************************** 00036 * @attention 00037 * 00038 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 00039 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 00040 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 00041 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 00042 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 00043 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 00044 * 00045 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> 00046 ****************************************************************************** 00047 */ 00048 00049 /* Includes ------------------------------------------------------------------*/ 00050 #include "stm8l15x_gpio.h" 00051 00052 /** @addtogroup STM8L15x_StdPeriph_Driver 00053 * @{ 00054 */ 00055 00056 /** @defgroup CLK 00057 * @brief CLK driver modules 00058 * @{ 00059 */ 00060 00061 /* Private typedef -----------------------------------------------------------*/ 00062 /* Private define ------------------------------------------------------------*/ 00063 /* Private macro -------------------------------------------------------------*/ 00064 /* Private variables ---------------------------------------------------------*/ 00065 /* Private function prototypes -----------------------------------------------*/ 00066 /* Private functions ---------------------------------------------------------*/ 00067 00068 /** @defgroup GPIO_Private_Functions 00069 * @{ 00070 */ 00071 00072 00073 /** @defgroup GPIO_Group1 Initialization and Configuration 00074 * @brief Initialization and Configuration 00075 * 00076 @verbatim 00077 =============================================================================== 00078 Initialization and Configuration 00079 =============================================================================== 00080 00081 @endverbatim 00082 * @{ 00083 */ 00084 00085 /** 00086 * @brief Deinitializes the GPIOx peripheral registers to their default reset values. 00087 * @param GPIOx: Select the GPIO peripheral number (x = A to I). 00088 * @retval None 00089 */ 00090 void GPIO_DeInit(GPIO_TypeDef* GPIOx) 00091 { 00092 GPIOx->CR2 = GPIO_CR2_RESET_VALUE; /* Reset Control Register 2 */ 00093 GPIOx->ODR = GPIO_ODR_RESET_VALUE; /* Reset Output Data Register */ 00094 GPIOx->DDR = GPIO_DDR_RESET_VALUE; /* Reset Data Direction Register */ 00095 GPIOx->CR1 = GPIO_CR1_RESET_VALUE; /* Reset Control Register 1 */ 00096 } 00097 00098 /** 00099 * @brief Initializes the GPIOx according to the specified parameters. 00100 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00101 * @param GPIO_Pin : This parameter contains the pin number 00102 * This parameter can be one of the following values: 00103 * @arg GPIO_Pin_0: Pin 0 00104 * @arg GPIO_Pin_1: Pin 1 00105 * @arg GPIO_Pin_2: Pin 2 00106 * @arg GPIO_Pin_3: Pin 3 00107 * @arg GPIO_Pin_4: Pin 4 00108 * @arg GPIO_Pin_5: Pin 5 00109 * @arg GPIO_Pin_6: Pin 6 00110 * @arg GPIO_Pin_7: Pin 7 00111 * @param GPIO_Mode : This parameter can be a value of the 00112 * This parameter can be one of the following values: 00113 * @arg GPIO_Mode_In_FL_No_IT: Input floating, no external interrupt 00114 * @arg GPIO_Mode_In_PU_No_IT: Input pull-up, no external interrupt 00115 * @arg GPIO_Mode_In_FL_IT: Input pull-up, external interrupt 00116 * @arg GPIO_Mode_Out_OD_Low_Fast: Output open-drain, low level, 10MHz 00117 * @arg GPIO_Mode_Out_PP_Low_Fast: Output push-pull, low level, 10MHz 00118 * @arg GPIO_Mode_Out_OD_Low_Slow: Output open-drain, low level, 2MHz 00119 * @arg GPIO_Mode_Out_PP_Low_Slow: Output push-pull, low level, 2MHz 00120 * @arg GPIO_Mode_Out_OD_HiZ_Fast: Output open-drain, high-impedance level, 10MHz 00121 * @arg GPIO_Mode_Out_PP_High_Fast: Output push-pull, high level, 10MHz 00122 * @arg GPIO_Mode_Out_OD_HiZ_Slow: Output open-drain, high-impedance level, 2MHz 00123 * @arg GPIO_Mode_Out_PP_High_Slow: Output push-pull, high level, 2MHz 00124 * @retval None 00125 */ 00126 00127 void GPIO_Init(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode) 00128 { 00129 /*----------------------*/ 00130 /* Check the parameters */ 00131 /*----------------------*/ 00132 00133 assert_param(IS_GPIO_MODE(GPIO_Mode)); 00134 assert_param(IS_GPIO_PIN(GPIO_Pin)); 00135 00136 /* Reset corresponding bit to GPIO_Pin in CR2 register */ 00137 GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); 00138 00139 /*-----------------------------*/ 00140 /* Input/Output mode selection */ 00141 /*-----------------------------*/ 00142 00143 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x80) != (uint8_t)0x00) /* Output mode */ 00144 { 00145 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x10) != (uint8_t)0x00) /* High level */ 00146 { 00147 GPIOx->ODR |= GPIO_Pin; 00148 } else /* Low level */ 00149 { 00150 GPIOx->ODR &= (uint8_t)(~(GPIO_Pin)); 00151 } 00152 /* Set Output mode */ 00153 GPIOx->DDR |= GPIO_Pin; 00154 } else /* Input mode */ 00155 { 00156 /* Set Input mode */ 00157 GPIOx->DDR &= (uint8_t)(~(GPIO_Pin)); 00158 } 00159 00160 /*------------------------------------------------------------------------*/ 00161 /* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */ 00162 /*------------------------------------------------------------------------*/ 00163 00164 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x40) != (uint8_t)0x00) /* Pull-Up or Push-Pull */ 00165 { 00166 GPIOx->CR1 |= GPIO_Pin; 00167 } else /* Float or Open-Drain */ 00168 { 00169 GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin)); 00170 } 00171 00172 /*-----------------------------------------------------*/ 00173 /* Interrupt (Input) or Slope (Output) modes selection */ 00174 /*-----------------------------------------------------*/ 00175 00176 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x20) != (uint8_t)0x00) /* Interrupt or Slow slope */ 00177 { 00178 GPIOx->CR2 |= GPIO_Pin; 00179 } else /* No external interrupt or No slope control */ 00180 { 00181 GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); 00182 } 00183 00184 } 00185 00186 /** 00187 * @brief Configures the external pull-up on GPIOx pins. 00188 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00189 * @param GPIO_Pin : Specifies the pin number 00190 * This parameter can be one of the following values: 00191 * @arg GPIO_Pin_0: Pin 0 00192 * @arg GPIO_Pin_1: Pin 1 00193 * @arg GPIO_Pin_2: Pin 2 00194 * @arg GPIO_Pin_3: Pin 3 00195 * @arg GPIO_Pin_4: Pin 4 00196 * @arg GPIO_Pin_5: Pin 5 00197 * @arg GPIO_Pin_6: Pin 6 00198 * @arg GPIO_Pin_7: Pin 7 00199 * @param NewState : The new state of the pull up pin. 00200 * Can be ENABLE or DISABLE 00201 * @retval None 00202 */ 00203 void GPIO_ExternalPullUpConfig(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin, FunctionalState NewState) 00204 { 00205 /* Check the parameters */ 00206 assert_param(IS_GPIO_PIN(GPIO_Pin)); 00207 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00208 00209 if (NewState != DISABLE) /* External Pull-Up Set*/ 00210 { 00211 GPIOx->CR1 |= GPIO_Pin; 00212 } else /* External Pull-Up Reset*/ 00213 { 00214 GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin)); 00215 } 00216 } 00217 00218 /** 00219 * @} 00220 */ 00221 00222 /** @defgroup GPIO_Group2 GPIO Read and Write 00223 * @brief GPIO Read and Write 00224 * 00225 @verbatim 00226 =============================================================================== 00227 GPIO Read and Write 00228 =============================================================================== 00229 00230 @endverbatim 00231 * @{ 00232 */ 00233 00234 /** 00235 * @brief Writes data to the specified GPIO data port. 00236 * @note The port must be configured in output mode. 00237 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00238 * @param GPIO_PortVal : Specifies the value to be written to the port output 00239 * data register. 00240 * @retval None 00241 */ 00242 void GPIO_Write(GPIO_TypeDef* GPIOx, uint8_t GPIO_PortVal) 00243 { 00244 GPIOx->ODR = GPIO_PortVal; 00245 } 00246 00247 /** 00248 * @brief Sets or clears the selected data port bit. 00249 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00250 * @param GPIO_Pin: Specifies the port bit to be written. 00251 * This parameter can be one of the following values: 00252 * @arg GPIO_Pin_0: Pin 0 00253 * @arg GPIO_Pin_1: Pin 1 00254 * @arg GPIO_Pin_2: Pin 2 00255 * @arg GPIO_Pin_3: Pin 3 00256 * @arg GPIO_Pin_4: Pin 4 00257 * @arg GPIO_Pin_5: Pin 5 00258 * @arg GPIO_Pin_6: Pin 6 00259 * @arg GPIO_Pin_7: Pin 7 00260 * @param GPIO_BitVal: specifies the desired status to be written. 00261 * This parameter can be SET or RESET 00262 * @retval None 00263 */ 00264 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, BitAction GPIO_BitVal) 00265 { 00266 /* Check the parameters */ 00267 assert_param(IS_GPIO_PIN(GPIO_Pin)); 00268 assert_param(IS_STATE_VALUE(GPIO_BitVal)); 00269 00270 if (GPIO_BitVal != RESET) 00271 { 00272 GPIOx->ODR |= GPIO_Pin; 00273 00274 } 00275 else 00276 { 00277 GPIOx->ODR &= (uint8_t)(~GPIO_Pin); 00278 } 00279 } 00280 00281 /** 00282 * @brief Writes high level to the specified GPIO pins. 00283 * @note The port must be configured in output mode. 00284 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00285 * @param GPIO_Pin : Specifies the pins to be turned high. 00286 * This parameter can be one of the following values: 00287 * @arg GPIO_Pin_0: Pin 0 00288 * @arg GPIO_Pin_1: Pin 1 00289 * @arg GPIO_Pin_2: Pin 2 00290 * @arg GPIO_Pin_3: Pin 3 00291 * @arg GPIO_Pin_4: Pin 4 00292 * @arg GPIO_Pin_5: Pin 5 00293 * @arg GPIO_Pin_6: Pin 6 00294 * @arg GPIO_Pin_7: Pin 7 00295 * @retval None 00296 */ 00297 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin) 00298 { 00299 GPIOx->ODR |= GPIO_Pin; 00300 } 00301 00302 /** 00303 * @brief Writes low level to the specified GPIO pins. 00304 * @note The port must be configured in output mode. 00305 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00306 * @param GPIO_Pin : Specifies the pins to be turned low 00307 * This parameter can be one of the following values: 00308 * @arg GPIO_Pin_0: Pin 0 00309 * @arg GPIO_Pin_1: Pin 1 00310 * @arg GPIO_Pin_2: Pin 2 00311 * @arg GPIO_Pin_3: Pin 3 00312 * @arg GPIO_Pin_4: Pin 4 00313 * @arg GPIO_Pin_5: Pin 5 00314 * @arg GPIO_Pin_6: Pin 6 00315 * @arg GPIO_Pin_7: Pin 7 00316 * @retval None 00317 */ 00318 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin) 00319 { 00320 GPIOx->ODR &= (uint8_t)(~GPIO_Pin); 00321 } 00322 00323 /** 00324 * @brief Toggles the specified GPIO pins. 00325 * @note The port must be configured in output mode. 00326 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00327 * @param GPIO_Pin : Specifies the pins to be toggled. 00328 * @retval None 00329 */ 00330 void GPIO_ToggleBits(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin) 00331 { 00332 GPIOx->ODR ^= GPIO_Pin; 00333 } 00334 00335 /** 00336 * @brief Reads the specified GPIO input data port. 00337 * @note The port must be configured in input mode. 00338 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00339 * @retval The GPIOx input data port value. 00340 */ 00341 uint8_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx) 00342 { 00343 return ((uint8_t)GPIOx->IDR); 00344 } 00345 00346 /** 00347 * @brief Reads the specified GPIO output data port. 00348 * @note The port must be configured in input mode. 00349 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00350 * @retval The GPIOx output data port value. 00351 */ 00352 uint8_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx) 00353 { 00354 return ((uint8_t)GPIOx->ODR); 00355 } 00356 00357 /** 00358 * @brief Reads the specified GPIO input data pin. 00359 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00360 * @param GPIO_Pin : Specifies the pin number. 00361 * This parameter can be one of the following values: 00362 * @arg GPIO_Pin_0: Pin 0 00363 * @arg GPIO_Pin_1: Pin 1 00364 * @arg GPIO_Pin_2: Pin 2 00365 * @arg GPIO_Pin_3: Pin 3 00366 * @arg GPIO_Pin_4: Pin 4 00367 * @arg GPIO_Pin_5: Pin 5 00368 * @arg GPIO_Pin_6: Pin 6 00369 * @arg GPIO_Pin_7: Pin 7 00370 * @retval BitStatus : GPIO input pin status. 00371 */ 00372 BitStatus GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin) 00373 { 00374 return ((BitStatus)(GPIOx->IDR & (uint8_t)GPIO_Pin)); 00375 } 00376 00377 /** 00378 * @brief Reads the specified GPIO Output data pin. 00379 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00380 * @param GPIO_Pin : Specifies the pin number 00381 * @retval BitStatus : GPIO output pin status. 00382 */ 00383 BitStatus GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin) 00384 { 00385 return ((BitStatus)(GPIOx->ODR & (uint8_t)GPIO_Pin)); 00386 } 00387 00388 /** 00389 * @} 00390 */ 00391 00392 /** 00393 * @} 00394 */ 00395 00396 /** 00397 * @} 00398 */ 00399 00400 /** 00401 * @} 00402 */ 00403 00404 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/