You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ma...@apache.org on 2022/04/11 22:55:14 UTC

[incubator-nuttx] 08/08: cxd56/spresense: Add callback mechanism to notice SDCard injection

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

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

commit 64e5867a8b9645f9a43e93052e1babbcba1495b4
Author: SPRESENSE <41...@users.noreply.github.com>
AuthorDate: Mon Apr 11 20:12:20 2022 +0900

    cxd56/spresense: Add callback mechanism to notice SDCard injection
    
    Add a mechanism to callback to an application to notice the SDCard
    status is changed (inserted or ejected).
---
 boards/arm/cxd56xx/spresense/include/board.h       |  4 +++
 .../arm/cxd56xx/spresense/include/cxd56_sdcard.h   | 13 ++++++++
 boards/arm/cxd56xx/spresense/src/cxd56_ioctl.c     | 17 ++++++++++
 boards/arm/cxd56xx/spresense/src/cxd56_sdcard.c    | 36 ++++++++++++++++++++++
 4 files changed, 70 insertions(+)

diff --git a/boards/arm/cxd56xx/spresense/include/board.h b/boards/arm/cxd56xx/spresense/include/board.h
index af64cfd1c4..fa720ca65c 100644
--- a/boards/arm/cxd56xx/spresense/include/board.h
+++ b/boards/arm/cxd56xx/spresense/include/board.h
@@ -261,6 +261,10 @@ enum board_power_device
 
 #define BOARDIOC_USBDEV_SETNOTIFYSIG      (BOARDIOC_USER+0x0001)
 
+/* Set callback function pointer for notify SDCard state change *************/
+
+#define BOARDIOC_SDCARD_SETNOTIFYCB       (BOARDIOC_USER+0x0002)
+
 /* Altair modem device pin definitions **************************************/
 
 #define ALTMDM_SLAVE_REQ          PIN_SPI2_SCK
diff --git a/boards/arm/cxd56xx/spresense/include/cxd56_sdcard.h b/boards/arm/cxd56xx/spresense/include/cxd56_sdcard.h
index 1553af5e90..0b8a3f4ab7 100644
--- a/boards/arm/cxd56xx/spresense/include/cxd56_sdcard.h
+++ b/boards/arm/cxd56xx/spresense/include/cxd56_sdcard.h
@@ -26,6 +26,7 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+#include <stdint.h>
 
 /****************************************************************************
  * Public Types
@@ -130,6 +131,18 @@ void board_sdcard_set_high_voltage(void);
 
 void board_sdcard_set_low_voltage(void);
 
+/****************************************************************************
+ * Name: board_sdcard_set_state_cb
+ *
+ * Description:
+ *   Register callback function to notify state change of card slot.
+ *   This function is called by board_ioctl()
+ *    as BOARDIOC_SDCARD_SETNOTIFYCB command.
+ *
+ ****************************************************************************/
+
+int board_sdcard_set_state_cb(uintptr_t cb);
+
 #undef EXTERN
 #if defined(__cplusplus)
 }
diff --git a/boards/arm/cxd56xx/spresense/src/cxd56_ioctl.c b/boards/arm/cxd56xx/spresense/src/cxd56_ioctl.c
index 025ff6b67c..b174d37aa2 100644
--- a/boards/arm/cxd56xx/spresense/src/cxd56_ioctl.c
+++ b/boards/arm/cxd56xx/spresense/src/cxd56_ioctl.c
@@ -34,6 +34,7 @@
 #include <arch/chip/chip.h>
 
 #include "cxd56_usbdev.h"
+#include "arch/board/cxd56_sdcard.h"
 
 #ifdef CONFIG_BOARDCTL_IOCTL
 
@@ -91,6 +92,22 @@ int board_ioctl(unsigned int cmd, uintptr_t arg)
         }
         break;
 #endif
+
+#ifdef CONFIG_CXD56_SDIO
+      /* CMD:           BOARDIOC_SDCARD_SETNOTIFYCB
+       * DESCRIPTION:   Set a callback function pointer to SDCard driver
+       *                to notify when card status is changed.
+       * ARG:           Callback function.
+       * CONFIGURATION: CONFIG_BOARDCTL & CONFIG_CXD56_SDIO
+       * DEPENDENCIES:  Board logic must provide board_app_initialization
+       */
+
+      case BOARDIOC_SDCARD_SETNOTIFYCB:
+        {
+          ret = board_sdcard_set_state_cb(arg);
+        }
+        break;
+#endif
       default:
         break;
     }
diff --git a/boards/arm/cxd56xx/spresense/src/cxd56_sdcard.c b/boards/arm/cxd56xx/spresense/src/cxd56_sdcard.c
index bfb88697a0..7f6c75fe98 100644
--- a/boards/arm/cxd56xx/spresense/src/cxd56_sdcard.c
+++ b/boards/arm/cxd56xx/spresense/src/cxd56_sdcard.c
@@ -70,6 +70,7 @@ struct cxd56_sdhci_state_s
   struct sdio_dev_s *sdhci;   /* R/W device handle */
   bool initialized;           /* TRUE: SDHCI block driver is initialized */
   bool inserted;              /* TRUE: card is inserted */
+  void (*cb)(bool);           /* Callback function pointer to application */
 };
 
 /****************************************************************************
@@ -165,6 +166,13 @@ static void board_sdcard_enable(FAR void *arg)
         }
 
       g_sdhci.initialized = true;
+
+      /* Callback to application to notice card is inserted */
+
+      if (g_sdhci.cb != NULL)
+        {
+          g_sdhci.cb(true);
+        }
     }
 
 release_frequency_lock:
@@ -203,6 +211,13 @@ static void board_sdcard_disable(FAR void *arg)
       cxd56_sdhci_finalize(0);
 
       g_sdhci.initialized = false;
+
+      /* Callback to application to notice card is ejected */
+
+      if (g_sdhci.cb != NULL)
+        {
+          g_sdhci.cb(false);
+        }
     }
 }
 
@@ -515,3 +530,24 @@ void board_sdcard_set_high_voltage(void)
 void board_sdcard_set_low_voltage(void)
 {
 }
+
+/****************************************************************************
+ * Name: board_sdcard_set_state_cb
+ *
+ * Description:
+ *   Register callback function to notify state change of card slot.
+ *   This function is called by board_ioctl()
+ *    as BOARDIOC_SDCARD_SETNOTIFYCB command.
+ *
+ ****************************************************************************/
+
+int board_sdcard_set_state_cb(uintptr_t cb)
+{
+  if (g_sdhci.cb != NULL && cb != 0)
+    {
+      return -EBUSY;
+    }
+
+  g_sdhci.cb = (void (*)(bool))cb;
+  return OK;
+}