Teensy with Freepascal

Status
Not open for further replies.
Hi

I am probably jumping the gun here, but I am eager to discover the possibilities.

I have just ordered a Teensy 3.1 board that wil hopefully arrive tomorrow.

I read several of the PRJC homepage entries that seems to suggest the supplied code and things are written and compiled in C.

I would like to know if it will be possible for me to use my IDE and programming language of choice which is Lazarus IDE and Freepascal to code my own solutions, compile them, and run them on the teensy board? AFAIK, Freepascal does have an ARM compiler but I'm totally to new to this particular area so don't really know where to even start. So do I have to use to C and the C compiler? If I do, then I'll have to start learning but I hoped I could use what I have learnt these past few years with FPC and Lazarus.

If that is possible, can anyone give me some direction as to the step required? Thanks
 
Arduino and Teensy use C++ and users may code in C or C++.
The libraries of I/O and the standard C functions are for a C/C++ environment.

There are several IDEs, including Arduino's, Microsoft Visual Studio 2013 (free) combined with free Visual Micro; VisualGDB, and for an IDE for Macs, and there's Eclipse. All these use GCC.

Freepascal - I know nothing of it. But without a big effort on your part, it's C/C++ and the Arduino simplifying veneer over C++.
 
Yup. Definitely jumping the gun.

Setting up a new tool chain run for a microprocessor is not an exercise for the faint of heart. You really need to be an expert at so many things: multiple programming languages, compiler options, make files, scripting, low-level debugging and waiting for errors that take hours to appear. If you scan through the forums here and find posts that are way over your head--that's exactly the sort of expertise needed to make something like Free Pascal run on a Teensy.

Since you're just getting started, I would encourage you to start playing with your Teensy 3.1 by following the documentation and running simple examples the way other beginners do. Yes, it's definitely annoying to need to learn a new programming language, but I suggest you go ahead and bite the bullet. The Pascal programming language was a dead end back in the 80s. C++ is very popular and still improving.

Not the answer you wanted, but there's a lot of fun and learning to be had here.
 
Since pascal is part of gcc, you could theoretically generate your own pascal compiler (--enable-languages=pascal). But this will not be trivial.
The big problem is with the arduino libraries, or more accurately with the header files, which contain c++ constructs like templates etc. There is tooling to access pure c header files, but hardly any (arduino) header file is pure C.

So you could theoretically program in pascal, but without libraries. And it does not really make sense to program without libraries.
Do not be too affraid of C. Pascal and C are not too dissimilar. Just replace every begin with '{' and every end with '}' end you solved 90% of the conversion. Futhermore, all of the examples willl be in C(++). With pascal you are all on your own.

Finally, just to comment on this
The Pascal programming language was a dead end back in the 80s
Pascal had a bit of a revival in around 2000 with Borland Delphi.
 
Ummm, Free Pascal is not part of the official GCC release. I'm not sure what it uses for a backend, though there appears to be a project to switch and use the LLVM backend. According to goggle search, support for embedded ARM devices was only recently added to the trunk. There is Gnu Pascal, which does use the GCC backend.
 
Sorry, I was not implying Free Pascal, just any pascal. As you mention gnu pascal is using the gcc backend, this seems to be the most viable solution. But building a crosscompiler with probably the pascal equivalent of libc, is definitely not a beginners project. I maintained a crosscompiler with uclibc for the sh4 for a while, this is pretty standard, but al sorts of (solvable) little problems kept popping up.

I would suggest to learn C, as said if you know pascal, C is not a big hurdle. If you really insist, p2c (pascal to c-converter) is available on linux, don't know if there are winows equivalents. Only used it a couple of time in the early 90s when still at school, it still seems to be around. I remember it resulted in compilable c-code, not necessarily readible code.
 
I went from UCSD Pascal to C more decades ago than I care to admit.
There was a day, when struggling with C and pointers and pointers to functions, when I realized that C is not a high level language like Pascal. C is a portable (machine independent) assembly language. Having spent years doing assembly for minicomputers, I warped my brain from Pascal mode to thinking how K&R's C was just that - a step up from assembly language. Then it all got easy. I just mentally set aside my Pascal thinking. Use a pointer wrong and you crash. Just like assembly.

C++ is way too arcane for someone coming directly from Pascal. Start with C on a PC, and avoid a complex IDE for now as you'll divert too much thinking to the IDE and not learning C.
 
Last edited:
I'm pretty sure FreePascal's ARM support isn't going to be very useful. On their web page, it says:

ARM architecture
16 MB of RAM is required. Runs on any ARM Linux installation.

With a tremendous amount of work, it might be possible to get some rudimentary system running on Teensy. Maybe. Probably a big first question is whether the FreePascal system can compile to lightweight bare metal programs? If it's got big (relative to Teensy 3.1's memory) support libraries that it always links into final executables, that's probably be a first huge roadblock.

I agree completely with the comments above, that you can (in theory) link C and Pascal code. Maybe, if you have 2 toolchains that produce compatible object code formats and a linker that can combine them. Must of the Teensy 3.1 core library is pure C, at least for the basic USB functionality. Some of it is in .cpp files, but is basically just C code, so it could be brought over. But things like the Print class can't, so in a best case scenario, you'd be able to get your Pascal program to at least use the basic functionality of the hardware, like USB and hardware serial and analog & digital I/O.

But even just that could be a long, uphill journey.
 
In the status, FP listed embedded ARM as being in the trunk and not yet in a release. I would imagine you would use it in the same way you use the ARM GCC cross compilers we currently use. However, I suspect it is probably a WIP, and if you went down that path, you would have a lot of work to get it done.
 
Interesting development regarding Free Pascal embedded! Note that Lazarus is a whole framework designed for creating desktop PC apps. For FP embedded, you would still need an IDE for Free Pascal, or use command line.

My first job in embedded software many years ago was writing code for network controllers - most of it was written in Pascal, using an Intel Pascal compiler. About the only embedded Pascal I have seen recently is http://www.mikroe.com/mikropascal/arm/. There is an embedded Pascal for Z80/6502/AVR.

The practicality of using Pascal means having to write every bit of code, it is hard to mix with C. That means rewriting libraries, and no "copy and paste" code samples off the web. I think I would only recommend Pascal for those die-hard guys, it will be a lot easier getting started in embedded with C/C++. Hopefully if FP embedded becomes a practical proposition that might change.
 
You can if you dare, go use MikroElectronica ARM boards (they're not Freescale), and their sorta-compilers for Pascal, C, Basic. Not at all like a big desktop run time, but there you got yer Pascal. I suppose the Pascal can target their mini-M4.

I don't believe that real Pascal and embedded systems (small MCUs) are a fit.
 
Thanks for the info guys. That was all the clarification I needed. I didn't want to miss something obvious and go down the route of new language learning unnecessarily. But seemingly it will be a big hill to climb to implement FPC with Teensy. I think we can consider the thread as solved. Thanks.
 
I asked my friends at the Lazarus Freepascal project (http://forum.lazarus.freepascal.org/index.php?topic=28561.msg178918#msg178918). Laksen and Michael Ring have kindly created the compiler for the Teensy processor which is committed to SVN trunk (http://svn.freepascal.org/svn/fpc/trunk/):

The SVN trunk is at

To build the compiler:
make buildbase OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7em CROSSINSTALL=1

To install it:
make installbase OS_TARGET=embedded CPU_TARGET=arm SUBARCH=armv7em CROSSINSTALL=1 PREFIX=C:/wherever you want to install

To build a program:
fpc -Parm -Tembedded -Cparmv7em -Wpmk20dx256xxx7 program.pas
 
How easy is it to do all the common hardware things from Freepascal? How much low-level setup is required use digital and analog I/O? Can you use the serial ports? Timers? Capacitive touch sensing? Can you even access the registers without porting a massive header file? Is it possible to link with existing C or C++ libraries?
 
How easy is it to do all the common hardware things from Freepascal? How much low-level setup is required use digital and analog I/O? Can you use the serial ports? Timers? Capacitive touch sensing? Can you even access the registers without porting a massive header file? Is it possible to link with existing C or C++ libraries?

Noone has to my knowledge ported the Freescale firmware library(if there is any?) to pascal, but it should be straight forward if anyone is up for the daunting task.. :)

The rest is just like how you would do it in C. The entire header has been ported and all registers are accessible. If you care the file showing the interfaces and startup code can be found in the freepascal SVN sources at rtl/embedded/arm/mk20d7.pp
Lazarus(the editor) also has full integration so the registers and contents will show up in code completion, if set up right.

It might be possible to link with C code or flattened C++ code, but I don't know if it's commonly done. I've heard someone does that for with Silabs SDK ARMv6M code. The ABI isn't standardized but as long as it's just simple functions without floating point stuff it should work (FPC doesn't support FPv4 that well yet).
 
Status
Not open for further replies.
Back
Top