USB Mass storage on STM32

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
Kisen
Posts: 73
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 4 times
Been thanked: 2 times
Contact:

USB Mass storage on STM32

Post by Kisen »

Hi,

I am beginning work on STM32 mass storage project. I want to make my device look like a flash drive on the PC so i can load and retrieve files. Eventually i would like to have this function as a firmware update tool and virtual com port depending on a set of circumstances.

For now i need to get the USB side up and running with some understanding in how it works.

I understand that i need to do the following:
USBD_INIT()
USBD_RegisterClass()
USBD_MSC_RegisterStorage()
USBD_Start

Following this i should get the device to detect in windows, create a drive and tell me the drive needs formatting.

I have a few examples that i am going to attempt to use to recreate in flowcode.
However i am struggling to wrap my head around a few aspects.

Code: Select all

#include "usb_device.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_msc.h"
#include "usbd_storage_if.h"

/* Return USBD_OK if the Battery Charging Detection mode (BCD) is used, else USBD_FAIL */
extern USBD_StatusTypeDef USBD_LL_BatteryCharging(USBD_HandleTypeDef *pdev);
/* USB Device Core handle declaration */
USBD_HandleTypeDef hUsbDeviceFS;

/* init function */				        
void MX_USB_DEVICE_Init(void)
{
  /* Init Device Library,Add Supported Class and Start the library*/
  USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
  USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC);
  USBD_MSC_RegisterStorage(&hUsbDeviceFS, &USBD_Storage_Interface_fops_FS);
  /* Verify if the Battery Charging Detection mode (BCD) is used : */
  /* If yes, the USB device is started in the HAL_PCDEx_BCD_Callback */
  /* upon reception of PCD_BCD_DISCOVERY_COMPLETED message. */
  /* If no, the USB device is started now. */
  if (USBD_LL_BatteryCharging(&hUsbDeviceFS) != USBD_OK) {
  USBD_Start(&hUsbDeviceFS);
  }
}
The main.c calls this MX_USB_DEVICE_Init(); function.

Within this function there are other functions that are called. They have variables that are being passed to them, but i dont know where they are coming from.

USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);

Some have & infront of them - &hUsbDeviceFS and others just appear to be variables - DEVICE_FS

I cant figure out what these are or where they are getting their values from, so i dont understand how to implement these in flowcode. If i were to create these as variables, then i dont ever give them a value, to pass on.

Am i misunderstanding what these are?

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: USB Mass storage on STM32

Post by mnf »

'&' (addressof) in C (or C++) passes the address of a variable (or object) - this maybe allows the function called to modify the value..

Martin

Kisen
Posts: 73
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: USB Mass storage on STM32

Post by Kisen »

mnf wrote:
Sat Sep 05, 2020 12:24 pm
'&' (addressof) in C (or C++) passes the address of a variable (or object) - this maybe allows the function called to modify the value..

Martin
So they are pointers? So in flowcode then I think I will need to have an array? I'm sure i have asked about pointers before and was told this was the best way.
I'm still struggling to see where the variables are getting values from tho to pass to the function.

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: USB Mass storage on STM32

Post by mnf »

By convention - so there is a good chance - all caps names are used for constants.. ,(for example DEVICE_FS) - these should be defined in one of the header files.
Also the structure of FS_Desc etc. If this is C they will either be initialised on a function (address passed in using & or dynamically allocated using malloc) or using assignments...
If c++ (.h points to C) then the classes will have an initialiser that does the work.

Martin

Kisen
Posts: 73
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: USB Mass storage on STM32

Post by Kisen »

mnf wrote:
Sat Sep 05, 2020 9:35 pm
By convention - so there is a good chance - all caps names are used for constants.. ,(for example DEVICE_FS) - these should be defined in one of the header files.
Also the structure of FS_Desc etc. If this is C they will either be initialised on a function (address passed in using & or dynamically allocated using malloc) or using assignments...
If c++ (.h points to C) then the classes will have an initialiser that does the work.

Martin
OK. I will go through the .h files and look for these.
With regards to structures. I understand that they are names that hold multiple variables. How would I go about creating these in flowcode? Is this something I can only do in C?

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: USB Mass storage on STM32

Post by mnf »

Structures are only available in C..
So struct {int a; char b;} x; would define a structure (x) with an int and then a char (so x.a = 1 and x.b = 'c') - depends on the situation but you could use the C code in C blocks or use an array of bytes (though this can get a bit ugly...) (so in this case use an array x[3] (and then x[0..1] = int and x[2] = char)
It's probably better to go with some supplementary C code... (If possible - try importing the usb header files as above - it might be possible to use them directly rather than rewriting everything?) (If C++ or many dependencies then this won't work! - but worth a try)

Martin

Kisen
Posts: 73
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: USB Mass storage on STM32

Post by Kisen »

mnf wrote:
Sun Sep 06, 2020 5:16 pm
Structures are only available in C..
So struct {int a; char b;} x; would define a structure (x) with an int and then a char (so x.a = 1 and x.b = 'c') - depends on the situation but you could use the C code in C blocks or use an array of bytes (though this can get a bit ugly...) (so in this case use an array x[3] (and then x[0..1] = int and x[2] = char)
It's probably better to go with some supplementary C code... (If possible - try importing the usb header files as above - it might be possible to use them directly rather than rewriting everything?) (If C++ or many dependencies then this won't work! - but worth a try)

Martin
You can import header files??
How would I do this ?

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: USB Mass storage on STM32

Post by mnf »

#include "filename.h"
Or
#include <filename.h> \\ searches current paths
In a c block or supplementary code.
You can then use imports in c blocks ....
Good luck!

Kisen
Posts: 73
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: USB Mass storage on STM32

Post by Kisen »

So I just put that include tag in the supplementary code box?

How does it then get the actual code?

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: USB Mass storage on STM32

Post by mnf »

Works for header only code....'

For 'simple' code - you can include the headers and then add the C from the C files to the supplementary code box..
Just tried here - and, unsurprisingly, it doesn't work (the functions called aren't found by the linker)
What we need is a way to add the 'c' files (so memory.c) to the compile/link commands...

Martin

Kisen
Posts: 73
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 4 times
Been thanked: 2 times
Contact:

Re: USB Mass storage on STM32

Post by Kisen »

Hi Martin,

I have been pointed in the direction of a pretty decent (so i am told) library for the USB MSD. I have zipped it up and PMd it to you.
I have also just figured out how to attach it here. In case anyone else needs it.

Credit to LonelyWolf on Github
cube-usb-msc.zip
(170.04 KiB) Downloaded 184 times
Can you give these files a try and see if FC has any intention in utilising them, I tried adding

Code: Select all

#include "somefile.h"
to supplementary code it started to tell me that other includes were missing relating to the STM32 chip itself. I can see why that would happen, but do i need them? I dont think i do and that FC will take care of the chip itself.

Still a bit puzzled as to where i then put the .c code, does it go under the include? Or does the .c file just go in the root folder and it gets used when its called?

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: USB Mass storage on STM32

Post by mnf »

I have an idea how it might be possible to link the C code with FC...

I did a simple test - I created a header file test.h and a corresponding c file test.c
Test.h declared a function

Code: Select all

#ifndef __TEST__
#define __TEST__

extern int addem (int a, int b);

#endif
and test.c defined this (not very useful) function addem..

Code: Select all

#include "test.h"

int addem(int a, int b) {
	return a + b ;
}
Including test.h is easy (in supplementary code) - and in this simple example it would be easy to add the code here too - but we need to get the compiler to handler the inclusion of test.c...

To do this I modified stm32comp.bat (make a backup first - different files are used for different targets) - by adding the following lines

Code: Select all

%MX_CC% %MX_CFLAGS% %MX_DEFINES% %MX_HEADERS% %MX_CTRL% "test.c" -o "%MX_OBJECTS%\test.o"
@IF %ERRORLEVEL% NEQ 0 GOTO Error
to the bottom of the build startup objects block...

This allows the program to compile and link (program just calls addem(3,4) in a c block.

This principle 'should' allow us to add C (not C++ - though this might be possible too) code relatively easily - so hopefully should now be possible to compile and link with the USB code (by adding each of the relevant C files) It should be possible to create different batch files for adding different libraries (as per HID) - and possibly create a python (or other) script to automate this a bit?
Flowcode2.fcfx
(5.84 KiB) Downloaded 167 times
Benj / Leigh - any potential pitfalls with this approach??

FC could allow a list of '.c' (and possibly '.o') files to be included in compilation / link process in the supplementary code section??

Martin

Post Reply