--- tags: notes, stm32, stm32cube, IDE --- # STM32CubeIDE Tips I was a Keil user. With the projects I'm dealing with are getting bigger, the free trial version of Keil with 32K byte limit no longer a choice for me. My teacher even encourage us to use a pirate version of Keil. I'm a fan of opensource and VS Code. Microsoft does a great job on optimization and `F1` command is so convinient. But there are still lots of steps to do for debugging in VS Code, I choosed `STM32CubeIDE`, which is based on ECLIPSE and intergrated with stm32Cube. This note is written with `STM32CubeIDE Version: 1.4.0` ## STM32CubeMX Bug list - USB device descriptor request failed: Adjust PLL parameters in clock configuration tab. Try different parameters that still outputs 48MHz to USB peripheral. - USB device DFU class: USBD_DFU_MEDIA Interface, aka `FLASH_DESC_STR`, longest size is 127 chars. Doesn't go any further even you change any parameter found in CubeMX. ## Include a header file correctly If you are including something like `stdio.h`, then you can probably compile and call your function without error. But when you are trying to include a header file elsewhere(not in project folder), such as `stm32746g_discovery_lcd.h`, it becomes triky. For the compiler to know where `stm32746g_discovery_lcd.h` is, you need to add it to `Include Paths`: > Project ==> Properties ==> C/C++ General ==> Paths and Symbol > ==> Includes ==> GNU C ==> Add..., then paste the folder path where your header file located. > ![](https://i.imgur.com/94rTUrT.png) And for linker to know where your `.c` file located(), you need to add the folder to `Link Folder...`: > Project ==> Properties ==> C/C++ General ==> Paths and Symbol > ==> Source Location ==> Link Folder ==> Check *Link to folder...*, then paste the folder path where `.c` loacted at the second textbar. > ![](https://i.imgur.com/R13KY7P.png) ## Link a static library(*.a or *.lib) > Get the path of the library > ![](https://i.imgur.com/C94NpXN.png) > Add in `Libraries` and `Library search path` > For `*.a` files, you can either remove `lib` and `.a` as shown in the picture > or you can type `:*.a` > ![](https://i.imgur.com/uKG8ChV.png) - Ref: [Include DSP library](https://www.youtube.com/watch?v=vCcALaGNlyw&ab_channel=YetAnotherElectronicsChannel) - Ref: [扩展库 .a 文件 在 CUBE IDE内 引用的方法](https://blog.csdn.net/ad56583964/article/details/105213425) ## Export and Import - Preference - Export > File ==> Export ==> General ==> Preference, Click Next > Check Export all ==> Choose To preference file, click Finish. - Import > File ==> Import ==> General ==> Preference, Click Next > Choose From preference file ==> Check Import all, click Finish. - [Preference backup](https://drive.google.com/file/d/1vRYL8WT9rGl_pQWXLRHqvJJpvEAX_wMf/view?usp=sharing) at 20201028 - Projects - bla bla bla ## Folding any codes > Not realy useful, not recommanded. It seems that STM32CubeIDE does not support folding on any {}, so I will use `#if 1` and `#endif` to fold codes that I'm not editing, especially those parts generated by Cube. Here is a clean `main.c` with `#if 1` and `#endif` at specific lines. When your project just built and you have the first code generation, erase everything in `./Core/Src/main.c` and paste the following snippet in it. It's important that you can only do this when the project just built and **there is nothing from you in `./Core/Src/main.c`**, otherwise you have to do it line by line manually. ::: spoiler `main.c` ```c= /* USER CODE BEGIN Header */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ // Folding Private variables by Cube #if 1 /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ // // Folding /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ #endif // Folding Private function prototypes of Cube #if 1 /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN PFP */ #endif /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ // Folding Private variables initialization of Cube #if 1 /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ /* USER CODE BEGIN 2 */ #endif /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } // Folding Private functions implementation of Cube #if 1 /* USER CODE END 3 */ } /* USER CODE BEGIN 4 */ #endif /* USER CODE END 4 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ ``` :::