How to change Chip Select of SPI during runtime?

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

How to change Chip Select of SPI during runtime?

1,057 Views
michaelkoller
Contributor III

Hey there!

 

I have used the SPI0 module on my K64F to read sensor date with success. I also tried the different Chip Selects of that module (PCs0,1,2,4 for each sensor in this case).

 

My problem occurs when I configure the Port for the additional PCs like this:

    PORT_HAL_SetMuxMode(PORTD,3u,kPortMuxAlt2);

    PORT_HAL_SetMuxMode(PORTD,1u,kPortMuxAlt2);

    PORT_HAL_SetMuxMode(PORTD,2u,kPortMuxAlt2);

    PORT_HAL_SetMuxMode(PORTD,0u,kPortMuxAlt2); //ptdo: spi0_pcs0

    PORT_HAL_SetMuxMode(PORTC,2u,kPortMuxAlt2); //ptc2: spi0_pcs2

    PORT_HAL_SetMuxMode(PORTC,0u,kPortMuxAlt2); //ptc0: spi0_pcs4

    PORT_HAL_SetMuxMode(PORTD,4u,kPortMuxAlt2); //ptd4: spi0_pcs1

 

and

 

dspi_master_user_config_t SPImasterUserConfigPCS0 = {

        .isChipSelectContinuous     = false,

        .isSckContinuous            = false,

        .pcsPolarity                = kDspiPcs_ActiveLow,

        .whichCtar                  = kDspiCtar0,

        .whichPcs                   = kDspiPcs0

    };

 

and then another like this

 

dspi_master_user_config_t SPImasterUserConfigPCS1 = {

            .isChipSelectContinuous     = false,

            .isSckContinuous            = false,

            .pcsPolarity                = kDspiPcs_ActiveLow,

            .whichCtar                  = kDspiCtar0,

            .whichPcs                   = kDspiPcs1

        };

and two more for 2 and 4.

 

I do the rest of the initialization according to the dspi non blocking example in my driver example folder of KSDK1.3

and call dspiResult = DSPI_DRV_MasterInit(DSPI_INSTANCE, &masterState, &SPImasterUserConfigPCS0); which sets PCS0.

 

If I leave it that way, no problem, readout is fine.

 

But if I try to change the ChipSelect during runtime in the main while loop, I don't get correct readouts.

I don't quite know how, but even if I disable the code part, where it is supposed to switch the ChipSelect, it won't work as well. the readout is always zero, but dspi_result also says success.

 

Can I change the Chip Select in the main while loop?

My try was, to do a new MasterInit call with the new dspi_master_user_config_t and then I can call the DSPI_DRV_MasterTransfer again using the new Chip Select.

 

Thanks for reading this far, I am thankful for any help and information.

 

Best regards,

Michael

Labels (1)
0 Kudos
1 Reply

635 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Michael,

I think it is okay to change the PCS pin when the  spi is running.

1)You set the SPI in master mode, if you want to change the PCS pin, you have to reinitialize the spi as following:

dspi_master_user_config_t SPImasterUserConfigPCS1 = {

            .isChipSelectContinuous     = false,

            .isSckContinuous            = false,

            .pcsPolarity                = kDspiPcs_ActiveLow,

            .whichCtar                  = kDspiCtar0,

            .whichPcs                   = kDspiPcs1

        };

dspiResult = DSPI_DRV_MasterInit(DSPI_INSTANCE, &masterState, &SPImasterUserConfigPCS0);

DSPI_DRV_MasterTransfer();

2)If you want to enable different PCS interleavedly, I think you can call the following function:

DSPI_GetDefaultDataCommandConfig();

command->whichPcs = kDSPI_Pcs1;

DSPI_MasterWriteDataBlocking();

I think it is okay, but I do not have a try.

BR

XiangJun Rong

void DSPI_GetDefaultDataCommandConfig(dspi_command_data_config_t *command)

{

    command->isPcsContinuous = false;

    command->whichCtar = kDSPI_Ctar0;

    command->whichPcs = kDSPI_Pcs0;

    command->isEndOfQueue = false;

    command->clearTransferCount = false;

}

void DSPI_MasterWriteDataBlocking(SPI_Type *base, dspi_command_data_config_t *command, uint16_t data)

{

    /* First, clear Transmit Complete Flag (TCF) */

    DSPI_ClearStatusFlags(base, kDSPI_TxCompleteFlag);

    while (!(DSPI_GetStatusFlags(base) & kDSPI_TxFifoFillRequestFlag))

    {

        DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);

    }

    base->PUSHR = SPI_PUSHR_CONT(command->isPcsContinuous) | SPI_PUSHR_CTAS(command->whichCtar) |

                  SPI_PUSHR_PCS(command->whichPcs) | SPI_PUSHR_EOQ(command->isEndOfQueue) |

                  SPI_PUSHR_CTCNT(command->clearTransferCount) | SPI_PUSHR_TXDATA(data);

    DSPI_ClearStatusFlags(base, kDSPI_TxFifoFillRequestFlag);

    /* Wait till TCF sets */

    while (!(DSPI_GetStatusFlags(base) & kDSPI_TxCompleteFlag))

    {

    }

}

0 Kudos