MQX3.8: How to Open SPI Ports for Multiple Slaves

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

MQX3.8: How to Open SPI Ports for Multiple Slaves

1,418 Views
huishao2
Contributor IV

Hi -

I have custom board loaded with MQX3.8 on K20d72m. There are two slave devices on MCU's SPI0 port that are driven by CS0 and CS2. Question is, how can I open/use the shared port for difference devices? In other words, does the SPI driver support multiple open to get file handler, like:

fopen("spi0:1", NULL) for CS0 and

fopen("spi0:4", NULL) for CS2 ?

Since the device on CS0 is already existing with fopen("spi0:", NULL), do I have to change it to fopen("spi0:1", NULL) to differentiate it with the one on CS2?

thanks!

Hui

0 Kudos
5 Replies

781 Views
soledad
NXP Employee
NXP Employee

You can use the same SPI to interface 2 or more slaves. You need to use IO_IOCTL_SPI_SET_CS command, you can refer to the

MQX I/O drivers users guide that can be found at this path C:\Program Files\Freescale\Freescale MQX 3.8\doc\mqx.

A single callback function for CS handling may be registered per SPI device. The callback function registration is performed by the IO_IOCTL_SPI_SET_CS_CALLBACK IOCTL command. SPI driver than calls the function any time when a change of the CS signals state is necessary. Besides the context data, the function is also passed a desired state of the CS signals. The callback function is then responsible for changing the state of the CS by any method (e.g. using LWGPIO).

Please note that setting the callback function possibly affects all file handles associated with the same SPI device since the function is called for any change to the state of CS signals, regardless of the file handle used for operation which is causing the CS state change.

For more information, please check the MQXIOUG (chapter 9)


Have a great day,
Sol

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

781 Views
huishao2
Contributor IV

Hi Sol

Thanks for your reply. I am however more confused by using IO_IOCTL_SPI_SET_CS_CALLBACK IOCTL command.

1. Is IO_IOCTL_SPI_SET_CS a one-time effort after fopen()? Or I have to set CS each time before SPI read/write?

2. The purpose to use the callback is because I have to restore the SPI HW context (baudrate, frame, CS, mode, etc) each time when a different SPI file handle is referred? I thought that is managed by SPI driver automatically.

3. CS is managed by SPI HW, it is associated with corresponding SPI file handle, right?

BTW, I am using SPI_legacy instead of SPI. It doesn't seem to support bus sharing/concurrency.

Thanks!

Hui

0 Kudos

781 Views
soledad
NXP Employee
NXP Employee

Hello,

Please check the below threads, and let me know if this helps!!

External SPI Chip select

How to activate CS on SPI2 Kinetis

Question about 3 SPI Device Control....

Regards

Sol

0 Kudos

781 Views
huishao2
Contributor IV

Hi Sol

I think I need to looked into SPI driver (not legacy one) source code and understand how driver works. I will post more specific questions in different threads.

Thanks!

Hui

0 Kudos

781 Views
matthewkendall
Contributor V

I have no experience with MQX 3.8, but I know that in MQX 4.0 and 4.1 this works fine. You can open the same SPI device multiple times, with different chip-select specifiers as in your example if desired, and then use the resulting multiple file handles to read and write to the specific devices on the bus.

Internal to the driver is a mutex that will prevent operations interrupting each other, e.g. if you call read() from two different tasks then one will complete before the other begins, even if one task pre-empts the other. However, if you have larger transactions that are made up of several individual read(), write(), etc., operations, and these transactions must not be split, and you access the bus from multiple tasks, then you will have to implement your own additional mutex to protect the transactions.