spi sd card through free rtos

ajithcs818 wrote on Monday, March 26, 2018:

Hai everyone ,

the code works without free rtos .but when integrating with free rtos it doesnt

FATFS fs;
FIL fil;
FRESULT res;
STM_SPI_Init();

if(f_mount(&fs, “”,1)== FR_OK)
{
GPIO_SetBits(GPIOC, GPIO_Pin_15);
}

if(f_open(&fil, “cardfile.txt”, FA_OPEN_ALWAYS | FA_WRITE)== FR_OK)
{
GPIO_SetBits(GPIOA, GPIO_Pin_1);
}

if(f_close(&fil)== FR_OK)
{
GPIO_SetBits(GPIOC, GPIO_Pin_14);
//vTaskDelay(1000/portTICK_RATE_MS);
}

then i put it into a simple task

void vTasksFree(void *p)
{
FATFS fs;
FIL fil;
FRESULT res;
STM_SPI_Init();

if(f_mount(&fs, “”,1)== FR_OK)
{
GPIO_SetBits(GPIOC, GPIO_Pin_15);
}

if(f_open(&fil, “cardfile.txt”, FA_OPEN_ALWAYS | FA_WRITE)== FR_OK)
{
GPIO_SetBits(GPIOA, GPIO_Pin_1);
}

if(f_close(&fil)== FR_OK)
{
GPIO_SetBits(GPIOC, GPIO_Pin_14);
//vTaskDelay(1000/portTICK_RATE_MS);
}

}

it is not working can anyone help me out.
srry if iam basically wrong.
thanks and regards
ajith

rtel wrote on Monday, March 26, 2018:

the code works without free rtos .but when integrating with free rtos it
doesnt

Can you elaborate on this - how does it not work? I’m going to assume
it still compiles, but what happens then? The card doesn’t mount? You
can’t read from the card? The card gets corrupted? What?

Are you trying to access the functions from two tasks at the same time?
Have you configured FATFS (which, by the way, is not our software, so we
can’t support it accurately) for multi-threaded use - there is a #define
you have to set I think.

ajithcs818 wrote on Tuesday, March 27, 2018:

hai everyone ,
happy to inform you that i got issue solved. iam posting this msg so that any others with same problem will find it helpful.
i was using this spi bus to control my fatfs file system. so this bus was not available to all functions iam calling so what i did was ,i include mutex function with it . So resources will be available till i gave the semaphore give . iam posting the same with you . if iam iam wrong in any ways please do notify me .
with regards ajith

void vTasksFree(void *p)
{
FATFS fs;
FIL fil;
FRESULT res;
FILINFO MyFileInfo;
DIR MyDirectory;
UINT BytesWritten;
UINT BytesRead;
for(;:wink:
{

if( xSemaphore != NULL )
{
//vTaskDelay(100/portTICK_RATE_MS);
if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
STM_SPI_Init();

USART_PutStr(" 8. Delete A File\r\n");
if(f_mount(&fs, “”,1)== FR_OK)
{
GPIO_SetBits(GPIOC, GPIO_Pin_15);

}
if(f_open(&fil, “carddaa.txt”, FA_OPEN_ALWAYS | FA_WRITE)== FR_OK)
{
GPIO_SetBits(GPIOA, GPIO_Pin_1);

res = f_write(&fil, “hello”, 4, &BytesRead);
USART_PutStr(" f_write(): “);
USART_PutHexByte((unsigned char) res);
USART_PutStr(” BytesRead: 0x");
USART_PutHexWord(BytesRead);
}
if(f_close(&fil)==FR_OK)
{
GPIO_SetBits(GPIOC, GPIO_Pin_14);
//vTaskDelay(1000/portTICK_RATE_MS);

}
xSemaphoreGive( xSemaphore );
}
}
vTaskDelay(10/portTICK_RATE_MS);
}

}
void vATask( void * pvParameters )
{
//USART_PutStr(" 8. Delete A File\r\n");
xSemaphore = xSemaphoreCreateMutex();
vTaskDelay(10000/portTICK_RATE_MS);

}