summaryrefslogtreecommitdiff
path: root/firmware/export/mpr121.h
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/export/mpr121.h')
-rw-r--r--firmware/export/mpr121.h170
1 files changed, 170 insertions, 0 deletions
diff --git a/firmware/export/mpr121.h b/firmware/export/mpr121.h
new file mode 100644
index 0000000000..96b83ecc90
--- /dev/null
+++ b/firmware/export/mpr121.h
@@ -0,0 +1,170 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2012 Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22/** Driver for the Freescale MPR121 Capacitive Proximity Sensor */
23#include "system.h"
24
25#define ELECTRODE_COUNT 12
26#define ELE_GPIO_FIRST 4
27#define ELE_GPIO_LAST 11
28
29/* gpio config (encoding: [0]=en,[1]=dir,[2]=ctl[1],[3]=ctl[0]) */
30#define ELE_GPIO_DISABLE 0
31#define ELE_GPIO_INPUT 1
32#define ELE_GPIO_INPUT_PULLDOWN 9 /* input with pull-down */
33#define ELE_GPIO_INPUT_PULLUP 13 /* input with pull-up */
34#define ELE_GPIO_OUTPUT 3
35#define ELE_GPIO_OUTPUT_OPEN 11 /* open drain low-side */
36#define ELE_GPIO_OUTPUT_OPEN_LED 15 /* open drain high-side (led driver) */
37
38/* internal use */
39#define ELE_GPIO_EN(val) ((val) & 1)
40#define ELE_GPIO_DIR(val) (((val) >> 1) & 1)
41#define ELE_GPIO_CTL0(val) (((val) >> 3) & 1)
42#define ELE_GPIO_CTL1(val) (((val) >> 1) & 1)
43
44struct mpr121_electrode_config_t
45{
46 uint8_t bv; /* baseline value */
47 uint8_t tth; /* touch threshold */
48 uint8_t rth; /* release threshold */
49 uint8_t cdc; /* charge current (optional if auto-conf) */
50 uint8_t cdt; /* charge time (optional if auto-conf) */
51 int gpio; /* gpio config */
52};
53
54struct mpr121_baseline_filter_config_t
55{
56 uint8_t mhd; /* max half delta (except for touched) */
57 uint8_t nhd; /* noise half delta */
58 uint8_t ncl; /* noise count limit */
59 uint8_t fdl; /* filter delay count limit */
60};
61
62struct mpr121_baseline_filters_config_t
63{
64 struct mpr121_baseline_filter_config_t rising;
65 struct mpr121_baseline_filter_config_t falling;
66 struct mpr121_baseline_filter_config_t touched;
67};
68
69struct mpr121_debounce_config_t
70{
71 uint8_t dt; /* debounce count for touch */
72 uint8_t dr; /* debounce count for release */
73};
74
75/* first filter iterations */
76#define FFI_6_SAMPLES 0
77#define FFI_10_SAMPLES 1
78#define FFI_18_SAMPLES 2
79#define FFI_34_SAMPLES 3
80/* charge discharge current */
81#define CDC_DISABLE 0
82#define CDC_uA(ua) (ua)
83/* charge discharge time */
84#define CDT_DISABLE 0
85#define CDT_log_us(lus) (lus) /* actual value = 2^{us-2} µs */
86/* second filter iterations */
87#define SFI_4_SAMPLES 0
88#define SFI_6_SAMPLES 1
89#define SFI_10_SAMPLES 2
90#define SFI_18_SAMPLES 3
91/* Eletrode sample interval */
92#define ESI_log_ms(lms) (lms) /* actual value = 2^{lms} ms */
93
94struct mpr121_global_config_t
95{
96 uint8_t ffi; /* first filter iterations */
97 uint8_t cdc; /* global charge discharge current */
98 uint8_t cdt; /* global charge discharge time */
99 uint8_t sfi; /* second first iterations */
100 uint8_t esi; /* electrode sample interval */
101};
102
103#define RETRY_NEVER 0
104#define RETRY_2_TIMES 1
105#define RETRY_4_TIMES 2
106#define RETRY_8_TIMES 3
107
108struct mpr121_auto_config_t
109{
110 bool en; /* auto-conf enable */
111 bool ren; /* auto-reconf enable */
112 uint8_t retry; /* retry count */
113 bool scts; /* skip charge time search */
114 uint8_t usl; /* upper-side limit */
115 uint8_t lsl; /* lower-side limit */
116 uint8_t tl; /* target level */
117 bool acfie; /* auto-conf fail interrupt en */
118 bool arfie; /* auto-reconf fail interrupt en */
119 bool oorie; /* out of range interrupt en */
120};
121
122/* electrode mode */
123#define ELE_DISABLE 0
124#define ELE_EN0_x(x) ((x) + 1)
125/* eleprox mode */
126#define ELEPROX_DISABLE 0
127#define ELEPROX_EN0_1 1
128#define ELEPROX_EN0_3 2
129#define ELEPROX_EN0_11 3
130/* calibration lock */
131#define CL_SLOW_TRACK 0
132#define CL_DISABLE 1
133#define CL_TRACK 2
134#define CL_FAST_TRACK 3
135
136struct mpr121_config_t
137{
138 struct mpr121_electrode_config_t ele[ELECTRODE_COUNT];
139 struct mpr121_electrode_config_t eleprox;
140 struct
141 {
142 struct mpr121_baseline_filters_config_t ele;
143 struct mpr121_baseline_filters_config_t eleprox;
144 }filters;
145 struct mpr121_debounce_config_t debounce;
146 struct mpr121_global_config_t global;
147 struct mpr121_auto_config_t autoconf;
148 uint8_t ele_en; /* eletroce mode */
149 uint8_t eleprox_en; /* proximity mode */
150 uint8_t cal_lock; /* calibration lock */
151};
152
153/* gpio value */
154#define ELE_GPIO_CLR 0
155#define ELE_GPIO_SET 1
156#define ELE_GPIO_TOG 2
157/* pwm value */
158#define ELE_PWM_DISABLE 0
159#define ELE_PWM_DUTY(x) (x)
160#define ELE_PWM_MIN_DUTY 1
161#define ELE_PWM_MAX_DUTY 15
162
163int mpr121_init(int dev_i2c_addr);
164int mpr121_soft_reset(void);
165int mpr121_set_config(struct mpr121_config_t *conf);
166/* gpios are only implemented for electrode>=4 */
167int mpr121_set_gpio_output(int ele, int gpio_val);
168int mpr121_set_gpio_pwm(int ele, int pwm);
169/* get electrode status (bitmap) */
170int mpr121_get_touch_status(unsigned *status);