Archive

Posts Tagged ‘atom’

Atom & Arduino :: First Program (pt. 2)

May 4th, 2009 John Van Enk 1 comment

Last time we talked about a few changes I made to Atom. This time we’re going to start writing some programs. Our first one will be pretty simple.

First, we need a short C file (this is the only C code, besides the #include‘s and the prototype we use in blink.h, we need to write our selves):

#include "blink.h"
 
/* PORTB corresponds some how to Pin 13. We get
 * a blinking LED on the board. We can also plug
 * an LED into Pin 13 and the GND pins. */
void setLED(uint8_t value)
{
    PORTB = 0xFF * !!value;
}
 
/* main only needs to continously call blink_atom(). */
int main(int argc, char * argv[])
{
    while(1)
    {
        blink_atom();
    }
}

Next, lets make Main in our Haskell file, pull in some modules, and define the AtomConfig:

module Main where
 
import Language.Atom
import Data.Word
 
-- Override some default values
cfg = AtomConfig {
    -- This pulls some stuff in we use below.
    cIncludes = Just ["blink.h"],
 
    -- The (u)intXX_t types are defined in
    -- stdint.h--which we include from blink.h
    cTyper    = Just $ \t -> case t of
                                Bool   -> "uint8_t"
                                Int8   -> "int8_t"
                                Int16  -> "int16_t"
                                Int32  -> "int32_t"
                                Int64  -> "int64_t"
                                Word8  -> "uint8_t"
                                Word16 -> "uint16_t"
                                Word32 -> "uint32_t"
                                Word64 -> "uint64_t"
                                Float  -> "float"
                                Double -> "double"
}

We reference the C include file blink.h. This is here to pull in some utility AVR chip utility functions and “stdint.h” (which defines uint8_t and friends). We also prototype the blink_atom function Atom will eventually define. We’ll go over what to do with this in just a bit.

Next we define main:

-- Main just has to compile the Atom expression
main :: IO ()
main = compile "blink_atom" (Just cfg) blink

Remember that the version of compile used here is not the version included with atom-0.0.2. This is the altered version we discussed earlier.

The first argument tells Atom what to name the top level function–in this case, blink_atom. You’ll notice that this was the function prototyped in blink.h. Secondly, we pass the config we just defined. If one does not wish to define any include files or override the default C types, it’s perfectly fine to pass Nothing here instead–the defaults will be used.

The last argument, blink, is the name of the Atom ()–the type used to describe our system–we’re about to define.

Lets take a look at that:

-- Simple Atom to toggle an LED
blink :: Atom ()
blink = do
    -- Is the LED currently on? (Assume it starts False/off)
    isOn    <- bool "isOn" False
 
    -- Does the toggle counter need a reset? (Assume it starts False/no)
    doReset <- bool "doReset" False
 
    -- Initialize the toggle counter to delayCycles
    toggle  <- word16 "toggle" delayCycles
 
    -- Decrements the toggle counter when it
    -- is greater than 0.
    period 1 $ atom "decrement" $ do
        cond $ value toggle >. 0
        toggle <== value toggle - 1
 
    -- Checks if we need to perform a toggle
    -- reset, and performs it when we need one.
    period 2 $ atom "reset" $ do
        cond $ value doReset
        doReset <== Const False
        toggle  <== Const delayCycles
 
    -- Checks if the toggle counter has expired.
    -- Toggles the LED if it has, then requests
    -- a reset.
    period 2 $ atom "flip" $ do
        cond $ value toggle <=. 0
        setLED isOn
        isOn <== (not_ $ value isOn)
        doReset <== Const True

cond acts as a guard. It ensures that the boolean statement passed to it is true before allowing the rest of the atom to execute. <== is the assignment operator. We'll discuss period in more depth later, but for now, you just need to know that something with a period of 2 will run every other tick1 through the system whereas something with a period of 3 will run every third tick.

delayCycles refers to the number of cycles through the main loop we want to wait before toggling the state of the LED. Since this particular CPU is running at about 16Mhz, we define this as some relatively large number:

-- How many cycles do we want to delay before
-- we flip the LED?
delayCycles :: Word16
delayCycles = 30000

setLED describes how to call the corresponding setLED C function we defined in blink.c. Lets look at the type signature of action:

action :: ([String] -> String) -> [UE] -> Atom ()

So, it expects, for its first argument, a function which takes a list of Strings and returns a String. Its second is a list of "untyped expressions", and it produces an Atom.

Atom takes the list of UE's, converts them into the C symbols (often times something like "e1" or "e2"), and then passes them to the provided function for inclusion in some C statment. We can see this at work in the code snippet below:

-- An action (basically raw C code) to set the value
-- of the LED. setLED() is defined in blink.c.
setLED :: V Bool -> Atom ()
setLED v = action (\[x] -> "setLED(" ++ x ++ ")") [v']
    where v' = ue . value $ v

Here, we take a Bool, and we pass it to the setLED function, which, presumably, turns the LED on when True, and turns it off when False.

This has been a brief discussion of the Haskell code needed to generate the C code for the Arduino board. Next time, we'll investigate the C code that was generated (along with some other artifacts) and how they correspond to the Haskell we have written.

Here are links to the files we've dicussed today:

  1. A tick is a single call to the blink_atom which is the top level Atom function.
Categories: atom Tags: ,

Atom & Arduino :: Some Hacking (pt. 1)

May 1st, 2009 John Van Enk 4 comments

When Tom Hawkins released the Atom package, I stole my friend’s Arduino Diecimila to see if I could get some Atom-generated code to work. Turns out it wasn’t that hard! I’ll be detailing this in the next few posts. I’m going to use this first post to point out two things I changed in atom-0.0.2.

  • By default, Atom hardcodes the C types which correspond to the Atom types. This means that an Int32 is always defined as signed int. Unfortunately, the ATmega168 CPU and the avr-gcc compiler define a signed int as an Int16. I needed a way to customize these types.
  • Even though an Atom action can refer to an external function, I wasn’t able to see a nice way to have Atom plop a #include in the generated code. I added this ability as well.

My changes only end up affecting two exposed functions. To facilitate the include files and the C types, I added an AtomConfig data type (this can be found in Language.Atom.Code).

data AtomConfig = AtomConfig {
    cIncludes :: Maybe [String],
    cTyper    :: Maybe (Type -> String)
}

I also changed the type signature of writeC. It now reads as:

writeC :: Name -> Maybe AtomConfig -> [[[Rule]]] -> [UV] -> IO ()

Now, we don’t actually have to call writeC our selves 1, but it does get called by compile from Language.Atom.Compile. The compile function now has this type signature:

compile :: Name -> Maybe AtomConfig -> Atom () -> IO ()

Other than the example in Language.Atom.Example, this is the only visible code that I changed.

Here’s how you use the AtomConfig type:

cType :: Type -> String
cType t = case t of
  Bool   -> "uint8_t"
  Int8   -> "int8_t"
  Int16  -> "int16_t"
  Int32  -> "int32_t"
  Int64  -> "int64_t"
  Word8  -> "uint8_t"
  Word16 -> "uint16_t"
  Word32 -> "uint32_t"
  Word64 -> "uint64_t"
  Float  -> "float"
  Double -> "double"
 
cfg = AtomConfig {
    cIncludes = Just ["blink.h"],
    cTyper    = Just cType
}

First, we define a function that takes a Type and produces a string representing the C type. We build the config with an optional list of include files 2 and an optional typing function. If Nothing is passed to both fields, then Atom will not use any include files and will use the built in type converter.

In the next post, we’ll start building a simple program: blink an LED hooked up to some I/O pins on the Arduino.

I’ve posted a diff of my changes to atom-0.0.2. Any criticisms are welcome.

  1. At least, I haven’t seen a reason to yet–perhaps this should not be exposed?
  2. These includes are the kinds that use quotes, not angle brackets. We’ll see #include "blink.h" rather than #include <blink.h>.
Categories: Distraction Tags: ,