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.
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).
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
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.
// 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?
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:
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?
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?
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.