static int ak7343_read(struct i2c_client *client, unsigned long reg, unsigned long *val) { unsigned long mfp_pin[2]; unsigned long i2c0_mfps[] = { GPIO053_GPIO_53, /* SCL */ GPIO054_GPIO_54, /* SDA */ }; unsigned long scl, sda; int ret = 0; scl = MFP_PIN_GPIO53; sda = MFP_PIN_GPIO54; i2c_lock_adapter(client->adapter); if (gpio_request(scl, "SCL")) { pr_err("Failed to request GPIO for SCL pin!\n"); ret = -1; goto out0; } if (gpio_request(sda, "SDA")) { pr_err("Failed to request GPIO for SDA pin!\n"); ret = -1; goto out1; }; mfp_pin[0] = mfp_read(MFP_PIN_GPIO53); mfp_pin[1] = mfp_read(MFP_PIN_GPIO54); mfp_config(i2c0_mfps, ARRAY_SIZE(i2c0_mfps)); i2c_start(scl, sda); if (i2c_sendbyte(scl, sda, (client->addr<<1) | 1) < 0) { ret = -1; goto out2; } if (i2c_sendbyte(scl, sda, (u8)reg) < 0) { ret = -1; goto out2; } if (i2c_rcvbyte(scl, sda, (u8 *)val) < 0) { ret = -1; goto out2; } out2: i2c_stop(scl, sda); mfp_write(MFP_PIN_GPIO53, mfp_pin[0]); mfp_write(MFP_PIN_GPIO54, mfp_pin[1]); gpio_free(sda); out1: gpio_free(scl); out0: i2c_unlock_adapter(client->adapter); return ret; }
unsigned int8 RTCRead(unsigned int8 address) { int1 nack; unsigned int8 value = 0; i2c_start(); nack = i2c_write(PCF8583_WRITE); if (nack) { i2c_stop(); printf("rtcread nack 1\n\r"); } else { nack = i2c_write(address); if (!nack) { i2c_start(); nack = i2c_write(PCF8583_READ); if (!nack) { value = i2c_read(); } else printf("rtcread nack 3\n\r"); } else printf("rtcread nack 2\n\r"); i2c_stop(); } return value; }
// Read a byte, store in "value". int8_t i2c_read_byte(uint8_t address, uint8_t *value) { uint8_t ret=0; if (i2c_start()!=0) //I2C START return(1); //Stop and return 0 if START fail ret |= i2c_write((address<<1)|I2C_READ_BIT); // Write the I2C 7-bit address with R bit if (ret != 0) // Returns 1 if failed return(1); *value = i2c_read(WITH_NACK); // Read byte from I2C Bus with NAK i2c_stop(); //I2C STOP return(0); // Return success }
void doI2CWrite(char** argv, char argc) { if(argc == 3) { i2c_start(); i2c_write(0x98); i2c_write(strtoint(argv[1])); i2c_write(strtoint(argv[2])); i2c_stop(); printf("OK\n"); } else printf("err: addr1 val1 addr2 val2\n"); }
void rtc_write_page(uint8_t start_register, uint8_t *buffer, uint8_t size) { uint8_t i; i2c_start(); // 1 i2c_write(RTC_ADDRESS | I2C_WRITE); // 2+3 i2c_write(start_register); // 4+5 for(i = 0; i < size; i++) i2c_write(buffer[i]); // 8+9 i2c_stop(); // 6 }
uint8_t i2c_WriteBuffer(uint8_t nAddress, uint8_t *pByte, uint8_t nLen) { uint8_t stat = 0; stat = i2c_start( nAddress, TW_WRITE ); while ( nLen && !stat) { stat = i2c_write( *pByte ); } i2c_stop(); return stat; }
static long read_mbbi(mbbiRecord *prec) { epicsMutexLock(mutex); i2c_start(); i2c_write_byte(PCA9571_RD_ADDR); prec->rval = i2c_read_byte(ACK); i2c_stop(); epicsMutexUnlock(mutex); if(prec->rval == 255) prec->rval = 0; return(0); }
void Lcd::write(uint8_t value) { // If it is a new line, set the cursor to the next line (1,0) if (value == '\n') setCursor(1,0); else { i2c_start(m_i2cAddress); i2c_write(RAM_WRITE_CMD); i2c_write(value); i2c_stop (); delay_ms (m_charDelay); } }
uint8 accel_read(uint8 reg_addr) { uint8 result; i2c_start(); i2c_write((MMA7455_I2CADDR << 1) | I2C_WRITE); i2c_write(reg_addr); i2c_repeated_restart(); i2c_write((MMA7455_I2CADDR << 1) | I2C_READ); result = i2c_read(); i2c_nack(); i2c_stop(); return result; }
// Sende in Array MD49commands gesetzte Befehle für MD49 an AVR_Slave_DriveControl // im Master-Transmitter-Mode void sendMD49commands(void){ uint8_t i; if(!(i2c_start(SLAVE_ADRESSE+I2C_WRITE))){ // Slave bereit zum schreiben? i2c_write(0x00); // Buffer Startadresse setzen for (i=0;i<15;i++){ i2c_write(MD49commands[i]); } i2c_stop(); // Zugriff beenden } else { // /* Fehlerbehandlung... */ } }
void show_version_msg() { // show version message i2c_start(LCD_I2C_DEVICE_ID); i2c_tx(10); i2c_tx(2); i2c_tx(6); i=0; while (pgm_read_byte(&msg_version[i]) != '\0') i2c_tx(pgm_read_byte(&msg_version[i++])); i2c_stop(); }
void rtc_w(void) { i2c_start(); // issue start signal i2c_wb(0xA2); // address PCF8563 i2c_wb(0); // start from word at address 0 (configuration word) i2c_wb(0x20); // write $20 to config. (pause counter...) i2c_wb(0); // write 0 to cents word i2c_wb(0x20); // write $20 to seconds word i2c_wb(0x34); // write $30 to minutes word i2c_wb(0x11); // write $11 to hours word i2c_wb(0x24); // write $24 to day word i2c_wb(0x04); // write $04 to weekday i2c_wb(0x02); // write $08 to month i2c_wb(0x14); // write $08 to year i2c_stop(); // issue stop signal i2c_start(); // issue start signal i2c_wb(0xA2); // address PCF8530 i2c_wb(0); // start from word at address 0 i2c_wb(0); // write 0 to config word (enable counting) i2c_stop(); // issue stop signal }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; int count; int value; i2c_start(obj); // Wait until SB flag is set timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB) == RESET) { timeout--; if (timeout == 0) { return -1; } } i2c->DR = __HAL_I2C_7BIT_ADD_READ(address); // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { timeout--; if (timeout == 0) { return -1; } } __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); // Read all bytes except last one for (count = 0; count < (length - 1); count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } // If not repeated start, send stop. // Warning: must be done BEFORE the data is read. if (stop) { i2c_stop(obj); } // Read the last byte value = i2c_byte_read(obj, 1); data[count] = (char)value; return length; }
// le do endereco de 8 bits, com endereco de 8 bytes (2k bit max) unsigned char I2C_EEIN (unsigned char address) { unsigned char data; i2c_start(); i2c_write(0xA0); i2c_write(address); i2c_repStart(); i2c_write(0xA1); data=i2c_read(0); i2c_stop(); return(data); }
/** * Set the speed of the motors * * @param motor_a Speed of motor 1 * @param motor_b Speed of motor 2 * @param motor_c Speed of motor 3 * @param motor_d Speed of motor 4 * @return If the setting was successful */ uint8_t motorcontrol_set_motors(uint8_t motor_a, uint8_t motor_b, uint8_t motor_c, uint8_t motor_d) { #ifdef MOTORCONTROL_AVAILABLE if (i2c_start(I2C_ADDR_MOTORCONTROL + I2C_WRITE)) { i2c_write(MC_MOTOR_1_SPEED); i2c_write(motor_a); i2c_write(motor_b); i2c_write(motor_c); i2c_write(motor_d); i2c_stop(); return 1; } #endif /* MOTORCONTROL_AVAILABLE */ return 0; }
//---------------------------------------------------------- // EEPROM書き込み // 引数(スレーブアドレス,アドレス,データ,バイト数) void i2c_2_write( unsigned char addr, unsigned char* data, unsigned char bytecnt) { i2c_start(); i2c_writebyte(0xD0); // SLA + W i2c_writebyte(addr); // address(high 8bits) while(bytecnt > 0){ i2c_writebyte(*data++); bytecnt--; } i2c_stop(); }
//New version WH, Tested OK for Start and Repeated Start //Old version was Wrong: Calls i2c_start without setting address, i2c_do_read continues before checking status, status check for wrong value int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { int count, status; //Store the address+RD and then generate STA I2C_DAT(obj) = address | 0x01; i2c_start(obj); // Wait for completion of STA and Sending of SlaveAddress+RD and first Read byte i2c_wait_SI(obj); status = i2c_status(obj); if (status == 0x03) { // NAK on SlaveAddress i2c_stop(obj); return I2C_ERROR_NO_SLAVE; } // Read in all except last byte for (count = 0; count < (length-1); count++) { // Wait for it to arrive, note that first byte read after address+RD is already waiting i2c_wait_SI(obj); status = i2c_status(obj); if (status != 0x01) { // RX RDY i2c_stop(obj); return count; } data[count] = I2C_DAT(obj) & 0xFF; // Store read byte obj->i2c->MSTCTL = (1 << 0); // Send ACK and Continue to read } // Read final byte // Wait for it to arrive i2c_wait_SI(obj); status = i2c_status(obj); if (status != 0x01) { // RX RDY i2c_stop(obj); return count; } data[count] = I2C_DAT(obj) & 0xFF; // Store final read byte // If not repeated start, send stop. if (stop) { i2c_stop(obj); // Also sends NAK for last read byte } else { repeated_start = 1; } return length; }
void write_nineAxisMag(uint8_t address, uint8_t byte) { unsigned char ret; ret = i2c_start(NineAxisMag+I2C_WRITE); // set device address and write mode if ( ret ) { /* failed to issue start condition, possibly no device found */ printString("Could not write to Nine-Axis Mag !!!\r\n"); i2c_stop(); } else { /* issuing start condition ok, device accessible */ i2c_write(address); // write register address i2c_write(byte); // ret=0 -> OK, ret=1 -> no ACK i2c_stop(); // set stop conditon = release bus } }
int16_t lm75_gettemp(uint8_t device){ device = LM75_BASE + (device << 1); if(i2c_start(device + 1)){ return 100; }else{ int16_t a = i2c_readAck() << 1; uint8_t b = i2c_readNak() >> 7; i2c_stop(); if(a & 256) a |= 0xfe00; return a+b; //return 50*2+1; } }
/* This performs a fast read of one or more consecutive bytes from an * I2C device. Not all devices support consecutive reads of more than * one byte; for these devices use efx_i2c_read() instead. */ int efx_i2c_fast_read(struct efx_i2c_interface *i2c, u8 device_id, u8 offset, u8 *data, unsigned int len) { int i; int rc; EFX_WARN_ON_PARANOID(getsda(i2c) != 1); EFX_WARN_ON_PARANOID(getscl(i2c) != 1); EFX_WARN_ON_PARANOID(data == NULL); EFX_WARN_ON_PARANOID(len < 1); /* Select device and starting offset */ i2c_start(i2c); rc = i2c_send_byte(i2c, i2c_write_cmd(device_id)); if (rc) goto out; rc = i2c_send_byte(i2c, offset); if (rc) goto out; /* Read data from device */ i2c_start(i2c); rc = i2c_send_byte(i2c, i2c_read_cmd(device_id)); if (rc) goto out; for (i = 0; i < (len - 1); i++) /* Read and acknowledge all but the last byte */ data[i] = i2c_recv_byte(i2c, 1); /* Read last byte with no acknowledgement */ data[i] = i2c_recv_byte(i2c, 0); out: i2c_stop(i2c); i2c_release(i2c); return rc; }
// Returns a set of acceleration and raw magnetic readings from the cmp01a. void read_data_raw(vector *a, vector *m) { // read accelerometer values i2c_start(); i2c_write_byte(0x30); // write acc i2c_write_byte(0xa8); // OUT_X_L_A, MSB set to enable auto-increment i2c_start(); // repeated start i2c_write_byte(0x31); // read acc unsigned char axl = i2c_read_byte(); unsigned char axh = i2c_read_byte(); unsigned char ayl = i2c_read_byte(); unsigned char ayh = i2c_read_byte(); unsigned char azl = i2c_read_byte(); unsigned char azh = i2c_read_last_byte(); i2c_stop(); // read magnetometer values i2c_start(); i2c_write_byte(0x3C); // write mag i2c_write_byte(0x03); // OUTXH_M i2c_start(); // repeated start i2c_write_byte(0x3D); // read mag unsigned char mxh = i2c_read_byte(); unsigned char mxl = i2c_read_byte(); unsigned char mzh = i2c_read_byte(); unsigned char mzl = i2c_read_byte(); unsigned char myh = i2c_read_byte(); unsigned char myl = i2c_read_last_byte(); i2c_stop(); a->x = axh << 8 | axl; a->y = ayh << 8 | ayl; a->z = azh << 8 | azl; m->x = mxh << 8 | mxl; m->y = myh << 8 | myl; m->z = mzh << 8 | mzl; }
bool I2C_MultipleRead(alt_u32 clk_base, alt_u32 data_base, alt_8 DeviceAddr, alt_u8 szData[], alt_u16 len){ int i; bool bSuccess = TRUE; //alt_u8 DeviceAddr, alt_u8 ControlAddr = 0; // device id //DeviceAddr = HMB_E2_I2C_ID; i2c_start(clk_base, data_base); if (!i2c_write(clk_base, data_base, DeviceAddr)){ // send ID bSuccess = FALSE; I2C_DEBUG(("I2C HMB_E2 Fail: Address NACK!\n")); } if (bSuccess && !i2c_write(clk_base, data_base, ControlAddr)){ // send sub-address bSuccess = FALSE; I2C_DEBUG(("I2C HMB_E2 Fail: SubAddress NACK!\n")); } if (bSuccess) i2c_start(clk_base, data_base); // restart DeviceAddr |= 1; // Read if (bSuccess && !i2c_write(clk_base, data_base, DeviceAddr)){ // send id bSuccess = FALSE; I2C_DEBUG(("I2C HMB_E2 Fail: Address+1 NACK!\n")); } if (bSuccess){ for(i=0;i<len && bSuccess;i++){ i2c_read(clk_base, data_base, &szData[i], (i==(len-1))?FALSE:TRUE); // read } } i2c_stop(clk_base, data_base); return bSuccess; }
static void select_row(uint8_t row) { if (row < 7) { // select on mcp23018 if (mcp23018_status) { // if there was an error // do nothing } else { // set active row low : 0 // set other rows hi-Z : 1 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out; mcp23018_status = i2c_write( 0xFF & ~(1<<row) & ~(0<<8) ); if (mcp23018_status) goto out; out: i2c_stop(); } } else { // select on teensy // Output low(DDR:1, PORT:0) to select switch (row) { case 7: DDRB |= (1<<0); PORTB &= ~(1<<0); break; case 8: DDRB |= (1<<1); PORTB &= ~(1<<1); break; case 9: DDRB |= (1<<2); PORTB &= ~(1<<2); break; case 10: DDRB |= (1<<3); PORTB &= ~(1<<3); break; case 11: DDRD |= (1<<2); PORTD &= ~(1<<3); break; case 12: DDRD |= (1<<3); PORTD &= ~(1<<3); break; } } }
//****************************************************************** //Function to a character string to EEPROM //****************************************************************** unsigned char EEPROM_write(unsigned char highAddress, unsigned char lowAddress, unsigned char* string) { unsigned char errorStatus, data; errorStatus = i2c_start(); if(errorStatus == 1) { i2c_stop(); return(1); } errorStatus = i2c_sendAddress(EEPROM_W); if(errorStatus == 1) { i2c_stop(); return(1); } /* errorStatus = i2c_sendData(highAddress); if(errorStatus == 1) { i2c_stop(); return(1); } */ errorStatus = i2c_sendData(lowAddress); if(errorStatus == 1) { i2c_stop(); return(1); } while(*string) { errorStatus = i2c_sendData(*(string++)); if(errorStatus == 1) { i2c_stop(); return(1); } } i2c_stop(); return(0); }
void ICACHE_FLASH_ATTR SSD1306Display::display() { const char* p = buffer; const char* stopper = buffer + buffer_size; while (p < stopper) { i2c_start(); if (!i2c->send_byte(0x40)) log("No ack from display for data start!\n"); for (int x = 0; x < 16; ++x) { if (!i2c->send_byte(*p++)) log("No ack from display for data byte!\n"); } i2c->end_transmission(); } }
uint8_t mma8451_read(uint8_t addr) { delay(1); i2c_start(I2C0_B); i2c_write(I2C0_B, MMA8451_I2C_ADDRESS | I2C_WRITE); i2c_write(I2C0_B, addr); i2c_repeated_start(I2C0_B); i2c_write(I2C0_B, MMA8451_I2C_ADDRESS | I2C_READ); i2c_set_rx(I2C0_B); i2c_give_nack(I2C0_B); i2c_read(I2C0_B); i2c_wait(I2C0_B); i2c_stop(I2C0_B); return i2c_read(I2C0_B); }
uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length) { if (i2c_start(devaddr | 0x00)) return 1; i2c_write(regaddr); for (uint16_t i = 0; i < length; i++) { if (i2c_write(data[i])) return 1; } i2c_stop(); return 0; }
i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_status_t status = i2c_start(address | I2C_WRITE, timeout); if (status) return status; for (uint16_t i = 0; i < length; i++) { status = i2c_write(data[i], timeout); if (status) return status; } status = i2c_stop(timeout); if (status) return status; return I2C_STATUS_SUCCESS; }
uint8_t i2c_transmit(uint8_t address, uint8_t * data, uint16_t length) { uint16_t i; if (i2c_start(address | I2C_WRITE)) return 1; for (i = 0; i < length; i++) { if (i2c_write(data[i])) return 1; } i2c_stop(); return 0; }
void memWriteByte(uint32_t address, uint8_t data) { uint8_t addA, addB, memAddress = MEMTWIADDRESS; if (address >= 65536) { // Address needs more than 16 bits, we have to set the PAGE bit in i2c address memAddress |= 2; } addA = (address & 0xFF00) >> 8; addB = address & 0xFF; if (i2c_start(memAddress | I2C_WRITE) == 0) { i2c_write(addA); i2c_write(addB); // Give address to memory i2c_write(data); i2c_stop(); } }