mrb_value mrb_mraa_i2c_init(mrb_state *mrb, mrb_value self){ mrb_int bus; mrb_bool raw; mraa_i2c_context i2c; raw = false; mrb_get_args(mrb, "i|b", &bus, &raw); if (raw == false){ i2c = mraa_i2c_init(bus); } else { i2c = mraa_i2c_init_raw(bus); } if (i2c == NULL){ mrb_raisef(mrb, E_RUNTIME_ERROR, "Failed to initialize BUS:%S.", mrb_fixnum_value(bus)); } DATA_TYPE(self) = &mrb_mraa_i2c_ctx_type; DATA_PTR(self) = i2c; return self; }
/** * Instantiates an i2c bus. Multiple instances of the same bus can * exist and the bus is not guarranteed to be on the correct address * before read/write. * * @param bus The i2c bus to use * @param raw Whether to disable pinmapper for your board */ I2c(int bus, bool raw=false) { if (raw) { m_i2c = mraa_i2c_init_raw(bus); } else { m_i2c = mraa_i2c_init(bus); } if (m_i2c == NULL) { throw std::invalid_argument("Invalid i2c bus"); } }
int atlsci_init() { i2c_context = mraa_i2c_init_raw(6); if (i2c_context == NULL) { DEBUG("atlsci_init(): mraa_i2c_init_raw() failure. Failed to initialize I2C\n"); return ATLSCI_FAILURE; } return ATLSCI_SUCCESS; }
/* * Wake up the sensors and set up scales and rates */ void lsm_init() { int i2c_bus = 1; i2c = mraa_i2c_init_raw(i2c_bus); if (i2c == NULL) { printf("LSM9DS0: Failed to init i2c bus %d\n", i2c_bus); return; } lsm_gyro_start(G_SCALE_500DPS, G_ODR_190_BW_25); lsm_accel_start(A_SCALE_4G, A_ODR_400, A_ABW_50); lsm_magn_start(M_SCALE_2GS, M_ODR_100); nanosleep((const struct timespec[]){{0, 500000000L}}, NULL); // Wait 500ms for sensors to wake-up
mraa_i2c_context mraa_i2c_init(int bus) { int checked_pin = mraa_setup_i2c(bus); if (checked_pin < 0) { switch(checked_pin) { case -1: fprintf(stderr, "No i2c on board\n"); return NULL; case -2: fprintf(stderr, "Failed to set-up i2c multiplexer!\n"); return NULL; case -3: fprintf(stderr, "Platform Not Initialised"); return NULL; default: return NULL; } } return mraa_i2c_init_raw((unsigned int) checked_pin); }
mraa_i2c_context mraa_i2c_init(int bus) { if (plat == NULL) { syslog(LOG_ERR, "i2c: Platform Not Initialised"); return NULL; } if (plat->i2c_bus_count == 0) { syslog(LOG_ERR, "No i2c buses defined in platform"); return NULL; } if (bus >= plat->i2c_bus_count) { syslog(LOG_ERR, "Above i2c bus count"); return NULL; } if (plat->i2c_bus[bus].bus_id == -1) { syslog(LOG_ERR, "Invalid i2c bus, moving to default i2c bus"); bus = plat->def_i2c_bus; } int pos = plat->i2c_bus[bus].sda; if (plat->pins[pos].i2c.mux_total > 0) { if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) { syslog(LOG_ERR, "i2c: Failed to set-up i2c sda multiplexer"); return NULL; } } pos = plat->i2c_bus[bus].scl; if (plat->pins[pos].i2c.mux_total > 0) { if (mraa_setup_mux_mapped(plat->pins[pos].i2c) != MRAA_SUCCESS) { syslog(LOG_ERR, "i2c: Failed to set-up i2c scl multiplexer"); return NULL; } } return mraa_i2c_init_raw((unsigned int) plat->i2c_bus[bus].bus_id); }
int main(void) { mraa_i2c_init_raw(6); }