/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// slave msp430G2553 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include /* * main.c */ const unsigned char TxData[] = // Table of data to transmit { 0x11, 0x22, 0x33, 0x44, 0x55 }; #define CRC_KEY 7 #define DELAY_LIMIT 0xffff void ClockInitialise() { DCOCTL = 0xE0; //DCO = 7, MOD = 0 BCSCTL1 &= 0x8F; //RSEL = 0, DCOR = 0, 20MHz Clock BCSCTL2 = 0x0; BCSCTL3 = 0; } void I2CInitialise() { P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0, P1.6 for SCL and P1.7 for SDA P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0, P1.6 for SCL and P1.7 for SDA UCB0CTL1 |= UCSWRST; // Enable SW reset, hold USCI logic in reset state UCB0CTL0 = UCMODE_3 + UCSYNC; //set to I2C mode, sync=1 UCB0CTL1 |= UCSSEL_2; // Use SMCLK UCB0BR0 = 4; // fSCL = SMCLK/12 = ~100kHz 10 for 100KHz 4 for 400KHz UCB0BR1 = 0; UCB0I2CIE = 0; IE2 &= ~(UCB0TXIE + UCB0RXIE); //disable interrupts // UCB0CTL1 |= UCSSEL_2; UCB0CTL1 &= ~UCSWRST; } int I2CSendByte(unsigned char I2CSlaveAddress, unsigned char data) { unsigned long int DelayCounter = 0; UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode UCB0I2COA = I2CSlaveAddress; UCB0CTL1 |= UCTR; //data in transmit direction UCB0CTL1 |= UCTXSTT; //Generate Start Condition //Send Start Byte while(!(IFG2 & UCB0TXIFG)) //if UCB0TXIFG != 0, wait here { DelayCounter ++; if (DelayCounter >= DELAY_LIMIT) break; } if (DelayCounter >= DELAY_LIMIT) return -1; UCB0TXBUF = data; // send the data DelayCounter = 0; while(DelayCounter < DELAY_LIMIT && !(IFG2 & UCB0TXIFG)) { DelayCounter++; } if (DelayCounter >= DELAY_LIMIT) return -1; UCB0CTL1 |= UCTXSTP; //send stop bit DelayCounter = 0; /* while(DelayCounter < DELAY_LIMIT && (UCB0CTL1 & UCTXSTP)) { DelayCounter++; }*/ if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred return -1; else return 0; } int I2CSendBytes(unsigned char I2CSlaveAddress, unsigned char *DataBuffer, unsigned int ByteCount, unsigned int *SentByte) { unsigned long int DelayCounter = 0; unsigned int NumberOfBytesSent = 0; unsigned char *DataPointer; UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode UCB0I2COA = I2CSlaveAddress; DataPointer = DataBuffer; UCB0CTL1 |= UCTR; //data in transmit direction UCB0CTL1 |= UCTXSTT; //Generate Start Condition //Send Start Byte while(!(IFG2 & UCB0TXIFG)) //if UCTXSTT != 0, wait here { DelayCounter ++; if (DelayCounter > DELAY_LIMIT) break; } if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred { *SentByte = NumberOfBytesSent; UCB0CTL1 |= UCTXSTP; return -1; } for(NumberOfBytesSent = 0; NumberOfBytesSent < ByteCount; NumberOfBytesSent++) { UCB0TXBUF= *DataPointer; DelayCounter = 0; while(DelayCounter < DELAY_LIMIT && (!(IFG2 & UCB0TXIFG) || (UCB0CTL1 & UCTXSTT))) //check if the byte has been sent { DelayCounter++; } if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred { *SentByte = NumberOfBytesSent; UCB0CTL1 |= UCTXSTP; //send stop condition return -1; } DataPointer++; } IFG2 &= ~UCB0TXIFG; UCB0CTL1 |= UCTXSTP; //send stop bit DelayCounter = 0; while(DelayCounter < DELAY_LIMIT && ((UCB0CTL1 & UCTXSTP))) { DelayCounter++; } *SentByte = NumberOfBytesSent; if (DelayCounter >= DELAY_LIMIT) //check if NACK condition occurred { UCB0CTL1 |= UCSWRST; return -1; } else return 0; } int I2CWriteRegisterByte(unsigned char I2CSlaveAddress, unsigned char Register, unsigned char Data) { unsigned char DataBuffer[2]; unsigned int SentByte = 0; DataBuffer[0] = Register; DataBuffer[1] = Data; return(I2CSendBytes(I2CSlaveAddress, DataBuffer, 2, &SentByte)); } int I2CWriteRegisterByteWithCRC(unsigned char I2CSlaveAddress, unsigned char Register, unsigned char Data) { unsigned char DataBuffer[4]; unsigned int SentByte = 0; DataBuffer[0] = I2CSlaveAddress << 1; DataBuffer[1] = Register; DataBuffer[2] = Data; DataBuffer[3] = CRC8(DataBuffer, 3, CRC_KEY); return(I2CSendBytes(I2CSlaveAddress, DataBuffer + 1, 3, &SentByte)); } int I2CWriteRegisterWordWithCRC(unsigned char I2CSlaveAddress, unsigned char Register, unsigned int Data) { unsigned char DataBuffer[6]; unsigned int SentByte = 0; DataBuffer[0] = I2CSlaveAddress << 1; DataBuffer[1] = Register; DataBuffer[2] = LOW_BYTE(Data); DataBuffer[3] = CRC8(DataBuffer, 3, CRC_KEY); DataBuffer[4] = HIGH_BYTE(Data); DataBuffer[5] = CRC8(DataBuffer + 4, 1, CRC_KEY); return(I2CSendBytes(I2CSlaveAddress, DataBuffer + 1, 5, &SentByte)); } int I2CWriteBlockWithCRC(unsigned char I2CSlaveAddress, unsigned char StartAddress, unsigned char *Buffer, unsigned char Length) { unsigned char *BufferCRC, *Pointer; int i; unsigned int SentByte = 0; int result; BufferCRC = (unsigned char*)malloc(2*Length + 2); if (NULL == BufferCRC) return -1; Pointer = BufferCRC; *Pointer = I2CSlaveAddress << 1; Pointer++; *Pointer = StartAddress; Pointer++; *Pointer = *Buffer; Pointer++; *Pointer = CRC8(BufferCRC, 3, CRC_KEY); for(i = 1; i < Length; i++) { Pointer++; Buffer++; *Pointer = *Buffer; *(Pointer + 1) = CRC8(Pointer, 1, CRC_KEY); Pointer++; } result = I2CSendBytes(I2CSlaveAddress, BufferCRC + 1, 2*Length + 1, &SentByte); free(BufferCRC); BufferCRC = NULL; return result; } int I2CWriteRegisterWord(unsigned char I2CSlaveAddress, unsigned char Register, unsigned int Data) { unsigned char DataBuffer[3]; unsigned int SentByte = 0; DataBuffer[0] = Register; DataBuffer[1] = LOWBYTE(Data); DataBuffer[2] = HIGHBYTE(Data); return(I2CSendBytes(I2CSlaveAddress, DataBuffer, 3, &SentByte)); } int I2CReadBytes(unsigned char I2CSlaveAddress, unsigned char *DataBuffer, unsigned int ExpectedByteNumber, unsigned int *NumberOfReceivedBytes) { unsigned long int DelayCounter = 0; unsigned char *DataPointer; unsigned int *NumberOfReceivedBytesPointer; NumberOfReceivedBytesPointer = NumberOfReceivedBytes; *NumberOfReceivedBytesPointer = 0; UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode DataPointer = DataBuffer; UCB0I2COA = I2CSlaveAddress; UCB0CTL1 &= ~(UCTR); //data in receive direction UCB0CTL1 |= UCTXSTT; //Generate Start Condition while((UCB0CTL1 & UCTXSTT) ) //if UCTXSTT != 0, wait here { DelayCounter ++; if (DelayCounter >= DELAY_LIMIT) break; } if (DelayCounter >= DELAY_LIMIT || UCB0STAT & UCNACKIFG) //check if NACK condition occurred return -1; for(*NumberOfReceivedBytesPointer = 0; *NumberOfReceivedBytesPointer < ExpectedByteNumber; (*NumberOfReceivedBytesPointer)++) { if(*NumberOfReceivedBytesPointer + 1 == ExpectedByteNumber) UCB0CTL1 |= UCTXSTP; DelayCounter = 0; while(DelayCounter < DELAY_LIMIT && !(IFG2 & UCB0RXIFG)) { DelayCounter++; } if(DelayCounter == DELAY_LIMIT) { UCB0CTL1 |= UCSWRST; //if I2C overtime condition occurred, reset I2C engine return -1; } *DataPointer = UCB0RXBUF; DataPointer++; } DelayCounter = 0; while(DelayCounter < DELAY_LIMIT && (UCB0CTL1 & UCTXSTP)) { DelayCounter++; } if(DelayCounter >= DELAY_LIMIT) { UCB0CTL1 |= UCSWRST; return -1; } return 0; } int I2CReadRegisterByte(unsigned char I2CSlaveAddress, unsigned char Register, unsigned char *Data) { unsigned char TargetRegister = Register; unsigned int SentByte = 0; unsigned int ReadDataCount = 0; int ReadStatus = 0; int WriteStatus = 0; WriteStatus = I2CSendBytes(I2CSlaveAddress, &TargetRegister, 1, &SentByte); ReadStatus = I2CReadBytes(I2CSlaveAddress, Data, 1, &ReadDataCount); if (ReadStatus != 0 || WriteStatus != 0) { return -1; } return 0; } int I2CReadBlock(unsigned char I2CSlaveAddress, unsigned char StartRegisterAddress, unsigned char *Buffer, unsigned int BlockSize, unsigned int *NumberOfBytes) { unsigned char TargetRegister = StartRegisterAddress; unsigned int SentByte = 0; int ReadStatus = 0; int WriteStatus = 0; WriteStatus = I2CSendBytes(I2CSlaveAddress, &TargetRegister, 1, &SentByte); ReadStatus = I2CReadBytes(I2CSlaveAddress, Buffer, BlockSize, NumberOfBytes); if(ReadStatus != 0 || WriteStatus != 0) { return -1; } return 0; } unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key) { unsigned char i; unsigned char crc=0; while(len--!=0) { for(i=0x80; i!=0; i/=2) { if((crc & 0x80) != 0) { crc *= 2; crc ^= key; } else crc *= 2; if((*ptr & i)!=0) crc ^= key; } ptr++; } return(crc); } int I2CReadRegisterByteWithCRC(unsigned char I2CSlaveAddress, unsigned char Register, unsigned char *Data) { unsigned char TargetRegister = Register; unsigned int SentByte = 0; unsigned char ReadData[2]; unsigned int ReadDataCount = 0; unsigned char CRCInput[2]; unsigned char CRC = 0; int ReadStatus = 0; int WriteStatus = 0; WriteStatus = I2CSendBytes(I2CSlaveAddress, &TargetRegister, 1, &SentByte); ReadStatus = I2CReadBytes(I2CSlaveAddress, ReadData, 2, &ReadDataCount); if (ReadStatus != 0 || WriteStatus != 0) { return -1; } CRCInput[0] = (I2CSlaveAddress << 1) + 1; CRCInput[1] = ReadData[0]; CRC = CRC8(CRCInput, 2, CRC_KEY); if (CRC != ReadData[1]) return -1; *Data = ReadData[0]; return 0; } int I2CReadRegisterWordWithCRC(unsigned char I2CSlaveAddress, unsigned char Register, unsigned int *Data) { unsigned char TargetRegister = Register; unsigned int SentByte = 0; unsigned char ReadData[4]; unsigned int ReadDataCount = 0; unsigned char CRCInput[2]; unsigned char CRC = 0; int ReadStatus = 0; int WriteStatus = 0; WriteStatus = I2CSendBytes(I2CSlaveAddress, &TargetRegister, 1, &SentByte); ReadStatus = I2CReadBytes(I2CSlaveAddress, ReadData, 4, &ReadDataCount); if (ReadStatus != 0 || WriteStatus != 0) { return -1; } CRCInput[0] = (I2CSlaveAddress << 1) + 1; CRCInput[1] = ReadData[0]; CRC = CRC8(CRCInput, 2, CRC_KEY); if (CRC != ReadData[1]) return -1; CRC = CRC8(ReadData + 2, 1, CRC_KEY); if (CRC != ReadData[3]) return -1; *Data = ReadData[0]; *Data = (*Data << 8) + ReadData[2]; return 0; } int I2CReadBlockWithCRC(unsigned char I2CSlaveAddress, unsigned char Register, unsigned char *Buffer, unsigned char Length) { unsigned char TargetRegister = Register; unsigned int SentByte = 0; unsigned char *ReadData = NULL, *StartData = NULL; unsigned int ReadDataCount = 0; unsigned char CRCInput[2]; unsigned char CRC = 0; int ReadStatus = 0; int WriteStatus = 0; int i; StartData = (unsigned char *)malloc(2 * Length); if (NULL == StartData) return -1; ReadData = StartData; WriteStatus = I2CSendBytes(I2CSlaveAddress, &TargetRegister, 1, &SentByte); ReadStatus = I2CReadBytes(I2CSlaveAddress, ReadData, 2 * Length, &ReadDataCount); if (ReadStatus != 0 || WriteStatus != 0) { free(StartData); StartData = NULL; return -1; } CRCInput[0] = (I2CSlaveAddress << 1) + 1; CRCInput[1] = *ReadData; CRC = CRC8(CRCInput, 2, CRC_KEY); ReadData++; if (CRC != *ReadData) { free(StartData); StartData = NULL; return -1; } else *Buffer = *(ReadData - 1); for(i = 1; i < Length; i++) { ReadData++; CRC = CRC8(ReadData, 1, CRC_KEY); ReadData++; Buffer++; if (CRC != *ReadData) { free(StartData); StartData = NULL; return -1; } else *Buffer = *(ReadData - 1); } free(StartData); StartData = NULL; return 0; } int main(void) { int Result; int i,a; WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer // DISABLE_INT; //ClockInitialise(); I2CInitialise(); int count = 0; while(1) { for( i=0 ; i<5 ;i++) { // I2CSendByte(0x48,TxData[i]); I2CWriteRegisterByteWithCRC(0x48, TxData[i],TxData[i]); } // i=0; // I2CWriteRegisterByteWithCRC(0x48, 0x41, 0x42) count++; } return Result; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// master tm4c1294ncpdt //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include #include #include "driverlib/debug.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "driverlib/gpio.h" #include "driverlib/i2c.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" #define SLAVE_ADDRESS_EXT 0x48 //uint8_t Checksum; #define NUM_OF_I2CBYTES 5 //RX_Bytes uint8_t Command = 0x03; //TX_Command //***************************************************************************** // // Enumerated Data Types for Master State Machine // //***************************************************************************** enum I2C_MASTER_STATE { I2C_OP_IDLE = 0, I2C_OP_RXDATA, I2C_OP_STOP, }; //***************************************************************************** // // Global variable for Delay Count // //***************************************************************************** volatile uint16_t data; volatile uint8_t g_ui8MasterTxData[1]; volatile uint16_t g_ui8MasterRxData[NUM_OF_I2CBYTES]; volatile uint8_t g_ui8MasterCurrState; volatile uint8_t g_ui8MasterPrevState; volatile bool g_bI2CRepeatedStart; volatile uint8_t g_ui8MasterBytes = NUM_OF_I2CBYTES; volatile uint8_t g_ui8MasterBytesLength = NUM_OF_I2CBYTES; uint32_t ui32I2CMasterInterruptStatus; //***************************************************************************** // // Interrupt Handler for I2C Master Interface // //***************************************************************************** void I2C2IntHandler(void) { // // Toggle PL4 High to Indicate Entry to ISR // GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_4, GPIO_PIN_4); // // Get the masked interrupt status and clear the flags // ui32I2CMasterInterruptStatus = I2CMasterIntStatusEx(I2C2_BASE, true); I2CMasterIntClearEx(I2C2_BASE, ui32I2CMasterInterruptStatus); // // Execute the State Machine // switch (g_ui8MasterCurrState) { case I2C_OP_IDLE: // // Move from IDLE to Transmit Address State // g_ui8MasterPrevState = g_ui8MasterCurrState; // // Write the upper bits of the page to the Slave // I2CMasterSlaveAddrSet(I2C2_BASE, SLAVE_ADDRESS_EXT, false); I2CMasterDataPut(I2C2_BASE, 0x01); I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_SEND); g_ui8MasterCurrState = I2C_OP_STOP; break; case I2C_OP_RXDATA: // // Move the current state to the previous state // Else continue with the transmission till last byte // g_ui8MasterPrevState = g_ui8MasterCurrState; // // If Address has been NAK'ed then go to stop state // If a Stop condition is seen due to number of bytes getting // done then move to STOP state and read the last data byte // if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_NACK) { g_ui8MasterCurrState = I2C_OP_STOP; } else if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_STOP) { g_ui8MasterCurrState = I2C_OP_STOP; g_ui8MasterRxData[g_ui8MasterBytes++] = I2CMasterDataGet(I2C2_BASE); // UARTprintf("Received--1: '%x'\n",g_ui8MasterRxData[g_ui8MasterBytes]); } else { // // If end then NAK the byte and put Stop. Else continue // with ACK of the current byte and receive the next byte // if(g_bI2CRepeatedStart) { // // Send the Slave Address with RnW as Receive. If only byte is // to be received then send START and STOP else send START // I2CMasterSlaveAddrSet(I2C2_BASE, SLAVE_ADDRESS_EXT, true); if(g_ui8MasterBytesLength == 1) { I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE); } else { I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START); } // // Change the Repeated Start Flag to false as the condition // is now to receive data // g_bI2CRepeatedStart = false; } else if(g_ui8MasterBytes == (g_ui8MasterBytesLength - 2)) { // // Read the byte from I2C Buffer and decrement the number // of bytes counter to see if end has been reached or not // g_ui8MasterRxData[g_ui8MasterBytes++] = I2CMasterDataGet(I2C2_BASE); // UARTprintf("Received---2: '%x'\n",g_ui8MasterRxData[g_ui8MasterBytes]); // SysCtlDelay(120000); // // Put a STOP Condition on the bus // I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); } else { // // Read the byte from I2C Buffer and decrement the number // of bytes counter to see if end has been reached or not // g_ui8MasterRxData[g_ui8MasterBytes++] = I2CMasterDataGet(I2C2_BASE); I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); } } break; case I2C_OP_STOP: // // Move the current state to the previous state // Else continue with the transmission till last byte // g_ui8MasterPrevState = g_ui8MasterCurrState; break; default: break; } // // Toggle PL4 Low to Indicate Exit from ISR // GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_4, 0x0); } //***************************************************************************** // // This function sets up UART0 to be used for a console to display information // as the example is running. // //***************************************************************************** void InitConsole(void) { // // Enable GPIO port A which is used for UART0 pins. // TODO: change this to whichever GPIO port you are using. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // // Configure the pin muxing for UART0 functions on port A0 and A1. // 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_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); // // Enable UART0 so that we can configure the clock. // SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // // Use the internal 16MHz oscillator as the UART clock source. // UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); // // Select the alternate (UART) function for these pins. // TODO: change this to select the port/pin you are using. // GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Initialize the UART for console I/O. // UARTStdioConfig(0, 115200, 16000000); } //***************************************************************************** // // Main Program to Configure and Use the I2C Master // //***************************************************************************** int main(void) { uint32_t ui32SysClock; uint8_t ui8Count; bool bError; // // Setup System Clock for 120MHz // ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ | SYSCTL_CFG_VCO_480), 120000000); // // Set up the serial console to use for displaying messages. This is // just for this example program and is not needed for EPI operation. // InitConsole(); initI2C(ui32SysClock); // // Initialize and Configure the Master Module State Machine // while(1) { //////////// for TX Command ////////////// /* g_ui8MasterCurrState = I2C_OP_IDLE; while(I2CMasterBusBusy(I2C2_BASE)); // g_ui8MasterBytes = 0; //UARTprintf("Transmit %x bytes to external Slave...\n\n",Command); // // Trigger the Transfer using Software Interrupt // IntTrigger(INT_I2C2); while(g_ui8MasterCurrState != I2C_OP_STOP); SysCtlDelay(1000); */ //////////// for RX Data ////////////// g_ui8MasterCurrState = I2C_OP_RXDATA; g_bI2CRepeatedStart = true; g_ui8MasterBytes = 0; // // Trigger the Transfer using Software Interrupt // IntTrigger(INT_I2C2); while(g_ui8MasterCurrState != I2C_OP_STOP); g_ui8MasterCurrState = I2C_OP_IDLE; data = (g_ui8MasterRxData[0] + (g_ui8MasterRxData[1]<<8)); // // Print Message before sending data // //UARTprintf("Receiving %d bytes from external Slave...\n\n",g_ui8MasterRxData[NUM_OF_I2CBYTES - 1]); // UARTprintf("Received--3: '%x'\n",g_ui8MasterRxData[g_ui8MasterBytes]); SysCtlDelay(12000); } } void initI2C(uint32_t ui32SysClock) { // // Enable GPIO for Configuring the I2C Interface Pins // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOL)); // // Configure Pins for I2C2 Master Interface // GPIOPinConfigure(GPIO_PL1_I2C2SCL); GPIOPinConfigure(GPIO_PL0_I2C2SDA); GPIOPinTypeI2C(GPIO_PORTL_BASE, GPIO_PIN_0); GPIOPinTypeI2CSCL(GPIO_PORTL_BASE, GPIO_PIN_1); // // Configure GPIO Pin PL4 for Interrupt Time Processing // GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_4); GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_4, 0x0); // // Stop the Clock, Reset and Enable I2C Module // in Master Function // SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C2); SysCtlPeripheralReset(SYSCTL_PERIPH_I2C2); SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2); // // Wait for the Peripheral to be ready for programming // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C2)); // // Initialize and Configure the Master Module // I2CMasterInitExpClk(I2C2_BASE, ui32SysClock, false); // false = 100khz , true = 400khz // // Enable Interrupts for Arbitration Lost, Stop, NAK, Clock Low // Timeout and Data. // I2CMasterIntEnableEx(I2C2_BASE, (I2C_MASTER_INT_STOP | I2C_MASTER_INT_NACK | I2C_MASTER_INT_DATA)); // // Enable the Interrupt in the NVIC from I2C Master // IntEnable(INT_I2C2); // // Enable the Glitch Filter. Writting a value 0 will // disable the glitch filter // I2C_MASTER_GLITCH_FILTER_DISABLED // I2C_MASTER_GLITCH_FILTER_1 // I2C_MASTER_GLITCH_FILTER_2 : Ideal Value when in HS Mode // for 120MHz clock // I2C_MASTER_GLITCH_FILTER_4 // I2C_MASTER_GLITCH_FILTER_8 : Ideal Value when in Std, // Fast, Fast+ for 120MHz clock // I2C_MASTER_GLITCH_FILTER_16 // I2C_MASTER_GLITCH_FILTER_32 // I2CMasterGlitchFilterConfigSet(I2C2_BASE, I2C_MASTER_GLITCH_FILTER_8); }