/* ** =================================================================== ** Method : EE241_ReadBlock (component 24AA_EEPROM) ** Description : ** Read a block of memory. ** Parameters : ** NAME - DESCRIPTION ** addr - Address where to read the memory ** * data - Pointer to a buffer where to store the ** data ** dataSize - Size of buffer the data pointer ** is pointing to ** Returns : ** --- - Error code, possible values ** ERR_OK - OK ** otherwise it can return an error code of ** the underlying communication protocol. ** =================================================================== */ byte EE241_ReadBlock(EE241_Address addr, byte *data, word dataSize) { uint8_t res; #if EE241_DEVICE_ID==EE241_DEVICE_ID_8 uint8_t addr8; addr8 = (uint8_t)(addr&0xff); #else uint8_t addr16[2]; /* big endian address on I2C bus needs to be 16bit */ addr16[0] = (uint8_t)(addr>>8); /* 16 bit address must be in big endian format */ addr16[1] = (uint8_t)(addr&0xff); #endif res = GI2C1_SelectSlave(EE241_DEVICE_ADDR(addr)); if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } #if EE241_DEVICE_ID==EE241_DEVICE_ID_8 res = GI2C1_WriteBlock(&addr8, 1, GI2C1_DO_NOT_SEND_STOP); /* send 8bit address */ #else res = GI2C1_WriteBlock(addr16, 2, GI2C1_DO_NOT_SEND_STOP); /* send 16bit address */ #endif if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } res = GI2C1_ReadBlock(data, dataSize, GI2C1_SEND_STOP); if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } return GI2C1_UnselectSlave(); }
uint8_t MCP4728_Read(uint8_t buf[3*8], size_t bufSize) { uint8_t res; if (bufSize!=2*3*4) { return ERR_FAILED; } res = GI2C1_SelectSlave(MCP4728_I2C_ADDRESS); if (res!=ERR_OK) { return res; } res = GI2C1_ReadBlock(buf, bufSize, GI2C1_SEND_STOP); if (res!=ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } res = GI2C1_UnselectSlave(); if (res!=ERR_OK) { return res; } return ERR_OK; }