Difference between revisions of "Forth"

From eLinux.org
Jump to: navigation, search
m (Adding new words)
(Adding new words)
Line 27: Line 27:
 
prim P_gert_setport() // channel state ---
 
prim P_gert_setport() // channel state ---
 
{ // Set a digital io port to a specified state
 
{ // Set a digital io port to a specified state
  Sl(2); // Make sure there is at least two params on the stack
+
  int rev;
// Your code goes here
+
  Sl(2);
 +
  // Map the I/O sections
 +
  setup_io();
 +
  if (S1 > 25)
 +
    trouble("Max GPIO is 25");
 +
  if (S1 == 21)
 +
    { // first find out which rev of RPi we have
 +
      rev = pi_revision();
 +
      if (rev != 1)
 +
        // GP21 on Gertboard is controller by GPIO27
 +
        S1 = 27;
 +
  }
 +
  INP_GPIO(S1);
 +
  OUT_GPIO(S1);
 +
  if (S0 == 1)
 +
    GPIO_SET0 = (1<<S1);
 +
  else
 +
    GPIO_CLR0 = (1<<S1);
 +
  Pop2;
 
}
 
}
  

Revision as of 05:42, 7 July 2013

Forth on RPi

sudo apt-get install wget
wget http://www.fourmilab.ch/atlast/download/1.2/atlast-1.2.tar.gz
tar -xzvf atlast-1.2.tar.gz
cd atlast-1.2/
make

Start the Forth interpreter with ./atlast and exit with Ctrl-D

atlast.html and atlast.pdf is included and an is an extensive atlast-forth manual. Read it online [1] and read about Forth itself here [2] (Beware that the Forth dialect in the book Starting Forth is a bit outdated compared to Atlast Forth).

Adding new words

Most of the power of Atlast derives from the ease with which C coded primitives can be added to the language. It is easy to add your own words, just add a "define GERTBOARD" to atlast.c around line 56. In my case I will add some words for controlling my Gertboard. I will leave it as a skeleton until I've done the implementation myself. If you can't wait there is a detailed description on how to do this in the atlast-forth manual. And you can copy much of the word implementations from the gertboard_sw directory if you have downloaded the gertboard demo files.

#define EVALUATE                      /* The EVALUATE primitive */
#define FILEIO                        /* File I/O primitives */
#define GERTBOARD                     /* Gertboard functions */

Then add your own word definitions at the end of the section with word definitions, around line 2704, right after #endif /* COMPILERW */ in atlast.c

#ifdef GERTBOARD

prim P_gert_setport() // channel state ---
{ // Set a digital io port to a specified state
  int rev;
  Sl(2);
  // Map the I/O sections
  setup_io();
  if (S1 > 25)
    trouble("Max GPIO is 25");
  if (S1 == 21)
    { // first find out which rev of RPi we have
      rev = pi_revision();
      if (rev != 1)
        // GP21 on Gertboard is controller by GPIO27
        S1 = 27;
  }
  INP_GPIO(S1);
  OUT_GPIO(S1);
  if (S0 == 1)
    GPIO_SET0 = (1<<S1);
  else
    GPIO_CLR0 = (1<<S1);
  Pop2;
}

prim P_gert_getport() // channel  ---
{ // Get the state of a digital port
   Sl(1); // Make sure there is at least one param on the stack
	// Your code goes here
}

#endif /* GERTBOARD */

And finally, add the actual words to the Table of primitive words, right after #endif /* EVALUATE */ at line 2960 or so.

#ifdef EVALUATE
    {"0EVALUATE", P_evaluate},
#endif /* EVALUATE */

#ifdef GERTBOARD
	 {"0SETIO", P_gert_setport},	
	 {"0GETIO", P_gert_getport},		
#endif /* GERTBOARD */

Now, save and run "make" again to recompile atlast.c.