void MMA_init(void) { uint8_t data2[2]; twi_master_transfer(MMA_ADDRESS, mma_init_cmd, 2, TWI_ISSUE_STOP); twi_master_transfer(MMA_ADDRESS, mma_init_cmd2, 2, TWI_ISSUE_STOP); twi_master_transfer(MMA_ADDRESS, mma_reg_who_am_i, 1, TWI_DONT_ISSUE_STOP); twi_master_transfer(MMA_ADDRESS | TWI_READ_BIT, data2, 1, TWI_ISSUE_STOP); }
bool fb_getTopLEDs(uint8_t faceNum, bool *redOn, bool *greenOn, bool *blueOn) { uint8_t twiBuf[2]; bool success = true; if ((faceNum < 1) || (faceNum > 6)) { return false; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_LEDS_TOP; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 1, true); twi_master_deinit(); if (success) { *redOn = (twiBuf[0] & 0x01) ? true : false; *greenOn = (twiBuf[0] & 0x02) ? true : false; *blueOn = (twiBuf[0] & 0x04) ? true : false; } return success; }
bool fb_getIRTxLEDs(uint8_t faceNum, bool *led1, bool *led2, bool *led3, bool *led4) { uint8_t twiBuf[2]; bool success = true; if ((faceNum < 1) || (faceNum > 6)) { return -1; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_TX_LED_SELECT; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 1, true); twi_master_deinit(); if (success) { *led1 = (twiBuf[0] & 0x01) ? true : false; *led2 = (twiBuf[0] & 0x02) ? true : false; *led3 = (twiBuf[0] & 0x04) ? true : false; *led4 = (twiBuf[0] & 0x08) ? true : false; } return success; }
bool fb_getRxEnable(uint8_t faceNum, bool *rxEnabled) { uint8_t twiBuf[2]; bool success = true; if (faceNum > 6) { return -1; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_RX_ENABLE; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 1, true); twi_master_deinit(); if (twiBuf[0] & 0x01) { *rxEnabled = true; } else { *rxEnabled = false; } return success; }
bool mpu6050_register_read(uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes) { bool transfer_succeeded; transfer_succeeded = twi_master_transfer(m_device_address, ®ister_address, 1, TWI_DONT_ISSUE_STOP); transfer_succeeded &= twi_master_transfer(m_device_address|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP); return transfer_succeeded; }
int16_t fb_getAmbientLight(uint8_t faceNum) { uint8_t twiBuf[2]; bool success = true; int16_t ambientLight; if ((faceNum < 1) || (faceNum > 6)) { return -1; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_AMBIENT_LIGHT; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 2, true); twi_master_deinit(); /* The 10-bit result is returned left-shifted so that it is possible to * read just one byte and still get most of the resolution (even though * we still read both bytes). */ ambientLight = twiBuf[0] << 2; ambientLight |= twiBuf[1] >> 6; if (!success) { ambientLight = -1; } return ambientLight; }
bool AT24CXX_ReadOneByte(uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes) { bool transfer_succeeded; transfer_succeeded = twi_master_transfer(AT24XXADDRESS, ®ister_address, 1, TWI_DONT_ISSUE_STOP); transfer_succeeded &= twi_master_transfer(AT24XXADDRESS|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP); return transfer_succeeded; }
/** * @brief Function for reading the current configuration of the sensor. * * @return uint8_t Zero if communication with the sensor failed. Contents (always non-zero) of configuration register (@ref SHT2X_ONESHOT_MODE and @ref SHT2X_CONVERSION_DONE) if communication succeeded. */ static sht2x_error_t sht2x_config_read(user_register_t* user_register) { read_config_result_t result; sht2x_error_t error = SHT2X_ERROR_NONE; uint8_t command = USER_REG_R; // Write: command protocol if (twi_master_transfer(SHT2X_I2C_ADDRESS, &command, 1, TWI_DONT_ISSUE_STOP)) { if (twi_master_transfer(SHT2X_I2C_ADDRESS | TWI_READ_BIT, (uint8_t*)&result.raw, sizeof(read_config_result_t), TWI_DONT_ISSUE_STOP)) // Read: current configuration { // validate checksum uint8_t checksum_error = sht2x_check_crc((uint8_t*)&result.result.user_register, sizeof(user_register_t), result.result.checksum); if (checksum_error == SHT2X_ERROR_CRC || checksum_error) { // propagate out the error. Poor design probably. return SHT2X_ERROR_CRC; } // Read succeeded *user_register = result.raw; } else { error = SHT2X_ERROR_READ_FAILED; } } else { error = SHT2X_ERROR_READ_FAILED; } return error; }
static void adc121c02_read_register(uint8_t addr, void *data, size_t len) { uint8_t cmd[] = { addr }; twi_master_transfer(ADC121C02, cmd, sizeof(cmd), TWI_DONT_ISSUE_STOP); twi_master_transfer(ADC121C02 | TWI_READ_BIT, data, len, TWI_ISSUE_STOP); }
bool hs3000a_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes) { bool transfer_succeeded; transfer_succeeded = twi_master_transfer(HS3000A_ADDR, ®ister_address, 1, TWI_DONT_ISSUE_STOP); transfer_succeeded &= twi_master_transfer(HS3000A_ADDR|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP); return transfer_succeeded; }
bool read_registers(uint8_t device_address, uint8_t register_address, int size, uint8_t *value) { bool transfer_succeeded = true; transfer_succeeded &= twi_master_transfer(device_address, ®ister_address, 1, TWI_DONT_ISSUE_STOP); if (transfer_succeeded) { transfer_succeeded &= twi_master_transfer(device_address | TWI_READ_BIT, value, size, TWI_ISSUE_STOP); } return transfer_succeeded; }
bool mpu6050_readBytes(uint8_t addr, uint8_t *data, uint8_t nBytes) { bool success = true; /* The caller must initialize the TWI interface */ if (!twi_master_get_init()) { return false; } success &= twi_master_transfer((mpu6050Address<<1), &addr, 1, TWI_DONT_ISSUE_STOP); success &= twi_master_transfer((mpu6050Address<<1) | TWI_READ_BIT, data, nBytes, TWI_ISSUE_STOP); return success; }
bool db_getVersion(char *verStr, uint8_t verStrSize) { uint8_t twiBuf[34]; uint32_t time_ms; char *strPtr; twi_master_init(); twiBuf[0] = DB_VERSION_CMD; if (!twi_master_transfer((DB_TWI_ADDR << 1), twiBuf, 1, true)) { twi_master_deinit(); return false; } delay_ms(DB_POLL_FIRST_INTERVAL_MS); for (time_ms = 0; time_ms < DB_VERSION_TIMEOUT_MS; time_ms += DB_POLL_INTERVAL_MS) { if (!twi_master_transfer((DB_TWI_ADDR << 1) | TWI_READ_BIT, twiBuf, sizeof(twiBuf), true)) { /* If the daughterboard fails to respond to the I2C master read, * something is wrong, so we return failure. */ twi_master_deinit(); return false; } /* If the daughterboard responds and echoes the command code in the the * first byte, and the second byte is 0x01, the version command was * successful. */ if ((twiBuf[0] == DB_VERSION_CMD) && (twiBuf[1] == 0x01)) { /* The following bytes contain a human-readable version string. */ strPtr = (char *)&twiBuf[2]; /* If the version string will fit into the buffer provided by the * caller, we copy it. Otherwise, we copy whatever will fit and * add a null-terminator. */ if (strlen(strPtr) < verStrSize) { strcpy(verStr, strPtr); } else { strncpy(verStr, strPtr, verStrSize - 1); verStr[verStrSize-1] = '\0'; } twi_master_deinit(); return true; } /* If the daughterboard responded but has not yet processed the command * we delay and then try to read from the daughterboard again. */ delay_ms(DB_POLL_INTERVAL_MS); } twi_master_deinit(); return false; }
bool db_setLEDs(bool redOn, bool greenOn, bool blueOn) { uint8_t twiBuf[2]; uint32_t time_ms; twi_master_init(); twiBuf[0] = DB_LED_CMD; twiBuf[1] = 0x00; if (redOn) { twiBuf[1] |= 0x01; } if (greenOn) { twiBuf[1] |= 0x02; } if (blueOn) { twiBuf[1] |= 0x04; } if (!twi_master_transfer((DB_TWI_ADDR << 1), twiBuf, 2, true)) { twi_master_deinit(); return false; } delay_ms(DB_POLL_FIRST_INTERVAL_MS); for (time_ms = 0; time_ms < DB_LED_TIMEOUT_MS; time_ms += DB_POLL_INTERVAL_MS) { if (!twi_master_transfer((DB_TWI_ADDR << 1) | TWI_READ_BIT, twiBuf, sizeof(twiBuf), true)) { /* If the daughterboard fails to respond to the I2C master read, * something is wrong, so we return failure. */ twi_master_deinit(); return false; } /* If the daughterboard responds and echoes the command code in the the * first byte, and the second byte is 0x01, the temperature command was * successful. */ if ((twiBuf[0] == DB_LED_CMD) && (twiBuf[1] == 0x01)) { twi_master_deinit(); return true; } /* If the daughterboard responded but has not yet processed the command * we delay and then try to read from the daughterboard again. */ delay_ms(DB_POLL_INTERVAL_MS); } twi_master_deinit(); return false; }
bool nrf6350_lcd_set_contrast(uint8_t contrast) { nrf_delay_us(10000); data_buffer[0] = FUNC_SET; data_buffer[1] = 0x70 | contrast; return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP); }
static bool nrf6350_lcd_set_instruction(uint8_t instr) { nrf_delay_us(10000); data_buffer[0] = FUNC_SET; data_buffer[1] = instr; return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP); }
bool ds1624_init(uint8_t device_address) { bool transfer_succeeded = true; m_device_address = DS1634_BASE_ADDRESS + (uint8_t)(device_address << 1); uint8_t config = ds1624_config_read(); if (config != 0) { // Configure DS1624 for 1SHOT mode if not done so already. if (!(config & DS1624_ONESHOT_MODE)) { uint8_t data_buffer[2]; data_buffer[0] = command_access_config; data_buffer[1] = DS1624_ONESHOT_MODE; transfer_succeeded &= twi_master_transfer(m_device_address, data_buffer, 2, TWI_ISSUE_STOP); } } else { transfer_succeeded = false; } return transfer_succeeded; }
bool nrf6350_js_get_value(int8_t *val) { uint8_t js_data; if (!twi_master_transfer(JS_ADDR << 1 | TWI_READ_BIT, data_buffer, 1, TWI_ISSUE_STOP)) return false; js_data = (~data_buffer[0] & 0x1D); // Select the useful bits. if((js_data & 0x01) != 0) // Check joystick position. { val[X] = -1; } else if((js_data & 0x10) != 0) { val[X] = 1; } else { val[X] = 0; } if((js_data & 0x04) != 0) { val[Y] = 1; } else if((js_data & 0x08) != 0) { val[Y] = -1; } else { val[Y] = 0; } return true; }
bool fb_sleep(uint8_t faceNum, bool sleepEnabled) { uint8_t twiBuf[2]; bool success; if (faceNum > 6) { return false; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_SLEEP; if (sleepEnabled) { twiBuf[1] = 0x01; } else { twiBuf[1] = 0x00; } success = twi_master_transfer((faceNum << 1), twiBuf, 2, true); /* After sending a sleep command, we do not attempt to read a response * because doing so will wake-up the daughterboard processor. */ twi_master_deinit(); return success; }
bool nrf6350_lcd_off(void) { nrf_delay_us(10000); data_buffer[0] = FUNC_SET; data_buffer[1] = 0x08; return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP); }
bool AT24CXX_WriteOneByte(uint8_t register_address, uint8_t value) { uint8_t w2_data[2]; w2_data[0] = register_address; w2_data[1] = value; return twi_master_transfer(AT24XXADDRESS, w2_data, 2, TWI_ISSUE_STOP); }
// send data size_t rgb_lcd_write(uint8_t value) { unsigned char dta[2] = {0x40, value}; // ardunio: i2c_send_byteS(dta, 2); twi_master_transfer (LCD_ADDRESS, dta, 2, true); return 1; // assume sucess }
/********************************************** // // write cmd api **********************************************/ void Write_Command(uint8_t command) { uint8_t send[2]; // send[0] = Slave_Address; send[0] = OP_Command; send[1] = command; twi_master_transfer(Slave_Address, send, 2 ,TWI_ISSUE_STOP ); }
/********************************************** // // write data api // **********************************************/ void Write_Data(uint8_t data) { uint8_t send[2]; // send[0] = Slave_Address; send[0] = OP_Data; send[1] = data; twi_master_transfer(Slave_Address, send, 2 ,TWI_ISSUE_STOP ); }
bool mpu6050_writeBytes(uint8_t *addrData, uint8_t nBytes) { /* The caller must initialize the TWI interface */ if (!twi_master_get_init()) { return false; } return twi_master_transfer((mpu6050Address<<1), addrData, nBytes, TWI_ISSUE_STOP); }
char mpu6050_read(uint8_t device_addr, uint8_t register_addr, uint8_t *register_data, uint8_t read_length) { if(read_length < 1) { xprintf("read length error.\r\n"); return false; } else { device_addr = (device_addr << 1) & 0xFE; twi_master_transfer(device_addr, ®ister_addr, 1, TWI_DONT_ISSUE_STOP); // write device_addr = device_addr | 0x01; twi_master_transfer(device_addr, register_data, read_length, TWI_ISSUE_STOP); // read return true; } }
bool mpu6050_register_write(uint8_t register_address, uint8_t value) { uint8_t w2_data[2]; w2_data[0] = register_address; w2_data[1] = value; return twi_master_transfer(m_device_address, w2_data, 2, TWI_ISSUE_STOP); }
static bool hs3000a_write(uint8_t register_address, uint8_t value) { uint8_t w2_data[2]; w2_data[0] = register_address; w2_data[1] = value; return twi_master_transfer(HS3000A_ADDR, w2_data, 2, TWI_ISSUE_STOP); }
void tcs3771_init(void) { uint8_t data[] = { TCS3771_CONTROL_PDIODE_IR }; tcs3771_write_register(TCS3771_CONTROL, data, sizeof(data)); uint8_t data2[] = { 1 }; tcs3771_write_register(TCS3771_PPULSE, data2, sizeof(data2)); uint8_t data3[] = { 216 }; tcs3771_write_register(TCS3771_WTIME, data3, sizeof(data3)); uint16_t data4[] = { 0x130, /* lower limit */ 0x170 /* high limit */ }; tcs3771_write_register(TCS3771_PILT, data4, sizeof(data4)); uint16_t data6[] = { 0x130, /* lower limit */ 0x170 /* high limit */ }; tcs3771_write_register(TCS3771_AILT, data6, sizeof(data6)); uint8_t data5[] = { TCS3771_PERS_PPERS(10) }; tcs3771_write_register(TCS3771_PERS, data5, sizeof(data5)); uint8_t data1[] = { TCS3771_ENABLE_PON | TCS3771_ENABLE_PEN | TCS3771_ENABLE_WEN | TCS3771_ENABLE_AEN | TCS3771_ENABLE_PIEN | TCS3771_ENABLE_AIEN }; tcs3771_write_register(TCS3771_ENABLE, data1, sizeof(data1)); //reset RGBC and PROX INTs uint8_t int_reset[] = { // 0x05 CLEAR PROX INT // 0x06 CLEAR RGBC INT // 0x07 CLEAR BOTH INT TCS3771_COMMAND_SELECT | TCS3771_COMMAND_TYPE_SPECIAL | 0x07 }; twi_master_transfer(TCS3771, int_reset, sizeof(int_reset), TWI_ISSUE_STOP); uint8_t rgb_cycles[] = { 252 }; tcs3771_write_register(TCS3771_ATIME, rgb_cycles, sizeof(rgb_cycles)); uint8_t prox_cycles[] = { 254 }; tcs3771_write_register(TCS3771_PTIME, prox_cycles, sizeof(prox_cycles)); }
bool nrf6350_lcd_clear(void) { nrf_delay_us(10000); data_buffer[0] = FUNC_SET; data_buffer[1] = (uint8_t)(DDRAM_ADR + LCD_UPPER_LINE); if (!twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP)) return false; if (!twi_master_transfer(LCD_ADDR << 1, empty_str, 18, TWI_ISSUE_STOP)) { return false; } data_buffer[1] = DDRAM_ADR + LCD_LOWER_LINE; if (!twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP)) return false; if (!twi_master_transfer(LCD_ADDR << 1, empty_str, 18, TWI_ISSUE_STOP)) return false; return true; }