Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / AT42QT1050 / v1 / alld_AT42QT1050.h @ f0dd1ac4

History | View | Annotate | Download (11.564 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    alld_AT42QT1050.h
21
 * @brief   Touch sensor macros and structures.
22
 *
23
 * @addtogroup lld_touch
24
 * @{
25
 */
26

    
27
#ifndef AMIROLLD_AT42QT1050_H
28
#define AMIROLLD_AT42QT1050_H
29

    
30
#include <amiro-lld.h>
31

    
32
/******************************************************************************/
33
/* CONSTANTS                                                                  */
34
/******************************************************************************/
35

    
36
/**
37
 * @brief   Maximum I2C frequency.
38
 */
39
#define AT42QT1050_LLD_I2C_MAXFREQUENCY         400000
40

    
41
/**
42
 * @brief   A falling edge indicats an interrupt.
43
 */
44
#define AT42QT1050_LLD_INT_EDGE                 APAL_GPIO_EDGE_FALLING
45

    
46
/**
47
 * @brief   Number of touch keys supported by AT42QT1050.
48
 */
49
#define AT42QT1050_LLD_NUM_KEYS                 5
50

    
51
/**
52
 * @brief   Maximum time (in microseconds) to acquire all key signals before the overflow bit of the detection status register is set.
53
 */
54
#define AT42QT1050_LLD_MAX_KEY_ACQUIRATION_TIME 8000
55

    
56
/**
57
 * @brief   The chip ID as can be read from the according register (constant).
58
 */
59
#define AT42QT1050_LLD_CHIPID                   0x46
60

    
61
/******************************************************************************/
62
/* SETTINGS                                                                   */
63
/******************************************************************************/
64

    
65
/******************************************************************************/
66
/* CHECKS                                                                     */
67
/******************************************************************************/
68

    
69
/******************************************************************************/
70
/* DATA STRUCTURES AND TYPES                                                  */
71
/******************************************************************************/
72

    
73
/**
74
 * @brief   The AT42QT1050Driver sruct.
75
 */
76
typedef struct {
77
  apalI2CDriver_t* i2cd;
78
  apalI2Caddr_t addr;
79
} AT42QT1050Driver;
80

    
81
/**
82
 * @brief   Possible I2C address configurations.
83
 */
84
enum {
85
  AT42QT1050_LLD_I2C_ADDRSEL_LOW  = 0x0041u,  /**< ADDR_SEL pin is pulled low. */
86
  AT42QT1050_LLD_I2C_ADDRSEL_HIGH = 0x0046u,  /**< ADDR_SEL pin is pulled high. */
87
};
88

    
89
/**
90
 * @brief   Available register addresses of the AT42QT1050.
91
 */
92
typedef enum {
93
  AT42QT1050_LLD_REG_CHIPID               = 0x00u,  /**<  read only */
94
  AT42QT1050_LLD_REG_FIRMWAREVERSION      = 0x01u,  /**<  read only */
95
  AT42QT1050_LLD_REG_DETECTIONSTATUS      = 0x02u,  /**<  read only */
96
  AT42QT1050_LLD_REG_KEYSTATUS            = 0x03u,  /**<  read only */
97
  AT42QT1050_LLD_REG_KEYSIGNAL_0          = 0x06u,  /**<  read only */
98
  AT42QT1050_LLD_REG_KEYSIGNAL_1          = 0x08u,  /**<  read only */
99
  AT42QT1050_LLD_REG_KEYSIGNAL_2          = 0x0Du,  /**<  read only */
100
  AT42QT1050_LLD_REG_KEYSIGNAL_3          = 0x0Fu,  /**<  read only */
101
  AT42QT1050_LLD_REG_KEYSIGNAL_4          = 0x11u,  /**<  read only */
102
  AT42QT1050_LLD_REG_REFERENCEDATA_0      = 0x14u,  /**<  read only */
103
  AT42QT1050_LLD_REG_REFERENCEDATA_1      = 0x16u,  /**<  read only */
104
  AT42QT1050_LLD_REG_REFERENCEDATA_2      = 0x1Au,  /**<  read only */
105
  AT42QT1050_LLD_REG_REFERENCEDATA_3      = 0x1Cu,  /**<  read only */
106
  AT42QT1050_LLD_REG_REFERENCEDATA_4      = 0x1Eu,  /**<  read only */
107
  AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_0  = 0x21u,  /**< read/write */
108
  AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_1  = 0x22u,  /**< read/write */
109
  AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_2  = 0x24u,  /**< read/write */
110
  AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_3  = 0x25u,  /**< read/write */
111
  AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_4  = 0x26u,  /**< read/write */
112
  AT42QT1050_LLD_REG_PULSE_SCALE_0        = 0x28u,  /**< read/write */
113
  AT42QT1050_LLD_REG_PULSE_SCALE_1        = 0x29u,  /**< read/write */
114
  AT42QT1050_LLD_REG_PULSE_SCALE_2        = 0x2Bu,  /**< read/write */
115
  AT42QT1050_LLD_REG_PULSE_SCALE_3        = 0x2Cu,  /**< read/write */
116
  AT42QT1050_LLD_REG_PULSE_SCALE_4        = 0x2Du,  /**< read/write */
117
  AT42QT1050_LLD_REG_INTEGRATOR_AKS_0     = 0x2Fu,  /**< read/write */
118
  AT42QT1050_LLD_REG_INTEGRATOR_AKS_1     = 0x30u,  /**< read/write */
119
  AT42QT1050_LLD_REG_INTEGRATOR_AKS_2     = 0x32u,  /**< read/write */
120
  AT42QT1050_LLD_REG_INTEGRATOR_AKS_3     = 0x33u,  /**< read/write */
121
  AT42QT1050_LLD_REG_INTEGRATOR_AKS_4     = 0x34u,  /**< read/write */
122
  AT42QT1050_LLD_REG_CHARGESHAREDELAY_0   = 0x36u,  /**< read/write */
123
  AT42QT1050_LLD_REG_CHARGESHAREDELAY_1   = 0x37u,  /**< read/write */
124
  AT42QT1050_LLD_REG_CHARGESHAREDELAY_2   = 0x39u,  /**< read/write */
125
  AT42QT1050_LLD_REG_CHARGESHAREDELAY_3   = 0x3Au,  /**< read/write */
126
  AT42QT1050_LLD_REG_CHARGESHAREDELAY_4   = 0x3Bu,  /**< read/write */
127
  AT42QT1050_LLD_REG_FINFOUTMAXCALGUARD   = 0x3Cu,  /**< read/write */
128
  AT42QT1050_LLD_REG_LOWPOWERMODE         = 0x3Du,  /**< read/write */
129
  AT42QT1050_LLD_REG_MAXONDURATION        = 0x3Eu,  /**< read/write */
130
  AT42QT1050_LLD_REG_RESET_CALIBRATE      = 0x3Fu,  /**< read/write */
131
} at42qt1050_lld_register_t;
132

    
133
/**
134
 * @brief   Firmware version register structure.
135
 */
136
typedef union {
137
  uint8_t raw;
138
  struct {
139
    uint8_t minor : 4;
140
    uint8_t major : 4;
141
  };
142
} at42qt1050_lld_firmwarereg_t;
143

    
144

    
145
/**
146
 * @brief   Relevant bits of the detection status register.
147
 */
148
typedef enum {
149
  AT42QT1050_LLD_DETECTIONSTATUS_TOUCH      = 0x01u,  /**< Set if any keys are in detect.  */
150
  AT42QT1050_LLD_DETECTIONSTATUS_OVERFLOW   = 0x40u,  /**< Set if the time to acquire all key signals exceeds 8ms. */
151
  AT42QT1050_LLD_DETECTIONSTATUS_CALIBRATE  = 0x80u,  /**< Set during calibration sequence. */
152
} at42qt1050_lld_detectionstatusreg_t;
153

    
154
/**
155
 * @brief   Key status register masks.
156
 */
157
typedef enum {
158
  AT42QT1050_LLD_KEYSTATUS_KEY0 = 0x02u,
159
  AT42QT1050_LLD_KEYSTATUS_KEY1 = 0x04u,
160
  AT42QT1050_LLD_KEYSTATUS_KEY2 = 0x10u,
161
  AT42QT1050_LLD_KEYSTATUS_KEY3 = 0x20u,
162
  AT42QT1050_LLD_KEYSTATUS_KEY4 = 0x40u,
163
} at42qt1050_lld_keystatusreg_t;
164

    
165
/**
166
 * @brief   Pulse/Scale register structure.
167
 */
168
typedef union {
169
  uint8_t raw;
170
  struct {
171
    uint8_t scale : 4;
172
    uint8_t pulse : 4;
173
  };
174
} at42qt1050_lld_pulsescalereg_t;
175

    
176
/**
177
 * @brief   Detection Integrator (DI) / AKS register structure.
178
 */
179
typedef union {
180
  uint8_t raw;
181
  struct {
182
    uint8_t aks : 2;
183
    uint8_t detection_integrator : 6;
184
  };
185
} at42qt1050_lld_detectionintegratoraksreg_t;
186

    
187
/**
188
 * @brief   Charge share delay constant sclaing factor.
189
 * @details Values in the charge share delay registers are multiplied by this factor.
190
 *          Unit is microseconds (µs).
191
 */
192
#define AT42QT1050_LLD_CHARGESHAREDELAY_FACTOR  2.5f
193

    
194
/**
195
 * @brief   FastIn / FastOutDI / Max Cal / Guard Channel register masks.
196
 */
197
typedef enum {
198
  AT42QT1050_LLD_FINFOUTMAXCALGUARD_GUARD   = 0x0Fu,
199
  AT42QT1050_LLD_FINFOUTMAXCALGUARD_MAXCAL  = 0x10u,
200
  AT42QT1050_LLD_FINFOUTMAXCALGUARD_FO      = 0x20u,
201
  AT42QT1050_LLD_FINFOUTMAXCALGUARD_FI      = 0x40u,
202
} at42qt1050_lld_finfoutmaxcalguardreg_t;
203

    
204
/**
205
 * @brief   Low power mode constant scaling factor.
206
 * @details The values in the low poer mode register is multiplied by this factor.
207
 *          Unit is microseconds (µs).
208
 * @note    Setting the power mode scaling register value to zero makes the AT42QT1050 enter deep-sleep mode.
209
 */
210
#define AT42QT1050_LLD_LOWPOWER_FACTOR          8000
211

    
212
/**
213
 * @brief   Man on duration constant scaling factor.
214
 * @details The value in the max on duration register is multiplied by this factor.
215
 *          Unit is microseconds (µs).
216
 */
217
#define AT42QT1050_LLD_MAXONDURATION_FACTOR     160000
218

    
219
/**
220
 * @brief   RESET / Calibrate register masks.
221
 */
222
typedef enum {
223
  AT42QT1050_LLD_RESETCALIBRATE_CALIBRATE = 0x7Fu,
224
  AT42QT1050_LLD_RESETCALIBRATE_RESET     = 0x80u,
225
} at42qt1050_lld_resetcalibratereg_t;
226

    
227
/******************************************************************************/
228
/* MACROS                                                                     */
229
/******************************************************************************/
230

    
231
/******************************************************************************/
232
/* EXTERN DECLARATIONS                                                        */
233
/******************************************************************************/
234

    
235
#ifdef __cplusplus
236
extern "C" {
237
#endif
238
  apalExitStatus_t at42qt1050_lld_read_reg(const AT42QT1050Driver* at42qt1050d, const at42qt1050_lld_register_t reg, uint8_t* const data, const apalTime_t timeout);
239
  apalExitStatus_t at42qt1050_lld_write_reg(const AT42QT1050Driver* at42qt1050d, const at42qt1050_lld_register_t reg, const uint8_t data, const apalTime_t timeout);
240

    
241
  apalExitStatus_t at42qt1050_lld_read_keyssignal(const AT42QT1050Driver* at42qt1050d, const uint8_t key, uint16_t* signal, const apalTime_t timeout);
242
  apalExitStatus_t at42qt1050_lld_read_referencedata(const AT42QT1050Driver* at42qt1050d, const uint8_t key, uint16_t* refdata, const apalTime_t timeout);
243

    
244
  apalExitStatus_t at42qt1050_lld_reset_safe(const AT42QT1050Driver* at42qt1050d, const bool wait4wakeup, const apalTime_t timeout);
245
  apalExitStatus_t at42qt1050_lld_reset(const AT42QT1050Driver* at42qt1050d, const apalTime_t timeout, const bool wait4wakeup);
246

    
247
  uint16_t at42qt1050_lld_pulse2samples(const uint8_t pulse);
248
  float at42qt1050_lld_samples2pulse(const uint16_t samples);
249
  uint16_t at42qt1050_lld_scale2scaling(const uint8_t scale);
250
  float at42qt1050_lld_scaling2scale(const uint16_t factor);
251

    
252

    
253
  /**
254
   * @brief   Calculates n-th address based on address of register 0.
255
   * @details Calculation: <scale value> = log2(<scaling factor>
256
   * )
257
   * @param[in]   base    Base address = frist register
258
   * @param[in]   inc     Jump to the next register inc times
259
   *
260
   * @return    Calculated register address
261
   */
262
  inline at42qt1050_lld_register_t at42qt1050_lld_addr_calc(const at42qt1050_lld_register_t base, const uint8_t inc) {
263
    apalDbgAssert(inc < 5);
264

    
265
    uint8_t double_result = 0; //16bit access
266

    
267
    switch (base) {
268
      case AT42QT1050_LLD_REG_KEYSIGNAL_0:         //2 4 2 2
269
      case AT42QT1050_LLD_REG_REFERENCEDATA_0:     //2 4 2 2
270
        double_result = 1;
271
        __attribute__((fallthrough));
272
      case AT42QT1050_LLD_REG_NEGATIVETHRESHOLD_0: //1 2 1 1
273
      case AT42QT1050_LLD_REG_PULSE_SCALE_0:       //1 2 1 1
274
      case AT42QT1050_LLD_REG_INTEGRATOR_AKS_0:    //1 2 1 1
275
      case AT42QT1050_LLD_REG_CHARGESHAREDELAY_0:  //1 2 1 1
276
      {
277
        uint8_t increase = ((inc>1)?inc+1:inc);
278
        return (at42qt1050_lld_register_t) (((uint8_t) base)+(increase << double_result));
279
      }
280
      default:
281
      {
282
        apalDbgPrintf("invalid base register 0x%04X\n", base);
283
        return (at42qt1050_lld_register_t) 0xFF; //does not exist
284
      }
285
    }
286
  }
287

    
288
#ifdef __cplusplus
289
}
290
#endif
291

    
292
/******************************************************************************/
293
/* INLINE FUNCTIONS                                                           */
294
/******************************************************************************/
295

    
296
#endif /* AMIROLLD_AT42QT1050_H */
297

    
298
/** @} */
299