ARM Embedded Tutorial - Raspberry Pi Pico running at full CPU speed

From Lazarus wiki
Revision as of 03:54, 31 January 2021 by MiR (talk | contribs) (Created page with "In the simple Blinky example we did the bare minimum that is necessary to blink a LED. Now we increase CPU speed to full power with the help of the init_clock procedure from...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

In the simple Blinky example we did the bare minimum that is necessary to blink a LED.

Now we increase CPU speed to full power with the help of the init_clock procedure from clocks.c.obj

First step we need to do is to add the clocks.c.obj file to our code. But when we do that and compile the code we are left again with linking errors because clocks.c.obj has dependencies to other code parts.

After fiddeling arround a bit we end up with the needed list of dependencies to link in:

   program Blinky;
   {$L platform.c.obj}
   {$L gpio.c.obj}
   {$L clocks.c.obj}
   {$L xosc.c.obj}
   {$L pll.c.obj}
   {$L platform.c.obj}
   {$L watchdog.c.obj}

Now we add clocks_init to our code and we are also now able to initialize all peripherals:

   procedure runtime_init;
   const
     RESETS_SAFE_BITS=     %1111111111100110110111111;
     RESETS_PRECLOCK_BITS= %0001111000100110110111110;
     RESETS_POSTCLOCK_BITS=%1110000111000000000000001;
   begin
     resets.reset_set := RESETS_SAFE_BITS;
     resets.reset_clr := RESETS_PRECLOCK_BITS;
     repeat
     until (resets.reset_done and RESETS_PRECLOCK_BITS) = RESETS_PRECLOCK_BITS;
     clocks_init;
     resets.reset_clr := RESETS_POSTCLOCK_BITS;
     repeat
     until (resets.reset_done and RESETS_POSTCLOCK_BITS) = RESETS_POSTCLOCK_BITS;
   end;

we start the compiler again and there is still one dependency missing, so we add a bit of code to get this fixed:


   function __aeabi_uidiv(const numerator : uint32; denominator : uint32):uint32;  public name '__aeabi_uidiv';
   begin
     Result := numerator div denominator;
   end;

This dependency comes with the gcc compiler but we are lazy and use this simple (likely slower) alternative.

After this final step we are able to blink the LED's at full CPU speed, so we increase our delay loop because we are now faster with waiting:

   program Blinky;
   {$L platform.c.obj}
   {$L gpio.c.obj}
   {$L clocks.c.obj}
   {$L xosc.c.obj}
   {$L pll.c.obj}
   {$L watchdog.c.obj}
   procedure clocks_init; external;
   procedure gpio_init(gpio : longWord); external;
   function __aeabi_uidiv(const numerator : uint32; denominator : uint32):uint32;  public name '__aeabi_uidiv';
   begin
     Result := numerator div denominator;
   end;
   procedure runtime_init;
   const
     RESETS_SAFE_BITS=     %1111111111100110110111111;
     RESETS_PRECLOCK_BITS= %0001111000100110110111110;
     RESETS_POSTCLOCK_BITS=%1110000111000000000000001;
   begin
     resets.reset_set := RESETS_SAFE_BITS;
     resets.reset_clr := RESETS_PRECLOCK_BITS;
     repeat
     until (resets.reset_done and RESETS_PRECLOCK_BITS) = RESETS_PRECLOCK_BITS;
     clocks_init;
     resets.reset_clr := RESETS_POSTCLOCK_BITS;
     repeat
     until (resets.reset_done and RESETS_POSTCLOCK_BITS) = RESETS_POSTCLOCK_BITS;
   end;
   procedure gpio_set_dir(gpio : longWord; &out : longbool);
   var
     mask : longWord;
   begin
     mask := 1 shl  gpio;
     if out = true then
       sio.gpio_oe_set := mask
     else
       sio.gpio_oe_clr := mask;
   end;
   procedure gpio_put(gpio : longWord; value : boolean);
   var
     mask : longWord;
   begin
     mask := 1 shl gpio;
     if value=true then
       sio.gpio_set := mask
     else
       sio.gpio_clr := mask;
   end;
   const
     LED_PIN=25;
     GPIO_OUT=true;
   var
     i : longWord;
   begin
     runtime_init;
     gpio_init(LED_PIN);
     gpio_set_dir(LED_PIN,GPIO_OUT);
     repeat
       gpio_put(LED_PIN,true);
       for i := 0 to 3000000 do ;
       gpio_put(LED_PIN,false);
       for i := 0 to 3000000 do ;
     until 1=0;
   end.