-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmpe1600_class.h
194 lines (172 loc) · 6.41 KB
/
stmpe1600_class.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
******************************************************************************
* @file stmpe1600_class.h
* @author AST / EST
* @version V0.0.1
* @date 14-April-2015
* @brief Header file for component stmpe1600
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __STMPE1600_CLASS
#define __STMPE1600_CLASS
/* Includes ------------------------------------------------------------------*/
#include <Wire.h>
#define STMPE1600_DEF_DEVICE_ADDRESS (uint8_t)0x42*2
#define STMPE1600_DEF_DIGIOUT_LVL 1
/** STMPE1600 registr map **/
#define ChipID_0_7 (uint8_t)0x00
#define ChipID_8_15 (uint8_t)0x01
#define VersionId (uint8_t)0x02
#define SYS_CTRL (uint8_t)0x03
#define IEGPIOR_0_7 (uint8_t)0x08
#define IEGPIOR_8_15 (uint8_t)0x09
#define ISGPIOR_0_7 (uint8_t)0x0A
#define ISGPIOR_8_15 (uint8_t)0x0B
#define GPMR_0_7 (uint8_t)0x10
#define GPMR_8_15 (uint8_t)0x11
#define GPSR_0_7 (uint8_t)0x12
#define GPSR_8_15 (uint8_t)0x13
#define GPDR_0_7 (uint8_t)0x14
#define GPDR_8_15 (uint8_t)0x15
#define GPIR_0_7 (uint8_t)0x16
#define GPIR_8_15 (uint8_t)0x17
typedef enum {
// GPIO Expander pin names
GPIO_0 = 0,
GPIO_1,
GPIO_2,
GPIO_3,
GPIO_4,
GPIO_5,
GPIO_6,
GPIO_7,
GPIO_8,
GPIO_9,
GPIO_10,
GPIO_11,
GPIO_12,
GPIO_13,
GPIO_14,
GPIO_15,
NOT_CON
} ExpGpioPinName;
typedef enum {
EXP_GPIO_INPUT = 0,
EXP_GPIO_OUTPUT,
EXP_GPIO_NOT_CONNECTED
} ExpGpioPinDirection;
/* Classes -------------------------------------------------------------------*/
/** Class representing a single stmpe1600 GPIO expander output pin
*/
class STMPE1600DigiOut {
public:
/** Constructor
* @param[in] &i2c device I2C to be used for communication
* @param[in] outpinname the desired out pin name to be created
* @param[in] DevAddr the stmpe1600 I2C device address (deft STMPE1600_DEF_DEVICE_ADDRESS)
* @param[in] lvl the default output pin level
*/
STMPE1600DigiOut(TwoWire *i2c, ExpGpioPinName outpinname, uint8_t DevAddr = STMPE1600_DEF_DEVICE_ADDRESS, bool lvl = STMPE1600_DEF_DIGIOUT_LVL): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(outpinname)
{
if (exppinname == NOT_CON) {
return;
}
pinlvl = lvl;
}
int begin()
{
bit16Tobit8_t data;
/* set the exppinname as output */
STMPE1600DigiOut_I2CRead(&data.u8bit[0], expdevaddr, GPDR_0_7, 1);
STMPE1600DigiOut_I2CRead(&data.u8bit[1], expdevaddr, GPDR_8_15, 1);
data.u16bit = data.u16bit | (1 << (uint16_t)exppinname); // set gpio as out
STMPE1600DigiOut_I2CWrite(&data.u8bit[0], expdevaddr, GPDR_0_7, 1);
STMPE1600DigiOut_I2CWrite(&data.u8bit[1], expdevaddr, GPDR_8_15, 1);
write(pinlvl);
return 0;
}
int end()
{
/* Do nothing */
return 0;
}
/**
* @brief Write on the out pin
* @param[in] lvl level to write
* @return 0 on Success
*/
void write(int lvl)
{
bit16Tobit8_t data;
if (exppinname == NOT_CON) {
return;
}
/* set the exppinname state to lvl */
STMPE1600DigiOut_I2CRead(data.u8bit, expdevaddr, GPSR_0_7, 2);
data.u16bit = data.u16bit & (uint16_t)(~(1 << (uint16_t)exppinname)); // set pin mask
if (lvl) {
data.u16bit = data.u16bit | (uint16_t)(1 << (uint16_t)exppinname);
}
STMPE1600DigiOut_I2CWrite(data.u8bit, expdevaddr, GPSR_0_7, 2);
}
private:
TwoWire *dev_i2c;
uint8_t expdevaddr;
ExpGpioPinName exppinname;
bool pinlvl;
int STMPE1600DigiOut_I2CWrite(uint8_t *pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToWrite)
{
dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)));
dev_i2c->write(RegisterAddr);
for (uint16_t i = 0 ; i < NumByteToWrite ; i++) {
dev_i2c->write(pBuffer[i]);
}
dev_i2c->endTransmission(true);
return 0;
}
int STMPE1600DigiOut_I2CRead(uint8_t *pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToRead)
{
dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)));
dev_i2c->write(RegisterAddr);
dev_i2c->endTransmission(false);
dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte) NumByteToRead);
int i = 0;
while (dev_i2c->available()) {
pBuffer[i] = dev_i2c->read();
i++;
}
return 0;
}
typedef union {
uint16_t u16bit;
uint8_t u8bit[2];
} bit16Tobit8_t;
};
#endif // __STMPE1600_CLASS