📄 stm32100e_eval_ioe.c
字号:
/**
******************************************************************************
* @file stm32100e_eval_ioe.c
* @author MCD Application Team
* @version V4.5.0
* @date 07-March-2011
* @brief This file includes the IO Expander driver for STMPE811 IO Expander
* devices.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* File Info : ---------------------------------------------------------------
Note:
-----
- This driver uses the DMA method for sending and receiving data on I2C bus
which allow higher efficiency and reliability of the communication.
SUPPORTED FEATURES:
- Touch Screen Features: Single point mode (Polling/Interrupt).
- TempSensor Feature: accuracy not determined (Polling).
- IO Read/write : Set/Reset and Read (Polling/Interrupt).
UNSUPPORTED FEATURES:
- Row ADC Feature is not supported (not implemented on STM32100E-EVAL board)
- Joystick: config and Read (Polling/Interrupt)
----------------------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#include "stm32100e_eval_ioe.h"
/** @addtogroup Utilities
* @{
*/
/** @addtogroup STM32_EVAL
* @{
*/
/** @addtogroup STM32100E_EVAL
* @{
*/
/** @defgroup STM32100E_EVAL_IOE
* @brief This file includes the IO Expander driver for STMPE811 IO Expander
* devices.
* @{
*/
/** @defgroup STM32100E_EVAL_IOE_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup STM32100E_EVAL_IOE_Private_Defines
* @{
*/
#define TIMEOUT_MAX 0x1000 /*<! The value of the maximal timeout for I2C waiting loops */
/**
* @}
*/
/** @defgroup STM32100E_EVAL_IOE_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup STM32100E_EVAL_IOE_Private_Variables
* @{
*/
TS_STATE TS_State; /*<! The global structure holding the TS state */
uint32_t IOE_TimeOut = TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */
/**
* @}
*/
/** @defgroup STM32100E_EVAL_IOE_Private_FunctionPrototypes
* @{
*/
static uint16_t IOE_TS_Read_X(void);
static uint16_t IOE_TS_Read_Y(void);
static uint16_t IOE_TS_Read_Z(void);
static void IOE_GPIO_Config(void);
static void IOE_I2C_Config(void);
static void IOE_DMA_Config(IOE_DMADirection_TypeDef Direction, uint8_t* buffer);
static void IOE_EXTI_Config(void);
#ifndef USE_Delay
static void delay(__IO uint32_t nCount);
#endif /* USE_Delay*/
/**
* @}
*/
/** @defgroup STM32100E_EVAL_IOE_Private_Functions
* @{
*/
/**
* @brief Initializes and Configures the IO_Expanders Functionalities
* (Touch Screen ..) and configures all STM32100E-EVAL necessary
* hardware (GPIOs, APB clocks ..).
* @param None
* @retval IOE_OK if all initializations done correctly. Other value if error.
*/
uint8_t IOE_Config(void)
{
/* Configure the needed pins */
IOE_GPIO_Config();
/* Configure I2C peripheral */
IOE_I2C_Config();
/* Read IO Expander 1 ID */
if(IOE_IsOperational(IOE_1_ADDR))
{
return IOE1_NOT_OPERATIONAL;
}
/* Generate IOExpander Software reset */
IOE_Reset(IOE_1_ADDR);
/* ---------------------- IO Expander configuration --------------------- */
/* Enable the GPIO, Touch Screen and ADC functionalities */
IOE_FnctCmd(IOE_1_ADDR, IOE_TS_FCT | IOE_ADC_FCT | IOE_IO_FCT | IOE_TEMPSENS_FCT , ENABLE);
/* Touch Screen controller configuration */
IOE_TS_Config();
/* Temperature Sensor configuration */
IOE_TempSens_Config();
/* ------------------------------------------------------------------------ */
/* Configuration is OK */
return IOE_OK;
}
/**
* @brief Configures The selected interrupts on the IO Expanders.
* @param IOE_ITSRC_Source: the source of the interrupts. Could be one or a
* combination of the following parameters:
* @arg IOE_ITSRC_TSC: Touch Screen interrupts.
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_ITConfig(uint32_t IOE_ITSRC_Source)
{
/* Configure the Interrupt output pin to generate low level (INT_CTRL) */
IOE_ITOutConfig(Polarity_Low, Type_Level);
/* Manage the Touch Screen Interrupts */
if (IOE_ITSRC_Source & IOE_ITSRC_TSC)
{
/* Enable the Global interrupt */
IOE_GITCmd(IOE_1_ADDR, ENABLE);
/* Enable the Global GPIO Interrupt */
IOE_GITConfig(IOE_1_ADDR, (uint8_t)(IOE_GIT_TOUCH | IOE_GIT_FTH | IOE_GIT_FOV), ENABLE);
/* Read the GPIO_IT_STA to clear all pending bits if any */
I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_GPIO_INT_STA);
}
/* Configure the Interrupt line as EXTI source */
IOE_EXTI_Config();
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Writes a bit value to an output IO pin.
* @param IO_Pin: The output pin to be set or reset.
* IO_Pin_x: Where x can be from 0 to 7.
* @param BitVal: The value to be set. This parameter can be one of the
* following values: BitSet or BitReset. See IOE_BitVal_TypeDef.
* @retval IOE_OK or PARAM_ERROR
*/
uint8_t IOE_WriteIOPin(uint8_t IO_Pin, IOE_BitValue_TypeDef BitVal)
{
uint8_t DeviceAddr = 0;
/* Get the IO expander Address according to which pin is to be controlled */
if (IO_Pin & IO1_OUT_ALL_PINS)
{
DeviceAddr = IOE_1_ADDR;
}
else
{
return PARAM_ERROR;
}
/* Apply the bit value to the selected pin */
if (BitVal == BitReset)
{
/* Set the register */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_CLR_PIN, IO_Pin);
}
else
{
/* Set the register */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_SET_PIN, IO_Pin);
}
return IOE_OK;
}
/**
* @brief Returns the status of the selected input IO pin.
* @param IO_Pin: The input pin to be read.
* IO_Pin_x: Where x can be from 0 to 7.
* @arg JOY_IO_PINS: Joystick IO pins (use IOE_JoyStickGetState for these pins)
* @retval None
*/
uint8_t IOE_ReadIOPin(uint32_t IO_Pin)
{
uint8_t DeviceAddr = 0;
uint8_t tmp = 0;
if (IO_Pin & IO1_IN_ALL_PINS)
{
DeviceAddr = IOE_1_ADDR;
}
else
{
return PARAM_ERROR;
}
/* Get all the Pins status */
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_MP_STA);
if ((tmp & (uint8_t)IO_Pin) != 0)
{
return BitSet;
}
else
{
return BitReset;
}
}
/**
* @brief Returns Status and positions of the Touch screen.
* @param None
* @retval Pointer to TS_STATE structure holding Touch Screen information.
*/
TS_STATE* IOE_TS_GetState(void)
{
uint32_t xDiff, yDiff , x , y;
static uint32_t _x = 0, _y = 0;
/* Check if the Touch detect event happenned */
TS_State.TouchDetected = (I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_CTRL) & 0x80);
if(TS_State.TouchDetected)
{
x = IOE_TS_Read_X();
y = IOE_TS_Read_Y();
xDiff = x > _x? (x - _x): (_x - x);
yDiff = y > _y? (y - _y): (_y - y);
if (xDiff + yDiff > 5)
{
_x = x;
_y = y;
}
}
/* Update the X position */
TS_State.X = _x;
/* Update the Y position */
TS_State.Y = _y;
/* Update the Z Pression index */
TS_State.Z = IOE_TS_Read_Z();
/* Clear the interrupt pending bit and enable the FIFO again */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x01);
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x00);
/* Return pointer to the updated structure */
return &TS_State;
}
/**
* @brief Returns the temperature row value (in 16 bit format).
* @param None
* @retval The temperature row value.
*/
uint32_t IOE_TempSens_GetData(void)
{
static __IO uint32_t tmp = 0;
/* Aquire data enable */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_CTRL, 0x03);
/* Enable the TEMPSENS module */
tmp = (uint32_t)((I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_DATA) & 0x03) << 8);
tmp |= (uint32_t)I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_DATA + 1);
tmp = (uint32_t)((33 * tmp * 100) / 751);
tmp = (uint32_t)((tmp + 5) / 10);
/* return the temprature row value */
return tmp;
}
/**
* @brief Checks the selected Global interrupt source pending bit
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR.
* @param Global_IT: the Global interrupt source to be checked, could be:
* @arg Global_IT_GPIO : All IOs interrupt
* @arg Global_IT_ADC : ADC interrupt
* @arg Global_IT_TEMP : Temperature Sensor interrupts
* @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt
* @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt
* @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt
* @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt
* @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt
* @retval Status of the checked flag. Could be SET or RESET.
*/
FlagStatus IOE_GetGITStatus(uint8_t DeviceAddr, uint8_t Global_IT)
{
__IO uint8_t tmp = 0;
/* get the Interrupt status */
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_INT_STA);
if ((tmp & (uint8_t)Global_IT) != 0)
{
return SET;
}
else
{
return RESET;
}
}
/**
* @brief Clears the selected Global interrupt pending bit(s)
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR.
* @param Global_IT: the Global interrupt to be cleared, could be any combination
* of the following values:
* @arg Global_IT_GPIO : All IOs interrupt
* @arg Global_IT_ADC : ADC interrupt
* @arg Global_IT_TEMP : Temperature Sensor interrupts
* @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt
* @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt
* @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt
* @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt
* @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_ClearGITPending(uint8_t DeviceAddr, uint8_t Global_IT)
{
/* Write 1 to the bits that have to be cleared */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_INT_STA, Global_IT);
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Checks the status of the selected IO interrupt pending bit
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR.
* @param IO_IT: the IO interrupt to be checked could be IO_ITx Where x can be
* from 0 to 7.
* @retval Status of the checked flag. Could be SET or RESET.
*/
FlagStatus IOE_GetIOITStatus(uint8_t DeviceAddr, uint8_t IO_IT)
{
uint8_t tmp = 0;
/* get the Interrupt status */
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_STA);
if ((tmp & (uint8_t)IO_IT) != 0)
{
return SET;
}
else
{
return RESET;
}
}
/**
* @brief Clears the selected IO interrupt pending bit(s).
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR.
* @param IO_IT: the IO interrupt to be checked could be IO_ITx Where x can be
* from 0 to 7.
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_ClearIOITPending(uint8_t DeviceAddr, uint8_t IO_IT)
{
/* Write 1 to the bits that have to be cleared */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_STA, IO_IT);
/* Clear the Edge detection pending bit*/
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_ED, IO_IT);
/* Clear the Rising edge pending bit */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_RE, IO_IT);
/* Clear the Falling edge pending bit */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_FE, IO_IT);
return IOE_OK;
}
/**
* @brief Checks if the selected device is correctly configured and
* communicates correctly ont the I2C bus.
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR.
* @retval IOE_OK if IOE is operational. Other value if failure.
*/
uint8_t IOE_IsOperational(uint8_t DeviceAddr)
{
/* Return Error if the ID is not correct */
if( IOE_ReadID(DeviceAddr) != (uint16_t)STMPE811_ID )
{
/* Check if a Timeout occured */
if (IOE_TimeOut == 0)
{
return(IOE_TimeoutUserCallback());
}
else
{
return IOE_FAILURE; /* ID is not Correct */
}
}
else
{
return IOE_OK; /* ID is correct */
}
}
/**
* @brief Resets the IO Expander by Software (SYS_CTRL1, RESET bit).
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR.
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_Reset(uint8_t DeviceAddr)
{
/* Power Down the IO_Expander */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL1, 0x02);
/* wait for a delay to insure registers erasing */
_delay_(2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -