Problem: Raspberry Pi to NXP MPC5744p over SPI

0

I am currently trying to get SPI data transfer between the MPC5744p and a Raspberry Pi 4 to work.

I'm using the SPIdev python toolkit to send the value 26 over to the MPC, using the following SPItest.py script.

import spidev
import time

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 7629

#test_list = [1,2,3]

test_list=[26]
while True:
    spi.writebytes(test_list)

#time.sleep(0.5)
#spi.readbytes(1)

To receive this, i've modified the code of the SPI_MPC5744P example to only expect data arriving on the slave DSPI2.

This is the main.c file on the NXP MPC5744P: int main(void)

{
    unsigned int i = 0;

    uint8_t intro[] = {"\n\rWelcome to the XDEVKIT-MPC5744P SPI transfer code example!\n\r"};
    uint8_t Before_Message[] = {"\n\rSPI has not yet transferred. SPI slave (SPI_2) sends 0x00001234 and "
            "SPI master (SPI_1) sends 0x00005678\n\r"};
    uint8_t After_Message[] = {"\n\rSPI transfer has happened.\n\r"};
    uint8_t Master_Message[] = {"\n\rRecDataMaster is 0x"};
    uint8_t Slave_Message[] = {"\n\rRecDataSlave is 0x"};
    uint8_t newline[] = {".\n\r"};
    uint8_t PrintBuffer[8] = {0};
    uint8_t Continue_Message[] = {"\n\rPress any key to continue.\n\r"};
    uint8_t dummychar = 0;

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

    smpu_config();              /* Configure memory and cache settings for memory regions */

    peri_clock_gating();                      /* Config gating/enabling peri. clocks for modes*/
                                                /* Configuraiton occurs after mode transition */
    system160mhz();                           /* sysclk=160MHz, dividers configured, mode trans*/

    LINFlexD_1_Init();                      /* Initialize LINFlexD_1 to print UART messages */

    init_dspi_ports();                         // DSPI1 Master, DSPI_2 Slave
    init_dspi_1();                             // DSPI_1 als master SPI
    init_dspi_2();                             // DSPI_2 als Slave SPI
                                            //Zie spi.c

    /* Print initial UART messages */
    TransmitData((const char*)intro,strlen((const char*)intro));
    TransmitData((const char*)Before_Message,strlen((const char*)Before_Message));
    TransmitData((const char*)Master_Message,strlen((const char*)Master_Message));
    HexToASCII_Word(RecDataMaster,PrintBuffer); //Convert RecDataMaster to ASCII
    TransmitData((const char*)PrintBuffer,8);
    TransmitData((const char*)newline,strlen((const char*)newline));
    TransmitData((const char*)Slave_Message,strlen((const char*)Slave_Message));
    HexToASCII_Word(RecDataSlave,PrintBuffer); //Convert RecDataSlave to ASCII
    TransmitData((const char*)PrintBuffer,8);
    TransmitData((const char*)newline,strlen((const char*)newline));
    TransmitData((const char*)Continue_Message,strlen((const char*)Continue_Message));
    ReceiveData((char*)&dummychar);

    TransmitData((const char*)After_Message,strlen((const char*)After_Message));

    //Eens uart ontvangen wordt vanuit putty, wordt  DSPI2 (slave) gecheckt voor input

    while( 1 )
    {
      SPI_2.PUSHR.PUSHR.R = 0x00001234;       /* Initialize slave DSPI_2's response to master */
      //SPI_1.PUSHR.PUSHR.R  = 0x08015678;     /* Transmit data from master to slave SPI with EOQ */

      //Deze data met EOQ heb ik vervangen door de SPI-date vanuit raspberry pi. EOQ zorgt ervoor dat het register gepushed wordt.

      read_data_DSPI_2();                      // Lees data op slave DSPI */
      //read_data_DSPI_1();

      TransmitData((const char*)Master_Message,strlen((const char*)Master_Message));
      HexToASCII_Word(RecDataMaster,PrintBuffer); //Convert RecDataMaster to ASCII
      TransmitData((const char*)PrintBuffer,8);
      TransmitData((const char*)newline,strlen((const char*)newline));
      TransmitData((const char*)Slave_Message,strlen((const char*)Slave_Message));
      HexToASCII_Word(RecDataSlave,PrintBuffer); //Convert RecDataSlave to ASCII
      TransmitData((const char*)PrintBuffer,8);
      TransmitData((const char*)newline,strlen((const char*)newline));
      TransmitData((const char*)Continue_Message,strlen((const char*)Continue_Message));
      ReceiveData((char*)&dummychar);

      i++;
    }
    return 0;
}

/****************************************************************************/
/* HexToASCII_Word                                                          */
/* Description: Converts an unsigned int to an array of 8 ASCII characters  */
/****************************************************************************/
void HexToASCII_Word(unsigned int input,uint8_t* buffer){
    uint8_t i = 0;
    unsigned int number = input;
    unsigned int temp = 0;

    for(i=0;i<8;i++){
        temp = number & 0x0000000F; //Mask least significant nibble of word
        /* Convert to ASCII. Store in buffer backwards because LSB gets stored first */
        if(temp > 9){
            *(buffer+8-(i+1)) = temp + '0' + 7; //'A' does not come immediately after '9' in ASCII table. Additional offset
        }else{
            *(buffer+8-(i+1)) = temp + '0'; //Otherwise store '0' to '9'
        }

        number = number >> 4; //Get next nibble
    }
}

The SPI settings were configured as follows:

void init_dspi_ports()
{
    //Bevestigt welke pins als input gezien worden, waar de clock vandaan komt en waar data heen gepushed wordt
    //Info over MSCR en IMCR: p499 ref man.
  // Master - DSPI_1
  SIUL2.MSCR[PA7].B.SSS = 1;                 /* Pad PA7: Source signal is DSPI_1 SOUT  */
  SIUL2.MSCR[PA7].B.OBE = 1;                 /* Pad PA7: OBE=1. */
  SIUL2.MSCR[PA7].B.SRC = 3;                 /* Pad PA7: Full strength slew rate */

  SIUL2.MSCR[PA6].B.SSS = 1;                /* Pad PA6: Source signal is DSPI_1 CLK  */
  SIUL2.MSCR[PA6].B.OBE = 1;                /* Pad PA6: OBE=1. */
  SIUL2.MSCR[PA6].B.SRC = 3;                /* Pad PA6: Full strength slew rate */

  SIUL2.MSCR[PA8].B.IBE = 1;                /* Pad PA8: Enable pad for input DSPI_1 SIN */
  SIUL2.IMCR[44].B.SSS = 1;                 /* Pad PA8: connected to pad PA8 */

  SIUL2.MSCR[PA5].B.SSS = 1;                 /* Pad PA5: Source signal is DSPI_1 CS0  */
  SIUL2.MSCR[PA5].B.OBE = 1;                 /* Pad PA5: OBE=1. */
  SIUL2.MSCR[PA5].B.SRC = 3;                 /* Pad PA5: Full strength slew rate */

  // Slave - DSPI_2
  SIUL2.MSCR[PA0].B.SSS = 2;                /* Pad PA0: Source signal is DSPI_2 CLK */
  SIUL2.MSCR[PA0].B.IBE = 1;                /* Pad PA0: IBE=1. */
  SIUL2.IMCR[48].B.SSS = 1;                /* Pad PA0: DSPI_2 CLK. Slave takes clock from master. Therefore input */

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

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

  SIUL2.MSCR[PA3].B.IBE = 1;                /* Pad PA3: IBE=1. DSPI_2 SS */
  SIUL2.IMCR[49].B.SSS = 1;            /* Pad PA3: connected to pad */
}

/*****************************************************************************/

void init_dspi_1(void)
{
  SPI_1.MCR.R = 0x80010001;                /* Configure DSPI_1 as master */
  SPI_1.MODE.CTAR[0].R = 0x78021004;       /* Configure CTAR0  */
  SPI_1.MCR.B.HALT = 0x0;                  /* Exit HALT mode: go from STOPPED to RUNNING state*/
                                          //CTAR maakt enkel uit voor master. Stelt de clock in.
}

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

//Info POPR en SR voor SPI: p1961 ref man

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

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

When testing this my connections were as follows:

Raspberry Pi NXP

SCLK → DSPI_2 CLK (PA0 - J5_1)

MOSI → DSPI_2_SIN (PA2 - J5_5)

The code on the MPC5744P was intended to send a message to UART when SPI transfer has happened. However, when using the script on the Raspberry Pi, I could not get this message to show up.

I'm currently stuck as I can't seem to find out what I'm forgetting?

At first I thought that i also needed to connect the Slave Select pin, but the reference manual says that you shouldn't tie this to ground?

Thank you for your time.

c
raspberry-pi
embedded
spi
powerpc
asked on Stack Overflow Jun 4, 2020 by WouterJ • edited Jun 4, 2020 by Lundin

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0