stm32f4: fatfs doesn't work together with mems accelerator

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
littlekoala
Posts: 8
Joined: Wed Jan 08, 2014 5:15 pm

stm32f4: fatfs doesn't work together with mems accelerator

Post by littlekoala »

Hi,

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;
}
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: stm32f4: fatfs doesn't work together with mems accelerat

Post by Giovanni »

There should be no conflict, it is more likely some side effect like a stack overflow or some other error.

Try to enable debug options in chconf.h.

Giovanni
littlekoala
Posts: 8
Joined: Wed Jan 08, 2014 5:15 pm

Re: stm32f4: fatfs doesn't work together with mems accelerat

Post by littlekoala »

I tried to increase the stack size from 128 to 256 bytes, but there is no change with the issue.
I tried also to enable following debug options in chconf:
CH_DBG_SYSTEM_STATE_CHECK, CH_DBG_ENABLE_CHECKS, CH_DBG_ENABLE_STACK_CHECK and CH_DBG_FILL_THREADS
But I don't know yet what should I check and expect after I enabled it. Anyway when I enabled CH_DBG_ENABLE_ASSERTS, it seems
the application hangs somewhere after the start, is it normal? Thanks.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: stm32f4: fatfs doesn't work together with mems accelerat

Post by Giovanni »

It is normal, debug options stop the execution of the program into the halt function, then you have to inspect the stack trace and the global variable dbg_panic_meg which points to an error message, the two things tell what happened.

Giovanni
littlekoala
Posts: 8
Joined: Wed Jan 08, 2014 5:15 pm

Re: stm32f4: fatfs doesn't work together with mems accelerat

Post by littlekoala »

both mems and fatfs can work now simultaneously after I changed dma configuration of SPI1 in mcuconf.h:

Code: Select all

-#define STM32_SPI_SPI1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 3)
+#define STM32_SPI_SPI1_TX_DMA_STREAM        STM32_DMA_STREAM_ID(2, 5)


does it make sense why it works now?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: stm32f4: fatfs doesn't work together with mems accelerat

Post by Giovanni »

Probably there was a DMA conflict among two peripherals. The F4 has more peripherals than available DMA channels.

Assertions should have caught that.

Giovanni
Post Reply