Taking the JNZ rev3 for a spin Dec 2016

Let’s make the JeeNode Zero do some simple things. Like blinking LEDs in a continuous loop:

The code for this was actually written step by step, and later saved in a file called ex/leds.fs:

forgetram

\ configure one pin as push-pull output
: out ( pin -- )  OMODE-PP swap io-mode! ;

\ configure all the LED pins as outputs
: setup  PA0 out  PA1 out  PA2 out  PA3 out  PA4 out  PA5 out ;

\ turn one pin on for 100 milliseconds
: blip ( pin -- )  dup ios!  100 ms  ioc! ;

\ blink LEDs in a loop, until new input is received from Folie
: go
  begin
    PA0 blip  PA1 blip  PA2 blip  PA3 blip  PA4 blip  PA5 blip
  key? until ;

setup go  \ press <enter> to quit

Let’s go through these lines of code in some more detail:

Speaking of loading, there are two ways to develop with Folie: either enter code interactively and try it out until working as intended, or save definitions in a file and use Folie’s “send”, by entering “!s <file>”. Interactive mode is best when you’re not yet sure about the code and need to try things out, but it’s volatile since everything typed in will be gone after a reset. When saved on file, you can edit and extend as needed, then resend. The forgetram at the start and the setup go at the end help streamline this approach. A mix of both is often used: define the more stable words in a file and resend when changed, then call them interactively.

The above demo code illustrates how to use the essential words defined in flib/stm32l0/io.fs. Here is a summary of all the pin modes you can set with io-mode!, taken from that same file:

IMODE-FLOAT  \ input, floating
IMODE-HIGH   \ input, pull up
IMODE-LOW    \ input, pull down
IMODE-ADC    \ input, analog

OMODE-AF-PP  \ alternate function, push-pull
OMODE-AF-OD  \ alternate function, open drain
OMODE-PP     \ output, push-pull
OMODE-OD     \ output, open drain

OMODE-WEAK   \ add to OMODE-* for 400 KHz iso 10 MHz drive
OMODE-SLOW   \ add to OMODE-* for 2 MHz iso 10 MHz drive
OMODE-FAST   \ add to OMODE-* for 35 MHz iso 10 MHz drive

Now let’s try something a bit more advanced. We’ll attach a BME280 sensor, load a Forth driver for it, and use that to initialise the sensor and get readings twice a second:

The code for this is small, because the driver in flib/i2c/bme280.fs does all the hard work:

forgetram

\ include ../flib/i2c/bme280.fs

\ use PA13 and PA14 to supply power to the BME280 sensor
: bme-power
  OMODE-PP PA14 io-mode!  PA14 ioc!  \ set PA14 to "0", acting as ground
  OMODE-PP PA13 io-mode!  PA13 ios!  \ set PA13 to "1", acting as +3.3V
;

\ configure I2C and the BME280 sensor attached to it
: setup  i2c-init bme-power bme-init . bme-calib ;

\ print BME readings every 500 ms, until new input is received from Folie
: go
  begin
    bme-data bme-calc
    cr . . .
    500 ms
  key? until ;

\ the delay is a hack to force a timeout in Folie before the loop starts
1234 ms setup go

Some notes:

Here is some sample output, with a new reading printed out every 500 milliseconds:

  ok.
!s ex/sensor.fs
1> ex/sensor.fs 23: 1234 ms setup go  (timeout)
0
2176 102972 4635
2176 102975 4634
2176 102975 4634
2175 102964 4634
2175 102964 4634

As you can see, it’s currently 21.76°C here at JeeLabs, the barometric pressure is 1029.72 hPa, and the relative humidity is 46.35%. Due to the excellent factory calibration of the BME280, we are getting realistic and reliable measurements!

Weblog © Jean-Claude Wippler. Generated by Hugo.