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