Shop OBEX P1 Docs P2 Docs Learn Events
Objects in SPIN and ASM - Can use in C program? — Parallax Forums

Objects in SPIN and ASM - Can use in C program?

John KauffmanJohn Kauffman Posts: 653
edited 2014-06-06 20:58 in Propeller 1
C is standard for teaching. But OBEX is almost all non-C. I'm probably using wrong search terms, but can't find answers to below.

Is there a way to include a SPIN object in a C program?
Is there a way to include an ASM object in a C program?
Source of examples?

I'll look at David Betz spin wrapper this afternoon:
http://forums.parallax.com/showthread.php/153919-SpinWrap-a-tool-for-allowing-C-or-C-to-use-Spin-Objects?p=1243144&viewfull=1#post1243144

Thanks.
«1

Comments

  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-05-30 07:17
    105 views. 0 responses. Is that a record?
    There must be other people with the same question: How to use objects written in Spin or ASM when using C.
    Thanks.
  • jazzedjazzed Posts: 11,803
    edited 2014-05-30 07:27
    I've been tied up.

    Several Propeller-C libraries use the PASM from Spin files.

    libfdserial uses pst.spin

    libvgatext uses vga.spin

    Generally however, C programs do not use Spin other than extracting PASM.

    As you have found already, David's spinwrap program will allow using Spin as-is without converting files, etc....

    We are not ready to incorporate that program into SimpleIDE just yet. Higher priorities exist.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-05-30 08:05
    Thanks for the briefing and observations, Jazz.

    The problem comes up when students see an interesting product in the Parallax catalog and want to buy it. But there is no C code example on the catalog page and only source of code is in OBEX is Spin/ASM. That cancels the sale.

    They could write a C program from scratch but after just a 12 hour intro course for hobbyists, coding most devices is above their ability.

    An alternate to the mixed language solution would be to include a C example with each product. For a person (intern?) comfortable with SPIN and C I think it would be quick to knock these out for the 30 or so best-selling devices in the catalog. It doesn't mean a full entry in the Learn system, just a sample like is already offered in BS2 or Spin.

    Thanks.
  • jazzedjazzed Posts: 11,803
    edited 2014-05-30 08:32
    I've often wondered about this.

    It is of course up to Parallax.

    Thanks for the briefing and observations, Jazz.

    The problem comes up when students see an interesting product in the Parallax catalog and want to buy it. But there is no C code example on the catalog page and only source of code is in OBEX is Spin/ASM. That cancels the sale.

    They could write a C program from scratch but after just a 12 hour intro course for hobbyists, coding most devices is above their ability.

    An alternate to the mixed language solution would be to include a C example with each product. For a person (intern?) comfortable with SPIN and C I think it would be quick to knock these out for the 30 or so best-selling devices in the catalog. It doesn't mean a full entry in the Learn system, just a sample like is already offered in BS2 or Spin.

    Thanks.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-05-30 09:40
    You can use spin2cpp to convert a Spin object to C or C++.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-30 09:48
    John,

    It's my (not uncontested) opinion that Parallax erred in going the GCC route instead of concentrating on a simpler compiler that produes Spin bytecodes and links to Spin/PASM objects. Based upon my experience teaching a H.S. robotics course using C with the ActivityBot, I've come to the conclusion that a C compiler that produces fast, optimized code is unnecessary in this environment. The students are not going to be writing low-level drivers or wrestling with function pointers. It's quite enough of a challenge for them to write a while loop with a bunch of conditionals, inside which they make calls to library functions. And if those library functions already exist in Spin, they should be usable in C without having to rewrite them in C.

    Fortunately, I think there may a solution coming in through the back door: Dave Hein's CSPIN C-to-Spin translator. Although it's a standalone command-line program now, I believe it could ultimately be integrated transparently into a development environment so that sudents would never have to know that their C programs were converted to Spin first before compiling. This would permit the current Spin library and OBEX objects to be used freely with C programs without having to recast them in C.

    -Phil
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-05-30 10:28
    I thought about suggesting CSPIN, but it lacks many features that are needed for teaching C to newbies. It does not have an IDE or extensive error checking, which I think are needed for teaching a C course. It also doesn't support a few of the features in the C language, and that could cause some problems when a student wants to try something new in C that isn't supported. CSPIN would be good for teaching a Spin programming class to people that have a C background. It generates readable Spin code that uses the same, or similar variable and routine names. Students could compare the original C code with the generated Spin code to help understand the differences between the two languages.

    As far as whether GCC was the right way to go -- I believe it was. GCC provides many important features that the proposed PMC compiler would not have had. In theory, the GCC code generator could be modified to produce Spin bytecodes. Even though the Spin VM is a stack machine there is at least one implementation of GCC for a stack-base machine (i.e., Zog) so it is proven that it can be done. One issue with the Spin VM is supporting global variables, but this could be done under the covers by using pointers that are initialized at link time.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-05-30 10:40
    Phil:

    (PM on your teaching to HS)

    My comp sci knowledge is not deep enough to understand all you are saying, particularly if a path was a mistake. From applied experience, I'm getting good results from the Learn C tutorials on line and will welcome a version similar to a SIC text book. C seems to be accepted well by my students as many have done at least a little in C or Java.

    The problem I have is very practical and at a business level: Students see a Parallax product that is cool and are ready to press the buy button but then realize they can't use it because there is no C example and the OBEX only has a SPIN object. Then I get an earful of junk that a similar-function board is available elsewhere for Arduino.

    Easiest solution (I think) is to write an example in C before listing the product in the catalog. I think that would just require a policy change along the lines of maybe we have to wait another day to list, but we won't offer a device until there are examples in all of SPIN, C and PBASIC. But I'm in no position to understand all the factors that go into a business decision. I don't have the skills to write code in best practices, but am glad to test if someone does.

    Longer term, it would be great to have a directive in C that instructs "Insert this SPIN object at top of this C program," similar to include an .h library. Then whatever functions are exposed in the SPIN/ASM are available in C.

    I admit I have not tried Dave's tool; I'm a little worried it will be a half day to understand and test (no offense, Dave. I guess I am a slow learned). And I hope that C examples will emerge soon for the devices of my interest.
  • David BetzDavid Betz Posts: 14,516
    edited 2014-05-30 11:04
    Another possible solution to this "OBEX problem" would be for Parallax to find programmers who actually like writing code in C and get them to either convert OBEX objects to C/C++ or just write new libraries that do the same thing. It seems they would have to look outside of this forum for people like this because there is a hostility toward C here. How many useful objects are there in OBEX anyway? I know there are endless variants on various objects like FDS. Obviously, the FAT filesystem objects are heavily used as are some of the video drivers. What beyond that?

    Parallax made a mistake going with GCC? You could argue that Parallax made a mistake putting the Spin VM in ROM in the first place thus locking everything into code that is now more than 8 years old with no opportunity to ever update it.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-30 11:26
    David Betz wrote:
    You could argue that Parallax made a mistake putting the Spin VM in ROM in the first place thus locking everything into code that is now more than 8 years old with no opportunity to ever update it.
    How does that lock anything, Dave? If the VM were not in ROM, it would have to be in RAM. But you can still write one for RAM, whether there's one in ROM or not. (The Forth folks do it all the time!) Besides, the fact that the ROM VM is eight years old does not make it any less usefull than it was eight years ago. Heck, the PASM instruction set is eight years old, too, but nobody's complained yet that it wasn't cast in EPROMable microcode.

    -Phil
  • David BetzDavid Betz Posts: 14,516
    edited 2014-05-30 11:31
    How does that lock anything, Dave? If the VM were not in ROM, it would have to be in RAM. But you can still write one for RAM, whether there's one in ROM or not. (The Forth folks do it all the time!) Besides, the fact that the ROM VM is eight years old does not make it any less usefull than it was eight years ago. Heck, the PASM instruction set is eight years old, too, but nobody's complained yet that it wasn't cast in EPROMable microcode.

    -Phil
    Since Spin compiles to the VM that is in ROM, it can never be updated to use a newer VM. If Parallax had really wanted to support both C/C++ and Spin on an equal footing then they could have produced a common target bytecode instruction set that worked well for each. That will never happen though because the Spin compiler always targets the ROM VM which is unchanged in over eight years. I guess there is no reason that the Spin compiler couldn't ignore the VM in ROM but in practice that has never happened.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-30 11:44
    The ROM VM isn't the problem, since the Spin (or any other) compiler can still be written to produce bytecodes for a different, RAM-resident VM, the same as if the ROM VM never existed. The ROM VM is certainly a convenient target but not a necessary one.

    -Phil
  • David BetzDavid Betz Posts: 14,516
    edited 2014-05-30 11:47
    The ROM VM isn't the problem, since the Spin (or any other) compiler can still be written to produce bytecodes for a different, RAM-resident VM, the same as if the ROM VM never existed. The ROM VM is certainly a convenient target but not a necessary one.

    -Phil
    Try to convince Parallax to modify the official Spin compiler to produce code for a different VM.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2014-05-30 11:50
    David Betz wrote: »
    ...How many useful objects are there in OBEX anyway?...

    The more I think about it the more useful would be the option to have example code in C for each product rather then a generic wrapper for SPIN/ASM objects. With example programs students would see more code and it would be well-written C. From that I can point to how various problems were solved. A wrapper would just point to a black box (from their perspective) and not be useful for teaching.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-30 11:54
    David Betz wrote:
    Try to convince Parallax to modify the official Spin compiler to produce code for a different VM.
    Frankly, I don't see any reason to, since the ROM VM works well and fits in cog RAM, which is probably its most sterling asset.

    But, anyway, we're getting off-track a bit from John's concerns. What we've got are two disjoint compilers, for Spin and GCC. Since Parallax made the decision to go with GCC, I guess the onus is on them to live with the consequences and to produce libraries and example programs written in GCC.

    -Phil
  • David BetzDavid Betz Posts: 14,516
    edited 2014-05-30 11:56
    Frankly, I don't see any reason to, since the ROM VM works well and fits in cog RAM, which is probably its most sterling asset.

    But, anyway, we're getting off-track a bit from John's concerns. What we've got are two disjoint compilers, for Spin and GCC. Since Parallax made the decision to go with GCC, I guess the onus is on them to live with the consequences and to produce libraries and example programs written in GCC.

    -Phil
    I agree and that is what they have been doing with the Simple Libraries effort.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-30 12:00
    With example programs students would see more code and it would be well-written C.
    That's assuming a lot! :) Even if the libraries were well-written in C, they would likely be above the level of most students to comprehend -- at least that would be true for my students. But I guess it depends on the overall objective: to teach the ins and outs of C or to teach something like robotics using C as the vehicle. My class met one hour a week, so there wasn't time for both. I opted for the latter.

    -Phil
  • David BetzDavid Betz Posts: 14,516
    edited 2014-05-30 12:07
    That's assuming a lot! :) Even if the libraries were well-written in C, they would likely be above the level of most students to comprehend -- at least that would be true for my students. But I guess it depends on the overall objective: to teach the ins and outs of C or to teach something like robotics using C as the vehicle. My class met one hour a week, so there wasn't time for both. I opted for the latter.

    -Phil
    I guess that's the problem. To some extent you write libraries or Spin objects or whatever to hide complexity and provide a simple interface to the underlying hardware. If you open up that code you'll often find it is not easy to understand whether written in C or Spin. However, for C students, the C library code should be a bit easier to understand since it is written in the same language they are learning. Also, what they learn by trying to understand the library code can be directly used to improve their own C skills.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-05-30 12:32
    Phil, I think a Spin object that is translated to C would look very similar to the original Spin code. It shouldn't be any more "above the level of most students to comprehend" than a Spin object would be.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-30 13:06
    Dave Hein wrote:
    Phil, I think a Spin object that is translated to C would look very similar to the original Spin code.
    I'm sure that's true if it started out as Spin. I was thinking more of the Learn library code that was written from scratch. I'm even struggling with some of it. But then, I'm no C guru, either. :)

    -Phil
  • GenetixGenetix Posts: 1,754
    edited 2014-05-31 01:11
    Parallax is a small company and has already devoted so many resources towards C on the Propeller.
    I can understand the frustration of not having C code for this device or that device, but how do you think non C users feel?

    I don't know what Parallax devices students are looking at but most of them are very easy to use.
    I guess if Ken listed the 10 most popular devices then instructions on how to use them in C or actual C code for be written.

    Also, couldn't Arduino code be ported to the Propeller?
  • Ken GraceyKen Gracey Posts: 7,392
    edited 2014-06-01 12:01
    But I guess it depends on the overall objective: to teach the ins and outs of C or to teach something like robotics using C as the vehicle. My class met one hour a week, so there wasn't time for both. I opted for the latter.

    -Phil

    It's really the latter, Phil, as you already know. There is no attempt in any of our material to try to teach students how to create low-level libraries along the ins and outs of C. The program is a high-level implementation of C and students can combine our different libraries to achieve their project. There is, however, a fairly light-duty explanation and use of multicore inherit in the design of the program, too. Students can learn some C just by taking a look at our syntax. In combination with any book about C they quickly become more capable.

    There's no need to make this something deeper than it is, at this time. And it's pretty common to spend days and months with students and feel that you've not effectively imparted everything that you'd like them to know. Take your desired rate of teaching and divide it by two or more and you'll succeed. This is their first exposure - some students can carry their understanding and interest deeper but you've got to have a reasonable expectation about how much they will learn, especially in a group setting.

    One thing that guys like you and I will have trouble grasping is how high-level everything has become. So much complexity is handled for us in libraries or objects, and to some extent we have to be comfortable not explaining all of this. You'll get glazed eyes, loss of interest and diversion to YouTube if you press these real programming concepts too early. Frustrating, I realize.

    I understand I took a sideways trip in this reply to you.

    Ken Gracey
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-06-01 12:46
    Ken,

    In a way I feel like a total fraud. The only mandate I was given for the class was that the language used had to be C or C++; the rest of the curriculum was up to me. But given the light C exposure required to accomplish the robotics aspect, the same could have been accomplished in almost any language, given libraries similar to the extensive ones provided by the Learn curriculum. I guess my point is that even though the schools demand C, the students aren't really learning C to any skills-marketable extent, so it could just as well have been Spin. That's not to say that my class was a dud. Far from it. The students learned a lot about robotics and seemed to be totally engaged and having fun doing it. But lacking the C mandate, I think I could have accomplished even more with the class by using Spin instead.

    But it is what it is. Convincing school admins that programming skills can be communicated regardless of the language chosen is probably an impossible uphill battle, so I can't really fault Parallax for choosing to cater to what the schools think they want rather than trying to sell them on something better.

    -Phil
  • Ken GraceyKen Gracey Posts: 7,392
    edited 2014-06-01 16:56
    That's right, Phil. They demand C and they don't even know why. They're just catching up to the wave and they're probably late, but now we need to give them what they want.

    Now, I'd love to mirror the ActivityBot tutorials in Spin/ASM. The benefits of showing off the architecture and having better low-level control are big. We just need some capable people to write the code :)

    Since we've arrived at what we agree on, maybe we can stop the banter about whether or not we should've made a translator, a compiler, or whatever. We aren't going back to repeat the process and in fact I expect to leverage all of our efforts to date for the P2, so there's not a lot of sense in digging up the bones. In fact, it might even discourage potential Parallax educators who are about to use our C tutorials but get confused and then discouraged by reading the forum posts.

    Ken Gracey
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-06-01 17:26
    Ken,

    I didn't really mean for my posts to come across as backward-looking or second-guessing. The OP raised a valid point, and I was just trying to sort out the decisions that led to where we are today and to explore options for how things could get better going forward. You're right: it's all about leveraging what you already have, i.e. getting the most bang for the bucks you've already spent. My objective, looking forward, was to find a way to leverage the current OBEX to make it usable from C, as an alternative to rewriting everything in C. With the help of Dave, David, John, and Steve, I think we had a productive discussion -- including both "can we?" and "should we?" -- with the conclusion that CSPIN is not quite ready for the task I had in mind, and maybe we shouldn't anyway if we want students to see library code written in C. So I'll leave it at that.

    -Phil
  • David BetzDavid Betz Posts: 14,516
    edited 2014-06-01 17:43
    Ken Gracey wrote: »
    Now, I'd love to mirror the ActivityBot tutorials in Spin/ASM. The benefits of showing off the architecture and having better low-level control are big.
    I don't see how Spin would allow you any greater low-level control than C would. I guess you're mostly talking about PASM. That, of course, gives you the ultimate in low-level control but it can be used from C as well.
  • Ken GraceyKen Gracey Posts: 7,392
    edited 2014-06-01 19:16
    David Betz wrote: »
    I don't see how Spin would allow you any greater low-level control than C would. I guess you're mostly talking about PASM. That, of course, gives you the ultimate in low-level control but it can be used from C as well.

    It doesn't. It's just a function of how we've implemented C in our libraries. In order to make C simple to use, we've taken out some clock configuration, visibility of when we launch cores to run libraries, etc. I think it's fine for the intended audience because they get quick results without confusion. For our educational customers I think the approach we've used is correct.
  • David BetzDavid Betz Posts: 14,516
    edited 2014-06-01 20:01
    Ken Gracey wrote: »
    It doesn't. It's just a function of how we've implemented C in our libraries. In order to make C simple to use, we've taken out some clock configuration, visibility of when we launch cores to run libraries, etc. I think it's fine for the intended audience because they get quick results without confusion. For our educational customers I think the approach we've used is correct.
    Sounds good. I should try going through your learn materials. I know the earlier ones were quite good. I'm sure the Propeller C ones are as well!
  • GenetixGenetix Posts: 1,754
    edited 2014-06-01 21:30
    Phil,
    When I took a college class on automation we used C on a 6811 and while some of the functions were in a library others required accessing the hardware directly.
    In Andy's Simple Protocols, he briefly mentions using the AND and OR operators to set and reset hardware registers. Bit manipulation is a very useful skill especially when dealing with hardware.
    I don't think it would be too difficult for students to change the bits in dira and outa, or use a mask on the ina register to see if a switch was pressed.

    Counters are another thing that most microcontrollers have and NCO mode will generate pulses for LEDs, Servos, or music.
    I don't think it would be too difficult for students to configure a counter, set it's frequency, and then start it running by accessing the Cog counter registers directly.

    These hardware basics will prepare students for whatever language or hardware they eventually use plus teach the skill of reading datasheets and looking in reference manuals.
    Learning by doing.

    John's student might want to look at the same lesson since it explains Serial I/O and SPI. Andy might want to expand on the lesson to include I2C. That should cover most devices.
  • jazzedjazzed Posts: 11,803
    edited 2014-06-01 21:42
    David Betz wrote: »
    Sounds good. I should try going through your learn materials. I know the earlier ones were quite good. I'm sure the Propeller C ones are as well!

    I went through the "Propeller C Library Studies" tutorial earlier today. It is quite good except for a type-o here and there.

    I've provided a Keyboard library, but it hasn't made it through the scrubber yet. I'm sure Andy is adding devices as fast as he can, but he's also supporting sales channels. We are all in a crunch to have IDE software updates ready for Educators courses starting this month.

    So much to do. So little time.
Sign In or Register to comment.