void jsi2cWrite(JshI2CInfo *inf, unsigned char address, int nBytes, const unsigned char *data, bool sendStop) { if (inf->pinSCL==PIN_UNDEFINED || inf->pinSDA==PIN_UNDEFINED) return; i2cInfo d; i2c_initstruct(&d, inf); i2c_start(&d); i2c_wr(&d, address<<1); int i; for (i=0;i<nBytes;i++) i2c_wr(&d, data[i]); if (sendStop) i2c_stop(&d); inf->started = d.started; }
int i2c_write(int bus, unsigned char slave, int address, int len, const unsigned char *data) { int ret; mutex_lock(&i2c_mtx[bus]); ret = i2c_wr(bus, slave, address, len, data); mutex_unlock(&i2c_mtx[bus]); return ret; }
/** @brief Check for HMC5883L presence on I2C @return True on present, false otherwise */ uint8_t hmc_present(void) { uint8_t b[3] = {10}; if( i2c_wr(hmcDev, HMC_I2C_ADDR, b, 1) ) { return 0; } if( i2c_rd(hmcDev, HMC_I2C_ADDR, b, 3) ) { return 0; } return (b[0] == 'H') && (b[1] == '4') && (b[2] == '3'); }
void jsi2cRead(JshI2CInfo *inf, unsigned char address, int nBytes, unsigned char *data, bool sendStop) { if (inf->pinSCL==PIN_UNDEFINED || inf->pinSDA==PIN_UNDEFINED) return; i2cInfo d; i2c_initstruct(&d, inf); i2c_start(&d); i2c_wr(&d, 1|(address<<1)); int i; for (i=0;i<nBytes;i++) data[i] = i2c_rd(&d, i==nBytes-1); if (sendStop) i2c_stop(&d); inf->started = d.started; }
/** @brief Read acc. values. @param[out] x Pointer to x @param[out] y Pointer to y @param[out] z Pointer to z @return True on success, false otherwise */ uint8_t hmc_read(int16_t* x, int16_t* y, int16_t* z) { uint8_t b[6] = {3}; if( i2c_wr(hmcDev, HMC_I2C_ADDR, b, 1) ) { return 0; } if( i2c_rd(hmcDev, HMC_I2C_ADDR, b, 6) ) { return 0; } *x = (((int16_t)b[0]) << 8) | b[1]; *z = (((int16_t)b[2]) << 8) | b[3]; *y = (((int16_t)b[4]) << 8) | b[5]; return 1; }
int pmu_wr_multiple(int address, int count, unsigned char* buffer) { return i2c_wr(0, 0xe6, address, count, buffer); }
/** @brief Init HMC5883L. @param[in] cra Control register A @param[in] crb Control register B @param[in] mode Mode @return Same as i2c_wr */ uint8_t hmc_init(uint8_t cra, uint8_t crb, uint8_t mode) { uint8_t b[4] = {0, cra, crb, mode}; return i2c_wr(hmcDev, HMC_I2C_ADDR, b, 4); }