Slave SPI component problem with 9S08DZ16

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Slave SPI component problem with 9S08DZ16

Jump to solution
2,411 Views
Snaku
Contributor III

Hi,

I got a problem about SPI slave component with 9S08DZ16, the MISO always outputs garbage data when I call SS1_SendBlock() function, and it also causes the SS1_RecvBlock() function receives wrong data. If I don't use the SS1_SendBlock() function, the data received from SS1_RecvBlock() function is correct, my test environment as below:

 

MC9S12XDG512 is the SPI master, it uses SM1_SendBlock() to send 4 bytes data every 1.5ms.

MC9S08DZ16 is the SPI slave, SPI interrupt is enabled, it receives 4 bytes data and then send 4 bytes data(const variable) in the SS1_OnFullRxBuf() function, here is the code of the function.

 

void SS1_OnFullRxBuf(void){    /* Write your code here ... */    char buf[4];    word rcv_len, snd_len;    SS1_RecvBlock(buf, 4, &rcv_len);    SS1_SendBlock(buf_tx, 4, &snd_len);}

 

Below comment is the SPI component setting of the ProcessorExpert

[ SPI slave setting ]

/*** ===================================================================**     Method      :  SS1_Init (bean SynchroSlave)****     Description :**         Initializes the associated peripheral(s) and the bean internal **         variables. The method is called automatically as a part of the **         application initialization code.**         This method is internal. It is used by Processor Expert only.** ===================================================================*/void SS1_Init(void){  SerFlag = 0;                         /* Reset all flags */  ErrFlag = 0;                         /* Reset all flags in mirror */  SS1_EnEvent = TRUE;                  /* Enable events */  SS1_InpLen = 0;                      /* No char in the receive buffer */  InpIndxW = InpIndxR = 0;             /* Set the indices to the start of the receive buffer */  SS1_OutLen = 0;                      /* No char in the transmit buffer */  OutIndxW = OutIndxR = 0;             /* Set the indices to the start of the transmit buffer */  #pragma MESSAGE DISABLE C4002        /* Disable warning C4002 "Result not used" */  (void)SPIS;                          /* Read the status register */  (void)SPID;                          /* Read the data register */  /* SPIC2: ??=0,??=0,??=0,MODFEN=0,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */  setReg8(SPIC2, 0x00);                /* Configure the SPI port - control register 2 */   /* SPIC1: SPIE=1,SPE=0,SPTIE=0,MSTR=0,CPOL=0,CPHA=1,SSOE=0,LSBFE=0 */  setReg8(SPIC1, 0x84);                /*  Configure the SPI port - control register 1 */   SPIC1_SPE = 1;                       /* Enable device */  SPID = SS1_EOF;                      /* Store the empty char to the transmit register */}

 

 

[ SPI Master setting ]

/*** ===================================================================**     Method      :  SM1_Init (component SynchroMaster)****     Description :**         Initializes the associated peripheral(s) and the component **         internal variables. The method is called automatically as a **         part of the application initialization code.**         This method is internal. It is used by Processor Expert only.** ===================================================================*/void SM1_Init(void){  /* SPI0CR1: SPIE=0,SPE=0,SPTIE=0,MSTR=0,CPOL=0,CPHA=1,SSOE=0,LSBFE=0 */  SPI0CR1 = 0x04U;                     /* Reset the device register */  (void)SPI0SR;                        /* Read the status register */  (void)SPI0DR;                        /* Read the device register */  /* SPI0BR: ??=0,SPPR2=1,SPPR1=0,SPPR0=0,??=0,SPR2=0,SPR1=1,SPR0=0 */  SPI0BR = 0x42U;                      /* Set the baud rate register */  /* SPI0CR2: ??=0,??=0,??=0,MODFEN=1,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */  SPI0CR2 = 0x10U;                     /* Set control register 2 */  /* SPI0CR1: SPIE=1,SPE=1,SPTIE=0,MSTR=1,CPOL=0,CPHA=1,SSOE=1,LSBFE=0 */  SPI0CR1 = 0xD6U;                     /* Set control register 1 */  SerFlag = 0U;                        /* Reset all flags */  ErrFlag = 0U;                        /* Reset all flags in mirror */  OnFreeTxBuf_semaphore = FALSE;       /* Clear the OnFreeTxBuf_semaphore */  SM1_InpLen = 0U;                     /* No char in the receive buffer */  InpPtrR = InpBuffer;                 /* Set read pointer on the first item in the receive buffer */  InpPtrW = InpBuffer;                 /* Set write pointer on the first item in the receive buffer */  SM1_OutLen = 0U;                     /* No char in the transmit buffer */  OutPtrR = OutBuffer;                 /* Set read pointer on the first item in the transmit buffer */  OutPtrW = OutBuffer;                 /* Set write pointer on the first item in the transmit buffer */}

 

Do I have any wrong usage of SS1_SendBlock() function?

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
1,941 Views
Snaku
Contributor III

Hi,

This problem is solved by change a new board of MC9S08DZ, it just my hardware problem.

Thank you,

Snack

View solution in original post

0 Kudos
Reply
4 Replies
1,941 Views
ProcessorExpert
Senior Contributor III

Hello,

 

the SPI works in general as shift register. In case you need some data from slave you must send the same number of data from Master. There could be issue during start the communication because after first send of data from master, slave send back only "empty char" (SPID = SS1_EOF:smileywink: (last line of init method).

 

 

void SS1_OnFullRxBuf(void){   /* event is called after the whole slave receive buffer is full, master received 4 chars (first time 4 empty ch.)*/
    char buf[4];    word rcv_len, snd_len;    SS1_RecvBlock(buf, 4, &rcv_len); /* correct data received from master 4, but to the master was sent 4 chars*/    SS1_SendBlock(buf_tx, 4, &snd_len); /* prepare 4 chars for send (these data will be send during sending next 4 chars from master )*/}

Note: whole communication from slave is delayed by size of the receive slave buffer in case the code above is used


best regards
Vojtech Filip
Processor Expert Support Team

1,941 Views
Snaku
Contributor III

Hi, 

Thanks for your reply. I understand this communication is delay by the buffer size(4 bytes), but I checked the bit stream (garbage data) of the MISO pin, there is no relationship to the send data, for example, send data is 0x51, 0x52, 0x53, 0x54, the garbage data measured by a SPI analyzer is 0x47, 0xB2, 0xE3, 0x8C.

 

I do another experiment, I exchange the roles of master and slave, i.e. SPI master is MC9S08DZ16, SPI slave is MC9S12XDG512. I use the same behavior of the SPI master and slave communication. The received and transmitted data of the SPI slave, i.e. MC9S12XDG512, is correct; I use a SPI analyzer to check it. But the SPI master, i.e. MC9S08DZ16, received garbage data; I check the bit stream in the RAM log. So there always a problem on the MC9S08DZ16, on matter it is a SPI slave or master.

0 Kudos
Reply
1,941 Views
Snaku
Contributor III

Hi,

I have interesting information. In my last experiment (master is 9SDZ16, slave is 9S12XDG512), if the 9SDZ16 always send 0x00 or 0xFF, it can receive correct data from 9S12XDG512. If the 9SDZ16 always send 0xAA, it will receive incorrect data from 9S12XDG512. I measured the waveform of the MISO pin, data from 9S12XDG512 always correct.

FYI.

0 Kudos
Reply
1,942 Views
Snaku
Contributor III

Hi,

This problem is solved by change a new board of MC9S08DZ, it just my hardware problem.

Thank you,

Snack

0 Kudos
Reply