Forth and Propeller HAT
Rsadeika
Posts: 3,837
I saw an interesting comment in one of the other threads, installing Forth on a Propeller HAT. OK, so what could you possibly gain by doing that?
First, how do use that setup, I think you need a terminal program to access the Forth interpreter. And since the HAT setup is lost once you lose power, you have to redo your whole setup, again and again.
Second, since the HAT will have devices/sensors attached, how would you access those from a program on the RPi(bash,python,C,...,etc)? I do not recall Forth having a command line access to it.
Third, how easy is it to add device drivers to Forth, for instance a sht11 module? Their is probably a dozen more items of inquiry, but this may be a good start.
I am sold on the usability of the Propeller HAT, just have to figure out the best way of incorporating it into the RPi system for the easiest and best use.
Ray
First, how do use that setup, I think you need a terminal program to access the Forth interpreter. And since the HAT setup is lost once you lose power, you have to redo your whole setup, again and again.
Second, since the HAT will have devices/sensors attached, how would you access those from a program on the RPi(bash,python,C,...,etc)? I do not recall Forth having a command line access to it.
Third, how easy is it to add device drivers to Forth, for instance a sht11 module? Their is probably a dozen more items of inquiry, but this may be a good start.
I am sold on the usability of the Propeller HAT, just have to figure out the best way of incorporating it into the RPi system for the easiest and best use.
Ray
Comments
Or just use the command line tools, prop-gcc and openspin. Sounds like you have never tried a Forth system or read anything about them. The command line interaction is one of the key selling points of Forth that you learn from the very beginning. Hmmm... on the other hand it ... seems you have the idea. I'm sure the Forth guys have thought about that and have solutions
But consider this... Your HAT is connected to your Pi. So on every power up or reboot some simple program/script on the Pi can reboot the Prop and send it it's program again. There is no need for even a EEPROM on the Prop.
If you want to learn C on the Propeller, SimpleIDE might be wisest. After all, you need to acquire an understanding of all those libraries.
If you want to interact and explore with the Propeller in an interpreted mode, Forth is an excellent choice. This may be better if you are starting at a very basic level and just want to control the Propeller remotely. It requies a serial terminal interface, such as Minicom or Terraterm.
If you want to learn SPIN and PASM, SimpleIDE is equalty valid. So are several other IDE solutions. I use BST due to some added features.
Currently, i am using Forth with a Bluetooth module, and either my Smart Phone or Notebook computer in a terminal mode. The actual Propeller board sits in a box that is powered 7/24 and I just log in when I want to play with it.
IOW, there are lots of ways to set up a Propeller that make for a fun learning environment. It is up to you to create the fun as personal learning goals and preferances vary widely.
BST does not run on a Pi.
Heater answered this quite well, so I will just fill in some detail FYI.
If you load source code into Forth that's compiled on the Prop it will be sitting in RAM but usually we just backup the image to EEPROM so that it's there on power-up which also includes any AUTORUN directive etc. There's no real need to have a terminal program as such, just the serial port but it depends on how it is being used. So it's possible for the RPi to download "scripts" and source code etc as the "text interpreter" is normally a feature of all but the most bare-bones Forths, if there is such a thing.
You mentioned device drivers like the sht11 module for instance but most of the time these "drivers" just end up being a handful of lines of Forth as in the case of the DHT22 sensors[/url[ I did recently. Even a ping routine that returns with the result in millimetres is only 5 lines of code (no PASM):
So if there is an advantage to having Forth it may well be that rather than having to download a new binary to run new code, you can instead just download the commands or scripts etc to effect the same action perhaps. My Tachyon Forth always compiles words as they are encountered in the text stream so by the time a line terminator is encountered it is ready to execute immediately and also without having to create a temporary word just to run that code just as fast as compiled code including loops and branches etc.
I know that the Propeller HAT is being addressed as a learning tool/method for Spin/PASM, but I am looking ahead to some functional uses for the HAT. So, I am trying to explore the idea that I suggested in another thread: using a scripting language, as an easy method, to get a temperature reading from a sht11 module that is attached to the Propeller HAT, whether it would be Forth, Spin, or PASM that is driving it. Of course the easiest experiment would be to simply turn on/off an LED attached to the Propeller HAT via a script language like bash. I think that when you start getting past the actual, just programming HAT stage, their will be some complexity involved in the setup.
Ray
Once the few lines of code are loaded for the sensor and assuming you haven't loaded any formatted report routines for the sensor you could do something similar to what I did for the DHT22. Sending this string will take a reading and format the reply so that:
DHT@ PRINT" DHT22: " .DECIMAL PRINT" 'C @ " .DECIMAL PRINT" %RH" CR
will return a response in this format:
DHT22: 19.3'C @ 41.5%RH
Of course you could just make a word like "REPORT" out of the reporting action so that is all that is needed to ask for a report.
pub REPORT DHT@ PRINT" DHT22: " .DECIMAL PRINT" 'C @ " .DECIMAL PRINT" %RH" CR ;
Once I have that taken care of, using a bash script file, how do I turn on/off the P16 LED? To me it looks like bash does not have a way of accessing the RPi UART to make the communication, so I have to use Python serial to link up? If that works, do I just put in the commands '16 pinout' CR, '16 pinhi' CR to do the job, at least to turn on the LED?
By describing the above action, I just narrowed down that the language that you use will have to have a serial component. I guess that means Python, C, Pascal, freeBasic, just too name a few would be able to do the job. Anything that I am missing?
Ray
should do it if you have permissions set correctly.
I would not want to use BASH to do this, except as a quick test from the command line.
Just now I'm putting together a webserver that will talk between a web page and the serial port. It's written in JavaScript for node.js. The serial code looks like:
FYI, sensors need an ADC, which the Raspberry Pi does not have.
Personally, (and not very surprisingly) I prefer RoboPi for my experiments.
Now if you have a specific set of sensors in mind, you could add them to any propeller board, and plug that board into a USB port on the Pi.
Or get a prototyping board large enough for the sensors you want, plus a prop, and mount that on a Pi (I've mounted one of my larger protoboards on Pi's to make a gang EEPROM programmer)
http://www.mikronauts.com/raspberry-pi/raspberry-pi-i2c-eeprom-gang-programmer/
which I use to program the firmware into blank 24LC512 EEPROM's for my RoboPi kits.
It might better to change the RPi, as changing baud on linux is very well known and all over the internet. The faster the comms, the faster the system, and the prop is already much slower than the RPi.
To you question, there are a couple ways to change the baud.
1) propforth manual found in
~ /V5.5/doc/propforth.htm
search the manual for baud, find the notes for the "serial" word. Mess with that for 15 minutes, and you should be able to figure it out. If not ask for clarification, I'll get back to you.
2) look in the GettingStartedPropForth.txt file in the same directory where you found the kernel, search for baud. It talks about what I mention in (1), and goes to advanced. The reason its "advanced" is we have to do some cypherin'.
The number we specify for baud in the propforth.spin kernel is internally multiplied by 4 in propforth. The value of the actual baud is bigger than the memory location that holds the number, so Sal did this funky thing rather than waste memory (as only one other person has asked about this in the last couple years). Anyway,
57_600 means 57600 * 4 = 230400 baud.
So if you want 9600 buad, you figure 9600 / 4 = 2400. so you chnage
to
or
(underscore are just formatting and are ignored)
and you get 9600 baud. Save the propforth.spin file, and load to the prop using proptool .
You might know that I admire your efforts of documentation and your regular posts about the importance of having strict formulated goals to keep track of a project.
I really like your posts about that, because I personally lack this skill and try to learn from you.
But this one is really stupid, don't you think?
I am aware that Sal writes the stuff and you are 'just' doing the public side of the stuff. But - honestly - Don't you think this is quite confusing?
How about:
just my two cents.
Mike
Thanks for the comments. You are right this is a bit confusing, and yours is also an effective solution. The real issue is Sal was specifically recovering bytes that day, and reduced the dictionary footprint by half or something. At the time this was deemed worth the fuss. Sal figured the way he did it would be the most straight forward; I reviewed it and agreed, and that's what we released. The decisions are up to the guy spending the money and/or time.
Nobody noticed or complained till now, so I don't think this is a show stopper. Divide by four before we type the value into the code, or include the shift in the source; which ever way feels right is fine as long as it gets the job done. In any case, that is the price of those particular two bytes recovered. A better solution would address the root cause, which is that we don't have much memory on the processor; obviously hardware changes are out of scope for this project.
Again, if messing with the propforth kernel source is too confusing, it may be easier to have the RPi run at 230400 baud.
It should be 16x the max baud rate that you want to obtain, so the value for 230400 should be:
Source: https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=73673
I also made modifications to the RPi+, "sudo usermod -a -G dialout pi", added "init_uart_clock=3686400" to the /boot/config.txt file. And after all that, when I do an:
$ echo " \n" > /dev/ttyAMA0
I get "Permission denied" error. Even when I do it with sudo, I still get "Permission denied" error. So, their must be more to it on the RPi+ side, to get access to the port.
Ray
For a, quick and dirty, PropForth may not be the one to use. I believe Tachyon is at 115200 BAUD at default, so I may have to give that a try. If worst comes to worst I might have to write a quick PropGCC program for the QS board, just to lite up an LED. Boy, their just does not seem to be any quick and dirty solutions for testing these ideas.
Ray
Quick and dirty solution:
Doing that via serial commands is only an OBJ block, repeat statement, and if statement away.
Do you mean watching for compiled SPIN code to be sent over serial, dumping it into a pre-determined portion in memory and then loading it into a cog for execution? I'd love to see a working example of this, it's been in the back of my head to try with Propeller HAT since I got started.
I will have to load SimpleIDE 0-9-45 to the RPi, make sure it can access /dev/ttyAMA0, write a quick and dirty C program for accessing the P16-P23 LEDs on the QS board. Then I can try some different programs on the RPi for turning on/off the LEDs on the QS board. This should get me in the frame of mind to purchase a Propeller HAT from Parallax, but I am not so sure about that tedious soldering of the headers...
Ray
It's fine when you only have to do one But yes, that's a lot of joints that I hope we'll soon have the expertise to wave solder!
No, I mean he should just load a Spin program into the Propeller from the RPi instead of using Forth.
Spin over serial is actually very possible. I've been wanting to try it for a while now.
Default baudrate for Tachyon is currently 115200 but to change it and have it lock in you only need to type/send "<mybaud> CONBAUD <cr>". So to change it to 57600 for instance you would send "57600 CONBAUD <cr>" and the baudrate on the next reboot will be locked in at 57600. You can go all the way up to 3Mbaud if you like. Remember that Tachyon compiles "commands" as it encounters them and then executes them in real-time at the end of the line so this provides a very snappy response in this setup.
BTW, the source for the Tachyon kernel is all in one .spin file so even if there were special changes it only needs an "F11" to recompile and burn.
To blink an LED that is on P4 just type:
4 BLINK
To vary the frequency of that blink to 10 HZ just type:
10 HZ
EDIT: I'm not sure why you didn't try Tachyon first when the link to the Tachyon "Explorer" binary is in my sig, and the whole load and go thing is as easy as pie.
Since you are saying that the default is set for 115200 BAUD, at some point, I will probably give it another try.
Ray
I noticed that the binary image was actually set for 230400 baud but I patched it back to 115200 baud so use the latest version just in case. It shouldn't have any LEDs on as the only thing the Explorer version does on boot is to check the hardware and report, so all the I/O is left as inputs with only P30 (TXD) as an output.
If you don't want it to do a hardware test and report on boot then just issue the command "AUTORUN NONE" after which it will be locked in not to do anything special on boot.
Ray
This is not looking good for the good guys.
Ray
A serial terminal program should work just as well as stty and echo/cat/whatever. They both talk to the serial device the same way, and the Linux kernel doesn't care that stty and echo are two separate programs as opposed to just one with a serial terminal program, so there should be no difference.
What's wrong with programming the propeller on your HAT with a spin compiler on the RPi to do stuff at a baud rate that the RPi likes?
from the Pi's camera and determines the detected objects x, y screen position and size then sends a 6 byte packet
to the Propeller after each frame. On the Propeller side I use C but have also used Spin to receive the same information.
I'm sure Tachyon or another language that runs on the Propeller could decipher what the Pi is spitting out.
are you getting the tachyon banner but cannot input?
hardware flow control must be off on your TERM/tty