Uint16 OSK5912_I2C_write( Uint16 slave_addr, Uint8* data, Uint16 length ) {
Uint16 *pdata = ( Uint16 * )data;
Uint16 last_byte = ( data[length-1] & 0xFF ); Uint16 status;
Int16 i, timecount, timeout = 0x1000;
/* Set slave address */ I2C_SA = slave_addr;
/* Set data count */ I2C_CNT = length;
/* Poll BB field on I2C_STAT - Wait for bus to become free */ for ( i = 0 ; I2C_STAT & I2C_STAT_BB ; i++ ) if ( i > timeout ) return 2001;
/* Configure Options for Transmitting - Master Transmit mode */ I2C_CON = 0
| I2C_CON_I2C_EN // I2C Enable | I2C_CON_MST // Master Mode | I2C_CON_TX // Transmit Mode ;
/* Set Mode:
* OSK5912_I2C_MODE_SADP // (S)tart..(A)ddr..(D)ata..(n)..sto(P) * OSK5912_I2C_MODE_SADD // (S)tart..(A)ddr..(D)ata..(n)..(D)ata */ I2C_CON |= OSK5912_I2C_MODE_SADP;
/* Poll until complete */
for ( timecount = 0 ; timecount < timeout ; timecount++ ) {
/* Read I2C Status register */ status = I2C_STAT;
/* Check for NACK */
if ( ( status & I2C_STAT_NACK ) != I2C_STAT_NACK ) {
/* Check for ARDY */
if ( ( status & I2C_STAT_ARDY ) != I2C_STAT_ARDY ) {
/* Check for XRDY */
if ( ( status & I2C_STAT_XRDY ) == I2C_STAT_XRDY )
{
/* Send Data */ if ( length == 1 ) {
I2C_DATA = last_byte; length--; } else {
I2C_DATA = *pdata++; length -= 2; }
/* Clear Transmit Ready field */ I2C_STAT &= I2C_STAT_XRDY; } }
/* Check for Errors - refresh status register */ status = I2C_STAT;
/* Check for Arbitration Lost */
if ( ( status & I2C_STAT_AL ) == I2C_STAT_AL ) {
I2C_STAT &= I2C_STAT_AL; return 1001; }
/* Check for NACK */
if ( ( status & I2C_STAT_NACK ) == I2C_STAT_NACK ) {
I2C_STAT &= I2C_STAT_NACK; return 1002; }
/* Check for Register Access Ready - Good Condition */
if ( ( I2C_STAT & I2C_STAT_ARDY ) == I2C_STAT_ARDY ) {
I2C_STAT &= I2C_STAT_ARDY; return 0; } } }
return 2002;
}
void main() {
OSK5912_AIC23_CodecHandle hCodec;
/* Start the codec */
hCodec = OSK5912_AIC23_openCodec( 0, &aic23config ); }