How to switch SPI mode in real time?

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

How to switch SPI mode in real time?

1,323 Views
dalim
Contributor I

Hi,

I am using MK64FN1MOVLQ12, MQX Rtos, and Keil uVision IDE. I was wondering how and if I can switch SPI modes in real time? I have two devices on the same SPI bus, but they have different SPI modes.

Thanks.

Labels (1)
0 Kudos
5 Replies

1,175 Views
mjbcswitzerland
Specialist V

Hi

The DSPI has two "clock and transfer attribute" registers (SPIx_CTAR0 and SPIx_CTAR1) which can configure different modes, speeds, etc.

Each time you send data you specify which of the CTAR is used for the transfer, and also which chip select (if used).

This means that you can change the mode on-the-fly. You can also queue up to four words to be transferred (only on SPI0 in K64), each with different characteristics and the DSPI will handle the details of each (and potentially switching modes as programmed for each one).

Regards

Mark

Complete Kinetis solutions for faster/more efficient professional needs, training and support: http://www.utasker.com/kinetis.html
i.MX RT project compatibility: http://www.utasker.com/iMX.html

Kinetis K64:
- http://www.utasker.com/kinetis/FRDM-K64F.html
- http://www.utasker.com/kinetis/TWR-K64F120M.html
- http://www.utasker.com/kinetis/TEENSY_3.5.html
- http://www.utasker.com/kinetis/Hexiwear-K64F.html

uTasker: supporting >1'000 registered Kinetis users get products faster and cheaper to market

Request Free emergency remote desk-top consulting at http://www.utasker.com/services.html

Open Source version at https://github.com/uTasker/uTasker-Kinetis

https://community.nxp.com/thread/512558
https://community.nxp.com/thread/352862
https://community.nxp.com/thread/498809

0 Kudos

1,175 Views
dalim
Contributor I

Hi Mark,

Thank you for your respond. What is DSPI? Is it different from SPI? I tried the following lines of codes to configure the SPI mode before sending data and pulling the chip select low. That didn't work, it always stays in the mode that first SPI mode was initialized first.

uint_32        spi0_ctar_reg;

// SPI MODE 0

spi0_ctar_reg = SPI0_CTAR1;

SPI0_CTAR1 = spi0_ctar_reg | SPI_CTAR_ASC(1);

// SPI MODE 3

spi0_ctar_reg = SPI0_CTAR0;

SPI0_CTAR0 = spi0_ctar_reg | SPI_CTAR_ASC(1)| SPI_CTAR_CPHA_MASK | SPI_CTAR_CPOL_MASK;

Any idea how I can troubleshoot this?

Thanks.

0 Kudos

1,175 Views
mjbcswitzerland
Specialist V

Hi

DSPI is the full name for the SPI used in the K64 ("DMA Serial Peripheral Interface) - it can also be called (simply) SPI.

When you send data you write the data (plus control information) to SPIx_PUSHR:

pastedImage_1.png

You tell the DSPI which chip select (PCS) it should use and also which attribute set (CTAS).

Therefore, in order to send (for example) the byte 0x55 - the first time with the first attribute set and the second time with the second you would write the data as follows:

SPIx_PUSHR = (0x00000000 | 0x55); // use CTAS0

SPIx_PUSHR = (0x10000000 | 0x55); // use CTAS1

Are you setting the CTAS accordingly?

Regards

Mark

0 Kudos

1,175 Views
dalim
Contributor I

Hi,

I'm using MQX IO SPI driver. My chip selects pins are controlled using 'lwgpio_set_value', and all SPI transactions are done using 'ioctl(spifd,IO_IOCTL_SPI_READ_WRITE,&rw)' and 'fflush(spifd)'. Using SPIx_PUSHR at the same time results in transaction instability. Hmmm, is there another way of setting the CTAS?

Thanks.

0 Kudos

1,175 Views
mjbcswitzerland
Specialist V

Hi

The K64s SPI0 has a 4 word deep FIFO but the SPI1 and SPI2 are only one deep. If you are using SPI0 you should be able to write 2 words immediately but on the other interfaces you need to wait until the first word has been sent before writing the second.

I don't know anything about the MQX SPI driver so it is best of you ask the manufacturer if it has restrictions.

In case of problems there is no reason why you don't send data using your own code - a polling SPI driver only requires a few lines of code.

Regards

Mark

0 Kudos