int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2cHandle = (I2C_TypeDef *)(obj->i2c); int count; if(!obj->is_setAddress) { if( I2C_Start(I2cHandle, address, I2C_WRITE_SA7) == ERROR ) { return -1; } obj->is_setAddress = 1; obj->ADDRESS = address; } else { I2C_Restart_Structure(I2cHandle, address, I2C_WRITE_SA7); obj->ADDRESS = address; } for (count = 0; count < length; count++) { i2c_byte_write(obj, data[count]); } // If not repeated start, send stop if (stop) { i2c_stop(obj); } return count; }
int I2C::write(int data) { lock(); int ret = i2c_byte_write(&_i2c, data); unlock(); return ret; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; int count; i2c_start(obj); // Send slave address for write I2C_Send7bitAddress(i2c, address, I2C_Direction_Transmitter); // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) { timeout--; if (timeout == 0) { return 0; } } for (count = 0; count < length; count++) { if (i2c_byte_write(obj, data[count]) != 1) { i2c_stop(obj); return 0; } } // If not repeated start, send stop. if (stop) { i2c_stop(obj); } return count; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; int count; if (length == 0) return 0; // Configure slave address, nbytes, reload, end mode and start generation I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); // Write all bytes for (count = 0; count < length; count++) { i2c_byte_write(obj, data[count]); } timeout = FLAG_TIMEOUT; while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { timeout--; if (timeout == 0) return 0; } if(stop) i2c_stop(obj); return count; }
int i2c_slave_write(i2c_t *obj, const char *data, int length) { int count; for (count = 0; count < length; count++) { i2c_byte_write(obj, data[count]); } return count; }
int i2c_slave_write(i2c_t *obj, const char *data, int length) { char size = 0; I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); do { i2c_byte_write(obj, data[size]); size++; } while (size < length); return size; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { i2c_start(obj); if (!i2c_byte_write(obj, (address & 0xFE))) { i2c_stop(obj); return I2C_ERROR_NO_SLAVE; //NACK or error when writing adress. Return 0 as 0 bytes were written } int i; for (i = 0; i < length; i++) { if (!i2c_byte_write(obj, data[i])) { i2c_stop(obj); return i; } } if (stop) { i2c_stop(obj); } return length; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; int count; if (length == 0) return 0; /* update CR2 register */ i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP))) | (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE); for (count = 0; count < length; count++) { i2c_byte_write(obj, data[count]); } // Wait transfer complete timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TC) == RESET) { timeout--; if (timeout == 0) { return 0; } } __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC); // If not repeated start, send stop. if (stop) { i2c_stop(obj); /* Wait until STOPF flag is set */ timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) { timeout--; if (timeout == 0) { return 0; } } /* Clear STOP Flag */ __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF); } return count; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); int timeout; int count; if (length == 0) return 0; 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 0; } } i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address); // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { timeout--; if (timeout == 0) { return 0; } } __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); for (count = 0; count < length; count++) { if (i2c_byte_write(obj, data[count]) != 1) { i2c_stop(obj); return 0; } } // If not repeated start, send stop. if (stop) { i2c_stop(obj); } return count; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); //int timeout; int count; if (length == 0) return 0; // [TODO] The stop is always sent even with I2C_SoftEnd_Mode. To be corrected. // Configure slave address, nbytes, reload, end mode and start or stop generation //if (stop) { I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); //} //else { // I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); //} // Write all bytes for (count = 0; count < length; count++) { if (i2c_byte_write(obj, data[count]) != 1) { i2c_stop(obj); return 0; } } /* if (stop) { // Wait until STOPF flag is set timeout = LONG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) { timeout--; if (timeout == 0) { return 0; } } // Clear STOPF flag I2C_ClearFlag(i2c, I2C_ICR_STOPCF); } */ return count; }
/* Returns number of bytes read */ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { int retval; i2c_start(obj); retval = i2c_byte_write(obj, (address | 1)); if ((!retval) || (length == 0)) { //Write address with W flag (last bit 1) obj->i2c.i2c->CMD = I2C_CMD_STOP | I2C_CMD_ABORT; while(obj->i2c.i2c->STATE & I2C_STATE_BUSY); // Wait until the bus is done return (retval == 0 ? I2C_ERROR_NO_SLAVE : 0); //NACK or error when writing adress. Return 0 as 0 bytes were read } int i = 0; while (i < length) { uint8_t last = (i == length - 1); data[i++] = i2c_byte_read(obj, last); } if (stop) { i2c_stop(obj); } return length; }
int I2C::write(int data) { return i2c_byte_write(&_i2c, data); }
/** Blocking sending data * * @param obj The I2C object * @param address 7-bit address (last bit is 0) * @param data The buffer for sending * @param length Number of bytes to write * @param stop Stop to be generated after the transfer is done * @return * zero or non-zero - Number of written bytes * negative - I2C_ERROR_XXX status */ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { struct i2c_s *obj_s = I2C_S(obj); gd_status_enum status = GD_OK; uint32_t count = 0; int timeout = 0; if (obj_s->global_trans_option == I2C_FIRST_AND_LAST_FRAME || obj_s->global_trans_option == I2C_LAST_FRAME) { if (stop) { obj_s->global_trans_option = I2C_FIRST_AND_LAST_FRAME; } else { obj_s->global_trans_option = I2C_FIRST_FRAME; } } else if (obj_s->global_trans_option == I2C_FIRST_FRAME || obj_s->global_trans_option == I2C_NEXT_FRAME) { if (stop) { obj_s->global_trans_option = I2C_LAST_FRAME; } else { obj_s->global_trans_option = I2C_NEXT_FRAME; } } if (obj_s->global_trans_option == I2C_FIRST_AND_LAST_FRAME || obj_s->global_trans_option == I2C_FIRST_FRAME) { /* wait until I2C_FLAG_I2CBSY flag is reset */ timeout = FLAG_TIMEOUT; while ((i2c_flag_get(obj_s->i2c, I2C_FLAG_I2CBSY)) == SET) { if ((timeout--) == 0) { i2c_stop(obj); return I2C_ERROR_BUS_BUSY; } } } if (obj_s->global_trans_option == I2C_FIRST_AND_LAST_FRAME || obj_s->global_trans_option == I2C_FIRST_FRAME || obj_s->previous_state_mode != I2C_STATE_MASTER_BUSY_TX) { /* generate a START condition */ i2c_start_on_bus(obj_s->i2c); /* ensure the i2c has been started successfully */ timeout = FLAG_TIMEOUT; while ((i2c_flag_get(obj_s->i2c, I2C_FLAG_SBSEND)) == RESET) { if ((timeout--) == 0) { i2c_stop(obj); return I2C_ERROR_BUS_BUSY; } } /* send slave address */ i2c_master_addressing(obj_s->i2c, address, I2C_TRANSMITTER); /* wait until I2C_FLAG_ADDSEND flag is set */ while (!i2c_flag_get(obj_s->i2c, I2C_FLAG_ADDSEND)) { timeout++; if (timeout > 100000) { i2c_stop(obj); return I2C_ERROR_NO_SLAVE; } } /* clear ADDSEND */ i2c_flag_clear(obj_s->i2c, I2C_FLAG_ADDSEND); } obj_s->state = (operation_state_enum)I2C_STATE_MASTER_BUSY_TX; for (count = 0; count < length; count++) { status = (gd_status_enum)i2c_byte_write(obj, data[count]); if (status != 1) { i2c_stop(obj); return count; } } obj_s->previous_state_mode = obj_s->state; /* if not sequential write, then send stop */ if (stop) { i2c_stop(obj); } return count; }