Close

Turning on an LED

A project log for Forth powered RISC-V microcontroller

Getting a hand-written Assembly Forth running on the 32-bit Longan Nano

alexander-williamsAlexander Williams 01/22/2023 at 01:260 Comments

I recently implemented the ability to handle HEX strings (it stores them as a 32-bit binary value) and now it's much easier to work with hardware.

As an example, I demonstrate how to turn on the blue LED on GPIOA pin 2:

: blue_led 0x40010800 @ 0xFFFFF0FF and 0x00000300 or 0x40010800 ! ;
blue_led

This is a very simple and inelegant solution, but it works!

There's some documentation on how/why it works here, but essentially it boils down to:

  1. read the current GPIOA port config
  2. apply a mask to clear the 4 bits for pin 2
  3. apply the new pin 2 config
  4. store the new GPIOA port config
  5. execute the new word to turn on the blue LED

I think this shows the merits of Forth. It's possible to easily define more robust functions (words) which for example might take some arguments from the stack (ex: GPIO port, pin number, status), and then perform calculations on those values to set the new config - which could then be used to toggle any pin, etc. As long as you know the HEX address of the memory-mapped devices, pretty much all the hardware can be controlled and configured interactively.

Discussions