Shop OBEX P1 Docs P2 Docs Learn Events
C with a SPIN object - example - PropWare_Spin2Dat — Parallax Forums

C with a SPIN object - example - PropWare_Spin2Dat

Hi David Zemon, I hope you can help me.

I was thinking how is the best way to use a SPIN object from a C program. After see some examples of PropWare I understand that I need some help to understand!!

I think that "PropWare_Spin2Dat" is a good example for some questions.

1 - I can use any SPIN object in C?
2 - In the example PropWare_Spin2Dat how you call the "pst.spin"? But if is
#include <fdserial.h>
why you include "pst.spin" too? This is probably a no sense question, but I need to ask because I don´t understand how this work.
3 - You have more examples with other SPIN objects?

Thanks

Comments

  • 1 - This question I'm going to defer to @ersmith, the author of spin2cpp. I think the answer is "almost any Spin object", but that's the most I dare say since I've never used it myself.

    2 - You won't want to use Spin2Dat most likely. This option only extracts the DAT section of the Spin file and converts it to a binary blob (usually that means PASM instructions). This allows you to extract the driver from a Spin file like pst.spin and then use a C or C++ interface to interact with it. But I believe, from your other posts, that's not what you really want. You want to let Spin2Cpp extract both the driver portion of your Spin file and the high-level wrapper code.

    3 - I'm afraid I don't have any more examples. I might have time tonight to put something more together for you. Sorry that it is so sparse right now, I know the one example for Spin2Cpp isn't enough.
  • ASAS Posts: 149
    Thanks a lot for your help.

    You right I looking for something like: "let Spin2Cpp extract both the driver portion of your Spin file and the high-level wrapper code". :)

    There are any example to use Spin2Cpp to a SPIN object and after call it from C?
  • ASAS Posts: 149
    AS wrote: »

    There are any example to use Spin2Cpp to a SPIN object and after call it from C?

    I think I can understand it from the code generated by Spin2Cpp. I will see it better.

  • AS wrote: »
    There are any example to use Spin2Cpp to a SPIN object and after call it from C?

    Ha, this is hard for me. I have to find some Spin first. I haven't touched Spin in years :P
  • I think the generated code is usually pretty straightforward, especially in C++. The only thing that may look a bit mystifying in C code is that some functions get an extra parameter ("self"). This is a pointer to the object they are going to work on. This allows functions to work properly when there are multiple copies of objects. If the function doesn't make any references to its object's variables (things in the VAR section) then it won't need that parameter.

    There's a GUI for spin2cpp now. See forums.parallax.com/discussion/163635/interactive-spin-to-pasm-or-c-converter.
  • ASAS Posts: 149
    @eresmith thanks for the sugestion, I think your work have interest for me.
    I´m not sure if my question is to @ersmith or @DavidZemon, but I think it´s for @DavidZemon!

    I used Spin2Cpp as a compiler, everything ok!
    (https://sites.google.com/site/propellergcc/documentation/tutorials/spin2cpp)
    $ spin2cpp --elf -Os -o test.elf test.spin
    $ propeller-load -r -t test.elf
    
    The spin file was compiled onto the Propeller.

    But I think It´s interesting use the object file from a code made in C++.
    Before I try to change the code maded with spin2cpp, I tried use PropWare to concatenate the files. I´m not sure if I can do this :P (I attached the files)
    pi@raspberrypi:~/spin2cpp/Demo/experiencia/bin $ rm -rf *
    pi@raspberrypi:~/spin2cpp/Demo/experiencia/bin $ cmake ..
    -- Found PropWare: /home/pi/PropWare (found version "2.0.0")
    -- The C compiler identification is GNU 4.6.1
    -- The CXX compiler identification is GNU 4.6.1
    -- LOADED: Generic-gcc-Propeller.cmake
    -- Check for working C compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc
    -- Check for working C compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc
    -- Check for working CXX compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - failed
    -- Detecting CXX compile features
    -- Detecting CXX compile features - failed
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/pi/spin2cpp/Demo/experiencia/bin
    pi@raspberrypi:~/spin2cpp/Demo/experiencia/bin $ make
    Scanning dependencies of target spin2cpp
    [ 25%] Building CXX object CMakeFiles/spin2cpp.dir/FullDuplexSerial.cpp.obj
    /home/pi/spin2cpp/Demo/experiencia/FullDuplexSerial.cpp: In member function 'int32_t FullDuplexSerial::start(int32_t, int32_t, int32_t, int32_t)':
    /home/pi/spin2cpp/Demo/experiencia/FullDuplexSerial.cpp:77:148: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
    [ 50%] Linking CXX static library libspin2cpp.a
    [ 50%] Built target spin2cpp
    Scanning dependencies of target Hello
    [ 75%] Building CXX object Examples/CMakeFiles/Hello.dir/Hello/Hello.cpp.obj
    /home/pi/spin2cpp/Demo/experiencia/Examples/Hello/Hello.cpp: In member function 'void Hello::demo()':
    /home/pi/spin2cpp/Demo/experiencia/Examples/Hello/Hello.cpp:20:11: warning: unused variable 'i' [-Wunused-variable]
    [100%] Linking C executable Hello.elf
    /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/../lib/gcc/propeller-elf/4.6.1/short-doubles/_crtbegin.o: In function `argc_cnt':
    (.init+0x3c): undefined reference to `_main'
    collect2: ld returned 1 exit status
    Examples/CMakeFiles/Hello.dir/build.make:98: recipe for target 'Examples/Hello.elf' failed
    make[2]: *** [Examples/Hello.elf] Error 1
    CMakeFiles/Makefile2:159: recipe for target 'Examples/CMakeFiles/Hello.dir/all' failed
    make[1]: *** [Examples/CMakeFiles/Hello.dir/all] Error 2
    Makefile:94: recipe for target 'all' failed
    make: *** [all] Error 2
    

    I will wait for PropWare 2.1, I think @DavidZemon have a solution to this :)

    Any sugestion is very welcome.

    Thanks!
  • DavidZemonDavidZemon Posts: 2,973
    edited 2016-02-28 05:00
    Hi @AS,

    The spin2cpp() function in PropWare invokes spin2cpp for you, so you don't have to convert the files before creating your PropWare project. I have attached an example that has a simple Spin file to generate a random number and then a C++ file which will invoke the converted code. Note that, when you create the bin directory and compile the code, you never actually see the C++ source code in your project - just the spin file (RealRandom.spin) and your own C++ file (main.cpp).

    I'll walk you through the CMakeLists.txt file:

    Whole file
    cmake_minimum_required(VERSION 3.3)
    find_package(PropWare 2.1 REQUIRED)
    
    project(Scratch)
    
    spin2cpp(RealRandom.spin RealRandomCpp)
    
    create_simple_executable(Scratch main.cpp ${RealRandomCpp})
    

    And line by line...

    Gotta have your header...
    cmake_minimum_required(VERSION 3.3)
    find_package(PropWare 2.1 REQUIRED)
    

    And some random project name that doesn't matter
    project(Scratch)
    

    Here's the good stuff. We're going to call a special PropWare function to invoke the spin2cpp executable (aptly named: "spin2cpp()"). The first parameter is the name of our Spin file. The second parameter is actually an output, because CMake does not support return values (I know... it's dumb... one of my few big complaints about CMake). So I repeat, the second parameter is an output value from the spin2cpp() function. By passing "RealRandomCpp" in as the second parameter, we're telling the spin2cpp() function that the names of the generated C++ files should be stored in a variable named "RealRandomCpp". We need to know the names of those C++ files because they need to be added into our executable.
    spin2cpp(RealRandom.spin RealRandomCpp)
    

    And finally, we create the executable. Notice there are three arguments to the function this time:
    1) Executable name
    2) Our C++ file
    3) Our convert Spin files
    If you have any more source files, you just go ahead and add them on as extra arguments. Just keep on adding all of your source files and they'll all get compiled into one executable. This function, "create_simple_executable()", takes an unlimited number of arguments and every argument after the first is interpreted as a source file.
    create_simple_executable(Scratch main.cpp ${RealRandomCpp})
    

    So there you have it! Nice and easy to use spin2cpp with PropWare.

    Now, while writing this post I found a small bug in the spin2cpp() function. I've fixed it already and the fix is available from download on my build server, but just be aware that you'll need to download a new version of PropWare again to pick up the fix.
  • ASAS Posts: 149
    In my opinion this is a very good solution, very useful.

    Thanks a lot to both, @DavidZemon and @ersmith!

    DavidZemon,

    At this moment I really want to install (update) the PropWare 2.1.
    As you know I have PropWare 2.0 working great in the Raspberry. I already download from here your last work in PropWare, but I´m not sure, how to do (best solution), to update PropWare.

    I need some help to update PropWare, but if you think this is not the best timing to update to 2.1 version, I prefer wait until you tell me.

    Thank you!

  • ASAS Posts: 149
    I was looking better to the files you send me "Scratch.zip".

    I can´t belive!! I can use a SPIN object so easy!! this is amazing! :thumb:
  • ASAS Posts: 149
    It´s working!! :D

    I just copy the files from:
    - "PropWare-2.1.0.22-Generic-propware" to the PropWare directory.
    - "PropWare-2.1.0.22-Generic-standalone-cmake" to the cmake´s directories with the same name, "Modules" and "Platform".

    And the Scratch is working!!! :thumb:

    When I write
    $ cmake ..
    
    I think is everything ok!

    But when I write
    $ make
    
    I get a few warnings! It´s normal?
    pi@raspberrypi:~/PropellerProjects/PropWare/Examples/Scratch/bin $ cmake ..
    -- Found PropWare: /home/pi/PropWare (found suitable version "2.1.0.22", minimum required is "2.1")
    -- The C compiler identification is GNU 4.6.1
    -- The CXX compiler identification is GNU 4.6.1
    -- Check for working C compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc
    -- Check for working C compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc -- works
    -- Detecting C compiler ABI info
    m-- Detecting C compiler ABI info - done
    a-- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc
    k-- Check for working CXX compiler: /home/pi/SimpleIDE/SimpleIDE-0-9-45/parallax/bin/propeller-elf-gcc -- works
    -- Detecting CXX compiler ABI info
    e-- Detecting CXX compiler ABI info - failed
    -- Detecting CXX compile features
    
    -- Detecting CXX compile features - failed
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/pi/PropellerProjects/PropWare/Examples/Scratch/bin
    pi@raspberrypi:~/PropellerProjects/PropWare/Examples/Scratch/bin $ make
    Scanning dependencies of target Scratch
    [ 25%] Building CXX object CMakeFiles/Scratch.dir/main.cpp.obj
    [ 50%] Building CXX object CMakeFiles/Scratch.dir/RealRandom.cpp.obj
    /home/pi/PropellerProjects/PropWare/Examples/Scratch/bin/RealRandom.cpp: In member function 'int32_t RealRandom::start()':
    /home/pi/PropellerProjects/PropWare/Examples/Scratch/bin/RealRandom.cpp:75:153: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
    [ 75%] Linking C executable Scratch.elf
    Code size  = 5236
    Total size = 5456
    [100%] Built target Scratch
    

    Really thanks!
  • Glad you figured it out!

    Yes, that warning is normal, don't worry about it :). @ersmith might see it and feel like changing something about the spin2cpp code generator so that the warning goes away, but if I recall correctly, that warning is okay in this case, and it will function correctly still.
Sign In or Register to comment.