Esempio n. 1
0
/**
 * @brief BMA180 ioctl control entry point
 *
 * @param   hal     Address of an initialized sensor hardware descriptor.
 * @param   cmd     Command to execute
 * @param   arg     Argument for command (varies)
 * @return  bool    true if the call succeeds, else false is returned.
 */
static bool bma180_ioctl(sensor_t *sensor, sensor_command_t cmd, void *arg)
{
	sensor_hal_t *const hal = sensor->hal;
	sensor_data_t sample = {.scaled = true};

	switch (cmd) {
	case SENSOR_SET_STATE:
		return bma180_set_state(hal, *((sensor_state_t *)arg));

	case SENSOR_SET_RANGE:
		return bma180_set_range(hal, (uint16_t)*((int *)arg));

	case SENSOR_SET_BANDWIDTH:
		return bma180_set_bandwidth(hal, (uint16_t)*((int *)arg));

	case SENSOR_READ_VECTOR:
		if (bma180_get_accel(hal, &sample)) {
			vector3_t *const pvec = (vector3_t *)arg;
			pvec->x = sample.axis.x;
			pvec->y = sample.axis.y;
			pvec->z = sample.axis.z;
			return true;
		} else {
			return false;
		}

	default:
		sensor->err = SENSOR_ERR_UNSUPPORTED;
		return false;
	}
}
Esempio n. 2
0
int
bma180_attach(struct spi_dev_s *spi, int spi_id)
{
	int	result = ERROR;
	
	bma180_dev.spi = spi;
	bma180_dev.spi_id = spi_id;
    
	SPI_LOCK(bma180_dev.spi, true);

	/* verify that the device is attached and functioning */
	if (bma180_read_reg(ADDR_CHIP_ID) == CHIP_ID) {
        
		bma180_write_reg(ADDR_RESET, SOFT_RESET);       // page 48

		up_udelay(13000);                               // wait 12 ms, see page 49

		/* Configuring the BMA180 */

		/* enable writing to chip config */
		uint8_t ctrl0 = bma180_read_reg(ADDR_CTRL_REG0);
		ctrl0 |= REG0_WRITE_ENABLE;
		bma180_write_reg(ADDR_CTRL_REG0, ctrl0);

		/* disable I2C interface, datasheet page 31 */
		uint8_t disi2c = bma180_read_reg(ADDR_DIS_I2C);
		disi2c |= 0x01;
		bma180_write_reg(ADDR_DIS_I2C, disi2c);

		/* block writing to chip config */
		ctrl0 = bma180_read_reg(ADDR_CTRL_REG0);
		ctrl0 &= (~REG0_WRITE_ENABLE);
		bma180_write_reg(ADDR_CTRL_REG0, ctrl0);

		// up_udelay(500);

		/* set rate */
		result = bma180_set_rate(BMA180_RATE_LP_600HZ);

		// up_udelay(500);

		/* set range */
		result += bma180_set_range(BMA180_RANGE_4G);

		// up_udelay(500);

		if (result == 0) {
			/* make ourselves available */
			register_driver("/dev/bma180", &bma180_fops, 0666, NULL);
		}
	} else {
		errno = EIO;
	}

	SPI_LOCK(bma180_dev.spi, false);
    
	return result;
}
Esempio n. 3
0
static int
bma180_ioctl(struct file *filp, int cmd, unsigned long arg)
{
	int	result = ERROR;
    
	switch (cmd) {
        case BMA180_SETRATE:
            result = bma180_set_rate(arg);
            break;

        case BMA180_SETRANGE:
            result = bma180_set_range(arg);
            break;

        case BMA180_SETBUFFER:
            bma180_dev.buffer = (struct bma180_buffer *)arg;
            result = 0;
            break;
	}
    
	if (result)
		errno = EINVAL;
	return result;
}
Esempio n. 4
0
/**
 * @brief Bosch BMA180 accelerometer driver initialization.
 *
 * This is the main initialization function for the BMA180 device.
 * The accelerometer range and bandwidth are set based on user-specified
 * values from the system configuration.
 *
 * @param sensor    Address of a sensor device descriptor.
 * @param resvd     Reserved value.
 * @return bool     true if the call succeeds, else false is returned.
 */
bool bma180_init(sensor_t *sensor, int resvd)
{
	bool status = false;

	sensor_hal_t *const hal = sensor->hal;

	if (BMA180_ID_VAL == sensor_bus_get(hal, BMA180_CHIP_ID)) {
		/* Set the driver function table and capabilities pointer. */
		static const sensor_device_t bma180_device = {
			.func.read        = bma180_read,
			.func.ioctl       = bma180_ioctl,

			.caps.feature     = SENSOR_CAPS_3_AXIS     |
					SENSOR_CAPS_SELFTEST   |
					SENSOR_CAPS_HI_G_EVENT |
					SENSOR_CAPS_LO_G_EVENT |
					SENSOR_CAPS_AUX_TEMP,

			.caps.vendor      = SENSOR_VENDOR_BOSCH,
			.caps.range_table = range_table,
			.caps.band_table  = band_table,
			.caps.range_count = ARRAYSIZE(range_table),
			.caps.band_count  = ARRAYSIZE(band_table),
			.caps.units       = SENSOR_UNITS_g0,
			.caps.scale       = SENSOR_SCALE_milli,
			.caps.name = "BMA180 Digital, triaxial acceleration sensor"
		};

		sensor->drv = &bma180_device;

		if (STATUS_OK == hal->bus.status) {
			/* Set the driver (device) default range, bandwidth, &
			 * resolution.
			 */
			hal->range      = 1000; /* milli-g */
			hal->bandwidth  = 10;   /* Hertz */
			hal->resolution = BMA180_DATA_RESOLUTION;

			bma180_set_range(hal, 0);
			bma180_set_bandwidth(hal, 0);

			/* Set the device burst read base address. */
			hal->burst_addr = BMA180_ACC_X_LSB;

			status = (STATUS_OK == hal->bus.status);
		}
	}

	return status;
}

/**
 * @brief Read BMA180 device ID and revision numbers.
 *
 * This function reads the accelerometer hardware identification registers
 * and returns these values in the specified data structure.
 *
 * @param hal       Address of an initialized sensor hardware descriptor.
 * @param data      Address of sensor_data_t structure to return values.
 * @return bool     true if the call succeeds, else false is returned.
 */
static bool bma180_device_id(sensor_hal_t *hal, sensor_data_t *data)
{
	data->device.id = sensor_bus_get(hal, BMA180_CHIP_ID);
	data->device.version = sensor_bus_get(hal, BMA180_CHIP_VERSION);

	return true;
}