void i2c_rx_multi(unsigned char SlaveAddr, unsigned char dest, unsigned char num_bytes, unsigned long *data) { unsigned int i=0; // Set the address I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_SEND ); I2CMasterDataPut( I2C1_MASTER_BASE, dest ); I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND ); while (I2CMasterBusy( I2C1_MASTER_BASE )); // Set the address again to tell the device to start sending data I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_RECEIVE ); I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START ); while(I2CMasterBusy( I2C1_MASTER_BASE )); *data++ = I2CMasterDataGet(I2C1_MASTER_BASE); while(i++ < (num_bytes-2)) { I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT ); while(I2CMasterBusy( I2C1_MASTER_BASE )); *data++ = I2CMasterDataGet(I2C1_MASTER_BASE); } I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH ); while(I2CMasterBusy( I2C1_MASTER_BASE )); *data++ = I2CMasterDataGet(I2C1_MASTER_BASE); }
uint32_t I2C_Read0(uint16_t device_address, uint16_t device_register) { //specify that we want to communicate to device address with an intended write to bus I2CMasterSlaveAddrSet(I2C0_BASE, device_address, false); //the register to be read I2CMasterDataPut(I2C0_BASE, device_register); //send control byte and register address byte to slave device I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND); //wait for MCU to complete send transaction while(I2CMasterBusy(I2C0_BASE)); //read from the specified slave device I2CMasterSlaveAddrSet(I2C0_BASE, device_address, true); //send control byte and read from the register from the MCU I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); //wait while checking for MCU to complete the transaction while(I2CMasterBusy(I2C0_BASE)); //Get the data from the MCU register and return to caller return( (uint32_t)I2CMasterDataGet(I2C0_BASE)); }
uint32_t i2c_read_bytes(uint8_t address, uint8_t* buffer, uint32_t length) { uint32_t future = I2C_MAX_DELAY_US; // Receive operation I2CMasterSlaveAddrSet(address, true); // Multiple receive operation I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_START); // Calculate timeout future += board_timer_get(); // Iterate overall all bytes while (length) { // Wait until complete or timeout while (I2CMasterBusy()) { // Update timeout status and return if expired if (board_timer_expired(future)) return length; } // Read data from I2C *buffer++ = I2CMasterDataGet(); length--; // Check if it's the last byte if (length == 1) I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_FINISH); else I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_CONT); } // Return bytes read return length; }
unsigned long I2CController::read(uint8_t addr, void *ptr, int count, bool sendStartCondition, bool sendStopCondition) { uint8_t *buf = (uint8_t *) ptr; RecursiveMutexGuard guard(&_lock); unsigned long ret; if (count<1) return 0; ret = read8(addr, buf, sendStartCondition, (sendStopCondition && count==1)); if (ret != I2C_MASTER_ERR_NONE) { return ret; } for (uint8_t i=1; i<count; i++) { I2CMasterControl(_base, ((i==(count-1)) && sendStopCondition) ? I2C_MASTER_CMD_BURST_RECEIVE_FINISH : I2C_MASTER_CMD_BURST_RECEIVE_CONT); ret = I2CMasterErr(_base); if (ret != I2C_MASTER_ERR_NONE) { return ret; } ret = waitFinish(_defaultTimeout); if (ret != I2C_MASTER_ERR_NONE) { return ret; } buf[i] = I2CMasterDataGet(_base); ret = I2CMasterErr(_base); if (ret != I2C_MASTER_ERR_NONE) { return ret; } } return 0; }
unsigned long I2CController::nolock_read(uint8_t addr, uint8_t *buf, int count, bool sendStartCondition, bool sendStopCondition) { unsigned long ret; if (count<1) return 0; ret = nolock_read8(addr, buf, sendStartCondition, (sendStopCondition && count==1)); if (ret != I2C_MASTER_ERR_NONE) { return ret; } for (uint8_t i=1; i<count; i++) { I2CMasterControl(_base, ((i==(count-1)) && sendStopCondition) ? I2C_MASTER_CMD_BURST_RECEIVE_FINISH : I2C_MASTER_CMD_BURST_RECEIVE_CONT); ret = I2CMasterErr(_base); if (ret != I2C_MASTER_ERR_NONE) { return ret; } ret = waitFinish(); if (ret != I2C_MASTER_ERR_NONE) { return ret; } buf[i] = I2CMasterDataGet(_base); ret = I2CMasterErr(_base); if (ret != I2C_MASTER_ERR_NONE) { return ret; } } return 0; }
uint32_t I2CReadSingle(uint32_t i2c_base, uint8_t address, uint8_t reg) { uint32_t data = 0; // Set slave register to be read while(I2CMasterBusy(i2c_base)); I2CMasterSlaveAddrSet(i2c_base, address, 0); I2CMasterDataPut(i2c_base, reg); I2CMasterControl(i2c_base, I2C_MASTER_CMD_SINGLE_SEND); while(I2CMasterBusy(i2c_base)); // Check for errors if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0; // Request for register data I2CMasterSlaveAddrSet(i2c_base, address, 1); I2CMasterControl(i2c_base, I2C_MASTER_CMD_SINGLE_RECEIVE); while(I2CMasterBusy(i2c_base)); // Check for errors if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0; // Read received data data = I2CMasterDataGet(i2c_base); return data; }
unsigned long I2CController::nolock_read8(uint8_t addr, uint8_t *data, bool sendStartCondition, bool sendStopCondition) { unsigned long ret; I2CMasterSlaveAddrSet(_base, addr, 1); if (sendStartCondition) { I2CMasterControl(_base, sendStopCondition ? I2C_MASTER_CMD_SINGLE_SEND : I2C_MASTER_CMD_BURST_RECEIVE_START); ret = I2CMasterErr(_base); if (ret != I2C_MASTER_ERR_NONE) { return ret; } } else { I2CMasterControl(_base, sendStopCondition ? I2C_MASTER_CMD_BURST_RECEIVE_FINISH : I2C_MASTER_CMD_BURST_RECEIVE_CONT); ret = I2CMasterErr(_base); if (ret != I2C_MASTER_ERR_NONE) { return ret; } } ret = waitFinish(); if (ret != I2C_MASTER_ERR_NONE) { return ret; } *data = I2CMasterDataGet(_base); ret = I2CMasterErr(_base); if (ret != I2C_MASTER_ERR_NONE) { return ret; } return 0; }
/* ** ISR to handler i2c interrupts */ void I2CCodecIsr(void) { unsigned int status; status = I2CMasterIntStatus(SOC_I2C_1_REGS); I2CMasterIntClearEx(SOC_I2C_1_REGS, (status & ~(I2C_INT_RECV_READY | I2C_INT_TRANSMIT_READY))); if (status & I2C_INT_TRANSMIT_READY) { /* Put data to data transmit register of i2c */ I2CMasterDataPut(SOC_I2C_1_REGS, slaveData[dataIdx]); I2CMasterIntClearEx(SOC_I2C_1_REGS, I2C_INT_TRANSMIT_READY); dataIdx++; if(dataMax == dataIdx) { I2CMasterIntDisableEx(SOC_I2C_1_REGS, I2C_INT_TRANSMIT_READY); } } if(status & I2C_INT_RECV_READY) { /* Receive data from data receive register */ slaveData[dataIdx] = I2CMasterDataGet(SOC_I2C_1_REGS); I2CMasterIntClearEx(SOC_I2C_1_REGS, I2C_INT_RECV_READY); dataIdx++; if(dataMax == dataIdx) { I2CMasterIntDisableEx(SOC_I2C_1_REGS, I2C_INT_RECV_READY); /* Generate a STOP */ I2CMasterStop(SOC_I2C_1_REGS); } } if (status & I2C_INT_STOP_CONDITION) { /* Disable transmit data ready and receive data read interupt */ I2CMasterIntDisableEx(SOC_I2C_1_REGS, I2C_INT_TRANSMIT_READY | I2C_INT_STOP_CONDITION | I2C_INT_RECV_READY); txCompFlag = 0; } if(status & I2C_INT_NO_ACK) { I2CMasterIntDisableEx(SOC_I2C_1_REGS, I2C_INT_TRANSMIT_READY | I2C_INT_RECV_READY | I2C_INT_NO_ACK | I2C_INT_STOP_CONDITION); /* Generate a STOP */ I2CMasterStop(SOC_I2C_1_REGS); txCompFlag = 0; } }
/* ********************************************************************************************************* * BSP_DACReadRegister (CPU_INT08U ucRegister, CPU_INT08U *pucData) * * Description : Read a register in the TLV320AIC3107 DAC. * * Argument(s) : ucRegister is the offset to the register to write. * pucData is a pointer to the returned data. * * Return(s) : True on success or false on error. * * Caller(s) : Sound driver. * * Note(s) : * ********************************************************************************************************* */ static CPU_BOOLEAN BSP_DACReadRegister (CPU_INT08U ucRegister, CPU_INT08U *pucData) { // Set the slave address and "WRITE"/false. I2CMasterSlaveAddrSet(DAC_I2C_MASTER_BASE, TI_TLV320AIC3107_ADDR, false); // Write the first byte to the controller (register) I2CMasterDataPut(DAC_I2C_MASTER_BASE, ucRegister); // Continue the transfer. I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START); // Wait until the current byte has been transferred. while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0) { } if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) { I2CMasterIntClear(DAC_I2C_MASTER_BASE); return(false); } // Wait until the current byte has been transferred. while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false)) { I2CMasterIntClear(DAC_I2C_MASTER_BASE); } // Set the slave address and "READ"/true. I2CMasterSlaveAddrSet(DAC_I2C_MASTER_BASE, TI_TLV320AIC3107_ADDR, true); // Read Data Byte. I2CMasterControl(DAC_I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); // Wait until the current byte has been transferred. while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false) == 0) { } if(I2CMasterErr(DAC_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) { I2CMasterIntClear(DAC_I2C_MASTER_BASE); return(false); } // Wait until the current byte has been transferred. while(I2CMasterIntStatus(DAC_I2C_MASTER_BASE, false)) { I2CMasterIntClear(DAC_I2C_MASTER_BASE); } *pucData = I2CMasterDataGet(DAC_I2C_MASTER_BASE); return(true); }
/** * \brief This function reads data from EEPROM. * * \param data Address where data is to be read. * \param length Length of data to be read * \param offset Address of the byte from which data to be read. * * \return None. * * \note This muxing depends on the profile in which the EVM is configured. * EEPROMI2CSetUp Shall be called Before this API is used */ void EEPROMI2CRead(unsigned char *data, unsigned int length, unsigned short offset) { unsigned int idx = 0; /* First send the register offset - TX operation */ I2CSetDataCount(I2C_BASE_ADDR, 2); StatusClear(); I2CMasterControl(I2C_BASE_ADDR, I2C_CFG_MST_TX); I2CMasterStart(I2C_BASE_ADDR); /* Wait for the START to actually occir on the bus */ while (0 == I2CMasterBusBusy(I2C_BASE_ADDR)); I2CMasterDataPut(I2C_BASE_ADDR, (unsigned char)((offset >> 8) & 0xFF)); /* Wait for the Tx register to be empty */ while (0 == I2CMasterIntRawStatusEx(I2C_BASE_ADDR, I2C_INT_TRANSMIT_READY)); /* Push offset out and tell CPLD from where we intend to read the data */ I2CMasterDataPut(I2C_BASE_ADDR, (unsigned char)(offset & 0xFF)); I2CMasterIntClearEx(I2C_BASE_ADDR, I2C_INT_TRANSMIT_READY); while(0 == (I2CMasterIntRawStatus(I2C_BASE_ADDR) & I2C_INT_ADRR_READY_ACESS)); StatusClear(); I2CSetDataCount(I2C_BASE_ADDR, length); /* Now that we have sent the register offset, start a RX operation*/ I2CMasterControl(I2C_BASE_ADDR, I2C_CFG_MST_RX); /* Repeated start condition */ I2CMasterStart(I2C_BASE_ADDR); while (length--) { while (0 == I2CMasterIntRawStatusEx(I2C_BASE_ADDR, I2C_INT_RECV_READY)); data[idx++] = (unsigned char)I2CMasterDataGet(I2C_BASE_ADDR); I2CMasterIntClearEx(I2C_BASE_ADDR, I2C_INT_RECV_READY); } I2CMasterStop(I2C_BASE_ADDR); while(0 == (I2CMasterIntRawStatus(I2C_BASE_ADDR) & I2C_INT_STOP_CONDITION)); I2CMasterIntClearEx(I2C_BASE_ADDR, I2C_INT_STOP_CONDITION); }
/* ** ISR to handler i2c interrupts */ void I2CCodecIsr(void) { unsigned int intCode = 0; unsigned int sysIntNum = 0; /* Get interrupt vector code */ intCode = I2CInterruptVectorGet(savedBase); if(SOC_I2C_0_REGS == savedBase) { #ifdef _TMS320C6X sysIntNum = SYS_INT_I2C0_INT; #else sysIntNum = SYS_INT_I2CINT0; #endif } else { intCode = 0; } while(intCode!=0) { /* Clear status of interrupt */ #ifdef _TMS320C6X IntEventClear(sysIntNum); #else IntSystemStatusClear(sysIntNum); #endif if (intCode == I2C_INTCODE_TX_READY) { I2CMasterDataPut(savedBase, slaveData[dataIdx]); dataIdx++; } if(intCode == I2C_INTCODE_RX_READY) { slaveData[dataIdx] = I2CMasterDataGet(savedBase); dataIdx++; } if (intCode == I2C_INTCODE_STOP) { /* Disable transmit data ready and receive data read interupt */ I2CMasterIntDisableEx(savedBase, I2C_INT_TRANSMIT_READY | I2C_INT_DATA_READY); txCompFlag = 0; } intCode = I2CInterruptVectorGet(savedBase); } }
uint8_t i2c_read_byte(i2c_connection conn, enum i2c_ack_type ack_type) { struct i2c_state *c = (struct i2c_state *) conn; if (c->first) { /* First byte is buffered since the time of start. */ c->first = 0; } else { i2c_command(c, ack_type == I2C_ACK ? I2C_MASTER_CMD_BURST_RECEIVE_CONT : I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } return (uint8_t) I2CMasterDataGet(c->base); }
/* ** This function will read/write data from/to PMIC through I2C bus. */ void I2CPMICInteract(void) { volatile unsigned int intCode = 0; /* Get interrupt vector code */ intCode = I2CInterruptVectorGet(SOC_I2C_0_REGS); while(intCode!=0) { /* Clear status of interrupt */ IntSystemStatusClear(SYS_INT_I2CINT0); if (intCode == I2C_INTCODE_TX_READY) { /* Put data to data transmit register of i2c */ I2CMasterDataPut(SOC_I2C_0_REGS, dataToPmic[count++]); } if(intCode == I2C_INTCODE_RX_READY) { /* Receive data from data receive register */ dataFromSlave[rCount] = I2CMasterDataGet(SOC_I2C_0_REGS); } if (intCode == I2C_INTCODE_STOP) { /* Disable transmit data ready and receive data read interupt */ I2CMasterIntDisableEx(SOC_I2C_0_REGS,I2C_INT_TRANSMIT_READY| I2C_INT_DATA_READY); flag = 0; } if (intCode == I2C_INTCODE_NACK) { I2CMasterIntDisableEx(SOC_I2C_0_REGS, I2C_INT_TRANSMIT_READY | I2C_INT_DATA_READY | I2C_INT_NO_ACK | I2C_INT_STOP_CONDITION); /* Generate a STOP */ I2CMasterStop(SOC_I2C_0_REGS); I2CStatusClear(SOC_I2C_0_REGS, I2C_CLEAR_STOP_CONDITION); /* Clear interrupt at AINTC, if we missed any, in case of error */ flag = 0; } intCode = I2CInterruptVectorGet(SOC_I2C_0_REGS); } }
void vI2C_ISR(void) { static portTickType xReading; /* Clear the interrupt. */ I2CMasterIntClear( I2C_MASTER_BASE ); /* Determine what to do based on the current uxState. */ switch (uxState) { case mainI2C_IDLE: break; case mainI2C_READ_1: /* Read ADC result high byte. */ xReading = I2CMasterDataGet( I2C_MASTER_BASE ); xReading <<= 8; /* Continue the burst read. */ I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT ); uxState = mainI2C_READ_2; break; case mainI2C_READ_2: /* Read ADC result low byte. */ xReading |= I2CMasterDataGet( I2C_MASTER_BASE ); /* Finish the burst read. */ I2CMasterControl( I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH ); uxState = mainI2C_READ_DONE; break; case mainI2C_READ_DONE: /* Complete. */ I2CMasterDataGet( I2C_MASTER_BASE ); uxState = mainI2C_IDLE; /* Send the result to the co-routine. */ crQUEUE_SEND_FROM_ISR( xADCQueue, &xReading, pdFALSE ); break; } }
unsigned long i2c_rx_single(unsigned char SlaveAddr, unsigned char dest) { I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_SEND ); I2CMasterDataPut( I2C1_MASTER_BASE, dest ); I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND ); while (I2CMasterBusy( I2C1_MASTER_BASE )); I2CMasterSlaveAddrSet( I2C1_MASTER_BASE, SlaveAddr, I2C_RECEIVE ); I2CMasterControl( I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE ); while(I2CMasterBusy( I2C1_MASTER_BASE )); return I2CMasterDataGet(I2C1_MASTER_BASE); }
//***************************************************************************** // // Reads a single gyro register. This routine is blocking. // // \param ui8RegisterAddress is the register address // \return read data // //***************************************************************************** uint8_t Accel_RegRead(uint8_t ui8RegisterAddress) { uint8_t ui8Data = 0; if (VERBOSE) UARTprintf("GryroRegRead(0x%x)\n", ui8RegisterAddress); // Set the slave device address for WRITE // false = this I2C Master is initiating a writes to the slave. // true = this I2C Master is initiating reads from the slave. I2CMasterSlaveAddrSet(I2C1_BASE, ACCEL_SLAVE_ADDRESS, false); // // Transaction #1: Send the register address // // Set the Gyro Register address to write I2CMasterDataPut(I2C1_BASE, ui8RegisterAddress); // Start, send device address, write one byte (register address), and end the transaction I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_SEND); // Wait for completion while(I2CMasterBusy(I2C1_BASE)) { //spin wait } //TODO: Check I2CMasterErr(I2C1_BASE) // // Transaction #2: Read the register data // // Set the slave device address for READ // false = this I2C Master is initiating a writes to the slave. // true = this I2C Master is initiating reads from the slave. I2CMasterSlaveAddrSet(I2C1_BASE, ACCEL_SLAVE_ADDRESS, true); // Start, send device address, read one byte (register data), and end the transaction I2CMasterControl(I2C1_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); // Wait for completion while(I2CMasterBusy(I2C1_BASE)) { //spin wait } //TODO: Check I2CMasterErr(I2C1_BASE) ui8Data = I2CMasterDataGet(I2C1_BASE); return ui8Data; }
void Read_temp(unsigned char *data){ // Read Temperature sensor unsigned char temp[2]; // storage for data I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); // Start condition SysCtlDelay(20000); // Delay temp[0] = I2CMasterDataGet(I2C0_BASE); // Read first char SysCtlDelay(20000); // Delay I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); // Push second Char SysCtlDelay(20000); // Delay temp[1] = I2CMasterDataGet(I2C0_BASE); // Read second char I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); // Stop Condition data[0] = (temp[0] / 10) + 0x30; // convert 10 place to ASCII data[1] = (temp[0] - ((temp[0] / 10)*10)) + 0x30; // Convert 1's place to ASCII if(temp[1] == 0x80){ // Test for .5 accuracy data[3] = 0x35; } else{ data[3] = 0x30; } }
unsigned long byteAccelRead(int reg) { short temp; I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x1D, false); I2CMasterDataPut(I2C_MASTER_BASE, reg); I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START); while(I2CMasterBusy(I2C_MASTER_BASE)); I2CMasterSlaveAddrSet(I2C_MASTER_BASE, 0x1D, true); I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); while(I2CMasterBusy(I2C_MASTER_BASE)); return I2CMasterDataGet(I2C_MASTER_BASE); }
void i2c3_master_interrupt(void) { unsigned long status = I2CMasterIntStatusEx(I2C3_MASTER_BASE, false); if (status & I2C_MASTER_INT_TIMEOUT) { i2c_flag = 2; I2CMasterIntClearEx(I2C3_MASTER_BASE, I2C_MASTER_INT_TIMEOUT); } if (status & I2C_MASTER_INT_DATA) { err = I2CMasterErr(I2C3_MASTER_BASE); if (err != I2C_MASTER_ERR_NONE) i2c_flag = 2; if (what_we_re_doing == sending) { sent_bytes++; if (sent_bytes < sizeof(data)-1) { //Continuing I2CMasterDataPut(I2C3_MASTER_BASE, ((unsigned char *)&data)[sent_bytes]); I2CMasterControl(I2C3_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); } else if (sent_bytes == sizeof(data)-1) { //Last byte remaining I2CMasterDataPut(I2C3_MASTER_BASE, ((unsigned char *)&data)[sent_bytes]); I2CMasterControl(I2C3_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); } else { //Transaction is done i2c_flag = 1; } } else if (what_we_re_doing == receiving) { unsigned char *p = (unsigned char *)&data_recv + received_bytes; *p = I2CMasterDataGet(I2C3_MASTER_BASE); received_bytes++; if (received_bytes < sizeof(data_recv) - 1) { I2CMasterControl(I2C3_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); } else if (received_bytes == sizeof(data_recv) - 1) { I2CMasterControl(I2C3_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } else { //Transaction is done i2c_flag = 1; } } I2CMasterIntClearEx(I2C3_MASTER_BASE, I2C_MASTER_INT_DATA); } }
//Read from device using address and put the read values into buff. num=num of bytes to read void I2CRead(unsigned char device, unsigned char regiester, unsigned char num, unsigned long *buff){ //All commented code could likely be delted currently saving for record / emergency /*I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, false); //Set Device to transmit to I2CMasterDataPut(I2C1_MASTER_BASE,regiester); //Put on regiester to prep writting I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_SINGLE_SEND); //while(I2CMasterBusBusy(I2C1_MASTER_BASE)); //Wait till data sent while(I2CMasterBusy(I2C1_MASTER_BASE)); //this is good short i=0; for (i=0; i<num; i++){ I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, true); //Set device to RECIEVE FROM /*I2CMasterDataPut(I2C1_MASTER_BASE,regiester+i); //Put on regiester to prep writting I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_SINGLE_SEND); //while(I2CMasterBusBusy(I2C1_MASTER_BASE)); //Wait till data sent while(I2CMasterBusy(I2C1_MASTER_BASE)); */ /*if(i==0){ I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); }else if(i==(num-1)){ I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); }else{ I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); } //while(I2CMasterIntStatus(I2C1_MASTER_BASE, false) == 0); while(I2CMasterBusy(I2C1_MASTER_BASE)); buff[i] = I2CMasterDataGet(I2C1_MASTER_BASE); //I2CMasterIntClear(I2C1_MASTER_BASE); } //buff[0]=0x02; */ short i=0; //Initalize i because CCS doesn't like initalizing inside for loop :P for(i=0; i<num; i++){ I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, false); //Set device to write to I2CMasterDataPut(I2C1_MASTER_BASE,regiester+i); //Put on regiester to prep writting I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_SINGLE_SEND); //Make a single send there is no bursting through while(I2CMasterBusy(I2C1_MASTER_BASE)); //wait for the transmission to end via checking the master's state NOT THE BUS I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, device, true); //Set device to RECIEVE FROM I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); //Set a single read because it will only return this address while(I2CMasterBusy(I2C1_MASTER_BASE)); //Wait for the master to stop recieving data buff[i] = I2CMasterDataGet(I2C1_MASTER_BASE); //Place the data recieved (that is in the FIFO) into the buffer to return } }
uint8_t TwoWire::getRxData(unsigned long cmd) { if (currentState == IDLE) while(I2CMasterBusBusy(MASTER_BASE)); HWREG(MASTER_BASE + I2C_O_MCS) = cmd; while(I2CMasterBusy(MASTER_BASE)); uint8_t error = I2CMasterErr(MASTER_BASE); if (error != I2C_MASTER_ERR_NONE) { I2CMasterControl(MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP); } else { while(I2CMasterBusy(MASTER_BASE)); rxBuffer[rxWriteIndex] = I2CMasterDataGet(MASTER_BASE); rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH; } return error; }
uint32_t I2CReadMultiple(uint32_t i2c_base, uint8_t address, uint8_t reg, uint8_t *data, uint32_t size) { uint32_t byte_count; uint32_t master_command; // Set slave register to be read while(I2CMasterBusy(i2c_base)); I2CMasterSlaveAddrSet(i2c_base, address, 0); I2CMasterDataPut(i2c_base, reg); I2CMasterControl(i2c_base, I2C_MASTER_CMD_SINGLE_SEND); while(I2CMasterBusy(i2c_base)); // Check for errors if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0; // Start burst read I2CMasterSlaveAddrSet(i2c_base, address, 1); master_command = I2C_MASTER_CMD_BURST_RECEIVE_START; for (byte_count = 0; byte_count < size; byte_count++) { // The second byte has to be read with CONT if (byte_count == 1) master_command = I2C_MASTER_CMD_BURST_RECEIVE_CONT; // The last byte has to be read with FINISH if (byte_count == size - 1) master_command = I2C_MASTER_CMD_BURST_RECEIVE_FINISH; // If only one byte, reconfigure to SINGLE if (size == 1) master_command = I2C_MASTER_CMD_SINGLE_RECEIVE; // Initiate read I2CMasterControl(i2c_base, master_command); // Wait for master operation while(I2CMasterBusy(i2c_base)); // Check for errors if (I2CMasterErr(i2c_base) != I2C_MASTER_ERR_NONE) return 0; // Put data into array data[byte_count] = I2CMasterDataGet(i2c_base); } return byte_count; }
bool i2c_read_byte(uint8_t address, uint8_t* byte) { uint32_t future = I2C_MAX_DELAY_US; // Receive operation I2CMasterSlaveAddrSet(address, true); // Single receive operation I2CMasterControl(I2C_MASTER_CMD_SINGLE_RECEIVE); // Calculate timeout future += board_timer_get(); // Wait until complete or timeout while (I2CMasterBusy()) { // Update timeout status and return if expired if (board_timer_expired(future)) return false; } // Read data from I2C *byte = I2CMasterDataGet(); // Return status return true; }
//***************************************************************************** // //! Reads one/multiple bytes of data from an I2C slave device. //! //! \param ulI2CBase is the base for the I2C module. //! \param ucSlaveAdress is the 7-bit address of the slave to be addressed. //! \param ucReg is the register to start reading from. //! \param cReadData is a pointer to the array to store the data. //! \param uiSize is the number of bytes to read from the slave. //! //! This function reads one/multiple bytes of data from an I2C slave device. //! The ulI2CBase parameter is the I2C modules master base address. //! \e ulI2CBase parameter can be one of the following values: //! //! - \b I2C0_MASTER_BASE //! - \b I2C1_MASTER_BASE //! - \b I2C2_MASTER_BASE //! - \b I2C3_MASTER_BASE //! //! \return 0 if there was an error or 1 if there was not. // //***************************************************************************** unsigned long I2CReadData(unsigned long ulI2CBase, unsigned char ucSlaveAdress, unsigned char ucReg, char* cReadData, unsigned int uiSize) { unsigned int uibytecount; // local variable used for byte counting/state determination int MasterOptionCommand; // used to assign the commands for ROM_I2CMasterControl() function // // Check the arguments. // ASSERT(I2CMasterBaseValid(ulI2CBase)); // // Wait until master module is done transferring. // while(ROM_I2CMasterBusy(ulI2CBase)) { }; // // Tell the master module what address it will place on the bus when // writing to the slave. // ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, 0); // // Place the command to be sent in the data register. // ROM_I2CMasterDataPut(ulI2CBase, ucReg); // // Initiate send of data from the master. // ROM_I2CMasterControl(ulI2CBase, I2C_MASTER_CMD_SINGLE_SEND); // // Wait until master module is done transferring. // while(ROM_I2CMasterBusy(ulI2CBase)) { }; // // Check for errors. // if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE) { return 0; } // // Tell the master module what address it will place on the bus when // reading from the slave. // ROM_I2CMasterSlaveAddrSet(ulI2CBase, ucSlaveAdress, true); // // Start with BURST with more than one byte to write // MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_START; for(uibytecount = 0; uibytecount < uiSize; uibytecount++) { // // The second and intermittent byte has to be read with CONTINUE control word // if(uibytecount == 1) MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_CONT; // // The last byte has to be send with FINISH control word // if(uibytecount == uiSize - 1) MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_FINISH; // // Re-configure to SINGLE if there is only one byte to read // if(uiSize == 1) MasterOptionCommand = I2C_MASTER_CMD_SINGLE_RECEIVE; // // Initiate read of data from the slave. // ROM_I2CMasterControl(ulI2CBase, MasterOptionCommand); // // Wait until master module is done reading. // while(ROM_I2CMasterBusy(ulI2CBase)) { }; // // Check for errors. // if(ROM_I2CMasterErr(ulI2CBase) != I2C_MASTER_ERR_NONE) { return 0; } // // Move byte from register // cReadData[uibytecount] = I2CMasterDataGet(ulI2CBase); } // send number of received bytes return uibytecount; }
/* ** I2C Interrupt Service Routine. This function will read and write ** data through I2C bus. */ void I2CIsr(new_twi* TwiStruct) { unsigned int status = 0; /* Get only Enabled interrupt status */ status = I2CMasterIntStatus(TwiStruct->BaseAddr); /* ** Clear all enabled interrupt status except receive ready and ** transmit ready interrupt status */ I2CMasterIntClearEx(TwiStruct->BaseAddr, (status & ~(I2C_INT_RECV_READY | I2C_INT_TRANSMIT_READY | I2C_INT_NO_ACK))); if(status & I2C_INT_RECV_READY) { /* Receive data from data receive register */ TwiStruct->RxBuff[TwiStruct->rCount++] = I2CMasterDataGet(TwiStruct->BaseAddr); /* Clear receive ready interrupt status */ I2CMasterIntClearEx(TwiStruct->BaseAddr, I2C_INT_RECV_READY); if(TwiStruct->rCount == TwiStruct->numOfBytes) { /* Disable the receive ready interrupt */ I2CMasterIntDisableEx(TwiStruct->BaseAddr, I2C_INT_RECV_READY); /* Generate a STOP */ I2CMasterStop(TwiStruct->BaseAddr); } } if (status & I2C_INT_TRANSMIT_READY) { /* Put data to data transmit register of i2c */ I2CMasterDataPut(TwiStruct->BaseAddr, TwiStruct->TxBuff[TwiStruct->tCount++]); /* Clear Transmit interrupt status */ I2CMasterIntClearEx(TwiStruct->BaseAddr, I2C_INT_TRANSMIT_READY); if(TwiStruct->tCount == TwiStruct->numOfBytes) { /* Disable the transmit ready interrupt */ I2CMasterIntDisableEx(TwiStruct->BaseAddr, I2C_INT_TRANSMIT_READY); } } if (status & I2C_INT_STOP_CONDITION) { /* Disable transmit data ready and receive data read interupt */ I2CMasterIntDisableEx(TwiStruct->BaseAddr, I2C_INT_TRANSMIT_READY | I2C_INT_RECV_READY | I2C_INT_STOP_CONDITION); TwiStruct->flag = 0; } if(status & I2C_INT_NO_ACK) { I2CMasterIntDisableEx(TwiStruct->BaseAddr, I2C_INT_TRANSMIT_READY | I2C_INT_RECV_READY | I2C_INT_NO_ACK | I2C_INT_STOP_CONDITION); /* Generate a STOP */ I2CMasterStop(TwiStruct->BaseAddr); TwiStruct->flag = 0; TwiStruct->error_flag = 1; } //I2CEndOfInterrupt(TwiStruct->BaseAddr, 0); }
char I2CGenTransmit(char * pbData, int cSize, bool fRW, char bAddr) { int i; char * pbTemp; pbTemp = pbData; /*Start*/ /*Send Address High Byte*/ /* Send Write Block Cmd*/ I2CMasterSlaveAddrSet(I2C0_BASE, bAddr, WRITE); I2CMasterDataPut(I2C0_BASE, *pbTemp); I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START); DelayMs(1); /* Idle wait*/ while(I2CGenIsNotIdle()); /* Increment data pointer*/ pbTemp++; /*Execute Read or Write*/ if(fRW == READ) { /* Resend Start condition ** Then send new control byte ** then begin reading */ I2CMasterSlaveAddrSet(I2C0_BASE, bAddr, READ); while(I2CMasterBusy(I2C0_BASE)); /* Begin Reading*/ for(i = 0; i < cSize; i++) { if(cSize == i + 1 && cSize == 1) { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); DelayMs(1); while(I2CMasterBusy(I2C0_BASE)); } else if(cSize == i + 1 && cSize > 1) { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); DelayMs(1); while(I2CMasterBusy(I2C0_BASE)); } else if(i == 0) { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); DelayMs(1); while(I2CMasterBusy(I2C0_BASE)); /* Idle wait*/ while(I2CGenIsNotIdle()); } else { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); DelayMs(1); while(I2CMasterBusy(I2C0_BASE)); /* Idle wait */ while(I2CGenIsNotIdle()); } while(I2CMasterBusy(I2C0_BASE)); /* Read Data */ *pbTemp = (char)I2CMasterDataGet(I2C0_BASE); pbTemp++; } } else if(fRW == WRITE) { /*Loop data bytes */ for(i = 0; i < cSize; i++) { /* Send Data */ I2CMasterDataPut(I2C0_BASE, *pbTemp); while(I2CMasterBusy(I2C0_BASE)); if(i == cSize - 1) { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); DelayMs(1); while(I2CMasterBusy(I2C0_BASE)); } else { I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); DelayMs(1); while(I2CMasterBusy(I2C0_BASE)); /* Idle wait */ while(I2CGenIsNotIdle()); } pbTemp++; } } /*Stop*/ return 0x00; }
int main(void) { unsigned long ulPeriod; unsigned long ulDelay; unsigned long recvData = 11; volatile int status = 0; char buffer[40]; unsigned int bytesdRead; FRESULT res; FIL fileobj; SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); //Initialize pins init_pins(); //Initialize peripherals init_periph(); I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false);//false for 100kHz mode //0x68 is the 7-bit address of the DS1307 I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, 0x68, false);//true for a read action I2CMasterDataPut(I2C0_MASTER_BASE, 0x10); //first Date/Time Register I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); while(I2CMasterBusy(I2C0_MASTER_BASE)){} I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); while(I2CMasterBusy(I2C0_MASTER_BASE)){} recvData = I2CMasterDataGet(I2C0_MASTER_BASE); while(I2CMasterBusy(I2C0_MASTER_BASE)){} fs_mount(); if(disk_status(0) == 0) { status++; } else { status--; } if (status == 1) { res = f_open(&fileobj, "config.txt", FA_OPEN_EXISTING | FA_READ); res = f_read(&fileobj, buffer, 6, &bytesdRead); } SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0); ulPeriod = SysCtlClockGet() / 10; ulDelay = ((ulPeriod / 2) / 3) - 4 ; while(1) { // Turn on the LED GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x01); // Delay for a bit SysCtlDelay(ulDelay); // Turn off the LED GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00); // Delay for a bit SysCtlDelay(ulDelay); } }
//***************************************************************************** // //! Reads one/multiple bytes of data from an I2C slave device. //! //! \param ACCEL_I2C_MASTER_BASE is the base for the I2C module. //! \param SlaveID is the 7-bit address of the slave to be addressed. //! \param addr is the register to start reading from. //! \param pBuf is a pointer to the array to store the data. //! \param nBytes is the number of bytes to read from the slave. //! //! This function reads one/multiple bytes of data from an I2C slave device. //! The ACCEL_I2C_MASTER_BASE parameter is the I2C modules master base address. //! \e ACCEL_I2C_MASTER_BASE parameter can be one of the following values: //! //! //! \return 0 if there was an error or 1 if there was not. // //int32_t i2c_RcvBuf(uint8_t SlaveID, uint8_t addr, int32_t nBytes, uint8_t* pBuf); //***************************************************************************** int32_t i2c_ReadBuf(uint8_t SlaveID, uint8_t addr, int32_t nBytes, uint8_t* pBuf) { uint8_t nBytesCount; // local variable used for byte counting/state determination uint16_t MasterOptionCommand; // used to assign the commands for ROM_I2CMasterControl() function // // Wait until master module is done transferring. // while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) { }; // // Tell the master module what address it will place on the bus when // writing to the slave. // ROM_I2CMasterSlaveAddrSet(ACCEL_I2C_MASTER_BASE, SlaveID, 0); // // Place the command to be sent in the data register. // ROM_I2CMasterDataPut(ACCEL_I2C_MASTER_BASE, addr); // // Initiate send of data from the master. // ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND); // // Wait until master module is done transferring. // while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) { }; // // Check for errors. // if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) { return 0; } // // Tell the master module what address it will place on the bus when // reading from the slave. // ROM_I2CMasterSlaveAddrSet(ACCEL_I2C_MASTER_BASE, SlaveID, true); // // Start with BURST with more than one byte to write // MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_START; for (nBytesCount = 0; nBytesCount < nBytes; nBytesCount++) { // // The second and intermittent byte has to be read with CONTINUE control word // if (nBytesCount == 1) MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_CONT; // // The last byte has to be send with FINISH control word // if (nBytesCount == nBytes - 1) MasterOptionCommand = I2C_MASTER_CMD_BURST_RECEIVE_FINISH; // // Re-configure to SINGLE if there is only one byte to read // if (nBytes == 1) MasterOptionCommand = I2C_MASTER_CMD_SINGLE_RECEIVE; // // Initiate read of data from the slave. // ROM_I2CMasterControl(ACCEL_I2C_MASTER_BASE, MasterOptionCommand); // // Wait until master module is done reading. // while (ROM_I2CMasterBusy(ACCEL_I2C_MASTER_BASE)) { }; // // Check for errors. // if (ROM_I2CMasterErr(ACCEL_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) { return 0; } // // Move byte from register // pBuf[nBytesCount] = I2CMasterDataGet(ACCEL_I2C_MASTER_BASE); } // send number of received bytes return nBytesCount; }
//***************************************************************************** // // Reads from the I2C-attached EEPROM device. // // \param pucData points to storage for the data read from the EEPROM. // \param ulOffset is the EEPROM address of the first byte to read. // \param ulCount is the number of bytes of data to read from the EEPROM. // // This function reads one or more bytes of data from a given address in the // ID EEPROM found on several of the lm3s9b96 development kit daughter boards. // The return code indicates whether the read was successful. // // \return Returns \b true on success of \b false on failure. // //***************************************************************************** static tBoolean EEPROMReadPolled(unsigned char *pucData, unsigned long ulOffset, unsigned long ulCount) { unsigned long ulToRead; // // Clear any previously signalled interrupts. // I2CMasterIntClear(ID_I2C_MASTER_BASE); // // Start with a dummy write to get the address set in the EEPROM. // I2CMasterSlaveAddrSet(ID_I2C_MASTER_BASE, ID_I2C_ADDR, false); // // Place the address to be written in the data register. // I2CMasterDataPut(ID_I2C_MASTER_BASE, ulOffset); // // Perform a single send, writing the address as the only byte. // I2CMasterControl(ID_I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START); // // Wait until the current byte has been transferred. // while(I2CMasterIntStatus(ID_I2C_MASTER_BASE, false) == 0) { } if(I2CMasterErr(ID_I2C_MASTER_BASE) != I2C_MASTER_ERR_NONE) { I2CMasterIntClear(ID_I2C_MASTER_BASE); return(false); } // // Clear any interrupts set. // I2CMasterIntClear(ID_I2C_MASTER_BASE); // // Put the I2C master into receive mode. // I2CMasterSlaveAddrSet(ID_I2C_MASTER_BASE, ID_I2C_ADDR, true); // // Start the receive. // I2CMasterControl(ID_I2C_MASTER_BASE, ((ulCount > 1) ? I2C_MASTER_CMD_BURST_RECEIVE_START : I2C_MASTER_CMD_SINGLE_RECEIVE)); // // Receive the required number of bytes. // ulToRead = ulCount; while(ulToRead) { // // Wait until the current byte has been read. // while(I2CMasterIntStatus(ID_I2C_MASTER_BASE, false) == 0) { } // // Read the received character. // *pucData++ = I2CMasterDataGet(ID_I2C_MASTER_BASE); ulToRead--; // // Clear pending interrupt notifications. // I2CMasterIntClear(ID_I2C_MASTER_BASE); // // Set up for the next byte if any more remain. // if(ulToRead) { I2CMasterControl(ID_I2C_MASTER_BASE, ((ulToRead == 1) ? I2C_MASTER_CMD_BURST_RECEIVE_FINISH : I2C_MASTER_CMD_BURST_RECEIVE_CONT)); } } // // Clear pending interrupt notification. // I2CMasterIntClear(ID_I2C_MASTER_BASE); // // Tell the caller we read the required data. // return(true); }
//***************************************************************************** // // 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); }