Esempio n. 1
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;
}
Esempio n. 2
0
File: ecezo.c Progetto: g-vidal/upm
upm_result_t ecezo_write(const ecezo_context dev, char *buffer, size_t len)
{
    assert(dev != NULL);

    if (dev->uart)
    {
        if (mraa_uart_write(dev->uart, buffer, len) != (int)len)
        {
            printf("%s: mraa_uart_write() failed.\n", __FUNCTION__);
            return UPM_ERROR_OPERATION_FAILED;
        }

    }
    else
    {
        // I2C
        if (mraa_i2c_write(dev->i2c, (uint8_t *)buffer, len))
        {
            printf("%s: mraa_i2c_write() failed.\n", __FUNCTION__);
            return UPM_ERROR_OPERATION_FAILED;
        }
    }

    return UPM_SUCCESS;
}
Esempio n. 3
0
File: grovepi.c Progetto: KurtE/mraa
static int
mraa_grovepi_read_internal(int function, int pin)
{
    uint8_t data[5];
    uint8_t result[3];
    data[0] = GROVEPI_REGISTER;
    data[1] = function;
    data[2] = pin;
    data[3] = 0;
    data[4] = 0;
    if (mraa_i2c_write(grovepi_bus, data, 5) != MRAA_SUCCESS) {
        syslog(LOG_WARNING, "grovepi: failed to write command to i2c bus /dev/i2c-%d", grovepi_bus->busnum);
        return -1;
    }
    if (mraa_i2c_write_byte(grovepi_bus, 1) != MRAA_SUCCESS) {
        syslog(LOG_WARNING, "grovepi: failed to write to i2c bus /dev/i2c-%d", grovepi_bus->busnum);
        return -1;
    }
    if (function == GROVEPI_GPIO_READ) {
        if (mraa_i2c_read(grovepi_bus, result, 1) != 1) {
            syslog(LOG_WARNING, "grovepi: failed to read result from i2c bus /dev/i2c-%d", grovepi_bus->busnum);
            return -1;
        }
        return result[0];
    }
    if (function == GROVEPI_AIO_READ) {
        if (mraa_i2c_read(grovepi_bus, result, 3) != 3) {
            syslog(LOG_WARNING, "grovepi: failed to read result from i2c bus /dev/i2c-%d", grovepi_bus->busnum);
            return -1;
        }
        return (result[1] << 8) | result [2];
    }
    return -1;
}
Esempio n. 4
0
mrb_value
mrb_mraa_i2c_write(mrb_state *mrb, mrb_value self){
    mraa_i2c_context i2c;
    mrb_value mrv_wbuf;
    mrb_int length;

    mraa_result_t result;
    uint8_t *wbuf;
    mrb_int argc;
    
    Data_Get_Struct(mrb, self, &mrb_mraa_i2c_ctx_type, i2c);

    argc = mrb_get_args(mrb, "o|i", &mrv_wbuf, &length);

    result = MRAA_ERROR_INVALID_PARAMETER;
    if (mrb_array_p(mrv_wbuf)){
        int i;
        if (argc == 1){
            length = RARRAY_LEN(mrv_wbuf);
        }

        wbuf = (uint8_t *)mrb_malloc(mrb, sizeof(uint8_t) * length);
        memset(wbuf, 0, sizeof(uint8_t) * length);
        for (i = 0; i < length; i++){
            wbuf[i] = mrb_fixnum(mrb_ary_ref(mrb, mrv_wbuf, i));
        }
        result = mraa_i2c_write(i2c, wbuf, length);
        mrb_free(mrb, wbuf);
    }

    return mrb_fixnum_value(result);
}
Esempio n. 5
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);
}
Esempio n. 6
0
/*
 *  Transmits a string. msg should be null-terminating, but the null byte
 *  is not transmitted. This function should be called after calling
 *  set_sensor_address(). Delays for TX_WAIT_TIME seconds after sending message.
 */
static int send_string(mraa_i2c_context i2c_context, char * msg)
{
    DEBUG("Sending string %s of length %d\n", msg, strlen(msg));

    int ret = mraa_i2c_write(i2c_context, (uint8_t *) msg, strlen(msg));
    sleep(TX_WAIT_TIME);
    return ret;
}
Esempio 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) ;
    }
}
Esempio n. 8
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;
}
Esempio n. 9
0
File: grovepi.c Progetto: KurtE/mraa
static mraa_result_t
mraa_grovepi_write_internal(int function, int pin, int value)
{
    uint8_t data[5];
    data[0] = GROVEPI_REGISTER;
    data[1] = function;
    data[2] = pin;
    data[3] = value;
    data[4] = 0;
    if (mraa_i2c_write(grovepi_bus, data, 5) != MRAA_SUCCESS) {
        syslog(LOG_WARNING, "grovepi: failed to write command to i2c bus /dev/i2c-%d", grovepi_bus->busnum);
        return MRAA_ERROR_UNSPECIFIED;
    }
    return MRAA_SUCCESS;
}
Esempio n. 10
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;
}
Esempio n. 11
0
upm_result_t bno055_write_regs(const bno055_context dev, uint8_t reg,
                               uint8_t *buffer, size_t len)
{
    assert(dev != NULL);

    uint8_t buf[len + 1];

    buf[0] = reg;
    for (int i=0; i<len; i++)
        buf[i+1] = buffer[i];

    if (mraa_i2c_write(dev->i2c, buf, len + 1))
    {
        printf("%s: mraa_i2c_write() failed\n",
               __FUNCTION__);
        return UPM_ERROR_OPERATION_FAILED;
    }

    return UPM_SUCCESS;
}
/*
bool I2Cdev4Edison::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) {
    //int8_t count = 0;
    uint8_t buf[128];
    //int fd;

    if (length > 127) {
        fprintf(stderr, "Byte write count (%d) > 127\n", length);
        return(FALSE);
    }
    //fd = open("/dev/i2c-0", O_RDWR);
    //if (fd < 0) {
    //    fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
    //    return(FALSE);
    //}
    i2c->address(devAddr);
    //if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
    //    fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
   //     close(fd);
   //     return(FALSE);
   // }
    buf[0] = regAddr;
    memcpy(buf+1,data,length);
     //count = write(fd, buf, length+1);
    mraa::Result result = i2c->write(buf, length + 1);
    if (result != mraa::SUCCESS){
       	mraa::printError(result);
       	return false;
    }
    return true;

    //if (count < 0) {
    //    fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
    //    close(fd);
    //    return(FALSE);
    //} else if (count != length+1) {
    //    fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
    //    close(fd);
    //    return(FALSE);
    //}
    //close(fd);

}
*/
int edison_i2c_write(unsigned char slave_addr,
                     unsigned char reg_addr,
                     unsigned char length,
                     unsigned char const *data)
{
	uint8_t buf[128];
	if (length > 127) {
		fprintf(stderr, "MRAA Byte write count (%d) > 127\n", length);
	    return(-1);
	}
	buf[0] = reg_addr;
	memcpy(buf+1,data,length);
	mraa_result_t mr = mraa_i2c_write(i2c, buf, length + 1);
	if (mr != MRAA_SUCCESS){
		fprintf(stderr, "MRAA Failed to write data in reg address %x on the i2c bus.\n", reg_addr);
		mraa_result_print(mr);
		return -1;
	}
	return 0;

	    //if (count < 0) {
	    //    fprintf(stderr, "Failed to write device(%d): %s\n", count, ::strerror(errno));
	    //    close(fd);
	    //    return(FALSE);
	    //} else if (count != length+1) {
	    //    fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
	    //    close(fd);
	    //    return(FALSE);
	    //}
	    //close(fd);

    /*
	unsigned long start, cur;
    if (!i2c.enabled)
        return -1;
    if (!length)
        return 0;

    // Populate struct.
    i2c.state = STATE_WRITING;
    i2c.slave_reg = reg_addr;
    i2c.slave_reg_written = 0;
    i2c.data = (unsigned char*)data;
    i2c.length = length;

    I2CSA = slave_addr;
    CTL1 |= UCTR | UCTXSTT;

    msp430_get_clock_ms(&start);
    while (i2c.state != STATE_WAITING) {
        __bis_SR_register(LPM0_bits + GIE);
        msp430_get_clock_ms(&cur);
        if (cur >= (start + I2C_TIMEOUT_MS)) {
            CTL1 |= UCTXSTP;
            i2c.state = STATE_WAITING;
            msp430_i2c_disable();
            msp430_delay_ms(1);
            CLEAR_SCL();
            CLEAR_SDA();
            msp430_delay_ms(1);
            SET_SCL();
            SET_SDA();
            msp430_i2c_enable();
            return -1;
        }
    }
    return 0;
    */
}
Esempio n. 13
0
 /**
  * Write length bytes to the bus, the first byte in the array is the
  * command/register to write
  *
  * @param data Buffer to send on the bus, first byte is i2c command
  * @param length Size of buffer to send
  * @return Result of operation
  */
 mraa_result_t write(const uint8_t* data, int length) {
     return mraa_i2c_write(m_i2c, data, length);
 }
Esempio n. 14
0
 /**
  * Write length bytes to the bus, the first byte in the array is the
  * command/register to write
  *
  * @param data Buffer to send on the bus, first byte is i2c command
  * @param length Size of buffer to send
  * @return Result of operation
  */
 Result
 write(const uint8_t* data, int length)
 {
     return (Result) mraa_i2c_write(m_i2c, data, length);
 }