Skip to main content
Associate III
March 30, 2026
Solved

How to open files on Host PC with fopen on STM32L5

  • March 30, 2026
  • 5 replies
  • 727 views

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

 

Best answer by Andrew Neil

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

5 replies

AScha.3
Super User
March 30, 2026

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""."
Andrew Neil
Super User
March 30, 2026

@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
March 30, 2026

@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.
Andrew Neil
Super User
March 30, 2026

@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.
Technical Moderator
March 30, 2026

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
Super User
March 30, 2026

@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.
KnarfB
Super User
March 31, 2026

Opening a file on the host or doing other host-related system calls is called semi-hosting and need special steps:
How to use semihosting with STM32CubeIDE and STM32 - STMicroelectronics Community

Semihosting - SEGGER Knowledge Base

Haven't used it myself for a long time however.

The downside is, that this requires a driver on the host and is said to be slow. 

hth

KnarfB

Andrew Neil
Super User
March 31, 2026

@KnarfB wrote:

downside is, that this requires a driver on the host and is said to be slow. 


Indeed.

@Domy_ST it also relies upon the debug interface - so you might just as well use that directly to do the programming!

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
March 31, 2026

@Domy_ST  I think your question about getting the bin file into a bootloader is now solved, so please mark the solution on whichever post you feel provided the best answer.

 

New question about issues with Semihosting split to a separate thread: Semihosting error: "Protocol error with Rcmd".

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.
Domy_STAuthor
Associate III
March 31, 2026

hi @Andrew Neil 

I haven't solved it yet. Before proceeding with YMODEM, I was about to try the semi-hosting solution, but there's a problem with rcmd. As soon as I fix it, I'll mark it as solved.

Andrew Neil
Super User
March 31, 2026

The original question of "How to open a file on a PC from an STM32" has been answered (and turned out to be the wrong question anyhow).

"How to do YMODEM" and "How to do Semihosting", etc,  are separate follow-on questions.

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.