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;
}