The VL53L1CB proximity sensor, based on ST’s FlightSense™, Time-of-Flight technology.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: VL53L1CB_noshield_1sensor_polls_auton VL53L1CB_noshield_1sensor_interrupt_auton X_NUCLEO_53L1A2
Based on VL53L1 library, this is a library for the VL53L1CB ToF chip.
STMPE1600/Stmpe1600.h@0:3ac96e360672, 2020-11-06 (annotated)
- Committer:
- charlesmn
- Date:
- Fri Nov 06 10:06:37 2020 +0000
- Revision:
- 0:3ac96e360672
Library for ST Vl53L1A1 time of flight sensor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:3ac96e360672 | 1 | /** |
charlesmn | 0:3ac96e360672 | 2 | ****************************************************************************** |
charlesmn | 0:3ac96e360672 | 3 | * @file Stmpe1600.h |
charlesmn | 0:3ac96e360672 | 4 | * @author AST / EST |
charlesmn | 0:3ac96e360672 | 5 | * @version V0.0.1 |
charlesmn | 0:3ac96e360672 | 6 | * @date 14-April-2015 |
charlesmn | 0:3ac96e360672 | 7 | * @brief Header file for component stmpe1600 |
charlesmn | 0:3ac96e360672 | 8 | ****************************************************************************** |
charlesmn | 0:3ac96e360672 | 9 | * @attention |
charlesmn | 0:3ac96e360672 | 10 | * |
charlesmn | 0:3ac96e360672 | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
charlesmn | 0:3ac96e360672 | 12 | * |
charlesmn | 0:3ac96e360672 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
charlesmn | 0:3ac96e360672 | 14 | * are permitted provided that the following conditions are met: |
charlesmn | 0:3ac96e360672 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
charlesmn | 0:3ac96e360672 | 16 | * this list of conditions and the following disclaimer. |
charlesmn | 0:3ac96e360672 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
charlesmn | 0:3ac96e360672 | 18 | * this list of conditions and the following disclaimer in the documentation |
charlesmn | 0:3ac96e360672 | 19 | * and/or other materials provided with the distribution. |
charlesmn | 0:3ac96e360672 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
charlesmn | 0:3ac96e360672 | 21 | * may be used to endorse or promote products derived from this software |
charlesmn | 0:3ac96e360672 | 22 | * without specific prior written permission. |
charlesmn | 0:3ac96e360672 | 23 | * |
charlesmn | 0:3ac96e360672 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
charlesmn | 0:3ac96e360672 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
charlesmn | 0:3ac96e360672 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
charlesmn | 0:3ac96e360672 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
charlesmn | 0:3ac96e360672 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
charlesmn | 0:3ac96e360672 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
charlesmn | 0:3ac96e360672 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
charlesmn | 0:3ac96e360672 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
charlesmn | 0:3ac96e360672 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
charlesmn | 0:3ac96e360672 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
charlesmn | 0:3ac96e360672 | 34 | * |
charlesmn | 0:3ac96e360672 | 35 | ****************************************************************************** |
charlesmn | 0:3ac96e360672 | 36 | */ |
charlesmn | 0:3ac96e360672 | 37 | #ifndef __STMPE1600_CLASS |
charlesmn | 0:3ac96e360672 | 38 | #define __STMPE1600_CLASS |
charlesmn | 0:3ac96e360672 | 39 | /* Includes ------------------------------------------------------------------*/ |
charlesmn | 0:3ac96e360672 | 40 | #include "DevI2C.h" |
charlesmn | 0:3ac96e360672 | 41 | |
charlesmn | 0:3ac96e360672 | 42 | #define STMPE1600_DEF_DEVICE_ADDRESS (uint8_t)0x42*2 |
charlesmn | 0:3ac96e360672 | 43 | #define STMPE1600_DEF_DIGIOUT_LVL 1 |
charlesmn | 0:3ac96e360672 | 44 | |
charlesmn | 0:3ac96e360672 | 45 | /** STMPE1600 registr map **/ |
charlesmn | 0:3ac96e360672 | 46 | #define CHIP_ID_0_7 (uint8_t)0x00 |
charlesmn | 0:3ac96e360672 | 47 | #define CHIP_ID_8_15 (uint8_t)0x01 |
charlesmn | 0:3ac96e360672 | 48 | #define VERSION_ID (uint8_t)0x02 |
charlesmn | 0:3ac96e360672 | 49 | #define SYS_CTRL (uint8_t)0x03 |
charlesmn | 0:3ac96e360672 | 50 | #define IEGPIOR_0_7 (uint8_t)0x08 |
charlesmn | 0:3ac96e360672 | 51 | #define IEGPIOR_8_15 (uint8_t)0x09 |
charlesmn | 0:3ac96e360672 | 52 | #define ISGPIOR_0_7 (uint8_t)0x0A |
charlesmn | 0:3ac96e360672 | 53 | #define ISGPIOR_8_15 (uint8_t)0x0B |
charlesmn | 0:3ac96e360672 | 54 | #define GPMR_0_7 (uint8_t)0x10 |
charlesmn | 0:3ac96e360672 | 55 | #define GPMR_8_15 (uint8_t)0x11 |
charlesmn | 0:3ac96e360672 | 56 | #define GPSR_0_7 (uint8_t)0x12 |
charlesmn | 0:3ac96e360672 | 57 | #define GPSR_8_15 (uint8_t)0x13 |
charlesmn | 0:3ac96e360672 | 58 | #define GPDR_0_7 (uint8_t)0x14 |
charlesmn | 0:3ac96e360672 | 59 | #define GPDR_8_15 (uint8_t)0x15 |
charlesmn | 0:3ac96e360672 | 60 | #define GPIR_0_7 (uint8_t)0x16 |
charlesmn | 0:3ac96e360672 | 61 | #define GPIR_8_15 (uint8_t)0x17 |
charlesmn | 0:3ac96e360672 | 62 | |
charlesmn | 0:3ac96e360672 | 63 | #define SOFT_RESET (uint8_t)0x80 |
charlesmn | 0:3ac96e360672 | 64 | |
charlesmn | 0:3ac96e360672 | 65 | typedef enum { |
charlesmn | 0:3ac96e360672 | 66 | // GPIO Expander pin names |
charlesmn | 0:3ac96e360672 | 67 | GPIO_0 = 0, |
charlesmn | 0:3ac96e360672 | 68 | GPIO_1, |
charlesmn | 0:3ac96e360672 | 69 | GPIO_2, |
charlesmn | 0:3ac96e360672 | 70 | GPIO_3, |
charlesmn | 0:3ac96e360672 | 71 | GPIO_4, |
charlesmn | 0:3ac96e360672 | 72 | GPIO_5, |
charlesmn | 0:3ac96e360672 | 73 | GPIO_6, |
charlesmn | 0:3ac96e360672 | 74 | GPIO_7, |
charlesmn | 0:3ac96e360672 | 75 | GPIO_8, |
charlesmn | 0:3ac96e360672 | 76 | GPIO_9, |
charlesmn | 0:3ac96e360672 | 77 | GPIO_10, |
charlesmn | 0:3ac96e360672 | 78 | GPIO_11, |
charlesmn | 0:3ac96e360672 | 79 | GPIO_12, |
charlesmn | 0:3ac96e360672 | 80 | GPIO_13, |
charlesmn | 0:3ac96e360672 | 81 | GPIO_14, |
charlesmn | 0:3ac96e360672 | 82 | GPIO_15, |
charlesmn | 0:3ac96e360672 | 83 | NOT_CON |
charlesmn | 0:3ac96e360672 | 84 | } ExpGpioPinName; |
charlesmn | 0:3ac96e360672 | 85 | |
charlesmn | 0:3ac96e360672 | 86 | typedef enum { |
charlesmn | 0:3ac96e360672 | 87 | INPUT = 0, |
charlesmn | 0:3ac96e360672 | 88 | OUTPUT, |
charlesmn | 0:3ac96e360672 | 89 | NOT_CONNECTED |
charlesmn | 0:3ac96e360672 | 90 | } ExpGpioPinDirection; |
charlesmn | 0:3ac96e360672 | 91 | |
charlesmn | 0:3ac96e360672 | 92 | /* Classes -------------------------------------------------------------------*/ |
charlesmn | 0:3ac96e360672 | 93 | /** Class representing a single stmpe1600 GPIO expander output pin |
charlesmn | 0:3ac96e360672 | 94 | */ |
charlesmn | 0:3ac96e360672 | 95 | class Stmpe1600DigiOut |
charlesmn | 0:3ac96e360672 | 96 | { |
charlesmn | 0:3ac96e360672 | 97 | |
charlesmn | 0:3ac96e360672 | 98 | public: |
charlesmn | 0:3ac96e360672 | 99 | /** Constructor |
charlesmn | 0:3ac96e360672 | 100 | * @param[in] &i2c device I2C to be used for communication |
charlesmn | 0:3ac96e360672 | 101 | * @param[in] outpinname the desired out pin name to be created |
charlesmn | 0:3ac96e360672 | 102 | * @param[in] DevAddr the stmpe1600 I2C device address (deft STMPE1600_DEF_DEVICE_ADDRESS) |
charlesmn | 0:3ac96e360672 | 103 | * @param[in] lvl the default ot pin level |
charlesmn | 0:3ac96e360672 | 104 | */ |
charlesmn | 0:3ac96e360672 | 105 | Stmpe1600DigiOut(DevI2C *i2c, ExpGpioPinName out_pin_name, uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS, |
charlesmn | 0:3ac96e360672 | 106 | bool lvl = STMPE1600_DEF_DIGIOUT_LVL) : _dev_i2c(i2c), exp_dev_addr(dev_addr), exp_pin_name(out_pin_name) |
charlesmn | 0:3ac96e360672 | 107 | { |
charlesmn | 0:3ac96e360672 | 108 | uint8_t data[2]; |
charlesmn | 0:3ac96e360672 | 109 | |
charlesmn | 0:3ac96e360672 | 110 | if (exp_pin_name == NOT_CON) { |
charlesmn | 0:3ac96e360672 | 111 | return; |
charlesmn | 0:3ac96e360672 | 112 | } |
charlesmn | 0:3ac96e360672 | 113 | /* set the exp_pin_name as output */ |
charlesmn | 0:3ac96e360672 | 114 | _dev_i2c->i2c_read(data, exp_dev_addr, GPDR_0_7, 1); |
charlesmn | 0:3ac96e360672 | 115 | _dev_i2c->i2c_read(&data[1], exp_dev_addr, GPDR_8_15, 1); |
charlesmn | 0:3ac96e360672 | 116 | * (uint16_t *) data = * (uint16_t *) data | (1 << (uint16_t) exp_pin_name); // set gpio as out |
charlesmn | 0:3ac96e360672 | 117 | _dev_i2c->i2c_write(data, exp_dev_addr, GPDR_0_7, 1); |
charlesmn | 0:3ac96e360672 | 118 | _dev_i2c->i2c_write(&data[1], exp_dev_addr, GPDR_8_15, 1); |
charlesmn | 0:3ac96e360672 | 119 | write(lvl); |
charlesmn | 0:3ac96e360672 | 120 | } |
charlesmn | 0:3ac96e360672 | 121 | |
charlesmn | 0:3ac96e360672 | 122 | /** |
charlesmn | 0:3ac96e360672 | 123 | * @brief Write on the out pin |
charlesmn | 0:3ac96e360672 | 124 | * @param[in] lvl level to write |
charlesmn | 0:3ac96e360672 | 125 | * @return 0 on Success |
charlesmn | 0:3ac96e360672 | 126 | */ |
charlesmn | 0:3ac96e360672 | 127 | void write(int lvl) |
charlesmn | 0:3ac96e360672 | 128 | { |
charlesmn | 0:3ac96e360672 | 129 | uint8_t data[2]; |
charlesmn | 0:3ac96e360672 | 130 | |
charlesmn | 0:3ac96e360672 | 131 | if (exp_pin_name == NOT_CON) { |
charlesmn | 0:3ac96e360672 | 132 | return; |
charlesmn | 0:3ac96e360672 | 133 | } |
charlesmn | 0:3ac96e360672 | 134 | /* set the exp_pin_name state to lvl */ |
charlesmn | 0:3ac96e360672 | 135 | _dev_i2c->i2c_read(data, exp_dev_addr, GPSR_0_7, 2); |
charlesmn | 0:3ac96e360672 | 136 | * (uint16_t *) data = * (uint16_t *) data & (uint16_t)(~(1 << (uint16_t) exp_pin_name)); // set pin mask |
charlesmn | 0:3ac96e360672 | 137 | if (lvl) { |
charlesmn | 0:3ac96e360672 | 138 | * (uint16_t *) data = * (uint16_t *) data | (uint16_t)(1 << (uint16_t) exp_pin_name); |
charlesmn | 0:3ac96e360672 | 139 | } |
charlesmn | 0:3ac96e360672 | 140 | _dev_i2c->i2c_write(data, exp_dev_addr, GPSR_0_7, 2); |
charlesmn | 0:3ac96e360672 | 141 | } |
charlesmn | 0:3ac96e360672 | 142 | |
charlesmn | 0:3ac96e360672 | 143 | /** |
charlesmn | 0:3ac96e360672 | 144 | * @brief Overload assignement operator |
charlesmn | 0:3ac96e360672 | 145 | */ |
charlesmn | 0:3ac96e360672 | 146 | Stmpe1600DigiOut &operator= (int lvl) |
charlesmn | 0:3ac96e360672 | 147 | { |
charlesmn | 0:3ac96e360672 | 148 | write(lvl); |
charlesmn | 0:3ac96e360672 | 149 | return *this; |
charlesmn | 0:3ac96e360672 | 150 | } |
charlesmn | 0:3ac96e360672 | 151 | |
charlesmn | 0:3ac96e360672 | 152 | private: |
charlesmn | 0:3ac96e360672 | 153 | DevI2C *_dev_i2c; |
charlesmn | 0:3ac96e360672 | 154 | uint8_t exp_dev_addr; |
charlesmn | 0:3ac96e360672 | 155 | ExpGpioPinName exp_pin_name; |
charlesmn | 0:3ac96e360672 | 156 | }; |
charlesmn | 0:3ac96e360672 | 157 | |
charlesmn | 0:3ac96e360672 | 158 | /* Classes -------------------------------------------------------------------*/ |
charlesmn | 0:3ac96e360672 | 159 | /** Class representing a single stmpe1600 GPIO expander input pin |
charlesmn | 0:3ac96e360672 | 160 | */ |
charlesmn | 0:3ac96e360672 | 161 | class Stmpe1600DigiIn |
charlesmn | 0:3ac96e360672 | 162 | { |
charlesmn | 0:3ac96e360672 | 163 | public: |
charlesmn | 0:3ac96e360672 | 164 | /** Constructor |
charlesmn | 0:3ac96e360672 | 165 | * @param[in] &i2c device I2C to be used for communication |
charlesmn | 0:3ac96e360672 | 166 | * @param[in] inpinname the desired input pin name to be created |
charlesmn | 0:3ac96e360672 | 167 | * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS) |
charlesmn | 0:3ac96e360672 | 168 | */ |
charlesmn | 0:3ac96e360672 | 169 | Stmpe1600DigiIn(DevI2C *i2c, ExpGpioPinName in_pin_name, |
charlesmn | 0:3ac96e360672 | 170 | uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS) : _dev_i2c(i2c), exp_dev_addr(dev_addr), |
charlesmn | 0:3ac96e360672 | 171 | exp_pin_name(in_pin_name) |
charlesmn | 0:3ac96e360672 | 172 | { |
charlesmn | 0:3ac96e360672 | 173 | uint8_t data[2]; |
charlesmn | 0:3ac96e360672 | 174 | |
charlesmn | 0:3ac96e360672 | 175 | if (exp_pin_name == NOT_CON) { |
charlesmn | 0:3ac96e360672 | 176 | return; |
charlesmn | 0:3ac96e360672 | 177 | } |
charlesmn | 0:3ac96e360672 | 178 | /* set the exp_pin_name as input pin direction */ |
charlesmn | 0:3ac96e360672 | 179 | _dev_i2c->i2c_read(data, exp_dev_addr, GPDR_0_7, 2); |
charlesmn | 0:3ac96e360672 | 180 | * (uint16_t *) data = * (uint16_t *) data & (uint16_t)(~(1 << (uint16_t) exp_pin_name)); // set gpio as in |
charlesmn | 0:3ac96e360672 | 181 | _dev_i2c->i2c_write(data, exp_dev_addr, GPDR_0_7, 2); |
charlesmn | 0:3ac96e360672 | 182 | } |
charlesmn | 0:3ac96e360672 | 183 | |
charlesmn | 0:3ac96e360672 | 184 | /** |
charlesmn | 0:3ac96e360672 | 185 | * @brief Read the input pin |
charlesmn | 0:3ac96e360672 | 186 | * @return The pin logical state 0 or 1 |
charlesmn | 0:3ac96e360672 | 187 | */ |
charlesmn | 0:3ac96e360672 | 188 | bool read() |
charlesmn | 0:3ac96e360672 | 189 | { |
charlesmn | 0:3ac96e360672 | 190 | uint8_t data[2]; |
charlesmn | 0:3ac96e360672 | 191 | |
charlesmn | 0:3ac96e360672 | 192 | if (exp_pin_name == NOT_CON) { |
charlesmn | 0:3ac96e360672 | 193 | return false; |
charlesmn | 0:3ac96e360672 | 194 | } |
charlesmn | 0:3ac96e360672 | 195 | /* read the exp_pin_name */ |
charlesmn | 0:3ac96e360672 | 196 | _dev_i2c->i2c_read(data, exp_dev_addr, GPMR_0_7, 2); |
charlesmn | 0:3ac96e360672 | 197 | * (uint16_t *) data = * (uint16_t *) data & (uint16_t)(1 << (uint16_t) exp_pin_name); // mask the in gpio |
charlesmn | 0:3ac96e360672 | 198 | if (data[0] || data[1]) { |
charlesmn | 0:3ac96e360672 | 199 | return true; |
charlesmn | 0:3ac96e360672 | 200 | } |
charlesmn | 0:3ac96e360672 | 201 | return false; |
charlesmn | 0:3ac96e360672 | 202 | } |
charlesmn | 0:3ac96e360672 | 203 | |
charlesmn | 0:3ac96e360672 | 204 | operator int() |
charlesmn | 0:3ac96e360672 | 205 | { |
charlesmn | 0:3ac96e360672 | 206 | return read(); |
charlesmn | 0:3ac96e360672 | 207 | } |
charlesmn | 0:3ac96e360672 | 208 | |
charlesmn | 0:3ac96e360672 | 209 | private: |
charlesmn | 0:3ac96e360672 | 210 | DevI2C *_dev_i2c; |
charlesmn | 0:3ac96e360672 | 211 | uint8_t exp_dev_addr; |
charlesmn | 0:3ac96e360672 | 212 | ExpGpioPinName exp_pin_name; |
charlesmn | 0:3ac96e360672 | 213 | }; |
charlesmn | 0:3ac96e360672 | 214 | |
charlesmn | 0:3ac96e360672 | 215 | /* Classes -------------------------------------------------------------------*/ |
charlesmn | 0:3ac96e360672 | 216 | /** Class representing a whole stmpe1600 component (16 gpio) |
charlesmn | 0:3ac96e360672 | 217 | */ |
charlesmn | 0:3ac96e360672 | 218 | class Stmpe1600 |
charlesmn | 0:3ac96e360672 | 219 | { |
charlesmn | 0:3ac96e360672 | 220 | |
charlesmn | 0:3ac96e360672 | 221 | public: |
charlesmn | 0:3ac96e360672 | 222 | /** Constructor |
charlesmn | 0:3ac96e360672 | 223 | * @param[in] &i2c device I2C to be used for communication |
charlesmn | 0:3ac96e360672 | 224 | * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS) |
charlesmn | 0:3ac96e360672 | 225 | */ |
charlesmn | 0:3ac96e360672 | 226 | Stmpe1600(DevI2C *i2c, uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS) : _dev_i2c(i2c) |
charlesmn | 0:3ac96e360672 | 227 | { |
charlesmn | 0:3ac96e360672 | 228 | exp_dev_addr = dev_addr; |
charlesmn | 0:3ac96e360672 | 229 | write_sys_ctrl(SOFT_RESET); |
charlesmn | 0:3ac96e360672 | 230 | |
charlesmn | 0:3ac96e360672 | 231 | gpdr0_15 = (uint16_t) 0; // gpio dir all IN |
charlesmn | 0:3ac96e360672 | 232 | write_16bit_reg(GPDR_0_7, &gpdr0_15); |
charlesmn | 0:3ac96e360672 | 233 | gpsr0_15 = (uint16_t) 0x0ffff; // gpio status all 1 |
charlesmn | 0:3ac96e360672 | 234 | write_16bit_reg(GPSR_0_7, &gpsr0_15); |
charlesmn | 0:3ac96e360672 | 235 | } |
charlesmn | 0:3ac96e360672 | 236 | |
charlesmn | 0:3ac96e360672 | 237 | /** |
charlesmn | 0:3ac96e360672 | 238 | * @brief Write the SYS_CTRL register |
charlesmn | 0:3ac96e360672 | 239 | * @param[in] Data to be written (bit fields) |
charlesmn | 0:3ac96e360672 | 240 | */ |
charlesmn | 0:3ac96e360672 | 241 | void write_sys_ctrl(uint8_t data) // data = SOFT_RESET reset the device |
charlesmn | 0:3ac96e360672 | 242 | { |
charlesmn | 0:3ac96e360672 | 243 | _dev_i2c->i2c_write(&data, exp_dev_addr, SYS_CTRL, 1); |
charlesmn | 0:3ac96e360672 | 244 | } |
charlesmn | 0:3ac96e360672 | 245 | |
charlesmn | 0:3ac96e360672 | 246 | /** |
charlesmn | 0:3ac96e360672 | 247 | * @brief Set the out pin |
charlesmn | 0:3ac96e360672 | 248 | * @param[in] The pin name |
charlesmn | 0:3ac96e360672 | 249 | * @return 0 on Success |
charlesmn | 0:3ac96e360672 | 250 | */ |
charlesmn | 0:3ac96e360672 | 251 | bool set_gpio(ExpGpioPinName pin_name) |
charlesmn | 0:3ac96e360672 | 252 | { |
charlesmn | 0:3ac96e360672 | 253 | if (pin_name == NOT_CON) { |
charlesmn | 0:3ac96e360672 | 254 | return true; |
charlesmn | 0:3ac96e360672 | 255 | } |
charlesmn | 0:3ac96e360672 | 256 | gpsr0_15 = gpsr0_15 | ((uint16_t) 0x0001 << pin_name); |
charlesmn | 0:3ac96e360672 | 257 | write_16bit_reg(GPSR_0_7, &gpsr0_15); |
charlesmn | 0:3ac96e360672 | 258 | return false; |
charlesmn | 0:3ac96e360672 | 259 | } |
charlesmn | 0:3ac96e360672 | 260 | |
charlesmn | 0:3ac96e360672 | 261 | /** |
charlesmn | 0:3ac96e360672 | 262 | * @brief Clear the out pin |
charlesmn | 0:3ac96e360672 | 263 | * @param[in] The pin name |
charlesmn | 0:3ac96e360672 | 264 | * @return 0 on Success |
charlesmn | 0:3ac96e360672 | 265 | */ |
charlesmn | 0:3ac96e360672 | 266 | bool clear_gpio(ExpGpioPinName pin_name) |
charlesmn | 0:3ac96e360672 | 267 | { |
charlesmn | 0:3ac96e360672 | 268 | if (pin_name == NOT_CON) { |
charlesmn | 0:3ac96e360672 | 269 | return true; |
charlesmn | 0:3ac96e360672 | 270 | } |
charlesmn | 0:3ac96e360672 | 271 | gpsr0_15 = gpsr0_15 & (~((uint16_t) 0x0001 << pin_name)); |
charlesmn | 0:3ac96e360672 | 272 | write_16bit_reg(GPSR_0_7, &gpsr0_15); |
charlesmn | 0:3ac96e360672 | 273 | return false; |
charlesmn | 0:3ac96e360672 | 274 | } |
charlesmn | 0:3ac96e360672 | 275 | |
charlesmn | 0:3ac96e360672 | 276 | /** |
charlesmn | 0:3ac96e360672 | 277 | * @brief Read the input pin |
charlesmn | 0:3ac96e360672 | 278 | * @param[in] The pin name |
charlesmn | 0:3ac96e360672 | 279 | * @return The logical pin level |
charlesmn | 0:3ac96e360672 | 280 | */ |
charlesmn | 0:3ac96e360672 | 281 | bool read_gpio(ExpGpioPinName pin_name) |
charlesmn | 0:3ac96e360672 | 282 | { |
charlesmn | 0:3ac96e360672 | 283 | uint16_t gpmr0_15; |
charlesmn | 0:3ac96e360672 | 284 | if (pin_name == NOT_CON) { |
charlesmn | 0:3ac96e360672 | 285 | return true; |
charlesmn | 0:3ac96e360672 | 286 | } |
charlesmn | 0:3ac96e360672 | 287 | read_16bit_reg(GPMR_0_7, &gpmr0_15); |
charlesmn | 0:3ac96e360672 | 288 | gpmr0_15 = gpmr0_15 & ((uint16_t) 0x0001 << pin_name); |
charlesmn | 0:3ac96e360672 | 289 | if (gpmr0_15) { |
charlesmn | 0:3ac96e360672 | 290 | return true; |
charlesmn | 0:3ac96e360672 | 291 | } |
charlesmn | 0:3ac96e360672 | 292 | return false; |
charlesmn | 0:3ac96e360672 | 293 | } |
charlesmn | 0:3ac96e360672 | 294 | |
charlesmn | 0:3ac96e360672 | 295 | /** |
charlesmn | 0:3ac96e360672 | 296 | * @brief Set the pin direction |
charlesmn | 0:3ac96e360672 | 297 | * @param[in] The pin name |
charlesmn | 0:3ac96e360672 | 298 | * @param[in] The pin direction |
charlesmn | 0:3ac96e360672 | 299 | * @return 0 on success |
charlesmn | 0:3ac96e360672 | 300 | */ |
charlesmn | 0:3ac96e360672 | 301 | bool set_gpio_dir(ExpGpioPinName pin_name, ExpGpioPinDirection pin_dir) |
charlesmn | 0:3ac96e360672 | 302 | { |
charlesmn | 0:3ac96e360672 | 303 | if (pin_name == NOT_CON || pin_dir == NOT_CONNECTED) { |
charlesmn | 0:3ac96e360672 | 304 | return true; |
charlesmn | 0:3ac96e360672 | 305 | } |
charlesmn | 0:3ac96e360672 | 306 | gpdr0_15 = gpdr0_15 & (~((uint16_t) 0x0001 << pin_name)); // clear the Pin |
charlesmn | 0:3ac96e360672 | 307 | gpdr0_15 = gpdr0_15 | ((uint16_t) pin_dir << pin_name); |
charlesmn | 0:3ac96e360672 | 308 | write_16bit_reg(GPDR_0_7, &gpdr0_15); |
charlesmn | 0:3ac96e360672 | 309 | return false; |
charlesmn | 0:3ac96e360672 | 310 | } |
charlesmn | 0:3ac96e360672 | 311 | |
charlesmn | 0:3ac96e360672 | 312 | /** |
charlesmn | 0:3ac96e360672 | 313 | * @brief Read a 16 bits register |
charlesmn | 0:3ac96e360672 | 314 | * @param[in] The register address |
charlesmn | 0:3ac96e360672 | 315 | * @param[in] The pointer to the read data |
charlesmn | 0:3ac96e360672 | 316 | */ |
charlesmn | 0:3ac96e360672 | 317 | void read_16bit_reg(uint8_t reg16_addr, uint16_t *reg16_data) |
charlesmn | 0:3ac96e360672 | 318 | { |
charlesmn | 0:3ac96e360672 | 319 | _dev_i2c->i2c_read((uint8_t *) reg16_data, exp_dev_addr, reg16_addr, 2); |
charlesmn | 0:3ac96e360672 | 320 | } |
charlesmn | 0:3ac96e360672 | 321 | |
charlesmn | 0:3ac96e360672 | 322 | /** |
charlesmn | 0:3ac96e360672 | 323 | * @brief Write a 16 bits register |
charlesmn | 0:3ac96e360672 | 324 | * @param[in] The register address |
charlesmn | 0:3ac96e360672 | 325 | * @param[in] The pointer to the data to be written |
charlesmn | 0:3ac96e360672 | 326 | */ |
charlesmn | 0:3ac96e360672 | 327 | void write_16bit_reg(uint8_t reg16_addr, uint16_t *reg16_data) |
charlesmn | 0:3ac96e360672 | 328 | { |
charlesmn | 0:3ac96e360672 | 329 | _dev_i2c->i2c_write((uint8_t *) reg16_data, exp_dev_addr, reg16_addr, 2); |
charlesmn | 0:3ac96e360672 | 330 | } |
charlesmn | 0:3ac96e360672 | 331 | |
charlesmn | 0:3ac96e360672 | 332 | private: |
charlesmn | 0:3ac96e360672 | 333 | DevI2C *_dev_i2c; |
charlesmn | 0:3ac96e360672 | 334 | uint16_t gpdr0_15; // local copy of bit direction reg |
charlesmn | 0:3ac96e360672 | 335 | uint16_t gpsr0_15; // local copy of bit status reg |
charlesmn | 0:3ac96e360672 | 336 | uint8_t exp_dev_addr; // expander device i2c addr |
charlesmn | 0:3ac96e360672 | 337 | }; |
charlesmn | 0:3ac96e360672 | 338 | |
charlesmn | 0:3ac96e360672 | 339 | #endif // __STMPE1600_CLASS |