summaryrefslogtreecommitdiff
path: root/nuttx/configs/freedom-kl25z/src/kl_tsi.c
blob: 18afb4580c27934ba6347475206a5171753f55a6 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/****************************************************************************
 * configs/freedom-kl25z/src/kl_tsi.c
 *
 *   Copyright (C) 2013 Alan Carvalho de Assis
 *   Author: Alan Carvalho de Assis <acassis@gmail.com>
 *           with adaptions from Gregory Nutt <gnutt@nuttx.org>
 *
 * Reference: https://community.freescale.com/community/
 *            the-embedded-beat/blog/2012/10/15/
 *            using-the-touch-interface-on-the-freescale-freedom-development-platform
 *
 * 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 NuttX 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 OWNER 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.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdint.h>
#include <errno.h>
#include <debug.h>

#include <nuttx/fs/fs.h>

#include "up_arch.h"
#include "kl_gpio.h"
#include "chip/kl_tsi.h"
#include "chip/kl_pinmux.h"
#include "chip/kl_sim.h"

#ifdef CONFIG_KL_TSI

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* The touchpad on the Freedom KL25Z board connects to the MCU via:
 *
 *   PTB16 TWI0_CH9
 *   PTB17 TSI0_CH10
 */

#define NSENSORS 2

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

static void    tsi_calibrate(void);
static ssize_t tsi_read(FAR struct file *filep, FAR char *buffer, size_t buflen);

/****************************************************************************
 * Private Data
 ****************************************************************************/

static uint16_t g_defcap[NSENSORS];    /* Default capacitance for each sensor */
static uint16_t g_currdelta = 0;       /* Current delta for the current button */
static uint8_t  g_channel = 0;         /* Current g_channel */

/* Channel assigned to each sensor */

static uint8_t const g_chsensor[NSENSORS] = { 9, 10 };

/* Character driver operations */

static const struct file_operations tsi_ops =
{
  0,           /* open */
  0,           /* close */
  tsi_read,    /* read */
  0,           /* write */
  0,           /* seek */
  0,           /* ioctl */
};

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: tsi_calibrate
 ****************************************************************************/

static void tsi_calibrate(void)
{
  uint32_t regval;
  int i;

  for (i = 0; i < NSENSORS; i++)
    {
      /* Clear end of scan flag */

      regval  = getreg32(KL_TSI_GENCS);
      regval |= TSI_GENCS_EOSF;
      putreg32(regval, KL_TSI_GENCS);

      /* Scan the next electrode */

      regval = TSI_DATA_TSICH(g_chsensor[i]);
      putreg32(regval, KL_TSI_DATA);

      regval |= TSI_DATA_SWTS;
      putreg32(regval, KL_TSI_DATA);

      /* Wait until the conversion is done */

      while (!(getreg32(KL_TSI_GENCS) & TSI_GENCS_EOSF));

      g_defcap[i] = getreg32(KL_TSI_DATA) & TSI_DATA_TSICNT_MASK;
      ivdbg("Sensor %d = %d\n", i+1, g_defcap[i]);
    }
}

/****************************************************************************
 * Name: tsi_read
 ****************************************************************************/

static ssize_t tsi_read(FAR struct file *filep, FAR char *buf, size_t buflen)
{
  static int deltacap = 0;
  uint32_t regval;

  if (buf == NULL || buflen < 1)
    {
      /* Well... nothing to do */

      return -EINVAL;
    }

  deltacap = 0;

  /* Clear end of scan flag */

  regval  = getreg32(KL_TSI_GENCS);
  regval |= TSI_GENCS_EOSF;
  putreg32(regval, KL_TSI_GENCS);

  /* Scan the next electrode */

  regval = TSI_DATA_TSICH(g_chsensor[g_channel]);
  putreg32(regval, KL_TSI_DATA);
  
  regval |= TSI_DATA_SWTS;
  putreg32(regval, KL_TSI_DATA);

  /* Wait until the conversion is done*/

  while (!(getreg32(KL_TSI_GENCS) & TSI_GENCS_EOSF));

  /* Compute delta using calibration reference counts */

  deltacap  = getreg32(KL_TSI_DATA) & TSI_DATA_TSICNT_MASK;
  deltacap -= g_defcap[g_channel];

  if (deltacap < 0)
    {
      deltacap = 0;
    }

  g_currdelta = (uint16_t)deltacap;

  ivdbg("Delta for g_channel %d = %d\n", g_channel, g_currdelta);

  buf[0] = g_currdelta & 0xff;
  buf[1] = (g_currdelta & 0xff00) >> 8;
  buf[2] = g_channel;

  /* Increment the channel index to sample the next sensor on the next timer
   * that read() is called.
   */

  g_channel++;
  if (g_channel >= NSENSORS)
    {
      g_channel = 0;
    }

  return 3;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: kl_tsi_initialize
 *
 * Description:
 *   Initialize the TSI hardware and interface for the sliders on board the
 *   Freedom KL25Z board.  Register a character driver at /dev/tsi that may
 *   be used to read from each sensor.
 *
 ****************************************************************************/

void kl_tsi_initialize(void)
{
  uint32_t regval;

  /* Enable access to the TSI module */

  regval = getreg32(KL_SIM_SCGC5);
  regval |= SIM_SCGC5_TSI;
  putreg32(regval, KL_SIM_SCGC5);

  /* Configure PINs used on TSI */

  kl_configgpio(PIN_TSI0_CH9);
  kl_configgpio(PIN_TSI0_CH10);

  /* Configure TSI parameter */

  regval = getreg32(KL_TSI_GENCS);
  regval |= TSI_GENCS_MODE_CAPSENSING | TSI_GENCS_REFCHRG_8UA |
            TSI_GENCS_DVOLT_1p03V | TSI_GENCS_EXTCHRG_64A |
            TSI_GENCS_PS_DIV16 | TSI_GENCS_NSCN_TIMES(12) | TSI_GENCS_STPE;
  putreg32(regval, KL_TSI_GENCS);

  /* Only after setting TSI we should enable it */

  regval  = getreg32(KL_TSI_GENCS);
  regval |= TSI_GENCS_TSIEN;
  putreg32(regval, KL_TSI_GENCS);

  /* Calibrate it before to use */

  tsi_calibrate();

  /* And finally register the TSI character driver */

  (void)register_driver("/dev/tsi", &tsi_ops, 0444, NULL);
}

#endif /* CONFIG_KL_TSI */