cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a file on STM32F7 – Returns NULL

B_D_R
Associate III

Hello Guys,

I’m working on a project using the STM32F767 evaluation board with STM32CubeIDE and FreeRTOS. In this project, I want to create a .csv file inside the MCU’s storage, write data to it, and then share that file over Ethernet.

So far, I have successfully configured the Ethernet communication and can transfer strings between the MCU and PC. However, when I try to create or open the .csv file, my fopen() call returns NULL, and the file is not created.

Code is attached with the post. Please help me.

Thanks.

FILE *f;   //Golobal declare for file	

void create(void )        //Function to create file,write
{ 
    {
        HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);
        f = fopen("trial.txt","w");
        if(f == NULL)
        {
            printf("\nCann't open file");
            HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_RESET);
        }
        else{
            fprintf (f, "Hello World ");
            fclose (f);
        }
    }
}

 

4 REPLIES 4
Andrew Neil
Super User

You haven't said what filesystem you're using - FatFS? FileX? other?

 

Is your filesystem correctly/successfully initialising, mounting the device, etc?

 

As always, start from a known-good example, which just does the file stuff - no other complications (ethernet, etc).

 

Here's a couple of Knowledge Base articles on setting up a filesystem on STM32:

How to create a file system on a SD card using STM32CubeIDE.

How to use FileX with eMMC for file system management.

via: https://community.st.com/t5/forums/searchpage/tab/message?filter=includeTkbs&q=STM32F7%20file%20system&noSynonym=false&include_tkbs=true&collapse_discussion=true 

 

Edit: fixed 1st link.

 

PS:

 


@B_D_R wrote:

using the STM32F767 evaluation board


You mean the NUCLEO-F767ZI ?

https://www.st.com/en/evaluation-tools/nucleo-f767zi.html 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

hello @Andrew Neil 

In all the above given examples file was created with in the SD card/USB. Is it possible to make file within the internal flash of controller and then transfer it to the ethernet/USB/Wifi.

Thanks.

Ozone
Principal

Because you almost certainly call the wrong function.

The declaration "FILE *f;" suggests you are using FatFS.
However, the corresponding function to open a file in FatFS is called "f_open()".

The fopen() function is a fully Posix compatible function for buffered file access. There is no such thing in FatFS.

You still haven't said what filesystem you are using.

As @Ozone showed, this matters!

 

The FatFS documentation says this:

Media Access Interface
Since FatFs module is the Filesystem Layer independent of platforms and storage media, it is completely separated from the physical devices, such as memory card, harddisk and any type of storage device. The storage device control module is not a part of FatFs module and it needs to be provided by implementer. FatFs controls the storage devices via a simple media access interface shown below. Also sample implementations for some platforms are available in the downloads.

So, yes - it can work on any storage medium.

 

This has come up before - try a forum search.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.