int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { int cnt; int status; /* There is a STOP condition for last processing */ if (obj->last_stop_flag != 0) { status = i2c_start(obj); if (status != 0) { i2c_set_err_noslave(obj); return I2C_ERROR_BUS_BUSY; } } obj->last_stop_flag = stop; /* Send Slave address */ status = i2c_do_write(obj, address); if (status != 0) { i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } /* Wait send end */ status = i2c_wait_TEND(obj); if ((status != 0) || ((REG(SR2.UINT32) & SR2_NACKF) != 0)) { /* Slave sends NACK */ i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } /* Send Write data */ for (cnt=0; cnt<length; cnt++) { status = i2c_do_write(obj, data[cnt]); if(status != 0) { i2c_set_err_noslave(obj); return cnt; } else { /* Wait send end */ status = i2c_wait_TEND(obj); if ((status != 0) || ((REG(SR2.UINT32) & SR2_NACKF) != 0)) { /* Slave sends NACK */ i2c_set_err_noslave(obj); return I2C_ERROR_NO_SLAVE; } } } /* If not repeated start, send stop. */ if (stop) { (void)i2c_set_STOP(obj); (void)i2c_wait_STOP(obj); i2c_set_SR2_NACKF_STOP(obj); } else { (void)i2c_restart(obj); (void)i2c_wait_START(obj); /* SR2.START = 0 */ REG(SR2.UINT32) &= ~SR2_START; } return length; }
int i2c_slave_write(i2c_t *obj, const char *data, int length) { int count = 0; int status = 0; if(length <= 0) { return 0; } while ((count < length) && (status == 0)) { status = i2c_do_write(obj, data[count]); if(status == 0) { /* Wait send end */ status = i2c_wait_TEND(obj); if ((status != 0) || ((count < (length - 1)) && ((REG(SR2.UINT32) & SR2_NACKF) != 0))) { /* NACK */ break; } } count++; } /* dummy read */ (void)REG(DRR.UINT32); (void)i2c_wait_STOP(obj); i2c_set_SR2_NACKF_STOP(obj); return count; }
int i2c_slave_write(i2c_t *obj, const char *data, int length) { int count = 0; int status = 0; if(length <= 0) { return 0; } while ((count < length) && (status == 0)) { status = i2c_do_write(obj, data[count]); count++; } if (status == 0) { /* Wait send end */ status = i2c_wait_TEND(obj); if (status != 0) { i2c_set_err_noslave(obj, 1); return 0; } } /* dummy read */ (void)REG(DRR.UINT32); (void)i2c_wait_STOP(obj); i2c_set_SR2_NACKF_STOP(obj); return count; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { int i, status; // full reset i2c_reg_reset(obj); status = i2c_start(obj); if ((status == 0xff)) { i2c_stop(obj); i2c_wait_STOP(obj); return I2C_ERROR_BUS_BUSY; } /**/ status = REG(CR2.UINT32); status = REG(SR2.UINT32); /**/ status = i2c_do_write(obj, address); if (status & 0x10) { i2c_stop(obj); i2c_wait_STOP(obj); return I2C_ERROR_NO_SLAVE; } /**/ status = REG(CR2.UINT32); status = REG(SR2.UINT32); /**/ for (i=0; i<length; i++) { /**/ status = REG(CR2.UINT32); status = REG(SR2.UINT32); /**/ status = i2c_do_write(obj, data[i]); if(status & 0x10) { i2c_stop(obj); i2c_wait_STOP(obj); return i; } } i2c_wait_TEND(obj); // If not repeated start, send stop. if (stop) { i2c_stop(obj); i2c_wait_STOP(obj); } return length; }