Quantcast
Channel: Articles on JeeLabs
Viewing all articles
Browse latest Browse all 296

This node speaks Forth (and C)

$
0
0

If you’ve been following the weblog for a while, you may have noticed that there’s a strong Forth wind blowing nowadays at JeeLabs. The JeeNode Zero is designed for use with Forth.

Setting up a Mecrisp Forth unit is very simple if you use the latest release of Folie, i.e. version 2.8 or later, since it includes all the necessary firmware:

$ folie
Folie v2.8
? Select the serial port:
  1: /dev/cu.Bluetooth-Incoming-Port
  2: /dev/cu.usbmodem3430DC31
? 2
Enter '!help' for additional help, or ctrl-d to quit.
[connected to /dev/cu.usbmodem3430DC31]
!u
These firmware images are built-in:
  1: F103-BMP         50096b  crc:F87A
  2: F103-Blink         724b  crc:4967
  3: F103-Mecrisp     20500b  crc:A585
  4: F103-SerPlus      7052b  crc:7DD0
  5: L052-Blink         504b  crc:54D5
  6: L052-Mecrisp     20500b  crc:7F8D
Use '!u <n>' to upload a specific one.
!u 6
  20500b .+V31 #0417 R .+W .+E161* writing: 81/81 done.
Mecrisp-Stellaris RA 2.3.2 with M0 core for STM32L053C8 by Matthias Koch
Erase block at  00005004  from Flash
Erase block at  00005080  from Flash
Finished. Reset Mecrisp-Stellaris RA 2.3.2 with M0 core for STM32L053C8 by Matthias Koch
  ok.

Note: you have to add a “-r” cmdline argument for raw mode when not using the SerPlus, as well as setting the BOOT0 jumper to “1” and pressing RESET to enter ROM boot loader mode.

As you can see, there are several firmware images built into Folie, so you’ll need to make sure that you send the right one.

With Mecrisp, the very first launch will always generate a duplicate welcome message, because it is set up to clear the rest of flash memory as very first post-re-flash step.

That’s all there is to it. As with the F103 setup, you need to load additional code to make this environment easier to use, i.e. adding L052-specific drivers for various bits and bobs of the L052’s built-in hardware peripherals.

The setup is the same as for F103 boards, but now from the jz3 folder:

  • change dir to embello/explore/1608-forth/jz3
  • launch folie
  • enter !s always.fs
  • enter !s board.fs
  • enter !s code.fs

Here is a transcript of the entire process:

$ cd embello/explore/1608-forth/jz3
$ folie
Folie v2.8
Select the serial port:
  1: /dev/cu.Bluetooth-Incoming-Port
  2: /dev/cu.usbmodem3430DC31
? 2
Enter '!help' for additional help, or ctrl-d to quit.
[connected to /dev/cu.usbmodem3430DC31]
  ok.
!s always.fs
1> always.fs 3:
1> always.fs 4: Finished. Reset Mecrisp-Stellaris RA 2.3.2 with M0 core for STM32L053C8 by Matthias Koch
1> always.fs 11: ( flash use: ) 00005064  ok.
1> always.fs 12: Redefine eraseflash.  ok.
!s board.fs
1> board.fs 4: eraseflash

Finished. Reset Mecrisp-Stellaris RA 2.3.2 with M0 core for STM32L053C8 by Matthias Koch
1> board.fs 6: ( board start: ) 00005100  ok.
1> board.fs 35: Redefine init.  ok.
1> board.fs 63: ( board end, size: ) 000080A8 12200  ok.
!s core.fs
1> core.fs 3: <<<board>>>

Finished. Reset Mecrisp-Stellaris RA 2.3.2 with M0 core for STM32L053C8 by Matthias Koch
64 KB <jnz> 39460C30 ram/flash: 6816 32512 free ok.
1> core.fs 4: cr compiletoflash
 ok.
1> core.fs 5: ( core start: ) 00008100  ok.
1> core.fs 16: ( core end, size: ) 0000AF08 11784  ok.
  ok.
!reset
Mecrisp-Stellaris RA 2.3.2 with M0 core for STM32L053C8 by Matthias Koch
64 KB <jnz> 39460C30 ram/flash: 4928 20608 free ok.

The final reset produces the Mecrisp welcome message, followed by the hello information. This example loads quite a few things (including graphics code and a font, and a 1 KB RAM buffer for an OLED). It leaves ≈ 5 KB of RAM and 20 KB of flash free for application use.

To try a little blink demo, enter this (you’ll need a reset to get out of it again):

: blink begin led iox! 500 ms again ; blink

But hey, there’s nothing preventing anyone from using the JeeNode Zero with C or C++ …

In which case you’ll need to set up the gcc toolchain, as described previously. You’ll also need a few files to get a “make” build working with an proper linker load map. The easiest way is to clone the embello repository from GitHub and look at the explore/1651-l052 subdirectory.

The blink code lives in a subdirectory (unimaginatively called “blink”) and is very simple:

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

int main (void) {
    rcc_periph_clock_enable(RCC_GPIOB);
    gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO5);
    for (;;) {
        for (int i = 0; i < 100000; ++i)
            __asm("");  // idle loop, takes a fraction of a second
        gpio_toggle(GPIOB, GPIO5);
    }
    return 0;
}

The Makefile is also simple, deferring all the real work to a few files in the parent directory:

BINARY = blink
OBJS =
LDSCRIPT = ../stm32l0xx8.ld
SCRIPT_DIR=..

default: $(BINARY).bin
include ../Makefile.include

Typing “make” will build the firmware image, called … “blink.bin“:

$ make
  CC      blink.c
  LD      blink.elf
  OBJCOPY blink.bin

The final step is to launch Folie and upload this firmware image with “!u blink.bin“:

$ folie
Folie v2.8
? Select the serial port:
  1: /dev/cu.Bluetooth-Incoming-Port
  2: /dev/cu.usbmodem3430DC31
? 2
Enter '!help' for additional help, or ctrl-d to quit.
[connected to /dev/cu.usbmodem3430DC31]
!u blink.bin
  504b .+V31 #0417 R .+W .+E4* writing: 2/2 done.

And that’s it: the LED on pin PB5 will start blinking. Amazing what technology can do, eh?


Viewing all articles
Browse latest Browse all 296

Trending Articles