8000 upgrade STM32CubeF4 to v1.13.0 by blazewicz · Pull Request #2408 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

upgrade STM32CubeF4 to v1.13.0 #2408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Nov 16, 2016

Conversation

blazewicz
Copy link
Contributor

This is the newest version of STM32CubeF4 from ST.

There is a lot of changes since previous HAL version was over 2 years older.

Key benefits:

  • replaces many possible dead-loops with proper timeout checks
  • unifies a lot of things between different HAL's
  • adds support for multiple new devices and peripherals
  • fixes many bugs
  • it will be easier to update HAL in the future

For complete reference of STM32CubeF4 changes please see Release_Notes.html provided by ST in Cube package, you can download it from here.

Tested on pyboard (PYBV10), all tests pass, SD card works, I didn't test anything beyond that. It doesn't break build for any other MCU series.

Executable size increased very slightly, exact numbers can be found below:

before:

   text    data     bss     dec     hex filename
 298144     344   27960  326448   4fb30 build-PYBV10/firmware.elf

after:

   text    data     bss     dec     hex filename
 298172     348   28060  326580   4fbb4 build-PYBV10/firmware.elf

Any volunteers to test this PR are welcome.

@dpgeorge
Copy link
Member

Thanks for this. I'm a bit concerned about the increase in the BSS by 100 bytes. Do you know what it is from?

@blazewicz
Copy link
Contributor Author

We can check that.

~/git/micropython/stmhal/ $ git checkout master
~/git/micropython/stmhal/ $ make clean
~/git/micropython/stmhal/ $ make
 ~/git/micropython/stmhal/ $ arm-none-eabi-nm --print-size --radix=d build-PYBV10/firmware.elf | grep -P "^\w+ \w+ (b|B)"  | cut -d " " -f 2- - > before.txt
~/git/micropython/stmhal/ $ git checkout stmhal-cube-1.13.0
~/git/micropython/stmhal/ $ make clean
~/git/micropython/stmhal/ $ make
 ~/git/micropython/stmhal/ $ arm-none-eabi-nm --print-size --radix=d build-PYBV10/firmware.elf | grep -P "^\w+ \w+ (b|B)"  | cut -d " " -f 2- - > after.txt
 ~/git/micropython/stmhal/ $ diff -u before.txt after.txt > bss.diff
--- before.txt  2016-09-14 12:14:27.901667159 +0200
+++ after.txt   2016-09-14 12:14:52.985549774 +0200
@@ -1,14 +1,14 @@
 00000092 b CDC_ClassData
 00000004 b CDC_fops
 00000001 b cdc_iface_num
-00000001 b cfgidx.9898
+00000001 b cfgidx.10035
 00000024 b DAC_Handle
 00000001 b dev_is_connected
 00000004 b dma_enable_mask
 00000064 b dma_handle
 00000002 B dma_idle
 00000016 b dma_last_sub_instance
-00000001 b enabled.11367
+00000001 b enabled.11504
 00000016 b FatFs
 00000001 B ff_CurrVol
 00000004 b flash_cache_sector_id
@@ -26,15 +26,15 @@
 00000001 b hid_in_ep
 00000004 b hid_report_desc
 00000548 B hUSBDDevice
-00000064 B I2CHandle1
-00000064 B I2CHandle2
+00000092 B I2CHandle1
+00000092 B I2CHandle2
 00000001 b led_pwm_state
 00000256 b lfn
 00000512 b LfnBuf
 00000708 B mp_state_ctx
 00002156 b MSC_BOT_ClassData
 00000004 b MSC_fops
-00000948 B pcd_fs_handle
+00000956 B pcd_fs_handle
 00000004 B pendsv_object
 00000032 B pFlash
 00000001 b pin_class_debug
@@ -46,15 +46,15 @@
 00000001 b repl_display_debugging_info
 00000004 b reset_cause
 00000028 b rl
-00000012 b RNGHandle
+00000016 b RNGHandle
 00000036 B RTCHandle
 00000004 b rtc_info
 00000001 b rtc_need_init_finalise
 00000004 b rtc_startup_tick
 00000001 b sdcard_started
 00000096 b sd_handle
-00000084 b sd_rx_dma
-00000084 b sd_tx_dma
+00000100 b sd_rx_dma
+00000100 b sd_tx_dma
 00000092 B SPIHandle1
 00000092 B SPIHandle2
 00000064 B TIM5_Handle

So we have:

  • +56 bytes because I2C_HandleTypeDef grew by 28 bytes
  • +8 bytes because PCD_HandleTypeDef grew by 8 bytes
  • +4 bytes because RNG_HandleTypeDef grew by 4 bytes
  • +32 bytes because DMA_HandleTypeDef grew by 16 bytes
  • +100 bytes total

@dpgeorge
Copy link
Member

A few more points/quetions:

  • Although there are F2 HAL files in the repo, we don't actually support F2 MCU's (it was going to be supported, but work stalled). So no need for special treatment of that MCU series.
  • I'd like to move the cmsis files out of stmhal and to lib/, so they can be used by other ports if needed. It would be good if that were a separate PR (and should be independent to changing the HAL...?)
  • Did they really remove the BSRRL and BSRRH registers?

@blazewicz
Copy link
Contributor Author

Although there are F2 HAL files in the repo, we don't actually support F2 MCU's (it was going to be supported, but work stalled). So no need for special treatment of that MCU series.

So maybe it would be a good idea to remove all F2 related files before merging this PR and add it fresh when/if they will be needed? Leaving them may result in more compatibility issues in future.

I'd like to move the cmsis files out of stmhal and to lib/, so they can be used by other ports if needed. It would be good if that were a separate PR (and should be independent to changing the HAL...?)

Ok, but then we have to separate files from stmhal/cmsis/inc and stmhal/cmsis/devinc. The former are ARM's generic files for any Cortex-M based MCU, but the latter are only for STM32's and must be merged within this PR since new HAL Driver depends on it.

For generic CMSIS files inside lib/ we could consider using a submodule of ARM's official repo: ARM-software/CMSIS

Did they really remove the BSRRL and BSRRH registers?

IMHO they made it represent hardware more literally, since technically there is one 32 bit register called BSRR.


So to conclude, before merge we need to:

  • create PR moving stmhal/cmsis/inc to lib/cmsis (or create a submodule of ARM-software/CMSIS)
  • remove changes to stmhal/cmsis/inc from this PR & move stmhal/cmsis/devinc/* one dir up?
  • remove stmhal/hal/f2 and #if defined(MCU_SERIES_F2)'s from the code

@blazewicz
Copy link
Contributor Author

Creating submodule from ARM-software/CMSIS is actually a bad idea, after pull it is over 356 MB!

@dpgeorge
Copy link
Member

create PR moving stmhal/cmsis/inc to lib/cmsis

Yes please, no submodule :)

remove changes to stmhal/cmsis/inc from this PR & move stmhal/cmsis/devinc/* one dir up?

Yes, move up one dir to simply stmhal/cmsis

remove stmhal/hal/f2 and #if defined(MCU_SERIES_F2)'s from the code

Yes.

@turbinenreiter
Copy link
Contributor

Is there a timeline to merge this? It enables adding support for the new F412 family, which I'm currently trying to do.

@blazewicz
Copy link
Contributor Author

You can use my branch to add F412, it would be a good verification for this PR since I've tested it only on pyboard (F405RG) and something could slip through automated tests.

@turbinenreiter
Copy link
Contributor

I'm trying to do exactly that. I got it compiling, but not working yet. See here.

@dpgeorge
Copy link
Member

@blazewicz is this ready to merge? Now that v1.8.5 is tagged we can proceed with this.

@blazewicz
Copy link
Contributor Author

Branch is based on current master, all CI checks pass, all tests on pyboard pass. I think we are ready to merge it in.

@siorpaes
Copy link
Contributor

Hello,

I noticed that there is a recent patch that upgrades to Cube HAL 1.13.1 which fixes in particular some issues with I2C peripheral:

image

would it be worth to integrate 1.13.1 directly so we are aligned to the very latest version ?

David

@blazewicz
Copy link
Contributor Author

@siorpaes was that version already released? The latest package from http://www.st.com/en/embedded-software/stm32cubef4.html is still 1.13.0 with latest changes in HAL from 01-July-2016.

I think its a good idea to use latest possible version if we make that upgrade.

@siorpaes
Copy link
Contributor

Yes, it has already been released but in form of a patch that goes on top of 1.13.0.
It is available here 1.13.1 patch

@dpgeorge
Copy link
Member

It would be good to use v1.13.1 with the extra patch.

@blazewicz
Copy link
Contributor Author

I have merged the patch into stmhal: upgrade to STM32CubeF4 v1.13.0 - HAL v1.5.1 but it broke I2C driver, I'm working on it.

@prusnak
Copy link
Contributor
prusnak commented Nov 6, 2016

Would it be possible to include the following files, even though they are not used currently in Micropython?

stmhal/hal/f4/inc/stm32f4xx_hal_sram.h
stmhal/hal/f4/inc/stm32f4xx_ll_fsmc.h
stmhal/hal/f4/src/stm32f4xx_hal_sram.c
stmhal/hal/f4/src/stm32f4xx_ll_fsmc.c

I am using these in a project of mine and I guess other developers could benefit from inclusion of these, but I understand if you don't want to include unused files.

@dpgeorge
Copy link
Member

I have merged the patch into stmhal: upgrade to STM32CubeF4 v1.13.0 - HAL v1.5.1 but it broke I2C driver, I'm working on it.

@blazewicz how is this going?

@blazewicz
Copy link
Contributor Author
blazewicz commented Nov 10, 2016 via email

@dpgeorge
Copy link
Member

@blazewicz I see you've made some new commits... is it ready to merge? Also, is it important to keep the upgrade v1.13.0 separate to v1.13.1, ie not squash them into a single upgrate?

@blazewicz
Copy link
Contributor Author

@blazewicz I see you've made some new commits...

Yes, I've rebased my branch on top of the upstream master.

is it ready to merge?

Actually after c4e58ea all pyboard tests pass, this is because the problem lays between I2C and DMA, and test pyb/i2c.py doesn't use DMA any more. Yet the problem still exists though.

Also, is it important to keep the upgrade v1.13.0 separate to v1.13.1, ie not squash them into a single upgrate?

You mean commits below?
stmhal: upgrade to STM32CubeF4 v1.13.0 - CMSIS/Device 2.5.1
stmhal: upgrade to STM32CubeF4 v1.13.1 - HAL v1.5.2

The v1.13.1 is a patch release and it only changes the HAL driver (v1.5.1->v1.5.2), it doesn't modify anything in CMSIS device drivers. I have squashed the patch into the latter commit and thus I've changed it message.

I've kept those commits separate having in mind note from stmhal/hal/HALCOMMITS:

Any commit which touches the HAL should be separate from any other changes (...)

@pfalcon
Copy link
Contributor
pfalcon commented Nov 15, 2016

Also, is it important to keep the upgrade v1.13.0 separate to v1.13.1, ie not squash them into a single upgrade?

My 2 cents: yes, it would be be important to keep them separate, to debug (bisect) any later issues. (And to just avoid "vendor fork == mess" syndrome).

I'm not sure if @blazewicz was quick to squash something indeed, but what I see now is:

stmhal: upgrade to STM32CubeF4 v1.13.1 - HAL v1.5.2
blazewicz committed on Sep 6

stmhal: upgrade to STM32CubeF4 v1.13.0 - CMSIS/Device 2.5.1
blazewicz committed on Oct 12

I.e, on Sep 6, it was upgraded to 1.13.1, then on Oct 12, "upgraded" to 1.13.0. There's apparently a mistake in the commit message, as it looks like a downgrade.

@blazewicz
Copy link
Contributor Author

The CMSIS/Device part was not affected by v1.13.1 patch, thats why I left its message as it was. But ok, now I've changed it for the sake of consistency.

@blazewicz
Copy link
Contributor Author

My 2 cents: yes, it would be be important to keep them separate, to debug (bisect) any later issues. (And to just avoid "vendor fork == mess" syndrome).

If you want to keep v1.13.0 and v1.13.1 separate we could rebase/split commits like this:

stmhal: apply STM32CubeF4 v1.13.1 patch - upgrade HAL driver to v1.5.2
stmhal: upgrade to STM32CubeF4 v1.13.0 - HAL v1.5.1
stmhal: upgrade to STM32CubeF4 v1.13.0 - CMSIS/Device 2.5.1
(master)

@pfalcon
Copy link
Contributor
pfalcon commented Nov 15, 2016

@blazewicz : You might notice that the root of my confusion was that commits were apparently in reverse order to what logically could be expected - both in linear order and by dates. If that was indeed true, then swapping commit order would be the way to address it.

Anyway, I don't call for any changes myself, I'm not stm32 maintainer. We just had an interesting discussion in some other ticket on how easy would it be supporting this or that vendor-specific feature for an arbitrary MCU, so I took a chance to peek at how easy this goes for stm32 which is already well supported. I don't find associated maintenance easy at all. Anyway, thanks for calling for these changes (and I hope they won't break much indeed ;-) ).

@blazewicz
Copy link
Contributor Author

@blazewicz : You might notice that the root of my confusion was that commits were apparently in reverse order to what logically could be expected - both in linear order and by dates. If that was indeed true, then swapping commit order would be the way to address it.

Yes, you are right, seeing first upgrade to v1.13.1 and then upgrade to v1.13.0 may have been confusing. Despite this part, I've tried to keep logical order and thus the chronology of the commits should be ignored, in the end it should look good in the git log.

@dpgeorge what do you think about the proposition form my previous comment? Then you could merge this PR as it is right now (most of functionality is working fine, and some people are waiting for it), and move the I2C/DMA problem to its own issue - the breaking change could be easily extracted/reverted.

the complete branch log would look like this:

* df42059 stmhal/mphalport.h: use single GPIOx->BSRR register
* 57de33d stmhal/hal: do not include <stdio.h> in HAL headers
* d116734 stmhal/i2c: provide custom IRQ handlers
* 05c3f92 stmhal/can: clear FIFO flags in IRQ handler
* b89ad01 stmhal/dma: mark DMA sate as READY even if HAL_DMA_Init is skipped
* 8bf608f stmhal/dma: precalculate register base and bitshift on handle init
* 3183de7 stmhal/i2c: handle I2C IRQs
* 3230955 stmhal/make-stmconst.py: fix regex's to work with current CMSIS
* 7165493 stmhal/boards: configure all F4 boards to work with new HAL
* bd91979 stmhal/hal/sd: reapply HAL commit 09de030 for f4
* ca1359f stmhal/hal/rcc: reapply HAL commit c568a2b for f4
* c45a875 stmhal/hal: reapply HAL commit 9db719b for f4
* ff50bef stmhal/hal/sd: reapply HAL commit 1d7fb82 for f4
* f4cacb7 stmhal/hal/i2c: reapply HAL commit ea040a4 for f4
* c144e6a stmhal: apply STM32CubeF4 v1.13.1 patch - upgrade HAL driver to v1.5.2
* c728321 stmhal: upgrade to STM32CubeF4 v1.13.0 - HAL v1.5.1
* dd9bffe stmhal: upgrade to STM32CubeF4 v1.13.0 - CMSIS/Device 2.5.1

In this state tests break only with this change:

diff --git a/tests/pyb/i2c.py b/tests/pyb/i2c.py
index a220f8e..0b3ba93 100644
--- a/tests/pyb/i2c.py
+++ b/tests/pyb/i2c.py
@@ -11,7 +11,7 @@ for bus in (-1, 0, 1, 2, 3, "X", "Y", "Z"):

 i2c = I2C(1)

-i2c.init(I2C.MASTER, baudrate=400000)
+i2c.init(I2C.MASTER, baudrate=400000, dma=True)
 print(i2c.scan())
 i2c.deinit()

@dpgeorge
Copy link
Member

what do you think about the proposition form my previous comment? Then you could merge this PR as it is right now (most of functionality is working fine, and some people are waiting for it), and move the I2C/DMA problem to its own issue - the breaking change could be easily extracted/reverted.

I agree that we can put the I2C DMA problem in an issue of its own later, and that what you've done so far is good for merging. I've had other reports that the latest HAL actually improves the I2C driver in some repects so there will be some test to do to see exactly what the issues are.

@blazewicz
Copy link
Contributor Author

Ok, and do you want me to make 1.13.1 patch a separate commit? As @pfalcon said, it would facilitate debugging.

@dpgeorge
Copy link
Member

If you want to keep v1.13.0 and v1.13.1 separate we could rebase/split commits like this:

stmhal: apply STM32CubeF4 v1.13.1 patch - upgrade HAL driver to v1.5.2
stmhal: upgrade to STM32CubeF4 v1.13.0 - HAL v1.5.1
stmhal: upgrade to STM32CubeF4 v1.13.0 - CMSIS/Device 2.5.1
(master)

Yes, doing it that way looks good. If you don't mind reorganising it like that and then I can merge, thanks!

Krzysztof Blazewicz added 17 commits November 16, 2016 12:43
changes include:
* use single GPIO.BSRR instead of BSRRH and BSRRL
* change HSE_STARTUP_TIMEOUT to 100 ms
* define LSE_STARTUP_TIMEOUT to 5 s
CMSIS v2.5.0 removed all uint32_t casts and uses only Misra Cast (U)
This is required by HAL Driver for error handling since v1.5.0
Current version of HAL drivers optimize IRQ handler by using precalculated
DMA register address and stream bitshift instead of calculating it on every interrupt.

Since we skip call to `HAL_DMA_Init` on reused DMA, fields StreamBaseAddress and StreamIndex
of DMA handle are not initialized and thus leads to SegFault in `DMA_IRQHandler`.

HAL_DMA_Init is a big routine and we do not need to call it on each use of DMA
(ex.: series of I2C operations) and DMA_CalcBaseAndBitshift is really small and
releasing it increases code size by only 8 bytes.
Current version of HAL drivers checks if `hdma->State == HAL_DMA_STATE_READY`
before executing some functions.
HAL Driver before v1.4.2 had a bug which caused clearing all pending
flags in MSR, TSR, RF0R and RF1R instead of only the requested one.

This is why micropython got away without explicitly clearing flags
in IRQ handler.
Use custom handlers providing minimal required functionality
because those provided by ST increase code size by almost 2 KiB.
stdio.h was included in all HAL files only to provide
definition of NULL symbol

"stdio.h" includes "types.h" which contains some conflicting definitions
with "drivers/cc3000/inc/socket.h"
@blazewicz
Copy link
Contributor Author

rebased & reorganized

gitlog:

* 4d9dce7 (HEAD -> stmhal-cube-1.13.0, origin/stmhal-cube-1.13.0) stmhal/mphalport.h: use single GPIOx->BSRR register
* 13400e1 stmhal/hal: do not include <stdio.h> in HAL headers
* 8fa0733 stmhal/i2c: provide custom IRQ handlers
* 7604de3 stmhal/can: clear FIFO flags in IRQ handler
* dc1ac5d stmhal/dma: mark DMA sate as READY even if HAL_DMA_Init is skipped
* 63ca7a2 stmhal/dma: precalculate register base and bitshift on handle init
* 0280b2c stmhal/i2c: handle I2C IRQs
* fa833f9 stmhal/make-stmconst.py: fix regex's to work with current CMSIS
* 7928b3e stmhal/boards: configure all F4 boards to work with new HAL
* 6a8f6c1 stmhal/hal/sd: reapply HAL commit 09de030 for f4
* e2b4822 stmhal/hal/rcc: reapply HAL commit c568a2b for f4
* 4f7c5fa stmhal/hal: reapply HAL commit 9db719b for f4
* c79ff99 stmhal/hal/sd: reapply HAL commit 1d7fb82 for f4
* a9fb88e stmhal/hal/i2c: reapply HAL commit ea040a4 for f4
* e8b435d stmhal: apply STM32CubeF4 v1.13.1 patch - upgrade HAL driver to v1.5.2
* c1fa33b stmhal: upgrade to STM32CubeF4 v1.13.0 - HAL v1.5.1
* 4f5c4fd stmhal: upgrade to STM32CubeF4 v1.13.0 - CMSIS/Device 2.5.1
* 32e9825 (upstream/master, origin/master, origin/HEAD, master) windows: Enable READER_POSIX to get access to lexer_new_from_file.

@dpgeorge dpgeorge merged commit 4d9dce7 into micropython:master Nov 16, 2016
@dpgeorge
Copy link
Member

Merged, see 4d9dce7 for last commit.

A very big thanks @blazewicz! Thanks for great commit messages and splitting the commits as appropriate.

There are 2 bugs (so far...):

  • I2C DMA is not working (but by default I2C doesn't use DMA)
  • pyb.LED().intensity seems broken for LEDs 3 and 4: if you run tests/pyb/led.py then it doesn't pulse the LEDs correctly at the end; it's fixed if the delay between the calls to intensity is increased from 1ms to 2ms. Needs further investigation.

@blazewicz blazewicz deleted the stmhal-cube-1.13.0 branch February 28, 2017 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0