// Initialize the I2C bus void I2C_Init(I2C_ID_T id, int speed) { Board_I2C_Init(id); // Initialize board I2C Chip_I2C_Init(id); // Initialize I2C1 Chip_I2C_SetClockRate(id, speed); // Set clock speed I2C_Set_Mode(id, 0); // Set default mode interrupt }
/* Setup I2C handle and parameters */ static void setupI2CMaster() { /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(LPC_I2C); /* Perform a sanity check on the storage allocation */ if (LPC_I2CD_API->i2c_get_mem_size() > sizeof(i2cMasterHandleMEM)) { /* Example only: this should never happen and probably isn't needed for most I2C code. */ errorI2C(); } /* Setup the I2C handle */ i2cHandleMaster = LPC_I2CD_API->i2c_setup(LPC_I2C_BASE, i2cMasterHandleMEM); if (i2cHandleMaster == NULL) { errorI2C(); } /* Set I2C bitrate */ if (LPC_I2CD_API->i2c_set_bitrate(i2cHandleMaster, Chip_Clock_GetSystemClockRate(), I2C_BITRATE) != LPC_OK) { errorI2C(); } }
/* Setup I2C handle and parameters */ static void setupI2CSlave() { ErrorCode_t error_code; /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(); /* Perform a sanity check on the storage allocation */ if (LPC_I2CD_API->i2c_get_mem_size() > sizeof(i2cSlaveHandleMEM)) { /* Example only: this should never happen and probably isn't needed for most I2C code. */ errorI2C(); } /* Setup the I2C handle */ i2cHandleSlave = LPC_I2CD_API->i2c_setup(LPC_I2C_BASE, i2cSlaveHandleMEM); if (i2cHandleSlave == NULL) { errorI2C(); } /* Set a single 7-bit I2C address, only 7-bit addressing is supported */ error_code = LPC_I2CD_API->i2c_set_slave_addr(i2cHandleSlave, I2C_ADDR_7BIT, 0xFF); if (error_code != LPC_OK) { DEBUGOUT("Error setting I2C slave address\r\n"); errorI2C(); } /* No need to set I2C clock rate in slave mode */ }
void IrTherm_Init(void){ /* Enable clocks to SWM and IOCON to save power */ Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM); Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON); Chip_SWM_MovablePinAssign(SWM_I2C_SDA_IO, 11); Chip_SWM_MovablePinAssign(SWM_I2C_SCL_IO, 10); #if (I2C_BITRATE > 400000) /* Enable Fast Mode Plus for I2C pins */ Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO10, PIN_I2CMODE_FASTPLUS); Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO11, PIN_I2CMODE_FASTPLUS); #else Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO10, PIN_I2CMODE_STDFAST); Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO11, PIN_I2CMODE_STDFAST); #endif /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(); /* Setup the I2C handle */ i2cHandleMaster = LPC_I2CD_API->i2c_setup(LPC_I2C_BASE, i2cMasterHandleMEM); /* Set I2C bitrate */ LPC_I2CD_API->i2c_set_bitrate(i2cHandleMaster, Chip_Clock_GetSystemClockRate(), I2C_BITRATE); /* Disable the interrupt for the I2C */ NVIC_DisableIRQ(I2C_IRQn); /* Disable clocks to SWM and IOCON to save power */ Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM); Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_IOCON); }
static void i2c_onboard_init(void) { Chip_I2C_Init(ONBOARD_I2C); Chip_I2C_SetClockRate(ONBOARD_I2C, 100000); // mode_poll &= ~(1 << id); Chip_I2C_SetMasterEventHandler(ONBOARD_I2C, Chip_I2C_EventHandler); NVIC_EnableIRQ(I2C0_IRQn); }
/* Initialize the I2C bus */ static void i2c_app_init(I2C_ID_T id, int speed) { Board_I2C_Init(id); /* Initialize I2C */ Chip_I2C_Init(id); Chip_I2C_SetClockRate(id, speed); }
void vI2CInit( I2C_ID_T i2c_id, I2C_Mode mode ) { char pcI2C_Tag[4]; uint8_t sla_addr; sprintf( pcI2C_Tag, "I2C%u", i2c_id ); /*! @todo Maybe wrap these functions, or use some board-specific defines * so this code is generic enough to be applied on other hardware. * Example: (if using LPC17xx and LPCOpen library) * @code * #define PIN_FUNC_CFG( port, pin, func ) Chip_IOCON_PinMux(...) * @endcode */ Chip_IOCON_PinMux( LPC_IOCON, i2c_cfg[i2c_id].pins.sda_port, i2c_cfg[i2c_id].pins.sda_pin, IOCON_MODE_INACT, i2c_cfg[i2c_id].pins.pin_func ); Chip_IOCON_PinMux( LPC_IOCON, i2c_cfg[i2c_id].pins.scl_port, i2c_cfg[i2c_id].pins.scl_pin, IOCON_MODE_INACT, i2c_cfg[i2c_id].pins.pin_func ); Chip_IOCON_EnableOD( LPC_IOCON, i2c_cfg[i2c_id].pins.sda_port, i2c_cfg[i2c_id].pins.sda_pin ); Chip_IOCON_EnableOD( LPC_IOCON, i2c_cfg[i2c_id].pins.scl_port, i2c_cfg[i2c_id].pins.scl_pin ); NVIC_SetPriority(i2c_cfg[i2c_id].irq, configMAX_SYSCALL_INTERRUPT_PRIORITY); NVIC_EnableIRQ( i2c_cfg[i2c_id].irq ); /* Create mutex for accessing the shared memory (i2c_cfg) */ I2C_mutex[i2c_id] = xSemaphoreCreateMutex(); /* Make sure that the mutex is freed */ xSemaphoreGive( I2C_mutex[i2c_id] ); /* Set I2C operating mode */ if( xSemaphoreTake( I2C_mutex[i2c_id], 0 ) ) { i2c_cfg[i2c_id].mode = mode; xSemaphoreGive( I2C_mutex[i2c_id] ); } /* Enable and configure I2C clock */ Chip_I2C_Init( i2c_id ); Chip_I2C_SetClockRate( i2c_id, 100000 ); /* Enable I2C interface (Master Mode only) */ I2CCONSET( i2c_id, I2C_I2EN ); if ( mode == I2C_Mode_IPMB ) { /* Configure Slave Address */ sla_addr = get_ipmb_addr( ); I2CADDR_WRITE( i2c_id, sla_addr ); /* Configure Slave Address Mask */ I2CMASK( i2c_id, 0xFE); /* Enable slave mode */ I2CCONSET( i2c_id, I2C_AA ); } /* Clear I2C0 interrupt (just in case) */ I2CCONCLR( i2c_id, I2C_SI ); } /* End of vI2C_Init */
void Board_I2C_Init(void) { Chip_SYSCTL_PeriphReset(RESET_I2C0); Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_4, (IOCON_FUNC1)); Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_5, (IOCON_FUNC1)); Chip_SYSCTL_DeassertPeriphReset(RESET_I2C0); Chip_I2C_Init(DEFAULT_I2C); Chip_I2C_SetClockRate(DEFAULT_I2C, SPEED_100KHZ); Chip_I2C_SetMasterEventHandler(DEFAULT_I2C, Chip_I2C_EventHandler); NVIC_EnableIRQ(I2C0_IRQn); }
/* void i2C0_IRQHandler (void) { if (Chip_I2C_IsMasterActive(I2C0)) { Chip_I2C_MasterStateHandler(I2C0); } else { Chip_I2C_SlaveStateHandler(I2C0); } } */ void Board_I2C_Master_Init(void) { Board_I2C_Init(I2C0); /* Initialize I2C */ Chip_I2C_Init(I2C0); Chip_I2C_SetClockRate(I2C0, 100000); /* Set default mode to poll */ i2c_set_mode(I2C0, 1); }
/**************************************************************************************************** * @fn i2c_init * Configures the GPIOs, enables clock and also resets the I2C slave * * @param obj - reference to the i2c slave data structure * * @return none * ***************************************************************************************************/ void i2c_init(i2c_t *obj) { /* Enable I2C clock and reset I2C peripheral */ Chip_I2C_Init(I2C_HOSTIF); /* Setup I2C pin mux */ Chip_IOCON_PinMuxSet(LPC_IOCON, HOSTIF_SCL_PIN); Chip_IOCON_PinMuxSet(LPC_IOCON, HOSTIF_SDA_PIN); pi2c_slave_obj = obj; }
/* Initialize the I2C bus */ static void i2c_app_init(I2C_ID_T id, int speed) { Init_I2C_PinMux(); /* Initialize I2C */ Chip_I2C_Init(id); Chip_I2C_SetClockRate(id, speed); /* Set default mode to interrupt */ i2c_set_mode(id, 0); }
/* Setup I2C */ static void setupI2CMaster(void) { /* Enable I2C clock and reset I2C peripheral */ Chip_I2C_Init(LPC_I2C_PORT); /* Setup clock rate for I2C */ Chip_I2C_SetClockDiv(LPC_I2C_PORT, I2C_CLK_DIVIDER); /* Setup I2CM transfer rate */ Chip_I2CM_SetBusSpeed(LPC_I2C_PORT, I2C_BITRATE); // /* Enable I2C master interface */ //Chip_I2CM_Enable(LPC_I2C_PORT); }
/* WM8904 initialize function */ int WM8904_Init(int input) { I2C_EVENTHANDLER_T old = Chip_I2C_GetMasterEventHandler(WM8904_I2C_BUS); int ret; /* Initialize I2C */ Board_I2C_Init(WM8904_I2C_BUS); Chip_I2C_Init(WM8904_I2C_BUS); Chip_I2C_SetClockRate(WM8904_I2C_BUS, 100000); Chip_I2C_SetMasterEventHandler(WM8904_I2C_BUS, Chip_I2C_EventHandlerPolling); /* Initialize the default values */ ret = Audio_Codec_SetDefaultValues((void *)&g_wm8904[0], sizeof(g_wm8904)/sizeof(WM8904_Init_Seq_t)); #if 0 if (ret) { ret = Audio_Codec_SetDefaultValues(UDA_interfil_regs_dat, sizeof(UDA_interfil_regs_dat)); } if (ret) { ret = Audio_Codec_SetDefaultValues(UDA_decimator_regs_dat, sizeof(UDA_decimator_regs_dat)); } if (ret && input) { /* Disable Power On for ADCR, PGAR, PGAL to get mic sound more clearly */ ret = UDA1380_REG_WriteVerify(UDA_POWER_CTRL, UDA1380_REG_PWRCTRL_DEFAULT_VALUE & (~(0x0B))); if (ret) { ret = UDA1380_REG_WriteVerify(UDA_ADC_CTRL, UDA1380_REG_ADC_DEFAULT_VALUE | input); } } #endif Chip_I2C_SetMasterEventHandler(WM8904_I2C_BUS, old); return ret; }
/**************************************************************************************************** * @fn I2C_HardwareSetup * Configures the GPIOs and h/w interface for the I2C bus * * @param busId - I2C bus identifier in case multiple buses are supported * * @return true if successful, false otherwise. * ***************************************************************************************************/ osp_bool_t I2C_HardwareSetup( I2C_TypeDef *busId ) { if (busId == LPC_I2C0) { if (sI2C_Bus1Initialized) { return true; } /* Configure the I2C interface in Master mode with the given speed */ Chip_IOCON_PinMuxSet(LPC_IOCON, I2C_SENSOR_BUS_SCL_PIN); Chip_IOCON_PinMuxSet(LPC_IOCON, I2C_SENSOR_BUS_SDA_PIN); Chip_I2C_Init(busId); /* Enables clock and resets the peripheral */ /* setup speed and config. as Master */ Chip_I2C_SetClockDiv( busId, I2C_MASTER_CLOCK_DIV ); Chip_I2CM_SetBusSpeed( busId, I2C_MCLOCK_SPEED ); /* Reset master state machine */ Chip_I2CM_Disable( busId ); Chip_I2CM_Enable( busId ); /* Enable interrupt for pending status */ Chip_I2C_EnableInt( busId, I2C_INTENSET_MSTPENDING ); I2C_Master_Initialise(); /* Configure TWI interrupts */ NVIC_SetPriority( I2C_SENSOR_BUS_IRQn, I2C_SENSOR_BUS_INT_PRIORITY ); NVIC_EnableIRQ( I2C_SENSOR_BUS_IRQn ); //enable I2C isr sI2C_Bus1Initialized = true; return true; } return false; }
TempSensDig::TempSensDig(int deviceNumber, uint32_t speed) { if(deviceNumber == 0) { device = LPC_I2C0; Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, IOCON_DIGMODE_EN | I2C_MODE); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 23, IOCON_DIGMODE_EN | I2C_MODE); Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SCL); Chip_SWM_EnableFixedPin(SWM_FIXED_I2C0_SDA); } else { // currently we support only I2C number 0 } /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(device); /* Setup clock rate for I2C */ Chip_I2C_SetClockDiv(device, I2C_CLK_DIVIDER); /* Setup I2CM transfer rate */ Chip_I2CM_SetBusSpeed(device, speed); /* Enable Master Mode */ Chip_I2CM_Enable(device); }