This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new e1f174e48 examples/npzero: add the nPZero G1S power-saving example
e1f174e48 is described below

commit e1f174e4803997f8cddbbf9d11f20cd250a813b9
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Wed Nov 11 19:12:39 2026 -0300

    examples/npzero: add the nPZero G1S power-saving example
    
    Add an example of the nPZero G1S driver for the sensors of the nPZero
    G1S Development Kit, like the example of the Nanopower driver API:
    * LIS2DW12 accelerometer on peripheral 3 (SPI), read every 5 s, wakes
      the host up above about 61 mg on X.
    * AS6212 temperature sensor on peripheral 4 (I2C), read every 30 s,
      wakes the host up above 25 C or below 10 C.
    * Global time-out of 300 s.
    
    At each boot of the host it shows the ID and the wake-up reason (with
    the values of the peripherals and of the ADC that triggered it), sends
    the configuration and, after CONFIG_EXAMPLES_NPZERO_IDLE_DELAY ms,
    enters Idle state, which switches the host off.  "npzero -s" shows the
    status only.  Both sensors are kept powered while the host is on
    (pswint): an unpowered AS6212 board blocks the shared I2C bus.
    
    Tested on the DevKit with npzero-g1s-dk:nsh:
    
      nsh> npzero -s
      nPZero G1S ID: 0x60
      STA1: 0x01 STA2: 0x00
      nsh> npzero
      nPZero G1S ID: 0x60
      STA1: 0x01 STA2: 0x00
      Configured, Idle state in 1000 ms
    
    Signed-off-by: Alan C. Assis <[email protected]>
    Assisted-by: Claude:claude-opus-5-5
---
 examples/npzero/CMakeLists.txt |  35 ++++
 examples/npzero/Kconfig        |  46 ++++++
 examples/npzero/Make.defs      |  25 +++
 examples/npzero/Makefile       |  34 ++++
 examples/npzero/npzero_main.c  | 368 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 508 insertions(+)

diff --git a/examples/npzero/CMakeLists.txt b/examples/npzero/CMakeLists.txt
new file mode 100644
index 000000000..2aa4f395b
--- /dev/null
+++ b/examples/npzero/CMakeLists.txt
@@ -0,0 +1,35 @@
+# 
##############################################################################
+# apps/examples/npzero/CMakeLists.txt
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more 
contributor
+# license agreements.  See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership.  The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may 
not
+# use this file except in compliance with the License.  You may obtain a copy 
of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+# 
##############################################################################
+
+if(CONFIG_EXAMPLES_NPZERO)
+  nuttx_add_application(
+    NAME
+    ${CONFIG_EXAMPLES_NPZERO_PROGNAME}
+    SRCS
+    npzero_main.c
+    STACKSIZE
+    ${CONFIG_EXAMPLES_NPZERO_STACKSIZE}
+    PRIORITY
+    ${CONFIG_EXAMPLES_NPZERO_PRIORITY}
+    MODULE
+    ${CONFIG_EXAMPLES_NPZERO})
+endif()
diff --git a/examples/npzero/Kconfig b/examples/npzero/Kconfig
new file mode 100644
index 000000000..e105bcff9
--- /dev/null
+++ b/examples/npzero/Kconfig
@@ -0,0 +1,46 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_NPZERO
+       tristate "nPZero G1S power-saving IC example"
+       default n
+       depends on NPZERO
+       ---help---
+               Example of the nPZero G1S driver for the nPZero G1S Development
+               Kit.  At each wake-up of the host, it shows the wake-up reason
+               and the values of the sensors, configures the nPZero to poll the
+               LIS2DW12 accelerometer (SPI, peripheral 3) and the AS6212
+               temperature sensor (I2C, peripheral 4) and enters Idle state,
+               which switches the host off.
+
+if EXAMPLES_NPZERO
+
+config EXAMPLES_NPZERO_PROGNAME
+       string "Program name"
+       default "npzero"
+       ---help---
+               This is the name of the program that will be used when the NSH 
ELF
+               program is installed.
+
+config EXAMPLES_NPZERO_PRIORITY
+       int "nPZero task priority"
+       default 100
+
+config EXAMPLES_NPZERO_STACKSIZE
+       int "nPZero stack size"
+       default DEFAULT_TASK_STACKSIZE
+
+config EXAMPLES_NPZERO_DEVPATH
+       string "nPZero device path"
+       default "/dev/npzero0"
+
+config EXAMPLES_NPZERO_IDLE_DELAY
+       int "Delay before Idle state (ms)"
+       default 1000
+       ---help---
+               Time to wait before sending the Idle command, which switches the
+               host off.  It leaves time to connect a debugger to the host.
+
+endif
diff --git a/examples/npzero/Make.defs b/examples/npzero/Make.defs
new file mode 100644
index 000000000..1396a60dc
--- /dev/null
+++ b/examples/npzero/Make.defs
@@ -0,0 +1,25 @@
+############################################################################
+# apps/examples/npzero/Make.defs
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+ifneq ($(CONFIG_EXAMPLES_NPZERO),)
+CONFIGURED_APPS += $(APPDIR)/examples/npzero
+endif
diff --git a/examples/npzero/Makefile b/examples/npzero/Makefile
new file mode 100644
index 000000000..6f4f95f57
--- /dev/null
+++ b/examples/npzero/Makefile
@@ -0,0 +1,34 @@
+############################################################################
+# apps/examples/npzero/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+# nPZero G1S example
+
+PROGNAME  = $(CONFIG_EXAMPLES_NPZERO_PROGNAME)
+PRIORITY  = $(CONFIG_EXAMPLES_NPZERO_PRIORITY)
+STACKSIZE = $(CONFIG_EXAMPLES_NPZERO_STACKSIZE)
+MODULE    = $(CONFIG_EXAMPLES_NPZERO)
+
+MAINSRC = npzero_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/examples/npzero/npzero_main.c b/examples/npzero/npzero_main.c
new file mode 100644
index 000000000..b7aa13816
--- /dev/null
+++ b/examples/npzero/npzero_main.c
@@ -0,0 +1,368 @@
+/****************************************************************************
+ * apps/examples/npzero/npzero_main.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/power/npzero.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Peripherals of the nPZero G1S Development Kit */
+
+#define ACC_PERIPH     3    /* LIS2DW12 accelerometer, SPI, PMOD 3 */
+#define TEMP_PERIPH    4    /* AS6212 temperature sensor, I2C, PMOD 4 */
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* LIS2DW12: CTRL1 (0x20) = 0x10 (12.5 Hz, low-power), then read OUT_X_L
+ * (0x28 | read bit).  Wake up when |X| >= 1000 (about 61 mg).
+ */
+
+static const uint8_t g_acc_init[] =
+{
+  0x20, 0x10
+};
+
+static const uint8_t g_acc_read[] =
+{
+  0xa8
+};
+
+static const struct npzero_periph_s g_acc =
+{
+  .pwmod    = NPZERO_PWMOD_PERIODIC,
+  .tmod     = NPZERO_TMOD_READ,
+  .pswmod   = NPZERO_PSWMOD_LOGIC,
+  .intmod   = NPZERO_INTMOD_IN_HIGH,
+  .spi      = true,
+  .spimode  = 0,
+  .dtype    = NPZERO_DTYPE_INT16,
+  .period   = 50,                 /* 5 s with the 10 Hz clock */
+  .throver  = 1000,
+  .thrunder = (uint16_t)-1000,
+  .twtp     = 10,
+  .tinit    = NPZERO_WAIT_256,
+  .twait    = NPZERO_WAIT_256,
+  .ninit    = sizeof(g_acc_init),
+  .init     = g_acc_init,
+  .nread    = sizeof(g_acc_read),
+  .read     = g_acc_read,
+};
+
+/* AS6212 at 0x49: CONFIG (0x01) = 0x82, TLOW (0x02) = 0xa0, then read the
+ * temperature (0x00, big endian).  Wake up when T >= 25 C or T <= 10 C.
+ */
+
+static const uint8_t g_temp_init[] =
+{
+  0x01, 0x82, 0x02, 0xa0
+};
+
+static const struct npzero_periph_s g_temp =
+{
+  .pwmod    = NPZERO_PWMOD_PERIODIC,
+  .tmod     = NPZERO_TMOD_READ,
+  .pswmod   = NPZERO_PSWMOD_LOGIC,
+  .intmod   = NPZERO_INTMOD_IN_HIGH,
+  .dtype    = NPZERO_DTYPE_INT16,
+  .seqrw    = true,
+  .wunak    = true,
+  .swap     = true,
+  .addr     = 0x49,
+  .rreg     = 0x00,
+  .retries  = 3,
+  .period   = 300,                /* 30 s with the 10 Hz clock */
+  .throver  = 3200,               /* 25 C in units of 1/128 C */
+  .thrunder = 1280,               /* 10 C */
+  .twtp     = 0x31,               /* 49 x 640 us = 31.36 ms */
+  .tinit    = NPZERO_WAIT_256,
+  .twait    = NPZERO_WAIT_256,
+  .ninit    = sizeof(g_temp_init) / 2,
+  .init     = g_temp_init,
+};
+
+static const struct npzero_config_s g_config =
+{
+  .hostmode = NPZERO_HOSTMODE_LOGIC,
+  .pswint   = (1 << (ACC_PERIPH - 1)) | (1 << (TEMP_PERIPH - 1)),
+  .wakeup   = NPZERO_SYSCFG1_WUP_P(ACC_PERIPH) |
+              NPZERO_SYSCFG1_WUP_P(TEMP_PERIPH),
+  .sclksel  = NPZERO_SCLK_LPOSC,
+  .sclkdiv  = NPZERO_SCLKDIV_1,
+  .adcclk   = NPZERO_ADCCLK_SCLK,
+  .i2cpull  = NPZERO_I2CPULL_AUTO,
+  .clkout   = NPZERO_CLKOUT_OFF,
+  .timeout  = 3000,               /* 300 s with the 10 Hz clock */
+  .intpull  =
+  {
+    NPZERO_INTPULL_50K, NPZERO_INTPULL_50K,
+    NPZERO_INTPULL_50K, NPZERO_INTPULL_50K
+  },
+  .periph   =
+  {
+    NULL, NULL, &g_acc, &g_temp
+  },
+};
+
+/* Lowest input voltage (mV) of the ADC codes (datasheet, Table 50) */
+
+static const uint8_t g_adc_ext_code[] =
+{
+  0x00, 0x08, 0x0f, 0x15, 0x19, 0x1c, 0x1f, 0x22, 0x23, 0x24, 0x26, 0x28,
+  0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34
+};
+
+static const uint16_t g_adc_ext_mv[] =
+{
+  600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
+  1800, 1900, 2000, 2100, 2200, 2300, 2400, 2600, 2700, 2900, 3100, 3400
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: npzero_adc_mv
+ *
+ * Description:
+ *   Convert an ADC code to the lowest matching input voltage.  The codes of
+ *   the VBAT channel are the ones of the ADC_IN channel minus 0x20.
+ *
+ ****************************************************************************/
+
+static int npzero_adc_mv(uint8_t code)
+{
+  int i;
+
+  for (i = sizeof(g_adc_ext_code) - 1; i > 0; i--)
+    {
+      if (code >= g_adc_ext_code[i])
+        {
+          break;
+        }
+    }
+
+  return g_adc_ext_mv[i];
+}
+
+/****************************************************************************
+ * Name: npzero_show_value
+ ****************************************************************************/
+
+static void npzero_show_value(int fd, int periph)
+{
+  struct npzero_value_s value;
+  int32_t v;
+  int32_t a;
+
+  value.periph = periph;
+  if (ioctl(fd, NPZEROIOC_GETVALUE, (unsigned long)&value) < 0)
+    {
+      printf("ERROR: GETVALUE failed: %d\n", errno);
+      return;
+    }
+
+  v = (int16_t)value.value;
+  if (periph == ACC_PERIPH)
+    {
+      /* LIS2DW12, +/-2 g full scale: 1 LSB = 2000 / 32768 mg */
+
+      printf("  Acceleration X: %" PRId32 " mg\n", v * 125 / 2048);
+    }
+  else if (periph == TEMP_PERIPH)
+    {
+      /* AS6212: 1 LSB = 1/128 C */
+
+      v = v * 1000 / 128;
+      a = v < 0 ? -v : v;
+      printf("  Temperature: %s%" PRId32 ".%03" PRId32 " C\n",
+             v < 0 ? "-" : "", a / 1000, a % 1000);
+    }
+  else
+    {
+      printf("  Value: 0x%04x\n", value.value);
+    }
+}
+
+/****************************************************************************
+ * Name: npzero_show_status
+ *
+ * Description:
+ *   Show the reason of the last wake-up of the host and the last values of
+ *   the peripherals that triggered it.
+ *
+ ****************************************************************************/
+
+static int npzero_show_status(int fd)
+{
+  struct npzero_status_s status;
+  struct npzero_adc_s adc;
+  int i;
+
+  if (ioctl(fd, NPZEROIOC_GETSTATUS, (unsigned long)&status) < 0)
+    {
+      printf("ERROR: GETSTATUS failed: %d\n", errno);
+      return -errno;
+    }
+
+  printf("STA1: 0x%02x STA2: 0x%02x\n", status.sta1, status.sta2);
+
+  /* Only the soft reset bit of RST_SRC is reliable (errata #5) */
+
+  if (status.sta1 & NPZERO_STA1_RSTSRC_SOFT)
+    {
+      printf("Soft reset\n");
+    }
+
+  if (status.sta1 & NPZERO_STA1_FTOUT)
+    {
+      printf("Global time-out\n");
+    }
+
+  if (status.sta1 & (NPZERO_STA1_FA1 | NPZERO_STA1_FA2))
+    {
+      if (ioctl(fd, NPZEROIOC_GETADC, (unsigned long)&adc) >= 0)
+        {
+          if (status.sta1 & NPZERO_STA1_FA1)
+            {
+              printf("VBAT trigger: 0x%02x (>= %d mV)\n", adc.core,
+                     npzero_adc_mv(adc.core + 0x20));
+            }
+
+          if (status.sta1 & NPZERO_STA1_FA2)
+            {
+              printf("ADC_IN trigger: 0x%02x (>= %d mV)\n", adc.ext,
+                     npzero_adc_mv(adc.ext));
+            }
+        }
+    }
+
+  for (i = 1; i <= NPZERO_NPERIPH; i++)
+    {
+      if (status.sta2 & NPZERO_STA2_FNAKP(i))
+        {
+          printf("Peripheral %d: NAK\n", i);
+        }
+
+      if (status.sta2 & NPZERO_STA2_FP(i))
+        {
+          printf("Peripheral %d: trigger\n", i);
+          npzero_show_value(fd, i);
+        }
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * npzero_main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  bool statusonly = false;
+  uint8_t id;
+  int ret;
+  int fd;
+
+  if (argc > 1)
+    {
+      if (strcmp(argv[1], "-s") != 0)
+        {
+          printf("Usage: %s [-s]\n"
+                 "  -s: show the status only, do not enter Idle state\n",
+                 argv[0]);
+          return EXIT_FAILURE;
+        }
+
+      statusonly = true;
+    }
+
+  fd = open(CONFIG_EXAMPLES_NPZERO_DEVPATH, O_RDWR);
+  if (fd < 0)
+    {
+      printf("ERROR: open %s failed: %d\n",
+             CONFIG_EXAMPLES_NPZERO_DEVPATH, errno);
+      return EXIT_FAILURE;
+    }
+
+  ret = ioctl(fd, NPZEROIOC_GETID, (unsigned long)&id);
+  if (ret < 0)
+    {
+      printf("ERROR: GETID failed: %d\n", errno);
+      goto errout;
+    }
+
+  printf("nPZero G1S ID: 0x%02x\n", id);
+
+  ret = npzero_show_status(fd);
+  if (ret < 0 || statusonly)
+    {
+      goto errout;
+    }
+
+  ret = ioctl(fd, NPZEROIOC_CONFIGURE, (unsigned long)&g_config);
+  if (ret < 0)
+    {
+      printf("ERROR: CONFIGURE failed: %d\n", errno);
+      goto errout;
+    }
+
+  printf("Configured, Idle state in %d ms\n",
+         CONFIG_EXAMPLES_NPZERO_IDLE_DELAY);
+  usleep(CONFIG_EXAMPLES_NPZERO_IDLE_DELAY * 1000);
+
+  /* The nPZero switches the host off now, until the next wake-up */
+
+  ret = ioctl(fd, NPZEROIOC_IDLE, 0);
+  if (ret < 0)
+    {
+      printf("ERROR: IDLE failed: %d\n", errno);
+    }
+
+errout:
+  close(fd);
+  return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}

Reply via email to