Page 1 of 1

STM32F3Discovery : I2c interface with the MPL3115a2 sensor

Posted: Fri Mar 28, 2014 4:13 pm
by giazax
i everybody,
for my first project i want to use the MPL3115a2 sensor with the stm32f3discovery with the i2c protocol
After many attempts i see nothing..(the value of the pressure is always zero)...what's wrong with it?

the pin connections for the MPL21115a2 are :

PA10 for SDA i2c data
PA9 for SCL i2c clock

other pin to manage? (from the datasheet ...i think no)
for the i2c configuration i'm using the example of the lsm303dlhc sensor...

a code snippet is here :

Code: Select all

 //I2C Configuration Parameter.....from the lsm303dlhc example
static const I2CConfig i2cconfig = {
  STM32_TIMINGR_PRESC(8U)  | STM32_TIMINGR_SCLDEL(3U) | STM32_TIMINGR_SDADEL(3U) |
  STM32_TIMINGR_SCLH(3U)   | STM32_TIMINGR_SCLL(9U),
  0,
  0
};


void padInit(void){

//PAD SETTING
//PA10 is SDA
//PA9 is SCL
//miss something?
  palSetPadMode(GPIOA, GPIOA_PIN9 ,       /*  pad settings for I2CD1 SCL  */
                    PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN |
                    PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_FLOATING);

  palSetPadMode(GPIOA, GPIOA_PIN10,       /*  pad settings for I2CD1 SDA  */
                   PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN |
                   PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_FLOATING);
}

int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   * - Setting pin for gyroscope and accelerometer\magnetometer
   */
  halInit();
  chSysInit();
  padInit();

  /*
   * Initializes a serial-over-USB CDC driver.
   */
  sduObjectInit(&SDU1);
  sduStart(&SDU1, &serusbcfg);

  /*
   * Activates the USB driver and then the USB bus pull-up on D+.
   * Note, a delay is inserted in order to not have to disconnect the cable
   * after a reset.
   */
  usbDisconnectBus(serusbcfg.usbp);
  chThdSleepMilliseconds(1500);
  usbStart(serusbcfg.usbp, &usbcfg);
  usbConnectBus(serusbcfg.usbp);

  int status;
  float pressure;

  while (TRUE) {

       palSetPad(GPIOE, GPIOE_LED3_RED);
       chThdSleepMilliseconds(125);
       palClearPad(GPIOE, GPIOE_LED3_RED);
       chThdSleepMilliseconds(125);

      //set active the power mode of the sensor...all the address are correct...
       i2cAcquireBus(&I2CD2);
      status = SetPowerMode(&I2CD2, MPL3115A2_DEFAULT_ADDR, ACTIVE);
       i2cReleaseBus (&I2CD2);

      //read the pressure
       i2cAcquireBus(&I2CD2);
       i2cStart(&I2CD2, &i2cconfig);
       ReadPressure(&I2CD2, MPL3115A2_DEFAULT_ADDR,&pressure);
       i2cStop(&I2CD2);
       i2cReleaseBus (&I2CD2);
     
      chprintf(chp, "MPL3115A2 pressure = %d \n" , pressure);       

      //set in stanby the sensor
      i2cAcquireBus(&I2CD2);
      SetPowerMode(&I2CD2, MPL3115A2_DEFAULT_ADDR, STANDBY);
      i2cReleaseBus (&I2CD2);

      }
  }


thanks for any suggestion

giancarlo

Re: STM32F3Discovery : I2c interface with the MPL3115a2 sens

Posted: Fri Mar 28, 2014 8:51 pm
by Giovanni
Hi, see if this can help: http://www.chibios.org/dokuwiki/doku.ph ... e_shooting

Please use the support forum if you are going to ask questions, this forum is for finished projects you want to share.

Giovanni

Re: STM32F3Discovery : I2c interface with the MPL3115a2 sens

Posted: Sat Apr 26, 2014 12:59 am
by Vic20
There is a ST aplication to configure the I2C timings:

http://www.st.com/web/en/catalog/tools/PF258335

If I where in your case I would try to read the WHO_AM_I register (0x0C) to check if the I2C communication works Ok.

If you are sure that the bus link work yoy have one less thing to worry about.

You can also test your sensor with my recently posted F3 Gizmo ;)
viewtopic.php?f=8&t=1877

Best regards
Vicente

Re: STM32F3Discovery : I2c interface with the MPL3115a2 sens

Posted: Sat Apr 26, 2014 11:53 am
by alan5
I've recently interfaced the MPL3115A2 with a STM32L152. I'm going to assume your I2CConfig is all specific to the F3 since its pretty different form my setup:

Code: Select all

void initialiseSensorHw(void)
{
    /* I2C for sensors */
    palSetPadMode(I2C_PORT, I2C_SDA, PAL_MODE_ALTERNATE(4) |
                                     PAL_STM32_OTYPE_OPENDRAIN |
                                     PAL_STM32_OSPEED_LOWEST);
    palSetPadMode(I2C_PORT, I2C_SCL, PAL_MODE_ALTERNATE(4) |
                                     PAL_STM32_OTYPE_OPENDRAIN |
                                     PAL_STM32_OSPEED_LOWEST);

    i2cObjectInit(&I2C_DRIVER);

    i2cConfig.op_mode = OPMODE_I2C;
    i2cConfig.duty_cycle = STD_DUTY_CYCLE;
    i2cConfig.clock_speed = 100000;
}


In your code you call setPowerMode() without I2C start? As a reference you could consider my mplOneShotReadBarometer() function here: https://github.com/alanbarr/ChibiOS_Sensors
Do you check the return codes from the ChibiOS I2C functions are RDY_OK?