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

From Lazarus wiki
Jump to navigationJump to search
Line 3: Line 3:
 
The next peripheral on the tutorial list is the UART.
 
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>
 
<syntaxhighlight lang=pascal>
    program Blinky;
+
program uart;
    {$L gpio.c.obj}
+
{$MODE OBJFPC}
    {$L uart.c.obj}
+
{$H+}
    {$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;
+
uses
    procedure gpio_init(gpio : longWord); external;
+
  pico_uart_c,
    procedure gpio_set_function(gpio:longWord;fn:longWord); external;
+
  pico_gpio_c,
    function uart_init(var uart : TUART_Registers; baudrate:longWord) : longWord; external;
+
  pico_timer_c;
 
+
const
    function uart_is_writable(var uart : TUART_Registers):boolean;
+
  BAUD_RATE=115200;
    begin
+
begin
      result := (uart.fr and (1 shl 5)) = 0;
+
  gpio_init(TPicoPin.LED);
    end;
+
  gpio_set_dir(TPicoPin.LED,TGPIODirection.GPIO_OUT);
 
+
  uart_init(uart0, BAUD_RATE);
    procedure uart_puts(var uart : TUART_Registers; const s : string);
+
  gpio_set_function(TPicoPin.GP0_UART0_TX, TGPIOFunction.GPIO_FUNC_UART);
    var
+
  gpio_set_function(TPicoPin.GP1_UART0_RX, TGPIOFunction.GPIO_FUNC_UART);
      i : longWord;
+
  repeat
    begin
+
    gpio_put(TPicoPin.LED,true);
      for i := 1 to length(s) do
+
    uart_puts(uart0, 'Hello, UART!'+#13+#10);
      begin
+
    busy_wait_us_32(500000);
        repeat
+
    gpio_put(TPicoPin.LED,false);
        until uart_is_writable(uart);
+
    busy_wait_us_32(500000);
        uart.dr := longWord(s[i]);
+
  until 1=0;
      end;
+
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.
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 01:09, 1 March 2021

English (en)

The next peripheral on the tutorial list is the UART.


program uart;
{$MODE OBJFPC}
{$H+}

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.

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 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.

Back to main Pico Page