Hello,
I am currently trying to run a Mass Storage Device example on FRDM-MCXN974 with SD card support and FreeRTOS. I managed to get the standard example to run fine. The code contains two tasks APP_task and USB_DeviceTask, I am trying to reduce this code to only one task, which I have designed as follows:
int main(void)
{
/* Initialize board hardware. */
APP_InitBoard();
USB_DeviceApplicationInit();
usb_echo("Available heap size before task creation: %d bytes\r\n", xPortGetFreeHeapSize());
if (g_msc.deviceHandle)
{
if (xTaskCreate(USB_DeviceTask, /* pointer to the task */
(char const *)"usb device task", /* task name for kernel awareness debugging */
9000L / sizeof(portSTACK_TYPE), /* task stack size */
g_msc.deviceHandle, /* optional task startup argument */
4, /* initial priority */
&g_msc.device_task_handle /* optional task handle to create */
) != pdPASS)
{
usb_echo("usb device task create failed!\r\n");
return 1;
}
}
vTaskStartScheduler();
while (1)
{
;
}
return 1;
}
But It always end with fard-fault in USB_DeviceApplicationInit() as in the attached picture.
The original code looked like this:
int main(void)
{
/* board initialization... */
/* task... */
if (xTaskCreate(APP_task, /* pointer to the task */
(char const *)"app task", /* task name for kernel awareness debugging */
5000L / sizeof(portSTACK_TYPE), /* task stack size */
&g_msc, /* optional task startup argument */
3, /* initial priority */
&g_msc.application_task_handle /* optional task handle to create */
) != pdPASS)
{
usb_echo("app task create failed!\r\n");
}
vTaskStartScheduler();
return 1;
}
void USB_DeviceTask(void *handle)
{
while (1U)
{
USB_DeviceTaskFn(handle);
}
}
void APP_task(void *handle)
{
USB_DeviceApplicationInit();
#if USB_DEVICE_CONFIG_USE_TASK
if (g_msc.deviceHandle)
{
if (xTaskCreate(USB_DeviceTask, /* pointer to the task */
(char const *)"usb device task", /* task name for kernel awareness debugging */
5000L / sizeof(portSTACK_TYPE), /* task stack size */
g_msc.deviceHandle, /* optional task startup argument */
5, /* initial priority */
&g_msc.device_task_handle /* optional task handle to create */
) != pdPASS)
{
usb_echo("usb device task create failed!\r\n");
return;
}
}
while (1)
{
}
}
Did I miss anything? Can I somehow reduce the code just to one task?
Thank you very much.
解決済! 解決策の投稿を見る。
hi,Doly02
I don't think it's possible that the SD_Init () function is still connected to the FreeRTOS task resource, and the program ends up with a hard failure, and SD card initialization is done in the USB_DeviceApplicationInit() function. You can refer to the frdmmcxn947_dev_msc_disk_bm demo program where the USB_DeviceApplicationInit() function is initialized in main.
If you use SD cards in Freertos, ensure that the tasks have enough stack space and do not conflict with other tasks, ensure that the initialization and use of the SD card is thread-safe, and you can use the Mutex provided by FreeRTOS to prevent problems caused by multiple tasks accessing the SD card at the same time. If the SD card initialization is done in one task, and other tasks need to wait for the initialization to complete before using the SD card, then Semaphore or other synchronization mechanism should be used to coordinate the tasks.
BR
Xu Zhang
hi,Doly02
Thank you for your interest in NXP Semiconductor products and the opportunity to serve you, I will gladly help you with this.
You can try to modify the frdmmcxn947_dev_msc_disk_freertos demo program in the way shown in the picture.
Wish it helps you.
If you still have question about it,please kindly let me know.
BR
Zhang Xu
hi,Doly02
I don't think it's possible that the SD_Init () function is still connected to the FreeRTOS task resource, and the program ends up with a hard failure, and SD card initialization is done in the USB_DeviceApplicationInit() function. You can refer to the frdmmcxn947_dev_msc_disk_bm demo program where the USB_DeviceApplicationInit() function is initialized in main.
If you use SD cards in Freertos, ensure that the tasks have enough stack space and do not conflict with other tasks, ensure that the initialization and use of the SD card is thread-safe, and you can use the Mutex provided by FreeRTOS to prevent problems caused by multiple tasks accessing the SD card at the same time. If the SD card initialization is done in one task, and other tasks need to wait for the initialization to complete before using the SD card, then Semaphore or other synchronization mechanism should be used to coordinate the tasks.
BR
Xu Zhang