Wellys Dev

  • Home
  • Search/Topics
  • Writings
  • About
  • 2022-05-06
    Mecrisp-Stellaris Forth: Dictionary 0

    Where I keep my dictionary 0 with all of the basic Forth definitions.

    Introduction

    It helps to have the complete dictionary for Mecrisp-Stellaris Forth, this is dictionary 0 which has my most basic definitions. I load this before I start doing any work in Forth on the RP2040.

    \ main dictionary for words which have been debugged
    compiletoflash
    : words4 ( -- ) cr  \ A columnar Word list printer. Width = 20 characters, handles overlength Words neatly 
       0				    \ column counter
       dictionarystart
          begin
    	 dup 6 + dup
    	 ctype			    \ dup before 6 is for dictionarynext input
    	 count nip		    \ get number of characters in the word and drop the address of the word
    	     20 swap - dup 0 > if   \ if Word is less than 20 chars
    		   spaces swap	    \ pad with spaces to equal 20 chars
    		   else drop cr	    \ issue immediate carriage return and drop negative number
    		   nip -1	    \ and reset to column -1
    		   then		       
    		      dup 3 = if 3 - cr \ if at 4th column, zero column counter			   
    		      else 1 +
    		      then
    	 swap		       
    	 dictionarynext		    \ 	( a-addr - - a-addr flag )
          until
       2drop
    ;
    
    : freememory ( -- )
    	compiletoflash unused ." FLASH: " .
    	compiletoram unused ." RAM: " .
    ;
    
    \ xterm colors 256!
    \ https://github.com/sindresorhus/xterm-colors
    : esc 27 emit ;
    : black      ( -- cursor colour )  esc ." [38;5;0m" ;
    : red        ( -- cursor colour )  esc ." [38;5;9m" ;
    : green      ( -- cursor colour )  esc ." [38;5;2m" ;
    : purple     ( -- cursor colour )  esc ." [38;5;93m" ;
    : blue       ( -- cursor colour )  esc ." [38;5;12m" ;
    : magenta    ( -- cursor colour )  esc ." [38;5;127m" ;
    : cyan       ( -- cursor colour )  esc ." [38;5;51m" ;
    : white      ( -- cursor colour )  esc ." [38;5;15m" ;
    : grey       ( -- cursor colour )  esc ." [38;5;8m" ;
    : fuchsia    ( -- cursor colour )  esc ." [38;5;13m" ;
    : green3     ( -- cursor colour )  esc ." [38;5;34m" ;
    : lime       ( -- cursor colour )  esc ." [38;5;10m" ;
    : navy       ( -- cursor colour )  esc ." [38;5;4m" ;
    : darkorange ( -- cursor colour )  esc ." [38;5;208m" ;
    : grey62     ( -- cursor colour )  esc ." [38;5;247m" ;
    : grey82     ( -- cursor colour )  esc ." [38;5;252m" ;
    
    : test_black      black      ." BLACK black " black ;
    : test_red        red        ." RED red " black ;
    : test_green      green      ." GREEN green " black ;
    : test_purple     purple     ." PURPLE purple " black ;
    : test_blue       blue       ." BLUE blue " black ;
    : test_magenta    magenta    ." MAGENTA magenta " black ;
    : test_cyan       cyan       ." CYAN cyan" black ;
    : test_white      white      ." WHITE white " black ;
    : test_grey       grey       ." GREY grey " black ;
    : test_fuchsia    fuchsia    ." FUCHSIA fuchsia " black ;
    : test_green3     green3     ." GREEN3 green3 " black ;
    : test_lime       lime       ." LIME lime " black ;
    : test_navy       navy       ." NAVY navy " black ;
    : test_darkorange darkorange ." DARKORANGE darkorange " black ;
    : test_grey62     grey62     ." GREY62 grey62 " black ;
    : test_grey82     grey82     ." GREY82 grey82 " black ;
    
    : colors 
            cr test_black cr test_grey cr test_grey62 cr test_grey82 
            cr test_white cr test_red cr test_darkorange cr test_lime
            cr test_green3 cr test_green cr test_cyan cr test_blue
            cr test_navy cr test_magenta cr test_fuchsia cr test_purple
            cr
    ;
    
    : bp blue cr . .s cr black ;
    
    $40014000 			constant IO_BANK0_GPIO0_STATUS \ GPIO status
    $40014004 			constant IO_BANK0_GPIO0_CTRL \ GPIO control including function select and overrides.
    $d0000000 			constant SIO_BASE
    
    #5 			    constant SIO 				\ SIO (F5) DS_p258
    SIO_BASE $004 + constant GPIO_IN       \ Input value for GPIO
    SIO_BASE $010 + constant GPIO_OUT      \ GPIO output value
    SIO_BASE $014 + constant GPIO_OUT_SET  \ GPIO output value set
    SIO_BASE $018 + constant GPIO_OUT_CLR  \ GPIO output value clear
    SIO_BASE $01c + constant GPIO_OUT_XOR  \ GPIO output value XOR
    SIO_BASE $020 + constant GPIO_OE       \ GPIO output enable
    SIO_BASE $024 + constant GPIO_OE_SET   \ GPIO output enable set
    SIO_BASE $028 + constant GPIO_OE_CLR   \ GPIO output enable clear
    SIO_BASE $02c + constant GPIO_OE_XOR   \ GPIO output enable XOR
    
    \ Feather RP2040 localization
    #13					constant GP13
    GP13      			constant LED
    #2						constant minGPIO
    #29					constant maxGPIO
    #0						constant minTest
    #3						constant maxTest
    #8						constant padsize
    
    : GPIO_ctrl ( GPIO -- ) \ get the address for the specific GPIO ctrl register
    	#8 * IO_BANK0_GPIO0_CTRL +
    ;
    
    \ print values of the GPIO_CTRL registers of all GPIO pins
    : .CTRL ( -- ) \ print CTRL values of all GPIO pins
    	30 0 CR DO 
    	I GPIO_ctrl @
    	I . . CR
    	LOOP 
    ;
    
    : one_sec ( -- ) 		\ one sec ( -- )ond delay
    	1000 ms
    ;
    
    : half_sec ( -- )		\ half sec ( -- )ond delay
    	500 ms
    ;
    
    : qtr_sec ( -- )		\ quarter sec ( -- )ond delay
    	250 ms
    ;
    
    : tenth_sec ( -- )		\ tenth sec ( -- )ond delay
    	100 ms
    ;
    
    : GPIO_F5 ( GPIO -- )		\ ensure GPIO is in F5
    	dup GPIO_ctrl 
    	@ %11111 and
    	5 = if drop else ." Not F5! " 5 swap GPIO_ctrl ! then
    ;
    
    : GPIO_OUT ( GPIO -- )	\ set GPIO to output, uses atomic set
    	1 swap lshift GPIO_OE_SET !
    ;
    
    : tog_GPIO ( GPIO -- )	
    	1 swap lshift GPIO_OUT_XOR !
    ;
    
    : high_GPIO ( GPIO -- )	
    	1 swap lshift GPIO_OUT_SET !
    ;
    
    : low_GPIO ( GPIO -- )	
    	1 swap lshift GPIO_OUT_CLR !
    ;
    
    : blink_GPIO ( GPIO -- ) 
    	dup GPIO_F5
    	dup GPIO_OUT 
    	  begin
    	    dup tog_GPIO
    	    tenth_sec ( -- )
    	  key? until drop
    ; 
    
    : ms_blink_GPIO ( n GPIO -- ) \ blink GPIO every n milliseconds, until key
    	dup GPIO_F5
    	dup GPIO_OUT 
    	  begin
    	    dup tog_GPIO
    	    swap dup ms swap
    	  key? until drop drop
    ; 
    
    : us_blink_GPIO ( n GPIO -- ) \ blink GPIO every n microseconds, infinite
    	dup GPIO_F5
    	dup GPIO_OUT 
    	  begin
    	    dup tog_GPIO
    	    swap dup us swap
    	  again drop drop
    ; 
    
    padsize buffer: pad
    : .pad 
    	padsize 0 do 
       pad I + c@ hex .
       loop
    ;
    
    : erase_pad 
    	padsize 0 do 
       0 pad I + c! 
       loop 
    ;
    
    : endofDict0 ;
    0 save#
    compiletoram
    
  • 2022-04-20
    Using Makefiles to Automate Development

    Where I demonstrate how to use make and makefiles to automate your build process.

    Update

    While this entry accurately describes how to use a Makefile, the approach has changed significantly. See Developing in C for the ATmega328P: Make, Makefile and env.make for the latest information.

    Automate using a Makefile

    We’ll use the Makefile from Elliot William’s book, he has in the folder setupProject. This Makefile is comprehensive and delivers an Arduino IDE type of simplicity with significantly increased speed. I’ve made some changes to it to make it easier to switch between different types of systems. Here is the file:

  • 2022-04-19
    Comparing Board and Language Speeds

    Where I compare the execution speeds of different combinations of boards and languages. I will continue to update this post with other languages and processor combinations.

    Table for the impatient

    ucontroller/Speed(MHz) Method* frequency Language
    ATSAMD21/48Mhz Integral .6kHz CircuitPython
    ATSAMD21/48Mhz Integral function .7kHz CircuitPython
    ATSAMD21/48Mhz Library .7kHz CircuitPython
    RP2040/133Mhz Integral function 1.0kHz CircuitPython
    RP2040/133Mhz Library 1.44kHz CircuitPython
    ATmega328/16MHz struct/function pointer 6.1kHz Arduino C++
    ATmega328/16MHz words in an infinite loop 27KHz FlashForth
    ATmega328/16MHz struct/function pointer 55kHz C
    ATmega328/16MHz struct/function pointer 56kHz Arduino C++ w/ native toggle
    ATmega328/16MHz Assembly language toggle 108kHz FlashForth
    ATmega328/16MHz Assembly language toggle inlined 444kHz FlashForth
    RP2040/133Mhz struct/function pointer 578.7kHz C
    RP2040/133Mhz words in an infinite loop 2.841 MHz Mecrisp Forth
    *See text for an explanation of method.

    Introduction

    While writing about CircuitPython and the FIDI board, I was curious as to the execution speed of CircuitPython on a extremely powerful (relative to the AVR ATmega328) ARM M0+ microcontroller. The M0+ is a modern RISC 32-bit processor with a considerable amount of memory, while the ATmega is 20 year old RISC 8-bit processor with a limited amount of memory. That said, one can’t run CircuitPython on ATmega processors, one must use C or Forth.

  • 2022-04-18
    Developing CircuitPython for the FIDI

    Where I go into detail as to how I develop code in CircuitPython for the omzlo FIDI board.

    Omzlo FIDI

    Technical details

    • Small!: it measures 25.4mm x 22mm (1" x 0.86")
    • Microchip ATSAMD21E18A 32-bit Arm Cortex M0+ running at 48MHz, with 256KB flash, and 32KB RAM
    • 4MB flash for CircuitPython code and other files
    • Six GPIOs, featuring SPI, I2C, UART, Digital/Analog IO, PWM, …
    • 3.5mm terminal block connectors and one 4 pin QWIC/STEMMA QT connector
    • 3.3V logic level, maximum 200mA
    • USB micro connection to PC
    • User-controlled RGB LED

    Pinout

    Pinout of omzlo FIDI

    Large Version to see detail

  • 2022-04-11
    Circuitpython on the omzlo FIDI

    Where I evaluate an interesting prototyping board using CircuitPython.

    Sources

    • omzlo FIDI
    • Github Repository for this entry

    Introduction

    I ran across this board, the omzlo FIDI via an Adafruit blog article. I was struck by its size and utility along with its relative powerful processor for its size. From the webpage:

    • Microchip ATSAMD21E18A 32-bit Arm Cortex M0+ running at 48MHz, with 256KB flash, and 32KB RAM
    • 4MB flash for CircuitPython code and other files
      Photo of omzlo FIDI

    Large Version to see detail

  • 2022-03-31
    Git: Beginning to Use It (ESP32)

    Where I discuss what to do once you’ve downloaded a repository from Github.

    Introduction

    I’ve advocated using the AVR_C (among others) repository to learn how to use C instead of the Arduino “C++” language. (I write C++ in quotes as while the language used is C++, there are some extensions which some people assume are part of the language, causing more confusion.) In this post, I want to start a short series on using Git as it is extremely helpful, once you begin to develop code. And it becomes mandatory, once you begin using someone’s git repository.

  • 2022-03-11
    ESP32: Automation

    Where I discuss different methods of automating the process of compiling/linking/loading code for an embedded microcontroller.

    Update March 13, 2022

    For automation to work well, it needs to work on multiple platforms along with other automation. For example, I use git as my versioning system, and GitHub for Dashboard, the repository for this entry. The problem I had with Option 2 ESP32 and Make below was that it is difficult to incorporate environmental variables. Environmental variables are values used locally to define specific aspects of your development environment. I use the environmental variables, AVR_PORT and AVR_MCU in my AVR_C code to set the microcontroller and serial port. On my Mac in my .zshrc file, I have

  • 2022-03-07
    Developing in C for the ATmega328: PRNG, FSM and more!

    Where I discuss psuedo-random number generators (PRNG), finite state machines (FSM) and other software topics.

    Sources

    Mersenne Twister

    • TinyMT - Tiny Mersenne Twister
    • TinyMT on GitHub
    • Mersenne Twister Readings

    Introduction

    This entry covers several topics, random number generation, finite state machines and native programming. All of the code discussed is in AVR_C on GitHub. The objective is to continue to expand on the development of a standard C library for the Arduino Uno and other ATmega328-based microcontroller boards.

  • 2022-02-17
    ESP32: Using a modular approach

    Where I examine the differences between writing a monolithic program versus a much more modular program.

    Important

    While I display some of the code on this page, the absolute truth of working code is on github. Use it to fully understand and/or implement what I describe below. The github site also contains the latest information in its README.

    Introduction

    Many of the examples of programming the ESP32 are monolithic programs, the data gathering code is combined with the setup code and the web display code. A monolithic approach is not a good programming practice for several reasons. I’ll go into detail as to why you won’t want to program in this manner, as well as how to write code in a more modular fashion.

  • 2022-02-11
    ESP32: Solving Issues with Arduino Development

    Where I demonstrate identifying and solving specific issues with developing code for the ESP32 using the Arduino framework.

    Use Both the IDE and the CLI

    While my entries are primarily related to using arduino-cli, I will use the IDE for a couple of reasons:

    1. To test something that might work or should work, however, it isn’t. For example, if I buy a new board, I’ll use the IDE and blink to simply check if the board works.
    2. To upload data to the SPIFFS (of LittleFS ) filesystem. Its very easy (see below) to add SPIFFS file upload capability to the IDE. Its not quite as easy using the command line and/or I haven’t spent time attempting to understand it.
    3. To find the USB port. Sometimes the easiest method of determing the USB port is to fire up the IDE, attach the board and see what port shows up.

    Sublime Text Integration

    Moved to here - Automation

Page 8 of 15
Copyright © 2025 Lief Koepsel
  • Home
  • Search/Topics
  • Writings
  • About