Lazarus для raspberry pi

Устанавливаем Lazarus на Raspberry Pi

Этим постом я начинаю цикл статей, посвященных использованию Lazarus на Raspberry Pi. Для начала просто установим его на «малинку». Как я уже писал, в Raspberry Pi OS имеется нормальная возможность устанавливать / удалять программы. Приличный оконный интерфейс без всяких там wget, tar, install. Хотя и без них, порой тоже не обойтись. Итак в главном меню открываем Preferences -> Add / Remove Software и видим следующее окно:

Здесь у меня в строке поиска уже введено «lazarus» и мы видим результаты поиска. Не мудрствуя лукаво, выбираем ВСЕ пункты касающиеся IDE, Free Pascal, Lazarus Components Library и т.д. Короче, не надо выбирать только Castle Game Engine. Хотя если хотите посмотреть, что это такое — то ради Бога, хуже не станет. Нажимаем Apply и среда разработки, компилятор и все компоненты автоматически устанавливаются 🙂 А в главном меню, в пункте Programming появляется новый подпункт Lazarus (2.0.0). Выбираем его и видим первоначальный экран настройки Lazarus’а:

Оставляем всё по умолчанию и жмем Start IDE. Теперь мы видим нашу привычную среду разработки Lazarus, теперь уже на Raspberry Pi!

На приведенном выше скриншоте вы можете видеть простейший проект «Hello Raspberry Pi» а-ля «Hello world». Проект был нормально скомпилирован и запущен. Теперь можно в своё удовольствие заниматься разработкой ПО на Raspberry Pi в любимой среде программирования 🙂

Источник

Lazarus on Raspberry Pi

This article applies to Raspberry Pi only.

The Raspberry Pi is a credit-card-sized single-board computer. It has been developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools. Raspberry Pis are also used for multiple other purposes that are as different as media servers, robotics and control engineering.

The Raspberry Pi Foundation recommends Raspbian as standard operating system. Alternative systems running on RPI include RISC OS and various Linux distributions, as well as Android and FreeBSD.

Lazarus runs natively under the Raspbian operating system.

Contents

Installing and compiling Lazarus

Correcting swap file size

(Info from forum user «Thaddy».) If you have RPi with memory size less then 4Gb, and you want to use FPCUPdeluxe, you need to adjust the swap file size before installing Lazarus:

  • sudo nano /etc/dphys-swapfile
  • in the file, find CONF_SWAPSIZE and change the value to 2048 or 1024.
  • save and restart.

Simple installation under Raspbian

Modern Raspbian versions

On modern versions of Raspbian installation is very easy. It can be performed with the PiPackage manager. You have to select simply «Add / Remove Software» in the global Preferences menu.

Step 1: Install Free Pascal with PiPackage.

Step 2: Install Lazarus

Lazarus is now available in the global «Programming» menu

A simple session with Lazarus on Raspbian Stretch.

Old Raspbian versions

Old versions of Raspbian OS (e.g. Wheezy) don’t provide the graphical installer. However, it is easy to install Lazarus and Free Pascal anyway. In order to do this simply open a terminal window and type:

Note: if sudo apt-get install fpc returns unable to find fpc, repeat the sudo apt-get update

This installs a precompiled, stable version of FPC and Lazarus on the Raspberry Pi. Of course, a network connection is required. Installation may take about 30 minutes, but major portions of this process take place automatically. After installation you may instantly start Lazarus from the «Programming» section of the LXDE start menu.

Lazarus «out of the box» on Raspberry Pi 2

Cross compiling for the Raspberry Pi from Windows

One way is to use fpcup to set up a cross compiler; follow these instructions: fpcup#Linux_ARM_cross_compiler

2. Using scripts

Alternatively, for a more manual approach using batch files, you can follow these steps.

2.2 Example Windows Batch file (adapt paths as needed)

With the resulting ppcrossarm.exe and ARM RTL you will be able to build a cross Lazarus version as usual and compile FPC projects for the Raspberry Pi and other armhf devices.

Remember that not all — especially Windows — libraries are available for Linux arm.

Cross compiling for the Raspberry Pi from Linux

Compiling from sources

You may want to compile Lazarus from subversion sources. See Michell Computing: Lazarus on the Raspberry Pi for details.

How to start Lazarus in Raspbian Wheezy

Lazarus on Raspberry Pi

Numbering of GPIO pins

Compiling from sources on Raspberry with Gentoo (and other distro)

If you want to install the latest stable release of fpc and, additional and isolated, the trunk fpc compiler: you can read the following guide. It was written using gentoo but this guide will be useful with any distro: Install fpc on Raspberry with Gentoo

Accessing external hardware

One of the goals in the development of Raspberry Pi was to facilitate effortless access to external devices like sensors and actuators. There are many ways to access the I/O facilities from Lazarus and Free Pascal:

  1. Direct access using the BaseUnix unit
  2. Access through encapsulated shell calls
  3. Access through the wiringPi library.
  4. Access through Unit rpi_hal.
  5. Access through Unit PiGpio.
  6. Access through the PascalIO library.
  7. Access through the PXL library.

Native hardware access

This method provides access to external hardware that doesn’t require additional libraries. The only requirement is the BaseUnix library that is part of Free Pascal’s RTL.

Switching a device via the GPIO port

The following example lists a simple program that controls the GPIO pin 17 as output to switch an LED, transistor or relays. This program contains a ToggleBox with name GPIO17ToggleBox and for logging return codes a TMemo called LogMemo.

For the example, the anode of a LED has been connected with Pin 11 on the Pi’s connector (corresponding to GPIO pin 17 of the BCM2835 SOC) and the LED’s cathode was wired via a 68 Ohm resistor to pin 6 of the connector (GND) as previously described by Upton and Halfacree. Subsequently, the LED may be switched on and off with the application’s toggle box.

The code may at first require to be run as root, i.e. either from a root account (not recommended) or via su. A better option is to add the user to the gpio group, the i2c group and the spi group.

Reading the status of a pin

Of course it is also possible to read the status of e.g. a switch that is connected to the GPIO port.

The following simple example is very similar to the previous one. It controls the GPIO pin 18 as input for a binary device like a switch, transistor or relais. This program contains a CheckBox with name GPIO18CheckBox and for logging return codes a TMemo called LogMemo.

For the example, one pole of a push-button has been connected to Pin 12 on the Pi’s connector (corresponding to GPIO pin 18 of the BCM2835 SOC) and via a 10k Ohm pull-up resistor with pin 1 (+3.3V, see wiring diagram). The other pole has been wired to pin 6 of the connector (GND). The program senses the status of the button and correspondingly switches the checkbox on or off, respectively.

Note that the potential of pin 18 is high if the button is released (by virtue of the connection to pin 1 via the pull-up resistor) and low if it is pressed (since in this situation pin 18 is connected to GND via the switch). Therefore, the GPIO pin signals 0 if the button is pressed and 1 if it is released.

This program has again to be executed as root.

The main program is identical to that of the example from above.

Hardware access via encapsulated shell calls

Another way to access the hardware is by encapsulating terminal commands. This is achieved by using the fpsystem function. This method gives access to functions that are not supported by the BaseUnix unit. The following code implements a program that has the same functionality as the program resulting from the first listing above.

The main program is identical to that of the example above. This program has to be executed with root privileges, too.

wiringPi procedures and functions

Alex Schaller’s wrapper unit for Gordon Henderson’s Arduino compatible wiringPi library provides a numbering scheme that resembles that of Arduino boards.

Function wiringPiSetup:longint: Initializes wiringPi system using the wiringPi pin numbering scheme.

Procedure wiringPiGpioMode(mode:longint): Initializes wiringPi system with the Broadcom GPIO pin numbering scheme.

Procedure pullUpDnControl(pin:longint; pud:longint): controls the internal pull-up/down resistors on a GPIO pin.

Procedure pinMode(pin:longint; mode:longint): sets the mode of a pin to either INPUT, OUTPUT, or PWM_OUTPUT.

Procedure digitalWrite(pin:longint; value:longint): sets an output bit.

Procedure pwmWrite(pin:longint; value:longint): sets an output PWM value between 0 and 1024.

Function digitalRead(pin:longint):longint: reads the value of a given Pin, returning 1 or 0.

Procedure delay(howLong:dword): waits for at least howLong milliseconds.

Procedure delayMicroseconds(howLong:dword): waits for at least howLong microseconds.

Function millis:dword: returns the number of milliseconds since the program called one of the wiringPiSetup functions.

rpi_hal-Hardware Abstraction Library (GPIO, I2C and SPI functions and procedures)

This Unit with around 1700 Lines of Code provided by Stefan Fischer, delivers procedures and functions to access the rpi HW I2C, SPI and GPIO:

Just an excerpt of the available functions and procedures:

procedure gpio_set_input (pin:longword);

procedure gpio_set_alt (pin,altfunc:longword);

function rpi_snr :string;

function rpi_hw :string;

function i2c_bus_write(baseadr,reg:word; var data:databuf_t; lgt:byte; testnr:integer) : integer;

function i2c_bus_read (baseadr,reg:word; var data:databuf_t; lgt:byte; testnr:integer) : integer;

function i2c_string_read(baseadr,reg:word; var data:databuf_t; lgt:byte; testnr:integer) : string;

function i2c_string_write(baseadr,reg:word; s:string; testnr:integer) : integer;

procedure SPI_Write(devnum:byte; reg,data:word);

function SPI_Read(devnum:byte; reg:word) : byte;

procedure SPI_BurstRead2Buffer (devnum,start_reg:byte; xferlen:longword);

procedure SPI_BurstWriteBuffer (devnum,start_reg:byte; xferlen:longword);

Test Program (testrpi.pas):

PiGpio Low-level native pascal unit (GPIO control instead of wiringPi c library)

This Unit (pigpio.pas[2]) with 270 Lines of Code provided by Gabor Szollosi, works very fast (for ex. GPIO pin output switching frequency 8 MHz) :

Controlling Lazarus unit:(Project files[3])

PXL (Platform eXtended Library) for low level native access to GPIO, I²C, SPI, PWM, UART, V4L2, displays and sensors

  • Easy and fast access to GPIO, I²C, SPI, PWM, UART and high precision CPU timer.
  • V4L2 video/image capture using onboard and USB cameras. Serial cameras such as VC0706 and LSY201.
  • OpenGL ES hardware rendering with and without X server. Software rendering.
  • I²C and SPI displays such as SSD1306, SSD1351, PCB8544 (Nokia), HX8357 and ILI9340.
  • Character LCDs with direct pin wiring.
  • Support for Software SPI and UART (bit-banging).
  • Support for SC16IS7x0 (UART controller connected via I²C or SPI), including extra GPIO pins.
  • Support for sensors such as BMP180, DHT22, L3GD20, LSM303 and SHT10.
  • Other features like networking, math, image transformation, effects, drawing primitives.

More info and download of PXL library.

The PinDrive property is not implemented in the latest download from Asphyre so I am posting the code for my implementation in case anyone needs it. I found that the definition for TPinDrive is incorrect for the BCM2837 chip so I added some code to change it. This could be accomplished more easily by changing the definition but that file could conceivably be used for multiple chips (and I don’t control the code) so I made all changes in the code to set the drive mode.

Источник

Adblock
detector