Changes to the library related to interrupt mode.

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Dependents:   Display_53L0A1_IntSatelites Display_53L0A1_InterruptMode

Fork of X_NUCLEO_53L0A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stmpe1600_class.h Source File

stmpe1600_class.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    stmpe1600_class.h
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-April-2015
00007  * @brief   Header file for component stmpe1600
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *       without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036 */
00037 #ifndef     __STMPE1600_CLASS
00038 #define     __STMPE1600_CLASS
00039 /* Includes ------------------------------------------------------------------*/
00040 #include    "DevI2C.h"
00041 
00042 #define STMPE1600_DEF_DEVICE_ADDRESS  (uint8_t)0x42*2   
00043 #define STMPE1600_DEF_DIGIOUT_LVL      1
00044 
00045 /**  STMPE1600 registr map **/
00046 #define ChipID_0_7      (uint8_t)0x00
00047 #define ChipID_8_15     (uint8_t)0x01
00048 #define VersionId       (uint8_t)0x02
00049 #define SYS_CTRL        (uint8_t)0x03
00050 #define IEGPIOR_0_7     (uint8_t)0x08
00051 #define IEGPIOR_8_15      (uint8_t)0x09
00052 #define ISGPIOR_0_7     (uint8_t)0x0A
00053 #define ISGPIOR_8_15      (uint8_t)0x0B
00054 #define GPMR_0_7          (uint8_t)0x10
00055 #define GPMR_8_15         (uint8_t)0x11
00056 #define GPSR_0_7          (uint8_t)0x12
00057 #define GPSR_8_15         (uint8_t)0x13
00058 #define GPDR_0_7          (uint8_t)0x14
00059 #define GPDR_8_15         (uint8_t)0x15
00060 #define GPIR_0_7          (uint8_t)0x16
00061 #define GPIR_8_15         (uint8_t)0x17
00062 
00063 #define SOFT_RESET          (uint8_t)0x80
00064 
00065   typedef enum {
00066     // GPIO Expander pin names
00067     GPIO_0=0,
00068     GPIO_1,
00069     GPIO_2,
00070     GPIO_3,            
00071     GPIO_4,
00072     GPIO_5,
00073     GPIO_6,
00074     GPIO_7,            
00075     GPIO_8,
00076     GPIO_9,
00077     GPIO_10,
00078     GPIO_11,            
00079     GPIO_12,
00080     GPIO_13,
00081     GPIO_14,
00082     GPIO_15,
00083     NOT_CON
00084 } ExpGpioPinName;   
00085 
00086 typedef enum {
00087     INPUT = 0,
00088     OUTPUT,
00089     NOT_CONNECTED
00090 }ExpGpioPinDirection;
00091 
00092 /* Classes -------------------------------------------------------------------*/
00093 /** Class representing a single stmpe1600 GPIO expander output pin
00094  */
00095 class STMPE1600DigiOut {
00096     
00097  public: 
00098     /** Constructor
00099      * @param[in] &i2c device I2C to be used for communication
00100      * @param[in] outpinname the desired out pin name to be created
00101      * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
00102      * @param[in] lvl the default ot pin level  
00103      */ 
00104     STMPE1600DigiOut (DevI2C &i2c, ExpGpioPinName outpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS, bool lvl=STMPE1600_DEF_DIGIOUT_LVL): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(outpinname) 
00105     {
00106        uint8_t data[2];             
00107              if (exppinname == NOT_CON) return;
00108        /* set the exppinname as output */
00109        dev_i2c.i2c_read(data, expdevaddr, GPDR_0_7, 1);
00110        dev_i2c.i2c_read(&data[1], expdevaddr, GPDR_8_15, 1);            
00111        *(uint16_t*)data = *(uint16_t*)data | (1<<(uint16_t)exppinname);  // set gpio as out             
00112        dev_i2c.i2c_write(data, expdevaddr, GPDR_0_7, 1);
00113        dev_i2c.i2c_write(&data[1], expdevaddr, GPDR_8_15, 1);           
00114        write(lvl);
00115     }   
00116 
00117     /**
00118      * @brief       Write on the out pin
00119      * @param[in]   lvl level to write
00120      * @return      0 on Success
00121      */         
00122     void write (int lvl) 
00123     {
00124        uint8_t data[2];         
00125              if (exppinname == NOT_CON) return;         
00126        /* set the exppinname state to lvl */
00127        dev_i2c.i2c_read(data, expdevaddr, GPSR_0_7, 2);
00128        *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t)exppinname));  // set pin mask           
00129        if (lvl) *(uint16_t*)data = *(uint16_t*)data | (uint16_t)(1<<(uint16_t)exppinname);
00130        dev_i2c.i2c_write(data, expdevaddr, GPSR_0_7, 2);
00131     }
00132 
00133     /**
00134      * @brief       Overload assignement operator
00135      */                 
00136     STMPE1600DigiOut& operator=(int lvl)
00137     {
00138        write (lvl);
00139        return *this;
00140     }       
00141         
00142  private:
00143     DevI2C &dev_i2c; 
00144     uint8_t expdevaddr;
00145     ExpGpioPinName exppinname;  
00146 };
00147 
00148 /* Classes -------------------------------------------------------------------*/
00149 /** Class representing a single stmpe1600 GPIO expander input pin
00150  */
00151 class STMPE1600DigiIn 
00152 {   
00153  public: 
00154      /** Constructor
00155      * @param[in] &i2c device I2C to be used for communication
00156      * @param[in] inpinname the desired input pin name to be created
00157      * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
00158      */ 
00159     STMPE1600DigiIn (DevI2C &i2c, ExpGpioPinName inpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(inpinname) 
00160     {
00161        uint8_t data[2];
00162              if (exppinname == NOT_CON) return;         
00163        /* set the exppinname as input pin direction */
00164        dev_i2c.i2c_read(data, expdevaddr, GPDR_0_7, 2);
00165        *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t)exppinname));  // set gpio as in         
00166        dev_i2c.i2c_write(data, expdevaddr, GPDR_0_7, 2);
00167     }         
00168 
00169     /**
00170      * @brief       Read the input pin
00171      * @return      The pin logical state 0 or 1
00172      */         
00173     bool read () 
00174     {
00175        uint8_t data[2];
00176              if (exppinname == NOT_CON) return 0;                       
00177        /* read the exppinname */
00178        dev_i2c.i2c_read(data, expdevaddr, GPMR_0_7, 2);
00179        *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(1<<(uint16_t)exppinname);  // mask the in gpio
00180        if (data[0] || data[1]) return 1;
00181        return 0;
00182     }
00183         
00184     operator int() 
00185     {       
00186        return read();
00187     }       
00188         
00189  private:
00190     DevI2C &dev_i2c; 
00191     uint8_t expdevaddr;
00192     ExpGpioPinName exppinname;      
00193 };
00194 
00195 /* Classes -------------------------------------------------------------------*/
00196 /** Class representing a whole stmpe1600 component (16 gpio)
00197  */
00198 class STMPE1600 {
00199     
00200  public: 
00201     /** Constructor
00202     * @param[in] &i2c device I2C to be used for communication
00203     * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
00204     */ 
00205     STMPE1600 (DevI2C &i2c, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS ) : dev_i2c(i2c)
00206     { 
00207        dev_i2c = i2c;                               
00208        expdevaddr = DevAddr;
00209              writeSYS_CTRL (SOFT_RESET);
00210             
00211        GPDR0_15 = (uint16_t)0;  // gpio dir all IN
00212              write16bitReg (GPDR_0_7, &GPDR0_15);           
00213        GPSR0_15 = (uint16_t)0x0ffff;  // gpio status all 1
00214              write16bitReg (GPSR_0_7, &GPSR0_15);           
00215     }
00216 
00217     /**
00218      * @brief       Write the SYS_CTRL register 
00219      * @param[in]   Data to be written (bit fields)
00220      */     
00221     void writeSYS_CTRL (uint8_t data)     // data = SOFT_RESET reset the device
00222     {
00223          dev_i2c.i2c_write(&data, expdevaddr, SYS_CTRL, 1);
00224     }
00225     
00226     /**
00227      * @brief       Set the out pin
00228      * @param[in]   The pin name
00229      * @return      0 on Success
00230      */         
00231     bool setGPIO (ExpGpioPinName PinName) 
00232     {
00233          if (PinName == NOT_CON) return true;           
00234          GPSR0_15 = GPSR0_15 | ((uint16_t)0x0001<<PinName);
00235          write16bitReg (GPSR_0_7 , &GPSR0_15);
00236          return false;
00237     }
00238     
00239     /**
00240      * @brief       Clear the out pin
00241      * @param[in]   The pin name
00242      * @return      0 on Success
00243      */         
00244     bool clrGPIO (ExpGpioPinName PinName) 
00245     {
00246          if (PinName == NOT_CON) return true;           
00247          GPSR0_15 = GPSR0_15 & (~((uint16_t)0x0001<<PinName));
00248          write16bitReg (GPSR_0_7 , &GPSR0_15);
00249          return false;
00250     }       
00251     
00252     /**
00253      * @brief       Read the input pin
00254      * @param[in]   The pin name
00255      * @return      The logical pin level
00256      */                 
00257     bool rdGPIO (ExpGpioPinName PinName) 
00258     {               
00259           uint16_t gpmr0_15;
00260           if (PinName == NOT_CON) return true;
00261           read16bitReg (GPMR_0_7, &gpmr0_15);
00262           gpmr0_15 = gpmr0_15 & ((uint16_t)0x0001<<PinName);
00263           if (gpmr0_15) return true;            
00264           return false;         
00265     }
00266     
00267     /**
00268      * @brief       Set the pin direction
00269      * @param[in]   The pin name
00270      * @param[in]   The pin direction    
00271      * @return      0 on success
00272      */                 
00273     bool setGPIOdir (ExpGpioPinName PinName, ExpGpioPinDirection PinDir) 
00274     {
00275            if (PinName == NOT_CON || PinDir == NOT_CONNECTED) return true; 
00276            GPDR0_15 = GPDR0_15 & (~((uint16_t)0x0001<<PinName));  // clear the Pin
00277            GPDR0_15 = GPDR0_15 | ((uint16_t)PinDir<<PinName);
00278            write16bitReg (GPDR_0_7 , &GPDR0_15);
00279            return false;
00280     }
00281 
00282     /**
00283      * @brief       Read a 16 bits register
00284      * @param[in]   The register address
00285      * @param[in]   The pointer to the read data
00286      */                 
00287     void read16bitReg (uint8_t Reg16Addr, uint16_t *Reg16Data)
00288     {
00289         dev_i2c.i2c_read((uint8_t*)Reg16Data, expdevaddr, Reg16Addr, 2);
00290     }               
00291 
00292     /**
00293      * @brief       Write a 16 bits register
00294      * @param[in]   The register address
00295      * @param[in]   The pointer to the data to be written
00296      */                         
00297     void write16bitReg (uint8_t Reg16Addr, uint16_t *Reg16Data)
00298     {
00299         dev_i2c.i2c_write((uint8_t*)Reg16Data, expdevaddr, Reg16Addr, 2);
00300     }       
00301 
00302  private:                   
00303     DevI2C &dev_i2c;     
00304     uint16_t GPDR0_15;  // local copy of bit direction reg 
00305     uint16_t GPSR0_15;  // local copy of bit status reg 
00306     uint8_t expdevaddr; // expander device i2c addr
00307 };
00308 
00309 #endif // __STMPE1600_CLASS
00310 
00311