AnsweredAssumed Answered

FatFS append file only once opened

Question asked by Lukasz Przenioslo on Dec 10, 2015
Latest reply on Dec 10, 2015 by Lukasz Przenioslo
Hello there,
I have a logger application on a STM32F407 device. I am saving logs to sd card. Generally it works, but I would like to make the procedure a bit faster by opening the file only once at the begining of the program. The problem is when I do that, I dont get anything saved...

This is my "append to file" procedure:
static HAL_StatusTypeDef log_AppendFileSdCard(uint8_t* text, uint32_t length)
{
    FRESULT res;
    uint32_t bytesWritten = 0;
 
    if (FR_OK != f_open(&log_File, (const TCHAR*)logName, FA_WRITE))
        return HAL_ERROR;
 
    if (FR_OK != f_lseek(&log_File, f_size(&log_File)))
    {
        return HAL_ERROR;
    }
 
    res = f_write(&log_File, text, length, (void *)&bytesWritten);
    if((bytesWritten > 0) && (res == FR_OK))
    {
        if (FR_OK != f_close(&log_File))
        {
            return HAL_ERROR;
        }
    }
    else
    {
        return HAL_ERROR;
    }
 
    return HAL_OK;
}

If for example, I create the file and leave it opened, and remove the open/ close functions from my procedure:
static HAL_StatusTypeDef log_AppendFileSdCard(uint8_t* text, uint32_t length)
{
    FRESULT res;
    uint32_t bytesWritten = 0;
 
    if (FR_OK != f_lseek(&log_File, f_size(&log_File)))
    {
        return HAL_ERROR;
    }
 
    res = f_write(&log_File, text, length, (void *)&bytesWritten);
    if((bytesWritten > 0) && (res == FR_OK))
    {
              // dont close
    }
    else
    {
        return HAL_ERROR;
    }
 
    return HAL_OK;
}

There is nothing saved to the file, even though write doesnt return any error and the index of the file struct is incremented. What could be the case here, am I doing something wrong?
I would apreciate all help!

Outcomes