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

 

23 REPLIES 23

As explained, you can't do that - the microcontroller has no access to the Host PC's file system

 


@Domy_ST wrote:

I get the file path by receiving it via UART.


Rather than sending the path via UART, send the data that you require!

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 purpose is that I can not pass data directly via UART but only .bin files and it is the microcontroller's job to read the file contents


@Domy_ST wrote:

I can not pass data directly via UART Of course you can!

Of course you can!

Sending the filename is just data - if you can send the file name, then you can equally send the content from the file.

 


@Domy_ST wrote:

it is the microcontroller's job to read the file contents


But, again, the microcontroller does not have any access to the PC's file system!

 

I think we're still stuck in an X-Y problem here: why do you want the SMT32 to read this file?

What is the purpose of the SMT32 reading this file?

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 Is it possible to send data via uart starting from the file name/path? Is there a code example?

 

You would need something on the PC to send it.

You could write something yourself, or use something ready made; eg, CubeProgrammer or a Terminal app.

 

You still haven't said why you want to do it - so impossible to be specific.

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 

as I specified in the previous post, I would like to make a sort of bootloader and then update the software to flash

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.

@Andrew Neil 

I need to develop a custom bootloader. I'll study the YMODEM protocol and let you know. Thanks a lot for the support.

You're welcome.

If your issue is resolved, please mark the solution on the post which provided the answer (not this one!)

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

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