STM32F3Discovery : I2c interface with the MPL3115a2 sensor

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

Moderators: RoccoMarco, barthess

giazax
Posts: 12
Joined: Tue Nov 26, 2013 4:43 pm

STM32F3Discovery : I2c interface with the MPL3115a2 sensor

Postby giazax » Fri Mar 28, 2014 4:13 pm

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

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32F3Discovery : I2c interface with the MPL3115a2 sens

Postby Giovanni » Fri Mar 28, 2014 8:51 pm

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

User avatar
Vic20
Posts: 15
Joined: Fri Apr 25, 2014 7:45 pm
Location: Barcelona, Spain
Contact:

Re: STM32F3Discovery : I2c interface with the MPL3115a2 sens

Postby Vic20 » Sat Apr 26, 2014 12:59 am

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

alan5
Posts: 24
Joined: Mon Nov 11, 2013 5:06 pm
Has thanked: 1 time
Been thanked: 1 time

Re: STM32F3Discovery : I2c interface with the MPL3115a2 sens

Postby alan5 » Sat Apr 26, 2014 11:53 am

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?


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 9 guests