SPI does not work when initializing multiple chip select pins

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

SPI does not work when initializing multiple chip select pins

跳至解决方案
4,041 次查看
VolcanicCheese
Contributor III

I used freertos_lpspi_b2b_master example code to implement the SPI into my project. When I try to initialize 3 different handlers for 3 different chip select pin for LPSI1, it gets stuck trying to finish the transfer, even if I am only using one of the handler. The problem does not persist if I initialize one handler only. It happens soon as I initialize more than one handler. 

0 项奖励
回复
1 解答
3,959 次查看
jeremyzhou
NXP Employee
NXP Employee

Hi,
Thanks for your reply.
Please try the modified the master_task function.

static void master_task(void *pvParameters)
{
    LPSPI1_init();
    SPI_transfer(&spi1_handle, &spi1_config, masterSendBuffer, masterReceiveBuffer, 16);
    LPSPI2_init();
	SPI_transfer(&spi2_handle, &spi2_config, masterSendBuffer, masterReceiveBuffer, 16);
    LPSPI3_init();
	SPI_transfer(&spi3_handle, &spi3_config, masterSendBuffer, masterReceiveBuffer, 16);
    uint32_t errorCount;
    uint32_t i;

    PRINTF("EXPECTED: \n");
    for (i = 0; i < 16; i++)
    	{
    		/* Print 16 numbers in a line */
    		if ((i % 0x08U) == 0U)
    		{
    			PRINTF("\r\n");
    		}
    		PRINTF(" %02X", slaveSendBuffer[i]);
    	}
    	PRINTF("\r\n");

        PRINTF("RECEIVED: \n");

    for (i = 0; i < 16; i++)
	{
		/* Print 16 numbers in a line */
		if ((i % 0x08U) == 0U)
		{
			PRINTF("\r\n");
		}
		PRINTF(" %02X", masterReceiveBuffer[i]);
	}
	PRINTF("\r\n");

    errorCount = 0;
    for (i = 0; i < 16; i++)
    {
        if (slaveSendBuffer[i] != masterReceiveBuffer[i])
        {
            errorCount++;
        }
    }

    if (errorCount == 0)
    {
        PRINTF("LPSPI transfer all data matched !\r\n");
    }
    else
    {
        PRINTF("Error occurred in LPSPI transfer !\r\n");
    }

    vTaskSuspend(NULL);
}


Looking forward to your reply.
Have a great day,
TIC

-------------------------------------------------------------------------------
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.
-------------------------------------------------------------------------------

在原帖中查看解决方案

5 回复数
4,035 次查看
jeremyzhou
NXP Employee
NXP Employee

Hi,
Thank you for your interest in NXP Semiconductor products and for the opportunity to serve you.
I think I need more information, so I was wondering if you can introduce the testing process, such as code modification, board, issue, etc.
Have a great day,
TIC

-------------------------------------------------------------------------------
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 项奖励
回复
4,012 次查看
VolcanicCheese
Contributor III

So I first test out using the project with one handler to my project and that worked fine without issue. It only became an issue once I initialized multiple handlers. The only time problem arises when multiple handlers are initialize. Everything else works fine. This is my code below:


/***********************************************************************************************************************
* Included files
**********************************************************************************************************************/
#include <peripherals.h>

/* Freescale includes. */
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"
#include "fsl_gpt.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_lpspi.h"


/*
*
* SPI 1
*
*/


#define TRANSFER_SIZE (512U) /*! Transfer dataSize.*/
#define TRANSFER_BAUDRATE (500000UL) /*! Transfer baudrate - 500k */
#define LPSPI_CLOCK_SOURCE_SELECT (1U)
#define LPSPI_CLOCK_SOURCE_DIVIDER (7U)
#define LPSPI_CLOCK_FREQ (CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk) / (LPSPI_CLOCK_SOURCE_DIVIDER + 1U))

//#define LPSPI_MASTER_PCS_FOR_INIT (kLPSPI_Pcs3)
//#define LPSPI_MASTER_PCS_FOR_TRANSFER (kLPSPI_MasterPcs3)

uint8_t masterReceiveBuffer[TRANSFER_SIZE] = {0};
uint8_t masterSendBuffer[TRANSFER_SIZE] = {0};
uint8_t slaveSendBuffer[TRANSFER_SIZE] = {0};

lpspi_rtos_handle_t spi_m_rwa1_handle;
lpspi_rtos_handle_t spi_m_rwa2_handle;
lpspi_rtos_handle_t spi_m_rwa3_handle;

lpspi_master_config_t spi_master_rwa1_config;
lpspi_master_config_t spi_master_rwa2_config;
lpspi_master_config_t spi_master_rwa3_config;

static void LPSPI1_RWA1_init(void) {

LPSPI_MasterGetDefaultConfig(&spi_master_rwa1_config);

spi_master_rwa1_config.baudRate = TRANSFER_BAUDRATE;
spi_master_rwa1_config.bitsPerFrame = 8U;
spi_master_rwa1_config.cpol = kLPSPI_ClockPolarityActiveHigh;
spi_master_rwa1_config.cpha = kLPSPI_ClockPhaseFirstEdge;
spi_master_rwa1_config.direction = kLPSPI_MsbFirst;
spi_master_rwa1_config.pcsToSckDelayInNanoSec = 1000U;
spi_master_rwa1_config.lastSckToPcsDelayInNanoSec = 1000U;
spi_master_rwa1_config.betweenTransferDelayInNanoSec = 1000U;
#if DEV_BOARD
spi_master_rwa1_config.whichPcs = kLPSPI_Pcs3;
#else
spi_master_rwa1_config.whichPcs = kLPSPI_Pcs3;
#endif
spi_master_rwa1_config.pcsActiveHighOrLow = kLPSPI_PcsActiveLow;
spi_master_rwa1_config.pinCfg = kLPSPI_SdiInSdoOut;
spi_master_rwa1_config.dataOutConfig = kLpspiDataOutRetained;

uint32_t sourceClock;

sourceClock = LPSPI_CLOCK_FREQ;
if(kStatus_Success != LPSPI_RTOS_Init(&spi_m_rwa1_handle, LPSPI1, &spi_master_rwa1_config, sourceClock)) {
PRINTF("SPI Master initialization failed! \r\n");
}
}

#if !DEV_BOARD
static void LPSPI1_RWA2_init(void) {

LPSPI_MasterGetDefaultConfig(&spi_master_rwa2_config);

spi_master_rwa2_config.baudRate = TRANSFER_BAUDRATE;
spi_master_rwa2_config.bitsPerFrame = 8U;
spi_master_rwa2_config.cpol = kLPSPI_ClockPolarityActiveHigh;
spi_master_rwa2_config.cpha = kLPSPI_ClockPhaseFirstEdge;
spi_master_rwa2_config.direction = kLPSPI_MsbFirst;
spi_master_rwa2_config.pcsToSckDelayInNanoSec = 1000U;
spi_master_rwa2_config.lastSckToPcsDelayInNanoSec = 1000U;
spi_master_rwa2_config.betweenTransferDelayInNanoSec = 1000U;
spi_master_rwa2_config.whichPcs = kLPSPI_Pcs1; //TODO: Replace with RWA2 chip select!
spi_master_rwa2_config.pcsActiveHighOrLow = kLPSPI_PcsActiveLow;
spi_master_rwa2_config.pinCfg = kLPSPI_SdiInSdoOut;
spi_master_rwa2_config.dataOutConfig = kLpspiDataOutRetained;

uint32_t sourceClock;

sourceClock = LPSPI_CLOCK_FREQ;
if(kStatus_Success != LPSPI_RTOS_Init(&spi_m_rwa2_handle, LPSPI1, &spi_master_rwa2_config, sourceClock)) {
PRINTF("SPI Master initialization failed! \r\n");
}
}

static void LPSPI1_RWA3_init(void) {

LPSPI_MasterGetDefaultConfig(&spi_master_rwa3_config);

spi_master_rwa3_config.baudRate = TRANSFER_BAUDRATE;
spi_master_rwa3_config.bitsPerFrame = 8U;
spi_master_rwa3_config.cpol = kLPSPI_ClockPolarityActiveHigh;
spi_master_rwa3_config.cpha = kLPSPI_ClockPhaseFirstEdge;
spi_master_rwa3_config.direction = kLPSPI_MsbFirst;
spi_master_rwa3_config.pcsToSckDelayInNanoSec = 1000U;
spi_master_rwa3_config.lastSckToPcsDelayInNanoSec = 1000U;
spi_master_rwa3_config.betweenTransferDelayInNanoSec = 1000U;
spi_master_rwa3_config.whichPcs = kLPSPI_Pcs2;; //TODO: Replace with RWA3 chip select!
spi_master_rwa3_config.pcsActiveHighOrLow = kLPSPI_PcsActiveLow;
spi_master_rwa3_config.pinCfg = kLPSPI_SdiInSdoOut;
spi_master_rwa3_config.dataOutConfig = kLpspiDataOutRetained;

uint32_t sourceClock;

sourceClock = LPSPI_CLOCK_FREQ;
if(kStatus_Success != LPSPI_RTOS_Init(&spi_m_rwa3_handle, LPSPI1, &spi_master_rwa3_config, sourceClock)) {
PRINTF("SPI Master initialization failed! \r\n");
}
}
#endif

//SPI transfer lpspi_master_config_t config
void SPI_transfer(lpspi_rtos_handle_t * handler, lpspi_master_config_t * config, uint8_t * txBuffer, uint8_t * rxBuffer, size_t transferSize)
#endif
{
lpspi_transfer_t masterXfer;
status_t status;

//Start master transfer
masterXfer.txData = txBuffer;
masterXfer.rxData = rxBuffer;
masterXfer.dataSize = transferSize;
masterXfer.configFlags = config->whichPcs << LPSPI_MASTER_PCS_SHIFT | kLPSPI_MasterPcsContinuous | kLPSPI_SlaveByteSwap;


if (status == kStatus_Success)
{
PRINTF("LPSPI master transfer completed successfully.\r\n");
}
else
{
PRINTF("LPSPI master transfer completed with error.\r\n");
}

#if SPI_TEST
uint32_t errorCount;
uint32_t i;

PRINTF("EXPECTED: \n");
for (i = 0; i < 16; i++)
{
/* Print 16 numbers in a line */
if ((i % 0x08U) == 0U)
{
PRINTF("\r\n");
}
PRINTF(" %02X", slaveSendBuffer[i]);
}
PRINTF("\r\n");

PRINTF("RECEIVED: \n");

for (i = 0; i < 16; i++)
{
/* Print 16 numbers in a line */
if ((i % 0x08U) == 0U)
{
PRINTF("\r\n");
}
PRINTF(" %02X", masterReceiveBuffer[i]);
}
PRINTF("\r\n");

errorCount = 0;
for (i = 0; i < 15; i++)
{
if (slaveSendBuffer[i] != masterReceiveBuffer[i+1])
{
errorCount++;
}
}

if (errorCount == 0)
{
PRINTF("LPSPI transfer all data matched !\r\n");
}
else
{
PRINTF("Error occurred in LPSPI transfer !\r\n");
}
}


/***********************************************************************************************************************
* Initialization functions
**********************************************************************************************************************/
void BOARD_InitPeripherals(void)
{
/*Set clock source for LPSPI*/
CLOCK_SetMux(kCLOCK_LpspiMux, LPSPI_CLOCK_SOURCE_SELECT);
CLOCK_SetDiv(kCLOCK_LpspiDiv, LPSPI_CLOCK_SOURCE_DIVIDER);

NVIC_SetPriority(LPSPI1_IRQn, 3);

/* Initialize data in transfer buffers */
for (int i = 0; i < TRANSFER_SIZE; i++)
{
masterSendBuffer[i] = i % 256;

slaveSendBuffer[i] = ~masterSendBuffer[i];//checks match with slave response
}

LPSPI1_RWA1_init();
LPSPI1_RWA3_init();
LPSPI1_RWA2_init();
}

 

0 项奖励
回复
3,982 次查看
jeremyzhou
NXP Employee
NXP Employee

Hi,
After having a brief review of the code, I don't find something wrong actually.
So I was wondering if you can upload the whole demo project, as I'd like to try to test with it.
BTW, I'm not very clear about the phenomenon you described above "it gets stuck trying to finish the transfer, even if I am only using one of the handler.", whether you can introduce it in more detail.
Looking forward to your reply.
Have a great day,
TIC

-------------------------------------------------------------------------------
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 项奖励
回复
3,967 次查看
VolcanicCheese
Contributor III

When I try to init multiple chip select the SPI transfer seems to fail. This is the code below. Thank you for the help!

0 项奖励
回复
3,960 次查看
jeremyzhou
NXP Employee
NXP Employee

Hi,
Thanks for your reply.
Please try the modified the master_task function.

static void master_task(void *pvParameters)
{
    LPSPI1_init();
    SPI_transfer(&spi1_handle, &spi1_config, masterSendBuffer, masterReceiveBuffer, 16);
    LPSPI2_init();
	SPI_transfer(&spi2_handle, &spi2_config, masterSendBuffer, masterReceiveBuffer, 16);
    LPSPI3_init();
	SPI_transfer(&spi3_handle, &spi3_config, masterSendBuffer, masterReceiveBuffer, 16);
    uint32_t errorCount;
    uint32_t i;

    PRINTF("EXPECTED: \n");
    for (i = 0; i < 16; i++)
    	{
    		/* Print 16 numbers in a line */
    		if ((i % 0x08U) == 0U)
    		{
    			PRINTF("\r\n");
    		}
    		PRINTF(" %02X", slaveSendBuffer[i]);
    	}
    	PRINTF("\r\n");

        PRINTF("RECEIVED: \n");

    for (i = 0; i < 16; i++)
	{
		/* Print 16 numbers in a line */
		if ((i % 0x08U) == 0U)
		{
			PRINTF("\r\n");
		}
		PRINTF(" %02X", masterReceiveBuffer[i]);
	}
	PRINTF("\r\n");

    errorCount = 0;
    for (i = 0; i < 16; i++)
    {
        if (slaveSendBuffer[i] != masterReceiveBuffer[i])
        {
            errorCount++;
        }
    }

    if (errorCount == 0)
    {
        PRINTF("LPSPI transfer all data matched !\r\n");
    }
    else
    {
        PRINTF("Error occurred in LPSPI transfer !\r\n");
    }

    vTaskSuspend(NULL);
}


Looking forward to your reply.
Have a great day,
TIC

-------------------------------------------------------------------------------
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.
-------------------------------------------------------------------------------