diff --git a/configs/freedom-kl25z/include/board.h b/configs/freedom-kl25z/include/board.h index cb42980da1b..08e9e0afe95 100644 --- a/configs/freedom-kl25z/include/board.h +++ b/configs/freedom-kl25z/include/board.h @@ -312,6 +312,20 @@ extern "C" { void kl_boardinitialize(void); +/************************************************************************************ + * 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. + * + ************************************************************************************/ + +#ifdef CONFIG_KL_TSI +void kl_tsi_initialize(void); +#endif + #undef EXTERN #if defined(__cplusplus) } diff --git a/configs/freedom-kl25z/src/Makefile b/configs/freedom-kl25z/src/Makefile index 30333c4cc05..68db441bca3 100644 --- a/configs/freedom-kl25z/src/Makefile +++ b/configs/freedom-kl25z/src/Makefile @@ -42,6 +42,10 @@ AOBJS = $(ASRCS:.S=$(OBJEXT)) CSRCS = kl_boardinitialize.c +ifeq ($(CONFIG_KL_TSI),y) +CSRCS += kl_tsi.c +endif + ifeq ($(CONFIG_HAVE_CXX),y) CSRCS += kl_cxxinitialize.c endif @@ -66,7 +70,7 @@ ifeq ($(CONFIG_WATCHDOG),y) CSRCS += kl_watchdog.c endif -ifeq ($(CONFIG_NSH_ARCHINIT),y) +ifeq ($(CONFIG_NSH_LIBRARY),y) CSRCS += kl_nsh.c endif diff --git a/configs/freedom-kl25z/src/kl_boardinitialize.c b/configs/freedom-kl25z/src/kl_boardinitialize.c index 56230223725..eff2873b120 100644 --- a/configs/freedom-kl25z/src/kl_boardinitialize.c +++ b/configs/freedom-kl25z/src/kl_boardinitialize.c @@ -100,3 +100,29 @@ void kl_boardinitialize(void) kl_ledinit(); #endif } +/**************************************************************************** + * Name: board_initialize + * + * Description: + * If CONFIG_BOARD_INITIALIZE is selected, then an additional + * initialization call will be performed in the boot-up sequence to a + * function called board_initialize(). board_initialize() will be + * called immediately after up_intitialize() is called and just before the + * initial application is started. This additional initialization phase + * may be used, for example, to initialize board-specific device drivers. + * + ****************************************************************************/ + +#ifdef CONFIG_BOARD_INITIALIZE +void board_initialize(void) +{ + /* Perform NSH initialization here instead of from the NSH. This + * alternative NSH initialization is necessary when NSH is ran in user-space + * but the initialization function must run in kernel space. + */ + +#if defined(CONFIG_NSH_LIBRARY) && !defined(CONFIG_NSH_ARCHINIT) + (void)nsh_archinitialize(); +#endif +} +#endif diff --git a/configs/freedom-kl25z/src/kl_nsh.c b/configs/freedom-kl25z/src/kl_nsh.c new file mode 100644 index 00000000000..3b918746605 --- /dev/null +++ b/configs/freedom-kl25z/src/kl_nsh.c @@ -0,0 +1,92 @@ +/**************************************************************************** + * config/stm32f4discovery/src/kl_nsh.c + * + * Copyright (C) 2013 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include + +#ifdef CONFIG_NSH_LIBRARY + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ +/* Configuration ************************************************************/ + +/* Debug ********************************************************************/ + +#ifdef CONFIG_CPP_HAVE_VARARGS +# if defined(CONFIG_DEBUG) || !defined(CONFIG_NSH_ARCHINIT) +# define message(...) lowsyslog(__VA_ARGS__) +# else +# define message(...) printf(__VA_ARGS__) +# endif +#else +# if defined(CONFIG_DEBUG) || !defined(CONFIG_NSH_ARCHINIT) +# define message lowsyslog +# else +# define message printf +# endif +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nsh_archinitialize + * + * Description: + * Perform architecture specific initialization + * + * CONFIG_NSH_ARCHINIT=y : + * Called from the NSH library + * + * CONFIG_BOARD_INITIALIZE=y, CONFIG_NSH_LIBRARY=y, && + * CONFIG_NSH_ARCHINIT=n : + * Called from board_initialize(). + * + ****************************************************************************/ + +int nsh_archinitialize(void) +{ + return OK; +} + +#endif /* CONFIG_NSH_LIBRARY */ diff --git a/configs/freedom-kl25z/src/kl_tsi.c b/configs/freedom-kl25z/src/kl_tsi.c new file mode 100644 index 00000000000..18afb4580c2 --- /dev/null +++ b/configs/freedom-kl25z/src/kl_tsi.c @@ -0,0 +1,261 @@ +/**************************************************************************** + * configs/freedom-kl25z/src/kl_tsi.c + * + * Copyright (C) 2013 Alan Carvalho de Assis + * Author: Alan Carvalho de Assis + * with adaptions from Gregory Nutt + * + * 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 + +#include +#include +#include + +#include + +#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 */