STM8S/A Standard Peripherals Drivers
|
stm8s_gpio.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8s_gpio.c 00004 * @author MCD Application Team 00005 * @version V2.3.0 00006 * @date 16-June-2017 00007 * @brief This file contains all the functions for the GPIO peripheral. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "stm8s_gpio.h" 00030 00031 /** @addtogroup STM8S_StdPeriph_Driver 00032 * @{ 00033 */ 00034 /* Private typedef -----------------------------------------------------------*/ 00035 /* Private define ------------------------------------------------------------*/ 00036 /* Private macro -------------------------------------------------------------*/ 00037 /* Private variables ---------------------------------------------------------*/ 00038 /* Private function prototypes -----------------------------------------------*/ 00039 /* Private functions ---------------------------------------------------------*/ 00040 00041 /* Public functions ----------------------------------------------------------*/ 00042 00043 /** 00044 * @addtogroup GPIO_Public_Functions 00045 * @{ 00046 */ 00047 00048 /** 00049 * @brief Deinitializes the GPIOx peripheral registers to their default reset values. 00050 * @param GPIOx: Select the GPIO peripheral number (x = A to I). 00051 * @retval None 00052 */ 00053 void GPIO_DeInit(GPIO_TypeDef* GPIOx) 00054 { 00055 GPIOx->ODR = GPIO_ODR_RESET_VALUE; /* Reset Output Data Register */ 00056 GPIOx->DDR = GPIO_DDR_RESET_VALUE; /* Reset Data Direction Register */ 00057 GPIOx->CR1 = GPIO_CR1_RESET_VALUE; /* Reset Control Register 1 */ 00058 GPIOx->CR2 = GPIO_CR2_RESET_VALUE; /* Reset Control Register 2 */ 00059 } 00060 00061 /** 00062 * @brief Initializes the GPIOx according to the specified parameters. 00063 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00064 * @param GPIO_Pin : This parameter contains the pin number, it can be any value 00065 * of the @ref GPIO_Pin_TypeDef enumeration. 00066 * @param GPIO_Mode : This parameter can be a value of the 00067 * @ref GPIO_Mode_TypeDef enumeration. 00068 * @retval None 00069 */ 00070 00071 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode) 00072 { 00073 /*----------------------*/ 00074 /* Check the parameters */ 00075 /*----------------------*/ 00076 00077 assert_param(IS_GPIO_MODE_OK(GPIO_Mode)); 00078 assert_param(IS_GPIO_PIN_OK(GPIO_Pin)); 00079 00080 /* Reset corresponding bit to GPIO_Pin in CR2 register */ 00081 GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); 00082 00083 /*-----------------------------*/ 00084 /* Input/Output mode selection */ 00085 /*-----------------------------*/ 00086 00087 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x80) != (uint8_t)0x00) /* Output mode */ 00088 { 00089 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x10) != (uint8_t)0x00) /* High level */ 00090 { 00091 GPIOx->ODR |= (uint8_t)GPIO_Pin; 00092 } 00093 else /* Low level */ 00094 { 00095 GPIOx->ODR &= (uint8_t)(~(GPIO_Pin)); 00096 } 00097 /* Set Output mode */ 00098 GPIOx->DDR |= (uint8_t)GPIO_Pin; 00099 } 00100 else /* Input mode */ 00101 { 00102 /* Set Input mode */ 00103 GPIOx->DDR &= (uint8_t)(~(GPIO_Pin)); 00104 } 00105 00106 /*------------------------------------------------------------------------*/ 00107 /* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */ 00108 /*------------------------------------------------------------------------*/ 00109 00110 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x40) != (uint8_t)0x00) /* Pull-Up or Push-Pull */ 00111 { 00112 GPIOx->CR1 |= (uint8_t)GPIO_Pin; 00113 } 00114 else /* Float or Open-Drain */ 00115 { 00116 GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin)); 00117 } 00118 00119 /*-----------------------------------------------------*/ 00120 /* Interrupt (Input) or Slope (Output) modes selection */ 00121 /*-----------------------------------------------------*/ 00122 00123 if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x20) != (uint8_t)0x00) /* Interrupt or Slow slope */ 00124 { 00125 GPIOx->CR2 |= (uint8_t)GPIO_Pin; 00126 } 00127 else /* No external interrupt or No slope control */ 00128 { 00129 GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin)); 00130 } 00131 } 00132 00133 /** 00134 * @brief Writes data to the specified GPIO data port. 00135 * @note The port must be configured in output mode. 00136 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00137 * @param PortVal : Specifies the value to be written to the port output 00138 * data register. 00139 * @retval None 00140 */ 00141 void GPIO_Write(GPIO_TypeDef* GPIOx, uint8_t PortVal) 00142 { 00143 GPIOx->ODR = PortVal; 00144 } 00145 00146 /** 00147 * @brief Writes high level to the specified GPIO pins. 00148 * @note The port must be configured in output mode. 00149 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00150 * @param PortPins : Specifies the pins to be turned high to the port output. 00151 * data register. 00152 * @retval None 00153 */ 00154 void GPIO_WriteHigh(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins) 00155 { 00156 GPIOx->ODR |= (uint8_t)PortPins; 00157 } 00158 00159 /** 00160 * @brief Writes low level to the specified GPIO pins. 00161 * @note The port must be configured in output mode. 00162 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00163 * @param PortPins : Specifies the pins to be turned low to the port output. 00164 * data register. 00165 * @retval None 00166 */ 00167 void GPIO_WriteLow(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins) 00168 { 00169 GPIOx->ODR &= (uint8_t)(~PortPins); 00170 } 00171 00172 /** 00173 * @brief Writes reverse level to the specified GPIO pins. 00174 * @note The port must be configured in output mode. 00175 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00176 * @param PortPins : Specifies the pins to be reversed to the port output. 00177 * data register. 00178 * @retval None 00179 */ 00180 void GPIO_WriteReverse(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef PortPins) 00181 { 00182 GPIOx->ODR ^= (uint8_t)PortPins; 00183 } 00184 00185 /** 00186 * @brief Reads the specified GPIO output data port. 00187 * @note The port must be configured in input mode. 00188 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00189 * @retval GPIO output data port value. 00190 */ 00191 uint8_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx) 00192 { 00193 return ((uint8_t)GPIOx->ODR); 00194 } 00195 00196 /** 00197 * @brief Reads the specified GPIO input data port. 00198 * @note The port must be configured in input mode. 00199 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00200 * @retval GPIO input data port value. 00201 */ 00202 uint8_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx) 00203 { 00204 return ((uint8_t)GPIOx->IDR); 00205 } 00206 00207 /** 00208 * @brief Reads the specified GPIO input data pin. 00209 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00210 * @param GPIO_Pin : Specifies the pin number. 00211 * @retval BitStatus : GPIO input pin status. 00212 */ 00213 BitStatus GPIO_ReadInputPin(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin) 00214 { 00215 return ((BitStatus)(GPIOx->IDR & (uint8_t)GPIO_Pin)); 00216 } 00217 00218 /** 00219 * @brief Configures the external pull-up on GPIOx pins. 00220 * @param GPIOx : Select the GPIO peripheral number (x = A to I). 00221 * @param GPIO_Pin : Specifies the pin number 00222 * @param NewState : The new state of the pull up pin. 00223 * @retval None 00224 */ 00225 void GPIO_ExternalPullUpConfig(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState) 00226 { 00227 /* Check the parameters */ 00228 assert_param(IS_GPIO_PIN_OK(GPIO_Pin)); 00229 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00230 00231 if (NewState != DISABLE) /* External Pull-Up Set*/ 00232 { 00233 GPIOx->CR1 |= (uint8_t)GPIO_Pin; 00234 } else /* External Pull-Up Reset*/ 00235 { 00236 GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin)); 00237 } 00238 } 00239 00240 /** 00241 * @} 00242 */ 00243 00244 /** 00245 * @} 00246 */ 00247 00248 00249 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/