Recent

Author Topic: LCD Displays on Pi or Beaglebone  (Read 5615 times)

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #15 on: July 21, 2020, 01:08:26 am »
I think the only way to get to the point where I can use Lazarus in the same way that Python is used on the Beagle is to first climb up the learning curve by doing many of the projects in the Second Edition of Derek Molloy's "Exploring Beaglebone".  I also have the First Edition but I think much of the Device Tree stuff has changed.

The attached program is only relevant for a Beagle because the Pi doesn't have the on board USRx LEDs.  But as I work through each of the sample Python programs I'll see what I can do to make them work on both systems.

The attached program is a command line Lazarus TCustomApplication.  Written and debugged on a PC running WIN-7 so there's the ifdef
Code: Pascal  [Select][+][-]
  1.  
  2.   {$IFDEF WIN64}
  3.     LED3_PATH := 'c:/Temp/sys/class/leds/beaglebone/usr3';
  4.   {$ELSE}
  5.     LED3_PATH := '/sys/class/leds/beaglebone:green:usr3';
  6.   {$ENDIF}
  7.  
to set the correct paths to the hardware or simulated files.  There are also two example__.txt file with the examples showing it running on both Windows and the Beagle.

I've also included Derek Molloy's original python example.

This set of mini projects may well be better on a separate thread.  I'm open to suggestions as to where to put it.  I've not added a lot of documentation to the program as the book really explains what it's all about.
John



jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #16 on: July 21, 2020, 04:23:58 am »
Reading further into the "Exploring Beaglebone" Edition 2, I find there is also a similar example to control all 4 on board LEDs but this time written in C++ called makeLEDOOP for Make LEDs OOP.

Using the previous program converted from the Python example I created an LED object and modeled the Lazarus program after the C++ other than keeping the TCustomApplication.

Ran in simulated mode on the PC.  copied the 3 files to the Beagle and compiled it there.  Ran perfectly.  No problems.

From Chapter 5 Example 5-17.

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #17 on: July 21, 2020, 09:59:05 am »
I thought I'd whip out the little sample program on page 260 that shows what bitwise operations do.  Of course it's C++ and I'm using Lazarus so I whipped up "Bits".  After a bit of fine tuning I suddenly realized I'd fallen into the Linux Command line trap.  I hate command line programs.  And here I was writing one and using a ReadLn at the end to keep the window from closing when I tested.

The whole point of Lazarus is that memory is cheap and user's time is not.  Spend a minute typing every 10 minutes and in 10 hours you've lost an hour.  And the silly thing is it didn't take much longer to create "BitsForm"

So here's the source for both.  One's a command line program and on the Beagle compile to 111K while the forms one is almost 600K.  But memory is cheap!  Haven't tried this on the Pi but since it's not hardware sensitive it should compile and work there too.

Hints are enabled so hover over the fields to see how to use it.  No need to type in the command  and try -? or -help  or -h to discover only --help dumps out a few lines of information.

Yes.  Forms based programs take longer to make with all the various 'features' but are so much more user friendly.

John

avra

  • Hero Member
  • *****
  • Posts: 2129
    • Additional info
Re: LCD Displays on Pi or Beaglebone
« Reply #18 on: July 21, 2020, 10:37:57 am »
But the LCL and the link to
https://wiki.freepascal.org/Hardware_Access
seem more to address windows parallel ports than what is required for the Beagle.
2nd link is more adequate for your needs, especially this subtopic:
https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware
I admit that maybe it should have been put at first place, but that's the way it is. You will find there several libs/ways for accessing hardware, and I think that some might even work on both Pi and BB (try 1, 2 and 7).

I think that would be far more convenient then converting examples from Exploring BeagleBone book. Anyway, that's up to you to decide. If you go the book way, then the same author has written Exploring Raspberry Pi book. Might interest you.

If you need to access or manipulate bits in a byte/word/long/quad, then you might want to take a look at BitHelpers library from my signature.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #19 on: July 21, 2020, 05:38:46 pm »
Thank you for your feedback.  The Pi is easy to work with for hardware.  I did a project a few years ago where CAN messages needed to be captured from power on.  The solution was to use a PIC32 which grabbed and timestamped the messages.  About 18 seconds later after the PiZeroW had booted my application on pulled up the logged CAN messages via SPI.  They were stored to a file and my customer then accessed them to put the logged files up into a cloud database when the vehicle was near WiFi or Cell service.
 
The BBB has the PRUs, crappy video, but also built in CAN bus. And how the hardware is accessed is quite different from the Pi.   

And I didn't really need to write the Bit access code.  I've learned the hard way over the years it's far smarter to not just dive in and do 'the project'.  That puts blinders on and I learn very little because I don't know what I don't know.  So while somewhat isolated due to COVID-19 it's a perfect opportunity to use Derek Molloy's book to revisit the Beagle but do _everything_ with Lazarus. This forces me to use it ways I wouldn't normally do things. 

And along the way if no one minds, I'll post the code examples.  And as per the thread topic, eventually have a simple and understandable method of creating small embedded stand alone systems that use embedded graphical displays and touch screens all done with Lazarus. 

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #20 on: May 10, 2021, 11:06:34 pm »
So it's been a while.  Lot's of things happening since the last time I posted which includes discovering an issue in one of the dispaly libraries.

Anyway, I'm back to trying to create a common I/O interface with the hardware and decided to take a stab at the PXL library for Lazarus.  The code compiles and runs properly on a PC with WIN-10.

Deep down into bunxh.inc the call
    Function  FpIOCtl      (Handle:cint;Ndx: TIOCtlRequest; Data: Pointer):cint; external name  'FPC_SYSC_IOCTL';

in the example
\\EBB\ebbHome\lazarus\pxl\Samples\FreePascal\SingleBoard\Generic\DisplaySPI folder

Code: Pascal  [Select][+][-]
  1. function TSysfsSPI.Transfer(const ReadBuffer, WriteBuffer: Pointer; const BufferSize: Cardinal): Cardinal;
  2. var
  3.   Data: spi_ioc_transfer;
  4.   Res: Integer;
  5. begin
  6.   if ((ReadBuffer = nil) and (WriteBuffer = nil)) or (BufferSize <= 0) then
  7.     raise ESysfsInvalidParams.Create(SInvalidParameters);
  8.  
  9.   Data.tx_buf := PtrUInt(WriteBuffer);
  10.   Data.rx_buf := PtrUInt(ReadBuffer);
  11.   Data.len := BufferSize;
  12.   Data.delay_usecs := 0;
  13.   Data.speed_hz := FFrequency;
  14.   Data.bits_per_word := FBitsPerWord;
  15.  
  16.   Res := fpioctl(FHandle, SPI_IOC_MESSAGE(1), @Data);
  17.   if Res < 0 then
  18.     raise ESysfsSPITransfer.Create(Format(SCannotSPITransferBytes, [BufferSize]));
  19.  
  20.   Result := Cardinal(Res);
  21. end;
  22.  
Quote
debian@ebb:~/lazarus/pxl/Samples/FreePascal/SingleBoard/Generic/DisplaySPI$ sudo ./DisplaySPI
[sudo] password for debian:
An unhandled exception occurred at $00032BB0:
                                             ESysfsSPITransfer: Cannot transfer <1> data byte(s) through SPI bus.
                                   $00032BB0  TSYSFSSPI__TRANSFER,  line 247 of /home/debian/lazarus/pxl/Source/PXL.Sysfs.SPI.pas
                                                   $00032AC4  TSYSFSSPI__WRITE,  line 228 of /home/debian/lazarus/pxl/Source/PXL.Sysfs.SPI.pas


As I understand it, fpioctl is a free pascal function and part of the system library.  The cannot transfer error doesn't say anything more than "Doesn't work!"  Not why.

I'm working with the BeagleBone group to configure the system so that sudo isn't needed for access to gpio pins so I can install break points etc.

Later today I'll write a very simple Lazarus program that just sets the GPIO48 pin to output and toggles it on a timer.  But that the code in the PXL sample makes it past the configuration of the GPIO48 and GPIO60 pins for DC and RESET on the LCD display suggests there is a different problem.

Looking for suggestions right now.
Thanks
John

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #21 on: May 11, 2021, 09:34:55 am »
Here's a pretty simple output test program in Lazarus for the BBB.  With an LED connected to the P9-15 (gpio48) it toggles on and off and the memo form shows the operations as they are done.

This program was derived from:
Code: Pascal  [Select][+][-]
  1. {Demo application for GPIO on Raspberry Pi}
  2. {Inspired by the Python input/output demo application by Gareth Halfacree}
  3. {written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5}
  4. { From https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware }
  5. {
  6. Modified for Beagle P9-15 (GPIO48) MAY2021 by:
  7. John Dammeyer
  8. Automation Artisans Inc.
  9. johnd@autoartisans.com
  10.  
  11.   Expanded information reported on LogMemo so it's clear who is reporting what
  12.   Note.  Run from command line with sudo in order to be able to work with /sys/class/gpio
  13. }
  14.  

As yet I have not yet figured out the issue with fpioctl() and why I get faults.  It might well be again something to do with permissions.
John

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #22 on: May 26, 2021, 02:46:59 am »
As yet I have not yet figured out the issue with fpioctl() and why I get faults.  It might well be again something to do with permissions.
John
Alright.  Been a few days and I'm closer to getting the LCD display working with the Pi and Beagle via the pxl library.  I've now got the GPIO group permissions set on the beagle so that I can run the applications from inside the IDE.  But the PXL.sysfs.SPI library would fail consistently on both the Pi and the Beagle during the Transfer() function which calls fpioctl().

So I took a step back and connected one of these:
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/spi-sensors-devices
to the Pi and got it working with the Python Code.

The Scope photo is actually from the Lazarus application.  I've included the project and the modified pxl file.
In essence the problem with the fpioctl is that the data structure passed has to be filled with zeros first.
Code: Pascal  [Select][+][-]
  1.  
  2.   FillByte(Data, Sizeof(Data), 0);  // Without this FillByte() fpioctl fails.
  3.  
  4.   Data.tx_buf := PtrUInt(WriteBuffer);
  5.   Data.rx_buf := PtrUInt(ReadBuffer);
  6.   Data.len := BufferSize;
  7.   Data.delay_usecs := 0;
  8.   Data.speed_hz := FFrequency;
  9.   Data.bits_per_word := FBitsPerWord;
  10.  

So that solves one problem in that now at least the data flows out the SPI bus but on the Pi still needs to be run from the command line with sudo.
Quote
pi@raspberrypi:~/projects/lazarus/TC $ sudo ./TC
Reading temperature, press any key to exit...
Temperature: 21.75C 71.15F
Temperature: 21.75C 71.15F
Temperature: 21.75C 71.15F

The problems happen here with the fast GPIO in teh pxl Samples folder for
\\RASPBERRYPI\Pi3Home\projects\lazarus\pxl\Samples\FreePascal\SingleBoard\RaspberryPI\DisplaySPI
 
Code: Pascal  [Select][+][-]
  1. begin
  2.   SystemCore := TFastSystemCore.Create;
  3.   try
  4.     GPIO := TFastGPIO.Create(SystemCore);
  5.  
I haven't yet tried this TC application on the Beagle yet.

Any suggestions?
« Last Edit: May 26, 2021, 03:00:27 am by jcdammeyer »

avra

  • Hero Member
  • *****
  • Posts: 2129
    • Additional info
Re: LCD Displays on Pi or Beaglebone
« Reply #23 on: May 26, 2021, 02:45:05 pm »
I must admit that I am confused what works for you and what doesn't. Do you have both GPIO and SPI working but only with sudo? Is that the case? Have you added your user to spi and gpio groups? For debugging purpose you could also temporary add your user to sudo users as mentioned here:
https://www.cb-net.co.uk/linux/creating-a-new-raspberry-pi-raspbian-user-with-gpio-access/

I have not yet figured out the issue with fpioctl() and why I get faults.  It might well be again something to do with permissions.
fpioctl should be mapped to linux ioctl calls, so this might be helpful for understanding SPI_IOC_MESSAGE and debugging:
https://www.kernel.org/doc/Documentation/spi/spidev
https://www.kernel.org/doc/Documentation/spi/spi-summary
https://stackoverflow.com/questions/54923582/spi-ioc-messagen-on-raspberry-pi-3

I would check wiring, chip select and data bits, and of course display's datasheet (including display support for 8MHz SPI as is fixed in PXL).

I know you've mentioned that you don't want desktop showing on the display, but I must say that it is a much easier and less challenging route (someone might ask what's the fun in that ;)), so I will give a link to a cheap Pi touch display that works as advertised after you follow steps to let Linux use it as a default display. I haven't tried to use display over SPI from my application - I simply let Linux handle all that so I could use LCL in it's full glory.
https://www.ebay.com/itm/362836286867

It is fun to look at Linux desktop on a small 480x320 display (both portrait and landscape modes are possible). As mentioned earlier, desktop can be skipped and Linux can be tuned to boot directly to your app.

You might also want to check if you're lucky and your display is already supported by that driver:
https://github.com/goodtft/LCD-show
« Last Edit: May 26, 2021, 03:20:35 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #24 on: May 26, 2021, 07:15:13 pm »
I must admit that I am confused what works for you and what doesn't.
Here's the rundown.
I now have with slight modifications to the code the SPI based thermocouple reading program working on both systems.  Modifications like /dev/spidev0.0 verses /dev/spi/0.0 and which GPIO pins are used since they are named differently.  But all this works now!

The Pi does belong to the gpio group.
Quote
pi@raspberrypi:~/projects/lazarus/TC $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev lpadmin gpio i2c spi
But still requires sudo to run the application as shown in the previous posting while the BBB now can debug from both inside and out of the IDE.
Here's the BBB accessing the thermocouple board.
Quote
debian@ebb:~/lazarus/TC$ ./TC
Reading temperature, press any key to exit...
Temperature: 21.5C 70.7F
Temperature: 21.5C 70.7F
I'm going to have to go back through my postings to the Beaglebone group to see what else I may have done to get it to work without sudo.  It too was having that problem but no longer.

With respect to the graphics and LCD displays the issue is more complicated.  The RPi pxl library uses fast SPI io features that require PXL.Boards.RPi.pas and there is no equivalent library of routines for the BBB to directly deal with the hardware registers.  So the BBB will be slower.

However, the \lazarus\pxl\Samples\FreePascal\SingleBoard\Generic\DisplaySPI doesn't use those and now does compile and run on the Beagle.  Just don't have a display connected yet to verify operation and to see if the Lenna.jpg shows up. 

The point is that the pxl spi transfer() function has been fixed so programs using it now work with stretch and buster on the beagle and on the Pi.  I'm still investigating why the FillByte() function is required.

You asked about showing the screen.  I have a small 64x64 bit SPI display.  I also have a PiZeroW with the EnviroPi board with display.  The code is all Python.  I want to port it to Lazarus.  (Actually I also have 320x240 SPI screens and some others).

I also want to port this:https://github.com/botheredbybees/kilnController
along with it's parent reflow oven controller to Lazarus.

The more of these types of projects out there the more we can bring people into the much easier Delphi/Lazarus programming world.  IMHO.


engkin

  • Hero Member
  • *****
  • Posts: 2815
Re: LCD Displays on Pi or Beaglebone
« Reply #25 on: May 26, 2021, 08:50:50 pm »
Regarding fpioctl failure, the returned value is important. I just checked the source code for spidev. fpioctl is going to end in spidev_ioctl.

spidev_ioctl which it checks for and the errno it could return:
 SPI_IOC_MAGIC ===> -ENOTTY
 access right (_IOC_WRITE in this case) ===> -EFAULT
 device availability ===> -ESHUTDOWN
 correct size: (the size) mod sizeof(struct spi_ioc_transfer) = 0 ===> -EINVAL
 available kernel memory ===> -ENOMEM
 copy the data kernel memory ===> -EFAULT

and finally it executes the command.


As you noted in your previous post, the driver path could be wrong. You might want to correct this code in PXL sample:
Code: Pascal  [Select][+][-]
  1. constructor TApplication.Create;
  2. begin
  3.   FGPIO := TSysfsGPIO.Create;
  4.   FDataPort := TSysfsSPI.Create('/dev/spidev1.0');

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #26 on: May 26, 2021, 09:55:29 pm »
Regarding fpioctl failure, the returned value is important. I just checked the source code for spidev. fpioctl is going to end in spidev_ioctl.

spidev_ioctl which it checks for and the errno it could return:
and finally it executes the command.


As you noted in your previous post, the driver path could be wrong. You might want to correct this code in PXL sample:
Code: Pascal  [Select][+][-]
  1. constructor TApplication.Create;
  2. begin
  3.   FGPIO := TSysfsGPIO.Create;
  4.   FDataPort := TSysfsSPI.Create('/dev/spidev1.0');

I'll check that error number.

I've tested it with both.  /dev/spidev1.0 and /dev/spi/0.0 and either is fine.  Not sure why the Beagle creates spidev1.0 while the Pi does spidev0.0.  One of those little mysteries I guess.

For now I'm investigating how portable the examples are for the different types of displays.  I've added this into the uses part of DisplaySPI app but at the moment work-work calls...
Code: Pascal  [Select][+][-]
  1. {$IFDEF DISPLAY_ILI9340}  
  2.   PXL.Displays.ILI9340;
  3. {$ELSE}
  4. PXL.Displays.SSD1351;
  5. {$ENDIF}
  6.  

John

engkin

  • Hero Member
  • *****
  • Posts: 2815
Re: LCD Displays on Pi or Beaglebone
« Reply #27 on: May 26, 2021, 10:01:35 pm »
One of the links posted above by Avra explains:
Quote
For a SPI device with chipselect C on bus B, you should see:
/dev/spidevB.C
« Last Edit: May 26, 2021, 10:05:04 pm by engkin »

jcdammeyer

  • Full Member
  • ***
  • Posts: 161
  • Embedded System Developer
    • Automation Artisans Inc.
Re: LCD Displays on Pi or Beaglebone
« Reply #28 on: May 26, 2021, 10:59:48 pm »
One of the links posted above by Avra explains:
Quote
For a SPI device with chipselect C on bus B, you should see:
/dev/spidevB.C
That document is written for the low level Linux expert and doesn't help someone who has no idea how to 'bind' something.  Nor does it give any clue as to whether it's referring to a Beagle or a Pi or one of the other ARM based systems.  Unfortunately.
For example:
Which one to choose?
pi@raspberrypi:~/projects/lazarus/TC $ ls /dev/spi*
/dev/spidev0.0  /dev/spidev0.1


Or here:
debian@ebb:~/lazarus/TC$ ls /dev/spi*
/dev/spidev1.0  /dev/spidev1.1  /dev/spidev2.0  /dev/spidev2.1

/dev/spi:
0.0  0.1  1.0  1.1

We know from reading Python programs (and some C examples) that for the Pi we use 0.0.  For the Beagle unless you disable the HDMI you don't have access to the second SPI port so theoretically it should also be 0.0 but not quite...


avra

  • Hero Member
  • *****
  • Posts: 2129
    • Additional info
Re: LCD Displays on Pi or Beaglebone
« Reply #29 on: May 27, 2021, 08:37:16 am »
Which one to choose?
pi@raspberrypi:~/projects/lazarus/TC $ ls /dev/spi*
/dev/spidev0.0  /dev/spidev0.1

Or here:
debian@ebb:~/lazarus/TC$ ls /dev/spi*
/dev/spidev1.0  /dev/spidev1.1  /dev/spidev2.0  /dev/spidev2.1

/dev/spi:
0.0  0.1  1.0  1.1
If we have a system with a single SPI bus where 3 chip select lines are available for that bus, we would have 0.0, 0.1 and 0.2. Choosing one of them will actually just select which one of the 3 predefined chip select lines will be used.

If we have a system with 3 SPI buses where buses 1 and 2 have 2 chip select lines each, while bus 3 has 4 chip select lines, then we would have 0.0 0.1 for the first bus, 1.0 1.1 for the 2nd bus, and 2.0 2.1 2.2 2.3 for the third bus. Choosing to connect our device to 2.3 would mean that we need to take a look at the documentation to find where are SPI3 MOSI/MISO/SCLK pins, and where is SPI3 CE3 pin. So, our software for 2.3 device will work only if we connect it to appropriate pins.

You can find more info here:
https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md
https://www.emcraft.com/stm32f429discovery/accessing-spi-devices-in-linux

I also want to port this:https://github.com/botheredbybees/kilnController
along with it's parent reflow oven controller to Lazarus.
Nice and useful Pi project, although I would have personally done it with ESP32.

The more of these types of projects out there the more we can bring people into the much easier Delphi/Lazarus programming world.  IMHO.
That's why I decided to bring SocketCAN to FreePascal when I saw in the forum thread (which has started it all) that many tried but gave up because of lack of SocketCAN experience, although I had everything I needed in C and Python already.

Btw. Although PXL has set SPI default speed at 8MHz for the Pi, according to https://elinux.org/RPi_SPI it seams that Pi will actually use 7.8MHz (because of the clock divider 32). You could enable SPI_DIAGNOSTICS define in PXL.Sysfs.SPI.pas to check that on console output. That link also explains how to do SPI loopback test and how to send bytes from command line to SPI.
« Last Edit: May 27, 2021, 09:05:32 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018