I want to use sdcard and mems accelerator on stm32f4 discovery. For this purpose I use DM-STF4BB baseboard and it's board files. I use also the fatfs demo from https://github.com/jedediahfrey/ARMCM4- ... F4BB-FATFS, and put MEMS code on top of it.
But it seems that fatfs and mems can't work together, either only fatfs, or mems, but not both. It depends also on the order of the functions sdcStart and spiStart. If I put spiStart after sdcStart, only mems will work, and the sdcard doesn't work.
And if I put sdcStart after spiStart, then sdcard/fatfs will work, but not the mems. Below is the main code where only MEMS can work.
The complete project can be also found here: https://github.com/cahya-wirawan/stm32f4-fatfs-mems
Is there any conflict between fatfs code and mems code? and how can I solve it? Thanks for any helps or suggestions.
Code: Select all
#include <stdio.h>
#include <string.h>
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "shell.h"
#include "ff.h"
#include "lis302dl.h"
#include "fat.h"
#include "usbcfg.h"
SerialUSBDriver SDU1;
int32_t accX, accY;
bool debug=FALSE;
#define SHELL_WA_SIZE THD_WA_SIZE(2048)
#define TEST_WA_SIZE THD_WA_SIZE(256)
static void cmd_debug(BaseSequentialStream *chp, int argc, char *argv[]) {
(void)argv;
if (argc != 1) {
chprintf(chp, "debug = %d\r\n", debug);
chprintf(chp, "Usage: debug <1|0>\r\n");
return;
}
if (strcmp(argv[0], "1") == 0) {
debug = TRUE;
}
else if(strcmp(argv[0], "0") == 0) {
debug = FALSE;
}
chprintf(chp, "debug = %d\r\n", debug);
}
static const ShellCommand commands[] = {
{"mkfs", cmd_mkfs},
{"mount", cmd_mount},
{"unmount", cmd_unmount},
{"tree", cmd_tree},
{"free", cmd_free},
{"mkdir", cmd_mkdir},
{"hello", cmd_hello},
{"cat", cmd_cat},
{"mem", cmd_mem},
{"threads", cmd_threads},
{"debug", cmd_debug},
{NULL, NULL}
};
static const ShellConfig shell_cfg1 = {
(BaseSequentialStream *)&SDU1,
commands
};
static const SPIConfig spi1cfg = {
NULL,
/* HW dependent part.*/
GPIOE,
GPIOE_CS_SPI,
SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_CPOL | SPI_CR1_CPHA
};
static WORKING_AREA(waThreadAccelerometer, 128);
static msg_t ThreadAccelerometer(void *arg) {
static int8_t xbuf[4], ybuf[4]; /* Last accelerometer data.*/
systime_t time; /* Next deadline.*/
static int32_t counter;
(void)arg;
chRegSetThreadName("Accelerometer");
lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG1, 0x43);
lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG2, 0x00);
lis302dlWriteRegister(&SPID1, LIS302DL_CTRL_REG3, 0x00);
time = chTimeNow();
while (TRUE) {
unsigned i;
for (i = 3; i > 0; i--) {
xbuf[i] = xbuf[i - 1];
ybuf[i] = ybuf[i - 1];
}
xbuf[0] = (int8_t)lis302dlReadRegister(&SPID1, LIS302DL_OUTX);
ybuf[0] = (int8_t)lis302dlReadRegister(&SPID1, LIS302DL_OUTY);
accX = ((int32_t)xbuf[0] + (int32_t)xbuf[1] +
(int32_t)xbuf[2] + (int32_t)xbuf[3]) / 4;
accY = ((int32_t)ybuf[0] + (int32_t)ybuf[1] +
(int32_t)ybuf[2] + (int32_t)ybuf[3]) / 4;
if(counter%5 == 0) {
if(debug)
chprintf((BaseSequentialStream *)&SDU1, "X:%d, Y:%d\r\n", accX, accY);
}
if(counter==1000000)
counter=0;
chThdSleepUntil(time += MS2ST(100));
}
return (msg_t)NULL;
}
static WORKING_AREA(waThreadLed, 128);
static msg_t ThreadLed(void *arg) {
(void)arg;
chRegSetThreadName("blinker");
while (TRUE) {
palTogglePad(GPIOD, GPIOD_LED4);
chThdSleep(MS2ST(500));
}
return (msg_t)NULL;
}
int main(void) {
Thread *shelltp = NULL;
halInit();
chSysInit();
shellInit();
sdcStart(&SDCD1, NULL); /* Start SD Driver */
spiStart(&SPID1, &spi1cfg); /* Initializes the SPI driver 1 in order to access the MEMS */
sduObjectInit(&SDU1);
sduStart(&SDU1, &serusbcfg);
usbDisconnectBus(serusbcfg.usbp);
chThdSleepMilliseconds(1000);
usbStart(serusbcfg.usbp, &usbcfg);
usbConnectBus(serusbcfg.usbp);
chThdCreateStatic(waThreadLed, sizeof(waThreadLed), NORMALPRIO+10, ThreadLed, NULL);
chThdCreateStatic(waThreadAccelerometer, sizeof(waThreadAccelerometer),
NORMALPRIO + 10, ThreadAccelerometer, NULL);
chRegSetThreadName("main");
chThdSetPriority(LOWPRIO);
while (TRUE) {
if (!shelltp) {
if (SDU1.config->usbp->state == USB_ACTIVE) {
/* Spawns a new shell.*/
shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO);
}
}
else {
/* If the previous shell exited.*/
if (chThdTerminated(shelltp)) {
/* Recovers memory of the previous shell.*/
chThdRelease(shelltp);
shelltp = NULL;
}
}
chThdSleepMilliseconds(500);
}
return (int)NULL;
}