STM32F429ZI Discovery board drivers

Dependents:   2a 2b 2c 2d1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stmpe811.c Source File

stmpe811.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stmpe811.c
00004   * @author  MCD Application Team
00005   * @version V2.0.0
00006   * @date    15-December-2014
00007   * @brief   This file provides a set of functions needed to manage the STMPE811
00008   *          IO Expander devices.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00013   *
00014   * Redistribution and use in source and binary forms, with or without modification,
00015   * are permitted provided that the following conditions are met:
00016   *   1. Redistributions of source code must retain the above copyright notice,
00017   *      this list of conditions and the following disclaimer.
00018   *   2. Redistributions in binary form must reproduce the above copyright notice,
00019   *      this list of conditions and the following disclaimer in the documentation
00020   *      and/or other materials provided with the distribution.
00021   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022   *      may be used to endorse or promote products derived from this software
00023   *      without specific prior written permission.
00024   *
00025   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035   *
00036   ******************************************************************************
00037   */  
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 #include "stmpe811.h"
00041 
00042 /** @addtogroup BSP
00043   * @{
00044   */
00045 
00046 /** @addtogroup Components
00047   * @{
00048   */ 
00049   
00050 /** @defgroup STMPE811
00051   * @{
00052   */   
00053 
00054 /** @defgroup STMPE811_Private_Types_Definitions
00055   * @{
00056   */ 
00057 
00058 /** @defgroup STMPE811_Private_Defines
00059   * @{
00060   */ 
00061 #define STMPE811_MAX_INSTANCE         2 
00062 /**
00063   * @}
00064   */
00065 
00066 /** @defgroup STMPE811_Private_Macros
00067   * @{
00068   */ 
00069 /**
00070   * @}
00071   */ 
00072 
00073 /** @defgroup STMPE811_Private_Variables
00074   * @{
00075   */ 
00076 
00077 /* Touch screen driver structure initialization */  
00078 TS_DrvTypeDef stmpe811_ts_drv = 
00079 {
00080   stmpe811_Init,
00081   stmpe811_ReadID,
00082   stmpe811_Reset,
00083   stmpe811_TS_Start,
00084   stmpe811_TS_DetectTouch,
00085   stmpe811_TS_GetXY,
00086   stmpe811_TS_EnableIT,
00087   stmpe811_TS_ClearIT,
00088   stmpe811_TS_ITStatus,
00089   stmpe811_TS_DisableIT,
00090 };
00091 
00092 /* IO driver structure initialization */ 
00093 IO_DrvTypeDef stmpe811_io_drv = 
00094 {
00095   stmpe811_Init,
00096   stmpe811_ReadID,
00097   stmpe811_Reset,
00098   stmpe811_IO_Start,
00099   stmpe811_IO_Config,
00100   stmpe811_IO_WritePin,
00101   stmpe811_IO_ReadPin,
00102   stmpe811_IO_EnableIT,
00103   stmpe811_IO_DisableIT,
00104   stmpe811_IO_ITStatus,
00105   stmpe811_IO_ClearIT,
00106 };
00107 
00108 /* stmpe811 instances by address */
00109 uint8_t stmpe811[STMPE811_MAX_INSTANCE] = {0};
00110 /**
00111   * @}
00112   */ 
00113 
00114 /** @defgroup STMPE811_Private_Function_Prototypes
00115   * @{
00116   */
00117 static uint8_t stmpe811_GetInstance(uint16_t DeviceAddr); 
00118 /**
00119   * @}
00120   */ 
00121 
00122 /** @defgroup STMPE811_Private_Functions
00123   * @{
00124   */
00125 
00126 /**
00127   * @brief  Initialize the stmpe811 and configure the needed hardware resources
00128   * @param  DeviceAddr: Device address on communication Bus.
00129   * @retval None
00130   */
00131 void stmpe811_Init(uint16_t DeviceAddr)
00132 {
00133   uint8_t instance;
00134   uint8_t empty;
00135   
00136   /* Check if device instance already exists */
00137   instance = stmpe811_GetInstance(DeviceAddr);
00138   
00139   /* To prevent double initialization */
00140   if(instance == 0xFF)
00141   {
00142     /* Look for empty instance */
00143     empty = stmpe811_GetInstance(0);
00144     
00145     if(empty < STMPE811_MAX_INSTANCE)
00146     {
00147       /* Register the current device instance */
00148       stmpe811[empty] = DeviceAddr;
00149       
00150       /* Initialize IO BUS layer */
00151       IOE_Init(); 
00152       
00153       /* Generate stmpe811 Software reset */
00154       stmpe811_Reset(DeviceAddr);
00155     }
00156   }
00157 }
00158  
00159 /**
00160   * @brief  Reset the stmpe811 by Software.
00161   * @param  DeviceAddr: Device address on communication Bus.  
00162   * @retval None
00163   */
00164 void stmpe811_Reset(uint16_t DeviceAddr)
00165 {
00166   /* Power Down the stmpe811 */  
00167   IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL1, 2);
00168 
00169   /* Wait for a delay to ensure registers erasing */
00170   IOE_Delay(10); 
00171   
00172   /* Power On the Codec after the power off => all registers are reinitialized */
00173   IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL1, 0);
00174   
00175   /* Wait for a delay to ensure registers erasing */
00176   IOE_Delay(2); 
00177 }
00178 
00179 /**
00180   * @brief  Read the stmpe811 IO Expander device ID.
00181   * @param  DeviceAddr: Device address on communication Bus.  
00182   * @retval The Device ID (two bytes).
00183   */
00184 uint16_t stmpe811_ReadID(uint16_t DeviceAddr)
00185 {
00186   /* Initialize IO BUS layer */
00187   IOE_Init(); 
00188   
00189   /* Return the device ID value */
00190   return ((IOE_Read(DeviceAddr, STMPE811_REG_CHP_ID_LSB) << 8) |\
00191           (IOE_Read(DeviceAddr, STMPE811_REG_CHP_ID_MSB)));
00192 }
00193 
00194 /**
00195   * @brief  Enable the Global interrupt.
00196   * @param  DeviceAddr: Device address on communication Bus.       
00197   * @retval None
00198   */
00199 void stmpe811_EnableGlobalIT(uint16_t DeviceAddr)
00200 {
00201   uint8_t tmp = 0;
00202   
00203   /* Read the Interrupt Control register  */
00204   tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
00205   
00206   /* Set the global interrupts to be Enabled */    
00207   tmp |= (uint8_t)STMPE811_GIT_EN;
00208   
00209   /* Write Back the Interrupt Control register */
00210   IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp); 
00211 }
00212 
00213 /**
00214   * @brief  Disable the Global interrupt.
00215   * @param  DeviceAddr: Device address on communication Bus.      
00216   * @retval None
00217   */
00218 void stmpe811_DisableGlobalIT(uint16_t DeviceAddr)
00219 {
00220   uint8_t tmp = 0;
00221   
00222   /* Read the Interrupt Control register  */
00223   tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
00224 
00225   /* Set the global interrupts to be Disabled */    
00226   tmp &= ~(uint8_t)STMPE811_GIT_EN;
00227  
00228   /* Write Back the Interrupt Control register */
00229   IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
00230     
00231 }
00232 
00233 /**
00234   * @brief  Enable the interrupt mode for the selected IT source
00235   * @param  DeviceAddr: Device address on communication Bus.  
00236   * @param Source: The interrupt source to be configured, could be:
00237   *   @arg  STMPE811_GIT_IO: IO interrupt 
00238   *   @arg  STMPE811_GIT_ADC : ADC interrupt    
00239   *   @arg  STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
00240   *   @arg  STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt      
00241   *   @arg  STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt     
00242   *   @arg  STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt   
00243   *   @arg  STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt  
00244   * @retval None
00245   */
00246 void stmpe811_EnableITSource(uint16_t DeviceAddr, uint8_t Source)
00247 {
00248   uint8_t tmp = 0;
00249   
00250   /* Get the current value of the INT_EN register */
00251   tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_EN);
00252 
00253   /* Set the interrupts to be Enabled */    
00254   tmp |= Source; 
00255   
00256   /* Set the register */
00257   IOE_Write(DeviceAddr, STMPE811_REG_INT_EN, tmp);   
00258 }
00259 
00260 /**
00261   * @brief  Disable the interrupt mode for the selected IT source
00262   * @param  DeviceAddr: Device address on communication Bus.  
00263   * @param  Source: The interrupt source to be configured, could be:
00264   *   @arg  STMPE811_GIT_IO: IO interrupt 
00265   *   @arg  STMPE811_GIT_ADC : ADC interrupt    
00266   *   @arg  STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
00267   *   @arg  STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt      
00268   *   @arg  STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt     
00269   *   @arg  STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt   
00270   *   @arg  STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt  
00271   * @retval None
00272   */
00273 void stmpe811_DisableITSource(uint16_t DeviceAddr, uint8_t Source)
00274 {
00275   uint8_t tmp = 0;
00276   
00277   /* Get the current value of the INT_EN register */
00278   tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_EN);
00279 
00280   /* Set the interrupts to be Enabled */    
00281   tmp &= ~Source; 
00282   
00283   /* Set the register */
00284   IOE_Write(DeviceAddr, STMPE811_REG_INT_EN, tmp);   
00285 }
00286 
00287 /**
00288   * @brief  Set the global interrupt Polarity.
00289   * @param  DeviceAddr: Device address on communication Bus.  
00290   * @param  Polarity: the IT mode polarity, could be one of the following values:
00291   *   @arg  STMPE811_POLARITY_LOW: Interrupt line is active Low/Falling edge      
00292   *   @arg  STMPE811_POLARITY_HIGH: Interrupt line is active High/Rising edge              
00293   * @retval None
00294   */
00295 void stmpe811_SetITPolarity(uint16_t DeviceAddr, uint8_t Polarity)
00296 {
00297   uint8_t tmp = 0;
00298   
00299   /* Get the current register value */ 
00300   tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
00301   
00302   /* Mask the polarity bits */
00303   tmp &= ~(uint8_t)0x04;
00304     
00305   /* Modify the Interrupt Output line configuration */
00306   tmp |= Polarity;
00307   
00308   /* Set the new register value */
00309   IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
00310  
00311 }
00312 
00313 /**
00314   * @brief  Set the global interrupt Type. 
00315   * @param  DeviceAddr: Device address on communication Bus.      
00316   * @param  Type: Interrupt line activity type, could be one of the following values:
00317   *   @arg  STMPE811_TYPE_LEVEL: Interrupt line is active in level model         
00318   *   @arg  STMPE811_TYPE_EDGE: Interrupt line is active in edge model           
00319   * @retval None
00320   */
00321 void stmpe811_SetITType(uint16_t DeviceAddr, uint8_t Type)
00322 {
00323   uint8_t tmp = 0;
00324   
00325   /* Get the current register value */ 
00326   tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
00327   
00328   /* Mask the type bits */
00329   tmp &= ~(uint8_t)0x02;
00330     
00331   /* Modify the Interrupt Output line configuration */
00332   tmp |= Type;
00333   
00334   /* Set the new register value */
00335   IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
00336  
00337 }
00338 
00339 /**
00340   * @brief  Check the selected Global interrupt source pending bit
00341   * @param  DeviceAddr: Device address on communication Bus. 
00342   * @param  Source: the Global interrupt source to be checked, could be:
00343   *   @arg  STMPE811_GIT_IO: IO interrupt 
00344   *   @arg  STMPE811_GIT_ADC : ADC interrupt    
00345   *   @arg  STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
00346   *   @arg  STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt      
00347   *   @arg  STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt     
00348   *   @arg  STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt   
00349   *   @arg  STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt      
00350   * @retval The checked Global interrupt source status.
00351   */
00352 uint8_t stmpe811_GlobalITStatus(uint16_t DeviceAddr, uint8_t Source)
00353 {
00354   /* Return the global IT source status */
00355   return((IOE_Read(DeviceAddr, STMPE811_REG_INT_STA) & Source) == Source);
00356 }
00357 
00358 /**
00359   * @brief  Return the Global interrupts status
00360   * @param  DeviceAddr: Device address on communication Bus. 
00361   * @param  Source: the Global interrupt source to be checked, could be:
00362   *   @arg  STMPE811_GIT_IO: IO interrupt 
00363   *   @arg  STMPE811_GIT_ADC : ADC interrupt    
00364   *   @arg  STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
00365   *   @arg  STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt      
00366   *   @arg  STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt     
00367   *   @arg  STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt   
00368   *   @arg  STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt      
00369   * @retval The checked Global interrupt source status.
00370   */
00371 uint8_t stmpe811_ReadGITStatus(uint16_t DeviceAddr, uint8_t Source)
00372 {
00373   /* Return the global IT source status */
00374   return((IOE_Read(DeviceAddr, STMPE811_REG_INT_STA) & Source));
00375 }
00376 
00377 /**
00378   * @brief  Clear the selected Global interrupt pending bit(s)
00379   * @param  DeviceAddr: Device address on communication Bus. 
00380   * @param  Source: the Global interrupt source to be cleared, could be any combination
00381   *         of the following values:        
00382   *   @arg  STMPE811_GIT_IO: IO interrupt 
00383   *   @arg  STMPE811_GIT_ADC : ADC interrupt    
00384   *   @arg  STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
00385   *   @arg  STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt      
00386   *   @arg  STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt     
00387   *   @arg  STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt   
00388   *   @arg  STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt 
00389   * @retval None
00390   */
00391 void stmpe811_ClearGlobalIT(uint16_t DeviceAddr, uint8_t Source)
00392 {
00393   /* Write 1 to the bits that have to be cleared */
00394   IOE_Write(DeviceAddr, STMPE811_REG_INT_STA, Source);
00395 }
00396 
00397 /**
00398   * @brief  Start the IO functionality use and disable the AF for selected IO pin(s).
00399   * @param  DeviceAddr: Device address on communication Bus.  
00400   * @param  IO_Pin: The IO pin(s) to put in AF. This parameter can be one 
00401   *         of the following values:
00402   *   @arg  STMPE811_PIN_x: where x can be from 0 to 7.
00403   * @retval None
00404   */
00405 void stmpe811_IO_Start(uint16_t DeviceAddr, uint32_t IO_Pin)
00406 {
00407   uint8_t mode;
00408   
00409   /* Get the current register value */
00410   mode = IOE_Read(DeviceAddr, STMPE811_REG_SYS_CTRL2);
00411   
00412   /* Set the Functionalities to be Disabled */    
00413   mode &= ~(STMPE811_IO_FCT | STMPE811_ADC_FCT);  
00414   
00415   /* Write the new register value */  
00416   IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode); 
00417 
00418   /* Disable AF for the selected IO pin(s) */
00419   stmpe811_IO_DisableAF(DeviceAddr, (uint8_t)IO_Pin);
00420 }
00421 
00422 /**
00423   * @brief  Configures the IO pin(s) according to IO mode structure value.
00424   * @param  DeviceAddr: Device address on communication Bus.  
00425   * @param  IO_Pin: The output pin to be set or reset. This parameter can be one 
00426   *         of the following values:   
00427   *   @arg  STMPE811_PIN_x: where x can be from 0 to 7.
00428   * @param  IO_Mode: The IO pin mode to configure, could be one of the following values:
00429   *   @arg  IO_MODE_INPUT
00430   *   @arg  IO_MODE_OUTPUT
00431   *   @arg  IO_MODE_IT_RISING_EDGE
00432   *   @arg  IO_MODE_IT_FALLING_EDGE
00433   *   @arg  IO_MODE_IT_LOW_LEVEL
00434   *   @arg  IO_MODE_IT_HIGH_LEVEL            
00435   * @retval 0 if no error, IO_Mode if error
00436   */
00437 uint8_t stmpe811_IO_Config(uint16_t DeviceAddr, uint32_t IO_Pin, IO_ModeTypedef IO_Mode)
00438 {
00439   uint8_t error_code = 0;
00440 
00441   /* Configure IO pin according to selected IO mode */
00442   switch(IO_Mode)
00443   {
00444   case IO_MODE_INPUT: /* Input mode */
00445     stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
00446     break;
00447     
00448   case IO_MODE_OUTPUT: /* Output mode */
00449     stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_OUT);
00450     break;
00451   
00452   case IO_MODE_IT_RISING_EDGE: /* Interrupt rising edge mode */
00453     stmpe811_IO_EnableIT(DeviceAddr);
00454     stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
00455     stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN); 
00456     stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_EDGE);      
00457     stmpe811_IO_SetEdgeMode(DeviceAddr, IO_Pin, STMPE811_EDGE_RISING); 
00458     break;
00459   
00460   case IO_MODE_IT_FALLING_EDGE: /* Interrupt falling edge mode */
00461     stmpe811_IO_EnableIT(DeviceAddr);
00462     stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
00463     stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN); 
00464     stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_EDGE);    
00465     stmpe811_IO_SetEdgeMode(DeviceAddr, IO_Pin, STMPE811_EDGE_FALLING); 
00466     break;
00467   
00468   case IO_MODE_IT_LOW_LEVEL: /* Low level interrupt mode */
00469     stmpe811_IO_EnableIT(DeviceAddr);
00470     stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
00471     stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN); 
00472     stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_LEVEL);
00473     stmpe811_SetITPolarity(DeviceAddr, STMPE811_POLARITY_LOW);      
00474     break;
00475     
00476   case IO_MODE_IT_HIGH_LEVEL: /* High level interrupt mode */
00477     stmpe811_IO_EnableIT(DeviceAddr);
00478     stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
00479     stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN); 
00480     stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_LEVEL);
00481     stmpe811_SetITPolarity(DeviceAddr, STMPE811_POLARITY_HIGH);  
00482     break;    
00483 
00484   default:
00485     error_code = (uint8_t) IO_Mode;
00486     break;
00487   } 
00488   return error_code;
00489 }
00490 
00491 /**
00492   * @brief  Initialize the selected IO pin direction.
00493   * @param  DeviceAddr: Device address on communication Bus.
00494   * @param  IO_Pin: The IO pin to be configured. This parameter could be any 
00495   *         combination of the following values:
00496   *   @arg  STMPE811_PIN_x: Where x can be from 0 to 7.   
00497   * @param  Direction: could be STMPE811_DIRECTION_IN or STMPE811_DIRECTION_OUT.      
00498   * @retval None
00499   */
00500 void stmpe811_IO_InitPin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Direction)
00501 {
00502   uint8_t tmp = 0;   
00503   
00504   /* Get all the Pins direction */
00505   tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_DIR);
00506   
00507   /* Set the selected pin direction */
00508   if (Direction != STMPE811_DIRECTION_IN)
00509   {
00510     tmp |= (uint8_t)IO_Pin;
00511   }  
00512   else 
00513   {
00514     tmp &= ~(uint8_t)IO_Pin;
00515   }
00516   
00517   /* Write the register new value */
00518   IOE_Write(DeviceAddr, STMPE811_REG_IO_DIR, tmp);   
00519 }
00520 
00521 /**
00522   * @brief  Disable the AF for the selected IO pin(s).
00523   * @param  DeviceAddr: Device address on communication Bus.  
00524   * @param  IO_Pin: The IO pin to be configured. This parameter could be any 
00525   *         combination of the following values:
00526   *   @arg  STMPE811_PIN_x: Where x can be from 0 to 7.        
00527   * @retval None
00528   */
00529 void stmpe811_IO_DisableAF(uint16_t DeviceAddr, uint32_t IO_Pin)
00530 {
00531   uint8_t tmp = 0;
00532   
00533   /* Get the current state of the IO_AF register */
00534   tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_AF);
00535 
00536   /* Enable the selected pins alternate function */
00537   tmp |= (uint8_t)IO_Pin;
00538 
00539   /* Write back the new value in IO AF register */
00540   IOE_Write(DeviceAddr, STMPE811_REG_IO_AF, tmp);
00541   
00542 }
00543 
00544 /**
00545   * @brief  Enable the AF for the selected IO pin(s).
00546   * @param  DeviceAddr: Device address on communication Bus.  
00547   * @param  IO_Pin: The IO pin to be configured. This parameter could be any 
00548   *         combination of the following values:
00549   *   @arg  STMPE811_PIN_x: Where x can be from 0 to 7.       
00550   * @retval None
00551   */
00552 void stmpe811_IO_EnableAF(uint16_t DeviceAddr, uint32_t IO_Pin)
00553 {
00554   uint8_t tmp = 0;
00555   
00556   /* Get the current register value */
00557   tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_AF);
00558 
00559   /* Enable the selected pins alternate function */   
00560   tmp &= ~(uint8_t)IO_Pin;   
00561   
00562   /* Write back the new register value */
00563   IOE_Write(DeviceAddr, STMPE811_REG_IO_AF, tmp); 
00564 }
00565 
00566 /**
00567   * @brief  Configure the Edge for which a transition is detectable for the
00568   *         selected pin.
00569   * @param  DeviceAddr: Device address on communication Bus.
00570   * @param  IO_Pin: The IO pin to be configured. This parameter could be any 
00571   *         combination of the following values:
00572   *   @arg  STMPE811_PIN_x: Where x can be from 0 to 7.  
00573   * @param  Edge: The edge which will be detected. This parameter can be one or
00574   *         a combination of following values: STMPE811_EDGE_FALLING and STMPE811_EDGE_RISING .
00575   * @retval None
00576   */
00577 void stmpe811_IO_SetEdgeMode(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Edge)
00578 {
00579   uint8_t tmp1 = 0, tmp2 = 0;   
00580   
00581   /* Get the current registers values */
00582   tmp1 = IOE_Read(DeviceAddr, STMPE811_REG_IO_FE);
00583   tmp2 = IOE_Read(DeviceAddr, STMPE811_REG_IO_RE);
00584 
00585   /* Disable the Falling Edge */
00586   tmp1 &= ~(uint8_t)IO_Pin;
00587   
00588   /* Disable the Falling Edge */
00589   tmp2 &= ~(uint8_t)IO_Pin;
00590 
00591   /* Enable the Falling edge if selected */
00592   if (Edge & STMPE811_EDGE_FALLING)
00593   {
00594     tmp1 |= (uint8_t)IO_Pin;
00595   }
00596 
00597   /* Enable the Rising edge if selected */
00598   if (Edge & STMPE811_EDGE_RISING)
00599   {
00600     tmp2 |= (uint8_t)IO_Pin;
00601   }
00602 
00603   /* Write back the new registers values */
00604   IOE_Write(DeviceAddr, STMPE811_REG_IO_FE, tmp1);
00605   IOE_Write(DeviceAddr, STMPE811_REG_IO_RE, tmp2);
00606 }
00607 
00608 /**
00609   * @brief  Write a new IO pin state.
00610   * @param  DeviceAddr: Device address on communication Bus.  
00611   * @param IO_Pin: The output pin to be set or reset. This parameter can be one 
00612   *        of the following values:
00613   *   @arg  STMPE811_PIN_x: where x can be from 0 to 7. 
00614   * @param PinState: The new IO pin state.
00615   * @retval None
00616   */
00617 void stmpe811_IO_WritePin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t PinState)
00618 {
00619   /* Apply the bit value to the selected pin */
00620   if (PinState != 0)
00621   {
00622     /* Set the register */
00623     IOE_Write(DeviceAddr, STMPE811_REG_IO_SET_PIN, (uint8_t)IO_Pin);
00624   }
00625   else
00626   {
00627     /* Set the register */
00628     IOE_Write(DeviceAddr, STMPE811_REG_IO_CLR_PIN, (uint8_t)IO_Pin);
00629   } 
00630 }
00631 
00632 /**
00633   * @brief  Return the state of the selected IO pin(s).
00634   * @param  DeviceAddr: Device address on communication Bus.  
00635   * @param IO_Pin: The output pin to be set or reset. This parameter can be one 
00636   *        of the following values:
00637   *   @arg  STMPE811_PIN_x: where x can be from 0 to 7. 
00638   * @retval IO pin(s) state.
00639   */
00640 uint32_t stmpe811_IO_ReadPin(uint16_t DeviceAddr, uint32_t IO_Pin)
00641 {
00642   return((uint32_t)(IOE_Read(DeviceAddr, STMPE811_REG_IO_MP_STA) & (uint8_t)IO_Pin));
00643 }
00644 
00645 /**
00646   * @brief  Enable the global IO interrupt source.
00647   * @param  DeviceAddr: Device address on communication Bus.  
00648   * @retval None
00649   */
00650 void stmpe811_IO_EnableIT(uint16_t DeviceAddr)
00651 { 
00652   IOE_ITConfig();
00653   
00654   /* Enable global IO IT source */
00655   stmpe811_EnableITSource(DeviceAddr, STMPE811_GIT_IO);
00656   
00657   /* Enable global interrupt */
00658   stmpe811_EnableGlobalIT(DeviceAddr); 
00659 }
00660 
00661 /**
00662   * @brief  Disable the global IO interrupt source.
00663   * @param  DeviceAddr: Device address on communication Bus.   
00664   * @retval None
00665   */
00666 void stmpe811_IO_DisableIT(uint16_t DeviceAddr)
00667 {
00668   /* Disable the global interrupt */
00669   stmpe811_DisableGlobalIT(DeviceAddr);
00670   
00671   /* Disable global IO IT source */
00672   stmpe811_DisableITSource(DeviceAddr, STMPE811_GIT_IO);    
00673 }
00674 
00675 /**
00676   * @brief  Enable interrupt mode for the selected IO pin(s).
00677   * @param  DeviceAddr: Device address on communication Bus.
00678   * @param  IO_Pin: The IO interrupt to be enabled. This parameter could be any 
00679   *         combination of the following values:
00680   *   @arg  STMPE811_PIN_x: where x can be from 0 to 7.
00681   * @retval None
00682   */
00683 void stmpe811_IO_EnablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
00684 {
00685   uint8_t tmp = 0;
00686   
00687   /* Get the IO interrupt state */
00688   tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_EN);
00689   
00690   /* Set the interrupts to be enabled */    
00691   tmp |= (uint8_t)IO_Pin;
00692   
00693   /* Write the register new value */
00694   IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_EN, tmp);  
00695 }
00696 
00697 /**
00698   * @brief  Disable interrupt mode for the selected IO pin(s).
00699   * @param  DeviceAddr: Device address on communication Bus.
00700   * @param  IO_Pin: The IO interrupt to be disabled. This parameter could be any 
00701   *         combination of the following values:
00702   *   @arg  STMPE811_PIN_x: where x can be from 0 to 7.
00703   * @retval None
00704   */
00705 void stmpe811_IO_DisablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
00706 {
00707   uint8_t tmp = 0;
00708   
00709   /* Get the IO interrupt state */
00710   tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_EN);
00711   
00712   /* Set the interrupts to be Disabled */    
00713   tmp &= ~(uint8_t)IO_Pin;
00714   
00715   /* Write the register new value */
00716   IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_EN, tmp);   
00717 }
00718 
00719 /**
00720   * @brief  Check the status of the selected IO interrupt pending bit
00721   * @param  DeviceAddr: Device address on communication Bus.
00722   * @param  IO_Pin: The IO interrupt to be checked could be:
00723   *   @arg  STMPE811_PIN_x Where x can be from 0 to 7.             
00724   * @retval Status of the checked IO pin(s).
00725   */
00726 uint32_t stmpe811_IO_ITStatus(uint16_t DeviceAddr, uint32_t IO_Pin)
00727 {
00728   /* Get the Interrupt status */
00729   return(IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_STA) & (uint8_t)IO_Pin); 
00730 }
00731 
00732 /**
00733   * @brief  Clear the selected IO interrupt pending bit(s).
00734   * @param  DeviceAddr: Device address on communication Bus.
00735   * @param  IO_Pin: the IO interrupt to be cleared, could be:
00736   *   @arg  STMPE811_PIN_x: Where x can be from 0 to 7.            
00737   * @retval None
00738   */
00739 void stmpe811_IO_ClearIT(uint16_t DeviceAddr, uint32_t IO_Pin)
00740 {
00741   /* Clear the global IO IT pending bit */
00742   stmpe811_ClearGlobalIT(DeviceAddr, STMPE811_GIT_IO);
00743   
00744   /* Clear the IO IT pending bit(s) */
00745   IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_STA, (uint8_t)IO_Pin);  
00746   
00747   /* Clear the Edge detection pending bit*/
00748   IOE_Write(DeviceAddr, STMPE811_REG_IO_ED, (uint8_t)IO_Pin);
00749   
00750   /* Clear the Rising edge pending bit */
00751   IOE_Write(DeviceAddr, STMPE811_REG_IO_RE, (uint8_t)IO_Pin);
00752   
00753   /* Clear the Falling edge pending bit */
00754   IOE_Write(DeviceAddr, STMPE811_REG_IO_FE, (uint8_t)IO_Pin); 
00755 }
00756 
00757 /**
00758   * @brief  Configures the touch Screen Controller (Single point detection)
00759   * @param  DeviceAddr: Device address on communication Bus.
00760   * @retval None.
00761   */
00762 void stmpe811_TS_Start(uint16_t DeviceAddr)
00763 {
00764   uint8_t mode;
00765   
00766   /* Get the current register value */
00767   mode = IOE_Read(DeviceAddr, STMPE811_REG_SYS_CTRL2);
00768   
00769   /* Set the Functionalities to be Enabled */    
00770   mode &= ~(STMPE811_IO_FCT);  
00771   
00772   /* Write the new register value */  
00773   IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode); 
00774 
00775   /* Select TSC pins in TSC alternate mode */  
00776   stmpe811_IO_EnableAF(DeviceAddr, STMPE811_TOUCH_IO_ALL);
00777   
00778   /* Set the Functionalities to be Enabled */    
00779   mode &= ~(STMPE811_TS_FCT | STMPE811_ADC_FCT);  
00780   
00781   /* Set the new register value */  
00782   IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode); 
00783   
00784   /* Select Sample Time, bit number and ADC Reference */
00785   IOE_Write(DeviceAddr, STMPE811_REG_ADC_CTRL1, 0x49);
00786   
00787   /* Wait for 2 ms */
00788   IOE_Delay(2); 
00789   
00790   /* Select the ADC clock speed: 3.25 MHz */
00791   IOE_Write(DeviceAddr, STMPE811_REG_ADC_CTRL2, 0x01);
00792   
00793   /* Select 2 nF filter capacitor */
00794   /* Configuration: 
00795      - Touch average control    : 4 samples
00796      - Touch delay time         : 500 uS
00797      - Panel driver setting time: 500 uS 
00798   */
00799   IOE_Write(DeviceAddr, STMPE811_REG_TSC_CFG, 0x9A); 
00800   
00801   /* Configure the Touch FIFO threshold: single point reading */
00802   IOE_Write(DeviceAddr, STMPE811_REG_FIFO_TH, 0x01);
00803   
00804   /* Clear the FIFO memory content. */
00805   IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
00806   
00807   /* Put the FIFO back into operation mode  */
00808   IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
00809   
00810   /* Set the range and accuracy pf the pressure measurement (Z) : 
00811      - Fractional part :7 
00812      - Whole part      :1 
00813   */
00814   IOE_Write(DeviceAddr, STMPE811_REG_TSC_FRACT_XYZ, 0x01);
00815   
00816   /* Set the driving capability (limit) of the device for TSC pins: 50mA */
00817   IOE_Write(DeviceAddr, STMPE811_REG_TSC_I_DRIVE, 0x01);
00818   
00819   /* Touch screen control configuration (enable TSC):
00820      - No window tracking index
00821      - XYZ acquisition mode
00822    */
00823   IOE_Write(DeviceAddr, STMPE811_REG_TSC_CTRL, 0x01);
00824   
00825   /*  Clear all the status pending bits if any */
00826   IOE_Write(DeviceAddr, STMPE811_REG_INT_STA, 0xFF);
00827 
00828   /* Wait for 2 ms delay */
00829   IOE_Delay(2); 
00830 }
00831 
00832 /**
00833   * @brief  Return if there is touch detected or not.
00834   * @param  DeviceAddr: Device address on communication Bus.
00835   * @retval Touch detected state.
00836   */
00837 uint8_t stmpe811_TS_DetectTouch(uint16_t DeviceAddr)
00838 {
00839   uint8_t state;
00840   uint8_t ret = 0;
00841   
00842   state = ((IOE_Read(DeviceAddr, STMPE811_REG_TSC_CTRL) & (uint8_t)STMPE811_TS_CTRL_STATUS) == (uint8_t)0x80);
00843   
00844   if(state > 0)
00845   {
00846     if(IOE_Read(DeviceAddr, STMPE811_REG_FIFO_SIZE) > 0)
00847     {
00848       ret = 1;
00849     }
00850   }
00851   else
00852   {
00853     /* Reset FIFO */
00854     IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
00855     /* Enable the FIFO again */
00856     IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
00857   }
00858   
00859   return ret;
00860 }
00861 
00862 /**
00863   * @brief  Get the touch screen X and Y positions values
00864   * @param  DeviceAddr: Device address on communication Bus.
00865   * @param  X: Pointer to X position value
00866   * @param  Y: Pointer to Y position value   
00867   * @retval None.
00868   */
00869 void stmpe811_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
00870 {
00871   uint8_t  dataXYZ[4];
00872   uint32_t uldataXYZ;
00873   
00874   IOE_ReadMultiple(DeviceAddr, STMPE811_REG_TSC_DATA_NON_INC, dataXYZ, sizeof(dataXYZ)) ;
00875   
00876   /* Calculate positions values */
00877   uldataXYZ = (dataXYZ[0] << 24)|(dataXYZ[1] << 16)|(dataXYZ[2] << 8)|(dataXYZ[3] << 0);     
00878   *X = (uldataXYZ >> 20) & 0x00000FFF;     
00879   *Y = (uldataXYZ >>  8) & 0x00000FFF;     
00880   
00881   /* Reset FIFO */
00882   IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
00883   /* Enable the FIFO again */
00884   IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
00885 }
00886 
00887 /**
00888   * @brief  Configure the selected source to generate a global interrupt or not
00889   * @param  DeviceAddr: Device address on communication Bus.  
00890   * @retval None
00891   */
00892 void stmpe811_TS_EnableIT(uint16_t DeviceAddr)
00893 {
00894   IOE_ITConfig();
00895   
00896   /* Enable global TS IT source */
00897   stmpe811_EnableITSource(DeviceAddr, STMPE811_TS_IT); 
00898   
00899   /* Enable global interrupt */
00900   stmpe811_EnableGlobalIT(DeviceAddr);
00901 }
00902 
00903 /**
00904   * @brief  Configure the selected source to generate a global interrupt or not
00905   * @param  DeviceAddr: Device address on communication Bus.    
00906   * @retval None
00907   */
00908 void stmpe811_TS_DisableIT(uint16_t DeviceAddr)
00909 {
00910   /* Disable global interrupt */
00911   stmpe811_DisableGlobalIT(DeviceAddr);
00912   
00913   /* Disable global TS IT source */
00914   stmpe811_DisableITSource(DeviceAddr, STMPE811_TS_IT); 
00915 }
00916 
00917 /**
00918   * @brief  Configure the selected source to generate a global interrupt or not
00919   * @param  DeviceAddr: Device address on communication Bus.    
00920   * @retval TS interrupts status
00921   */
00922 uint8_t stmpe811_TS_ITStatus(uint16_t DeviceAddr)
00923 {
00924   /* Return TS interrupts status */
00925   return(stmpe811_ReadGITStatus(DeviceAddr, STMPE811_TS_IT));
00926 }
00927 
00928 /**
00929   * @brief  Configure the selected source to generate a global interrupt or not
00930   * @param  DeviceAddr: Device address on communication Bus.  
00931   * @retval None
00932   */
00933 void stmpe811_TS_ClearIT(uint16_t DeviceAddr)
00934 {
00935   /* Clear the global TS IT source */
00936   stmpe811_ClearGlobalIT(DeviceAddr, STMPE811_TS_IT);
00937 }
00938 
00939 /**
00940   * @brief  Check if the device instance of the selected address is already registered
00941   *         and return its index  
00942   * @param  DeviceAddr: Device address on communication Bus.
00943   * @retval Index of the device instance if registered, 0xFF if not.
00944   */
00945 static uint8_t stmpe811_GetInstance(uint16_t DeviceAddr)
00946 {
00947   uint8_t idx = 0;
00948   
00949   /* Check all the registered instances */
00950   for(idx = 0; idx < STMPE811_MAX_INSTANCE ; idx ++)
00951   {
00952     if(stmpe811[idx] == DeviceAddr)
00953     {
00954       return idx; 
00955     }
00956   }
00957   
00958   return 0xFF;
00959 }
00960 
00961 /**
00962   * @}
00963   */ 
00964 
00965 /**
00966   * @}
00967   */ 
00968 
00969 /**
00970   * @}
00971   */ 
00972 
00973 /**
00974   * @}
00975   */ 
00976 
00977 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/