Skip to main content
martonmiklos
Senior
January 27, 2017
Solved

[CubeMX feature request]Please add an user section after the HAL_Init()

  • January 27, 2017
  • 15 replies
  • 2775 views
Posted on January 27, 2017 at 10:05

Dear STM32 Cube MX developers!

I am facing with the following problem, and I have not been able to find a proper solution:

I am using a STM32F103 with CDC stack via USB.

I have to add a code snippet to force the re-enumeration between the debug sessions.

Here is the generated code:

/* USER CODE BEGIN 1 */

uint8_t i, checksum;

/* USER CODE END 1 */

/* MCU Configuration----------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

/* Configure the system clock */

SystemClock_Config();

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_USART1_UART_Init();

MX_USB_DEVICE_Init();

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

*/

I need to add my code after the

SystemClock_Config();

but before the

MX_USB_DEVICE_Init();

I have not found any user generated sections between these two sections.

    This topic has been closed for replies.
    Best answer by Sirma Siang
    Posted on September 12, 2017 at 14:08

    USB_DEVICE user code section has been

    delivered in CubeMX4.22.1

    Kind regards

    Sirma

    15 replies

    Nesrine M_O
    Associate
    January 27, 2017
    Posted on January 27, 2017 at 12:01

    Hi

    Marton.Miklos

    ‌,

    Thanks for the suggestion! I will forward this to MX our team.

    -Nesrine-

    martonmiklos
    Senior
    January 27, 2017
    Posted on January 27, 2017 at 13:16

    Thanks for the fast response, please let me know their feedback!

    Jeanne Joly
    Associate
    January 30, 2017
    Posted on January 30, 2017 at 10:59

    Hello

    Marton.Miklos

    ,

    Just for my information and to know where we can put the user section you want, which code do you want to put between the SystemClock_config and USB_Device_Init?

    By the way, will it solve your issue if we put the user section begin and end just after the SystemClock_config (hence before the GPIO_Init)?

    BR

    Eric

    martonmiklos
    Senior
    February 3, 2017
    Posted on February 03, 2017 at 09:55

    Hello Eric,

    Thank you for your quick response!

    I am developing an USB CDC based application for a STM32F103 based board, and my board did not get re-enumerated between debug sessions unless I pull the D+ line to ground for 100 ms, (The re-enumeration would be required for the host application.)

    It is going to be an USB powered board, so this feature will not be in the final firmware, but I need it during the development.

    Technically the best would be placing user sections to all of the MX_*_Init() functions (MX_USB_DEVICE_Init in my case).

    I know the following idea would goes beyond the topic of this question, but I would recommend to extend the CubeMX code parser in such a way that users could put code everywhere between a general comment pair like this:

    /* USER CODE BEGIN Custom code */ 
    ... 
    /* USER CODE END Custom code */�?�?�?�?�?�?�?

    Jeanne Joly
    Associate
    March 30, 2017
    Posted on March 30, 2017 at 10:50

    Hello

    Marton.Miklos

    ,

    I will keep you informed as soon as we have some news about the target release where the fix will be delivered.

    BR. Eric

    Sirma Siang
    ST Employee
    July 6, 2017
    Posted on July 06, 2017 at 10:53

    Hello

    Marton.Miklos

    ,

    We propose to add a pre, and post, user code sections into MX_USB_DEVICE_Init function.

    Then it will looks like:

    void MX_USB_DEVICE_Init(void)

    {

    /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */

    /* USER CODE END USB_DEVICE_Init_PreTreatment */

    /* Init Device Library,Add Supported Class and Start the library*/

    USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);

    USBD_RegisterClass(&hUsbDeviceFS, &USBD_AUDIO);

    USBD_AUDIO_RegisterInterface(&hUsbDeviceFS, &USBD_AUDIO_fops_FS);

    USBD_Start(&hUsbDeviceFS);

    /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */

    /* USER CODE END USB_DEVICE_Init_PostTreatment */

    }

    To be sure it will fullfill your needs, could you please share your ioc ?

    This will be in CubeMX4.23 planned in Q3 2

    Kind Regards

    Sirma

    martonmiklos
    Senior
    July 6, 2017
    Posted on July 06, 2017 at 11:50

    Hello

    ‌

    Amazing news!

    ThesePreTreatment and PosTreatment sections will be added to each function or just to the MX_USB_DEVICE_Init?

    Here is my ioc file:

    https://www.dropbox.com/s/nje8srs0gfpoqas/xlineusb.ioc?dl=0

    And here is my current code:

    /* init function */ void MX_USB_DEVICE_Init(void){ /* USER CODE BEGIN Workaround */ GPIO_InitTypeDef GPIO_InitStruct; /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(USB_R_P_GPIO_Port, USB_R_P_Pin, GPIO_PIN_RESET); /*Configure USB DP pin as GPIO */ __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Pin = USB_R_P_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(USB_R_P_GPIO_Port, &GPIO_InitStruct); HAL_Delay(150); HAL_GPIO_WritePin(USB_R_P_GPIO_Port, USB_R_P_Pin, GPIO_PIN_SET); /* USER CODE END Workaround */ /* Init Device Library,Add Supported Class and Start the library*/ USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC); USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS); USBD_Start(&hUsbDeviceFS);}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    Sirma Siang
    ST Employee
    July 6, 2017
    Posted on July 06, 2017 at 12:21

    Hello

    Marton.Miklos

    ,

    As this subject is a large one, we will do it step by step, and USB_DEVICE will be the first updated.

    Thanks for your code, and ioc, sharing.

    Kind regards

    Sirma

    Sirma Siang
    Sirma SiangBest answer
    ST Employee
    September 12, 2017
    Posted on September 12, 2017 at 14:08

    USB_DEVICE user code section has been

    delivered in CubeMX4.22.1

    Kind regards

    Sirma

    martonmiklos
    Senior
    September 13, 2017
    Posted on September 13, 2017 at 08:40

    Well my usb_device.c still has the following generated contents (no user sections):

    /* 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_CDC);
     USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);
     USBD_Start(&hUsbDeviceFS);
    }�?�?�?�?�?�?�?�?�?

    Also the code generation fails with the following output (however this file got regenerated):

    2017-09-13 08:38:26,320 [FATAL] Middleware:652 - 
    2017-09-13 08:38:26,324 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/usb_device.c_save
    2017-09-13 08:38:26,420 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/usb_device.c
    2017-09-13 08:38:26,451 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Inc/usb_device.h_save
    2017-09-13 08:38:26,516 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Inc/usb_device.h
    2017-09-13 08:38:26,541 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Inc/usbd_conf.h_save
    2017-09-13 08:38:26,599 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Inc/usbd_conf.h
    2017-09-13 08:38:26,621 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/usbd_conf.c_save
    2017-09-13 08:38:26,732 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/usbd_conf.c
    2017-09-13 08:38:26,734 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Inc/usbd_desc.h_save
    2017-09-13 08:38:26,771 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Inc/usbd_desc.h
    2017-09-13 08:38:26,773 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/usbd_desc.c_save
    2017-09-13 08:38:26,812 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/usbd_desc.c
    2017-09-13 08:38:26,814 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Inc/usbd_cdc_if.h_save
    2017-09-13 08:38:26,851 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Inc/usbd_cdc_if.h
    2017-09-13 08:38:26,869 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/usbd_cdc_if.c_save
    2017-09-13 08:38:26,916 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/usbd_cdc_if.c
    2017-09-13 08:38:26,934 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/stm32f1xx_it.c_save
    2017-09-13 08:38:27,015 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/stm32f1xx_it.c
    2017-09-13 08:38:27,041 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Inc/stm32f1xx_it.h_save
    2017-09-13 08:38:27,079 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Inc/stm32f1xx_it.h
    2017-09-13 08:38:27,099 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/stm32f1xx_hal_msp.c_save
    2017-09-13 08:38:27,184 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/stm32f1xx_hal_msp.c
    2017-09-13 08:38:27,187 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/system.tmp_save
    2017-09-13 08:38:27,225 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/system.tmp
    2017-09-13 08:38:27,228 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Inc/stm32f1xx_hal_conf.h_save
    2017-09-13 08:38:27,275 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Inc/stm32f1xx_hal_conf.h
    2017-09-13 08:38:27,287 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Inc/main.h_save
    2017-09-13 08:38:27,332 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Inc/main.h
    2017-09-13 08:38:27,351 [INFO] CodeEngine:162 - oldGeneratedFile, /home/mm/STM32-workspace/xlineusb/Src/main.c_save
    2017-09-13 08:38:27,442 [INFO] CodeEngine:180 - Generated code: /home/mm/STM32-workspace/xlineusb/Src/main.c
    Exception in thread 'Thread-1222' java.lang.NoSuchMethodError: stm32Families.Families.getDefaultDefines(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/util/Vector;
     at com.st.microxplorer.plugins.projectmanager.engine.ProjectBuilder.searchNeededFiles(ProjectBuilder.java:1500)
     at com.st.microxplorer.plugins.projectmanager.engine.ProjectBuilder.createProject(ProjectBuilder.java:442)
     at com.st.microxplorer.plugins.projectmanager.engine.GenerateProjectThread.run(GenerateProjectThread.java:41)�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    Sirma Siang
    ST Employee
    September 13, 2017
    Posted on September 13, 2017 at 15:43

    Hello

    Marton.Miklos

    ,

    Have you tested with 4.1 version ?

    Mine generates the following code:

    void MX_USB_DEVICE_Init(void)

    {

    /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */

    /* USER CODE END USB_DEVICE_Init_PreTreatment */

    /* 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);

    USBD_Start(&hUsbDeviceFS);

    /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */

    /* USER CODE END USB_DEVICE_Init_PostTreatment */

    }

    Or

    /* init function */

    void MX_USB_DEVICE_Init(void)

    {

    /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */

    /* USER CODE END USB_DEVICE_Init_PreTreatment */

    /* Init Device Library,Add Supported Class and Start the library*/

    USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);

    USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);

    USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);

    USBD_Start(&hUsbDeviceFS);

    /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */

    /* USER CODE END USB_DEVICE_Init_PostTreatment */

    }

    If you can share your ioc, I would be able to test in same conditions at my end.

    Kind regards

    Sirma