RaspberryPi Zero2W lazarus / 77-Embedded ---------------------------------------------------------------------- Mark20 · 2023-12-16 19:52 Hi, is it possible develop with Lazarus some console program that can use SPI and I2C interfaces on PiZero ? Somebody have developed anything on this board and have experiences to share ? Regards, Marco ---------------------------------------------------------------------- cdbc · 2023-12-16 23:11 Hi Maybe search for posts by @ccrause & @PascalByThree in this forum. They have been discussing something like this... Regards Benny ---------------------------------------------------------------------- dbannon · 2023-12-17 04:12 yes, should work fine. I have a app that uses 1-wire temp sensors, same general sort of thing, works perfectly. Have you looked at the lazarus wiki page on the RasPi ? Davo ---------------------------------------------------------------------- ccrause · 2023-12-17 05:07 Will this be an embedded program (no OS), or run in Linux? The sub forum suggests embedded? ---------------------------------------------------------------------- Mark20 · 2023-12-17 08:44 Quote from: dbannon on December 17, 2023, 05:12:39 amyes, should work fine. I have a app that uses 1-wire temp sensors, same general sort of thing, works perfectly. Have you looked at the lazarus wiki page on the RasPi ? thank you Davo, can you give me some links on wiki ?  I asked because the RpiZero2W is smaller than classic Rpi, so I don't know if enough Ram to run a Freepascal application... I have to use an MQTTclient and some DigI/O for data collection. @ccrause for "No OS" do you mean direct hw programming, in C like with microcontrollers ? I don't know how do this on this board (did a lot on ATmega256), but I'd like  much know where get information to develop on RpiZero in this way. Can you help ? Regards, Marco ---------------------------------------------------------------------- ccrause · 2023-12-17 09:43 Quote from: Mark20 on December 17, 2023, 09:44:03 am@ccrause for "No OS" do you mean direct hw programming, in C like with microcontrollers ? Yes, this is what I meant. I ask because you posted your question under the Embedded forum.  If you want to go the embedded route on the Pi Zero, consider the Ultibo project.  Raw embedded on an ARM processor is a bit more challenging than on an 8 bit AVR.  Ultibo provides a bunch of libraries and low level support to simplify the work you have to do for an embedded program. From your other questions it seems that you really want to run a Free pascal program on top of Linux. The link that Davo referred to is for use in Linux: https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Acce ssing_external_hardware ---------------------------------------------------------------------- Mark20 · 2023-12-17 10:29 Thank you ! have to study now :-) ---------------------------------------------------------------------- hansotten · 2023-12-17 11:15 Quote from: Mark20 on December 16, 2023, 08:52:57 pmHi, is it possible develop with Lazarus some console program that can use SPI and I2C interfaces on PiZero ? Somebody have developed anything on this board and have experiences to share ? Regards, Marco Yes, that will work. Use the fpcupdeluxe-armv6hf-linux variant. If you want to run on the Pi and not cross compile, enlarge the swapfile. There are nice libraries available, see the wiki. I have good results with rpihal but there is more. https://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi will help. Lazarus is a bit heavy for a Pi Zero. Freepascal apps  runs fine. ---------------------------------------------------------------------- MarkMLl · 2023-12-17 11:22 Quote from: hansotten on December 17, 2023, 12:15:00 pmLazarus is a bit heavy for a Pi Zero. Freepascal apps  runs fine. The bottom line however is that the amount of swap is probably the most important thing. Also try to move that off the SD-Card if possible, since that type of medium has little-to-no wear levelling and it won't last long in that role. A couple of thoughts on terminology: hopefully everybody appreciates that the Pi Zero family usually runs Linux, as distinct from the Pi Pico (RP2040) family which doesn't. And of course these days, it's common to consider Linux as a candidate OS for an embedded system, even if by historical standards it is vastly overweight and insufficiently reliable. MarkMLl ---------------------------------------------------------------------- Mark20 · 2023-12-17 18:12 @hansotten if Freepascal apps run fine, for me is enough. I can develop on Rpi or even PC with cross compile. Thank you ! @MarkMLl I need understand if for my app is necessary an OS (Rpi Zero2) or not (Rpi Pico), but now I have the paths to follow. Thank you, Marco ---------------------------------------------------------------------- dbannon · 2023-12-18 22:40 Yes Mark20, you probably would be disappointed running Lazarus on a Pi Zero, or even a Linux Desktop but for command line driven, no GUI, absolutely fine. Depending on what I am doing, I generally edit using Lazarus, because the IDE is really useful, and cross compile from my laptop. Then use sshfs from the Pi to mount that directory from my laptop on the Pi so its easy to run and test the compiled executable. Alternatively, you may be able to compile on the PI  zero but do keep in mind, as MarkMLI said, the SDcards and not made for repeated rewrites, and compiling does a lot of repeated rewrites. If you have a 'bigger' PI, at least a 3 with 2G ram, running Lazarus is practicable, just, (must increase swap to initially build Lazarus at least) and the (32bit ?) binary can be used, when perfected, in the zero. Davo ---------------------------------------------------------------------- MarkMLl · 2023-12-19 08:14 Quote from: dbannon on December 18, 2023, 11:40:52 pmAlternatively, you may be able to compile on the PI  zero but do keep in mind, as MarkMLI said, the SDcards and not made for repeated rewrites, and compiling does a lot of repeated rewrites. It's specifically swap that causes problems, not rewrites per se. It would be easy enough to have a script- a few lines or Perl or whatever- which detected that a .lpi file had been changed and ran lazbuild on it. MarkMLl ---------------------------------------------------------------------- dbannon · 2023-12-19 09:23 I think you do need to consider just the i/o on the SDcard too. Its just the total number of times a particular part of the card is written to. The SDCards do not have the load leveling circuitry that a SSD has to even out those writes over the whole thing. So, you can easily write repeatably to the same spot and they have a limited number of times each spot is written to. Yep, a particular problem with swap files but I always ensure, for example, my Lazarus Source is on a USB disk before I compile Lazarus.  (Its actually a conventional hard disk connected over USB, slower but the RasPi is no ball of fire anyway). Davo ---------------------------------------------------------------------- Thaddy · 2023-12-19 09:45 The swap is only necessary during compilation annd you can revert it  when your software is running properly. When compiling with FPC on the zero itself I change the swap from 128 to 512, when compiling with Lazarus too I use either 1024 or 2048 depending on components needed. Nowadays I merely use cross-compiling, though. It also helps if you use at least a class 10 card although a class 4 should still work. ---------------------------------------------------------------------- dbannon · 2023-12-19 10:06 I recently found, using Debian Bookworm on a PasPi4 with only 2G ram that I needed to increase swap to 1G when compiling Lazarus. The same hardware a year (?) ago running Debian Bullseye based Raspi OS worked 'out of the box', so I don't know if its because Lazarus has got bigger or the OS has got bigger, both true of course. But I did not reverse the increase, no point really, if it does not need that extra swap, it won't use it, if it does need it, its there. Using swap is always undesirable but preferable to crashing the system. There was a large HPC facility in this Nations capital where a good friend of mine would always kill any long running job that went into swap, I got a lot of credit at my facility by just sending a polite email. Davo ---------------------------------------------------------------------- MarkMLl · 2023-12-19 10:17 Quote from: dbannon on December 19, 2023, 11:06:16 amI recently found, using Debian Bookworm on a PasPi4 with only 2G ram that I needed to increase swap to 1G when compiling Lazarus. The same hardware a year (?) ago running Debian Bullseye based Raspi OS worked 'out of the box', so I don't know if its because Lazarus has got bigger or the OS has got bigger, both true of course. I'm not entirely happy with the performance of "Bookworm" on an x86_64, so that would be my first suspect. Let's blame it on systemd... MarkMLl ---------------------------------------------------------------------- Mark20 · 2023-12-21 21:25 Thank you for all tips ! About the "SD-Card that has little-to-no wear levelling and it won't last long", also a standard Rpi4 could have the same problem ? ---------------------------------------------------------------------- TRon · 2023-12-21 22:36 Quote from: Mark20 on December 21, 2023, 10:25:57 pmAbout the "SD-Card that has little-to-no wear levelling and it won't last long", also a standard Rpi4 could have the same problem ? Not could, but does have the same issue. The only difference is that a pi4 has the option to come in variations with more memory which reduces (or can reduce) the number of writes to the sd-card. If you have a pi4 with enough memory then consider compiling to a ram disk or use a (USB) attached storage device that is more robust. ---------------------------------------------------------------------- MarkMLl · 2023-12-22 08:43 Quote from: TRon on December 21, 2023, 11:36:06 pmQuote from: Mark20 on December 21, 2023, 10:25:57 pmAbout the "SD-Card that has little- to-no wear levelling and it won't last long", also a standard Rpi4 could have the same problem ? Not could, but does have the same issue. The only difference is that a pi4 has the option to come in variations with more memory which reduces (or can reduce) the number of writes to the sd-card. If you have a pi4 with enough memory then consider compiling to a ram disk or use a (USB) attached storage device that is more robust. Plus modern SD-Cards /might/ be more robust. But apart from that: /all/ devices which write to an SD-Card have the same problem unless they're using a filesystem designed- from the ground up- to have its own wear-levelling. Swap is particularly brutal, and this has been an issue since the Pi-1. I think it was the Pi-3 which introduced the ability to boot from USB- connected media, although it was disabled by default. But in any event it's never been difficult to boot from card but mount external media a the root device (i.e. /) although there's obviously a possibility that an version of Raspbian that wasn't expecting it might be unhappy e.g. applying updates. In practice, I've got an RPi-3 here driving a 3D printer etc., it boots Buster from "spinning rust" and apart from font metrics issues that I think I've mentioned in another thread I've never had any problems. MarkMLl ---------------------------------------------------------------------- TRon · 2023-12-22 13:16 Quote from: MarkMLl on December 22, 2023, 09:43:14 amPlus modern SD- Cards /might/ be more robust. But apart from that: /all/ devices which write to an SD-Card have the same problem unless they're using a filesystem designed- from the ground up- to have its own wear- levelling. Correct. QuoteSwap is particularly brutal, and this has been an issue since the Pi-1. I can confirm. QuoteI think it was the Pi-3 which introduced the ability to boot from USB-connected media, although it was disabled by default. But in any event it's never been difficult to boot from card but mount external media a the root device (i.e. /) although there's obviously a possibility that an version of Raspbian that wasn't expecting it might be unhappy e.g. applying updates. AFAIK every device listed here is capable of booting using USB host mode (at least it is advertised as such) and also AFAIK the pi3 allows to directly boot from a USB device (after some initial setup). QuoteIn practice, I've got an RPi-3 here driving a 3D printer etc., it boots Buster from "spinning rust" and apart from font metrics issues that I think I've mentioned in another thread I've never had any problems. I do not have any issue with my pi's either but it seems that either there are people that have a terrible choice in selecting a proper SD card manufacturer/brand or do not fully understand that when you log data every 2 milliseconds that you are not suppose to write that log to a file on a SD-card that every 2 milliseconds. That will kill your SD-card for sure within a couple of weeks, if not days. I have tested with running Lazarus on a pi3B+ but that is definitely too much to handle for that poor little thing as it requires swap for a rebuild and Lazarus/FPC does write a lot of data for/on each compilation. It is an eye opener when you dive deeper into that topic and also made me more aware so that I now use a ramdisk instead of ssd-storage for my builds and compiles (even though the wear leveling is in a much better position/state for SSD storage). ---------------------------------------------------------------------- MarkMLl · 2023-12-22 13:53 Quote from: TRon on December 22, 2023, 02:16:32 pmI do not have any issue with my pi's either but it seems that either there are people that have a terrible choice in selecting a proper SD card manufacturer/brand https://www.bunniestudios.com/blog/?page_id=1022 is a good read under the circumstances. In practice, my recollection is that there were issues on the Pi-3, even after you'd told it to boot from an external medium, if you wanted to implement your root device using software RAID or with an encryption layer. I forget the detail, but allowing that the initial boot code was in 32K of internal ROM I don't think it was fixable. MarkMLl ---------------------------------------------------------------------- dbannon · 2023-12-23 04:54 Quote from: TRon on December 22, 2023, 02:16:32 pm I do not have any issue with my pi's either but it seems that either there are people that have a terrible choice in selecting a proper SD card manufacturer/brand or do not fully understand that when you log data every 2 milliseconds that you are not suppose to write that log to a file on a SD-card that every 2 milliseconds. That will kill your SD-card for sure within a couple of weeks, if not days. Back when I was using a Pi 1, I was also having quite a lot of (external) power problems. I was surprised to note that the Pi seemed to cache writes for close to 24 hours. So, if the power goes off today, no data was saved after some time yesterday. Unless you call sync.... So, I guess that was to protect the sdcard from just what we are talking about. But that is not going to help when we get a page fault ! So, using it for swap is the real danger. David ---------------------------------------------------------------------- Mark20 · 2023-12-24 18:51 I see some Rpi4 clones that have 32GB of eMMC flash onboard; do you know if this kind if flash solve the wear problem ? ---------------------------------------------------------------------- MarkMLl · 2023-12-24 19:49 A few seconds Googling turns up https://forums.raspberrypi.com/viewtopic.php?t=358782 However I have no personal experience of that, and I'd suggest that "clone" behaviour might or might not be the same as the "real thing". On reflection, I do have a very vague recollection of something which might have been an eMMC module on the back of an RPi clone badged (I think) Acer, which failed for unspecified reasons within weeks. I'd also remind you of https://hackaday.com/2021/02/11/tesla-recalls- cars-with-emmc-failures-calls-part-a-wear-item/ as a cautionary tale. MarkMLl ---------------------------------------------------------------------- TRon · 2023-12-24 20:37 EMMC suffers from the same wear leveling issues, especially because the capacity of the devices for these kind of SBC is very small (in comparison). I do not have fond memories of emmc and avoid it like the plague (but perhaps things have improved over time). I would be wary if it's soldered down on the board as that makes it nearly impossible to replace unless you have the equipment and are familiar with replacing such parts. ---------------------------------------------------------------------- dbannon · 2023-12-29 06:06 I find that depressing. No wear leveling for eMMC "laptops" ?  These thing dominate the small laptop market. I still have little 10 year old Netbook, I have replaced the screen (I stood on it), the hard disk (with a standard SSD) and memory. Its no power house but its my 32bit test platform ! I'd buy a new version of it if available but would not touch a eMMC driven box. I guess I'd think the same way with a RasPi clone but it is a different price point ..... EDIT: No, I think I might be a bit too critical. NAND flash storage is not a simple read/write data medium. For reliable use, several algorithms should be implemented: NAND block management, garbage collection, error control and wear levelling. Modern NAND flash is managed, with algorithms on the storage device, not implemented in the host processor. https://www.kingston.com/en/blog/embedded-and-industrial/emmc- lifecycle Davo ---------------------------------------------------------------------- Mark20 · 2023-12-29 08:24 Seems a problem with no way out...  at least for solid long last solution... May be we have to change  the way we program, bringing all possible in Ram, and commit to flash only when really necessary... ---------------------------------------------------------------------- MarkMLl · 2023-12-29 08:28 Quote from: Mark20 on December 29, 2023, 09:24:53 amSeems a problem with no way out...  at least for solid long last solution... May be we have to change  the way we program, bringing all possible in Ram, and commit to flash only when really necessary... Which doesn't really help if the RAM isn't protected by ECC bits. However I repeat my earlier position: eMMC is supposedly protected by at least some internal "intelligence", but that didn't help the Tesla owners. MarkMLl ---------------------------------------------------------------------- dbannon · 2023-12-31 00:03 Quote from: Mark20 on December 29, 2023, 09:24:53 amSeems a problem with no way out...  at least for solid long last solution... May be we have to change  the way we program, bringing all possible in Ram, and commit to flash only when really necessary... Mark20, if you are doing a small project that, once done will just run in the background, you will probably be alright.  Just keep in mind its better to write dribbling data to somewhere other that the sdcard, even a USB key. If you are using the Pi as an on going development platform, then you are taking more significant risks. Its up to you to work out the impact, on you, of those risks. Note - Almost all risks are mitigated if you mount some external disk and use that for both swap and compiling.  You will probably find compiling a great deal quicker using a (reasonable) external disk for the i/o intensive stuff anyway. If your Pi has 4g ram, it probably will not go into swap anyway. If you keep good backups, SDcards are cheap, maybe you are willing to risk it ? Davo ---------------------------------------------------------------------- Mark20 · 2023-12-31 11:21 Yes Davo, for me it worth study a proper configuration and best practice to minimize risks. I see room to work on this. If I have to deal with projects where an ATmega32 is not enough, I want to switch on PiZero. Also because  other "micro" as ESP32 are complex too; it's not easy to get a real and full control of such devices. At least with Rpi and Lazarus I have a full software ecosystem which I can rely on. ---------------------------------------------------------------------- MarkMLl · 2023-12-31 12:43 Quote from: dbannon on December 31, 2023, 01:03:07 amMark20, if you are doing a small project that, once done will just run in the background, you will probably be alright.  Just keep in mind its better to write dribbling data to somewhere other that the sdcard, even a USB key. A key/thumb drive of a reputable brand sourced from a reputable retailer should have wear-levelling, except in the case where it actually is an adapter for an SD-Card since in that case it will be attempting to preserve medium compatibility. I've seen somebody do a transistor-level repair on a failed SCSI disc, but if the wear levelling on an SSD etc. fails you're screwed since the files will be fragmented at block level. I don't know to what extent an FS intentionally designed for Flash storage would help, or something that explicitly versioned files; mirrored devices are obviously an option but as I've already said some RPis don't support them (at least for /). MarkMLl ---------------------------------------------------------------------- dbannon · 2024-01-01 00:18 Quote from: MarkMLl on December 31, 2023, 01:43:53 pmI've seen somebody do a transistor-level repair on a failed SCSI (SSD ?) disc, Wow, that would imply a deeper knowledge of the electronics than the manufacturer would be happy about ? QuoteI don't know to what extent an FS intentionally designed for Flash storage would help, I have seen vague references to specifically Flash friendly FS, but nothing specific. It would seem to me that doing that wear leveling at an OS level could be quite messy if the storage media was also trying to level things out. Maybe its specific to embedded systems ? Quote from: Mark20 on December 31, 2023, 12:21:53 pm... At least with Rpi and Lazarus I have a full software ecosystem which I can rely on. True but generally, if doing any sort of development, I find find cross compiling, where FPC/Lazarus runs on my laptop and writes a binary in a directory that is sshfs'ed to the Pi just a bit better. I don't have the newest Pi and compute power needed for editing and compiling is only just enough. And if you move to a Pi Zero, more so.  And I am quite sure the disk and general system of your desktop is a lot more robust that the Pi.  On the other hand, maybe I just do all that because I get to sit in the comfy chair ! Davo ---------------------------------------------------------------------- MarkMLl · 2024-01-01 07:53 Something off with the quoting in your last msg. Quote from: dbannon on January 01, 2024, 01:18:35 amWow, that would imply a deeper knowledge of the electronics than the manufacturer would be happy about ? It was an SMD in the motor control, so while "deep magic" in the 90s by today's standards would be relatively simple. However the story might interest. The drive was from a development system running OS/2, which was such a pig that one really didn't want to reinstall any more times than was necessary. The chap who I found to do the repair had, it turned out, run a training course I'd attended on disc drive maintenance and repair when we both worked for Burroughs. That meant working through multiple PCBs and replacing chips: the company didn't budget for spare boards to be kept on site. From that you might get the impression that the company was going through a bad phase. Even without things like Operation Ill Wind and Alweiss's memoir you'd be right. Apart from anything else the senior sales staff used to get together and play poker on payday, which hardly helped set a healthy tone. A bunch of those same senior guys left and founded Rodime, I don't know whether they got any IP from Memorex which was bought by Burroughs at about the same time. Rodime eventually went downhill, so they bought the gambling business from Littlewoods and returned to their roots. Great shame: historically the company had had some very good stuff and it was of course significant in popularising ALGOL-style languages. MarkMLl ---------------------------------------------------------------------- Mark20 · 2024-01-01 11:04 @Davo of course I agree on cross compilation on desktop and ssh the bin to the target Pi ;-) @MarkMLl interesting; I worked for Memorex and then Burroughs in the eigthies... ---------------------------------------------------------------------- MarkMLl · 2024-01-01 11:20 Funnily enough, I bumped into an ex-B FE at a party just before Xmas. Like most people he assumed that nobody had heard of the company, and was apologetic for having been involved... MarkMLl