Part Number:MSP430F5529
Tool/software: Code Composer Studio
Hi guy
Now I need to use two EXP_MSP5529LP development boards to verify I2C communication, one for master mode, the other for slave mode, using ccs example code. Now I can confirm that the master mode is OK (by reading the EEPROM IC ), the problem is that there is no data returned from the slave's module (seems can't in the interrupt), I really don't know where the problem is?
slave master
P3.2 ----------------------P3.1
P3.1 ----------------------P3.0
There are two 4.7k pull up resistance.
slave mode code
#define SLAVE_ADDR 0x48 /* CMD_TYPE_X_SLAVE are example commands the master sends to the slave. * The slave will send example SlaveTypeX buffers in response. * * CMD_TYPE_X_MASTER are example commands the master sends to the slave. * The slave will initialize itself to receive MasterTypeX example buffers. * */ #define CMD_TYPE_0_SLAVE 0 #define CMD_TYPE_1_SLAVE 1 #define CMD_TYPE_2_SLAVE 2 #define CMD_TYPE_0_MASTER 3 #define CMD_TYPE_1_MASTER 4 #define CMD_TYPE_2_MASTER 5 #define TYPE_0_LENGTH 1 #define TYPE_1_LENGTH 2 #define TYPE_2_LENGTH 6 #define MAX_BUFFER_SIZE 20 /* MasterTypeX are example buffers initialized in the master, they will be * sent by the master to the slave. * SlaveTypeX are example buffers initialized in the slave, they will be * sent by the slave to the master. * */ uint8_t MasterType2 [TYPE_2_LENGTH] = {0}; uint8_t MasterType1 [TYPE_1_LENGTH] = { 0, 0}; uint8_t MasterType0 [TYPE_0_LENGTH] = { 0}; uint8_t SlaveType2 [TYPE_2_LENGTH] = {'A', 'B', 'C', 'D', '1', '2'}; uint8_t SlaveType1 [TYPE_1_LENGTH] = {15, 16}; uint8_t SlaveType0 [TYPE_0_LENGTH] = {12}; //****************************************************************************** // General I2C State Machine *************************************************** //****************************************************************************** typedef enum I2C_ModeEnum{ IDLE_MODE, NACK_MODE, TX_REG_ADDRESS_MODE, RX_REG_ADDRESS_MODE, TX_DATA_MODE, RX_DATA_MODE, SWITCH_TO_RX_MODE, SWITHC_TO_TX_MODE, TIMEOUT_MODE } I2C_Mode; /* Used to track the state of the software state machine*/ I2C_Mode SlaveMode = RX_REG_ADDRESS_MODE; /* The Register Address/Command to use*/ uint8_t ReceiveRegAddr = 0; /* ReceiveBuffer: Buffer used to receive data in the ISR * RXByteCtr: Number of bytes left to receive * ReceiveIndex: The index of the next byte to be received in ReceiveBuffer * TransmitBuffer: Buffer used to transmit data in the ISR * TXByteCtr: Number of bytes left to transfer * TransmitIndex: The index of the next byte to be transmitted in TransmitBuffer * */ uint8_t ReceiveBuffer[MAX_BUFFER_SIZE] = {0}; uint8_t RXByteCtr = 0; uint8_t ReceiveIndex = 0; uint8_t TransmitBuffer[MAX_BUFFER_SIZE] = {0}; uint8_t TXByteCtr = 0; uint8_t TransmitIndex = 0; /* Initialized the software state machine according to the received cmd * * cmd: The command/register address received * */ void I2C_Slave_ProcessCMD(uint8_t cmd); /* The transaction between the slave and master is completed. Uses cmd * to do post transaction operations. (Place data from ReceiveBuffer * to the corresponding buffer based in the last received cmd) * * cmd: The command/register address corresponding to the completed * transaction */ void I2C_Slave_TransactionDone(uint8_t cmd); void CopyArray(uint8_t *source, uint8_t *dest, uint8_t count); void I2C_Slave_ProcessCMD(uint8_t cmd) { ReceiveIndex = 0; TransmitIndex = 0; RXByteCtr = 0; TXByteCtr = 0; switch (cmd) { case (CMD_TYPE_0_SLAVE): //Send slave device id (This device's id) SlaveMode = TX_DATA_MODE; TXByteCtr = TYPE_0_LENGTH; //Fill out the TransmitBuffer CopyArray(SlaveType0, TransmitBuffer, TYPE_0_LENGTH); UCB0IE &= ~UCRXIE; // Disable RX interrupt UCB0IE |= UCTXIE; // Enable TX interrupt break; case (CMD_TYPE_1_SLAVE): //Send slave device time (This device's time) SlaveMode = TX_DATA_MODE; TXByteCtr = TYPE_1_LENGTH; //Fill out the TransmitBuffer CopyArray(SlaveType1, TransmitBuffer, TYPE_1_LENGTH); UCB0IE &= ~UCRXIE; // Disable RX interrupt UCB0IE |= UCTXIE; // Enable TX interrupt break; case (CMD_TYPE_2_SLAVE): //Send slave device location (This device's location) SlaveMode = TX_DATA_MODE; TXByteCtr = TYPE_2_LENGTH; //Fill out the TransmitBuffer CopyArray(SlaveType2, TransmitBuffer, TYPE_2_LENGTH); UCB0IE &= ~UCRXIE; // Disable RX interrupt UCB0IE |= UCTXIE; // Enable TX interrupt break; case (CMD_TYPE_0_MASTER): SlaveMode = RX_DATA_MODE; RXByteCtr = TYPE_0_LENGTH; UCB0IE &= ~UCTXIE; // Disable RX interrupt UCB0IE |= UCRXIE; // Enable TX interrupt break; case (CMD_TYPE_1_MASTER): SlaveMode = RX_DATA_MODE; RXByteCtr = TYPE_1_LENGTH; UCB0IE &= ~UCTXIE; // Disable RX interrupt UCB0IE |= UCRXIE; // Enable TX interrupt break; case (CMD_TYPE_2_MASTER): SlaveMode = RX_DATA_MODE; RXByteCtr = TYPE_2_LENGTH; UCB0IE &= ~UCTXIE; // Disable RX interrupt UCB0IE |= UCRXIE; // Enable TX interrupt break; default: __no_operation(); break; } } void I2C_Slave_TransactionDone(uint8_t cmd) { switch (cmd) { case (CMD_TYPE_0_SLAVE): //Slave device id was sent(This device's id) break; case (CMD_TYPE_1_SLAVE): //Slave device time was sent(This device's time) break; case (CMD_TYPE_2_SLAVE): //Send slave device location (This device's location) break; case (CMD_TYPE_0_MASTER): CopyArray(ReceiveBuffer, MasterType0, TYPE_0_LENGTH); break; case (CMD_TYPE_1_MASTER): CopyArray(ReceiveBuffer, MasterType1, TYPE_1_LENGTH); break; case (CMD_TYPE_2_MASTER): CopyArray(ReceiveBuffer, MasterType2, TYPE_2_LENGTH); break; default: __no_operation(); break; } } void CopyArray(uint8_t *source, uint8_t *dest, uint8_t count) { uint8_t copyIndex = 0; for (copyIndex = 0; copyIndex < count; copyIndex++) { dest[copyIndex] = source[copyIndex]; } } //****************************************************************************** // Device Initialization ******************************************************* //****************************************************************************** void initClockTo16MHz() { UCSCTL3 |= SELREF_2; // Set DCO FLL reference = REFO UCSCTL4 |= SELA_2; // Set ACLK = REFO __bis_SR_register(SCG0); // Disable the FLL control loop UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation UCSCTL2 = FLLD_0 + 487; // Set DCO Multiplier for 16MHz // (N + 1) * FLLRef = Fdco // (487 + 1) * 32768 = 16MHz // Set FLL Div = fDCOCLK __bic_SR_register(SCG0); // Enable the FLL control loop // Worst-case settling time for the DCO when the DCO range bits have been // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx // UG for optimization. // 32 x 32 x 16 MHz / 32,768 Hz = 500000 = MCLK cycles for DCO to settle __delay_cycles(500000);// // Loop until XT1,XT2 & DCO fault flag is cleared do { UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags SFRIFG1 &= ~OFIFG; // Clear fault flags }while (SFRIFG1&OFIFG); // Test oscillator fault flag } void initGPIO() { //LEDs P1OUT = 0x00; // P1 setup for LED & reset output P1DIR |= BIT0; P4DIR |= BIT7; P4OUT &= ~(BIT7); //I2C Pins P3SEL |= BIT1 + BIT2; // P3.1,2 option select } void initI2C() { UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0I2COA = SLAVE_ADDR; // Own Address UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation UCB0IE |= UCRXIE; } void SetVcoreUp (unsigned int level) { // Open PMM registers for write PMMCTL0_H = PMMPW_H; // Set SVS/SVM high side new level SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level; // Set SVM low side to new level SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level; // Wait till SVM is settled while ((PMMIFG & SVSMLDLYIFG) == 0); // Clear already set flags PMMIFG &= ~(SVMLVLRIFG + SVMLIFG); // Set VCore to new level PMMCTL0_L = PMMCOREV0 * level; // Wait till new level reached if ((PMMIFG & SVMLIFG)) while ((PMMIFG & SVMLVLRIFG) == 0); // Set SVS/SVM low side to new level SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level; // Lock PMM registers for write access PMMCTL0_H = 0x00; } //****************************************************************************** // Main ************************************************************************ // Enters LPM0 and waits for I2C interrupts. The data sent from the master is * // then interpreted and the device will respond accordingly * //****************************************************************************** int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer SetVcoreUp (0x01); SetVcoreUp (0x02); SetVcoreUp (0x03); initClockTo16MHz(); initGPIO(); initI2C(); __enable_interrupt(); while(1){ P4OUT ^= BIT7; __delay_cycles(10000000000/625); } return 0; } //****************************************************************************** // I2C Interrupt *************************************************************** //****************************************************************************** #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector=USCI_B0_VECTOR __interrupt void USCI_B0_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCI_B0_ISR (void) #else #error Compiler not supported! #endif { //Must read from UCB0RXBUF uint8_t rx_val = 0; switch(__even_in_range(UCB0IV,0xC)) { case USCI_NONE:break; // Vector 0 - no interrupt case USCI_I2C_UCALIFG:break; // Interrupt Vector: I2C Mode: UCALIFG case USCI_I2C_UCNACKIFG:break; // Interrupt Vector: I2C Mode: UCNACKIFG case USCI_I2C_UCSTTIFG:break; // Interrupt Vector: I2C Mode: UCSTTIFG case USCI_I2C_UCSTPIFG:break; // Interrupt Vector: I2C Mode: UCSTPIFG case USCI_I2C_UCRXIFG: rx_val = UCB0RXBUF; switch (SlaveMode) { case (RX_REG_ADDRESS_MODE): ReceiveRegAddr = rx_val; I2C_Slave_ProcessCMD(ReceiveRegAddr); break; case (RX_DATA_MODE): ReceiveBuffer[ReceiveIndex++] = rx_val; RXByteCtr--; if (RXByteCtr == 0) { //Done Receiving MSG SlaveMode = RX_REG_ADDRESS_MODE; UCB0IE &= ~(UCTXIE); UCB0IE |= UCRXIE; // Enable RX interrupt I2C_Slave_TransactionDone(ReceiveRegAddr); } break; default: __no_operation(); break; } break; // Interrupt Vector: I2C Mode: UCRXIFG case USCI_I2C_UCTXIFG: switch (SlaveMode) { case (TX_DATA_MODE): UCB0TXBUF = TransmitBuffer[TransmitIndex++]; TXByteCtr--; if (TXByteCtr == 0) { //Done Transmitting MSG SlaveMode = RX_REG_ADDRESS_MODE; UCB0IE &= ~(UCTXIE); UCB0IE |= UCRXIE; // Enable RX interrupt I2C_Slave_TransactionDone(ReceiveRegAddr); } break; default: __no_operation(); break; } break; // Interrupt Vector: I2C Mode: UCTXIFG default: break; } }