Skip to content

Commit

Permalink
cxd56/spresense: Add callback mechanism to notice SDCard injection
Browse files Browse the repository at this point in the history
Add a mechanism to callback to an application to notice the SDCard
status is changed (inserted or ejected).
  • Loading branch information
SPRESENSE authored and masayuki2009 committed Apr 11, 2022
1 parent 81534df commit 64e5867
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions boards/arm/cxd56xx/spresense/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions boards/arm/cxd56xx/spresense/include/cxd56_sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
****************************************************************************/

#include <nuttx/config.h>
#include <stdint.h>

/****************************************************************************
* Public Types
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 17 additions & 0 deletions boards/arm/cxd56xx/spresense/src/cxd56_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <arch/chip/chip.h>

#include "cxd56_usbdev.h"
#include "arch/board/cxd56_sdcard.h"

#ifdef CONFIG_BOARDCTL_IOCTL

Expand Down Expand Up @@ -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;
}
Expand Down
36 changes: 36 additions & 0 deletions boards/arm/cxd56xx/spresense/src/cxd56_sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};

/****************************************************************************
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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;
}

0 comments on commit 64e5867

Please sign in to comment.