AdaCore / bb-runtimes

Source repository for the GNAT Bare Metal BSPs
Other
65 stars 51 forks source link

Add support for STM32F103 (Blue Pill) #45

Closed jcassette closed 3 years ago

jcassette commented 3 years ago

Hello This is the BSP from @morbos See https://github.com/morbos/embedded-runtimes/commit/26b9b4378bb2db98459dbdfe23dd5d303e2e6d00 I might test it on a Blue Pill if I have some time

jcassette commented 3 years ago

I was able to flash this project: bluepill_blink.zip It blinks the LED on the BluePill but very slowly My delay is surely inaccurate or maybe the clock is setup incorrectly RTS: zfp-stm32f103.zip

Fabien-Chouteau commented 3 years ago

Hello @jcassette, thanks for you contribution.

The delay has to be accurate before we can merge this PR. You can check the values of clock constants and clock setup.

jcassette commented 3 years ago

I have made a new busy wait in assembly but it is still too slow I reviewed the clock setup compared to the reference manual but for me it is ok I could use some help, maybe from @morbos ?

damaki commented 3 years ago

@jcassette I have some experience with similar STM32s so I thought I'd take a look in case I can help out. I looked at your clock setup code and noticed some things which might be causing problems:

The clock setup code otherwise looks OK to me. As far as I can tell the registers are being configured correctly to generate a 72 MHz system clock.

I looked at your bluepill_blink example and I can see that you are trying to busy loop for 1 second. But beware that the compiler will generate code that takes multiple cycles per loop iteration! So I would expect each loop to take several seconds to run, which isn't what you want.

You could try using the Ravenscar runtime features to perform a proper timed delay. Try running this example:

with Ada.Real_Time; use Ada.Real_Time;
with Interfaces.STM32.RCC; use Interfaces.STM32.RCC;
with Interfaces.STM32.GPIO; use Interfaces.STM32.GPIO;

procedure Main is
   Period : constant Time_Span := Seconds (1);
   Next : Time;
begin
   --  Enable clock for GPIO C
   RCC_Periph.APB2ENR.IOPCEN := 1;
   --  Configure PC13
   GPIOC_Periph.CRH.Val := 16#00100000#;
   -- Blink the LED
   Next := Clock;
   loop
      -- Set PC13 low for 1 second
      GPIOC_Periph.BSRR.BR.Val := 16#2000#;
      Next := Next + Period;
      delay until Next;

      -- Set PC13 high for 1 second
      GPIOC_Periph.BSRR.BS.Val := 16#2000#;
      Next := Next + Period;
      delay until Next;
   end loop;
end Main;

You will need to build the example with either the ravenscar-sfp or ravenscar-full version of your runtime.

Note: I haven't tried compiling or running this code, but I think I got it right.

jcassette commented 3 years ago

Hi @damaki and thank you for helping out :-) I have pushed some changes to update the SVD and remove unused code, but without luck I have also removed some startup code which seemed to be for Cortex M4 I am afraid I have no support for Ravenscar at the moment This is the project with the modified busy wait: bluepill_blink.zip You can see that the loop is 2 instructions long only with arm-eabi-objdump -d -S main

jcassette commented 3 years ago

@Fabien-Chouteau please can you reopen the PR so that we can see the updates ?

Fabien-Chouteau commented 3 years ago

@jcassette let us know when the BSP is fully working an we will be able to review it.

jcassette commented 3 years ago

Hello @Fabien-Chouteau Are you still interested in this PR and can you review it? The BSP is working, it was only my test program which was incorrect zfp-stm32f103.zip bluepill_blink.zip

Fabien-Chouteau commented 3 years ago

Hello @jcassette, Did you see this blog post about our new solution for micro-controller run-times? https://blog.adacore.com/ada-on-any-arm-cortex-m-device-in-just-a-couple-minutes

If you only need a ZFP run-time, this is a better option than doing the BSP here.