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.

CCS/MSP-EXP432P4111: CCS/MSP-EXP432P4111 SPI Issue

Part Number: MSP-EXP432P4111

Tool/software: Code Composer Studio

I am trying to use EUSCI_B0 in SPI mode to talk to a very old SPI sensor. I am using 4 pin mode as the sensor needs CS to go low 250 uSec before communications. Looking at a logic analyzer trace I see the CS line (P1.4) going low about 30 uSec before CLK (P1.5) starts toggling. MOSI (P1.6) is sending data properly however MISO (P1.7) is receiving improper data. Is there a way to delay the start of P1.5-P1.7 toggling a certain period of time after CS becomes active?

I have used the example code from the E:\ti\simplelink_msp432p4_sdk_2_20_00_12\examples\nortos\MSP_EXP432P401R\registerLevel\msp432p401x_euscib0_spi_11 directory as a starting point.

Thanks.

  • I suggest you use 3-wire mode and wiggle CS (as a GPIO) yourself. That gives you full control over the delay.

    Curiously enough, I don't see any examples for this mode, even though it's what I use for pretty much every SPI device ever.

  • I dug this out of my CCS archive. It talks to an ST LIS3DH via 3-wire+GPIO-CS. It doesn't do much, but it's small.

    No warranty, and all that.

    ///
    //      lis3dh-432.c
    //      Poke at LIS3DH on Adafruit breakout board
    //
    #include "msp.h"
    #include <stdint.h>
    #define SPIU    EUSCI_B0
    #define HZ      3000000UL       // more or less
    #define SPI_HZ  1000000UL       // 1MHz clock
    
    //  LIS3DH registers
    #define WHO_AM_I    0x0F
    #define CTRL_REG1   0x20
    volatile uint8_t rreg;          // Register 0x20 should read as 0x06 since that's what we set
    volatile uint8_t who_am_i;      // Register 0x0F should be 0x33 for the LIS3DH
    
    #define CS_LO() do {P3->OUT &= ~BIT0;} while (0)
    #define CS_HI() do {P3->OUT |=  BIT0;} while (0)
    
    ///
    //  spi_init()
    //  Set UCB0 to SPI, Master, 1MHz, mode=(1,1), 3-wire
    //
    void
    spi_init(void)
    {
        SPIU->CTLW0 = EUSCI_B_CTLW0_SWRST|
                      EUSCI_B_CTLW0_SSEL__SMCLK|EUSCI_B_CTLW0_SYNC|
                      EUSCI_B_CTLW0_MODE_0|EUSCI_B_CTLW0_MST|EUSCI_B_CTLW0_MSB|
                      (1*EUSCI_B_CTLW0_CKPL)|(0*EUSCI_B_CTLW0_CKPH); // mode=(1,1)
        SPIU->BRW = HZ/SPI_HZ;          // SPI clock rate
        SPIU->CTLW0 &= ~EUSCI_B_CTLW0_SWRST; // Out of reset
        P1->SEL0 |=  (BIT5|BIT6|BIT7);  // UCB0 per SLAS826G Table 6-62
        P1->SEL1 &= ~(BIT5|BIT6|BIT7);
        P3->OUT |= BIT0;                // P3.0 as /CS (but not STE)
        P3->DIR |= BIT0;
        return;
    }
    
    ///
    //  spix()
    //  Exchange one byte
    //
    uint8_t
    spix(uint8_t b)
    {
        while (!(SPIU->IFG & EUSCI_B_IFG_TXIFG)) /*EMPTY*/;
        SPIU->TXBUF = b;
        while (!(SPIU->IFG & EUSCI_B_IFG_RXIFG)) /*EMPTY*/;
        b = SPIU->RXBUF;
        return(b);
    }
    
    ///
    //  lis_wrr()
    //  Write LIS3DH register
    //
    void
    lis_wrr(uint8_t reg, uint8_t val)
    {
        CS_LO();
        (void)spix(0x00 | reg); // W bit
        (void)spix(val);
        CS_HI();
        return;
    }
    
    ///
    //  lis_rdr()
    //  Read LIS3DH register
    //
    uint8_t
    lis_rdr(uint8_t reg)
    {
        uint8_t val;
        CS_LO();
        (void)spix(0x80 | reg); // R bit (no MS, I suppose)
        val = spix(0x55);
        CS_HI();
        return(val);
    }
    
    ///
    //  main()
    //  Write LIS3DH CTRL_REG1 and read it back
    //
    void
    main(void)
    {
    	WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;		// stop watchdog timer
    	P1->OUT &= ~BIT0;               // Launchpad LED P1.0
    	P1->DIR |=  BIT0;
    	spi_init();
    	who_am_i = lis_rdr(WHO_AM_I);   // Register WHO_AM_I (0x0F) should read as 0x33
    	while (1)
    	{
    	    P1->OUT ^= BIT0;
    	    lis_wrr(CTRL_REG1, 0x06);   // CTRL_REG1 (0x20): Disable Xen
    	    rreg = lis_rdr(CTRL_REG1);  // Read it back
    	    __delay_cycles(HZ/3);       // 1/3 second
    	}
    }
    

  • Bruce,

    I greatly appreciate the code you sent and I will try (modify) it.

    I am very new to CCS and C so everything helps.

    Thanks a lot!

    Guy

  • Bruce,

    Your sample code worked great! I modified it and can now talk to my sensor.

    Thanks again.

**Attention** This is a public forum