void i2c_init(i2c_t *obj, PinName sda, PinName scl) { const SWM_Map *swm; uint32_t regVal; int i2c_ch = 0; //LPC824 //I2C0 can support FM+ but only on P0_11 and P0_10 if (sda == I2C_SDA && scl == I2C_SCL) { //Select I2C mode for P0_11 and P0_10 LPC_SWM->PINENABLE0 &= ~(0x3 << 11); #if(LPC824_I2C0_FMPLUS == 1) // Enable FM+ mode on P0_11, P0_10 LPC_IOCON->PIO0_10 &= ~(0x3 << 8); LPC_IOCON->PIO0_10 |= (0x2 << 8); //FM+ mode LPC_IOCON->PIO0_11 &= ~(0x3 << 8); LPC_IOCON->PIO0_11 |= (0x2 << 8); //FM+ mode #endif } else { //Select any other pin for I2C1, I2C2 or I2C3 i2c_ch = get_available_i2c(); if (i2c_ch == -1) return; i2c_used |= (1 << (i2c_ch - 1)); swm = &SWM_I2C_SDA[i2c_ch - 1]; regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset); LPC_SWM->PINASSIGN[swm->n] = regVal | ((sda >> PIN_SHIFT) << swm->offset); swm = &SWM_I2C_SCL[i2c_ch - 1]; regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset); LPC_SWM->PINASSIGN[swm->n] = regVal | ((scl >> PIN_SHIFT) << swm->offset); } switch(i2c_ch) { case 0: obj->i2c = (LPC_I2C0_Type *)LPC_I2C0; break; case 1: obj->i2c = (LPC_I2C0_Type *)LPC_I2C1; break; case 2: obj->i2c = (LPC_I2C0_Type *)LPC_I2C2; break; case 3: obj->i2c = (LPC_I2C0_Type *)LPC_I2C3; break; default: break; } // enable power i2c_power_enable(i2c_ch); // set default frequency at 100k i2c_frequency(obj, 100000); i2c_interface_enable(obj); }
void i2c_init(i2c_t *obj, PinName sda, PinName scl) { const SWM_Map *swm; uint32_t regVal; int i2c_ch = 0; if (sda == I2C_SDA && scl == I2C_SCL) { LPC_SWM->PINENABLE0 &= ~(0x3 << 11); } else { i2c_ch = get_available_i2c(); if (i2c_ch == -1) return; i2c_used |= (1 << (i2c_ch - 1)); swm = &SWM_I2C_SDA[i2c_ch - 1]; regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset); LPC_SWM->PINASSIGN[swm->n] = regVal | ((sda >> PIN_SHIFT) << swm->offset); swm = &SWM_I2C_SCL[i2c_ch - 1]; regVal = LPC_SWM->PINASSIGN[swm->n] & ~(0xFF << swm->offset); LPC_SWM->PINASSIGN[swm->n] = regVal | ((scl >> PIN_SHIFT) << swm->offset); } switch(i2c_ch) { case 0: obj->i2c = (LPC_I2C0_Type *)LPC_I2C0; break; case 1: obj->i2c = (LPC_I2C0_Type *)LPC_I2C1; break; case 2: obj->i2c = (LPC_I2C0_Type *)LPC_I2C2; break; case 3: obj->i2c = (LPC_I2C0_Type *)LPC_I2C3; break; default: break; } // enable power i2c_power_enable(i2c_ch); uint32_t size_in_bytes = LPC_I2CD_API->i2c_get_mem_size(); i2c_buffer = malloc(size_in_bytes); obj->handler = LPC_I2CD_API->i2c_setup((uint32_t)(obj->i2c), i2c_buffer); LPC_I2CD_API->i2c_set_bitrate(obj->handler, SystemCoreClock, 100000); LPC_I2CD_API->i2c_set_timeout(obj->handler, 100000); i2c_interface_enable(obj); }