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; }
uint8_t MPC4728_SingleWrite(uint8_t channel, uint16_t val) { uint8_t res; uint8_t data[3]; /* 01011|DAC1|DAC0|UDAC VREF|PD1|PD0|Gx|D11-D0 */ data[0] = 0xB0|((channel&0x3)<<1); data[1] = (uint8_t)(val>>8); data[2] = (uint8_t)(val&0xff); res = GI2C1_SelectSlave(MPC4728_I2C_ADDRESS); if (res!=ERR_OK) { return res; } res = GI2C1_WriteBlock(&data[0], sizeof(data), GI2C1_SEND_STOP); if (res!=ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } res = GI2C1_UnselectSlave(); if (res!=ERR_OK) { return res; } return ERR_OK; }
/* ** =================================================================== ** Method : EE241_WriteByte (component 24AA_EEPROM) ** Description : ** Writes a single byte to specified address ** Parameters : ** NAME - DESCRIPTION ** addr - The address inside the EEPROM ** data - The data value to write ** Returns : ** --- - Error code, possible values ** ERR_OK - OK ** otherwise it can return an error code of ** the underlying communication protocol. ** =================================================================== */ byte EE241_WriteByte(EE241_Address addr, byte data) { uint8_t res, block[3]; res = GI2C1_SelectSlave(EE241_DEVICE_ADDR(addr)); if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } #if EE241_DEVICE_ID==EE241_DEVICE_ID_8 block[0] = (uint8_t)(addr&0xff); /* low byte of address */ block[1] = data; /* switch to read mode */ res = GI2C1_WriteBlock(block, 2, GI2C1_SEND_STOP); /* send address and data */ #else block[0] = (uint8_t)(addr>>8); /* high byte of address */ block[1] = (uint8_t)(addr&0xff); /* low byte of address */ block[2] = data; /* switch to read mode */ res = GI2C1_WriteBlock(block, sizeof(block), GI2C1_SEND_STOP); /* send address and data */ #endif if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } #if EE241_DO_ACKNOWLEDGE_POLLING /* do acknowledge polling */ block[0] = 0xff; /* dummy value */ do { WAIT1_WaitOSms(EE241_PAGE_WRITE_TIME_MS); res = GI2C1_ProbeACK(block, 1, GI2C1_SEND_STOP, EE241_ACK_POLLING_TIME_US); /* send address and data */ } while(res!=ERR_OK); /* wait until we get an ACK */ #endif /* EE241_DO_ACKNOWLEDGE_POLLING */ if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } return GI2C1_UnselectSlave(); }
/* ** =================================================================== ** Method : GI2C1_WriteAddress (component GenericI2C) ** Description : ** Write to the device: (S+i2cAddr+0), (memAddr), (data)... ** (data+P) ** Parameters : ** NAME - DESCRIPTION ** i2cAddr - I2C address of device ** * memAddr - Pointer to device memory address ** memAddrSize - number of address bytes ** * data - Pointer to data write buffer ** dataSize - Size of data buffer in bytes ** Returns : ** --- - Error code ** =================================================================== */ byte GI2C1_WriteAddress(byte i2cAddr, byte *memAddr, byte memAddrSize, byte *data, word dataSize) { static byte writeBuf[GI2C1_WRITE_BUFFER_SIZE]; byte *p; word i; byte res = ERR_OK; if (GI2C1_SelectSlave(i2cAddr)!=ERR_OK) { return ERR_FAILED; } if (memAddrSize+dataSize>GI2C1_WRITE_BUFFER_SIZE) { return ERR_FAILED; } i = 0; p = memAddr; while(i<GI2C1_WRITE_BUFFER_SIZE && memAddrSize>0) { writeBuf[i++] = *p++; memAddrSize--; } p = data; while(i<GI2C1_WRITE_BUFFER_SIZE && dataSize>0) { writeBuf[i++] = *p++; dataSize--; } for(;;) { /* breaks */ /* send device address, memory address and data */ GI2C1_deviceData.dataTransmittedFlg = FALSE; if (CI2C1_MasterSendBlock(GI2C1_deviceData.handle, writeBuf, i, LDD_I2C_SEND_STOP)!=ERR_OK) { break; /* break for(;;) */ } do { /* Wait until data is sent */ } while (!GI2C1_deviceData.dataTransmittedFlg); break; /* break for(;;) */ } /* for(;;) */ if (GI2C1_UnselectSlave()!=ERR_OK) { return ERR_FAILED; } return res; }
byte EE241_WriteBlockPage(EE241_Address addr, byte *data, word dataSize) { uint8_t res, i, *p, block[EE241_BLOCK_BUF_SIZE+2]; /* additional 2 bytes for the address */ uint16_t eepromPage = (uint16_t)(addr/EE241_PAGE_SIZE); uint8_t offset = (uint8_t)(addr%EE241_PAGE_SIZE); if (dataSize==0 || dataSize>EE241_BLOCK_BUF_SIZE) { return ERR_OVERFLOW; /* you may increase the buffer size in the properties? */ } if (dataSize>EE241_PAGE_SIZE) { uint16_t size; size = (uint16_t)(EE241_PAGE_SIZE-offset); if (size!=0) { res = EE241_WriteBlock(addr, data, size); /* first page write */ if (res != ERR_OK) { return res; } data += size; /* increment data pointer */ addr += size; /* increment address */ dataSize -= size; /* reduce size */ } /* write multiple block of PAGE_SIZE */ while (dataSize>EE241_PAGE_SIZE) { res = EE241_WriteBlock(addr, data, EE241_PAGE_SIZE); if (res != ERR_OK) { return res; } data += EE241_PAGE_SIZE; /* increment data pointer */ addr += EE241_PAGE_SIZE; /* increment address */ dataSize -= EE241_PAGE_SIZE; /* reduce size */ } /* write remainder (if any) */ if (dataSize>0) { return EE241_WriteBlock(addr, data, dataSize); } return ERR_OK; } if (offset+dataSize <= EE241_PAGE_SIZE) { /* no page boundary crossing */ res = GI2C1_SelectSlave(EE241_DEVICE_ADDR(addr)); if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } #if EE241_DEVICE_ID==EE241_DEVICE_ID_8 /* 8 bit address byte, high byte of address have been place in SelectSlave(addr) */ block[0] = (uint8_t)(addr&0xff); /* low byte of address */ p = &block[1]; i = (uint8_t)dataSize; #else /* 16 bit address byte */ block[0] = (uint8_t)(addr>>8); /* high byte of address */ block[1] = (uint8_t)(addr&0xff); /* low byte of address */ p = &block[2]; i = (uint8_t)dataSize; #endif /* copy block */ while(i>0) { *p++ = *data++; i--; } res = GI2C1_WriteBlock(block, dataSize+((EE241_DEVICE_ID==EE241_DEVICE_ID_8)? 1:2), GI2C1_SEND_STOP); /* send address and data */ if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } #if EE241_DO_ACKNOWLEDGE_POLLING /* do acknowledge polling */ block[0] = 0xff; /* dummy value */ do { WAIT1_WaitOSms(EE241_PAGE_WRITE_TIME_MS); res = GI2C1_ProbeACK(block, 1, GI2C1_SEND_STOP, EE241_ACK_POLLING_TIME_US); /* send address and data */ } while(res!=ERR_OK); /* wait until we get an ACK */ if (res != ERR_OK) { (void)GI2C1_UnselectSlave(); return res; } #endif /* EE241_DO_ACKNOWLEDGE_POLLING */ return GI2C1_UnselectSlave(); } else { /* crossing page boundaries: make two page writes */