Hey there!
I am trying to use the SPI0 module of my K64F to read sensor data. I can initialize the SPI0 in a way, that I get the reading of only one of the four sensors. That means, I configure the SPI0 at the start of the program to PCS0, 1, 2 or four.
But I cannot change the PCS during runtime. What is the way to do this?
This is my most elaborate try, which also does not work:
while(1){
.
.
.
dspiResult = DSPI_DRV_MasterDeinit(DSPI_MASTER_INSTANCE); //Deinit old PCS
dspiResult = DSPI_DRV_MasterInit(DSPI_MASTER_INSTANCE, //Init new PCS
&masterState,
&masterUserConfig);
dspiResult = DSPI_DRV_MasterConfigureBus(DSPI_MASTER_INSTANCE,
&masterDevice,
&calculatedBaudRate);
dspiResult = DSPI_DRV_MasterTransfer(DSPI_MASTER_INSTANCE,
NULL,
NULL,//sendBuffer,
receiveBuffer,//NULL,
TRANSFER_SIZE);
while (DSPI_DRV_MasterGetTransferStatus(DSPI_MASTER_INSTANCE, &wordsTransfer) == kStatus_DSPI_Busy)
{
}
.
.
.
}
Where is my problem, what am I missing? Please tell me the steps and function calls to do this.
I would be very glad if someone could tell me the correct procedure to do this. I have read the Spi chapter in the manual, but could not find the appropriate pages for that.
Thanks for any help and information!
Cheers,
Michael
Solved! Go to Solution.
Hi, Michael,
I have checked the main.c you have written, I think you misunderstood me, it is unnecessary to call the following function:
while(1)
{
DSPI_DRV_MasterDeinit()
DSPI_DRV_MasterInit();
DSPI_DRV_MasterConfigureBus();
DSPI_DRV_MasterTransfer();
while (DSPI_DRV_MasterGetTransferStatus) {}
}
You can use the following code:
while(1)
{
DSPI_DRV_MasterTransfer();
while (DSPI_DRV_MasterGetTransferStatus) {}
}
I attach my project which is modified with the project located at:
C:\DriverE\Freescale\KSDK_1.3.0\examples\frdmk64f\driver_examples\dspi\dspi_non_blocking\master\kds
The *.jpeg file is the screenshot from scope, the yellow is SPI0_PCS0(PTD0), the blue is SPI0_PCS1(PTD4), each pin toggle 64 times, it is correct.
Hope it can help you.
BR
XiangJun Rong
Hello!
Thank you for that information. I have also tried the method where I only change the pcs in the masterstate without the deinit function. That did not work, as well.
But after comparing my code to yours I noticed that you set the polaritymode separately like this:
DSPI_HAL_SetPcsPolarityMode(g_dspiBase[DSPI_MASTER_INSTANCE],kDspiPcs0,kDspiPcs_ActiveLow); | |
DSPI_HAL_SetPcsPolarityMode(g_dspiBase[DSPI_MASTER_INSTANCE],kDspiPcs1,kDspiPcs_ActiveLow); |
and not only in the
dspi_master_user_config_t SPImasterUserConfigPCS0 = { | ||
.isChipSelectContinuous | = false, | |
.isSckContinuous | = false, | |
.pcsPolarity | = kDspiPcs_ActiveLow, | |
.whichCtar | = kDspiCtar0, | |
.whichPcs | = kDspiPcs0 |
};
And that was what I was missing the whole time, apparantly.
Now it works fine and I can switch the pcs like you described
Thank you a lot! :smileyhappy:
Best regards,
Michael
Hey, there!
Thanks for the help, but this does not work in my dspi non blocking example. I have attached the main.c file of my dspi non blocking example, maybe you could take a quick look at it and maybe tell me what I'm doing wrong As I said, in my example dspi project I can do pcs1 OR pcs0, but not both in the same run, like your code for example. In my code they only work if I only initialize and use one.
Other people have suggested using the master deinit function, which also didnt work. I have read the manual and the comments in the fsl_dspi_hal, but it does not say anywhere how to correctly change the pcs.
I am already trying for a long time to do this, where is my mistake?
Best regards,
Michael
Hi, Michael,
I have checked the main.c you have written, I think you misunderstood me, it is unnecessary to call the following function:
while(1)
{
DSPI_DRV_MasterDeinit()
DSPI_DRV_MasterInit();
DSPI_DRV_MasterConfigureBus();
DSPI_DRV_MasterTransfer();
while (DSPI_DRV_MasterGetTransferStatus) {}
}
You can use the following code:
while(1)
{
DSPI_DRV_MasterTransfer();
while (DSPI_DRV_MasterGetTransferStatus) {}
}
I attach my project which is modified with the project located at:
C:\DriverE\Freescale\KSDK_1.3.0\examples\frdmk64f\driver_examples\dspi\dspi_non_blocking\master\kds
The *.jpeg file is the screenshot from scope, the yellow is SPI0_PCS0(PTD0), the blue is SPI0_PCS1(PTD4), each pin toggle 64 times, it is correct.
Hope it can help you.
BR
XiangJun Rong
Hi, Michael,
I think you use the SDK1.3 based on FRDM-K64, and use the example:
C:\Freescale\KSDK_1.3.0\examples\frdmk64f\driver_examples\dspi_rongModification\dspi_non_blocking\master\kds
You develop your code based on the example. I think you have to consider two points:
1)finish the pin assignment for SPI0_PCS1, SPI0_PCS2... with the following code:
void PCSInit(void)
{
/* SPI0 */
/* Affects PORTD_PCR0 register, SPI0_PCS0 */
PORT_HAL_SetMuxMode(PORTD,0u,kPortMuxAlt2);
/* Affects PORTD_PCR4 register, SPI0_PCS1 */
PORT_HAL_SetMuxMode(PORTD,4u,kPortMuxAlt2);
/* Affects PORTD_PCR5 register, PSI0_PCS2 */
PORT_HAL_SetMuxMode(PORTD,5u,kPortMuxAlt2);
/* Affects PORTD_PCR6 register, SPI0_PCS3 */
PORT_HAL_SetMuxMode(PORTD,6u,kPortMuxAlt2);
}
2)secondly, you can use the code to set up different SPI0_PCSx.
masterState.whichPcs= kDspiPcs1;
I attach the code I modified based on the example, I can see the waveform on different SPI0_PCSx, it seems it is okay.
Hope it can help you.
BR
XiangJun Rong