uint32_t i2c_read_buff(uint32_t dev_addr, uint8_t reg, uint8_t * dat, uint8_t n){ uint32 rc = 0; int i = 0; rc = I2C_I2CMasterSendStart(dev_addr, I2C_I2C_WRITE_XFER_MODE); /* Error Checking */ if(rc != I2C_I2C_MSTR_NO_ERROR){ LOG_ERROR("Device Not On Bus %02x rc = %i", dev_addr, rc); I2C_I2CMasterSendStop(); return HAPTIC_WRITE_ERROR; } I2C_I2CMasterWriteByte( reg); I2C_I2CMasterSendRestart(dev_addr, I2C_I2C_READ_XFER_MODE); /* Copy In the Approprate ammount of data */ for(i = 0; i < (n - 1); i++){ dat[i] = (uint8_t) I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); } /* For the last read make sure to send a NACK */ dat[i] = (uint8_t) I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA); rc = I2C_I2CMasterSendStop(); return rc; }
/******************************************************************************* * Function Name: DS1307_ReadTimeToStruct ******************************************************************************** * * Summary: * Read real time from chip DS1307 * * Return: * status the I2C connect * *******************************************************************************/ static uint32 DS1307_ReadTimeToStruct(void) { uint8_t data[DS1307_NUMBER_OF_BYTES]; uint32_t status; int i; I2C_Start(); /*reset pointer register reading*/ //I2C_I2CMasterClearStatus(); status = I2C_I2CMasterSendStart(DS1307_SLAVE_ADRESS,I2C_I2C_WRITE_XFER_MODE); if(status == I2C_I2C_MSTR_NO_ERROR) { I2C_I2CMasterWriteByte(DS1307_NULL_ADRESS); } else { /*error connect from DS1307*/ return status; } I2C_I2CMasterSendStop(); /*read data*/ I2C_I2CMasterClearStatus(); status = I2C_I2CMasterSendStart(DS1307_SLAVE_ADRESS,I2C_I2C_READ_XFER_MODE); if(I2C_I2C_MSTR_NO_ERROR == status) { /*write data in buffer*/ for(i=0; i<DS1307_NUMBER_OF_BYTES; i++) { if(i < DS1307_NUMBER_OF_BYTES-1) { data[i] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); } else { data[i] = I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA); } } } I2C_I2CMasterSendStop(); I2C_Stop(); /*write data from buffer in struct*/ DS1307_currentTimeDate.second = (data[DS1307_SEC_ADRESS]& DS1307_SEC_MASK)+(((data[DS1307_SEC_ADRESS]&DS1307_10_SEC_MASK)>>4)*10); DS1307_currentTimeDate.minutes = (data[DS1307_MIN_ADRESS]& DS1307_MIN_MASK)+(((data[DS1307_MIN_ADRESS]&DS1307_10_MIN_MASK)>>4)*10); DS1307_currentTimeDate.timeFormat = (data[DS1307_HOUR_ADRESS] & DS1307_TIME_FORMAT_MASK)>>6; if(DS1307_currentTimeDate.timeFormat == DS1307_12TIME_FORMAT) { DS1307_currentTimeDate.hours = (data[DS1307_HOUR_ADRESS]& DS1307_HOUR_MASK)+(((data[DS1307_HOUR_ADRESS]&DS1307_10_12HOUR_MASK)>>4)*10); DS1307_currentTimeDate.AmRm = (data[DS1307_HOUR_ADRESS]&DS1307_AM_RM_MASK)>>5; }
uint32_t i2c_read_reg(uint32_t dev_addr, uint8_t reg){ uint32_t data = 0; uint32_t rc; rc = I2C_I2CMasterSendStart(dev_addr, I2C_I2C_WRITE_XFER_MODE); /* Error Checking */ if(rc != I2C_I2C_MSTR_NO_ERROR){ LOG_ERROR("Device Not On Bus %02x rc = %i", dev_addr, rc); I2C_I2CMasterSendStop(); return HAPTIC_WRITE_ERROR; } I2C_I2CMasterWriteByte( reg); I2C_I2CMasterSendRestart(dev_addr, I2C_I2C_READ_XFER_MODE); /* For the last read make sure to send a NACK */ data = I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA); LOG_TRACE( "Read Reg: %02x %02x" , reg, data); I2C_I2CMasterSendStop(); return data; }