cancel
Showing results for 
Search instead for 
Did you mean: 

How to open files on Host PC with fopen on STM32L5

Domy_ST
Associate III

Title edited to clarify that this is about accessing a file located on a Host PC


hello ST Community,

I'm trying to open a binary file with the fopen function for a STM32L5 microcontroller, but it always gives me a null value. The code is like this:

char filepath[256];
sprintf(filepath,"%s","C:\\fileBIN.bin");
		  
FILE *file;
file = fopen(filepath,"rb");
if(file == NULL)
{
   printf("Error occurred opening file. \r\n");
}
else
{
   printf("File opened successfully. \r\n");
}

 

It always gives me a null value but the path exists and is correct. What's wrong?

Thanks for supports

 

1 ACCEPTED SOLUTION

Accepted Solutions

You never said that's what you're trying to achieve in this thread!

 

In that case, you will need to have something on the PC which sends the data to the Target.

The PC pushes to the Target - the Target doesn't pull from the PC.

 

CubeProgrammer can do this, and is ideal for use with the STM32 System Bootloader:

AN2606Introduction to system memory boot mode on STM32 MCUs

AN3155How to use USART protocol in bootloader on STM32 MCUs

 

If you're making a custom bootloader, then you'll have to define your own protocol; eg,

AN4657STM32 in-application programming (IAP) using the USART - uses the YMODEM protocol, which is widely supported by terminal emulators & serial comms libraries/frameworks.

 

There's also ST Open Bootloader - also works with CubeProgrammer

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.

View solution in original post

23 REPLIES 23
AScha.3
Super User

Hi,

no info: what you use + did...

use ? FATFS ?

what drive ?  C ??

interface ? SD-card ?

did you mount the volume ?

If you feel a post has answered your question, please click "Accept as Solution".

@AScha.3 wrote:

no info: what you use + did...


Indeed!

 


@AScha.3 wrote:

use ? FATFS ?


I don't think it's FatFs:  that has f_open rather than fopen, and (AFAIK) uses drive numbers - not letters ?

Path Names on the FatFs API

 

@Domy_ST please see: How to write your question to maximize your chances to find a solution

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.
Andrew Neil
Super User

@Domy_ST wrote:

the fopen function ... always gives me a null value.


In that case, errno (or something similar) should be set to an error code showing what went wrong...

 

As previously noted, you need to give a lot more details about your setup.

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.
Saket_Om
ST Employee

Hello @Domy_ST 

If you want to read file system from SD card, you need ti use FATFS library.

Please refer to the examples below:

STM32CubeL5/Projects/STM32L552E-EV/Applications/FatFs at master · STMicroelectronics/STM32CubeL5 · GitHub

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om

@Andrew Neil wrote:


I don't think it's FatFs: 


Also doesn't look like FileX: that appears to have fx_file_open

 

@Domy_ST - You're not trying to open a file that's on your Host PC, are you?

For that, you'd need Semihosting

How to use semihosting with STM32CubeIDE and STM32

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.

@Saket_Om wrote:

you need to use FATFS library.


* other embedded file system libraries are available

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.

@Andrew Neil 

no the file is exactly on the local PC the fopen function does not find it

If you had two PCs sitting side-by-side on your desk, one PC would not be able to see files on the other - unless you add networking of some sort.

Similarly, your microcontroller can't see any resources on your PC unless you provide suitable connectivity.

As mentioned earlier, Semihosting  may be an option for you?

 

I think you need to take a step back, and explain what you're actually trying to achieve here - this sounds very much like an X-Y problem.

Describe the goal, not the step

 

PS:

Is there a clue in the filename: "C:\\fileBIN.bin" ?

Are you, perhaps, trying to do a firmware update?

As noted in your previous thread, you'd use a bootloader for that - not try to access the file on the Host PC

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.

@Andrew Neil 

 

The goal I'm trying to achieve is simply to read the file contents in binary and assign 32 bits of hex to one array parameter (uint32_t) at a time and overwrite it to flash memory (sort of bootloader). I get the file path by receiving via UART.