Now you can use NC as InterruptIn
Fork of X_NUCLEO_6180XA1 by
Components/STMPE1600/stmpe1600_class.h@33:1573db91352c, 2015-11-17 (annotated)
- Committer:
- licio.mapelli@st.com
- Date:
- Tue Nov 17 17:15:47 2015 +0100
- Revision:
- 33:1573db91352c
- Parent:
- 23:dfb5ccc7b780
- Child:
- 36:f6278b3e7c82
Added Switch class, added self contained Display class, added STMPE1600 component
and modified X_NUCLEO_6180XA1 class accordingly
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gallonm | 7:2dc81120c917 | 1 | /** |
gallonm | 7:2dc81120c917 | 2 | ****************************************************************************** |
gallonm | 7:2dc81120c917 | 3 | * @file vl6180x_class.h |
gallonm | 7:2dc81120c917 | 4 | * @author AST / EST |
gallonm | 7:2dc81120c917 | 5 | * @version V0.0.1 |
gallonm | 7:2dc81120c917 | 6 | * @date 14-April-2015 |
gallonm | 7:2dc81120c917 | 7 | * @brief Header file for component VL6180X |
gallonm | 7:2dc81120c917 | 8 | ****************************************************************************** |
gallonm | 7:2dc81120c917 | 9 | * @attention |
gallonm | 7:2dc81120c917 | 10 | * |
gallonm | 7:2dc81120c917 | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
gallonm | 7:2dc81120c917 | 12 | * |
gallonm | 7:2dc81120c917 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
gallonm | 7:2dc81120c917 | 14 | * are permitted provided that the following conditions are met: |
gallonm | 7:2dc81120c917 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
gallonm | 7:2dc81120c917 | 16 | * this list of conditions and the following disclaimer. |
gallonm | 7:2dc81120c917 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
gallonm | 7:2dc81120c917 | 18 | * this list of conditions and the following disclaimer in the documentation |
gallonm | 7:2dc81120c917 | 19 | * and/or other materials provided with the distribution. |
gallonm | 7:2dc81120c917 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
gallonm | 7:2dc81120c917 | 21 | * may be used to endorse or promote products derived from this software |
gallonm | 7:2dc81120c917 | 22 | * without specific prior written permission. |
gallonm | 7:2dc81120c917 | 23 | * |
gallonm | 7:2dc81120c917 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
gallonm | 7:2dc81120c917 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
gallonm | 7:2dc81120c917 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
gallonm | 7:2dc81120c917 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
gallonm | 7:2dc81120c917 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
gallonm | 7:2dc81120c917 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
gallonm | 7:2dc81120c917 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
gallonm | 7:2dc81120c917 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
gallonm | 7:2dc81120c917 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
gallonm | 7:2dc81120c917 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
gallonm | 7:2dc81120c917 | 34 | * |
gallonm | 7:2dc81120c917 | 35 | ****************************************************************************** |
gallonm | 7:2dc81120c917 | 36 | */ |
gallonm | 7:2dc81120c917 | 37 | #ifndef __STMPE1600_CLASS |
gallonm | 7:2dc81120c917 | 38 | #define __STMPE1600_CLASS |
gallonm | 7:2dc81120c917 | 39 | /* Includes ------------------------------------------------------------------*/ |
gallonm | 10:4954b09b72d8 | 40 | #include "DevI2C.h" |
gallonm | 7:2dc81120c917 | 41 | |
licio.mapelli@st.com | 8:f943a1fca15f | 42 | #define STMPE1600_DEF_DEVICE_ADDRESS (uint8_t)0x42*2 |
licio.mapelli@st.com | 8:f943a1fca15f | 43 | #define STMPE1600_DEF_DIGIOUT_LVL 1 |
gallonm | 7:2dc81120c917 | 44 | |
gallonm | 7:2dc81120c917 | 45 | /** STMPE1600 registr map **/ |
licio.mapelli@st.com | 33:1573db91352c | 46 | #define ChipID_0_7 (uint8_t)0x00 |
licio.mapelli@st.com | 33:1573db91352c | 47 | #define ChipID_8_15 (uint8_t)0x01 |
gallonm | 16:0d4776564733 | 48 | #define VersionId (uint8_t)0x02 |
licio.mapelli@st.com | 33:1573db91352c | 49 | #define SYS_CTRL (uint8_t)0x03 |
licio.mapelli@st.com | 33:1573db91352c | 50 | #define IEGPIOR_0_7 (uint8_t)0x08 |
licio.mapelli@st.com | 33:1573db91352c | 51 | #define IEGPIOR_8_15 (uint8_t)0x09 |
licio.mapelli@st.com | 33:1573db91352c | 52 | #define ISGPIOR_0_7 (uint8_t)0x0A |
licio.mapelli@st.com | 33:1573db91352c | 53 | #define ISGPIOR_8_15 (uint8_t)0x0B |
licio.mapelli@st.com | 33:1573db91352c | 54 | #define GPMR_0_7 (uint8_t)0x10 |
licio.mapelli@st.com | 33:1573db91352c | 55 | #define GPMR_8_15 (uint8_t)0x11 |
licio.mapelli@st.com | 33:1573db91352c | 56 | #define GPSR_0_7 (uint8_t)0x12 |
licio.mapelli@st.com | 33:1573db91352c | 57 | #define GPSR_8_15 (uint8_t)0x13 |
licio.mapelli@st.com | 33:1573db91352c | 58 | #define GPDR_0_7 (uint8_t)0x14 |
licio.mapelli@st.com | 33:1573db91352c | 59 | #define GPDR_8_15 (uint8_t)0x15 |
licio.mapelli@st.com | 33:1573db91352c | 60 | #define GPIR_0_7 (uint8_t)0x16 |
licio.mapelli@st.com | 33:1573db91352c | 61 | #define GPIR_8_15 (uint8_t)0x17 |
licio.mapelli@st.com | 33:1573db91352c | 62 | |
licio.mapelli@st.com | 33:1573db91352c | 63 | #define SOFT_RESET (uint8_t)0x80 |
gallonm | 7:2dc81120c917 | 64 | |
gallonm | 7:2dc81120c917 | 65 | typedef enum { |
gallonm | 7:2dc81120c917 | 66 | // GPIO Expander pin names |
gallonm | 15:454710d17358 | 67 | GPIO_0=0, |
gallonm | 7:2dc81120c917 | 68 | GPIO_1, |
gallonm | 7:2dc81120c917 | 69 | GPIO_2, |
gallonm | 7:2dc81120c917 | 70 | GPIO_3, |
gallonm | 7:2dc81120c917 | 71 | GPIO_4, |
gallonm | 7:2dc81120c917 | 72 | GPIO_5, |
gallonm | 7:2dc81120c917 | 73 | GPIO_6, |
gallonm | 7:2dc81120c917 | 74 | GPIO_7, |
gallonm | 7:2dc81120c917 | 75 | GPIO_8, |
gallonm | 7:2dc81120c917 | 76 | GPIO_9, |
gallonm | 7:2dc81120c917 | 77 | GPIO_10, |
gallonm | 7:2dc81120c917 | 78 | GPIO_11, |
gallonm | 7:2dc81120c917 | 79 | GPIO_12, |
gallonm | 7:2dc81120c917 | 80 | GPIO_13, |
gallonm | 7:2dc81120c917 | 81 | GPIO_14, |
licio.mapelli@st.com | 33:1573db91352c | 82 | GPIO_15, |
licio.mapelli@st.com | 33:1573db91352c | 83 | NOT_CON |
gallonm | 7:2dc81120c917 | 84 | } ExpGpioPinName; |
gallonm | 7:2dc81120c917 | 85 | |
gallonm | 7:2dc81120c917 | 86 | typedef enum { |
gallonm | 16:0d4776564733 | 87 | INPUT = 0, |
gallonm | 7:2dc81120c917 | 88 | OUTPUT, |
gallonm | 16:0d4776564733 | 89 | NOT_CONNECTED |
gallonm | 7:2dc81120c917 | 90 | }ExpGpioPinDirection; |
gallonm | 7:2dc81120c917 | 91 | |
gallonm | 7:2dc81120c917 | 92 | |
gallonm | 7:2dc81120c917 | 93 | class STMPE1600DigiOut { |
gallonm | 7:2dc81120c917 | 94 | |
gallonm | 7:2dc81120c917 | 95 | public: |
gallonm | 7:2dc81120c917 | 96 | 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) |
gallonm | 16:0d4776564733 | 97 | { |
gallonm | 16:0d4776564733 | 98 | uint8_t data[2]; |
licio.mapelli@st.com | 33:1573db91352c | 99 | if (exppinname == NOT_CON) return; |
gallonm | 16:0d4776564733 | 100 | /* set the exppinname as output */ |
gallonm | 16:0d4776564733 | 101 | dev_i2c.i2c_read(data, expdevaddr, GPDR_0_7, 1); |
gallonm | 16:0d4776564733 | 102 | dev_i2c.i2c_read(&data[1], expdevaddr, GPDR_8_15, 1); |
gallonm | 16:0d4776564733 | 103 | *(uint16_t*)data = *(uint16_t*)data | (1<<(uint16_t)exppinname); // set gpio as out |
gallonm | 16:0d4776564733 | 104 | dev_i2c.i2c_write(data, expdevaddr, GPDR_0_7, 1); |
gallonm | 16:0d4776564733 | 105 | dev_i2c.i2c_write(&data[1], expdevaddr, GPDR_8_15, 1); |
gallonm | 16:0d4776564733 | 106 | write(lvl); |
gallonm | 23:dfb5ccc7b780 | 107 | } |
gallonm | 7:2dc81120c917 | 108 | |
gallonm | 23:dfb5ccc7b780 | 109 | void write (int lvl) |
gallonm | 23:dfb5ccc7b780 | 110 | { |
gallonm | 23:dfb5ccc7b780 | 111 | uint8_t data[2]; |
licio.mapelli@st.com | 33:1573db91352c | 112 | if (exppinname == NOT_CON) return; |
gallonm | 23:dfb5ccc7b780 | 113 | /* set the exppinname state to lvl */ |
gallonm | 23:dfb5ccc7b780 | 114 | dev_i2c.i2c_read(data, expdevaddr, GPSR_0_7, 2); |
gallonm | 23:dfb5ccc7b780 | 115 | *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t)exppinname)); // set pin mask |
gallonm | 23:dfb5ccc7b780 | 116 | if (lvl) *(uint16_t*)data = *(uint16_t*)data | (uint16_t)(1<<(uint16_t)exppinname); |
gallonm | 23:dfb5ccc7b780 | 117 | dev_i2c.i2c_write(data, expdevaddr, GPSR_0_7, 2); |
gallonm | 23:dfb5ccc7b780 | 118 | } |
gallonm | 7:2dc81120c917 | 119 | |
gallonm | 16:0d4776564733 | 120 | STMPE1600DigiOut& operator=(int lvl) |
gallonm | 16:0d4776564733 | 121 | { |
gallonm | 16:0d4776564733 | 122 | write (lvl); |
gallonm | 16:0d4776564733 | 123 | return *this; |
gallonm | 16:0d4776564733 | 124 | } |
gallonm | 7:2dc81120c917 | 125 | |
gallonm | 7:2dc81120c917 | 126 | private: |
gallonm | 16:0d4776564733 | 127 | DevI2C &dev_i2c; |
gallonm | 16:0d4776564733 | 128 | uint8_t expdevaddr; |
gallonm | 16:0d4776564733 | 129 | ExpGpioPinName exppinname; |
gallonm | 7:2dc81120c917 | 130 | }; |
gallonm | 7:2dc81120c917 | 131 | |
gallonm | 16:0d4776564733 | 132 | class STMPE1600DigiIn |
gallonm | 16:0d4776564733 | 133 | { |
gallonm | 7:2dc81120c917 | 134 | public: |
gallonm | 23:dfb5ccc7b780 | 135 | STMPE1600DigiIn (DevI2C &i2c, ExpGpioPinName inpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(inpinname) |
gallonm | 23:dfb5ccc7b780 | 136 | { |
gallonm | 23:dfb5ccc7b780 | 137 | uint8_t data[2]; |
licio.mapelli@st.com | 33:1573db91352c | 138 | if (exppinname == NOT_CON) return; |
gallonm | 23:dfb5ccc7b780 | 139 | /* set the exppinname as input pin direction */ |
gallonm | 23:dfb5ccc7b780 | 140 | dev_i2c.i2c_read(data, expdevaddr, GPDR_0_7, 2); |
gallonm | 23:dfb5ccc7b780 | 141 | *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t)exppinname)); // set gpio as in |
gallonm | 23:dfb5ccc7b780 | 142 | dev_i2c.i2c_write(data, expdevaddr, GPDR_0_7, 2); |
gallonm | 23:dfb5ccc7b780 | 143 | } |
gallonm | 7:2dc81120c917 | 144 | |
gallonm | 23:dfb5ccc7b780 | 145 | bool read () |
gallonm | 23:dfb5ccc7b780 | 146 | { |
gallonm | 23:dfb5ccc7b780 | 147 | uint8_t data[2]; |
licio.mapelli@st.com | 33:1573db91352c | 148 | if (exppinname == NOT_CON) return 0; |
gallonm | 23:dfb5ccc7b780 | 149 | /* read the exppinname */ |
gallonm | 23:dfb5ccc7b780 | 150 | dev_i2c.i2c_read(data, expdevaddr, GPMR_0_7, 2); |
gallonm | 23:dfb5ccc7b780 | 151 | *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(1<<(uint16_t)exppinname); // mask the in gpio |
gallonm | 23:dfb5ccc7b780 | 152 | if (data[0] || data[1]) return 1; |
gallonm | 23:dfb5ccc7b780 | 153 | return 0; |
gallonm | 23:dfb5ccc7b780 | 154 | } |
gallonm | 7:2dc81120c917 | 155 | |
gallonm | 16:0d4776564733 | 156 | operator int() |
gallonm | 16:0d4776564733 | 157 | { |
gallonm | 16:0d4776564733 | 158 | return read(); |
gallonm | 16:0d4776564733 | 159 | } |
gallonm | 7:2dc81120c917 | 160 | |
gallonm | 7:2dc81120c917 | 161 | private: |
gallonm | 16:0d4776564733 | 162 | DevI2C &dev_i2c; |
gallonm | 16:0d4776564733 | 163 | uint8_t expdevaddr; |
gallonm | 16:0d4776564733 | 164 | ExpGpioPinName exppinname; |
gallonm | 7:2dc81120c917 | 165 | }; |
gallonm | 7:2dc81120c917 | 166 | |
gallonm | 7:2dc81120c917 | 167 | |
gallonm | 16:0d4776564733 | 168 | class STMPE1600 { |
gallonm | 16:0d4776564733 | 169 | |
gallonm | 16:0d4776564733 | 170 | public: |
licio.mapelli@st.com | 33:1573db91352c | 171 | STMPE1600 (DevI2C &i2c, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS ) : dev_i2c(i2c) |
gallonm | 23:dfb5ccc7b780 | 172 | { |
gallonm | 23:dfb5ccc7b780 | 173 | dev_i2c = i2c; |
gallonm | 23:dfb5ccc7b780 | 174 | expdevaddr = DevAddr; |
licio.mapelli@st.com | 33:1573db91352c | 175 | writeSYS_CTRL (SOFT_RESET); |
licio.mapelli@st.com | 33:1573db91352c | 176 | |
gallonm | 23:dfb5ccc7b780 | 177 | GPDR0_15 = (uint16_t)0; // gpio dir all IN |
licio.mapelli@st.com | 33:1573db91352c | 178 | write16bitReg (GPDR_0_7, &GPDR0_15); |
gallonm | 23:dfb5ccc7b780 | 179 | GPSR0_15 = (uint16_t)0x0ffff; // gpio status all 1 |
licio.mapelli@st.com | 33:1573db91352c | 180 | write16bitReg (GPSR_0_7, &GPSR0_15); |
gallonm | 23:dfb5ccc7b780 | 181 | } |
licio.mapelli@st.com | 33:1573db91352c | 182 | |
licio.mapelli@st.com | 33:1573db91352c | 183 | void writeSYS_CTRL (uint8_t data) // data = SOFT_RESET reset the device |
gallonm | 23:dfb5ccc7b780 | 184 | { |
licio.mapelli@st.com | 33:1573db91352c | 185 | dev_i2c.i2c_write((uint8_t*)SYS_CTRL, expdevaddr, data, 1); |
gallonm | 23:dfb5ccc7b780 | 186 | } |
gallonm | 16:0d4776564733 | 187 | |
licio.mapelli@st.com | 33:1573db91352c | 188 | bool setGPIO (ExpGpioPinName PinName) |
licio.mapelli@st.com | 33:1573db91352c | 189 | { |
licio.mapelli@st.com | 33:1573db91352c | 190 | if (PinName == NOT_CON) return true; |
licio.mapelli@st.com | 33:1573db91352c | 191 | GPSR0_15 = GPSR0_15 | ((uint16_t)0x0001<<PinName); |
licio.mapelli@st.com | 33:1573db91352c | 192 | write16bitReg (GPSR_0_7 , &GPSR0_15); |
licio.mapelli@st.com | 33:1573db91352c | 193 | return false; |
licio.mapelli@st.com | 33:1573db91352c | 194 | } |
licio.mapelli@st.com | 33:1573db91352c | 195 | |
licio.mapelli@st.com | 33:1573db91352c | 196 | bool clrGPIO (ExpGpioPinName PinName) |
licio.mapelli@st.com | 33:1573db91352c | 197 | { |
licio.mapelli@st.com | 33:1573db91352c | 198 | if (PinName == NOT_CON) return true; |
licio.mapelli@st.com | 33:1573db91352c | 199 | GPSR0_15 = GPSR0_15 & (~((uint16_t)0x0001<<PinName)); |
licio.mapelli@st.com | 33:1573db91352c | 200 | write16bitReg (GPSR_0_7 , &GPSR0_15); |
licio.mapelli@st.com | 33:1573db91352c | 201 | return false; |
licio.mapelli@st.com | 33:1573db91352c | 202 | } |
gallonm | 16:0d4776564733 | 203 | |
licio.mapelli@st.com | 33:1573db91352c | 204 | bool rdGPIO (ExpGpioPinName PinName) |
licio.mapelli@st.com | 33:1573db91352c | 205 | { |
licio.mapelli@st.com | 33:1573db91352c | 206 | uint16_t gpmr0_15; |
licio.mapelli@st.com | 33:1573db91352c | 207 | if (PinName == NOT_CON) return true; |
licio.mapelli@st.com | 33:1573db91352c | 208 | read16bitReg (GPMR_0_7, &gpmr0_15); |
licio.mapelli@st.com | 33:1573db91352c | 209 | gpmr0_15 = gpmr0_15 & ((uint16_t)0x0001<<PinName); |
licio.mapelli@st.com | 33:1573db91352c | 210 | if (gpmr0_15) return true; |
licio.mapelli@st.com | 33:1573db91352c | 211 | return false; |
licio.mapelli@st.com | 33:1573db91352c | 212 | } |
licio.mapelli@st.com | 33:1573db91352c | 213 | |
licio.mapelli@st.com | 33:1573db91352c | 214 | bool setGPIOdir (ExpGpioPinName PinName, ExpGpioPinDirection PinDir) |
licio.mapelli@st.com | 33:1573db91352c | 215 | { |
licio.mapelli@st.com | 33:1573db91352c | 216 | if (PinName == NOT_CON || PinDir == NOT_CONNECTED) return true; |
licio.mapelli@st.com | 33:1573db91352c | 217 | GPDR0_15 = GPDR0_15 & (~((uint16_t)0x0001<<PinName)); // clear the Pin |
licio.mapelli@st.com | 33:1573db91352c | 218 | GPDR0_15 = GPDR0_15 | ((uint16_t)PinDir<<PinName); |
licio.mapelli@st.com | 33:1573db91352c | 219 | write16bitReg (GPDR_0_7 , &GPDR0_15); |
licio.mapelli@st.com | 33:1573db91352c | 220 | return false; |
licio.mapelli@st.com | 33:1573db91352c | 221 | } |
licio.mapelli@st.com | 33:1573db91352c | 222 | |
licio.mapelli@st.com | 33:1573db91352c | 223 | void read16bitReg (uint8_t Reg16Addr, uint16_t *Reg16Data) |
licio.mapelli@st.com | 33:1573db91352c | 224 | { |
licio.mapelli@st.com | 33:1573db91352c | 225 | dev_i2c.i2c_read((uint8_t*)Reg16Data, expdevaddr, Reg16Addr, 2); |
licio.mapelli@st.com | 33:1573db91352c | 226 | } |
licio.mapelli@st.com | 33:1573db91352c | 227 | |
licio.mapelli@st.com | 33:1573db91352c | 228 | void write16bitReg (uint8_t Reg16Addr, uint16_t *Reg16Data) |
licio.mapelli@st.com | 33:1573db91352c | 229 | { |
licio.mapelli@st.com | 33:1573db91352c | 230 | dev_i2c.i2c_write((uint8_t*)Reg16Data, expdevaddr, Reg16Addr, 2); |
licio.mapelli@st.com | 33:1573db91352c | 231 | } |
licio.mapelli@st.com | 33:1573db91352c | 232 | |
licio.mapelli@st.com | 33:1573db91352c | 233 | private: |
gallonm | 23:dfb5ccc7b780 | 234 | DevI2C &dev_i2c; |
licio.mapelli@st.com | 33:1573db91352c | 235 | uint16_t GPDR0_15; // local copy of bit direction reg |
licio.mapelli@st.com | 33:1573db91352c | 236 | uint16_t GPSR0_15; // local copy of bit status reg |
licio.mapelli@st.com | 33:1573db91352c | 237 | uint8_t expdevaddr; // expander device i2c addr |
gallonm | 23:dfb5ccc7b780 | 238 | }; |
licio.mapelli@st.com | 33:1573db91352c | 239 | |
gallonm | 7:2dc81120c917 | 240 | #endif // __STMPE1600_CLASS |