Librairie adaptée au laboratoire 2

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_6180XA1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STMPE1600.h Source File

STMPE1600.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    STMPE1600.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 } exp_gpio_pin_name;   
00085 
00086 typedef enum {
00087     INPUT = 0,
00088     OUTPUT,
00089     NOT_CONNECTED
00090 } exp_gpio_pin_direction;
00091 
00092 
00093 /* Classes -------------------------------------------------------------------*/
00094 
00095 /**
00096  * Class representing a single stmpe1600 GPIO expander output pin
00097  */
00098 class STMPE1600DigiOut {
00099 public: 
00100     /** Constructor
00101      * @param[in] &i2c device I2C to be used for communication
00102      * @param[in] outpinname the desired out pin name to be created
00103      * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
00104      * @param[in] lvl the default ot pin level  
00105      */ 
00106         STMPE1600DigiOut (DevI2C &i2c, exp_gpio_pin_name outpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS, bool lvl=STMPE1600_DEF_DIGIOUT_LVL):
00107     _dev_i2c(&i2c),
00108     _expdevaddr(DevAddr),
00109     _exppinname(outpinname) 
00110     {
00111         uint8_t data[2];                
00112         if (_exppinname == NOT_CON) {
00113             return;
00114         }
00115         /* set the _exppinname as output */
00116         _dev_i2c->i2c_read(data, _expdevaddr, GPDR_0_7, 1);
00117         _dev_i2c->i2c_read(&data[1], _expdevaddr, GPDR_8_15, 1);            
00118         *(uint16_t*)data = *(uint16_t*)data | (1<<(uint16_t)_exppinname);  // set gpio as out           
00119         _dev_i2c->i2c_write(data, _expdevaddr, GPDR_0_7, 1);
00120         _dev_i2c->i2c_write(&data[1], _expdevaddr, GPDR_8_15, 1);           
00121         write (lvl);
00122     }   
00123 
00124     /**
00125      * @brief       Write on the out pin
00126      * @param[in]   lvl level to write
00127      * @return      0 on Success
00128      */         
00129     void write (int lvl) 
00130     {
00131         uint8_t data[2];            
00132               if (_exppinname == NOT_CON) return;           
00133         /* set the _exppinname state to lvl */
00134         _dev_i2c->i2c_read(data, _expdevaddr, GPSR_0_7, 2);
00135         *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t)_exppinname));  // set pin mask             
00136         if (lvl) *(uint16_t*)data = *(uint16_t*)data | (uint16_t)(1<<(uint16_t)_exppinname);
00137         _dev_i2c->i2c_write(data, _expdevaddr, GPSR_0_7, 2);
00138     }
00139 
00140     /**
00141      * @brief       Overload assignement operator
00142      */                 
00143     STMPE1600DigiOut& operator=(int lvl)
00144     {
00145         write (lvl);
00146         return *this;
00147     }       
00148         
00149 private:
00150     DevI2C *_dev_i2c;
00151     uint8_t _expdevaddr;
00152     exp_gpio_pin_name _exppinname;  
00153 };
00154 
00155 /* Classes -------------------------------------------------------------------*/
00156 /** Class representing a single stmpe1600 GPIO expander input pin
00157  */
00158 class STMPE1600DigiIn 
00159 {   
00160 public:
00161     /** Constructor
00162      * @param[in] &i2c device I2C to be used for communication
00163      * @param[in] inpinname the desired input pin name to be created
00164      * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
00165      */ 
00166     STMPE1600DigiIn (DevI2C &i2c, exp_gpio_pin_name inpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS): _dev_i2c(&i2c), _expdevaddr(DevAddr), _exppinname(inpinname) 
00167     {
00168         uint8_t data[2];
00169         if (_exppinname == NOT_CON) return;         
00170         /* set the _exppinname as input pin direction */
00171         _dev_i2c->i2c_read(data, _expdevaddr, GPDR_0_7, 2);
00172         *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t) _exppinname));  // set gpio as in          
00173         _dev_i2c->i2c_write(data, _expdevaddr, GPDR_0_7, 2);
00174     }         
00175 
00176     /**
00177      * @brief       Read the input pin
00178      * @return      The pin logical state 0 or 1
00179      */         
00180     bool read () {
00181         uint8_t data[2];
00182         if (_exppinname == NOT_CON) {
00183             return 0;                       
00184         }
00185 
00186         /* read the _exppinname */
00187         _dev_i2c->i2c_read(data, _expdevaddr, GPMR_0_7, 2);
00188         *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(1<<(uint16_t)_exppinname);  // mask the in gpio
00189 
00190         if (data[0] || data[1]) {
00191             return 1;
00192         }
00193         return 0;
00194     }
00195         
00196     operator int() {        
00197         return read();
00198     }       
00199         
00200 private:
00201     DevI2C *_dev_i2c; 
00202     uint8_t _expdevaddr;
00203     exp_gpio_pin_name _exppinname;      
00204 };
00205 
00206 
00207 /* Classes -------------------------------------------------------------------*/
00208 
00209 /**
00210  * Class representing a whole stmpe1600 component (16 gpio)
00211  */
00212 class STMPE1600 {
00213 public: 
00214     /** Constructor
00215     * @param[in] &i2c device I2C to be used for communication
00216     * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
00217     */ 
00218     STMPE1600 (DevI2C &i2c, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS ) : _dev_i2c(&i2c)
00219     { 
00220         _dev_i2c = &i2c;                                
00221         _expdevaddr = DevAddr;
00222         write_sys_ctrl (SOFT_RESET);
00223             
00224         _gpdro_15 = (uint16_t)0;    // gpio dir all IN
00225         write_16_bit_reg (GPDR_0_7, &_gpdro_15);            
00226         _gpsro_15 = (uint16_t)0x0ffff;  // gpio status all 1
00227         write_16_bit_reg (GPSR_0_7, &_gpsro_15);            
00228     }
00229 
00230     /**
00231      * @brief       Write the SYS_CTRL register 
00232      * @param[in]   Data to be written (bit fields)
00233      */     
00234     void write_sys_ctrl (uint8_t data)     // data = SOFT_RESET reset the device
00235     {
00236         _dev_i2c->i2c_write((uint8_t*)SYS_CTRL, _expdevaddr, data, 1);
00237     }
00238       
00239     /**
00240      * @brief       Set the out pin
00241      * @param[in]   The pin name
00242      * @return      0 on Success
00243      */         
00244     bool set_gpio (exp_gpio_pin_name pin_name) 
00245     {
00246           if (pin_name == NOT_CON) return true;         
00247           _gpsro_15 = _gpsro_15 | ((uint16_t)0x0001<<pin_name);
00248           write_16_bit_reg (GPSR_0_7 , &_gpsro_15);
00249           return false;
00250     }
00251     
00252     /**
00253      * @brief       Clear the out pin
00254      * @param[in]   The pin name
00255      * @return      0 on Success
00256      */         
00257     bool clr_gpio (exp_gpio_pin_name pin_name) 
00258     {
00259           if (pin_name == NOT_CON) return true;         
00260           _gpsro_15 = _gpsro_15 & (~((uint16_t)0x0001<<pin_name));
00261           write_16_bit_reg (GPSR_0_7 , &_gpsro_15);
00262           return false;
00263     }       
00264     
00265     /**
00266      * @brief       Read the input pin
00267      * @param[in]   The pin name
00268      * @return      The logical pin level
00269      */                 
00270     bool rd_gpio (exp_gpio_pin_name pin_name) 
00271     {               
00272           uint16_t gpmr0_15;
00273           if (pin_name == NOT_CON) return true;
00274           read_16_bit_reg (GPMR_0_7, &gpmr0_15);
00275           gpmr0_15 = gpmr0_15 & ((uint16_t)0x0001<<pin_name);
00276           if (gpmr0_15) {
00277             return true;
00278         }
00279           return false;         
00280     }
00281     
00282     /**
00283      * @brief       Set the pin direction
00284      * @param[in]   The pin name
00285      * @param[in]   The pin direction    
00286      * @return      0 on success
00287      */
00288     bool set_gpio_dir (exp_gpio_pin_name pin_name, exp_gpio_pin_direction pin_dir) 
00289     {
00290         if (pin_name == NOT_CON || pin_dir == NOT_CONNECTED) {
00291             return true;
00292         }
00293         _gpdro_15 = _gpdro_15 & (~((uint16_t)0x0001<<pin_name));  // clear the Pin
00294         _gpdro_15 = _gpdro_15 | ((uint16_t)pin_dir<<pin_name);
00295         write_16_bit_reg (GPDR_0_7 , &_gpdro_15);
00296         return false;
00297     }
00298 
00299     /**
00300      * @brief       Read a 16 bits register
00301      * @param[in]   The register address
00302      * @param[in]   The pointer to the read data
00303      */                 
00304     void read_16_bit_reg (uint8_t reg_16_addr, uint16_t *reg_16_data)
00305     {
00306         _dev_i2c->i2c_read((uint8_t*)reg_16_data, _expdevaddr, reg_16_addr, 2); 
00307     }               
00308 
00309     /**
00310      * @brief       Write a 16 bits register
00311      * @param[in]   The register address
00312      * @param[in]   The pointer to the data to be written
00313      */                         
00314     void write_16_bit_reg (uint8_t reg_16_addr, uint16_t *reg_16_data)
00315     {
00316         _dev_i2c->i2c_write((uint8_t*)reg_16_data, _expdevaddr, reg_16_addr, 2);    
00317     }       
00318 
00319 private:                    
00320     DevI2C *_dev_i2c;    
00321     uint16_t _gpdro_15;   // local copy of bit direction reg 
00322     uint16_t _gpsro_15;   // local copy of bit status reg 
00323     uint8_t _expdevaddr; // expander device i2c addr
00324 };
00325 
00326 #endif // __STMPE1600_CLASS