Regarding Semaphore Give and take for LPSPI1

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

Regarding Semaphore Give and take for LPSPI1

1,044 Views
Jayaganesh
Contributor II

Hi,

I am currently encountering an issue with preempting from a semaphore during a semaphoreGive operation. The process seems to get stuck when data starts arriving from the master. Despite my efforts, I have been unable to identify the root cause or resolve the blocking behavior.

Could you please provide guidance or suggest possible solutions to address this issue? Any insights or recommendations would be greatly appreciated, as it is impacting the system's performance.

I have set task priority to 4 and NVIC_SetPriority to 2+1!

Looking forward to your prompt assistance.



#include
"FreeRTOS.h"

#include "task.h"

#include "queue.h"

#include "timers.h"

#include "semphr.h"

 

/* Freescale includes. */

#include "fsl_device_registers.h"

#include "fsl_debug_console.h"

#include "pin_mux.h"

#include "clock_config.h"

#include "board.h"

#include "FreeRTOSConfig.h"

#include "mv_lpspi_edma.h"

 

/*******************************************************************************

* Definitions

******************************************************************************/

#define TASK_PRIO (configMAX_PRIORITIES - 1)

#define CONSUMER_LINE_SIZE 3

SemaphoreHandle_t xSemaphore_producer;

SemaphoreHandle_t xSemaphore_consumer;

 

SemaphoreHandle_t xSemaphore;

lpspi_transfer_t slaveXfer;

AT_NONCACHEABLE_SECTION_INIT(uint8_t slaveRxData[TRANSFER_SIZE]) = {0};

AT_NONCACHEABLE_SECTION_INIT(uint8_t slaveTxData[TRANSFER_SIZE]) = {0U};

 

UBaseType_t priority;

 

//volatile bool isTransferCompleted = false; // Added volatile keyword

 

/*******************************************************************************

* Prototypes

******************************************************************************/

static void mv_lpspi_edma_task(void *pvParameters);

 

/*******************************************************************************

* Code

******************************************************************************/

 

 

void LPSPI_SlaveUserCallback(LPSPI_Type *base, lpspi_slave_edma_handle_t *handle, status_t status, void *userData)

{

if (status == kStatus_Success)

{

PRINTF("\nLPSPI_SlaveUserCallback\r\n");

//priority = uxTaskPriorityGet(NULL);

 

// Print the priority

//xSemaphoreGive(xSemaphore);

BaseType_t xHigherPriorityTaskWoken = pdFALSE;

 

xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);

portYIELD_FROM_ISR(xHigherPriorityTaskWoken);

}

// BaseType_t xHigherPriorityTaskWoken = pdTRUE;

// xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);

// portYIELD_FROM_ISR(xHigherPriorityTaskWoken);

 

 

// Get the priority of the current task

 

 

 

 

isTransferCompleted = true;

}

 

/*! @brief Main function */

int main(void)

{

BOARD_ConfigMPU();

BOARD_InitBootPins();

BOARD_InitBootClocks();

BOARD_InitDebugConsole();

 

gpio_pin_config_t USER_LED_config = {

.direction = kGPIO_DigitalOutput,

.outputLogic = 0U,

.interruptMode = kGPIO_NoIntmode

};

GPIO_PinInit(GPIO1, 16u, &USER_LED_config);

 

xSemaphore = xSemaphoreCreateBinary();

if (xSemaphore == NULL)

{

PRINTF("Semaphore creation failed.\r\n");

while (1);

}

//xSemaphoreTake(xSemaphore, 0); // Clear any initial signal

 

if (xTaskCreate(mv_lpspi_edma_task, "mv_lpspi_edma_task", configMINIMAL_STACK_SIZE + 128, NULL, TASK_PRIO, NULL) != pdPASS)

{

PRINTF("mv_lpspi_edma_task creation failed!\r\n");

while (1);

}

else

{

PRINTF("\n\rmv_lpspi_edma_task created.\r\n");

}

 

mv_lpspi_edma_init();

 

NVIC_SetPriority(LPSPI1_IRQn, configMAX_SYSCALL_INTERRUPT_PRIORITY +1); // Set proper priority

 

vTaskStartScheduler();

 

for (;;);

}

 

/*! @brief mv_lpspi_edma_task. */

static void mv_lpspi_edma_task(void *pvParameters)

{

while (1)

{

PRINTF("\r\n Slave is running...\r\n");

 

for (int i = 0U; i < TRANSFER_SIZE; i++)

{

slaveRxData[i] = 0U;

}

 

isTransferCompleted = false;

 

slaveXfer.txData = slaveTxData;

slaveXfer.rxData = slaveRxData;

slaveXfer.dataSize = TRANSFER_SIZE;

slaveXfer.configFlags = EXAMPLE_LPSPI_SLAVE_PCS_FOR_TRANSFER | kLPSPI_SlaveByteSwap;

 

LPSPI_SlaveTransferEDMA(EXAMPLE_LPSPI_SLAVE_BASEADDR, &g_s_edma_handle, &slaveXfer);

 

// while (!isTransferCompleted)

// {

// }

 

 

 

// Get the priority of the current task

priority = uxTaskPriorityGet(NULL);

 

// Print the priority

PRINTF("Current task priority: %d\r\n", priority);

 

PRINTF("Before Take");

 

if (xSemaphoreTakeFromISR(xSemaphore, portMAX_DELAY) == pdTRUE)

{

PRINTF("lpspi1_edma consumed an item.\r\n");

}

PRINTF("After Take");

 

uint32_t firstInt = (slaveRxData[3] << 24) | (slaveRxData[2] << 16) | (slaveRxData[1] << | slaveRxData[0];

uint32_t secondInt = (slaveRxData[7] << 24) | (slaveRxData[6] << 16) | (slaveRxData[5] << | slaveRxData[4];

PRINTF("First 32-bit integer: 0x%08X\t\t", firstInt);

PRINTF("Second 32-bit integer: 0x%08X", secondInt);

}

}

 

Tags (2)
0 Kudos
Reply
2 Replies

1,010 Views
Gavin_Jia
NXP TechSupport
NXP TechSupport

Hi @Jayaganesh ,

Thanks for your interest in NXP MIMXRT series!

I briefly looked at your code and the interrupt handler function should be fine. But shouldn't xSemaphoreTakeFromISR() in the main function be changed to xSemaphoreTake() ? Also, please check if the semaphore was successfully released and acquired after the modification. For more details, you can refer to this example in the SDK: evkmimxrt1064_freertos_sem

Best regards,
Gavin

0 Kudos
Reply

1,017 Views
Jayaganesh
Contributor II
I'm using IMXRT1064
0 Kudos
Reply