SPI Peripheral Configuration

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

SPI Peripheral Configuration

920 Views
ankitv
Contributor II

Hi,

I am using MR-CANHUBK344. I want to use SPI as a master device. I have configured the GPIO SPI pins (17 18 20 21) as follows:

ankitv_1-1702448194944.png

 

ankitv_0-1702448086307.png

Can you please advise how to configure the peripherals?

ankitv_2-1702448272293.png

 

and what blocks do I need to add in the MBDT? 

ankitv_3-1702448384417.png

I do not read any signal on the PINS using a oscilloscope. Thanks

-Ankit

0 Kudos
9 Replies

356 Views
Bravin_Link
Contributor II

I am currently trying to send SPI commands to a motor driver by configuring the MR-CANHUBK344 evaluation board as a MASTER. Here are my pin configurations using lpspi2. I just want to confirm which 4 SPI pins from the connectors that I should connect my motor driver to.

Bravin_Link_0-1706197142555.png

My current setup is shown below which utilizes the P1B connector which is shown in the schematic:

Bravin_Link_3-1706197287011.pngBravin_Link_4-1706197961851.png

 

0 Kudos

341 Views
robertv
NXP Employee
NXP Employee

Hello @Bravin_Link ,

First of all, it seems like the pins showcased in the configuration screenshot do not match with MR-CANHUBK344's LPSPI2 pins. More info regarding the setup process can be found in the first article from the MR-CANHUBK344 series, https://community.nxp.com/t5/NXP-Model-Based-Design-Tools/Interacting-with-Digital-Inputs-Outputs-on... . 

I will attach the MR-CANHUBK344's LPSPI2 pins (obtained from the schematic) below:

robertv_0-1706258573310.png

Since MR-CANHUBK344 uses a custom configuration currently (available as a .mex file in the attachments from the articles), peripherals outside the ones configured in the article series (DIO, ADC, PWM, UART) must be configured separately. In this thread you can find some info regarding the configuration of the SPI peripheral, but the main difference will probably the pins.

The SIN signal is the input (MISO in case the instance is a master, MOSI in case the instance is a slave) and the SOUT signal is the output (MOSI in case the instance is a master, MISO in case the instance is a slave).

What I can recommend in this case is making sure that the SIN signal that comes from the MR-CANHUBK344 is connected to the MISO line and that the SOUT signal is connected to the MOSI line (avoiding the confusing label names found in the schematic). 

Regards,
Robert V

 

0 Kudos

898 Views
robertv
NXP Employee
NXP Employee

Hello @ankitv ,

Since the direction of the pins is not visible in the attached image, I would like to double check it with you.
When configuring a Master instance, lpspi4_sin should be an input, while the other 3 signals are configured as outputs.

Also, to verify the physical connections, these are the SPI4 pins:

robertv_0-1702459082400.png

Regarding the peripheral configuration, the first step is to configure the SPI instance as a master instance.

robertv_1-1702459297606.png
From your SpiDriver screenshot, everything looks alright. Maintaining the default settings would work fine here.

As for the MBDT  blocks needed, you need an SPI block to Spi_SetAsyncMode, along with the blocks responsible for storing data in the buffer, sending the data and then validating that the data has been sent using a hardware interrupt callback

robertv_4-1702460738252.png

robertv_3-1702459836618.png

 

The "data" Data Store Read instance contains an array of elements of data type uint8 since that is what the SPI driver expects.

Regards,
Robert V

0 Kudos

872 Views
ankitv
Contributor II

Hello @robertv 
I am getting the following error:  undefined reference to `Lpspi_Ip_LPSPI_2_IRQHandler'

ankitv_1-1702505360042.png


To confirm, I am using the same pins as shown in the image below

ankitv_0-1702504175302.png

Pins are configured as output except for  lpspi4_sin which is input.

ankitv_2-1702505490784.png

I am attaching my project zip for your reference. Can you please help ? Thanks

-Ankit

 

 

 

 

0 Kudos

858 Views
robertv
NXP Employee
NXP Employee

Hello @ankitv ,

I have looked over the attached project and identified the problem. When the LPSPI_2 instance was replaced with the LPSPI_4 instance inside the SpiGeneral -> SpiPhyUnit, the instance was removed from use, but the interrupts were not updated to reflect that.

The following settings will be found inside Platform -> Interrupt Controller.

robertv_1-1702568373291.png

To solve the error, the interrupt for the LPSPI_2 has to be changed to undefined_handler and disabled using the checkbox.
Additionally, the newly configured instance's interrupts must be enabled by clicking the checkbox, setting a priority level and an interrupt handler, Lpspi_lp_LPSPI_4_IRQHandler.

robertv_2-1702568488766.png

 

Note: ADC1's interrupt priority is set to 5 and having the LPSPI_4's priority set to the same level could cause the CS line of the SPI to behave differently than normal, so I advise you to use a lower value, which translates to a higher priority.

Regards,
Robert V

0 Kudos

849 Views
ankitv
Contributor II

Hello @robertv 
That resolved the issue and I was able to build and deploy. However, I am still not getting the desired signal on the SPI pins. I connected an external oscilloscope but could not read any signal. My Freemaster config looks like this

ankitv_0-1702661780044.png

In the above image, the logic seems to be working as my "master_send" increments by 1 everytime I press the "USER_SW1", which indicates a successful transmissions 

ankitv_1-1702661905792.png

ankitv_2-1702661924698.png

Is there a way I can monitor the signal on LPSPI4_MOSI, LPSPI4_MISO, LPSPI4_CS0 AND LPSPI4_CS0 in the free master tool ? Just to make sure that I am sending the right bits?

Signal I want to send: Should toggle the SCLK for each bit, and Chip Select (CS) should be pulled low during the entire data frame 

ankitv_4-1702662474121.png

ankitv_5-1702662526404.png

I have attached a sample Arduino code that generates the above mentioned desired signal.

Another query:
Since I am using the MRCANHUB board as a Master device, the PIN LPSPI4_MOSI (PTB11) should it not be Output and PIN LPSPI4_MISO (PTB9) as an Input ?

ankitv_3-1702662354882.png

Kind Regards,
-Ankit

 

0 Kudos

810 Views
robertv
NXP Employee
NXP Employee

Hello @ankitv ,

After building and deploying the model you have attached here, along with the suggested fixes from my last reply, I validated that the SPI instance was sending data using a logic analyzer. 

robertv_1-1702910154682.png
Unfortunately the FreeMASTER tool does not provide the ability to monitor signals the way a logic analyzer would. 

Since the Job0 transmission ends properly, it indicates that the board is sending the SPI data, so that theoretically points us towards connection issues. 

First, I would suggest running the application without any other devices connected to the MR-CANHUBK344 board.
Connecting the MR-CANHUB's LPSPI_4's pins directly to an oscilloscope/logic analyzer would show if the signals are being generated by the board. One other thing worth mentioning is that the HIGH level is 3.3 Volts.

Regarding the question about the signal's direction, I understand that the schematic might be unclear when it comes to the labels.
According to the S32K3 Reference Manual, SIN is an input and SOUT is an output.

robertv_0-1702910017744.png

Since the instance is configured as master, the data out line represents a master output, so it should connect to a slave input on the other device.  And similarly for the data input signal, which should be connected to the other device's output SPI pin.

robertv_2-1702911282920.png



Regards,
Robert V

 

0 Kudos

762 Views
ankitv
Contributor II

Hi Robert,
I got the model working. After analyzing the signal I found that my CS and CLK were inverted. The image below shows fixed CS but CLK as inverted. After correcting CLK as well, I got the desired signal.

ankitv_0-1702999882583.jpeg
I did this by changing the "output inversion select" from Pin config

ankitv_1-1702999967025.png

Another issue I faced was of the FreeMaster Tool. While I am sending [8 128] as an array which corresponds to 00001000 10000000 in binary and 2176 in decimal. The FreeMaster tool is reading it as [128 8] which is 10000000 00001000 in binary and 32776 in decimal

ankitv_2-1703000317707.png

Is there any way I could fix this so that I can quickly monitor the correct 16bit signal that I am sending and receiving? Thanks. 

-Ankit

 

 

0 Kudos

718 Views
robertv
NXP Employee
NXP Employee

Hello @ankitv ,

Regarding the polarity inversion for the clock, one other thing you could try is the "SpiShiftClockIdleLevel" setting inside SpiDriver. This allows you to set the Clock level outside of the transmission. The level should be the same between the connected devices, as to avoid errors. 
In the same section, there's a setting for "SpiCsSelection" that defines the active polarity of Chip Select, in case you want to change that as well.
I'm not aware whether using the Output Inversion option, instead of the SPI peripheral settings, leads to the same result or not, but I recommend keeping the alternatives in mind.

robertv_0-1703078125645.png

From what I can tell from your oscilloscope picture, you are using SpiDataWidth set to 8 and sending 8 bits of data twice, leading to a transfer of 16 bits in the same SPI message. And judging from the master out signal, I think the data is received as [128 8], which does not look normal since the bytes should not be reordered.  

Regarding the FreeMASTER question, I managed to find a solution. In FreeMASTER, the data from the Data Store Memory (the array) has its bits concatenated into a 16 bit value, but not before reordering the bytes, due to endianness. 
A solution to this would be to use the array's indexes to see the values in the order they are stored in Simulink.
The second solution would be to use a few simulink blocks to invert the bytes, which will allow the data in FreeMASTER to be displayed as initially declared.
Below you can see how you can access the data using indexes, and also that outputting a different variable (spi_data_reversed), that contains the reversed bytes shows the value you are expecting.

robertv_1-1703080396859.png
To obtain the reversed value, you will need another Data Store Memory and the Byte Unpack, Byte Reversal and the Byte Pack blocks.

robertv_2-1703080569297.png
They have to be configured as follows:
The Data Store Memory that will hold the reversed data has to be set up similarly to the original Data Store Memory block, same array initialization, same data type.
Byte Unpack receives 2 uint8 values and transforms them into a uint16 value.

robertv_3-1703080683572.png

The Byte Reversal block only needs to have the number of inputs configured. This changes the order of the bytes in the variable.
For example
11001000 01110011 -> Byte Reversal -> 01110011 11001000

robertv_5-1703080787411.png

Byte Pack has to be configured to accept uint16 as input.

robertv_4-1703080748840.png
After that, you will be able to see the initial bytes in FreeMASTER after adding the variable to the Variable Watch section.

This newly obtained Data Store Read variable can also be used as an input for the SPI, sending the bytes in a different order.


Regards,
Robert V

0 Kudos