some experiments with a chinese stm8s103
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
stm8s_experiments/TinySPL_S103/stm8s_gpio.h

381 lines
13 KiB

/**
******************************************************************************
* @file stm8s_gpio.h
* @author MCD Application Team
* @version V2.2.0
* @date 30-September-2014
* @brief This file contains all functions prototype and macros for the GPIO peripheral.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM8S_GPIO_H
#define __STM8S_GPIO_H
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
/* Exported variables ------------------------------------------------------- */
/* Exported types ------------------------------------------------------------*/
/** @addtogroup GPIO_Exported_Types
* @{
*/
/**
* @brief GPIO modes
*
* Bits definitions:
* - Bit 7: 0 = INPUT mode
* 1 = OUTPUT mode
* 1 = PULL-UP (input) or PUSH-PULL (output)
* - Bit 5: 0 = No external interrupt (input) or No slope control (output)
* 1 = External interrupt (input) or Slow control enabled (output)
* - Bit 4: 0 = Low level (output)
* 1 = High level (output push-pull) or HI-Z (output open-drain)
*/
typedef enum {
GPIO_MODE_IN_FL_NO_IT = (uint8_t) 0x00, /*!< Input floating, no external interrupt */
GPIO_MODE_IN_PU_NO_IT = (uint8_t) 0x40, /*!< Input pull-up, no external interrupt */
GPIO_MODE_IN_FL_IT = (uint8_t) 0x20, /*!< Input floating, external interrupt */
GPIO_MODE_IN_PU_IT = (uint8_t) 0x60, /*!< Input pull-up, external interrupt */
GPIO_MODE_OUT_OD_LOW_FAST = (uint8_t) 0xA0, /*!< Output open-drain, low level, 10MHz */
GPIO_MODE_OUT_PP_LOW_FAST = (uint8_t) 0xE0, /*!< Output push-pull, low level, 10MHz */
GPIO_MODE_OUT_OD_LOW_SLOW = (uint8_t) 0x80, /*!< Output open-drain, low level, 2MHz */
GPIO_MODE_OUT_PP_LOW_SLOW = (uint8_t) 0xC0, /*!< Output push-pull, low level, 2MHz */
GPIO_MODE_OUT_OD_HIZ_FAST = (uint8_t) 0xB0, /*!< Output open-drain, high-impedance level,10MHz */
GPIO_MODE_OUT_PP_HIGH_FAST = (uint8_t) 0xF0, /*!< Output push-pull, high level, 10MHz */
GPIO_MODE_OUT_OD_HIZ_SLOW = (uint8_t) 0x90, /*!< Output open-drain, high-impedance level, 2MHz */
GPIO_MODE_OUT_PP_HIGH_SLOW = (uint8_t) 0xD0 /*!< Output push-pull, high level, 2MHz */
} GPIO_Mode_TypeDef;
/**
* @brief Definition of the GPIO pins. Used by the @ref GPIO_Init function in
* order to select the pins to be initialized.
*/
typedef enum {
GPIO_PIN_0 = ((uint8_t) 0x01), /*!< Pin 0 selected */
GPIO_PIN_1 = ((uint8_t) 0x02), /*!< Pin 1 selected */
GPIO_PIN_2 = ((uint8_t) 0x04), /*!< Pin 2 selected */
GPIO_PIN_3 = ((uint8_t) 0x08), /*!< Pin 3 selected */
GPIO_PIN_4 = ((uint8_t) 0x10), /*!< Pin 4 selected */
GPIO_PIN_5 = ((uint8_t) 0x20), /*!< Pin 5 selected */
GPIO_PIN_6 = ((uint8_t) 0x40), /*!< Pin 6 selected */
GPIO_PIN_7 = ((uint8_t) 0x80), /*!< Pin 7 selected */
GPIO_PIN_LNIB = ((uint8_t) 0x0F), /*!< Low nibble pins selected */
GPIO_PIN_HNIB = ((uint8_t) 0xF0), /*!< High nibble pins selected */
GPIO_PIN_ALL = ((uint8_t) 0xFF) /*!< All pins selected */
} GPIO_Pin_TypeDef;
/**
* @}
*/
/* Exported constants --------------------------------------------------------*/
/* Exported macros -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/** @addtogroup GPIO_Private_Macros
* @{
*/
/**
* @brief Macro used by the assert function to check the different functions parameters.
*/
/**
* @brief Macro used by the assert function in order to check the different
* values of GPIOMode_TypeDef.
*/
#define IS_GPIO_MODE_OK(MODE) \
(((MODE) == GPIO_MODE_IN_FL_NO_IT) || \
((MODE) == GPIO_MODE_IN_PU_NO_IT) || \
((MODE) == GPIO_MODE_IN_FL_IT) || \
((MODE) == GPIO_MODE_IN_PU_IT) || \
((MODE) == GPIO_MODE_OUT_OD_LOW_FAST) || \
((MODE) == GPIO_MODE_OUT_PP_LOW_FAST) || \
((MODE) == GPIO_MODE_OUT_OD_LOW_SLOW) || \
((MODE) == GPIO_MODE_OUT_PP_LOW_SLOW) || \
((MODE) == GPIO_MODE_OUT_OD_HIZ_FAST) || \
((MODE) == GPIO_MODE_OUT_PP_HIGH_FAST) || \
((MODE) == GPIO_MODE_OUT_OD_HIZ_SLOW) || \
((MODE) == GPIO_MODE_OUT_PP_HIGH_SLOW))
/**
* @brief Macro used by the assert function in order to check the different
* values of GPIO_Pins.
*/
#define IS_GPIO_PIN_OK(PIN) ((PIN) != (uint8_t)0x00)
/**
* @}
*/
/* Exported functions ------------------------------------------------------- */
/** @addtogroup GPIO_Exported_Functions
* @{
*/
#if 0
void GPIO_DeInit(GPIO_TypeDef *GPIOx);
void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode);
void GPIO_Write(GPIO_TypeDef *GPIOx, uint8_t PortVal);
void GPIO_WriteHigh(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins);
void GPIO_WriteLow(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins);
void GPIO_WriteReverse(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins);
uint8_t GPIO_ReadInputData(GPIO_TypeDef *GPIOx);
uint8_t GPIO_ReadOutputData(GPIO_TypeDef *GPIOx);
BitStatus GPIO_ReadInputPin(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin);
void GPIO_ExternalPullUpConfig(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState);
#endif
/**
* @}
*/
/** @addtogroup STM8S_StdPeriph_Driver
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
/**
* @addtogroup GPIO_Public_Functions
* @{
*/
/**
* @brief Deinitializes the GPIOx peripheral registers to their default reset values.
* @param GPIOx: Select the GPIO peripheral number (x = A to I).
* @retval None
*/
inline void GPIO_DeInit(GPIO_TypeDef *GPIOx)
{
GPIOx->ODR = GPIO_ODR_RESET_VALUE; /* Reset Output Data Register */
GPIOx->DDR = GPIO_DDR_RESET_VALUE; /* Reset Data Direction Register */
GPIOx->CR1 = GPIO_CR1_RESET_VALUE; /* Reset Control Register 1 */
GPIOx->CR2 = GPIO_CR2_RESET_VALUE; /* Reset Control Register 2 */
}
/**
* @brief Initializes the GPIOx according to the specified parameters.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @param GPIO_Pin : This parameter contains the pin number, it can be any value
* of the @ref GPIO_Pin_TypeDef enumeration.
* @param GPIO_Mode : This parameter can be a value of the
* @Ref GPIO_Mode_TypeDef enumeration.
* @retval None
*/
inline void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode)
{
/*----------------------*/
/* Check the parameters */
/*----------------------*/
assert_param(IS_GPIO_MODE_OK(GPIO_Mode));
assert_param(IS_GPIO_PIN_OK(GPIO_Pin));
/* Reset corresponding bit to GPIO_Pin in CR2 register */
GPIOx->CR2 &= (uint8_t) (~(GPIO_Pin));
/*-----------------------------*/
/* Input/Output mode selection */
/*-----------------------------*/
if ((((uint8_t) (GPIO_Mode)) & (uint8_t) 0x80) != (uint8_t) 0x00) /* Output mode */
{
if ((((uint8_t) (GPIO_Mode)) & (uint8_t) 0x10) != (uint8_t) 0x00) /* High level */
{
GPIOx->ODR |= (uint8_t) GPIO_Pin;
} else /* Low level */
{
GPIOx->ODR &= (uint8_t) (~(GPIO_Pin));
}
/* Set Output mode */
GPIOx->DDR |= (uint8_t) GPIO_Pin;
} else /* Input mode */
{
/* Set Input mode */
GPIOx->DDR &= (uint8_t) (~(GPIO_Pin));
}
/*------------------------------------------------------------------------*/
/* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */
/*------------------------------------------------------------------------*/
if ((((uint8_t) (GPIO_Mode)) & (uint8_t) 0x40) != (uint8_t) 0x00) /* Pull-Up or Push-Pull */
{
GPIOx->CR1 |= (uint8_t) GPIO_Pin;
} else /* Float or Open-Drain */
{
GPIOx->CR1 &= (uint8_t) (~(GPIO_Pin));
}
/*-----------------------------------------------------*/
/* Interrupt (Input) or Slope (Output) modes selection */
/*-----------------------------------------------------*/
if ((((uint8_t) (GPIO_Mode)) & (uint8_t) 0x20) != (uint8_t) 0x00) /* Interrupt or Slow slope */
{
GPIOx->CR2 |= (uint8_t) GPIO_Pin;
} else /* No external interrupt or No slope control */
{
GPIOx->CR2 &= (uint8_t) (~(GPIO_Pin));
}
}
/**
* @brief Writes data to the specified GPIO data port.
* @note The port must be configured in output mode.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @param GPIO_PortVal : Specifies the value to be written to the port output
* data register.
* @retval None
*/
inline void GPIO_Write(GPIO_TypeDef *GPIOx, uint8_t PortVal)
{
GPIOx->ODR = PortVal;
}
/**
* @brief Writes high level to the specified GPIO pins.
* @note The port must be configured in output mode.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @param PortPins : Specifies the pins to be turned high to the port output.
* data register.
* @retval None
*/
inline void GPIO_WriteHigh(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins)
{
GPIOx->ODR |= (uint8_t) PortPins;
}
/**
* @brief Writes low level to the specified GPIO pins.
* @note The port must be configured in output mode.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @param PortPins : Specifies the pins to be turned low to the port output.
* data register.
* @retval None
*/
inline void GPIO_WriteLow(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins)
{
GPIOx->ODR &= (uint8_t) (~PortPins);
}
/**
* @brief Writes reverse level to the specified GPIO pins.
* @note The port must be configured in output mode.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @param PortPins : Specifies the pins to be reversed to the port output.
* data register.
* @retval None
*/
inline void GPIO_WriteReverse(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef PortPins)
{
GPIOx->ODR ^= (uint8_t) PortPins;
}
/**
* @brief Reads the specified GPIO output data port.
* @note The port must be configured in input mode.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @retval GPIO output data port value.
*/
inline uint8_t GPIO_ReadOutputData(GPIO_TypeDef *GPIOx)
{
return ((uint8_t) GPIOx->ODR);
}
/**
* @brief Reads the specified GPIO input data port.
* @note The port must be configured in input mode.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @retval GPIO input data port value.
*/
inline uint8_t GPIO_ReadInputData(GPIO_TypeDef *GPIOx)
{
return ((uint8_t) GPIOx->IDR);
}
/**
* @brief Reads the specified GPIO input data pin.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @param GPIO_Pin : Specifies the pin number.
* @retval BitStatus : GPIO input pin status.
*/
inline BitStatus GPIO_ReadInputPin(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
return ((BitStatus) (GPIOx->IDR & (uint8_t) GPIO_Pin));
}
/**
* @brief Configures the external pull-up on GPIOx pins.
* @param GPIOx : Select the GPIO peripheral number (x = A to I).
* @param GPIO_Pin : Specifies the pin number
* @param NewState : The new state of the pull up pin.
* @retval None
*/
inline void GPIO_ExternalPullUpConfig(GPIO_TypeDef *GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN_OK(GPIO_Pin));
assert_param(IS_FUNCTIONALSTATE_OK(NewState));
if (NewState != DISABLE) /* External Pull-Up Set*/
{
GPIOx->CR1 |= (uint8_t) GPIO_Pin;
} else /* External Pull-Up Reset*/
{
GPIOx->CR1 &= (uint8_t) (~(GPIO_Pin));
}
}
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#endif /* __STM8L_GPIO_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/