Loading...
 

SW4STM32 and SW4Linux fully supports the STM32MP1 asymmetric multicore Cortex/A7+M4 MPUs

   With System Workbench for Linux, Embedded Linux on the STM32MP1 family of MPUs from ST was never as simple to build and maintain, even for newcomers in the Linux world. And, if you install System Workbench for Linux in System Workbench for STM32 you can seamlessly develop and debug asymmetric applications running partly on Linux, partly on the Cortex-M4.
You can get more information from the ac6-tools website and download (registration required) various documents highlighting:

System Workbench for STM32


How best to do CDC Host PC to microcontroller virtual com port communications?

How best to do CDC Host PC to microcontroller virtual com port communications?

I followed this excellent tutorial https://www.youtube.com/watch?v=XRocqTfUxboQuestion
to get USB working as mouse (tis a custom board with STM32F103C8TX) - so I know USB is
working. Then I found more tutorials and got microcontroller to Host PC working
using CDC_Transmit_FS.
(I’m running Ubuntu 14.04 and used CubeMX 4.15.1 to generate all the code and AC6
to compile using SW4STM32 toolchain and debug using ST-Link V2.)

Then I ran into a whole mass of confusing information about CDC_Receive_FS
and how to receive data from Host PC to microcontroller.

The source of the problems seems to be

1) There is no overal description of how CDC_Receive_FS should be used with worked cubemx example - anyone know where where I can get it?
2) No clean example of how CDC_Receive_FS is used with CubeMx similar to video tutorial like the one for mouse mentioned above
3) Not clear if CDC_Receive_FS is a callback function - if it is a callback function, how are the buffers set up first time around and how should the data be extracted from those buffers?
4) There are manual examples without use of CubeMX that I could try to use, but CubeMX benefit would get lost so I prefer to use CubeMx generated code as much as possible.
5) It is possible to trawl through all the material and eventually solve this puzzle - so aim is to save time.

I have seen all kinds of material where USB virtual com post host PC to microcontroller is working for someone - its just that as a relative newbie, it seems to be harsh journey to get a grip on the subject to make CubeMX work with AC6 to make Host PC to microcontroller working. Any help to get there conscisely and quickly greatly appreciated :-)

Solved already :-)

[As an aside, the CDC_Transmit_FS requires a check before being called:

char msg100;

sprintf(msg,”ARM reporting %i\n”)i++);
if (hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED)
CDC_Transmit_FS((uint8_t *)msg, strlen(msg));
]

And then static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) is a callback function
defined in usbd_cdc_if.c which gets called whenever there is data arriving
in USB through the virtual com port.

I modified the function to add 3 lines of code to copy out the incoming buffered data like this:

static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf0);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);

// This function CDC_Receive_FS is a callback function invoked when data is received - add 3 extra lines of code to copy the data to my own buffer
received_data_size = *Len;
memcpy(received_data, Buf, received_data_size);
receive_total += received_data_size;

return (USBD_OK);
/* USER CODE END 6 */
}

Further up the file usbd_cdc_if.c, I modified the data sizes and declared the 3 variables I am using

//#define APP_RX_DATA_SIZE 4
//#define APP_TX_DATA_SIZE 4

  1. define APP_RX_DATA_SIZE 64
  2. define APP_TX_DATA_SIZE 64

uint8_t received_data100;
uint32_t received_data_size;
uint32_t receive_total = 0;

in file usbd_cdc_if.h, I added 3 lines to export the three variables I am using:

extern uint8_t received_data[];
extern uint32_t received_data_size;
extern uint32_t receive_total;

In main.c I added #include “usbd_cdc_if.h”
and then printed out the receive_total and also the received_data.

It turns out the data is now received in blocks of 64 bytes
as decide in usbd_cdc_if.c by

  1. define APP_RX_DATA_SIZE 64
  2. define APP_TX_DATA_SIZE 64


Yippeee it works from CubeMX generated code compiled in AC6 with SW4STM32 toolchain :-)


One thing CubeMX developers can do to help is to add some comments
to CDC_Receive_FS to say it is a callback function and that you should copy
out the data presented in Buf (of length Len) to your own buffer before it gets
lost with next data arriving through USB. Also hint Len is maximum APP_RX_DATA_SIZE.

(Aside - the code in previous post seems to have had the hash in hash defines replaced by numbers :-(
Thats just the interpreting the hash as numbered indented points - ignore it and use hash symbols for hash defines)