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/MSP432P401R: msp432

Part Number: MSP432P401R

Tool/software: Code Composer Studio

I am writing a SPI driver to communicate with Accelerometer, I have written the code given below. The problem that I am facing is:
a) I am not able to locate all P1.4 - P1.7 on my MSP432 board
b) Can anybody check my code and point out any error in my configuration of SPI(specially bitrate) and read and write byte function. 
c) I am confused regarding the use of STE, since STE is nothing but a Chip select signal, cant we directly use a port pin(GPIO) to directly make it low or high as per requirement rather than setting one of the port pin as STE
Code:

/**
* @file spi.c
* @brief This file is to be used for the setting the SPI peripheral of the microcontroller.
* @author Vikrant Waje
* @date November 20, 2018
*
*/
//***********************************************************************************
// Include files
//***********************************************************************************
#include"main.h"


//***********************************************************************************
// Global variables
//***********************************************************************************


//***********************************************************************************
// Function implementation
//***********************************************************************************
/*------------------------------------------------------------------------------------------------------------------------------------*/
/*
@brief: Setup the SPI peripheral.


@param: None
@param:None

@return: None
*/
/*-----------------------------------------------------------------------------------------------------------------------------*/
void spi_setup(){
spi_gpio_setup();
spi_set_parameters();
spi_enable();
}
/*------------------------------------------------------------------------------------------------------------------------------------*/
/*
@brief: Assign GPIO to SPI peripherals.

SOMI = P1.7 (UCSI B)(SEL1 =0 , SEL0 = 1 )
SIMO = P1.6 (UCSI B)(SEL1 =0 , SEL0 = 1 )
CLK = P1.5 (UCSI B)(SEL1 =0 , SEL0 = 1 )
STE = P1.4 (UCSI B)(SEL1 =0 , SEL0 = 1 )
@param: None
@param:None

@return: None
*/
/*-----------------------------------------------------------------------------------------------------------------------------*/
void spi_gpio_setup(){
P1->SEL0 |=(BIT4 | BIT5 | BIT6 | BIT7);
}

/*------------------------------------------------------------------------------------------------------------------------------------*/
/*
@brief: Set the parameters for SPI peripheral.


@param: None
@param:None

@return: None
*/
/*-----------------------------------------------------------------------------------------------------------------------------*/
void spi_set_parameters(){
//Enable Reset, MSB first, 7 bit mode, master, SPI mode with STE =0, Sync bit, SMCLK as source clock and STE as slave select
EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_SWRST | EUSCI_B_CTLW0_MSB | EUSCI_B_CTLW0_SEVENBIT | EUSCI_B_CTLW0_MST | EUSCI_B_CTLW0_MODE_2 | \
EUSCI_B_CTLW0_SYNC | EUSCI_B_CTLW0_SSEL__SMCLK | EUSCI_B_CTLW0_STEM;
EUSCI_B0->BRW = 120;
EUSCI_B0->CTLW0 &= ~(EUSCI_B_CTLW0_SWRST); //Come out of Reset
}

/*------------------------------------------------------------------------------------------------------------------------------------*/
/*
@brief: Write byte for SPI peripheral.


@param: None
@param:None

@return: None
*/
/*-----------------------------------------------------------------------------------------------------------------------------*/
void spi_write_byte(uint8_t reg_addr, uint8_t data_byte){
P1->OUT &=~(BIT4); //Make CS low
EUSCI_B0->TXBUF = (0<<7) & (reg_addr); //Send the register address with write bit at MSb position(write bit = 0)
while(!(EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG ); //Wait for Byte to get transmitted
EUSCI_B0->TXBUF = data_byte;
while(!(EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG ); //Wait for Byte to get transmitted
EUSCI_B2->IFG &= ~(EUSCI_B_IFG_TXIFG0); //Clear the flag
P1->OUT |=(BIT4); //Make CS high
}

/*------------------------------------------------------------------------------------------------------------------------------------*/
/*
@brief: Read byte for SPI peripheral.


@param: reg_addr: Register address from which data is to be read
@param:None

@return: uint8_t : 8 bit data that is read from register
*/
/*-----------------------------------------------------------------------------------------------------------------------------*/
uint8_t spi_read_byte(uint8_t reg_addr){
uint8_t data_byte;
P1->OUT &=~(BIT4); //Make CS low
EUSCI_B0->TXBUF = (0<<7) & (reg_addr); //Send the register address with write bit at MSb position(write bit = 0)
while(!(EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG ); //Wait for Byte to get transmitted
EUSCI_B2->IFG &= ~(EUSCI_B_IFG_TXIFG0); //Clear the flag
while(!(EUSCI_B0->IFG & EUSCI_B_IFG_RXIFG ); //Wait for Byte to get transmitted
data_byte = EUSCI_B0->RXBUF; // Read data from RXBUF
P1->OUT |=(BIT4); //Make CS high
return data_byte;
}

  • Hi Vikrant,

    to your questions:
    a) I am not able to locate all P1.4 - P1.7 on my MSP432 board => not sure what you are asking for here - are you trying to find the pins on the launchpad PCB? Note: P1_4 is not easy to connect as it is on Button 2 - details on that can be found in the Launchpad Users Guide slau597f.pdf
    b) Can anybody check my code and point out any error in my configuration of SPI(specially bitrate) and read and write byte function.
    -> sorry can not fully check your code but there is syntax you should check you have used on some places e.g.
    EUSCI_B0->TXBUF = (0<<7) & (resPos);
    -> you shift 0 seven times to left which is still 0 and the you and this with something -> the result is still 0 -> so you transmit 0
    c) I am confused regarding the use of STE, since STE is nothing but a Chip select signal, cant we directly use a port pin(GPIO) to directly make it low
    or high as per requirement rather than setting one of the port pin as STE
    as you can see in the MSP432 Users Guide section of the SPI - Master mode - Figure eUSCI Master and External Slave (UCSTEM = 0) : the output on the Master side for the STE signal is a port pin. STE as function for the SPI in the MSP430 can only be used in Slave mode.

    Regards,
    Stefan
  • So since I will be using master mode, what I understand from your post is that I only require to configure UCSIMO,UCSOMI AND UCCLK IN PxSEL register. The Chip select signal can be any general purpose Gpio which I can manipulate based on requirement. Is that correct?

  • yes, this is correct.

    ~ Stefan
  • Another small thing that I am worried about is whether the UC7BIT in UCCTLW0 register is to be set or not. My sensor needs 7 bit addressing while the length of each data packet is 8 bit. I have kept UC7BIT as 1 assuming it means I am using address of 7 bit and not the data.
    also, do I need to send the read and write bit manually or will the USCI module take care of it somehow?
  • SPI does not work like I²C. Whenever you write to TXBUF, the master will generate 8 (or 7) clock cycles, and on each cycle, transmit one bit on MOSI, and read one bit on MISO. Everything is writing and reading at the same time. And that's it as far as the SPI protocol (and the USCI module) is concerned.

    That R/W bit is some protocol that your sensor implements on top of SPI. The first byte contains 8 bits, so you have to tell the USCI module to transmit 8 bits.
  • Hi Vikrant,

    do you have further questions regarding this topic? If not, please select "Resolved" for the post that solved your issue so this thread can be closed out. If you have a different question, please select "Ask a related question" or " Ask a new question".
    Thanks a lot!

    Best regards,
    Stefan
  • resolved my issue

**Attention** This is a public forum