Пример #1
0
void mma7660_init(void)
{
  int32_t val;

  i2c_enable();

  buf[0] = INTSU;
  buf[1] = 0x00;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = MODE;
  buf[1] = 0x01;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = SR;
  buf[1] = 0x1F;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = PDET;
  buf[1] = 0xFF;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = PD;
  buf[1] = 0xFF;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  i2c_disable();
}
Пример #2
0
int sht21_humidity(void)
{
  int val;

  i2c_enable();
  /* For for about 15ms before the SHT11 can be used */
  sht21_timer = RTIMER_NOW();
  while(RTIMER_CLOCK_LT(RTIMER_NOW(), sht21_timer + (RTIMER_SECOND/1000)*15));

  buf[0] = 0xe5;
  i2c_transmitinit(0x40, 1, buf);
  while(!i2c_transferred()) ;

  /* Wait for measurement about 85ms */
  sht21_timer = RTIMER_NOW();
  while(RTIMER_CLOCK_LT(RTIMER_NOW(), sht21_timer + (RTIMER_SECOND/1000)*85));

  i2c_receiveinit(0x40, 3, buf);
  while(!i2c_transferred()) ;

  val = (int)(buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);

//  i2c_disable();
  /* return relative humidity * 100 (0.04 % accuracy) */
  return (-6.0 + (125.0*((val>>16)&0x0000fffc))/0x10000)*100;
}
Пример #3
0
void resetFIFO() {
  uint8_t user_config= 0b01000100;
  uint8_t cmd[] = {MPU9150_USER_CTRL, user_config} ;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("User control configured!\n");
}
Пример #4
0
/*############################## FIFO COUNT FUNCTION ##################################  */
uint16_t mpu9150_FIFO_count(uint8_t *buffer) {
	prepareRead(MPU9150_FIFO_COUNT_H);

	i2c_receiveinit( MPU9150_I2C_ADDR, 2, buffer ) ;
	while(!i2c_transferred()) /* Wait for transfer */ ;
	
	uint16_t fifo_count = ((buffer[0]<<8) | buffer[1]);
        printf("FIFO count in bits: %i and in bytes %i\n", fifo_count, fifo_count/8);
        return fifo_count / 8;
}
Пример #5
0
/*############################## GET DATA FUNCTION ##################################  */
void mpu9150_get_data(uint8_t *buffer) {
  // Poll number of new measurements to read from the FIFO:
  //uint8_t set[] = {MPU9150_FIFO_COUNT_H,0};
  prepareRead(MPU9150_FIFO_R_W);
  
  i2c_receiveinit( MPU9150_I2C_ADDR, 2, buffer ) ;
  while(!i2c_transferred());
  
  uint16_t fifo_count = ((buffer[0]<<8) | buffer[1]);
}
Пример #6
0
/*############################## INIT FUNCTION ###################################  */
void mpu9150_init(void) {
  printf("Initialising power management...\n");
  // Chip starts in sleep mode and we need to turn it on: (REG:107)
  uint8_t cmd[] = {MPU9150_PWR_MGMT_1, 0x00} ;


  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("Power management initialised!\n");
  file = fopen("testfile","w");
}
Пример #7
0
void mma7660_acc(int *x, int *y, int *z)
{
  int tmp;
  i2c_enable();

  /* Read data */
  buf[0] = XOUT;
  i2c_transmitinit(ADDR, 1, buf);
  while(!i2c_transferred()) ;
  i2c_receiveinit(ADDR, 3, buf);
  while(!i2c_transferred()) ;

  i2c_disable();

  tmp = (signed char)((buf[0] & 0x20) ? (buf[0] | 0xC0) : (buf[0] & 0x3F));
  *x = (tmp*150)/32;
  tmp = (signed char)((buf[1] & 0x20) ? (buf[1] | 0xC0) : (buf[1] & 0x3F));
  *y = (tmp*150)/32;
  tmp = (signed char)((buf[2] & 0x20) ? (buf[2] | 0xC0) : (buf[2] & 0x3F));
  *z = (tmp*150)/32;
}
Пример #8
0
/*######################### GET DATA FUNCTION WITH SIZE ############################## */
void mpu9150_get_data_size(uint8_t *buffer, int size) {
  // Poll number of new measurements to read from the FIFO:
  prepareRead(MPU9150_FIFO_R_W);
  
  //size = 2;
  size = 18;
  
  printf("Prepared FIFO for reading %i bytes...\n",size);
  
  i2c_receiveinit( MPU9150_I2C_ADDR, size, buffer ) ;
  while(!i2c_transferred());
  
  uint16_t fifo_count = ((buffer[0]<<8) | buffer[1]);
}
Пример #9
0
/*############################## START FUNCTION ##################################  */
void mpu9150_start(void) {
  printf("Setting sample rate...\n");
  /*=== Set sample rate divider: (REG:25) ===*/
  uint8_t cmd[] = {MPU9150_SMPLRT_DIV, SAMPLE_RATE_CONFIG};
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("Sample rate set to: %i!\n", SAMPLE_RATE_CONFIG);
  
  /*=== Set up FSYNC and dlpf: (REG:26) ===*/
  printf("Setting fsync dlpf config...\n");
  uint8_t fsync_dlpf_config = 0b00000011; //FSYNC disabled and dlpf_cfg set to 3.
  cmd[0] = MPU9150_CONFIG;
  cmd[1] = fsync_dlpf_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("FSYNC rate set!\n");
  /*
   * With current dlpf_cfg (3) the sample rate of the accelerometer is 1kHz and the 
   * gyroscope is 1kHz too. The sample rate divider has been set to 20 (register 20).
   * Sample rate = Gyroscope output rate / (1 + sample rate divider)
   *             =                  1000 / (1 + 19)
   *             =                       50Hz
   */
  /* Set up the fifo_config parameter and enable sensors if they are defined in the header file: */
  uint8_t fifo_config = 0b00000000;
  /*=== Trigger the gyroscopes self-test and set the scale range: (REG:27) ===*/
  if (INV_XYZ_GYRO) {
    fifo_config = fifo_config | 0b01110000;
    printf("Starting up gyroscope...\n");
    //Self test xyz and set full scale range to +-2000s:
    uint8_t gyro_config = 0b11111000;
    cmd[0] = MPU9150_GYRO_CONFIG;
    cmd[1] = gyro_config;
    i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
    while(!i2c_transferred()) /* Wait for transfer */ ;
    printf("Gyroscope started!\n");
  }
 
  /*=== Trigger the accelorometer self-test and set the scale range: (REG:28) ===*/
  if (INV_XYZ_ACCEL) {
     fifo_config = fifo_config | 0b00001000;
     printf("Starting up accelorometer...\n");
     uint8_t accel_config = 0b11111000; //self test xyz, with full scale range +-16g and reset hpf
     cmd[0] = MPU9150_ACCEL_CONFIG;
     cmd[1] = accel_config;
     i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
     while(!i2c_transferred()) /* Wait for transfer */ ;
     printf("Accelorometer started!\n");
  }
  
  /*=== Enable Temp on FIFO: ===*/
  if (INV_TEMP) {
    fifo_config = fifo_config | 0b10000000;
    printf("Adding thermometer to FIFO...\n");
  }
  
  if (INV_XYZ_ACCEL) {
        packet_size += 6;}
    if (INV_TEMP) {
        packet_size += 2;}
    if (INV_XYZ_GYRO) {
        packet_size += 6;}
    
    printf("packet size: %i\n", packet_size);
    //exit(0);
  
  /*
   * SKIPPING FREE FALL, MOTION DETECTION, ETC. CONFIGURATION
   */
  
   /*=== Setup FIFO queue: (REG:35) ===
    Bit order [1: Temp, 2-4: Gyro{x,y.z}, 5: Accel, 6-8: SLV{2,1,0}] */
  int BUF_SIZE = 9;
  char buffer[BUF_SIZE];
  buffer[BUF_SIZE - 1] = '\0';
  printf("Configuring FIFO with parameter %s...\n", int2bin(fifo_config, buffer, BUF_SIZE-1));
  cmd[0] = MPU9150_FIFO_EN;
  cmd[1] = fifo_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("FIFO configured!\n");
  
  /* Setup i2c master control (REG:36)
     First bit controls multi-master capability (not needed for us I think)
     Second bit controls wait for external sensors (to keep data in sync), which is not needed by us, because we do ot use external sensors
     Third bit controls FIFO_EN for salve 3 (other slaves controled in REG35)
     Fourth bit controls slave read and write mode (don't really know waht the implications of the different write modes are, think it's not significant for us?)
     Last four bits control the i2c master clock. We set the 8mhz clock divider to 19, so set this to 12 1100??? */
  printf("Setting up I2C master controller...\n");
  uint8_t i2cmc_config= 0b00001101;
  cmd[0] = MPU9150_I2C_MST_CTRL;
  cmd[1] = i2cmc_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("I2C master configured!\n");
  
  /* Setup user control */
  printf("Setting up user control...\n");
  uint8_t user_config= 0b01000100;
  cmd[0] = MPU9150_USER_CTRL;
  cmd[1] = user_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("User control configured!\n");
  
  /*
   * SLAVE CONTROL REGISTERS (37-53) WE DON'T NEED TO CONFIGURE (I THINK). 54 IS THE I2C MASTER CONTROLLER,
   * ALSO NOT USED BY US, 55 IS USED TO CONFIGURE INTERRUPTS (MAYBE WE SHOULD CONFIGURE), 56 ENABLES
   * INTERRUPTS, MAYBE WE SHOULD SET THE FIFO INTERRUPT TO ONE, THEN WAIT FOR THE INTERRUPT TO OCCOUR
   * AND THEN READ ALL SENSORS IN ONE GO AND WAIT FOR NEXT INTERRUPT. REG 58 CONTAINS THE INTERRUPT STATA.
   * 59-96 CONTAINS THE LATEST VALUES FROM THE SENOSRS, BUT WE READ THEM FROM THE FIFO INSTEAD. REG 97 IS
   * MOTION DETETION STATUS, 98-102 HAS THE DATAOUT FOR THE IC2 SLAVES, 103 CONTROLS IC2 MASTER DEALY, 104
   * CAN BE USED TO RESET THE PATHS FOR THE INTERNAL SENSORS, 105 IS MOTION DETECTION CONTROL, 106 USER CONTROL,
   * BUT WE ENABLE THE FIFO IN A DIFFERENT REGISTER, SO SON'T NEED TO REENABLE IT HERE. 107 AND 108 ARE
   * POWER MANAGEMENT.
   * NOW TO GET DATA, WE SHOULD LOOK AT REGISTERS 114 AND 115 TO GET THE COUNT OF NEW MEASUREMENTS AND THEN
   * READ THEM FROM THE FIFO QUEUE:
   */
}
Пример #10
0
/*############################## STOP FUNCTION ###################################  */
void mpu9150_stop(void) {
  uint8_t cmd[] = {MPU9150_PWR_MGMT_1, 0x40} ;

  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
  while(!i2c_transferred()) /* Wait for transfer */ ;
}
Пример #11
0
/*
 * Function to return the WHO_AM_I value
 * of the mpu9150 chip.
*/
void mpu9150_who_am_i(uint8_t *buffer) {
   prepareRead(MPU9150_WHO_AM_I);
     
   i2c_receiveinit( MPU9150_I2C_ADDR, 2, buffer ); //need to receive two bytes, otherwise we get ERROR I2C: Arbitration lost
   while(!i2c_transferred()); /* Wait for transfer */
}
Пример #12
0
/*################ FUNCTION TO CALL BEFORE READING DATA FROM A REGISTER ##############  */
void prepareRead(uint8_t slv_adr) {
  i2c_transmitinit( MPU9150_I2C_ADDR, 1, &slv_adr ) ;
  while(!i2c_transferred()) /* Wait for transfer */ ;
}
Пример #13
0
void wait_for_transfer()
{
  while(!i2c_transferred()) /* Wait for transfer */ ;
  clock_delay_msec(1);
}