Using SPI0 to control two slave devices with differnent SPIconfig

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

Using SPI0 to control two slave devices with differnent SPIconfig

Jump to solution
927 Views
Leon06
Contributor III

Hi all,

I have some issues about using the SPI module.

I want to use the S32k148 SPI0 to control two slave PMIC(FS65) and EEPROM(M95M01)

Because of the different baudrate and framsize, need to re-initialize the SPI0 configurations in the runtime. Before SPI data transmited, deinit the SPI first and then re-init SPI with the corresponded configuration.

status_t StartBlockingTransfer_SPI0(lpspi_which_pcs_t cs, uint8_t* u8_tx_buff, uint8_t* u8_rx_buff, uint16_t u16_len, uint32_t u32_timeout)
{
	static uint8_t cs_temp;
	if(cs_temp != cs){
		cs_temp = cs;
		SPI_MasterDeinit(&spi0Instance);
		SPI_MasterInit(&spi0Instance, &spi0MasterConfig[cs]);
	}else{

	}
	//LPSPI_DRV_SetPcs(0U, cs, LPSPI_ACTIVE_LOW);
	return SPI_MasterTransferBlocking(&spi0Instance, u8_tx_buff, u8_rx_buff, u16_len, (uint16_t)u32_timeout);
}

Below are the SPI0 configurations for the 2 slave devices. 

spi_master_t spi0MasterConfig[2] =
{
	[0] ={//PMIC
		.baudRate      = 500000,
		.ssPolarity    = SPI_ACTIVE_LOW,
		.frameSize     = 16U,
		.clockPhase    = READ_ON_EVEN_EDGE,
		.clockPolarity = SPI_ACTIVE_HIGH ,
		.bitOrder      = SPI_TRANSFER_MSB_FIRST,
		.transferType  = LPSPI_USING_INTERRUPTS,
		.rxDMAChannel  = 255U,
		.txDMAChannel  = 255U,
		.callback      = NULL,
		.callbackParam = NULL,
		.ssPin         = 0,
		.extension     = NULL
	},
	[1] = {//EEPROM
		.baudRate	   = 1000000, //1MHz
		.ssPolarity    = SPI_ACTIVE_LOW,
		.frameSize	   = 8U,
		.clockPhase    = READ_ON_ODD_EDGE,
		.clockPolarity = SPI_ACTIVE_HIGH ,
		.bitOrder	   = SPI_TRANSFER_MSB_FIRST,
		.transferType  = LPSPI_USING_INTERRUPTS,
		.rxDMAChannel  = 255U,
		.txDMAChannel  = 255U,
		.callback	   = NULL,
		.callbackParam = NULL,
		.ssPin		   = 1,
		.extension	   = NULL,
	}
};

Now I want to change the different configurations after the EEPROM page-write(256 bytes) transmitted completely, how could I make it?

About 16 bytes transmitted and then  SPI0 deinit.

Leon06_1-1682318220800.png

 

Leon

 

 

 

0 Kudos
Reply
1 Solution
844 Views
Leon06
Contributor III

Hi Robin,

Thanks for your help.

Now , I add a SPI status checked before doing SPI de-initialized and initialized.

This way, it ensures that if the SPI module is busy, nothing will be done before the SPI configuration is changed.

status_t StartBlockingTransfer_SPI0(lpspi_which_pcs_t cs, uint8_t* u8_tx_buff, uint8_t* u8_rx_buff, uint16_t u16_len, uint32_t u32_timeout)
{
	//If busy, not do spi initialize, Leon 20230427
	LPSPI_Type *base = g_lpspiBase[0];
    if (LPSPI_GetStatusFlag(base, LPSPI_MODULE_BUSY))
    {
        return STATUS_BUSY;
    }

	static uint8_t cs_temp;
	if(cs_temp != cs){
		cs_temp = cs;
		SPI_MasterDeinit(&spi0Instance);
		SPI_MasterInit(&spi0Instance, &spi0MasterConfig[cs]);
	}else{

	}
	return SPI_MasterTransferBlocking(&spi0Instance, u8_tx_buff, u8_rx_buff, u16_len, (uint16_t)u32_timeout);
}

Leon

 

View solution in original post

4 Replies
911 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Leon,

What's the value of u16_len and u32_timeout? If time expires SPI_MasterTransferBlocking()  will return error and the transmission will be aborted. Did you check the return value?

Best Regards,
Robin
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
Reply
893 Views
Leon_Hsieh
Contributor II

Hi Robin,

I didn't check the return value yet. 

I set the value u16_len = 260U,and u32_twmeout = 10U.

For testing, I close PMIC SPI and only EEPROM SPI active.

Also, I trace that the SPI messages fully transmitted is taken about 2ms.

Leon_Hsieh_0-1682384520947.png

Leon

 

0 Kudos
Reply
879 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Leon,

Which version of S32DS+S32K SDK are you using? It is recommended to use the latest S32DSv3.4+S32K1 SDK RTM4.0.3
What is the return value?

Best Regards,
Robin

0 Kudos
Reply
845 Views
Leon06
Contributor III

Hi Robin,

Thanks for your help.

Now , I add a SPI status checked before doing SPI de-initialized and initialized.

This way, it ensures that if the SPI module is busy, nothing will be done before the SPI configuration is changed.

status_t StartBlockingTransfer_SPI0(lpspi_which_pcs_t cs, uint8_t* u8_tx_buff, uint8_t* u8_rx_buff, uint16_t u16_len, uint32_t u32_timeout)
{
	//If busy, not do spi initialize, Leon 20230427
	LPSPI_Type *base = g_lpspiBase[0];
    if (LPSPI_GetStatusFlag(base, LPSPI_MODULE_BUSY))
    {
        return STATUS_BUSY;
    }

	static uint8_t cs_temp;
	if(cs_temp != cs){
		cs_temp = cs;
		SPI_MasterDeinit(&spi0Instance);
		SPI_MasterInit(&spi0Instance, &spi0MasterConfig[cs]);
	}else{

	}
	return SPI_MasterTransferBlocking(&spi0Instance, u8_tx_buff, u8_rx_buff, u16_len, (uint16_t)u32_timeout);
}

Leon