Difference between revisions of "ARM Embedded Tutorial - Raspberry Pi Pico saying Hello via UART"

From Free Pascal wiki
Jump to navigationJump to search
(Add page template; categorise page (in template); add syntax highlighting; fix typos)
Line 1: Line 1:
Next peripheral on the list is the UART.
+
{{ARM Embedded Tutorial - Raspberry Pi Pico saying Hello via UART}}
  
You will now start to find a pattern, we add some objectfiles, implement a few dependencies that were defined as inline in the C-Code and in the end we have our app that outputs test via UART0 to the Pins 0+1 of the Pico:
+
The next peripheral on the tutorial list is the UART.
  
 +
You will now start to notice a pattern: we add some object files, implement a few dependencies that were defined as inline in the C code and in the end we have our application that outputs test via UART0 to Pins 0+1 of the Pico:
 +
 +
<syntaxhighlight lang=pascal>
 
     program Blinky;
 
     program Blinky;
 
     {$L gpio.c.obj}
 
     {$L gpio.c.obj}
Line 12: Line 15:
 
     {$L platform.c.obj}
 
     {$L platform.c.obj}
 
     {$LinkLib gcc,static}
 
     {$LinkLib gcc,static}
 +
 
     procedure clocks_init; external;
 
     procedure clocks_init; external;
 
     procedure gpio_init(gpio : longWord); external;
 
     procedure gpio_init(gpio : longWord); external;
 
     procedure gpio_set_function(gpio:longWord;fn:longWord); external;
 
     procedure gpio_set_function(gpio:longWord;fn:longWord); external;
 
     function uart_init(var uart : TUART_Registers; baudrate:longWord) : longWord; external;
 
     function uart_init(var uart : TUART_Registers; baudrate:longWord) : longWord; external;
 +
 
     function uart_is_writable(var uart : TUART_Registers):boolean;
 
     function uart_is_writable(var uart : TUART_Registers):boolean;
 
     begin
 
     begin
 
       result := (uart.fr and (1 shl 5)) = 0;
 
       result := (uart.fr and (1 shl 5)) = 0;
 
     end;
 
     end;
 +
 
     procedure uart_puts(var uart : TUART_Registers; const s : string);
 
     procedure uart_puts(var uart : TUART_Registers; const s : string);
 
     var
 
     var
Line 31: Line 37:
 
       end;
 
       end;
 
     end;
 
     end;
 +
 
     procedure runtime_init;
 
     procedure runtime_init;
 
     const
 
     const
Line 46: Line 53:
 
       until (resets.reset_done and RESETS_POSTCLOCK_BITS) = RESETS_POSTCLOCK_BITS;
 
       until (resets.reset_done and RESETS_POSTCLOCK_BITS) = RESETS_POSTCLOCK_BITS;
 
     end;
 
     end;
 +
 
     const
 
     const
 
       BAUD_RATE=115200;
 
       BAUD_RATE=115200;
Line 63: Line 71:
 
       until 1=0;
 
       until 1=0;
 
     end.
 
     end.
 +
</syntaxhighlight>
  
one notable change is that you may have seen that we do not provide __aeabi_uidiv anymore, instead we link to libgcc.a and get (hopefully) best possible performnce for the div....
+
One notable change is that you may have seen that we do not provide <tt>__aeabi_uidiv</tt> anymore, instead we link to <tt>libgcc.a</tt> and get (hopefully) best possible performance for the div.
  
This application is best tested with picoprobe, when you connect it to your development board as described in the Getting Started Guide chapter then the gpio pins 0 and 1 are connected to the debug probe which makes them visible as a serial interface on your computer.
+
This application is best tested with picoprobe, when you connect it to your development board as described in the Getting Started Guide chapter then the GPIO pins 0 and 1 are connected to the debug probe which makes them visible as a serial interface on your computer.
  
Connect your terminal program to the UART port and enjoy the message from your pico...
+
Connect your terminal program to the UART port and enjoy the message from your Pico.

Revision as of 02:18, 1 February 2021

English (en)

The next peripheral on the tutorial list is the UART.

You will now start to notice a pattern: we add some object files, implement a few dependencies that were defined as inline in the C code and in the end we have our application that outputs test via UART0 to Pins 0+1 of the Pico:

    program Blinky;
    {$L gpio.c.obj}
    {$L uart.c.obj}
    {$L clocks.c.obj}
    {$L xosc.c.obj}
    {$L pll.c.obj}
    {$L watchdog.c.obj}
    {$L platform.c.obj}
    {$LinkLib gcc,static}

    procedure clocks_init; external;
    procedure gpio_init(gpio : longWord); external;
    procedure gpio_set_function(gpio:longWord;fn:longWord); external;
    function uart_init(var uart : TUART_Registers; baudrate:longWord) : longWord; external;

    function uart_is_writable(var uart : TUART_Registers):boolean;
    begin
      result := (uart.fr and (1 shl 5)) = 0;
    end;

    procedure uart_puts(var uart : TUART_Registers; const s : string);
    var
      i : longWord;
    begin
      for i := 1 to length(s) do
      begin
        repeat
        until uart_is_writable(uart);
        uart.dr := longWord(s[i]);
      end;
    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;

    const
      BAUD_RATE=115200;
      UART_TX_PIN=0;
      UART_RX_PIN=1;
      GPIO_FUNC_UART=2;
    var
      i : longWord;
    begin
      runtime_init;
      uart_init(uart0, BAUD_RATE);
      gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
      gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
      repeat
        uart_puts(uart0, 'Hello, UART!'+#13+#10);
        for i := 0 to 300000 do ;
       until 1=0;
    end.

One notable change is that you may have seen that we do not provide __aeabi_uidiv anymore, instead we link to libgcc.a and get (hopefully) best possible performance for the div.

This application is best tested with picoprobe, when you connect it to your development board as described in the Getting Started Guide chapter then the GPIO pins 0 and 1 are connected to the debug probe which makes them visible as a serial interface on your computer.

Connect your terminal program to the UART port and enjoy the message from your Pico.