SD Card change detection w/ debouncing

This commit is contained in:
ikari 2011-02-08 00:57:16 +01:00
parent 0df364760a
commit 986e37ee06
2 changed files with 35 additions and 20 deletions

View File

@ -117,6 +117,8 @@ enum cmd_state { CMD_RSP = 0, CMD_RSPDAT, CMD_DAT };
int during_blocktrans = TRANS_NONE; int during_blocktrans = TRANS_NONE;
uint32_t last_block = 0; uint32_t last_block = 0;
volatile int sd_changed;
/** /**
* getbits - read value from bit buffer * getbits - read value from bit buffer
* @buffer: pointer to the data buffer * @buffer: pointer to the data buffer
@ -298,6 +300,7 @@ int send_command_fast(uint8_t* cmd, uint8_t* rsp, uint8_t* buf){
rsplen = 6; rsplen = 6;
waitbusy = 1; waitbusy = 1;
break; break;
case 13:
case 17: case 17:
case 18: case 18:
dat = 1; dat = 1;
@ -825,8 +828,8 @@ DSTATUS disk_initialize(BYTE drv) __attribute__ ((weak, alias("sdn_initialize"))
void sdn_init(void) { void sdn_init(void) {
/* enable GPIO interrupt on SD detect pin, both edges */ /* enable GPIO interrupt on SD detect pin, both edges */
NVIC_EnableIRQ(EINT3_IRQn); /* NVIC_EnableIRQ(EINT3_IRQn);
SD_DT_INT_SETUP(); SD_DT_INT_SETUP(); */
/* disconnect SSP1 */ /* disconnect SSP1 */
LPC_PINCON->PINSEL0 &= ~(BV(13) | BV(15) | BV(17) | BV(19)); LPC_PINCON->PINSEL0 &= ~(BV(13) | BV(15) | BV(17) | BV(19));
/* prepare GPIOs */ /* prepare GPIOs */
@ -910,10 +913,14 @@ DRESULT disk_write(BYTE drv, const BYTE *buffer, DWORD sector, BYTE count) __att
/* Detect changes of SD card 0 */ /* Detect changes of SD card 0 */
void sdn_changed() { void sdn_changed() {
if (SDCARD_DETECT) { if (sd_changed) {
printf("ch ");
if(SDCARD_DETECT) {
disk_state = DISK_CHANGED; disk_state = DISK_CHANGED;
} else { } else {
disk_state = DISK_REMOVED; disk_state = DISK_REMOVED;
} }
sd_changed = 0;
}
} }

View File

@ -6,6 +6,7 @@
#include "timer.h" #include "timer.h"
#include "clock.h" #include "clock.h"
#include "uart.h" #include "uart.h"
#include "sdnative.h"
/* bit definitions */ /* bit definitions */
#define RITINT 0 #define RITINT 0
@ -13,6 +14,7 @@
#define PCRIT 16 #define PCRIT 16
extern volatile int sd_changed;
volatile tick_t ticks; volatile tick_t ticks;
volatile int wokefromrit; volatile int wokefromrit;
@ -23,6 +25,12 @@ void __attribute__((weak,noinline)) SysTick_Hook(void) {
/* Systick interrupt handler */ /* Systick interrupt handler */
void SysTick_Handler(void) { void SysTick_Handler(void) {
ticks++; ticks++;
static uint16_t sdch_state = 0;
sdch_state = (sdch_state << 1) | SDCARD_DETECT | 0xe000;
if((sdch_state == 0xf000) || (sdch_state == 0xefff)) {
sd_changed = 1;
}
sdn_changed();
SysTick_Hook(); SysTick_Hook();
} }