# Firmware - Code Generation - STM32CubeMX ###### tags: `Firmware` `STM32` `Code Generation` `STM32CubeMX` --- https://www.digikey.tw/en/maker/projects/getting-started-with-stm32-timers-and-timer-interrupts/d08e6493cefa486fb1e79c43c0b08cc6 * Polling "ODR" Read High/ Low Voltage * Interrupt * DMA - 頻繁處理事, 不需要 CPU 運算, 把 Data 從某一個 Register copy 到另一個 Address 用開發板的用意是啥? MCU, Pin 要幹嘛? Porting https://github.com/STMicroelectronics/STM32CubeG0 HAL_TIM_Base_Start() HAL_TIM_PWM_Start() --- ![](https://i.imgur.com/akCtvk4.png) ## New Project ![](https://i.imgur.com/SDOIxhf.png) ### Access to Board Selector Find ``NUCLEO-G0B1RE`` ![](https://i.imgur.com/b5Eyr8J.png) ![](https://i.imgur.com/waXVjwS.png) Select and Start Project: ![](https://i.imgur.com/aXKQSYI.png) ## Project ### Project Manager ![](https://i.imgur.com/XwrBenQ.png) ![](https://i.imgur.com/dtXgudN.png) ![](https://i.imgur.com/32znwTR.png) Choose "No" ![](https://i.imgur.com/d8AxTJR.png) --> Choose "Continue" ![](https://i.imgur.com/h20yrvx.png) If Choose "Yes" ![](https://i.imgur.com/Q5KkJp0.png) ![](https://i.imgur.com/23X29EE.png) ### Clock Config: Select Clock Config Tab: ![](https://i.imgur.com/8TyTe6u.png) Use default (Internal) HSI: Then pick ``PLLCLK`` to get 64Hz ![](https://i.imgur.com/bdQplNA.png) ![](https://i.imgur.com/ioyQtRJ.png) ### GPIO option: ![](https://i.imgur.com/YyBYL8q.png) User can specify its own ``User Label`` ![](https://i.imgur.com/qvBf2tS.png) ### UART option: ![](https://i.imgur.com/tIBHIRP.png) ### ADC option: ![](https://i.imgur.com/n7g6UZl.png) ### DAC option: ![](https://i.imgur.com/txQueLZ.png) ### Select wanted pin ![](https://i.imgur.com/71EYPrY.png) ![](https://i.imgur.com/2zIdRla.png) ### Code Generation result ![](https://i.imgur.com/IswdJ6V.png) --- # Special setting for the project ## Use MicroLIB uncheck Fiilex Color Series 產品, 使用 CubeMx 產生後的 project, 需要檢查 __Use MicroLIB__ 這個item 是否有被打勾; 若過往的 project 是需要使用 MicroLIB, __一旦沒打勾, 程式行為會不如預期__!! ![](https://i.imgur.com/cJ6RiF8.png) ![](https://i.imgur.com/0kztl6d.png) 根據這篇論譠討論 [STM32CubeMX 6.0.1 with STM32Cube MCU Package for STM32H7 Series version 1.8.0 unchecks the MicroLIB checkbox](https://community.st.com/s/question/0D53W00000JhzcnSAB/stm32cubemx-601-with-stm32cube-mcu-package-for-stm32h7-series-version-180-unchecks-the-microlib-checkbox) 使用 STM32CubeMx 6.0 以後的版本, 每次 Generate proejct 後就會把 __Use MicroLIB__ uncheck!! 保險的作法是每次在 Generate proejct 後, 自行將 __Use MicroLIB__ check!! --- ### [STM32CubeMX 6.0.1 with STM32Cube MCU Package for STM32H7 Series version 1.8.0 unchecks the MicroLIB checkbox](https://community.st.com/s/question/0D53W00000JhzcnSAB/stm32cubemx-601-with-stm32cube-mcu-package-for-stm32h7-series-version-180-unchecks-the-microlib-checkbox) #### Question 2020/10/01 Hello, I have an IOC file, which was generated with help of STM32CubeMX ``` MxCube.Version=5.6.1 MxDb.Version=DB.5.0.60 ``` When I open this project to update the Keil project (which has Keil Compiler version 6 selected), with STM32CubeMX version 6.0.1 (with STM32Cube MCU Package for STM32H7 series version 1.8.0), and generate code, the STM32CubeMX __deselects__ the __MicroLIB__ check box. I am observing __the code is not behaving as expected with MicroLIB disabled / unchecked__. __As the issue is not observed when using the ULINKpro debugger__ (which I am using to find the reason for the issue) I am unable to find the reason for different behavior of the same code j ust based on whether the __MicroLIB__ is in use or not in use. --- #### Khouloud ZEMMELI (ST Employee) 2020/10/xx Hi @Rajeev Arora (Community Member) After checking, this is not a bug but a change made by our development team __on H7 & MP1 to avoid some compilation errors__, the __user can activate the MicroLIB on Keil__ at the end if it is necessary for his project (once your MX setup is complete). Best Regards, Khouloud --- #### Rajeev Arora (Community Member) 2020/10/xx Hello @Khouloud ZEMMELI (ST Employee) Thanks for sharing the update. from your inputs I understand that every time I generate the code with help of IOC file, the MicroLIB setting (even if enabled) in the existing UVPROJX file will get updated to unchecked, and then I will have to __manually add that setting with help of Keil IDE__ or by __making change to the UVPROJX file__ (by opening it in a text editor). Is my understanding correct? Thanks, Rajeev --- #### Khouloud ZEMMELI (ST Employee) 2020/10/12 Yes, __because MX force the MicroLIB to be unchecked for H7 and MP1__, so every time you re-generate code MicroLIB will take the MX configuration. Thanks for your understanding and you're always welcome. Regards, Khouloud --- ### [Building an application with microlib](https://www.keil.com/support/man/docs/armlib/armlib_chr1358938939195.htm) 但這個行為的缺點是要自己先確認哪些 ``*.c`` 檔需要使用 ``microlib`` 然後再作類似 MakeFile 去設定 compile option!! ### 2.5 Building an application with microlib To build a program using microlib, you must use the command-line option ``--library_type=microlib``. This option can be used by the compiler, assembler or linker. Use ``--library_type=microlib`` with the linker to override all other options. #### Compiler option ``` armcc --library_type=microlib -c main.c armcc -c extra.c armlink -o image.axf main.o extra.o ``` Specifying ``--library_type=microlib`` when compiling ``main.c`` results in an object file containing an attribute that asks the linker to use microlib. Compiling ``extra.c`` with ``--library_type=microlib`` is __unnecessary, because the request to link against microlib exists in the object file generated by compiling ``main.c``__. #### Assembler option ``` armcc -c main.c armcc -c extra.c armasm --library_type=microlib more.s armlink -o image.axf main.o extra.o more.o ``` The request to the linker to use microlib is made as a result of assembling ``more.s`` with ``--library_type=microlib``. #### Linker option ``` armcc -c main.c armcc -c extra.c armlink --library_type=microlib -o image.axf main.o extra.o ``` Neither object file contains the attribute requesting that the linker link against microlib, so the linker selects microlib as a result of being explicitly asked to do so on the command line. --- ### Practice 觀察 Keil C 下列3個 Tab 的設定 ### C/C++ - Compiler control string ``` Compiler control string --c99 -c --cpu Cortex-M4.fp -g -O3 --apcs=interwork --split_sections -I ../Core/Inc -I ../Drivers/STM32F4xx_HAL_Driver/Inc -I ../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy -I ../Drivers/CMSIS/Device/ST/STM32F4xx/Include -I ../Drivers/CMSIS/Include -ID:/Firmware/TEST/Time_Applications/F413_Study/MDK-ARM/RTE/_F413_Study -IC:/Keil_v5/ARM/PACK/ARM/CMSIS/5.0.0/CMSIS/Include -IC:/Keil_v5/ARM/PACK/Keil/STM32F4xx_DFP/2.15.0/Drivers/CMSIS/Device/ST/STM32F4xx/Include -D__UVISION_VERSION="522" -D_RTE_ -DSTM32F413xx -DUSE_HAL_DRIVER -DSTM32F413xx -o F413_Study\*.o --omf_browse F413_Study\*.crf --depend F413_Study\*.d ``` ![](https://i.imgur.com/UK0vM9z.png) #### Asm - Assember control string ``` --cpu Cortex-M4.fp -g --apcs=interwork -I D:\Firmware\TEST\Time_Applications\F413_Study\MDK-ARM\RTE\_F413_Study -I C:\Keil_v5\ARM\PACK\ARM\CMSIS\5.0.0\CMSIS\Include -I C:\Keil_v5\ARM\PACK\Keil\STM32F4xx_DFP\2.15.0\Drivers\CMSIS\Device\ST\STM32F4xx\Include --pd "__UVISION_VERSION SETA 522" --pd "_RTE_ SETA 1" --pd "STM32F413xx SETA 1" --list "*.lst" --xref -o "*.o" --depend "*.d" ``` ![](https://i.imgur.com/ZfCTezH.png) #### Linker - Linkercontrol string ``` --cpu Cortex-M4.fp *.o --strict --scatter "F413_Study\F413_Study.sct" --summary_stderr --info summarysizes --map --xref --callgraph --symbols --info sizes --info totals --info unused --info veneers --list "F413_Study.map" -o F413_Study\F413_Study.axf ``` ![](https://i.imgur.com/L51jBzT.png) 在把 __Use MicroLIB__ 打勾後, 比較這3個項目異動的地方為何 * Compiler control string ``-D__MICROLIB`` ``` --c99 -c --cpu Cortex-M4.fp -D__MICROLIB -g -O3 --apcs=interwork --split_sections -I ../Core/Inc -I ../Drivers/STM32F4xx_HAL_Driver/Inc -I ../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy -I ../Drivers/CMSIS/Device/ST/STM32F4xx/Include -I ../Drivers/CMSIS/Include ``` ![](https://i.imgur.com/rbcLZ2N.png) * Assember control string ``--pd "__MICROLIB SETA 1" `` ``` --cpu Cortex-M4.fp -g --apcs=interwork --pd "__MICROLIB SETA 1" ``` ![](https://i.imgur.com/5iTRMmY.png) * Linker - control string ``--library_type=microlib`` ``` --library_type=microlib --strict --scatter "F413_Study\F413_Study.sct" ``` ![](https://i.imgur.com/ERuJ6SS.png) 所以把這3項分別加在各自的 Misc Control 中就可以了 ![](https://i.imgur.com/pCvHrGV.png) ![](https://i.imgur.com/XCr4wXB.png) ![](https://i.imgur.com/DNjrs6a.png) ___ # Heap size 有時程式需要利用 ``malloc`` 來配置較大空間的 Memory 使用 CubeMx 來 Generate proejct, 可以在 __Project Manager__ 這個 Tab 中的 __Project-->Linker Settings__ 來設定 預設最小的 Heap size 是 ``0x200`` ![](https://i.imgur.com/y0HgRpl.png) 例如: 需求是 ``0x4000`` ![](https://i.imgur.com/A7dUxo9.png) 這樣產出的 ``MDK-ARM`` 資料夾中有一個 ``startup_stm32<abcd>xe.s`` 檔案 ![](https://i.imgur.com/Y99zod9.png) 可以看見 ``Heap_Size`` 從 ``0x200`` 變成 ``0x4000`` ``` ; <h> Heap Configuration ; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8> ; </h> Heap_Size EQU 0x4000 ```