MPC5748G: SPI communication in S32DS

cancel
Showing results for 
Search instead for 
Did you mean: 

MPC5748G: SPI communication in S32DS

323 Views
schoi355
Contributor I

Hi, 

I use MPC5748 as a master and the accelerometer as a slave in SPI communication and debug using S32DS. I tested them with SPI example code within S32DS and the MPC5748 doesn't seem like receiving any data from the sensor. I also found that CS and CLK go high, but SOUT and SIN signals are not detected in the oscilloscope. I have no idea whether MPC5748 or the code causes the problem. The sensor worked well over SPI with Arduino. The following is the code that I used.

 

#include "derivative.h" /* include peripheral declarations */
#include "project.h"
#include "spi.h"
#include "mode.h"

#define KEY_VALUE1 0x5AF0ul
#define KEY_VALUE2 0xA50Ful

extern void xcptn_xmpl(void);
void peri_clock_gating (void); /* Configures gating/enabling peripheral(DSPI, SPI) clocks */

void hw_init(void)
{
#if defined(DEBUG_SECONDARY_CORES)
uint32_t mctl = MC_ME.MCTL.R;
#if defined(TURN_ON_CPU1)
/* enable core 1 in all modes */
MC_ME.CCTL[2].R = 0x00FE;
/* Set Start address for core 1: Will reset and start */
MC_ME.CADDR[2].R = 0x11d0000 | 0x1;
#endif
#if defined(TURN_ON_CPU2)
/* enable core 2 in all modes */
MC_ME.CCTL[3].R = 0x00FE;
/* Set Start address for core 2: Will reset and start */
MC_ME.CADDR[3].R = 0x13a0000 | 0x1;
#endif
MC_ME.MCTL.R = (mctl & 0xffff0000ul) | KEY_VALUE1;
MC_ME.MCTL.R = mctl; /* key value 2 always from MCTL */
#endif /* defined(DEBUG_SECONDARY_CORES) */
}

__attribute__ ((section(".text")))

/************************************ Main ***********************************/

int main(void)
{
unsigned int i = 0;

xcptn_xmpl (); /* Configure and Enable Interrupts */

peri_clock_gating(); /* Configures gating/enabling peripheral(DSPI, SPI) clocks for modes*/
/* Configuration occurs after mode transition */
system160mhz(); /* sysclk=160MHz, dividers configured, mode trans*/

init_SPI_0(); /* Initialize SPI_0 as Slave SPI and initialize CTAR0 */
init_spi_ports(); /* DSPI3 Master, SPI_0 Slave */

while( 1 )
{
SPI_0.PUSHR.PUSHR.R = 0x00001234; /* Initialize slave SPI_0's response to master */
read_data_SPI_0(); /* Read data on slave SPI */
i++;
}
return 0;
}

/************************ End of Main ***************************************/

void peri_clock_gating (void) { /* Configures gating/enabling peripheral(DSPI, SPI) clocks */

MC_ME.RUN_PC[0].R = 0x00000000; /* Gate off clock for all RUN modes */
MC_ME.RUN_PC[1].R = 0x000000FE; /* Configures peripheral clock for all RUN modes */
MC_ME.PCTL[43].B.RUN_CFG = 0x1; /* DSPI 2: select peripheral configuration RUN_PC[1] */
MC_ME.PCTL[96].B.RUN_CFG = 0x1; /* SPI 0: select peripheral configuration RUN_PC[1] */

-----------------------------------------------------------------------------------------------------------------------------------

 

spi.c

#include "project.h"
#include "spi.h"

unsigned int RecDataMaster;
unsigned int RecDataSlave;

/*****************************************************************************/
/* There are only 2 options available on the EVB
** the "x" signifies the channel used
**
** DSPI_3 MASTER
** CLK PG4x(J4_2) PH12
** SOUT PG2x(J4_1) PH11
** SIN PG5x(J4_4) PI11
** SS/CS0 PG3x(J4_3) PH13
**
** SPI_0 SLAVE
** CLK PF7x(J13_12) PG13 PH14 PI5 PK11
** SOUT PG12x(J14_3) PH13 PI4 PJ1 PK10
** SIN PG10 PI14x(J14_13) PK9
** SS/CS0 PG11 PH11 PI6 PI15x(J14_15) PK12
*/
/*****************************************************************************/

void init_spi_ports(){
/* Master - DSPI_3 */
SIUL2.MSCR[PG2].B.SSS = 2; /* Pad PG2: Source signal is DSPI_3 SOUT */
SIUL2.MSCR[PG2].B.OBE = 1; /* Pad PG2: OBE=1. */
SIUL2.MSCR[PG2].B.src=3; /* Pad PG2: Full strength slew rate */

SIUL2.MSCR[PG4].B.SSS = 2; /* Pad PG4: Source signal is DSPI_3 CLK */
SIUL2.MSCR[PG4].B.OBE = 1; /* Pad PG4: OBE=1. */
SIUL2.MSCR[PG4].B.src=3; /* Pad PG4: Full strength slew rate */

SIUL2.MSCR[PG5].B.IBE = 1; /* Pad PG5: Enable pad for input DSPI_3 SIN */
SIUL2.IMCR[297].B.SSS = 1; /* Pad PG5: connected to pad PG5 */

SIUL2.MSCR[PG3].B.SSS = 2; /* Pad PG3: Source signal is DSPI_3 CS0 */
SIUL2.MSCR[PG3].B.OBE = 1; /* Pad PG3: OBE=1. */
SIUL2.MSCR[PG3].B.src=3; /* Pad PG3: Full strength slew rate */

/* Slave - SPI_0 */
SIUL2.MSCR[PF7].B.SSS = 1; /* Pad PF7: Source signal is SPI_0 CLK */
SIUL2.MSCR[PF7].B.IBE = 1; /* Pad PF7: IBE=1. */
SIUL2.IMCR[301].B.SSS = 1; /* Pad PF7: SPI_0 CLK */

SIUL2.MSCR[PG12].B.SSS = 2; /* Pad PG12: Source signal is SPI_0 SOUT */
SIUL2.MSCR[PG12].B.OBE = 1; /* Pad PG12: OBE=1. */
SIUL2.MSCR[PG12].B.src=3; /* Pad PG12: Full strength slew rate */

SIUL2.MSCR[PI14].B.IBE = 1; /* Pad PI14: Enable pad for input SPI_0 SIN */
SIUL2.IMCR[300].B.SSS = 2; /* Pad PI14: connected to pad */

SIUL2.MSCR[PI15].B.IBE = 1; /* Pad PI15: IBE=1. SPI_0 SS */
SIUL2.IMCR[302].B.SSS = 4; /* Pad PI15: connected to pad */
}

void init_DSPI_3(void) {
DSPI_3.MCR.R = 0x80010001; /* Configure DSPI as master */
DSPI_3.MODE.CTAR[0].R = 0x78021004; /* Configure CTAR0 */
DSPI_3.MCR.B.HALT = 0x0; /* Exit HALT mode: go from STOPPED to RUNNING state*/
}

void read_data_DSPI_3(void) {
while (DSPI_3.SR.B.RFDF != 1){} /* Wait for Receive FIFO Drain Flag = 1 */
RecDataSlave = DSPI_3.POPR.R; /* Read data received by slave SPI */
DSPI_3.SR.R = 0xFCFE0000; /* Clear ALL status flags by writing 1 to them */
}

void init_SPI_0(void) {
SPI_0.MCR.R = 0x00010001; /* Configure DSPI_0 as slave */
SPI_0.MODE.CTAR[0].R = 0x78021004; /* Configure CTAR0 */
SPI_0.MCR.B.HALT = 0x0; /* Exit HALT mode: go from STOPPED to RUNNING state*/
}

void read_data_SPI_0(void) {
while (SPI_0.SR.B.RFDF != 1){} /* Wait for Receive FIFO Drain Flag = 1 */
RecDataMaster = SPI_0.POPR.R; /* Read data received by master SPI */
SPI_0.SR.R = 0xFCFE0000; /* Clear ALL status flags by writing 1 */
}

0 Kudos
0 Replies