Library for use with VL53L0X, cut 1.1, based on mass-market API v1.1.

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Dependents:   ConcorsoFinal HelloWorld_IHM01A1 m3pi_BT m3pi_LIDAR ... more

Fork of X_NUCLEO_53L0A1 by ST Expansion SW Team

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 CHIP_ID_0_7       (uint8_t)0x00
00047 #define CHIP_ID_8_15      (uint8_t)0x01
00048 #define VERSION_ID        (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 
00098 public:
00099     /** Constructor
00100      * @param[in] &i2c device I2C to be used for communication
00101      * @param[in] outpinname the desired out pin name to be created
00102      * @param[in] DevAddr the stmpe1600 I2C device address (deft STMPE1600_DEF_DEVICE_ADDRESS)
00103      * @param[in] lvl the default ot pin level
00104      */
00105     Stmpe1600DigiOut(DevI2C &i2c, ExpGpioPinName out_pin_name, uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS,
00106                      bool lvl = STMPE1600_DEF_DIGIOUT_LVL) : dev_i2c(&i2c), exp_dev_addr(dev_addr), exp_pin_name(out_pin_name)
00107     {
00108         uint8_t data[2];
00109 
00110         if (exp_pin_name == NOT_CON)
00111             return;
00112         /* set the exp_pin_name as output */
00113         dev_i2c->i2c_read(data, exp_dev_addr, GPDR_0_7, 1);
00114         dev_i2c->i2c_read(&data[1], exp_dev_addr, GPDR_8_15, 1);
00115         *(uint16_t *)data = *(uint16_t *)data | (1 << (uint16_t)exp_pin_name);         // set gpio as out
00116         dev_i2c->i2c_write(data, exp_dev_addr, GPDR_0_7, 1);
00117         dev_i2c->i2c_write(&data[1], exp_dev_addr, GPDR_8_15, 1);
00118         write(lvl);
00119     }
00120 
00121     /**
00122      * @brief       Write on the out pin
00123      * @param[in]   lvl level to write
00124      * @return      0 on Success
00125      */
00126     void write(int lvl)
00127     {
00128         uint8_t data[2];
00129 
00130         if (exp_pin_name == NOT_CON)
00131             return;
00132         /* set the exp_pin_name state to lvl */
00133         dev_i2c->i2c_read(data, exp_dev_addr, GPSR_0_7, 2);
00134         *(uint16_t *)data = *(uint16_t *)data & (uint16_t)(~(1 << (uint16_t)exp_pin_name));               // set pin mask
00135         if (lvl)
00136             *(uint16_t *)data = *(uint16_t *)data | (uint16_t)(1 << (uint16_t)exp_pin_name);
00137         dev_i2c->i2c_write(data, exp_dev_addr, 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 exp_dev_addr;
00152     ExpGpioPinName exp_pin_name;
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, ExpGpioPinName in_pin_name,
00167                     uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS) : dev_i2c(&i2c), exp_dev_addr(dev_addr),
00168         exp_pin_name(in_pin_name)
00169     {
00170         uint8_t data[2];
00171 
00172         if (exp_pin_name == NOT_CON)
00173             return;
00174         /* set the exp_pin_name as input pin direction */
00175         dev_i2c->i2c_read(data, exp_dev_addr, GPDR_0_7, 2);
00176         *(uint16_t *)data = *(uint16_t *)data & (uint16_t)(~(1 << (uint16_t)exp_pin_name));               // set gpio as in
00177         dev_i2c->i2c_write(data, exp_dev_addr, GPDR_0_7, 2);
00178     }
00179 
00180     /**
00181      * @brief       Read the input pin
00182      * @return      The pin logical state 0 or 1
00183      */
00184     bool read()
00185     {
00186         uint8_t data[2];
00187 
00188         if (exp_pin_name == NOT_CON)
00189             return false;
00190         /* read the exp_pin_name */
00191         dev_i2c->i2c_read(data, exp_dev_addr, GPMR_0_7, 2);
00192         *(uint16_t *)data = *(uint16_t *)data & (uint16_t)(1 << (uint16_t)exp_pin_name);            // mask the in gpio
00193         if (data[0] || data[1])
00194             return true;
00195         return false;
00196     }
00197 
00198     operator int()
00199     {
00200         return read();
00201     }
00202 
00203 private:
00204     DevI2C *dev_i2c;
00205     uint8_t exp_dev_addr;
00206     ExpGpioPinName exp_pin_name;
00207 };
00208 
00209 /* Classes -------------------------------------------------------------------*/
00210 /** Class representing a whole stmpe1600 component (16 gpio)
00211  */
00212 class Stmpe1600
00213 {
00214 
00215 public:
00216     /** Constructor
00217     * @param[in] &i2c device I2C to be used for communication
00218     * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
00219     */
00220     Stmpe1600(DevI2C &i2c, uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS) : dev_i2c(&i2c)
00221     {
00222         exp_dev_addr = dev_addr;
00223         write_sys_ctrl(SOFT_RESET);
00224 
00225         gpdr0_15 = (uint16_t) 0;    // gpio dir all IN
00226         write_16bit_reg(GPDR_0_7, &gpdr0_15);
00227         gpsr0_15 = (uint16_t) 0x0ffff;   // gpio status all 1
00228         write_16bit_reg(GPSR_0_7, &gpsr0_15);
00229     }
00230 
00231     /**
00232      * @brief       Write the SYS_CTRL register
00233      * @param[in]   Data to be written (bit fields)
00234      */
00235     void write_sys_ctrl(uint8_t data)      // data = SOFT_RESET reset the device
00236     {
00237         dev_i2c->i2c_write(&data, exp_dev_addr, SYS_CTRL, 1);
00238     }
00239 
00240     /**
00241      * @brief       Set the out pin
00242      * @param[in]   The pin name
00243      * @return      0 on Success
00244      */
00245     bool set_gpio(ExpGpioPinName pin_name)
00246     {
00247         if (pin_name == NOT_CON)
00248             return true;
00249         gpsr0_15 = gpsr0_15 | ((uint16_t) 0x0001 << pin_name);
00250         write_16bit_reg(GPSR_0_7, &gpsr0_15);
00251         return false;
00252     }
00253 
00254     /**
00255      * @brief       Clear the out pin
00256      * @param[in]   The pin name
00257      * @return      0 on Success
00258      */
00259     bool clear_gpio(ExpGpioPinName pin_name)
00260     {
00261         if (pin_name == NOT_CON)
00262             return true;
00263         gpsr0_15 = gpsr0_15 & (~((uint16_t) 0x0001 << pin_name));
00264         write_16bit_reg(GPSR_0_7, &gpsr0_15);
00265         return false;
00266     }
00267 
00268     /**
00269      * @brief       Read the input pin
00270      * @param[in]   The pin name
00271      * @return      The logical pin level
00272      */
00273     bool read_gpio(ExpGpioPinName pin_name)
00274     {
00275         uint16_t gpmr0_15;
00276         if (pin_name == NOT_CON)
00277             return true;
00278         read_16bit_reg(GPMR_0_7, &gpmr0_15);
00279         gpmr0_15 = gpmr0_15 & ((uint16_t) 0x0001 << pin_name);
00280         if (gpmr0_15)
00281             return true;
00282         return false;
00283     }
00284 
00285     /**
00286      * @brief       Set the pin direction
00287      * @param[in]   The pin name
00288      * @param[in]   The pin direction
00289      * @return      0 on success
00290      */
00291     bool set_gpio_dir(ExpGpioPinName pin_name, ExpGpioPinDirection pin_dir)
00292     {
00293         if (pin_name == NOT_CON || pin_dir == NOT_CONNECTED)
00294             return true;
00295         gpdr0_15 = gpdr0_15 & (~((uint16_t) 0x0001 << pin_name));        // clear the Pin
00296         gpdr0_15 = gpdr0_15 | ((uint16_t) pin_dir << pin_name);
00297         write_16bit_reg(GPDR_0_7, &gpdr0_15);
00298         return false;
00299     }
00300 
00301     /**
00302      * @brief       Read a 16 bits register
00303      * @param[in]   The register address
00304      * @param[in]   The pointer to the read data
00305      */
00306     void read_16bit_reg(uint8_t reg16_addr, uint16_t *reg16_data)
00307     {
00308         dev_i2c->i2c_read((uint8_t *) reg16_data, exp_dev_addr, reg16_addr, 2);
00309     }
00310 
00311     /**
00312      * @brief       Write a 16 bits register
00313      * @param[in]   The register address
00314      * @param[in]   The pointer to the data to be written
00315      */
00316     void write_16bit_reg(uint8_t reg16_addr, uint16_t *reg16_data)
00317     {
00318         dev_i2c->i2c_write((uint8_t *) reg16_data, exp_dev_addr, reg16_addr, 2);
00319     }
00320 
00321 private:
00322     DevI2C *dev_i2c;
00323     uint16_t gpdr0_15;  // local copy of bit direction reg
00324     uint16_t gpsr0_15;  // local copy of bit status reg
00325     uint8_t exp_dev_addr; // expander device i2c addr
00326 };
00327 
00328 #endif // __STMPE1600_CLASS
00329 
00330