//***************************************************************************** // //! \brief xi2c 002 test execute main body. //! //! \return None. // //***************************************************************************** static void xI2C002Execute(void) { I2CMasterInit(ulMaster, 400000); I2CEnable(ulMaster); I2CMasterWriteBufS1(ulMaster, 0x12, ucTempData, 4, xfalse); I2CMasterReadS1(ulMaster, 0x12, ucTemp, xfalse); I2CMasterReadS2(ulMaster, &ucTemp[1], xfalse); I2CMasterReadS2(ulMaster, &ucTemp[2], xfalse); I2CMasterReadS2(ulMaster, &ucTemp[3], xfalse); I2CMasterReadBufS1(ulMaster, 0x12, ucTemp, 5, xtrue); TestAssert(ucTemp[0] == 'a', "xi2c, \"I2C send or receive\" error!"); TestAssert(ucTemp[1] == 'b', "xi2c, \"I2C send or receive\" error!"); TestAssert(ucTemp[2] == 'c', "xi2c, \"I2C send or receive\" error!"); TestAssert(ucTemp[3] == 'd', "xi2c, \"I2C send or receive\" error!"); }
//***************************************************************************** // //! Initialization the I2C bus module. //! //! \param bus specifies the i2c bus data structure. //! //! This function initialization the I2C hardware bus, it will open the i2c peripheral //! and config the I2C gpio //! //! \return 0 is OK. // //***************************************************************************** int ipmi_i2c_bus_init(ipmi_i2c_bus *bus) { // // Check the arguments. // ASSERT(bus->sys_peripheral); ASSERT(bus->i2c_scl_periph); ASSERT(bus->i2c_sda_periph); ASSERT(bus->i2c_scl_gpio_port); ASSERT(bus->i2c_sda_gpio_port); ASSERT(bus->i2c_scl_gpio_pin); ASSERT(bus->i2c_sda_gpio_pin); ASSERT(bus->i2c_hw_master_base); // 初始化链表结构 INIT_LIST_HEAD(&bus->list); // 初始化系统硬件外设 SysCtlPeripheralEnable(bus->sys_peripheral); // 初始化SCL管脚硬件外设 SysCtlPeripheralEnable(bus->i2c_scl_periph); if (bus->i2c_scl_gpio_mux) GPIOPinConfigure(bus->i2c_scl_gpio_mux); GPIOPinTypeI2C(bus->i2c_scl_gpio_port, bus->i2c_scl_gpio_pin); // 初始化SDA管脚硬件外设 SysCtlPeripheralEnable(bus->i2c_sda_periph); if (bus->i2c_sda_gpio_mux) GPIOPinConfigure(bus->i2c_sda_gpio_mux); GPIOPinTypeI2C(bus->i2c_sda_gpio_port, bus->i2c_sda_gpio_pin); // 设备使能,开中断 I2CMasterInit(bus->i2c_hw_master_base, false); if (bus->i2c_int) { //I2CIntRegister(bus->i2c_hw_master_base); IntEnable(bus->i2c_int); I2CMasterIntEnable(bus->i2c_hw_master_base); } I2CMasterEnable(bus->i2c_hw_master_base); list_add(&bus->list, &ipmi_i2c_bus_root.head); ipmi_i2c_bus_root.count++; return 0; }
static void prvSetupHardware( void ) { /* Setup the PLL. */ SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ ); /* Enable the I2C used to read the pot. */ SysCtlPeripheralEnable( SYSCTL_PERIPH_I2C ); SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB ); GPIOPinTypeI2C( GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3 ); /* Initialize the I2C master. */ I2CMasterInit( I2C_MASTER_BASE, pdFALSE ); /* Enable the I2C master interrupt. */ I2CMasterIntEnable( I2C_MASTER_BASE ); IntEnable( INT_I2C ); /* Initialise the hardware used to talk to the LED's. */ vParTestInitialise(); }
//***************************************************************************** // //! \brief something should do before the test execute of xsysctl002 test. //! //! \return None. // //***************************************************************************** static void xI2C001Setup(void) { xSysCtlPeripheralEnable2(ulSlave); xSysCtlPeripheralReset2(ulSlave); xSysCtlPeripheralEnable2(ulMaster); xSysCtlPeripheralReset2(ulMaster); xSysCtlPeripheralEnable2(GPIOB_BASE); xSysCtlPeripheralEnable(SYSCTL_PERIPH_AFIO); xSPinTypeI2C(I2C1SCK, PB6); xSPinTypeI2C(I2C1SDA, PB7); //xSPinTypeI2C(I2C1SCK, PB8); //xSPinTypeI2C(I2C1SDA, PB9); xSPinTypeI2C(I2C2SCK, PB10); xSPinTypeI2C(I2C2SDA, PB11); I2CSlaveInit(ulSlave, I2C_ADDR_7BIT, 0x12, I2C_GENERAL_CALL_DIS); I2CEnable(ulSlave); I2CIntEnable(ulSlave, I2C_INT_BUF | I2C_INT_EVT | I2C_INT_ERR); xIntEnable(xSysCtlPeripheraIntNumGet(ulSlave)); I2CIntCallbackInit(ulSlave, I2CSlaveCallback); I2CMasterInit(ulMaster, SysCtlAPB1ClockGet(), xfalse, xtrue); I2CEnable(ulMaster); I2CMasterWriteS1(ulMaster, 0x12, 'a', xfalse); I2CMasterWriteS2(ulMaster, 'b', xfalse); I2CMasterWriteS2(ulMaster, 'c', xfalse); I2CMasterWriteS2(ulMaster, 'd', xfalse); I2CMasterReadS1(ulMaster, 0x12, ucTemp, xfalse); I2CMasterReadS2(ulMaster, &ucTemp[1], xfalse); I2CMasterReadS2(ulMaster, &ucTemp[2], xfalse); I2CMasterReadS2(ulMaster, &ucTemp[3], xfalse); I2CMasterReadS2(ulMaster, &ucTemp[4], xtrue); }
//***************************************************************************** // //! Initialize the OLED display. //! //! \param bFast is a boolean that is \e true if the I2C interface should be //! run at 400 kbps and \e false if it should be run at 100 kbps. //! //! This function initializes the I2C interface to the OLED display and //! configures the SSD0303 controller on the panel. //! //! This function is contained in <tt>osram96x16.c</tt>, with //! <tt>osram96x16.h</tt> containing the API definition for use by //! applications. //! //! \return None. // //***************************************************************************** void OSRAMInit(tBoolean bFast) { unsigned long ulIdx; // // Enable the I2C and GPIO port B blocks as they are needed by this driver. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the I2C SCL and SDA pins for I2C operation. // GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Initialize the I2C master. // I2CMasterInit(I2C_MASTER_BASE, bFast); // // Compute the inter-byte delay for the SSD0303 controller. This delay is // dependent upon the I2C bus clock rate; the slower the clock the longer // the delay required. // // The derivation of this formula is based on a measured delay of // OSRAMDelay(1700) for a 100 kHz I2C bus with the CPU running at 50 MHz // (referred to as C). To scale this to the delay for a different CPU // speed (since this is just a CPU-based delay loop) is: // // f(CPU) // C * ---------- // 50,000,000 // // To then scale this to the actual I2C rate (since it won't always be // precisely 100 kHz): // // f(CPU) 100,000 // C * ---------- * ------- // 50,000,000 f(I2C) // // This equation will give the inter-byte delay required for any // configuration of the I2C master. But, as arranged it is impossible to // directly compute in 32-bit arithmetic (without loosing a lot of // accuracy). So, the equation is simplified. // // Since f(I2C) is generated by dividing down from f(CPU), replace it with // the equivalent (where TPR is the value programmed into the Master Timer // Period Register of the I2C master, with the 1 added back): // // 100,000 // f(CPU) ------- // C * ---------- * f(CPU) // 50,000,000 ------------ // 2 * 10 * TPR // // Inverting the dividend in the last term: // // f(CPU) 100,000 * 2 * 10 * TPR // C * ---------- * ---------------------- // 50,000,000 f(CPU) // // The f(CPU) now cancels out. // // 100,000 * 2 * 10 * TPR // C * ---------------------- // 50,000,000 // // Since there are no clock frequencies left in the equation, this equation // also works for 400 kHz bus operation as well, since the 100,000 in the // numerator becomes 400,000 but C is 1/4, which cancel out each other. // Reducing the constants gives: // // TPR TPR TPR // C * --- = 1700 * --- = 340 * --- = 68 * TPR // 25 25 5 // // Note that the constant C is actually a bit larger than it needs to be in // order to provide some safety margin. // // When the panel is being initialized, the value of C actually needs to be // a bit longer (3400 instead of 1700). So, set the larger value for now. // g_ulDelay = 136 * (HWREG(I2C_MASTER_BASE + I2C_MASTER_O_TPR) + 1); // // Initialize the SSD0303 controller. Loop through the initialization // sequence doing a single I2C transfer for each command. // for(ulIdx = 0; ulIdx < sizeof(g_pucOSRAMInit); ulIdx += g_pucOSRAMInit[ulIdx] + 1) { // // Send this command. // OSRAMWriteFirst(g_pucOSRAMInit[ulIdx + 1]); OSRAMWriteArray(g_pucOSRAMInit + ulIdx + 2, g_pucOSRAMInit[ulIdx] - 2); OSRAMWriteFinal(g_pucOSRAMInit[ulIdx + g_pucOSRAMInit[ulIdx]]); } // // Now, switch to the actual value of C. // g_ulDelay = 68 * (HWREG(I2C_MASTER_BASE + I2C_MASTER_O_TPR) + 1); // // Clear the frame buffer. // OSRAMClear(); }