Use of spin2cpp

As a beginner please advise resolution of error:
C:\FlexGUI\kgsamples>spin2cpp --main --ccode --files blink_all_cogs.spin
blink_all_cogs.spin:33: error: coginit stack is not array
line 33:     coginit(cognum, doblink(cognum, delay), @stack[cognum*4])
Thanks,
Kevin
«13

Comments

  • I decided to try a simpler sample:
    C:\FlexGUI\kgsamples>spin2cpp --main --ccode --files blink1.spin2
    blink1.spin2:11: error: clkset is not a function
    line 11:   clkset(mode, freq)
    
  • ReinhardReinhard Posts: 338
    edited 2019-12-30 - 14:48:02
    I would like to help you,(better I try it) , can you post the full source code?

    edit: found it in the sample dictionary
  • ReinhardReinhard Posts: 338
    edited 2019-12-30 - 14:52:54
    I am not very sure, but I think spin2cpp is a kind of preprocessor.
    Use Instead fastspin, this works:
    fastspin -2 blink1.spin2
    Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
    Version 4.0.4 Compiled on: Dec 18 2019
    blink1.spin2
    blink1.p2asm
    Done.
    Program size is 1376 bytes
    
    
  • For a beginner, I'd suggest getting FlexGui, which can compile Spin2 code.

    The way I understand it, spin2cpp was made to turn Spin code into something that GCC can compile.
    But, GCC for prop2 is not really ready for prime time, I think...
  • @kg1: you generally seem to have the correct command line for spin2cpp. Unfortunately, spin2cpp has some limitations on the code it can convert from Spin to C, and you managed to hit two of them: there are restrictions on the coginit/cognew function (the stack parameter can only be a simple array) and the clkset() function can't be converted yet. That second one is a bug, I'll try to fix it.

    Also note that for converting Propeller 2 code you should give the --p2 command line option to spin2cpp.

    What are you trying to accomplish with spin2cpp? From the looks of it you want to convert some Spin code to C, presumably to run with one of the C compilers. Is this for Prop1 (using PropGCC) or for Prop2 (using a different C compiler)?
  • Thank you @Reinhard for your reply. True the readme.md file in the samples folder does provide the instruction to use fastspin.
    Also the readme.md file that comes with spin2cpp does lists the parameter -p2.
    @ersmith I am a beginner with regard to P2 but looking at my photo I can't claim to be a beginner beginner. I started by looking at Catalina (RossH) due to my interest in C programming and debugging. Here there is a suggestion that one could use spin2cpp to use spin examples in the Catalina system. I have only tried two samples.
    What is the current status of the C programming language (relative to P1) in the P2 environment: Is it say 80% on target?
    What is the current status of spin2 in the P2 environment? Is it 99% on target?
    I am searching for a beginners path. At this moment is the C language a no go area?
    Kevin.
  • kg1 wrote: »
    @ersmith I am a beginner with regard to P2 but looking at my photo I can't claim to be a beginner beginner. I started by looking at Catalina (RossH) due to my interest in C programming and debugging. Here there is a suggestion that one could use spin2cpp to use spin examples in the Catalina system. I have only tried two samples.
    What is the current status of the C programming language (relative to P1) in the P2 environment: Is it say 80% on target?
    What is the current status of spin2 in the P2 environment? Is it 99% on target?
    I am searching for a beginners path. At this moment is the C language a no go area?
    Kevin.

    Using spin2cpp with Catalina on P2 will work for some things, but it's not a very well supported combination (as you've found) and definitely not a path I'd recommend for beginners. spin2cpp was originally designed for use with PropGCC on P1.

    You can use C directly in flexgui (the .c files in the samples directory should all compile and run directly, just load them into the flexgui main window and hit "Compile and Run on P2"). Even some significant programs like the Lisp interpreter in samples/proplisp will work. Your estimate of 80% done for the C language is probably about right for the C support in flexgui. Most simple programs will compile and run just fine, but the C library is still incomplete.

    Chip is still designing the Spin2 language. flexgui supports Spin1 even for P2, and has a number of Spin2 features, so it's kind of a Spin1.5. If you're already familiar with Spin then the Spin in flexgui should work well for you.
  • I wish I could believe there is a future for the spin programming language. If I could guess the corporate goal it is aimed at the educator. If I could guess the goals of the educator it includes industry standards, tools that are always up to date, programming languages that are highly valued in the market place, students that remember the corporate that set them on the right path. Is this SPIN? I wonder what @ntosme2 thinks?
    Kevin.
  • whickerwhicker Posts: 517
    edited 2019-12-30 - 22:58:04
    kg1 wrote: »
    I wish I could believe there is a future for the spin programming language. If I could guess the corporate goal it is aimed at the educator. If I could guess the goals of the educator it includes industry standards, tools that are always up to date, programming languages that are highly valued in the market place, students that remember the corporate that set them on the right path. Is this SPIN? I wonder what @ntosme2 thinks?
    Kevin.

    Couple of thoughts,

    Primary education isn't concerned with corporate influence (and its accusations of corruption from angry parents). Teaching the fundamentals is more critical. Variables, branching, loops, input-processing-output, etc.

    C isn't a good teaching tool because is it really fair like one person out of a whole class is going to grasp those semicolons and brackets? Where everyone else's code isn't even going to compile at all and they can feel the crushing weight of failure? That's what I've heard from teachers. Too much to do, not enough time. It must be taught in easily digestible blocks.

    There's also the problem that whatever tools (IDE, menus, command line) are taught in school, they'll be entirely obsolete in less than a decade. Yes being a master at a certain language gets you hired. But, personally speaking, having been in industry it's a more valuable skill to be able to jump quickly into an obscure tool or language and fix the immediate need.

    Seeing a diversity of languages, and never stop learning, that's the ticket to success.
  • kg1 wrote: »
    I wish I could believe there is a future for the spin programming language. If I could guess the corporate goal it is aimed at the educator. If I could guess the goals of the educator it includes industry standards, tools that are always up to date, programming languages that are highly valued in the market place, students that remember the corporate that set them on the right path. Is this SPIN? I wonder what @ntosme2 thinks?
    Kevin.

    Spin may not be much of that, but it's a very simple way to write code that doesn't obfuscate and undermine the inescapable underlying realities of computing.
  • My daughter is a teacher in the UK, and she also teaches computing. Her 5yo twins are in her class for computing.

    They are learning scratch (similar to blocklyprop?). One of the boys made a spaceship, scaled a person and put them inside the spaceship. Added ground, and a flame, and made the rocket launch into space.

    I was amazed that a 5yo could do this in just a few lessons.
  • Thank you for your replies. I will look more into spin.
    Kevin.
  • Cluso99 wrote: »
    My daughter is a teacher in the UK, and she also teaches computing. Her 5yo twins are in her class for computing.

    They are learning scratch (similar to blocklyprop?). One of the boys made a spaceship, scaled a person and put them inside the spaceship. Added ground, and a flame, and made the rocket launch into space.

    I was amazed that a 5yo could do this in just a few lessons.

    Very cool...I was wondering where to start with my grandkids :cool:
  • Since I've been mentioned...

    From an education perspective I think any language that enables kids to quickly learn how to program is a fine choice. I was taught Java in high school, and while it's one of my very least favorite languages, it did the trick and I moved on to better options.

    For general industrial use, I believe C/C++ is the most likely to generate customers and widespread adoption. Another interesting option would be a code generator for Matlab/Simulink, though C/C++ would be my personal choice for the generated code.

    A combination for P2 that I would love to explore is microPython with easy C++/assembly/Spin loadable modules for the real-time bits. In my day job I code 80% Python with 20% wrapped C++ for desktop code and having that combo at the embedded level would be terrifically fun.
  • ntosme2 wrote: »
    A combination for P2 that I would love to explore is microPython with easy C++/assembly/Spin loadable modules for the real-time bits. In my day job I code 80% Python with 20% wrapped C++ for desktop code and having that combo at the embedded level would be terrifically fun.

    Yes, I would agree. I think that it would be compelling to interact with and should become even easier if we get a full native compiler setup instead of using either Eric's RISC-V translation or the somewhat library limited P2GCC approaches. With additional work something could probably start to become possible using these existing compilers though the interfacing hasn't been fully mapped out quite yet. I'm still hopeful that the official Spin2 code could be launched/called from another language/environment such as C and MicroPython etc however how easily that will be may depend on how Chip does it.
  • ntosme2 wrote: »
    A combination for P2 that I would love to explore is microPython with easy C++/assembly/Spin loadable modules for the real-time bits. In my day job I code 80% Python with 20% wrapped C++ for desktop code and having that combo at the embedded level would be terrifically fun.
    Does the standard MicroPython allow for easily loadable modules written in C/C++? If so, it shouldn't be hard to get that working with Eric's version of MicroPython.

  • I frequently use pybind11 to wrap C code...the MicroPython wrapping seems a bit more involved, but it looks like it does!
  • David Betz wrote: »
    ntosme2 wrote: »
    A combination for P2 that I would love to explore is microPython with easy C++/assembly/Spin loadable modules for the real-time bits. In my day job I code 80% Python with 20% wrapped C++ for desktop code and having that combo at the embedded level would be terrifically fun.
    Does the standard MicroPython allow for easily loadable modules written in C/C++? If so, it shouldn't be hard to get that working with Eric's version of MicroPython.

    Standard MicroPython only allows loadable modules under Unix. The embedded versions do not have any facility for loading C/C++ code at run time -- I think the design philosophy was that if you wanted to add C/C++ code you'd compile a custom version of the interpreter for those environments.

    That said it probably would be feasible to add the ability to load position independent code (e.g. from ELF files) at run time to ports other than Unix. It would require implementing something like the dlopen/dlsym dynamic library interface for a bare metal machine.
  • Getting back to the original post, the 4.0.6 version of spin2cpp has the clkset issue fixed so it's possible to convert blink1.spin2. Unfortunately there is one minor problem with using it with Catalina (on Catalina the function for fetching the current cycle counter should be called _cnt rather than _getcnt) but that's easily fixed with a define. For example:
    spin2cpp --ccode --main --p2 blink1.spin2
    catalina -lci -p2 -C P2_EVAL -C NATIVE -D_getcnt=_cnt blink1.c
    

    The _cnt / _getcnt problem is fixed in github.
  • @ersmith Thank you Eric. Your assistance is much appreciated.
    Thank you everyone for a very worthwhile discussion.
    Kevin.
  • The _cnt / _getcnt problem is fixed in github.
    @ersmith For Windows I guess I need to wait for the next release?
    Regards,
    Kevin.
  • kg1 wrote: »
    The _cnt / _getcnt problem is fixed in github.
    @ersmith For Windows I guess I need to wait for the next release?
    Regards,
    Kevin.

    You can build it yourself under Windows with mingw32 or a similar compiler set. Or, just keep using -D_getcnt=_cnt on the compiler command line until the next release.
  • @ersmith re
    Or, just keep using -D_getcnt=_cnt on the compiler command line
    Can I force output from:
    C:\FlexGUI\kgsamples>spin2cpp --ccode --main --p2 blink1.spin2
    blink1.spin2:11: error: clkset is not a function
    
    Thanks
    Kevin.
  • kg1 wrote: »
    @ersmith re
    Or, just keep using -D_getcnt=_cnt on the compiler command line
    Can I force output from:
    C:\FlexGUI\kgsamples>spin2cpp --ccode --main --p2 blink1.spin2
    blink1.spin2:11: error: clkset is not a function
    
    Thanks
    Kevin.

    Which version of spin2cpp are you using? The latest released one is 4.0.6 and that should have the "clkset is not a function" problem fixed. The binary releases are at https://github.com/totalspectrum/spin2cpp/releases.
  • @ersmith Yes you are correct. I must have more than one copy of spin2cpp installed. I placed a copy of spin2cpp in the same folder as the spin code and it converts ok.
    Thank you
    Kevin.
  • Complete:
    https://github.com/totalspectrum/flexgui/releases/tag/v4.0.6
    C:
    cd \FlexGUI\kgsamples
    
    C:\FlexGUI\kgsamples>spin2cpp --ccode --main --files --p2 blink1.spin2
    blink1.c
    
    Copy blink1.c and blink1.h to C:\Program Files (x86)\Catalina\demos
    
    C:\Program Files (x86)\Catalina>use_catalina
    
       ===================
       SETTING UP CATALINA
       ===================
    
    CATALINA_DEFINE  = [default]
    CATALINA_INCLUDE = [default]
    CATALINA_LIBRARY = [default]
    CATALINA_TARGET  = [default]
    CATALINA_LCCOPT  = [default]
    CATALINA_TEMPDIR = [default]
    LCCDIR           = "C:\Program Files (x86)\Catalina"
    
    C:\Program Files (x86)\Catalina>cd demos
    
    C:\Program Files (x86)\Catalina\demos>catalina.exe -lci -p2 -C P2_EVAL -C NATIVE -D_getcnt=_cnt blink1.c
    Catalina Compiler 4.0
    
    code = 276 bytes
    cnst = 0 bytes
    init = 4 bytes
    data = 4 bytes
    file = 8736 bytes
    
    C:\Program Files (x86)\Catalina\demos>payload blink1.bin
    Using Propeller (version 2) on port COM4 for download
    
    
  • Does Catalina generate better code than just letting FastSpin compile to native code itself?
  • David Betz wrote: »
    Does Catalina generate better code than just letting FastSpin compile to native code itself?

    Generally not, fastspin's optimizer is better than Catalina's and is tuned for the P2. On the other hand Catalina is better documented and has a debugger, so there are times when it might be nice to use spin2cpp to convert Spin programs to Catalina compatible C.

    Incidentally, spin2cpp can also convert (some) BASIC programs to C as well, although that support is still pretty incomplete (the BASIC library calls tend not to be converted correctly, so it only really works on low level BASIC programs like drivers).
  • Using FlexGui:
    "C:/FlexGUI/bin/fastspin" -2 -l -O1 -I "C:/FlexGUI/include"  "C:/FlexGUI/kgsamples/blink1.h"
    Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
    Version 4.0.6 Compiled on: Dec 30 2019
    blink1.h
    error: could not find function main
    error: could not find function main
    child process exited abnormally
    Finished at Thu Jan  2 21:37:09 2020
    
  • kg1 wrote: »
    Using FlexGui:
    "C:/FlexGUI/bin/fastspin" -2 -l -O1 -I "C:/FlexGUI/include"  "C:/FlexGUI/kgsamples/blink1.h"
    Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
    Version 4.0.6 Compiled on: Dec 30 2019
    blink1.h
    error: could not find function main
    error: could not find function main
    child process exited abnormally
    Finished at Thu Jan  2 21:37:09 2020
    

    You're trying to compile "blink1.h" and not "blink1.c". Make sure "blink1.c" is the tab you have selected when you press the compile button.
Sign In or Register to comment.