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

From Lazarus wiki
Jump to navigationJump to search
m (→‎See also: New section)
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{ARM Embedded Tutorial - Raspberry Pi Pico saying Hello via UART}}
 
{{ARM Embedded Tutorial - Raspberry Pi Pico saying Hello via UART}}
  
The next peripheral on the tutorial list is the UART.
+
== Introduction ==
  
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:
+
UART Interfaces are often used for debugging output, the pico is no exception to this rule.
  
<syntaxhighlight lang=pascal>
+
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.
    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;
+
Here's again our debugging setup on a breadboard, the picoprobe is on the right, the pico that will run our code is on the left:
    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;
+
[[File:picoPicoDebug_Steckplatine.png|800px]]
    begin
 
      result := (uart.fr and (1 shl 5)) = 0;
 
    end;
 
  
    procedure uart_puts(var uart : TUART_Registers; const s : string);
+
<syntaxhighlight lang=pascal>
    var
+
program uart;
      i : longWord;
+
{$MODE OBJFPC}
    begin
+
{$H+}
      for i := 1 to length(s) do
+
{$MEMORY 10000,10000}
      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
+
uses
      BAUD_RATE=115200;
+
  pico_uart_c,
      UART_TX_PIN=0;
+
  pico_gpio_c,
      UART_RX_PIN=1;
+
  pico_timer_c;
      GPIO_FUNC_UART=2;
+
const
    var
+
  BAUD_RATE=115200;
      i : longWord;
+
begin
    begin
+
  gpio_init(TPicoPin.LED);
      runtime_init;
+
  gpio_set_dir(TPicoPin.LED,TGPIODirection.GPIO_OUT);
      uart_init(uart0, BAUD_RATE);
+
  uart_init(uart0, BAUD_RATE);
      gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
+
  gpio_set_function(TPicoPin.GP0_UART0_TX, TGPIOFunction.GPIO_FUNC_UART);
      gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
+
  gpio_set_function(TPicoPin.GP1_UART0_RX, TGPIOFunction.GPIO_FUNC_UART);
      repeat
+
  repeat
        uart_puts(uart0, 'Hello, UART!'+#13+#10);
+
    gpio_put(TPicoPin.LED,true);
        for i := 0 to 300000 do ;
+
    uart_puts(uart0, 'Hello, UART!'+#13+#10);
      until 1=0;
+
    busy_wait_us_32(500000);
    end.
+
    gpio_put(TPicoPin.LED,false);
 +
    busy_wait_us_32(500000);
 +
  until 1=0;
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
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 unsigned integer division.
 
  
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.
 +
In this example we are using ansistrings, so the $MEMORY setting is important as we will use dynamically allocated AnsiStrings.
 +
To visually get confirmation that the app is running we additionally blink the LED on each turn of the main loop.
 +
 +
== See also ==
  
[[ARM Embedded Tutorial - FPC and the Raspberry Pi Pico|Back to main Page]]
+
* [[ARM Embedded Tutorial - FPC and the Raspberry Pi Pico|ARM Embedded Raspberry Pi Pico Tutorials]]

Latest revision as of 01:53, 30 December 2021

English (en)

Introduction

UART Interfaces are often used for debugging output, the pico is no exception to this rule.

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.

Here's again our debugging setup on a breadboard, the picoprobe is on the right, the pico that will run our code is on the left:

picoPicoDebug Steckplatine.png

program uart;
{$MODE OBJFPC}
{$H+}
{$MEMORY 10000,10000}

uses
  pico_uart_c,
  pico_gpio_c,
  pico_timer_c;
const
  BAUD_RATE=115200;
begin
  gpio_init(TPicoPin.LED);
  gpio_set_dir(TPicoPin.LED,TGPIODirection.GPIO_OUT);
  uart_init(uart0, BAUD_RATE);
  gpio_set_function(TPicoPin.GP0_UART0_TX, TGPIOFunction.GPIO_FUNC_UART);
  gpio_set_function(TPicoPin.GP1_UART0_RX, TGPIOFunction.GPIO_FUNC_UART);
  repeat
    gpio_put(TPicoPin.LED,true);
    uart_puts(uart0, 'Hello, UART!'+#13+#10);
    busy_wait_us_32(500000);
    gpio_put(TPicoPin.LED,false);
    busy_wait_us_32(500000);
  until 1=0;
end.


Connect your terminal program to the UART port and enjoy the message from your Pico. In this example we are using ansistrings, so the $MEMORY setting is important as we will use dynamically allocated AnsiStrings. To visually get confirmation that the app is running we additionally blink the LED on each turn of the main loop.

See also