// Setup the imu
LSM9DS0_t* imu_setup(uint8_t gAddr, uint8_t xmAddr)
{
	LSM9DS0_t* imu;

	imu->gx = 0;
	imu->gy = 0;
	imu->gz = 0;

	imu->ax = 0;
	imu->ay = 0;
	imu->az = 0;

	imu->mx = 0;
	imu->my = 0;
	imu->mz = 0;

	imu->temperature = 0;

	imu->gScale = G_SCALE_245DPS;
	imu->aScale = A_SCALE_4G;
	imu->mScale = M_SCALE_2GS;

	imu->gRes = 0;
	imu->aRes = 0;
	imu->mRes = 0;

	// setting up the i2c
	imu->gyro = mraa_i2c_init(1);
	mraa_i2c_address(imu->gyro, gAddr);
	imu->xm = mraa_i2c_init(1);
	mraa_i2c_address(imu->xm, xmAddr);

	return imu;
}
Ejemplo n.º 2
0
void MPU9250_I2C_Config(uint8_t sen_count)
{
	for(i = 0; i < sen_count; i++)
	{
    	printf("%s------------------------------------------------------------%s\n", ANSI_COLOUR_WHITE_BOLD, ANSI_COLOUR_RESET);
    
		//Select the Multiplexer Channel
		MPU9250_MUX_Select(i);
    
    	//Select Text Colour
    	MPU9250_Text_Colour(i);
		
		//Reset all the Registers
  		mraa_i2c_address(MPU9250_i2c, MPU_ADDR);
		MPU9250_I2C_Write(PWR_MGMT_1, PWR_RESET);
		printf("%s[  SEN%d  ] Reset %s\n", Ansi_Colour, i, ANSI_COLOUR_RESET);
		usleep(100000); //100ms delay
	
		mraa_i2c_address(MPU9250_i2c, MPU_ADDR);
 		MPU9250_I2C_Write(PWR_MGMT_1, DEVICE_ON);
		printf("%s[  SEN%d  ] Switched ON %s\n", Ansi_Colour, i, ANSI_COLOUR_RESET);
		usleep(100000); //100ms delay
	
		mraa_i2c_address(MPU9250_i2c, MPU_ADDR);
  		uint8_t data = mraa_i2c_read_byte_data(MPU9250_i2c, WHO_AM_I); //Should return 0x71
  		printf("%s[  SEN%d  ] Who am I: 0x%x %s\n", Ansi_Colour, i, data, ANSI_COLOUR_RESET);
		usleep(100000); //100ms delay
		
		if (data != 0x71) 
		{ 
			// Read "WHO_AM_I" register
    		printf("Error reading [  SEN%d  ]!", i);
    		while (1);
  		}
	
 		MPU9250_I2C_Write(SMPRT_DIV, SAMPLE_RATE);
	
 		MPU9250_I2C_Write(CONFIG, DLPF_CFG);

 		//Set the Gyroscope Scale to 250°/s
 		MPU9250_I2C_Write(GYRO_CONFIG, GYRO_250);
	
		//Set the Accelerometer Scale to 2G
 		MPU9250_I2C_Write(ACCEL_CONFIG, ACCEL_2G);
    		
		printf("%s[  SEN%d  ] Ready %s\n", Ansi_Colour, i, ANSI_COLOUR_RESET);
    
    	printf("%s------------------------------------------------------------%s\n", ANSI_COLOUR_WHITE_BOLD, ANSI_COLOUR_RESET);
    
		sleep(1); //1s delay
	}
	printf("%s\n\tInitialization Complete: All Systems are GO!!!%s\n\n", ANSI_COLOUR_WHITE_BOLD, ANSI_COLOUR_RESET);
}
Ejemplo n.º 3
0
mraa_result_t mag_init(int bus) {
  mag_context = mraa_i2c_init(bus);
  mraa_i2c_address(mag_context, HMC5883L_I2C_ADDR);
  mraa_result_t result;

  mag_rx_tx_buf[0] = HMC5883L_CONF_REG_B;
  mag_rx_tx_buf[1] = GA_1_3_REG;
  result = mraa_i2c_write(mag_context, mag_rx_tx_buf, 2);
  if (result != MRAA_SUCCESS) {
    printError("unable to write to compass (7)");
    return result;
  }


  mag_rx_tx_buf[0] = HMC5883L_MODE_REG;
  mag_rx_tx_buf[1] = HMC5883L_CONT_MODE;
  result = mraa_i2c_write(mag_context, mag_rx_tx_buf, 2);
  if (result != MRAA_SUCCESS) {
    printError("unable to read from compass (8)");
    return result;
  }

  mag_update();
  return MRAA_SUCCESS;
}
Ejemplo n.º 4
0
void sendi2c(unsigned int address, unsigned int reg, unsigned char tosend)
{
	mraa_i2c_address(i2c, address);
  rx_tx_buf[0] = reg;
  rx_tx_buf[1] = tosend;
  mraa_i2c_write(i2c, rx_tx_buf, 2);
}
Ejemplo n.º 5
0
void MPU9250_I2C_Read(uint8_t address, uint8_t *value)
{	
	//Set ALS Device Address
	mraa_i2c_address(MPU9250_i2c, MPU_ADDR);
	
	//Write Command and Read Data
	*value = mraa_i2c_read_byte_data(MPU9250_i2c, address);
}
Ejemplo n.º 6
0
void MPU9250_I2C_Write(uint8_t address, uint8_t value)
{	
	//Set MPU Device Address
	mraa_i2c_address(MPU9250_i2c, MPU_ADDR);
	
	//Write Command and Data
	mraa_i2c_write_byte_data(MPU9250_i2c, value, address);
}
Ejemplo n.º 7
0
int
main(int argc, char **argv)
{
    mraa_init();
    float direction = 0;
    int16_t x = 0, y = 0, z = 0;
    char rx_tx_buf[MAX_BUFFER_LENGTH];

//! [Interesting]
    mraa_i2c_context i2c;
    i2c = mraa_i2c_init(0);

    mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
    rx_tx_buf[0] = HMC5883L_CONF_REG_B;
    rx_tx_buf[1] = GA_1_3_REG;
    mraa_i2c_write(i2c, rx_tx_buf, 2);
//! [Interesting]

    mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
    rx_tx_buf[0] = HMC5883L_MODE_REG;
    rx_tx_buf[1] = HMC5883L_CONT_MODE;
    mraa_i2c_write(i2c, rx_tx_buf, 2);

    for(;;) {
        mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
        mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG);

        mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
        mraa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE);

        x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ;
        z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ;
        y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Y_LSB_REG] ;

        //scale and calculate direction
        direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG);

        //check if the signs are reversed
        if (direction < 0)
            direction += 2 * M_PI;

        printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG, z * SCALE_0_92_MG) ;
        printf("Heading : %f\n", direction * 180/M_PI) ;
    }
}
Ejemplo n.º 8
0
char readi2c(int address, int reg, int count)
{
	int i=0;
	mraa_i2c_address(i2c, address);
  for (i = 0; i < count; i++) {
      rx_tx_buf[i] = mraa_i2c_read_byte_data(i2c, reg+i);
  }
	if(count==1)return rx_tx_buf[0];
	return 0;
}
Ejemplo n.º 9
0
/**
 *Set the i2c bus and address of where the data will be written to.
 *
 *@param i2c context, pass in the i2c context that represents
 *a master on the i2c bus.
 *@param addr The address of the I2C device
 *
*/
static int set_address(mraa_i2c_context i2c, int addr) {
	mraa_result_t mraa_result = mraa_i2c_address(i2c, addr);

    if (mraa_result != MRAA_SUCCESS)
    {
        DEBUG("set_address(): mraa_i2c_address() failure, error code: %d\n", mraa_result);
        mraa_result_print(mraa_result);
    }

    return mraa_result;
}
Ejemplo n.º 10
0
int main()
{
    uint8_t m[2];
    m[0]=8;
    m[1]=255;
    mraa_init(); // can we put it in the beginning. Avoid repeating definition.
    mraa_i2c_context pwm12;
    pwm12 = mraa_i2c_init(2);
    mraa_i2c_address(pwm12,12);
    while(1)
    mraa_i2c_write(pwm12,m,2);
    return 0;
}
Ejemplo n.º 11
0
void MPU9250_I2C_Init()
{
  if (MPU9250_i2c == NULL) 
	{
		printf("MPU9250 I2C initialization failed, exit...\n");
		exit(1);
	}
	
	printf("MPU9250 I2C initialized successfully\n");
	
	mraa_i2c_address(MPU9250_i2c, MPU_ADDR);
	printf("MPU9250 I2C Address set to 0x%x\n", MPU_ADDR);
}
Ejemplo n.º 12
0
mrb_value
mrb_mraa_i2c_address(mrb_state *mrb, mrb_value self){
    mraa_i2c_context i2c;
    mrb_int addr;

    mraa_result_t result;

    Data_Get_Struct(mrb, self, &mrb_mraa_i2c_ctx_type, i2c);

    mrb_get_args(mrb, "i", &addr);
    result = mraa_i2c_address(i2c, addr);

    return mrb_fixnum_value(result);
}
Ejemplo n.º 13
0
mraa_result_t gyro_init(int bus) {
  //init bus and reset chip
  gyro_context = mraa_i2c_init(bus);
  mraa_i2c_address(gyro_context, ITG3200_I2C_ADDR);
  mraa_result_t result;

  gyro_buffer[0] = ITG3200_PWR_MGM;
  gyro_buffer[1] = ITG3200_RESET;
  result = mraa_i2c_write(gyro_context, gyro_buffer, 2);
  if (result != MRAA_SUCCESS) {
    printError("unable to write to gyro (4)");
    return result;
  }

  gyro_calibrate();
  gyro_update();
  return MRAA_SUCCESS;
}
Ejemplo n.º 14
0
Archivo: ecezo.c Proyecto: g-vidal/upm
// i2c ublox init
ecezo_context ecezo_i2c_init(unsigned int bus, uint8_t addr)
{
    // make sure MRAA is initialized
    int mraa_rv;
    if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
    {
        printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
        return NULL;
    }

    ecezo_context dev =
        (ecezo_context)malloc(sizeof(struct _ecezo_context));

    if (!dev)
        return NULL;

    // zero out context
    memset((void *)dev, 0, sizeof(struct _ecezo_context));

    // initialize the MRAA contexts

    if (!(dev->i2c = mraa_i2c_init(bus)))
    {
        printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__);
        ecezo_close(dev);
        return NULL;
    }

    if (mraa_i2c_address(dev->i2c, addr))
    {
        printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__);
        ecezo_close(dev);
        return NULL;
    }

    if (generic_init(dev))
    {
        printf("%s: generic_init() failed.\n", __FUNCTION__);
        ecezo_close(dev);
        return NULL;
    }

    return dev;
}
Ejemplo n.º 15
0
Archivo: mpu.c Proyecto: Liwsh/Robot
void main(){
	devAddr = MPU6050_DEFAULT_ADDRESS;
	i2c = mraa_i2c_init(0);
	/** Power on and prepare for general usage.
	 * This will activate the device and take it out of sleep mode (which must be done
	 * after start-up). This function also sets both the accelerometer and the gyroscope
	 * to their most sensitive settings, namely +/- 2g and +/- 250 degrees/sec, and sets
	 * the clock source to use the X Gyro for reference, which is slightly better than
	 * the default internal clock source.
	 */
	mraa_i2c_address(i2c,devAddr);
	mraa_i2c_write_byte_data(i2c,MPU6050_CLOCK_PLL_XGYRO,MPU6050_RA_PWR_MGMT_1);
	mraa_i2c_write_byte_data(i2c,MPU6050_RA_GYRO_CONFIG,MPU6050_GYRO_FS_250);
	mraa_i2c_write_byte_data(i2c,MPU6050_RA_ACCEL_CONFIG,MPU6050_ACCEL_FS_2);
	int ev1=every(20,getangle,-1);
	int ev2=every(50,print,-1);
while(1){
	timeupdate();
}
}
Ejemplo n.º 16
0
mraa_result_t
i2c_set(int bus, uint8_t device_address, uint8_t register_address, uint8_t data)
{
    mraa_result_t status = MRAA_SUCCESS;
    mraa_i2c_context i2c = mraa_i2c_init(bus);
    if (i2c == NULL) {
        return MRAA_ERROR_NO_RESOURCES;
    }
    status = mraa_i2c_address(i2c, device_address);
    if (status != MRAA_SUCCESS) {
        fprintf(stderr, "Could not set i2c device address\n");
        goto i2c_set_exit;
    }
    status = mraa_i2c_write_byte_data(i2c, data, register_address);
    if (status != MRAA_SUCCESS) {
        fprintf(stderr, "Could not write to i2c register. Status = %d\n", status);
        goto i2c_set_exit;
    }
i2c_set_exit:
    mraa_i2c_stop(i2c);
    return status;
}
Ejemplo n.º 17
0
mraa_result_t
i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data)
{
    mraa_result_t status = MRAA_SUCCESS;
    mraa_i2c_context i2c = mraa_i2c_init(bus);
    if (i2c == NULL) {
        return MRAA_ERROR_NO_RESOURCES;
    }
    status = mraa_i2c_address(i2c, device_address);
    if (status != MRAA_SUCCESS) {
        goto i2c_get_exit;
    }
    status = mraa_i2c_write_byte(i2c, register_address);
    if (status != MRAA_SUCCESS) {
        goto i2c_get_exit;
    }
    status = mraa_i2c_read(i2c, data, 1) == 1 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
    if (status != MRAA_SUCCESS) {
        goto i2c_get_exit;
    }
i2c_get_exit:
    mraa_i2c_stop(i2c);
    return status;
}
Ejemplo n.º 18
0
mraa_i2c_context gyro_init()
{
	mraa_i2c_context gyro;
	gyro = mraa_i2c_init(1);

	if (gyro == NULL) {
		fprintf(stderr, "Failed to initialize I2C.\n");
		exit(1);
	}
	mraa_i2c_address(gyro, GYRO_ADDR);
	
	uint8_t gyro_id = mraa_i2c_read_byte_data(gyro, WHO_AM_I_G);
	if (gyro_id != 0xD4) {
		fprintf(stderr, "Gyroscope ID does not match.\n");
	}
	
	/* CTRL_REG1_G sets output data rate, bandwidth, power-down and enables
	Bits[7:0]: DR1 DR0 BW1 BW0 PD Zen Xen Yen
	DR[1:0] - Output data rate selection
		00=95Hz, 01=190Hz, 10=380Hz, 11=760Hz
	BW[1:0] - Bandwidth selection (sets cutoff frequency)
		 Value depends on ODR. See datasheet table 21.
	PD - Power down enable (0=power down mode, 1=normal or sleep mode)
	Zen, Xen, Yen - Axis enable (o=disabled, 1=enabled)	*/
	//gWriteByte(CTRL_REG1_G, 0x0F); // Normal mode, enable all axes
	mraa_i2c_write_byte_data(gyro, 0x0F, CTRL_REG1_G);		

	
	/* CTRL_REG2_G sets up the HPF
	Bits[7:0]: 0 0 HPM1 HPM0 HPCF3 HPCF2 HPCF1 HPCF0
	HPM[1:0] - High pass filter mode selection
		00=normal (reset reading HP_RESET_FILTER, 01=ref signal for filtering,
		10=normal, 11=autoreset on interrupt
	HPCF[3:0] - High pass filter cutoff frequency
		Value depends on data rate. See datasheet table 26.
	*/
	//gWriteByte(CTRL_REG2_G, 0x00); // Normal mode, high cutoff frequency
	mraa_i2c_write_byte_data(gyro, 0x00, CTRL_REG2_G);		

	
	/* CTRL_REG3_G sets up interrupt and DRDY_G pins
	Bits[7:0]: I1_IINT1 I1_BOOT H_LACTIVE PP_OD I2_DRDY I2_WTM I2_ORUN I2_EMPTY
	I1_INT1 - Interrupt enable on INT_G pin (0=disable, 1=enable)
	I1_BOOT - Boot status available on INT_G (0=disable, 1=enable)
	H_LACTIVE - Interrupt active configuration on INT_G (0:high, 1:low)
	PP_OD - Push-pull/open-drain (0=push-pull, 1=open-drain)
	I2_DRDY - Data ready on DRDY_G (0=disable, 1=enable)
	I2_WTM - FIFO watermark interrupt on DRDY_G (0=disable 1=enable)
	I2_ORUN - FIFO overrun interrupt on DRDY_G (0=disable 1=enable)
	I2_EMPTY - FIFO empty interrupt on DRDY_G (0=disable 1=enable) */
	// Int1 enabled (pp, active low), data read on DRDY_G:
	//gWriteByte(CTRL_REG3_G, 0x88);
	mraa_i2c_write_byte_data(gyro, 0x88, CTRL_REG3_G);		

	
	/* CTRL_REG4_G sets the scale, update mode
	Bits[7:0] - BDU BLE FS1 FS0 - ST1 ST0 SIM
	BDU - Block data update (0=continuous, 1=output not updated until read
	BLE - Big/little endian (0=data LSB @ lower address, 1=LSB @ higher add)
	FS[1:0] - Full-scale selection
		00=245dps, 01=500dps, 10=2000dps, 11=2000dps
	ST[1:0] - Self-test enable
		00=disabled, 01=st 0 (x+, y-, z-), 10=undefined, 11=st 1 (x-, y+, z+)
	SIM - SPI serial interface mode select
		0=4 wire, 1=3 wire */
	//gWriteByte(CTRL_REG4_G, 0x00); // Set scale to 245 dps
	mraa_i2c_write_byte_data(gyro, 0x00, CTRL_REG4_G);
	
	/* CTRL_REG5_G sets up the FIFO, HPF, and INT1
	Bits[7:0] - BOOT FIFO_EN - HPen INT1_Sel1 INT1_Sel0 Out_Sel1 Out_Sel0
	BOOT - Reboot memory content (0=normal, 1=reboot)
	FIFO_EN - FIFO enable (0=disable, 1=enable)
	HPen - HPF enable (0=disable, 1=enable)
	INT1_Sel[1:0] - Int 1 selection configuration
	Out_Sel[1:0] - Out selection configuration */
	//gWriteByte(CTRL_REG5_G, 0x00);
	mraa_i2c_write_byte_data(gyro, 0x00, CTRL_REG5_G);
	
	return gyro;
}
Ejemplo n.º 19
0
lcm1602_context lcm1602_i2c_init(int bus, int address, bool is_expander,
                                 uint8_t num_columns, uint8_t num_rows)
{
    lcm1602_context dev =
        (lcm1602_context)malloc(sizeof(struct _lcm1602_context));

    if (!dev)
        return NULL;

    memset((void *)dev, 0, sizeof(struct _lcm1602_context));

    // make sure MRAA is initialized
    int mraa_rv;
    if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
    {
        printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
        lcm1602_close(dev);
        return NULL;
    }

    // initialize the MRAA context

    if (!(dev->i2c = mraa_i2c_init(bus)))
    {
        printf("%s: mraa_i2c_init failed.\n", __FUNCTION__);
        lcm1602_close(dev);

        return NULL;
    }

    // now check the address...
    if (mraa_i2c_address(dev->i2c, address) != MRAA_SUCCESS)
    {
        printf("%s: mraa_i2c_address failed.\n", __FUNCTION__);

        lcm1602_close(dev);

        return NULL;
    }

    dev->isI2C = true;
    dev->backlight = HD44780_BACKLIGHT;
    dev->columns = num_columns;
    dev->rows = num_rows;

    // if we are not dealing with an expander we will only initialize
    // the I2C context and bail, leaving it up to the caller to handle
    // further communications (like JHD1313M1)

    if (!is_expander)
        return dev;

    upm_delay_us(50000);
    lcm1602_backlight_on(dev, true);
    upm_delay_us(100000);

    // try to put us into 4 bit mode
    write4bits(dev, 0x03 << 4);
    upm_delay_us(4500);

    write4bits(dev, 0x30);
    upm_delay_us(4500);

    write4bits(dev,0x30);
    upm_delay_us(150);

    // Put us into 4 bit mode, for realz yo.
    write4bits(dev, 0x20);

    // Set number of lines
    lcm1602_command(dev, HD44780_FUNCTIONSET | 0x0f);

    // default display control
    dev->displayControl = HD44780_DISPLAYON | HD44780_CURSOROFF
        | HD44780_BLINKOFF;

    lcm1602_command(dev, HD44780_DISPLAYCONTROL | dev->displayControl);
    upm_delay_us(2000);
    lcm1602_clear(dev);

    // Set entry mode.
    dev->entryDisplayMode = HD44780_ENTRYLEFT | HD44780_ENTRYSHIFTDECREMENT;
    lcm1602_command(dev, HD44780_ENTRYMODESET | dev->entryDisplayMode);

    lcm1602_home(dev);

    return dev;
}
Ejemplo n.º 20
0
mraa_i2c_context accel_init()
{
	mraa_i2c_context accel;
	accel = mraa_i2c_init(1);

	if (accel == NULL) {
		fprintf(stderr, "Failed to initialize I2C.\n");
		exit(1);
	}
	mraa_i2c_address(accel, XM_ADDR);
	
	uint8_t accel_id = mraa_i2c_read_byte_data(accel, WHO_AM_I_XM);
	if (accel_id != 0x49) {
		fprintf(stderr, "Accelerometer/Magnetometer ID does not match.\n");
	}
	
	/* CTRL_REG0_XM (0x1F) (Default value: 0x00)
	Bits (7-0): BOOT FIFO_EN WTM_EN 0 0 HP_CLICK HPIS1 HPIS2
	BOOT - Reboot memory content (0: normal, 1: reboot)
	FIFO_EN - Fifo enable (0: disable, 1: enable)
	WTM_EN - FIFO watermark enable (0: disable, 1: enable)
	HP_CLICK - HPF enabled for click (0: filter bypassed, 1: enabled)
	HPIS1 - HPF enabled for interrupt generator 1 (0: bypassed, 1: enabled)
	HPIS2 - HPF enabled for interrupt generator 2 (0: bypassed, 1 enabled)   */
	//xmWriteByte(CTRL_REG0_XM, 0x00);
	mraa_i2c_write_byte_data(accel, 0x00, CTRL_REG0_XM);		
	/* CTRL_REG1_XM (0x20) (Default value: 0x07)
	Bits (7-0): AODR3 AODR2 AODR1 AODR0 BDU AZEN AYEN AXEN
	AODR[3:0] - select the acceleration data rate:
		0000=power down, 0001=3.125Hz, 0010=6.25Hz, 0011=12.5Hz, 
		0100=25Hz, 0101=50Hz, 0110=100Hz, 0111=200Hz, 1000=400Hz,
		1001=800Hz, 1010=1600Hz, (remaining combinations undefined).
	BDU - block data update for accel AND mag
		0: Continuous update
		1: Output registers aren't updated until MSB and LSB have been read.
	AZEN, AYEN, and AXEN - Acceleration x/y/z-axis enabled.
		0: Axis disabled, 1: Axis enabled									 */	
	//xmWriteByte(CTRL_REG1_XM, 0x57); // 100Hz data rate, x/y/z all enabled
	mraa_i2c_write_byte_data(accel, 0x57, CTRL_REG1_XM);		
	
	//Serial.println(xmReadByte(CTRL_REG1_XM));
	/* CTRL_REG2_XM (0x21) (Default value: 0x00)
	Bits (7-0): ABW1 ABW0 AFS2 AFS1 AFS0 AST1 AST0 SIM
	ABW[1:0] - Accelerometer anti-alias filter bandwidth
		00=773Hz, 01=194Hz, 10=362Hz, 11=50Hz
	AFS[2:0] - Accel full-scale selection
		000=+/-2g, 001=+/-4g, 010=+/-6g, 011=+/-8g, 100=+/-16g
	AST[1:0] - Accel self-test enable
		00=normal (no self-test), 01=positive st, 10=negative st, 11=not allowed
	SIM - SPI mode selection
		0=4-wire, 1=3-wire													 */
	//xmWriteByte(CTRL_REG2_XM, 0x00); // Set scale to 2g
	mraa_i2c_write_byte_data(accel, 0x00, CTRL_REG2_XM);		
	
	/* CTRL_REG3_XM is used to set interrupt generators on INT1_XM
	Bits (7-0): P1_BOOT P1_TAP P1_INT1 P1_INT2 P1_INTM P1_DRDYA P1_DRDYM P1_EMPTY
	*/
	// Accelerometer data ready on INT1_XM (0x04)
	//xmWriteByte(CTRL_REG3_XM, 0x04);
	mraa_i2c_write_byte_data(accel, 0x04, CTRL_REG3_XM);		

	return accel;
}
Ejemplo n.º 21
0
Archivo: lis3dh.c Proyecto: g-vidal/upm
// Init
lis3dh_context
lis3dh_init(int bus, int addr, int cs)
{
    lis3dh_context dev = (lis3dh_context) malloc(sizeof(struct _lis3dh_context));

    if (!dev) {
        return NULL;
    }

    // Zero out context
    memset((void*) dev, 0, sizeof(struct _lis3dh_context));

    // Make sure MRAA is initialized
    if (mraa_init() != MRAA_SUCCESS) {
        printf("%s: mraa_init() failed\n", __FUNCTION__);
        lis3dh_close(dev);
        return NULL;
    }

    if (addr < 0) {
        // SPI
        if (!(dev->spi = mraa_spi_init(bus))) {
            printf("%s: mraa_spi_init() for bus %d failed\n", __FUNCTION__, bus);
            lis3dh_close(dev);
            return NULL;
        }

        // Only create CS context if we are actually using a valid pin.
        // A hardware controlled pin should specify CS as -1.
        if (cs >= 0) {
            if (!(dev->gpioCS = mraa_gpio_init(cs))) {
                printf("%s: mraa_gpio_init() for CS pin %d failed\n", __FUNCTION__, cs);
                lis3dh_close(dev);
                return NULL;
            }
            mraa_gpio_dir(dev->gpioCS, MRAA_GPIO_OUT);
        }

        mraa_spi_mode(dev->spi, MRAA_SPI_MODE0);
        if (mraa_spi_frequency(dev->spi, 5000000)) {
            printf("%s: mraa_spi_frequency() failed\n", __FUNCTION__);
            lis3dh_close(dev);
            return NULL;
        }
    } else {
        // I2C
        if (!(dev->i2c = mraa_i2c_init(bus))) {
            printf("%s: mraa_i2c_init() for bus %d failed\n", __FUNCTION__, bus);
            lis3dh_close(dev);
            return NULL;
        }

        if (mraa_i2c_address(dev->i2c, addr)) {
            printf("%s: mraa_i2c_address() for address 0x%x failed\n", __FUNCTION__, addr);
            lis3dh_close(dev);
            return NULL;
        }
    }

    // Check the chip id
    uint8_t chipID = lis3dh_get_chip_id(dev);
    if (chipID != LIS3DH_CHIPID) {
        printf("%s: invalid chip id: %02x, expected %02x\n", __FUNCTION__, chipID, LIS3DH_CHIPID);
        lis3dh_close(dev);
        return NULL;
    }

    // Call devinit with default options
    if (lis3dh_devinit(dev, LIS3DH_ODR_100HZ, LIS3DH_FS_2G, true)) {
        printf("%s: lis3dh_devinit() failed\n", __FUNCTION__);
        lis3dh_close(dev);
        return NULL;
    }

    return dev;
}
Ejemplo n.º 22
0
mraa_i2c_context mag_init()
{
	mraa_i2c_context mag;
	mag = mraa_i2c_init(1);

	if (mag == NULL) {
		fprintf(stderr, "Failed to initialize I2C.\n");
		exit(1);
	}
	mraa_i2c_address(mag, XM_ADDR);
	
	uint8_t mag_id = mraa_i2c_read_byte_data(mag, WHO_AM_I_XM);
	if (mag_id != 0x49) {
		fprintf(stderr, "Accelerometer/Magnetometer ID does not match.\n");
	}
		
	/* CTRL_REG5_XM enables temp sensor, sets mag resolution and data rate
	Bits (7-0): TEMP_EN M_RES1 M_RES0 M_ODR2 M_ODR1 M_ODR0 LIR2 LIR1
	TEMP_EN - Enable temperature sensor (0=disabled, 1=enabled)
	M_RES[1:0] - Magnetometer resolution select (0=low, 3=high)
	M_ODR[2:0] - Magnetometer data rate select
		000=3.125Hz, 001=6.25Hz, 010=12.5Hz, 011=25Hz, 100=50Hz, 101=100Hz
	LIR2 - Latch interrupt request on INT2_SRC (cleared by reading INT2_SRC)
		0=interrupt request not latched, 1=interrupt request latched
	LIR1 - Latch interrupt request on INT1_SRC (cleared by readging INT1_SRC)
		0=irq not latched, 1=irq latched 									 */
	//xmWriteByte(CTRL_REG5_XM, 0x94); // Mag data rate - 100 Hz, enable temperature sensor
	mraa_i2c_write_byte_data(mag, 0x94, CTRL_REG5_XM);
	
	/* CTRL_REG6_XM sets the magnetometer full-scale
	Bits (7-0): 0 MFS1 MFS0 0 0 0 0 0
	MFS[1:0] - Magnetic full-scale selection
	00:+/-2Gauss, 01:+/-4Gs, 10:+/-8Gs, 11:+/-12Gs							 */
	//xmWriteByte(CTRL_REG6_XM, 0x00); // Mag scale to +/- 2GS
	mraa_i2c_write_byte_data(mag, 0x00, CTRL_REG6_XM);
	
	/* CTRL_REG7_XM sets magnetic sensor mode, low power mode, and filters
	AHPM1 AHPM0 AFDS 0 0 MLP MD1 MD0
	AHPM[1:0] - HPF mode selection
		00=normal (resets reference registers), 01=reference signal for filtering, 
		10=normal, 11=autoreset on interrupt event
	AFDS - Filtered acceleration data selection
		0=internal filter bypassed, 1=data from internal filter sent to FIFO
	MLP - Magnetic data low-power mode
		0=data rate is set by M_ODR bits in CTRL_REG5
		1=data rate is set to 3.125Hz
	MD[1:0] - Magnetic sensor mode selection (default 10)
		00=continuous-conversion, 01=single-conversion, 10 and 11=power-down */
	//xmWriteByte(CTRL_REG7_XM, 0x00); // Continuous conversion mode
	mraa_i2c_write_byte_data(mag, 0x00, CTRL_REG7_XM);
	
	/* CTRL_REG4_XM is used to set interrupt generators on INT2_XM
	Bits (7-0): P2_TAP P2_INT1 P2_INT2 P2_INTM P2_DRDYA P2_DRDYM P2_Overrun P2_WTM
	*/
	//xmWriteByte(CTRL_REG4_XM, 0x04); // Magnetometer data ready on INT2_XM (0x08)
	mraa_i2c_write_byte_data(mag, 0x04, CTRL_REG4_XM);
	
	/* INT_CTRL_REG_M to set push-pull/open drain, and active-low/high
	Bits[7:0] - XMIEN YMIEN ZMIEN PP_OD IEA IEL 4D MIEN
	XMIEN, YMIEN, ZMIEN - Enable interrupt recognition on axis for mag data
	PP_OD - Push-pull/open-drain interrupt configuration (0=push-pull, 1=od)
	IEA - Interrupt polarity for accel and magneto
		0=active-low, 1=active-high
	IEL - Latch interrupt request for accel and magneto
		0=irq not latched, 1=irq latched
	4D - 4D enable. 4D detection is enabled when 6D bit in INT_GEN1_REG is set
	MIEN - Enable interrupt generation for magnetic data
		0=disable, 1=enable) */
	//xmWriteByte(INT_CTRL_REG_M, 0x09); // Enable interrupts for mag, active-low, push-pull
	mraa_i2c_write_byte_data(mag, 0x09, INT_CTRL_REG_M);
	
	return mag;
}
Ejemplo n.º 23
0
 /**
  * Set the slave to talk to, typically called before every read/write
  * operation
  *
  * @param address Communicate to the i2c slave on this address
  * @return Result of operation
  */
 Result
 address(uint8_t address)
 {
     return (Result) mraa_i2c_address(m_i2c, address);
 }
int main()
{
	uint8_t event_type;
	int exit_code;

	mraa_init();

	//Declaring opaque pointer to the internal struct_i2c
	mraa_i2c_context i2c;
	i2c = mraa_i2c_init(0);

	//If no i2c connection is detected
	if (i2c == NULL)
	{
		system("echo $(date) No I2C connection detected. >> log.txt");
		return 0;
	}

	//Setting the i2c context address
	mraa_i2c_address(i2c, I2C_ADDRESS);

	//Reading a single byte from the i2c context.
	event_type = mraa_i2c_read_byte(i2c);

	if (event_type != -1)
	{
		if (event_type == 'A')
		{
			system("echo Entered event A >> log.txt");
		}
		else if (event_type == 'B')
		{
			system("echo Entered event B >> log.txt");
		}
		else
		{
			system("echo $(date) Wrong wake-up character received. >> log.txt");
		}

		//Waiting for one second to ensure program stability
		sleep(1);

		//Writing a single byte back to the i2c context
		mraa_i2c_write_byte(i2c, 1);

		//De-initializing the mraa_i2c_context device.
		mraa_i2c_stop(i2c);
	}

	if (event_type == 'A' || event_type == 'B')
	{
		//Shutdown intel edison if program ran successfully.
		system("shutdown -h now");
	}
	else
	{
		system("echo $(date) No data received!!!!! >> log.txt");
		//De-initialize the mraa_i2c_context device
		mraa_i2c_stop(i2c);
	}

	return 0;

}
Ejemplo n.º 25
0
/**
 * Fast call for single byte write to LSM9DS0 Accelerometer / Magnetometer
 *
 */
void lsm_xm_write(unsigned char reg, unsigned char value) {
	mraa_i2c_address(i2c, LSM_ADDRESS_XM);
	if(mraa_i2c_write_byte_data(i2c, value, reg) != MRAA_SUCCESS) {
		printf("write single byte to LSM9DS0 XM failed...\n");
	}
}
Ejemplo n.º 26
0
 /**
  * Set the slave to talk to, typically called before every read/write
  * operation
  *
  * @param address Communicate to the i2c slave on this address
  * @return Result of operation
  */
 mraa_result_t address(uint8_t address) {
     return mraa_i2c_address(m_i2c, address);
 }
Ejemplo n.º 27
0
/**
 * Fast call for single byte read to LSM9DS0 Accelerometer / Magnetometer
 *
 */
unsigned char lsm_xm_read(unsigned char reg) {
	mraa_i2c_address(i2c, LSM_ADDRESS_XM);
	return mraa_i2c_read_byte_data(i2c, reg);
}
Ejemplo n.º 28
0
Archivo: grovepi.c Proyecto: KurtE/mraa
mraa_platform_t
mraa_grovepi_platform(mraa_board_t* board, const int i2c_bus)
{
    mraa_board_t* b = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
    if (b == NULL) {
        return MRAA_NULL_PLATFORM;
    }

    grovepi_bus = mraa_i2c_init(i2c_bus);
    if (grovepi_bus == NULL) {
        syslog(LOG_WARNING, "grovepi: Failed to initialize i2c bus %d", i2c_bus);
        free(b);
        return MRAA_NULL_PLATFORM;
    }
    mraa_i2c_address(grovepi_bus, GROVEPI_ADDRESS);

    b->platform_name = "grovepi";
    b->platform_version = "1.2.7"; // TODO: add firmware query function
    b->platform_type = MRAA_GROVEPI;
    b->gpio_count = 10;
    b->aio_count = 4;
    b->adc_supported = 10;
    b->phy_pin_count = 14;
    b->pwm_min_period = 2048;
    b->pwm_max_period = 2048;

    b->pins = (mraa_pininfo_t*) calloc(b->phy_pin_count, sizeof(mraa_pininfo_t));
    if (b->pins == NULL) {
        free(b);
        return MRAA_NULL_PLATFORM;
    }

    strncpy(b->pins[0].name, "IO0", 8);
    b->pins[0].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
    b->pins[0].gpio.pinmap = 0;
    strncpy(b->pins[1].name, "IO1", 8);
    b->pins[1].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
    b->pins[1].gpio.pinmap = 1;
    strncpy(b->pins[2].name, "IO2", 8);
    b->pins[2].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
    b->pins[2].gpio.pinmap = 2;
    strncpy(b->pins[3].name, "IO3", 8);
    b->pins[3].capabilities = (mraa_pincapabilities_t){ 1, 1, 1, 0, 0, 0, 0, 0 };
    b->pins[3].gpio.pinmap = 3;
    strncpy(b->pins[4].name, "IO4", 8);
    b->pins[4].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
    b->pins[4].gpio.pinmap = 4;
    strncpy(b->pins[5].name, "IO5", 8);
    b->pins[5].capabilities = (mraa_pincapabilities_t){ 1, 1, 1, 0, 0, 0, 0, 0 };
    b->pins[5].gpio.pinmap = 5;
    strncpy(b->pins[6].name, "IO6", 8);
    b->pins[6].capabilities = (mraa_pincapabilities_t){ 1, 1, 1, 0, 0, 0, 0, 0 };
    b->pins[6].gpio.pinmap = 6;
    strncpy(b->pins[7].name, "IO7", 8);
    b->pins[7].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
    b->pins[7].gpio.pinmap = 7;
    strncpy(b->pins[8].name, "IO8", 8);
    b->pins[8].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
    b->pins[8].gpio.pinmap = 8;
    strncpy(b->pins[9].name, "IO9", 8);
    b->pins[9].capabilities = (mraa_pincapabilities_t){ 1, 1, 1, 0, 0, 0, 0, 0 };
    b->pins[9].gpio.pinmap = 9;
    strncpy(b->pins[10].name, "A0", 8);
    b->pins[10].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 1, 0 };
    b->pins[10].gpio.pinmap = 10;
    b->pins[10].aio.pinmap = 0;
    strncpy(b->pins[11].name, "A1", 8);
    b->pins[11].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 1, 0 };
    b->pins[11].gpio.pinmap = 11;
    b->pins[11].aio.pinmap = 1;
    strncpy(b->pins[12].name, "A2", 8);
    b->pins[12].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 1, 0 };
    b->pins[12].gpio.pinmap = 12;
    b->pins[12].aio.pinmap = 2;
    strncpy(b->pins[13].name, "A3", 8);
    b->pins[13].capabilities = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 1, 0 };
    b->pins[13].gpio.pinmap = 13;
    b->pins[13].aio.pinmap = 3;

    b->adv_func = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
    if (b->adv_func == NULL) {
        free(b->pins);
        free(b);
        return MRAA_NULL_PLATFORM;
    }

    b->adv_func->gpio_init_internal_replace = &mraa_grovepi_gpio_init_internal_replace;
    b->adv_func->gpio_mode_replace = &mraa_grovepi_gpio_mode_replace;
    b->adv_func->gpio_dir_replace = &mraa_grovepi_gpio_dir_replace;
    //TODO: add interrupt support
    //b->adv_func->gpio_edge_mode_replace = &mraa_grovepi_gpio_edge_mode_replace;
    //b->adv_func->gpio_interrupt_handler_init_replace = &mraa_grovepi_gpio_interrupt_handler_init_replace;
    //b->adv_func->gpio_wait_interrupt_replace = &mraa_grovepi_gpio_wait_interrupt_replace;
    b->adv_func->gpio_read_replace = &mraa_grovepi_gpio_read_replace;
    b->adv_func->gpio_write_replace = &mraa_grovepi_gpio_write_replace;
    b->adv_func->gpio_close_replace = &mraa_grovepi_gpio_close_replace;

    b->adv_func->aio_init_internal_replace = &mraa_grovepi_aio_init_internal_replace;
    b->adv_func->aio_read_replace = &mraa_grovepi_aio_read_replace;

    b->adv_func->pwm_init_internal_replace = &mraa_grovepi_pwm_init_internal_replace;
    b->adv_func->pwm_write_replace = &mraa_grovepi_pwm_write_replace;
    b->adv_func->pwm_read_replace = &mraa_grovepi_pwm_read_replace;
    b->adv_func->pwm_enable_replace = &mraa_grovepi_pwm_enable_replace;
    b->adv_func->pwm_period_replace = &mraa_grovepi_pwm_period_replace;

    board->sub_platform = b;

    return b->platform_type;
}
Ejemplo n.º 29
0
// init
bno055_context bno055_init(int bus, uint8_t addr)
{
    bno055_context dev =
        (bno055_context)malloc(sizeof(struct _bno055_context));

    if (!dev)
        return NULL;

    // zero out context
    memset((void *)dev, 0, sizeof(struct _bno055_context));

    // make sure MRAA is initialized
    int mraa_rv;
    if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
    {
        printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
        bno055_close(dev);
        return NULL;
    }

    if (!(dev->i2c = mraa_i2c_init(bus)))
    {
        printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__);
        bno055_close(dev);
        return NULL;
    }

    if (mraa_i2c_address(dev->i2c, addr) != MRAA_SUCCESS)
    {
        printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__);
        bno055_close(dev);
        return NULL;
    }

    _clear_data(dev);

    // forcibly set page 0, so we are synced with the device
    if (bno055_set_page(dev, 0, true))
    {
        printf("%s: bno055_set_page() failed.\n", __FUNCTION__);
        bno055_close(dev);
        return NULL;
    }

    // check the chip id.  This has to be done after forcibly setting
    // page 0, as that is the only page where the chip id is present.
    uint8_t chipID = 0;
    if (bno055_get_chip_id(dev, &chipID))
    {
        printf("%s: Could not read chip id\n", __FUNCTION__);
        bno055_close(dev);
        return NULL;
    }

    if (chipID != BNO055_CHIPID)
    {
        printf("%s: Invalid chip ID. Expected 0x%02x, got 0x%02x\n",
               __FUNCTION__, BNO055_CHIPID, chipID);
        bno055_close(dev);
        return NULL;
    }

    upm_result_t urv = UPM_SUCCESS;
    // set config mode
    urv += bno055_set_operation_mode(dev, BNO055_OPERATION_MODE_CONFIGMODE);

    // default to internal clock
    urv += bno055_set_clock_external(dev, false);

    // we specifically avoid doing a reset so that if the device is
    // already calibrated, it will remain so.

    // we always use C for temperature
    urv += bno055_set_temperature_units_celsius(dev);

    // default to accelerometer temp
    urv += bno055_set_temperature_source(dev, BNO055_TEMP_SOURCE_ACC);

    // set accel units to m/s^2
    urv += bno055_set_accelerometer_units(dev, false);

    // set gyro units to degrees
    urv += bno055_set_gyroscope_units(dev, false);

    // set Euler units to degrees
    urv += bno055_set_euler_units(dev, false);

    // by default, we set the operating mode to the NDOF fusion mode
    urv += bno055_set_operation_mode(dev, BNO055_OPERATION_MODE_NDOF);

    // if any of those failed, bail
    if (urv != UPM_SUCCESS)
    {
        printf("%s: Initial device configuration failed\n", __FUNCTION__);
        bno055_close(dev);
        return NULL;
    }

    return dev;
}
Ejemplo n.º 30
0
/**
 * Fast call for multiple bytes read from LSM9DS0 Accelerometer / Magnetometer
 *
 */
void lsm_xm_read_bytes(unsigned char reg, unsigned char *buf, int len) {
	mraa_i2c_address(i2c, LSM_ADDRESS_XM);
	if(mraa_i2c_read_bytes_data(i2c, reg|0x80, buf, len) == -1) {
		printf("read multiple bytes from LSM9DS0 XM failed...\n");
	}
}