Problems using SPI component SynchroMaster

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

Problems using SPI component SynchroMaster

1,641 Views
andrewclegg
Contributor III

I am using the SynchroMaster processorexpert component to talk to a simple 4-wire SPI component attached to a TWR-K60F120m via the primary elevator. My settings are included at the bottom. I am using simple code to test (below): check buffers are empty, send the characters, wait for the transmit buffer to empty and the receive buffer to fill up, read out all the characters.

 

My problem seems to be that I always get back 0xff bytes no matter what should be coming back. I know that the SPI slave device works correctly as it has been tested on a different microcontroller. I am going to try to get my hands on a logic analyzer, but for now does anyone have any idea what might be causing this?

 

Thanks

 

int spi_readwrite(unsigned char *sendBuf, unsigned char *recvBuf, int size) {   if (size <= 0) {        return -1;   }   int retval;   unsigned char discard;    ana_Enable();    if (ana_GetCharsInTxBuf() != 0) {        printf("spi_readwrite called with non-empty transmit buffer\n");        return -11;   }    if (ana_GetCharsInRxBuf() != 0) {        printf("spi_readwrite called with non-empty receive buffer\n");        return -12;   }    // Send first   for (int i=0; i<size; i++) {        // Send character        retval = ana_SendChar(sendBuf[i]);        switch (retval) {        case ERR_OK:             break; // move on to the next character        case ERR_SPEED:             printf("sendChar(%d): ERR_SPEED\n", i);             return -2;        case ERR_DISABLED:             printf("sendChar(%d): ERR_DISABLED\n", i);             return -3;        case ERR_TXFULL:             printf("sendChar(%d): ERR_TXFULL\n", i);             return -4;        default:             printf("sendChar(%d): Unknown response code %d\n", i, retval);             return -5;        }   }    // Wait for transmit buffer to empty   int tx_bufsize;   while (1) {       tx_bufsize = ana_GetCharsInTxBuf();       if (tx_bufsize == 0) {            break;       }   }    // Wait for receive buffer to fill up   int rx_bufsize;   while (1) {        rx_bufsize = ana_GetCharsInRxBuf();        if (rx_bufsize == size) {             break;        }   }    // Receive   for (int i=0; i<size; i++) {        if (recvBuf == NULL) {             retval = ana_RecvChar(&discard);        } else {             retval = ana_RecvChar(&recvBuf[i]);        }        switch (retval) {             case ERR_OK:                  break;             case ERR_SPEED:                  printf("recvChar(%d): ERR_SPEED\n", i);                  return -6;             case ERR_RXEMPTY:                  printf("recvChar(%d): ERR_RX_EMPTY\n", i);                  return -7;             case ERR_OVERRUN:                  printf("recvChar(%d): ERR_OVERRUN\n", i);                  return -8;             case ERR_FAULT:                  printf("recvChar(%d): ERR_FAULT\n", i);                  return -9;             default:                  printf("recvChar(%d): Unknown response code %d\n", i, retval);                  return -10;        }   }  ana_Disable();   return 0; } 

 

16484_16484.pngspi_settings.png

Labels (1)
0 Kudos
9 Replies

1,073 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Andrew,

Thank you for your interest in Freescale's kinetis product, I would like to provide service for you !


I don't quite understand your meaning , do you want to know the bytes you received ?  

SPIx_TCR indicates the number of SPI transfers.


Hope it helps

Alice

0 Kudos

1,073 Views
andrewclegg
Contributor III

Hi Alice,

Thanks for your reply. The problem is that ana_RecvChar() always outputs the 0xff byte, even though it should be returning different values.

0 Kudos

1,073 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hi Andrew,

Can you check the "BufferRead" , and the "SPI2_POPR " register  ?

Alice

0 Kudos

1,073 Views
andrewclegg
Contributor III

Hi Alice,

I have now changed my code to use SendBlock/RecvBlock instead of SendChar/RecvChar.

I was able to get a logic analyzer trace of an SPI master-write transaction (top to bottom: Clock, MISO, MOSI, Chip Select). From this I can see that the MISO signal is always high and thus the 0xff value is 'correct'. However I think this is happening because the chip-select is toggling between bytes, whereas the slave device requires the chip-select to be held low for the duration of the transaction (usually 5, 8 or 11 bytes).

If I look at the SMasterLdd1 subcomponent, I can see that 'Chip select toggling' is set to 'no'. Do you know why the chip select appears to be toggling and is there a way I can hold it low for the duration of the multi-byte transfer?

Thanks

TEK00001.PNG.png

0 Kudos

1,073 Views
andrewclegg
Contributor III

Hi again,

I disabled the chip select pin in the SynchroMaster, and instead controlled it myself manually using a BitIO component. This works, and I am now receiving data correctly. This confirms that the problem was that the chip select was being toggled, even through 'chip select toggling' was set to 'no' in SPIMaster_LDD. Is this a bug in processor expert or one of its components? I would prefer to use the component properly rather than using the BitIO workaround.

Thanks

0 Kudos

1,073 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hi Andrew,

Which IDE do you use ? and which version of processor expert do you use ?

And if you can , send your project  would be the best.

Best Regards!

Alice

0 Kudos

1,073 Views
andrewclegg
Contributor III

Hi, I am using CodeWarrior for MCU, Version: 10.6, Build Id:140329. I am using the Processor Expert that came bundled with it, version 10.6.1. My installation is up to date.

My Processor Expert file for the bsp_twrk60f120m project is here.

Thanks

0 Kudos

1,073 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hi Andrew,

I checked your project configuration , and i haven't checked out problem ,  and I  create a very  simple  SPI sample , SPI0 send data to SPI1  on CW PE, and it work well .

so , i think that , whether the pin of PTD11 is  used in other place ?

Best Regards!

Alice

0 Kudos

1,073 Views
andrewclegg
Contributor III

Hi Alice,

I realised the PE file I sent you is after I made my 'fix' (controlling PTD11 manually using BitIO). But I have checked and I am not using PTD11 for anything else.

I think it probably works on many devices, where the chip select can be toggled between each byte without any problem. But on my particular device the chip select must be held down for the entire multi-byte transaction, and the PE code doesn't seem to do this.

I haven't been able to confirm it (I am new to debugging with Codewarrior + microcontrollers in general) but it looks like the CONT bit in SPI2_PUSHR is not being set. This appears to be what tells the MCU to keep the chip select down for the duration of the transaction.

Thanks,

Andy

0 Kudos