git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4355 35acf78f-673a-0410-8e92-d51de3d6d3f4

master
gdisirio 2012-06-28 13:54:19 +00:00
parent a40226ad68
commit d5cfd83d17
3 changed files with 141 additions and 3 deletions

View File

@ -51,6 +51,35 @@ void __early_init(void) {
stm32_clock_init(); stm32_clock_init();
} }
#if HAL_USE_SDC
/*
* Card detection through the card internal pull-up on D3.
*/
bool_t sdc_lld_is_card_inserted(SDCDriver *sdcp) {
(void)sdcp;
palSetPadMode(GPIOC, GPIOC_SD_D3, PAL_MODE_INPUT);
if (palReadPad(GPIOC, GPIOC_SD_D3) != PAL_LOW) {
/* Switching the pin to SDIO mode because after detecting the card the
SDC driver will start accessing it.*/
palSetPadMode(GPIOC, GPIOC_SD_D3, PAL_MODE_ALTERNATE(12));
return TRUE;
}
/* Leaving the pin in input mode, it will be polled again.*/
return FALSE;
}
/*
* Card write protection detection is not possible, the card is always
* reported as not protected.
*/
bool_t sdc_lld_is_write_protected(SDCDriver *sdcp) {
(void)sdcp;
return FALSE;
}
#endif /* HAL_USE_SDC */
/* /*
* Board-specific initialization code. * Board-specific initialization code.
*/ */

View File

@ -122,7 +122,7 @@
* @brief Enables the SDC subsystem. * @brief Enables the SDC subsystem.
*/ */
#if !defined(HAL_USE_SDC) || defined(__DOXYGEN__) #if !defined(HAL_USE_SDC) || defined(__DOXYGEN__)
#define HAL_USE_SDC FALSE #define HAL_USE_SDC TRUE
#endif #endif
/** /**

View File

@ -27,6 +27,103 @@
#include "usbshell.h" #include "usbshell.h"
#include "web/web.h" #include "web/web.h"
/*===========================================================================*/
/* Card insertion monitor. */
/*===========================================================================*/
#define POLLING_INTERVAL 10
#define POLLING_DELAY 10
/**
* @brief Card monitor timer.
*/
static VirtualTimer tmr;
/**
* @brief Debounce counter.
*/
static unsigned cnt;
/**
* @brief Card event sources.
*/
static EventSource inserted_event, removed_event;
/**
* @brief Insertion monitor timer callback function.
*
* @param[in] p pointer to the @p BaseBlockDevice object
*
* @notapi
*/
static void tmrfunc(void *p) {
BaseBlockDevice *bbdp = p;
/* The presence check is performed only while the driver is not in a
transfer state because it is often performed by changing the mode of
the pin connected to the CS/D3 contact of the card, this could disturb
the transfer.*/
blkstate_t state = blkGetDriverState(bbdp);
chSysLockFromIsr();
if ((state != BLK_READING) && (state != BLK_WRITING)) {
/* Safe to perform the check.*/
if (cnt > 0) {
if (blkIsInserted(bbdp)) {
if (--cnt == 0) {
chEvtBroadcastI(&inserted_event);
}
}
else
cnt = POLLING_INTERVAL;
}
else {
if (!blkIsInserted(bbdp)) {
cnt = POLLING_INTERVAL;
chEvtBroadcastI(&removed_event);
}
}
}
chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, bbdp);
chSysUnlockFromIsr();
}
/**
* @brief Polling monitor start.
*
* @param[in] p pointer to an object implementing @p BaseBlockDevice
*
* @notapi
*/
static void tmr_init(void *p) {
chEvtInit(&inserted_event);
chEvtInit(&removed_event);
chSysLock();
cnt = POLLING_INTERVAL;
chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, p);
chSysUnlock();
}
/*===========================================================================*/
/* Main and generic code. */
/*===========================================================================*/
/*
* Card insertion event.
*/
static void InsertHandler(eventid_t id) {
(void)id;
}
/*
* Card removal event.
*/
static void RemoveHandler(eventid_t id) {
(void)id;
}
/* /*
* Green LED blinker thread, times are in milliseconds. * Green LED blinker thread, times are in milliseconds.
*/ */
@ -47,6 +144,11 @@ static msg_t Thread1(void *arg) {
* Application entry point. * Application entry point.
*/ */
int main(void) { int main(void) {
static const evhandler_t evhndl[] = {
InsertHandler,
RemoveHandler
};
struct EventListener el0, el1;
/* /*
* System initializations. * System initializations.
@ -68,6 +170,11 @@ int main(void) {
*/ */
sdStart(&SD6, NULL); sdStart(&SD6, NULL);
/*
* Activates the card insertion monitor.
*/
tmr_init(&SDCD1);
/* /*
* Creates the blinker thread. * Creates the blinker thread.
*/ */
@ -87,12 +194,14 @@ int main(void) {
/* /*
* Normal main() thread activity, in this demo it does nothing except * Normal main() thread activity, in this demo it does nothing except
* sleeping in a loop and check the button state. * sleeping in a loop and listen for events.
*/ */
chEvtRegister(&inserted_event, &el0, 0);
chEvtRegister(&removed_event, &el1, 1);
while (TRUE) { while (TRUE) {
usStateCheck(); usStateCheck();
if (palReadPad(GPIOA, GPIOA_BUTTON_WKUP) != 0) { if (palReadPad(GPIOA, GPIOA_BUTTON_WKUP) != 0) {
} }
chThdSleepMilliseconds(500); chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS));
} }
} }