summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-12-03 16:41:09 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-12-03 16:41:09 -0600
commit90e6fca187a8e320220a8accc64e9512dd3d90e5 (patch)
tree848aa32d32049be0c44b0831b7b62bb5329827e6
parentb8589f390556dacd1faa0f654a905b887aabe86a (diff)
downloadnuttx-90e6fca187a8e320220a8accc64e9512dd3d90e5.tar.gz
nuttx-90e6fca187a8e320220a8accc64e9512dd3d90e5.tar.bz2
nuttx-90e6fca187a8e320220a8accc64e9512dd3d90e5.zip
Nucleo-F4x1RE: Add support for an analog joystick shield. There are still some ADC issues to be worked through as of the initial commit
-rw-r--r--nuttx/configs/nucleo-f4x1re/README.txt69
-rw-r--r--nuttx/configs/nucleo-f4x1re/src/Makefile7
-rw-r--r--nuttx/configs/nucleo-f4x1re/src/nucleo-f4x1re.h81
-rw-r--r--nuttx/configs/nucleo-f4x1re/src/stm32_adc.c162
-rw-r--r--nuttx/configs/nucleo-f4x1re/src/stm32_ajoystick.c444
-rw-r--r--nuttx/configs/sama5d3-xplained/src/sam_ajoystick.c2
6 files changed, 760 insertions, 5 deletions
diff --git a/nuttx/configs/nucleo-f4x1re/README.txt b/nuttx/configs/nucleo-f4x1re/README.txt
index fd5855bae..835bad18b 100644
--- a/nuttx/configs/nucleo-f4x1re/README.txt
+++ b/nuttx/configs/nucleo-f4x1re/README.txt
@@ -499,7 +499,9 @@ Serial Consoles
Shields
=======
- RS-232 from Cutedigi.com. Supports a single RS-232 connected via
+ RS-232 from Cutedigi.com
+ ------------------------
+ Supports a single RS-232 connected via
Nucleo CN9 STM32F4x1RE Cutedigi
----------- ------------ --------
@@ -509,6 +511,65 @@ Shields
Support for this shield is enabled by selecting USART2 and configuring
SB13, 14, 62, and 63 as described above under "Serial Consoles"
+ Itead Joystick Shield
+ ---------------------
+ See http://imall.iteadstudio.com/im120417014.html for more information
+ about this joystick.
+
+ Itead Joystick Connection:
+
+ --------- ----------------- ---------------------------------
+ ARDUINO ITEAD NUCLEO-F4x1
+ PIN NAME SIGNAL SIGNAL
+ --------- ----------------- ---------------------------------
+ D3 Button E Output PB3
+ D4 Button D Output PB5
+ D5 Button C Output PB4
+ D6 Button B Output PB10
+ D7 Button A Output PA8
+ D8 Button F Output PA9
+ D9 Button G Output PC7
+ A0 Joystick Y Output PA0 ADC_IN0
+ A1 Joystick X Output PA1 ADC_IN1
+ --------- ----------------- ---------------------------------
+
+ All buttons are pulled on the shield. A sensed low value indicates
+ when the button is pressed.
+
+ Itead Joystick Signal interpretation:
+
+ --------- ----------------------- ---------------------------
+ BUTTON TYPE NUTTX ALIAS
+ --------- ----------------------- ---------------------------
+ Button A Large button A JUMP/BUTTON 3
+ Button B Large button B FIRE/BUTTON 2
+ Button C Joystick select button SELECT/BUTTON 1
+ Button D Tiny Button D BUTTON 6
+ Button E Tiny Button E BUTTON 7
+ Button F Large Button F BUTTON 4
+ Button G Large Button G BUTTON 5
+ --------- ----------------------- ---------------------------
+
+ Itead Joystick configuration settings:
+
+ System Type -> STM32 Peripheral Support
+ CONFIG_STM32_ADC1=y : Enable ADC1 driver support
+
+ Drivers
+ CONFIG_ANALOG=y : Should be automatically selected
+ CONFIG_ADC=y : Should be automatically selected
+ CONFIG_INPUT=y : Select input device support
+ CONFIG_AJOYSTICK=y : Select analog joystick support
+
+ There is nothing in the configuration that currently uses the joystick.
+ For testing, you can add the following configuration options to enable the
+ analog joystick example at apps/examples/ajoystick:
+
+ CONFIG_NSH_ARCHINIT=y
+ CONFIG_EXAMPLES_AJOYSTICK=y
+ CONFIG_EXAMPLES_AJOYSTICK_DEVNAME="/dev/ajoy0"
+ CONFIG_EXAMPLES_AJOYSTICK_SIGNO=13
+
Configurations
==============
@@ -539,13 +600,13 @@ Configurations
3. Although the default console is USART2 (which would correspond to
the Virtual COM port) I have done all testing with the console
device configured for USART1 (see instruction above under "Serial
- Consoles). I have been using a TTL-to-RS-232 converted connected
+ Consoles). I have been using a TTL-to-RS-232 converter connected
as shown below:
Nucleo CN10 STM32F4x1RE
----------- ------------
- Pin 21 PA9 USART2_RX
- Pin 33 PA10 USART2_TX
+ Pin 21 PA9 USART1_RX
+ Pin 33 PA10 USART1_TX
Pin 20 GND
Pin 8 U5V
diff --git a/nuttx/configs/nucleo-f4x1re/src/Makefile b/nuttx/configs/nucleo-f4x1re/src/Makefile
index 60a6aa0b6..e2ff50b6d 100644
--- a/nuttx/configs/nucleo-f4x1re/src/Makefile
+++ b/nuttx/configs/nucleo-f4x1re/src/Makefile
@@ -63,6 +63,13 @@ CSRCS += stm32_io.c
endif
endif
+ifeq ($(CONFIG_ADC),y)
+CSRCS += stm32_adc.c
+ifeq ($(CONFIG_AJOYSTICK),y)
+CSRCS += stm32_ajoystick.c
+endif
+endif
+
ifeq ($(CONFIG_NSH_LIBRARY),y)
CSRCS += stm32_nsh.c
endif
diff --git a/nuttx/configs/nucleo-f4x1re/src/nucleo-f4x1re.h b/nuttx/configs/nucleo-f4x1re/src/nucleo-f4x1re.h
index 1d99e7d35..d2138260a 100644
--- a/nuttx/configs/nucleo-f4x1re/src/nucleo-f4x1re.h
+++ b/nuttx/configs/nucleo-f4x1re/src/nucleo-f4x1re.h
@@ -171,6 +171,75 @@
(GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_2MHz|GPIO_OUTPUT_CLEAR|GPIO_PORTB|GPIO_PIN9)
#endif
+/* Itead Joystick Shield
+ *
+ * See http://imall.iteadstudio.com/im120417014.html for more information
+ * about this joystick.
+ *
+ * --------- ----------------- ---------------------------------
+ * ARDUINO ITEAD NUCLEO-F4x1
+ * PIN NAME SIGNAL SIGNAL
+ * --------- ----------------- ---------------------------------
+ * D3 Button E Output PB3
+ * D4 Button D Output PB5
+ * D5 Button C Output PB4
+ * D6 Button B Output PB10
+ * D7 Button A Output PA8
+ * D8 Button F Output PA9
+ * D9 Button G Output PC7
+ * A0 Joystick Y Output PA0 ADC_IN0
+ * A1 Joystick X Output PA1 ADC_IN1
+ * --------- ----------------- ---------------------------------
+ *
+ * All buttons are pulled on the shield. A sensed low value indicates
+ * when the button is pressed.
+ */
+
+#define ADC_XOUPUT 1 /* X output is on ADC channel 1 */
+#define ADC_YOUPUT 0 /* Y output is on ADC channel 0 */
+
+#define GPIO_BUTTON_A \
+ (GPIO_INPUT | GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN3)
+#define GPIO_BUTTON_B \
+ (GPIO_INPUT | GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN5)
+#define GPIO_BUTTON_C \
+ (GPIO_INPUT | GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN4)
+#define GPIO_BUTTON_D \
+ (GPIO_INPUT | GPIO_FLOAT |GPIO_EXTI | GPIO_PORTB | GPIO_PIN10)
+#define GPIO_BUTTON_E \
+ (GPIO_INPUT | GPIO_FLOAT |GPIO_EXTI | GPIO_PORTA | GPIO_PIN8)
+#define GPIO_BUTTON_F \
+ (GPIO_INPUT | GPIO_FLOAT |GPIO_EXTI | GPIO_PORTA | GPIO_PIN9)
+#define GPIO_BUTTON_G \
+ (GPIO_INPUT | GPIO_FLOAT |GPIO_EXTI | GPIO_PORTC | GPIO_PIN7)
+
+/* Itead Joystick Signal interpretation:
+ *
+ * --------- ----------------------- ---------------------------
+ * BUTTON TYPE NUTTX ALIAS
+ * --------- ----------------------- ---------------------------
+ * Button A Large button A JUMP/BUTTON 3
+ * Button B Large button B FIRE/BUTTON 2
+ * Button C Joystick select button SELECT/BUTTON 1
+ * Button D Tiny Button D BUTTON 6
+ * Button E Tiny Button E BUTTON 7
+ * Button F Large Button F BUTTON 4
+ * Button G Large Button G BUTTON 5
+ * --------- ----------------------- ---------------------------
+ */
+
+#define GPIO_BUTTON_1 GPIO_BUTTON_C
+#define GPIO_BUTTON_2 GPIO_BUTTON_B
+#define GPIO_BUTTON_3 GPIO_BUTTON_A
+#define GPIO_BUTTON_4 GPIO_BUTTON_F
+#define GPIO_BUTTON_5 GPIO_BUTTON_G
+#define GPIO_BUTTON_6 GPIO_BUTTON_D
+#define GPIO_BUTTON_7 GPIO_BUTTON_E
+
+#define GPIO_SELECT GPIO_BUTTON_1
+#define GPIO_FIRE GPIO_BUTTON_2
+#define GPIO_JUMP GPIO_BUTTON_3
+
/************************************************************************************
* Public Data
************************************************************************************/
@@ -223,4 +292,16 @@ void stm32_usbinitialize(void);
void board_led_initialize(void);
#endif
+/************************************************************************************
+ * Name: board_adc_initialize
+ *
+ * Description:
+ * Initialize and register the ADC driver(s)
+ *
+ ************************************************************************************/
+
+#ifdef CONFIG_ADC
+int board_adc_initialize(void);
+#endif
+
#endif /* __CONFIGS_NUCLEO_F401RE_SRC_NUCLEO_F401RE_H */
diff --git a/nuttx/configs/nucleo-f4x1re/src/stm32_adc.c b/nuttx/configs/nucleo-f4x1re/src/stm32_adc.c
new file mode 100644
index 000000000..6d0515e38
--- /dev/null
+++ b/nuttx/configs/nucleo-f4x1re/src/stm32_adc.c
@@ -0,0 +1,162 @@
+/************************************************************************************
+ * configs/nucleo-f4x1re/src/stm32_adc.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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 <errno.h>
+#include <debug.h>
+
+#include <nuttx/analog/adc.h>
+#include <arch/board/board.h>
+
+#include "chip.h"
+#include "up_arch.h"
+
+#include "stm32_pwm.h"
+#include "nucleo-f4x1re.h"
+
+#ifdef CONFIG_STM32_ADC1
+
+/************************************************************************************
+ * Pre-processor Definitions
+ ************************************************************************************/
+
+/* The number of ADC channels in the conversion list */
+
+#define ADC1_NCHANNELS 2
+
+/************************************************************************************
+ * Private Data
+ ************************************************************************************/
+/* Identifying number of each ADC channel. */
+
+#ifdef CONFIG_STM32_ADC1
+#ifdef CONFIG_AJOYSTICK
+/* The Itead analog joystick gets inputs on ADC_IN0 and ADC_IN1 */
+
+static const uint8_t g_adc1_chanlist[ADC1_NCHANNELS] = {0, 1};
+
+/* Configurations of pins used byte each ADC channels */
+
+static const uint32_t g_adc1_pinlist[ADC1_NCHANNELS] = {GPIO_ADC1_IN0, GPIO_ADC1_IN0};
+#endif
+#endif
+
+/************************************************************************************
+ * Private Functions
+ ************************************************************************************/
+
+/************************************************************************************
+ * Public Functions
+ ************************************************************************************/
+
+/************************************************************************************
+ * Name: board_adc_initialize
+ *
+ * Description:
+ * Initialize and register the ADC driver
+ *
+ ************************************************************************************/
+
+int board_adc_initialize(void)
+{
+ static bool initialized = false;
+ struct adc_dev_s *adc;
+ int ret;
+ int i;
+
+ /* Check if we have already initialized */
+
+ if (!initialized)
+ {
+#ifdef CONFIG_STM32_ADC1
+ /* Configure the pins as analog inputs for the selected channels */
+
+ for (i = 0; i < ADC1_NCHANNELS; i++)
+ {
+ stm32_configgpio(g_adc1_pinlist[i]);
+ }
+
+ /* Call stm32_adcinitialize() to get an instance of the ADC interface */
+
+ adc = stm32_adcinitialize(1, g_adc1_chanlist, ADC1_NCHANNELS);
+ if (adc == NULL)
+ {
+ adbg("ERROR: Failed to get ADC interface\n");
+ return -ENODEV;
+ }
+
+ /* Register the ADC driver at "/dev/adc0" */
+
+ ret = adc_register("/dev/adc0", adc);
+ if (ret < 0)
+ {
+ adbg("adc_register failed: %d\n", ret);
+ return ret;
+ }
+#endif
+ /* Now we are initialized */
+
+ initialized = true;
+ }
+
+ return OK;
+}
+
+/************************************************************************************
+ * Name: adc_devinit
+ *
+ * Description:
+ * All STM32 architectures must provide the following interface to work with
+ * examples/adc.
+ *
+ ************************************************************************************/
+
+#ifdef CONFIG_EXAMPLES_ADC
+int adc_devinit(void)
+{
+#ifdef CONFIG_SAMA5_ADC
+ return board_adc_initialize();
+#else
+ return -ENOSYS;
+#endif
+}
+#endif /* CONFIG_EXAMPLES_ADC */
+
+#endif /* CONFIG_STM32_ADC1 */
diff --git a/nuttx/configs/nucleo-f4x1re/src/stm32_ajoystick.c b/nuttx/configs/nucleo-f4x1re/src/stm32_ajoystick.c
new file mode 100644
index 000000000..4a3de9033
--- /dev/null
+++ b/nuttx/configs/nucleo-f4x1re/src/stm32_ajoystick.c
@@ -0,0 +1,444 @@
+/****************************************************************************
+ * configs/nucleo-f3x1re/src/stm32_ajoystick.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * 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 <fcntl.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/input/ajoystick.h>
+
+#include "stm32_gpio.h"
+#include "stm32_adc.h"
+#include "chip/stm32_adc.h"
+#include "nucleo-f4x1re.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+/* Check for pre-requisites and pin conflicts */
+
+#ifdef CONFIG_AJOYSTICK
+# if !defined(CONFIG_ADC)
+# error CONFIG_ADC is required for the Itead joystick
+# undef CONFIG_AJOYSTICK
+# elif !defined(CONFIG_STM32_ADC1)
+# error CONFIG_STM32_ADC1 is required for Itead joystick
+# undef CONFIG_AJOYSTICK
+# endif
+#endif /* CONFIG_AJOYSTICK */
+
+#ifdef CONFIG_AJOYSTICK
+
+/* Maximum number of ADC channels */
+
+#define MAX_ADC_CHANNELS 8
+
+/* Number of Joystick buttons */
+
+#define AJOY_NGPIOS 7
+
+/* Bitset of supported Joystick buttons */
+
+#define AJOY_SUPPORTED (AJOY_BUTTON_1_BIT | AJOY_BUTTON_2_BIT | \
+ AJOY_BUTTON_3_BIT | AJOY_BUTTON_4_BIT | \
+ AJOY_BUTTON_5_BIT | AJOY_BUTTON_6_BIT | \
+ AJOY_BUTTON_7_BIT )
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static ajoy_buttonset_t ajoy_supported(FAR const struct ajoy_lowerhalf_s *lower);
+static int ajoy_sample(FAR const struct ajoy_lowerhalf_s *lower,
+ FAR struct ajoy_sample_s *sample);
+static ajoy_buttonset_t ajoy_buttons(FAR const struct ajoy_lowerhalf_s *lower);
+static void ajoy_enable(FAR const struct ajoy_lowerhalf_s *lower,
+ ajoy_buttonset_t press, ajoy_buttonset_t release,
+ ajoy_handler_t handler, FAR void *arg);
+
+static void ajoy_disable(void);
+static int ajoy_interrupt(int irq, FAR void *context);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+/* Pin configuration for each Itead joystick button. Index using AJOY_*
+ * button definitions in include/nuttx/input/ajoystick.h.
+ */
+
+static const uint32_t g_joygpio[AJOY_NGPIOS] =
+{
+ GPIO_BUTTON_1, GPIO_BUTTON_2, GPIO_BUTTON_3, GPIO_BUTTON_4,
+ GPIO_BUTTON_5, GPIO_BUTTON_6, GPIO_BUTTON_6
+};
+
+/* This is the button joystick lower half driver interface */
+
+static const struct ajoy_lowerhalf_s g_ajoylower =
+{
+ .al_supported = ajoy_supported,
+ .al_sample = ajoy_sample,
+ .al_buttons = ajoy_buttons,
+ .al_enable = ajoy_enable,
+};
+
+/* Descriptor for the open ADC driver */
+
+static int g_adcfd = -1;
+
+/* Current interrupt handler and argument */
+
+static ajoy_handler_t g_ajoyhandler;
+static FAR void *g_ajoyarg;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: ajoy_supported
+ *
+ * Description:
+ * Return the set of buttons supported on the button joystick device
+ *
+ ****************************************************************************/
+
+static ajoy_buttonset_t ajoy_supported(FAR const struct ajoy_lowerhalf_s *lower)
+{
+ ivdbg("Supported: %02x\n", AJOY_SUPPORTED);
+ return (ajoy_buttonset_t)AJOY_SUPPORTED;
+}
+
+/****************************************************************************
+ * Name: ajoy_sample
+ *
+ * Description:
+ * Return the current state of all button joystick buttons
+ *
+ ****************************************************************************/
+
+static int ajoy_sample(FAR const struct ajoy_lowerhalf_s *lower,
+ FAR struct ajoy_sample_s *sample)
+{
+ struct adc_msg_s adcmsg[MAX_ADC_CHANNELS];
+ FAR struct adc_msg_s *ptr;
+ ssize_t nread;
+ ssize_t offset;
+ int have;
+ int i;
+
+ /* Read all of the available samples (handling the case where additional
+ * channels are enabled).
+ */
+
+ nread = read(g_adcfd, adcmsg, MAX_ADC_CHANNELS * sizeof(struct adc_msg_s));
+ if (nread < 0)
+ {
+ int errcode = get_errno();
+ if (errcode != EINTR)
+ {
+ idbg("ERROR: read failed: %d\n", errcode);
+ }
+
+ return -errcode;
+ }
+ else if (nread < 2 * sizeof(struct adc_msg_s))
+ {
+ idbg("ERROR: read too small: %ld\n", (long)nread);
+ return -EIO;
+ }
+
+ /* Sample and the raw analog inputs */
+
+ for (i = 0, offset = 0, have = 0;
+ i < MAX_ADC_CHANNELS && offset < nread && have != 3;
+ i++, offset += sizeof(struct adc_msg_s))
+ {
+ ptr = &adcmsg[i];
+
+ /* Is this one of the channels that we need? */
+
+ if ((have & 1) == 0 && ptr->am_channel == 0)
+ {
+ int32_t tmp = ptr->am_data;
+ sample->as_x = (int16_t)tmp;
+ have |= 1;
+
+ ivdbg("X sample: %ld -> %d\n", (long)tmp, (int)sample->as_x);
+ }
+
+ if ((have & 2) == 0 && ptr->am_channel == 1)
+ {
+ int32_t tmp = ptr->am_data;
+ sample->as_y = (int16_t)tmp;
+ have |= 2;
+
+ ivdbg("Y sample: %ld -> %d\n", (long)tmp, (int)sample->as_y);
+ }
+ }
+
+ if (have != 3)
+ {
+ idbg("ERROR: Could not find joystack channels\n");
+ return -EIO;
+ }
+
+
+ /* Sample the discrete button inputs */
+
+ sample->as_buttons = ajoy_buttons(lower);
+ ivdbg("Returning: %02x\n", AJOY_SUPPORTED);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: ajoy_buttons
+ *
+ * Description:
+ * Return the current state of button data (only)
+ *
+ ****************************************************************************/
+
+static ajoy_buttonset_t ajoy_buttons(FAR const struct ajoy_lowerhalf_s *lower)
+{
+ ajoy_buttonset_t ret = 0;
+ int i;
+
+ /* Read each joystick GPIO value */
+
+ for (i = 0; i < AJOY_NGPIOS; i++)
+ {
+ /* Button outputs are pulled high. So a sensed low level means that the
+ * button is pressed.
+ */
+
+ if (!stm32_gpioread(g_joygpio[i]))
+ {
+ ret |= (1 << i);
+ }
+ }
+
+ ivdbg("Returning: %02x\n", ret);
+ return ret;
+}
+
+/****************************************************************************
+ * Name: ajoy_enable
+ *
+ * Description:
+ * Enable interrupts on the selected set of joystick buttons. And empty
+ * set will disable all interrupts.
+ *
+ ****************************************************************************/
+
+static void ajoy_enable(FAR const struct ajoy_lowerhalf_s *lower,
+ ajoy_buttonset_t press, ajoy_buttonset_t release,
+ ajoy_handler_t handler, FAR void *arg)
+{
+ irqstate_t flags;
+ ajoy_buttonset_t either = press | release;
+ ajoy_buttonset_t bit;
+ bool rising;
+ bool falling;
+ int i;
+
+ /* Start with all interrupts disabled */
+
+ flags = irqsave();
+ ajoy_disable();
+
+ illvdbg("press: %02x release: %02x handler: %p arg: %p\n",
+ press, release, handler, arg);
+
+ /* If no events are indicated or if no handler is provided, then this
+ * must really be a request to disable interrupts.
+ */
+
+ if (either && handler)
+ {
+ /* Save the new the handler and argument */
+
+ g_ajoyhandler = handler;
+ g_ajoyarg = arg;
+
+ /* Check each GPIO. */
+
+ for (i = 0; i < AJOY_NGPIOS; i++)
+ {
+ /* Enable interrupts on each pin that has either a press or
+ * release event associated with it.
+ */
+
+ bit = (1 << i);
+ if ((either & bit) != 0)
+ {
+ /* Active low so a press corresponds to a falling edge and
+ * a release corresponds to a rising edge.
+ */
+
+ falling = ((press & bit) != 0);
+ rising = ((release & bit) != 0);
+
+ illvdbg("GPIO %d: rising: %d falling: %d\n",
+ i, rising, falling);
+
+ (void)stm32_gpiosetevent(g_joygpio[i], rising, falling,
+ true, ajoy_interrupt);
+ }
+ }
+ }
+
+ irqrestore(flags);
+}
+
+/****************************************************************************
+ * Name: ajoy_disable
+ *
+ * Description:
+ * Disable all joystick interrupts
+ *
+ ****************************************************************************/
+
+static void ajoy_disable(void)
+{
+ irqstate_t flags;
+ int i;
+
+ /* Disable each joystick interrupt */
+
+ flags = irqsave();
+ for (i = 0; i < AJOY_NGPIOS; i++)
+ {
+ (void)stm32_gpiosetevent(g_joygpio[i], false, false, false, NULL);
+ }
+
+ irqrestore(flags);
+
+ /* Nullify the handler and argument */
+
+ g_ajoyhandler = NULL;
+ g_ajoyarg = NULL;
+}
+
+/****************************************************************************
+ * Name: ajoy_interrupt
+ *
+ * Description:
+ * Discrete joystick interrupt handler
+ *
+ ****************************************************************************/
+
+static int ajoy_interrupt(int irq, FAR void *context)
+{
+ DEBUGASSERT(g_ajoyhandler);
+ if (g_ajoyhandler)
+ {
+ g_ajoyhandler(&g_ajoylower, g_ajoyarg);
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm32_ajoy_initialization
+ *
+ * Description:
+ * Initialize and register the button joystick driver
+ *
+ ****************************************************************************/
+
+int stm32_ajoy_initialization(void)
+{
+ int ret;
+ int i;
+
+ /* Initialize ADC. We will need this to read the ADC inputs */
+
+ ret = board_adc_initialize();
+ if (ret < 0)
+ {
+ idbg("ERROR: board_adc_initialize() failed: %d\n", ret);
+ return ret;
+ }
+
+ /* Open the ADC driver for reading */
+
+ g_adcfd = open("/dev/adc0", O_RDONLY);
+ if (g_adcfd < 0)
+ {
+ int errcode = get_errno();
+ idbg("ERROR: Failed to open /dev/adc0: %d\n", errcode);
+ return -errcode;
+ }
+
+ /* Configure the GPIO pins as interrupting inputs. */
+
+ for (i = 0; i < AJOY_NGPIOS; i++)
+ {
+ /* Configure the PIO as an input */
+
+ stm32_configgpio(g_joygpio[i]);
+ }
+
+ /* Register the joystick device as /dev/ajoy0 */
+
+ ret = ajoy_register("/dev/ajoy0", &g_ajoylower);
+ if (ret < 0)
+ {
+ idbg("ERROR: ajoy_register failed: %d\n", ret);
+ close(g_adcfd);
+ g_adcfd = -1;
+ }
+
+ return ret;
+}
+
+#endif /* CONFIG_AJOYSTICK */
diff --git a/nuttx/configs/sama5d3-xplained/src/sam_ajoystick.c b/nuttx/configs/sama5d3-xplained/src/sam_ajoystick.c
index f1911e236..f98c496f9 100644
--- a/nuttx/configs/sama5d3-xplained/src/sam_ajoystick.c
+++ b/nuttx/configs/sama5d3-xplained/src/sam_ajoystick.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * configs/sam10e-eval/src/sam_ajoystick.c
+ * configs/sama5d3-xplained/src/sam_ajoystick.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>