1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2012 Amaury Pouly
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /** Driver for the Freescale MPR121 Capacitive Proximity Sensor */
25 #define ELECTRODE_COUNT 12
26 #define ELE_GPIO_FIRST 4
27 #define ELE_GPIO_LAST 11
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) */
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)
44 struct mpr121_electrode_config_t
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 */
54 struct mpr121_baseline_filter_config_t
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 */
62 struct mpr121_baseline_filters_config_t
64 struct mpr121_baseline_filter_config_t rising
;
65 struct mpr121_baseline_filter_config_t falling
;
66 struct mpr121_baseline_filter_config_t touched
;
69 struct mpr121_debounce_config_t
71 uint8_t dt
; /* debounce count for touch */
72 uint8_t dr
; /* debounce count for release */
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 */
82 #define CDC_uA(ua) (ua)
83 /* charge discharge time */
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 */
94 struct mpr121_global_config_t
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 */
103 #define RETRY_NEVER 0
104 #define RETRY_2_TIMES 1
105 #define RETRY_4_TIMES 2
106 #define RETRY_8_TIMES 3
108 struct mpr121_auto_config_t
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 */
123 #define ELE_DISABLE 0
124 #define ELE_EN0_x(x) ((x) + 1)
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
134 #define CL_FAST_TRACK 3
136 struct mpr121_config_t
138 struct mpr121_electrode_config_t ele
[ELECTRODE_COUNT
];
139 struct mpr121_electrode_config_t eleprox
;
142 struct mpr121_baseline_filters_config_t ele
;
143 struct mpr121_baseline_filters_config_t eleprox
;
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 */
154 #define ELE_GPIO_CLR 0
155 #define ELE_GPIO_SET 1
156 #define ELE_GPIO_TOG 2
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
163 int mpr121_init(int dev_i2c_addr
);
164 int mpr121_soft_reset(void);
165 int mpr121_set_config(struct mpr121_config_t
*conf
);
166 /* gpios are only implemented for electrode>=4 */
167 int mpr121_set_gpio_output(int ele
, int gpio_val
);
168 int mpr121_set_gpio_pwm(int ele
, int pwm
);
169 /* get electrode status (bitmap) */
170 int mpr121_get_touch_status(unsigned *status
);