This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553: MSP430G2955: MSP430 interfacing MPU9250(sparkfun) via SPI

Part Number: MSP430G2553

Hello guys,

i have written small code where i can communicate with the MPU9250 of sparkfun, my pin connection are followiing

MSP4302553                                           mpu9250 sparkfun board

VCC---------------------------------------------VDD

P1.1 (UCA0SOMI/)--------------------------SDO

P1.2 (UCA0SIMO/)--------------------------SDA

P1.4 (UCA0CLK/)---------------------------SCL

P1.5---------------------------------------------nCS

and below is my code for just having a first communication with the MPU9250 by asking his recognization:

#include <msp430g2553.h>
#include <mpu9250.h>
#define READ                 0x80
#define I2C_IF_DIS           0x10
#define USER_CTRL            0x6A  // Bit 7 enable DMP, bit 3 reset DMP
#define WHO_AM_I             0x75 // Should return 0x71
/*
 * main.c
 */
char connect;
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer

    P2DIR |=BIT2;
    SPI_init();
    while(1){
    SpiMpuTxWrByte(USER_CTRL,I2C_IF_DIS);               //Disable I2C Slave module and put the serial interface in SPI mode only
    // Read the WHO_AM_I register,test of communication
    SpiMpuRxReByte(WHO_AM_I,&connect,1);
    if(connect==0x71){
        P2OUT&=~BIT2;
    }
    else  P2OUT|=BIT2;
    }
    return 0;
}

/*
 * mpu9250.c
 *
 *  Created on: Jun 30, 2017
 *      Author: tarik  H
 */
#include <msp430g2553.h>

#include <mpu9250.h>


void SPI_init(void){

       //UCA0CTL1 |= UCSWRST;    // enable software reset ,USCI held in reset state
       P1SEL |= BIT1|BIT2|BIT4;          //P3.0 UCA0CLK select,P3.4 UCA0SIMO select
       P1SEL2 |= BIT1|BIT2|BIT4;       //~(BIT0|BIT4)
       UCA0CTL0 |= UCCKPH|UCMSB|UCMST|UCSYNC; //UCCKPH=(ph=1;po=00, 3-pin, MSB first, synchronous mode, MAster mode, 8-bit
       UCA0CTL1 |= UCSSEL_2;                  //BRCLK Source Clock,USCI 0 Clock Source: 2,f=f(BRCLK)/UCBRx, sub main clock SMCLK
       UCA0BR0 |= 0x02;                       //set clock prescaler to 2
       UCA0BR1 = 0x00;                        //(UCAxBR0 + UCAxBR1 × 256)=UCBRx forms the prescaler value
       UCA0MCTL = 0x00;                       // Modulation disabled
       UCA0CTL1 &=~UCSWRST;                   // USCI reset released for operation
       __delay_cycles(99000);                 //52500 of f=16Mhz or 2.1875ms
   }


void SpiMpuTxWrByte(char WriteAddress,char WriteData){
    P1OUT |=BIT5;
    P1OUT &=~BIT5;
    // write the address of the register
    while(UCB0STAT & UCBUSY);
    while(!(IFG2 & UCA0TXIFG));   //TX buffer is ready and has transmitted the data,check if the data written in UC0TXIFG is set and IFG2(its initial state is 0x1001110)
    UCA0TXBUF=WriteAddress;            //write address
    // write the data to the register
    while(UCB0STAT & UCBUSY);
    while(!(IFG2 & UCA0TXIFG));   //TX buffer is ready and has transmitted the data,check if the data written in UC0TXIFG is set and IFG2(its initial state is 0x1001110)
    UCA0TXBUF=WriteData;
    //P1OUT |=BIT5;
}


void SpiMpuRxReByte(char ReadAddress,char* ReadBuff, char ReadSize){
    P1OUT |=BIT5;
    P1OUT &=~BIT5;
    // register address to read
    while(UCB0STAT & UCBUSY);
    while(!(IFG2 & UCA0TXIFG));   //TX buffer is ready and has transmitted the data,
    UCA0TXBUF=ReadAddress|READ;            //read address
    // data to read
    while(ReadSize){
        while(UCB0STAT & UCBUSY);
        while(!(IFG2 & UCA0TXIFG));   //TX buffer is ready and has transmitted the data
        UCA0TXBUF=0x00;               // to rea data we need to send 0x00 to data register
        while (!(IFG2 & UCA0RXIFG)); // wait for transmission complete
        *ReadBuff = UCA0RXBUF;
        ReadBuff++;
        ReadSize--;
    }
    //P1OUT |=BIT5;

}

My question is that, whenever i disconnect the the SDO from pin P1.1  when running the code step by step, i find that the UCA0RXBUF is filled whith the value of 0XFF which it suppose to be empty, so how can the UCA0 receives 0xFF while it is completely disconnected fro the MPU9250, 
i have noticedd also the bit UCA0RXIFG is set to 1 whenver i send the data which is supposed to be set when i receive the data.
please any clarificaion is welcome

  • It is the nature of SPI that when the master (MSP430) transmits a byte, it also receives a byte. It doesn't matter if you have anything connected to the MISO pin as what counts is that the clock is running.

    The SparkFun MPU9250 board comes configured for I2C and requires that you change a jumper so that SPI works. Have you done that?

    I have worked with the MPU9250 and MSP430 but all of my code is in Forth which isn't likely to help you.

  •  Hello Mr.David,

    thank you for your reply, as from what i see from the MPU9250 sparkfun, there is

  • sorry, i did not finish what i want to say, in the picture above i didn't find any jumper that i can use for the SPI configuration
  • If you look at the schematic you will find a solder jumper for AD0/SD0. It is normally configured to connect that pin to ground. You have to change that solder jumper to remove that short and route the signal to the pin headers.
  • Hey David,
    thank you very much for your help , i will do it now and let you know of my progress.
  • Hello Davi, your hint was very helpfull for me and i reveive the 0x71 value from the MPU9250. as you have worked on MPU9250, i would like if can give me some hints how to program because in the datasheet they don't give any information about the procedure of initialiation and configuration.
    thank you again
  • It depends entirely on what you want it to do. I have only configured the sensors directly but you can also initialize its motion processing which requires uploading code. If you want to do that just search around and you can find code for it. Once you can read and write registers the rest is pretty portable.

  • Hi david,
    yes i want to use to detect any motion while in the initial position the sensor must be on table, so i think i just need the raw data of it.
    i have just another issue reading the who_am_i, i send the byte and i receive 0x71 wich is correct,neverheless the variable connect does not change its value and stay the same as 0xFF and it must be 0x71 which in the code goes all the time to the instruction else, here is the code above
  • Your receive code ignores the fact that after transmitting the read command and register, you receive a byte. So when you enter the loop to read the registers RXIFG is already set. Which means that the value stored is whatever was sent while the MPU9250 was receiving the command byte. For general purpose use I typically use a subroutine that transmits a byte and returns a byte. For example:

    unsigned int RW_SPIA(int c)
    {
      UCA0TXBUF = c;
      while(UCA0STATW & UCBUSY)
        ;
      return UCA0RXBUF;
    }
    

  • Hi david,thank you very much, i have solved my problem straighforward, you are completely right,thank you again

**Attention** This is a public forum