fastspin compiler for P2: Assembly, Spin, BASIC, and C in one compiler

Comments

One of the Spin objects that is used in almost all of my Spin projects is FullDuplexSerial (and its cousin FullDuplexserialPlus)... Of course that object has a dependency on its PASM code that includes several jmpret instructions. Building P2 projects with fastspin is chocking on those instructions...

Is there a quick solution or should I just remove my Spin code projects dependencies on FDS? Seems that I'll need to replace FDS with a P2 equivalent, eventually, right?

You'll certainly have to replace FDS (as you've noticed, it won't work on P2 anyway because of the PASM code not being compatible). I usually use some variant of SimpleSerial.spin, which is a dead simple half-duplex driver that'll work on P1, P2, and PC. I think it's in the fastspin Libs/ directory, but I've attached a copy here just in case.

Nice and simple as advertised but I don't see how rx will work on a PC.

You're right, rx was a late addition and I forgot to put in an #ifdef PC to make it use getchar(). So at the moment SimpleSerial won't work on PC. It'd be easy enough to fix (just follow the template for tx).

Actually it looks like rx is broken on P2 as well . I'll have to take a look at that.

I don't really have a need to run it on the PC but getting it working on the P2 would be nice.

I've fixed the missing rx for PC and P2 and updated my comment above. The rx code on P2 seems a bit fragile; it could be that it's triggering a P2 only optimizer bug (P1 seems OK) or else there's a subtle timing problem that I'm missing. I had to compile hello.spin for my P2 FPGA with -O0 to get it to work correctly.

I should warn everyone that the P2 code generation has received a lot less testing than P1 code generation. Now that we're really getting P2's out "in the wild" I suspect we're going to find lots of P2 bugs in fastspin. Please bear with me while we shake those out

I wrote a short Basic program to demonstrate doing things in different cogs. I list the BASIC program and the beginning of P2asm file (to be saved with extension ".spin2") modified as described in the above post. The P2asm file was too long to list here and the beginning is where the edits are needed.

' testcpubas.bas
' Use 3 cogs. Open a new cog and blink an LED (pin 1) at a given frequency,
' open a second cog and blink LED at pin 2 twice as fast,
' print a series of numbers from cog 0.
' rename p2asm file to testcpubas.spin2 and change/add lines with ****
' clock will be set to 180MHz
' set terminal baud to 250000
sub blink(pin, freq) ' this subroutine is used in both cogs 1 and 2
direction(pin) = output
do
output(pin) = not output(pin)
waitcnt(getcnt() + freq)
loop
end sub
dim stack(8)
dim stack1(8)
' blink in cog 1
var a = cpu(blink(1, 180_000_000), @stack(1))
' blink 2x as fast in cog 2
var b = cpu(blink(2, 90_000_000), @stack1(1))
' print a number every half second independent of what is happening in cogs 1 and 2
' I used PST as the terminal and set the baud to 250000
dim i
i=0
do
i=i+1
print i,
pausems 500
loop

OK, sorry I haven't been quicker to respond to the various issues people have found on P2 Eval boards but the holidays have been crazy busy for me. I have managed to try out fastspin and spin2gui on a P2 Eval board. As you've noticed, some fixes were required to get the generated code to work on real hardware. I've made those fixes now. It's entirely possible that P2 FPGA doesn't work any more -- I'll have to look at that later, but I suspect most FPGA users have Eval boards now, so hopefully that won't be a big blocker.

The biggest changes are (1) using an updated loadp2 from Dave Hein which supports the P2 Eval, and (2) not implicitly setting the clock mode at startup on P2. I recommend putting in an explicit "clkset(mode, freq)" in P2 programs (BASIC or Spin). In BASIC you should follow this by "_setbaud(2000000)" if you want to use the serial port. The clkset() takes as a mode argument the hubset mode for setting up the oscillator. It implicitly enables the oscillator with an XSEL of %11. That's something we can change later, but I wanted to get some code out quickly.

I hope this works for everyone -- as always, let me know of any issues you find.

I have tried spin 2 gui and get an error. See attached.
I have also tried fastspin but it launches a window and disappears. ?????
Help please.

Folder names with spaces in them seem to be a problem for spin2gui. Just rename them (in Windows right-click and select Rename) to something without a space, for example by replacing all spaces with underscores.

ersmith,
Thanks for the update! I'd like to use this for doing C/C++. I know it's "not ready for prime time" or whatever, with a lot missing, but you've mentioned having some simple stuff working.
I don't see any C samples in the samples folder, do you have some stuff that works for P2 Eval board available someplace?

I did see the c.md doc file which explains a bunch, but do you have any "builtin" things or does that sort of stuff all need to be done with inline asm? I'm thinking things like a clkset equivalent, or other basic stuff.

ersmith,
Thanks for the update! I'd like to use this for doing C/C++. I know it's "not ready for prime time" or whatever, with a lot missing, but you've mentioned having some simple stuff working.
I don't see any C samples in the samples folder, do you have some stuff that works for P2 Eval board available someplace?
[/code]
The C compiler is very much not ready for prime time, and has a ton of bugs (and missing features). If you want to try it out feel free, but you'll probably be disappointed in its current state.

Here's a simple C program to toggle an LED and print a message on the serial port. It works with my board on spin2gui v1.3.0.

You might try the sample programs in p2gcc to see if they work with fastspin. There are some simple programs like hello.c and prime.c, and more complicated ones like chess.c and bas.c.

I think hello.c works, but the others manage to turn up bugs in the fastspin C compiler -- prime.c, for example, has a large local array that fastspin tries to cram into COG RAM. The p2gcc demo programs are great tests though, and I hope to get them running soon.

I've tried spin2gui vers1.3.0 together with Eric's recommendations for the timing defines and additions to main(). It works. I've also used the CLKMODE for 180MHz and that works. 2000000 baud is the fastest I've been able to use with the builtin terminal, PST can use 3000000.

I wrote a program (below) to reverse the bits in an integer (for a MSBFIRST data transfer). It is listed below and it works to change the initial value of 26 (0b11010) to 11 (0b01011). Because of problems I had with the earlier version of spin2gui, I did the printout in an infinite loop so I would be able to start the PST.

Btw getting rid of the spaces in the directory names did not fix the problem.

The error you received is precisely about spaces in the directory named "NEW SPIN2GUI UPDATE 1.3.0"...

There may be other issues past that problem, but that directory name was causing the issue you displayed in the image you attached. Attach any new error images and maybe we'll be able to help you further.

Is Pnut still running? Or a terminal program? If any other program is using the P2 then loadp2 will not be able to find it.

If that's not the problem you could try unplugging and re-plugging the P2 eval board. If it's still not found, take a look at what PNut is reporting as the serial port for the P2. If, for example, it's COM9, try adding "-p COM9" to the loadp2 command line in spin2gui. To do this, go to Commands > Configure Commands..., and in the "Run Command" (the one that starts with "cmd.exe /c start ..." add -p COM9 at the very end of the line.