int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); SysCtlDelay(10000); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); GPIOPinConfigure(GPIO_PD0_I2C3SCL); GPIOPinConfigure(GPIO_PD1_I2C3SDA); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); I2CSlaveInit(I2C3_SLAVE_BASE, I2C_SLAVE_ADDRESS); I2CSlaveAddressSet(I2C3_SLAVE_BASE, I2C_SLAVE_ADDRESS, 0); I2CSlaveIntEnableEx(I2C3_SLAVE_BASE, I2C_SLAVE_INT_START|I2C_SLAVE_INT_STOP|I2C_SLAVE_INT_DATA); I2CSlaveEnable(I2C3_SLAVE_BASE); IntEnable(INT_I2C3); IntMasterEnable(); while(1) { SysCtlDelay(100000); } }
//Initialize as a slave void TwoWire::begin(uint8_t address) { if(i2cModule == NOT_ACTIVE) { i2cModule = BOOST_PACK_WIRE; } ROM_SysCtlPeripheralEnable(g_uli2cPeriph[i2cModule]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][0]); ROM_GPIOPinConfigure(g_uli2cConfig[i2cModule][1]); ROM_GPIOPinTypeI2C(g_uli2cBase[i2cModule], g_uli2cSDAPins[i2cModule]); ROM_GPIOPinTypeI2CSCL(g_uli2cBase[i2cModule], g_uli2cSCLPins[i2cModule]); slaveAddress = address; //Enable slave interrupts ROM_IntEnable(g_uli2cInt[i2cModule]); I2CSlaveIntEnableEx(SLAVE_BASE, I2C_SLAVE_INT_DATA | I2C_SLAVE_INT_STOP); HWREG(SLAVE_BASE + I2C_O_SICR) = I2C_SICR_DATAIC | I2C_SICR_STARTIC | I2C_SICR_STOPIC; //Setup as a slave device ROM_I2CMasterDisable(MASTER_BASE); I2CSlaveEnable(SLAVE_BASE); I2CSlaveInit(SLAVE_BASE, address); ROM_IntMasterEnable(); }
//***************************************************************************** // //! The main function of the i2c slave receive with poll mode // //! return none // //***************************************************************************** void SlaveReceivePoll(void) { // // Congigure the i2c pin // xSPinTypeI2C(I2C0SCK, PA11); xSPinTypeI2C(I2C0DATA, PA10); // // Enable the i2c peripheral // xSysCtlPeripheralEnable(xSYSCTL_PERIPH_I2C0); // // Init the I2C as slave // I2CSlaveInit(I2C0_BASE, SlaveAddress, I2C_GENERAL_CALL_DIS); // // Install the i2c0 interrupt service function // // I2CIntCallbackInit(I2C0_BASE, ( xtEventCallback)SlavaRecvFunc); WriteBuf[8] = '\0'; // // I2C master transfer config // I2CSlaveRcCfg.pvWBuf = WriteBuf; I2CSlaveRcCfg.ulWLen = WriteLength; I2CSlaveRcCfg.ulWCount = 0; I2CSlaveRcCfg.pvRBuf = ReceiveBuf; I2CSlaveRcCfg.ulRLen = ReceiveLength; //I2CSlaveRcCfg.pvRBuf = 0; //I2CSlaveRcCfg.ulRLen = 0; I2CSlaveRcCfg.ulRCount = 0; // // UART initialize // UART0Configuration(); xI2CIntCallbackInit(I2C0_BASE, SlavaRecvFunc); // // I2C salve receive wiht pollint mode // I2CSlaveTransfer(I2C0_BASE, &I2CSlaveRcCfg, I2C_TRANSFER_POLLING); // // Print the receive data form the master // while(1) { } }
/******************************************************************************* ** Main Function main() *******************************************************************************/ int main (void) { uint32_t i; SystemInit(); for ( i = 0; i < BUFSIZE; i++ ) { I2CRdBuffer[i] = 0x00; } I2CSlaveInit(); /* initialize I2c */ /* When the NACK occurs, the master has stopped the communication. Just check the content of I2CRd/WrBuffer. */ while ( I2CSlaveState != DATA_NACK ); return 0; }
int ipmi_i2c_ram_dev_init(ipmi_i2c_ram_dev *dev, ipmi_i2c_bus *bus, unsigned char addr, unsigned long reg_size, char *reg_buf) { ASSERT(dev); ASSERT(dev->bus); if ((addr == 0) || // 不能是广播地址 (addr & 0x80 == 0x80) || // 必须是7位地址,而不是8位地址 (addr & 0x78 == 0x78)) // 不能是保留地址 { return IPMI_I2C_ERR_ADDR; } INIT_LIST_HEAD(&dev->list); dev->bus = bus; dev->my_addr = addr; dev->ram_reg_size = reg_size; dev->ram_reg = reg_buf; dev->read_lock = OSSemCreate(0); dev->write_lock = OSSemCreate(0); if (dev->read_lock == (void*)0 || dev->write_lock == (void*)0) { return IPMI_I2C_ERR_INTER; } // 设置从设备地址 I2CSlaveInit(dev->bus->i2c_hw_slave_base, dev->my_addr); // 开中断 I2CSlaveIntEnable(dev->bus->i2c_hw_slave_base); IntEnable(dev->bus->i2c_int); IntMasterEnable(); // 从设备使能 I2CSlaveEnable(dev->bus->i2c_hw_slave_base); list_add(&dev->list, &ipmi_i2c_ram_dev_root.head); ipmi_i2c_ram_dev_root.count++; return 0; }
//***************************************************************************** // //! \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); }
//***************************************************************************** // //! The main function of the i2c slave receive with poll mode // //! return none // //***************************************************************************** void SlaveReceivePoll(void) { // //Set the external 12MHZ clock as system clock // SysCtlKeyAddrUnlock(); xHWREG(SYSCLK_PWRCON) |= SYSCLK_PWRCON_XTL12M_EN; // // Congigure the i2c pin // // XPinTypeI2C(I2C0SCL, PA9); // XPinTypeI2C(I2C0DATA, PA8); GPIOPinFunctionSet(GPIO_FUNCTION_I2C , GPIO_PORTA_BASE , GPIO_PIN_9); GPIOPinFunctionSet(GPIO_FUNCTION_I2C , GPIO_PORTA_BASE , GPIO_PIN_8); // // Enable the i2c peripheral // xSysCtlPeripheralEnable(xSYSCTL_PERIPH_I2C0); // // Init the I2C as slave // I2CSlaveInit(I2C0_BASE, SlaveAddress, I2C_GENERAL_CALL_DIS); // // Install the i2c0 interrupt service function // // I2CIntCallbackInit(I2C0_BASE, ( xtEventCallback)SlavaRecvFunc); WriteBuf[8] = '\0'; // // I2C master transfer config // I2CSlaveRcCfg.pvWBuf = WriteBuf; I2CSlaveRcCfg.ulWLen = WriteLength; I2CSlaveRcCfg.ulWCount = 0; I2CSlaveRcCfg.pvRBuf = ReceiveBuf; I2CSlaveRcCfg.ulRLen = ReceiveLength; //I2CSlaveRcCfg.pvRBuf = 0; //I2CSlaveRcCfg.ulRLen = 0; I2CSlaveRcCfg.ulRCount = 0; // // UART initialize // UART0Configuration(); xI2CIntCallbackInit(I2C0_BASE, SlavaRecvFunc); // // I2C salve receive wiht pollint mode // I2CSlaveTransfer(I2C0_BASE, &I2CSlaveRcCfg, I2C_TRANSFER_POLLING); // // Print the receive data form the master // while(1) { } }
//***************************************************************************** // Defines IPMI Test Task //***************************************************************************** void ipmi_cmd_test_task(void *args) { #if 0 // SPI0 master to FPGA test { uint8_t spibuf[3] = {0}; uint8_t i; while (1) { #if 1 for (i = 0; i < 15; i++) { spibuf[0] = 0x00; spibuf[1] = i; spibuf[2] = 0xa0 + i; SPI_spi0_xfer(spibuf, 3, 0, 0); DEBUG("write_spi data=0x%x\r\n", spibuf[2]); } OSTimeDlyHMSM(0, 0, 1, 0); #endif for (i = 0; i < 15; i++) { spibuf[0] = 0x80; spibuf[1] = i; spibuf[2] = 0; SPI_spi0_xfer(spibuf, 2, &spibuf[2], 1); DEBUG("read_spi data=0x%x\r\n", spibuf[2]); } OSTimeDlyHMSM(0, 0, 3, 0); } } #endif #if 0 { #define IPMB_SELF_ADDR (0x71) #define SLAVE_ADDR (0x73) // 定义从机地址 #if 0 SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // 使能I2C模块 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // 使能I2C管脚所在的GPIO模块 GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // 配置相关管脚为I2C收发功能 I2CSlaveInit(I2C0_SLAVE_BASE, SLAVE_ADDR); // I2C从机模块初始化 I2CSlaveIntEnable(I2C0_SLAVE_BASE); // 使能I2C从机模块中断 IntEnable(INT_I2C0); // 使能I2C中断 I2CSlaveEnable(I2C0_SLAVE_BASE); // 使能I2C从机 #endif { unsigned char ulDataTx[128] = {0}; unsigned char i; unsigned long error; struct ipmi_req *req; ulDataTx[0] = IPMI_FRAME_CHAR[0]; ulDataTx[1] = IPMI_FRAME_CHAR[1]; ulDataTx[2] = IPMI_FRAME_CHAR[2]; req = (struct ipmi_req *)&ulDataTx[3]; req->msg.data_len = 7; req->msg.rs_sa = SLAVE_ADDR; req->msg.netfn = 0x06; req->msg.rs_lun = 0x0; req->msg.checksum1 = 0xff; req->msg.rq_sa = IPMB_SELF_ADDR; req->msg.rq_lun = 0x0; req->msg.rq_seq = 0x01; req->msg.cmd = 0x01; while (1) { OSTimeDlyHMSM(0, 0, 5, 0); DEBUG("netfn=0x%x cmd=0x%x rs_sa=0x%x rq_sa=0x%x ", req->msg.netfn, req->msg.cmd, req->msg.rs_sa, req->msg.rq_sa); error = I2C_dev_write(I2C0_MASTER_BASE, SLAVE_ADDR, 0, 1, 20, &ulDataTx[0]); DEBUG("error=0x%x\r\n", error); //SysCtlDelay(10000000); } } } #endif #if 0 // test for eeprom uint8_t val[32] = { 0 }; uint8_t i; uint32_t error, addr; I2C_DEVICE at24xx_dev; I2C_i2c1_slave_dev_init(&at24xx_dev, 0x50, 2); #if 0 // set val to i for (i = 0; i < 32; i++) { val[i] = i; } // write to eeprom I2C_i2c1_slave_dev_set(&at24xx_dev, 0x1800, (uint8_t*)&val[0], 32); error = I2C_i2c1_master_write(&at24xx_dev); if (error) { DEBUG("write error=0x%x\r\n", error); } else { DEBUG("write to eeprom ok\r\n"); } OSTimeDlyHMSM(0, 0, 1, 0); #endif #if 0 // clear eeprom error = at24xx_clear(0x1800, 0x800); if (error) { DEBUG("clear error=0x%x\r\n", error); } else { DEBUG("clear OK\r\n"); } #endif // read eeprom for (addr = 0x1800; addr < 0x2000; addr += 32) { // clear val for (i = 0; i < 32; i++) { val[i] = 0; } // read from eeprom error = at24xx_read(addr, val, 32); //I2C_i2c1_slave_dev_set(&at24xx_dev, addr, (uint8_t*)&val[0], 32); //error = I2C_i2c1_master_read(&at24xx_dev); if (error) { DEBUG("read error=0x%x\r\n", error); } else { // show the read for (i = 0; i < 32; i++) { if (i % 8 == 0) { DEBUG("\r\n0x%04x ", addr+i); } DEBUG("0x%02x ", val[i]); } } } while (1) { OSTimeDlyHMSM(0, 0, 1, 0); } #endif #if 0 // test for i2c1/ipmb I2C_DEVICE ipmb_dev; uint32_t error; //uint8_t addr = 0; uint8_t buffer[4] = {0x00, 0x00, 0x00, 0x00}; I2C_i2c1_slave_dev_init(&ipmb_dev, 0x41, 1); #if 1 while (1) { // write to i2c0 //addr = (addr + 1) & 0xff; I2C_i2c1_slave_dev_set(&ipmb_dev, 0x00, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 cfg=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x01, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 shunt=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x02, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 bus=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } buffer[0] = 0x2e; buffer[1] = 0xcf; I2C_i2c1_slave_dev_set(&ipmb_dev, 0x05, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_write(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 cal=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x04, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 cur=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } I2C_i2c1_slave_dev_set(&ipmb_dev, 0x03, (uint8_t*)&buffer[0], 2); error = I2C_i2c1_master_read(&ipmb_dev); if (error) { DEBUG("i2c0 error=0x%x\r\n", error); } else { DEBUG("ina230 pow=0x%x\r\n", (buffer[0] << 8) | buffer[1]); } OSTimeDlyHMSM(0, 0, 3, 0); } #endif #if 0 while (1) { // read device id temp_val[0] = temp_val[1] = 0; I2C_i2c1_slave_dev_set(&adt7470_dev, 0x3d, (uint8_t*)&temp_val[0], 1); error = I2C_i2c1_master_read(&adt7470_dev); if (error) { DEBUG("error=0x%x\r\n", error); } else { DEBUG("device id=0x%x\t", temp_val[0]); } OSTimeDlyHMSM(0, 0, 1, 0); DEBUG("\r\n"); } #endif #endif #if 1 // test empty while (1) { OSTimeDlyHMSM(0, 0, 1, 0); } #endif }
//***************************************************************************** // // Configure the I2C0 master and slave and connect them using loopback mode. // //***************************************************************************** int main(void) { unsigned long ulDataTx[NUM_I2C_DATA]; unsigned long ulDataRx[NUM_I2C_DATA]; unsigned long ulindex; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // The I2C0 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // // For this example I2C0 is used with PortB[3:2]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port B needs to be enabled so these pins can // be used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the pin muxing for I2C0 functions on port B2 and B3. // This step is not necessary if your part does not support pin muxing. // TODO: change this to select the port/pin you are using. // GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // // Select the I2C function for these pins. This function will also // configure the GPIO pins pins for I2C operation, setting them to // open-drain operation with weak pull-ups. Consult the data sheet // to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Enable loopback mode. Loopback mode is a built in feature that is // useful for debugging I2C operations. It internally connects the I2C // master and slave terminals, which effectively let's you send data as // a master and receive data as a slave. // NOTE: For external I2C operation you will need to use external pullups // that are stronger than the internal pullups. Refer to the datasheet for // more information. // HWREG(I2C0_MASTER_BASE + I2C_O_MCR) |= 0x01; // // Enable and initialize the I2C0 master module. Use the system clock for // the I2C0 module. The last parameter sets the I2C data transfer rate. // If false the data rate is set to 100kbps and if true the data rate will // be set to 400kbps. For this example we will use a data rate of 100kbps. // I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false); // // Enable the I2C0 slave module. This module is enabled only for testing // purposes. It does not need to be enabled for proper operation of the // I2Cx master module. // I2CSlaveEnable(I2C0_SLAVE_BASE); // // Set the slave address to SLAVE_ADDRESS. In loopback mode, it's an // arbitrary 7-bit number (set in a macro above) that is sent to the // I2CMasterSlaveAddrSet function. // I2CSlaveInit(I2C0_SLAVE_BASE, SLAVE_ADDRESS); // // Tell the master module what address it will place on the bus when // communicating with the slave. Set the address to SLAVE_ADDRESS // (as set in the slave module). The receive parameter is set to false // which indicates the I2C Master is initiating a writes to the slave. If // true, that would indicate that the I2C Master is initiating reads from // the slave. // I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS, false); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for I2C operation. // InitConsole(); // // Display the example setup on the console. // UARTprintf("I2C Loopback Example ->"); UARTprintf("\n Module = I2C0"); UARTprintf("\n Mode = Single Send/Receive"); UARTprintf("\n Rate = 100kbps\n\n"); // // Initalize the data to send. // ulDataTx[0] = 'I'; ulDataTx[1] = '2'; ulDataTx[2] = 'C'; // // Initalize the receive buffer. // for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { ulDataRx[ulindex] = 0; } // // Indicate the direction of the data. // UARTprintf("Tranferring from: Master -> Slave\n"); // // Send 3 peices of I2C data from the master to the slave. // for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { // // Display the data that the I2C0 master is transferring. // UARTprintf(" Sending: '%c' . . . ", ulDataTx[ulindex]); // // Place the data to be sent in the data register // I2CMasterDataPut(I2C0_MASTER_BASE, ulDataTx[ulindex]); // // Initiate send of data from the master. Since the loopback // mode is enabled, the master and slave units are connected // allowing us to receive the same data that we sent out. // I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); // // Wait until the slave has received and acknowledged the data. // while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SCSR_RREQ)) { } // // Read the data from the slave. // ulDataRx[ulindex] = I2CSlaveDataGet(I2C0_SLAVE_BASE); // // Wait until master module is done transferring. // while(I2CMasterBusy(I2C0_MASTER_BASE)) { } // // Display the data that the slave has received. // UARTprintf("Received: '%c'\n", ulDataRx[ulindex]); } // // Reset receive buffer. // for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { ulDataRx[ulindex] = 0; } // // Indicate the direction of the data. // UARTprintf("\n\nTranferring from: Slave -> Master\n"); // // Modifiy the data direction to true, so that seeing the address will // indicate that the I2C Master is initiating a read from the slave. // I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS, true); // // Do a dummy receive to make sure you don't get junk on the first receive. // I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); // // Dummy acknowledge and wait for the receive request from the master. // This is done to clear any flags that should not be set. // while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ)) { } for(ulindex = 0; ulindex < NUM_I2C_DATA; ulindex++) { // // Display the data that I2C0 slave module is transferring. // UARTprintf(" Sending: '%c' . . . ", ulDataTx[ulindex]); // // Place the data to be sent in the data register // I2CSlaveDataPut(I2C0_SLAVE_BASE, ulDataTx[ulindex]); // // Tell the master to read data. // I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); // // Wait until the slave is done sending data. // while(!(I2CSlaveStatus(I2C0_SLAVE_BASE) & I2C_SLAVE_ACT_TREQ)) { } // // Read the data from the master. // ulDataRx[ulindex] = I2CMasterDataGet(I2C0_MASTER_BASE); // // Display the data that the slave has received. // UARTprintf("Received: '%c'\n", ulDataRx[ulindex]); } // // Tell the user that the test is done. // UARTprintf("\nDone.\n\n"); // // Return no errors // return(0); }
//***************************************************************************** // // Configure the I2C0 master and slave and connect them using loopback mode. // //***************************************************************************** int main(void) { uint32_t ui32DataTx; // // Set the clocking to run directly from the external crystal/oscillator. // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the // crystal on your board. // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // The I2C0 peripheral must be enabled before use. // SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); // // For this example I2C0 is used with PortB[3:2]. The actual port and // pins used may be different on your part, consult the data sheet for // more information. GPIO port B needs to be enabled so these pins can // be used. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // // Configure the pin muxing for I2C0 functions on port B2 and B3. // This step is not necessary if your part does not support pin muxing. // TODO: change this to select the port/pin you are using. // GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // // Select the I2C function for these pins. This function will also // configure the GPIO pins pins for I2C operation, setting them to // open-drain operation with weak pull-ups. Consult the data sheet // to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using. // GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3); // // Enable loopback mode. Loopback mode is a built in feature that helps // for debug the I2Cx module. It internally connects the I2C master and // slave terminals, which effectively lets you send data as a master and // receive data as a slave. NOTE: For external I2C operation you will need // to use external pull-ups that are faster than the internal pull-ups. // Refer to the datasheet for more information. // HWREG(I2C0_BASE + I2C_O_MCR) |= 0x01; // // Enable the I2C0 interrupt on the processor (NVIC). // IntEnable(INT_I2C0); // // Configure and turn on the I2C0 slave interrupt. The I2CSlaveIntEnableEx() // gives you the ability to only enable specific interrupts. For this case // we are only interrupting when the slave device receives data. // I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA); // // Enable and initialize the I2C0 master module. Use the system clock for // the I2C0 module. The last parameter sets the I2C data transfer rate. // If false the data rate is set to 100kbps and if true the data rate will // be set to 400kbps. For this example we will use a data rate of 100kbps. // I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false); // // Enable the I2C0 slave module. // I2CSlaveEnable(I2C0_BASE); // // Set the slave address to SLAVE_ADDRESS. In loopback mode, it's an // arbitrary 7-bit number (set in a macro above) that is sent to the // I2CMasterSlaveAddrSet function. // I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS); // // Tell the master module what address it will place on the bus when // communicating with the slave. Set the address to SLAVE_ADDRESS // (as set in the slave module). The receive parameter is set to false // which indicates the I2C Master is initiating a writes to the slave. If // true, that would indicate that the I2C Master is initiating reads from // the slave. // I2CMasterSlaveAddrSet(I2C0_BASE, SLAVE_ADDRESS, false); // // Set up the serial console to use for displaying messages. This is just // for this example program and is not needed for proper I2C operation. // InitConsole(); // // Enable interrupts to the processor. // IntMasterEnable(); // // Display the example setup on the console. // UARTprintf("I2C Slave Interrupt Example ->"); UARTprintf("\n Module = I2C0"); UARTprintf("\n Mode = Receive interrupt on the Slave module"); UARTprintf("\n Rate = 100kbps\n\n"); // // Initialize the data to send. // ui32DataTx = 'I'; // // Indicate the direction of the data. // UARTprintf("Transferring from: Master -> Slave\n"); // // Display the data that I2C0 is transferring. // UARTprintf(" Sending: '%c'", ui32DataTx); // // Place the data to be sent in the data register. // I2CMasterDataPut(I2C0_BASE, ui32DataTx); // // Initiate send of single piece of data from the master. Since the // loopback mode is enabled, the Master and Slave units are connected // allowing us to receive the same data that we sent out. // I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); // // Wait for interrupt to occur. // while(!g_bIntFlag) { } // // Display that interrupt was received. // UARTprintf("\n Slave Interrupt Received!\n"); // // Display the data that the slave has received. // UARTprintf(" Received: '%c'\n\n", g_ui32DataRx); // // Loop forever. // while(1) { } }