I would like to create a simple data logger for F4-Discovery Board using MMC over SPI Driver.
I only need to write some string into a file!
I've found some examples for other hardware so I "wrote" (maybe I've copied and pasted!) just an "ugly" draft...
Code: Select all
#include <string.h>
#include "ch.h"
#include "hal.h"
#include "chsprintf.h"
#include "chprintf.h"
#include "evtimer.h"
#include "ff.h"
#define POLLING_INTERVAL 10
#define POLLING_DELAY 10
static VirtualTimer tmr; // Card monitor timer
static unsigned cnt; // Debounce counter
static EventSource inserted_event, removed_event; // Card event sources
static FRESULT scan_files(BaseSequentialStream *chp, char *path) {
FRESULT res;
FILINFO fno;
DIR dir;
int i;
char *fn;
#if _USE_LFN
fno.lfname = 0;
fno.lfsize = 0;
#endif
res = f_opendir(&dir, path);
if (res == FR_OK) {
i = strlen(path);
for (;;) {
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0)
break;
if (fno.fname[0] == '.')
continue;
fn = fno.fname;
if (fno.fattrib & AM_DIR) {
path[i++] = '/';
strcpy(&path[i], fn);
res = scan_files(chp, path);
if (res != FR_OK)
break;
path[--i] = 0;
}
else {
chprintf(chp, "%s/%s\r\n", path, fn);
}
}
}
return res;
}
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();
}
static void tmr_init(void *p) {
chEvtInit(&inserted_event);
chEvtInit(&removed_event);
chSysLock();
cnt = POLLING_INTERVAL;
chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, p);
chSysUnlock();
}
// MMC card insertion event.
static void InsertHandler(eventid_t id) {
FRESULT err;
(void)id;
//On insertion MMC initialization and FS mount.
if (mmcConnect(&MMCD1)) {
return;
}
err = f_mount(0, &MMC_FS);
if (err != FR_OK) {
mmcDisconnect(&MMCD1);
return;
}
fs_ready = TRUE;
}
// MMC card removal event.
static void RemoveHandler(eventid_t id) {
(void)id;
mmcDisconnect(&MMCD1);
fs_ready = FALSE;
}
/*===========================================================================*/
/* FatFs related. */
/*===========================================================================*/
FATFS MMC_FS; // brief FS object
MMCDriver MMCD1; // MMC driver instance.
static bool_t fs_ready = FALSE; // FS mounted and ready
/* Maximum speed SPI configuration (18MHz, CPHA=0, CPOL=0, MSb first).*/
static SPIConfig hs_spicfg = {NULL, IOPORT2, GPIOB_SPI2NSS, 0}; // GPIOB_SPI2NSS is not defined for F4
/* Low speed SPI configuration (281.250kHz, CPHA=0, CPOL=0, MSb first).*/
static SPIConfig ls_spicfg = {NULL, IOPORT2, GPIOB_SPI2NSS, // GPIOB_SPI2NSS is not defined for F4
SPI_CR1_BR_2 | SPI_CR1_BR_1};
/* MMC/SD over SPI driver configuration.*/
static MMCConfig mmccfg = {&SPID2, &ls_spicfg, &hs_spicfg};
/* Generic large buffer.*/
uint8_t fbuff[1024];
int main (void){
halInit();
chSysInit();
sdStart(&SD2, NULL); // Activates the serial driver 2 using the driver default configuration.
//Initializes the MMC driver to work with SPI2.
palSetPadMode(IOPORT2, GPIOB_SPI2NSS, PAL_MODE_OUTPUT_PUSHPULL); // GPIOB_SPI2NSS is not defined for F4
palSetPad(IOPORT2, GPIOB_SPI2NSS); // GPIOB_SPI2NSS is not defined for F4
mmcObjectInit(&MMCD1);
mmcStart(&MMCD1, &mmccfg);
//Activates the card insertion monitor.
tmr_init(&MMCD1);
// and NOW???
return 0;
}
I'm a beginner of "FatFS" so maybe it's all wrong or maybe someone has a small and simple example to post here...
Thank you very much in advance
Fede