Take The Blue Pill And Go Forth

Forth has a long history of being a popular hacker language. It is simple to bootstrap. It is expressive. It can be a very powerful system. [jephthal] took the excellent Mecrisp Forth and put it on the very inexpensive STM32 “blue pill” board to create a development system that cost about $2. You can see the video below.

If you have thirty minutes, you can see just how easy it is to duplicate his feat. The blue pill board has to be programmed once using an STM32 programmer. After that, you can use most standard Forth words and also use some that can manipulate the low-level microcontroller resources.

The blue pill boards are really quite amazing for the price, offering a lot of computing power for a few bucks. Adding an interactive high-level language to it makes it even more usable. We have covered the platform several times using the Arduino IDE and Platform.io.

We’ve done our own take on this same topic. Mecrisp is available for quite a few other platforms and we’ve covered quite a few of them. If you want to know more about Forth as a language, [Elliot Williams] covered that earlier this year.

38 thoughts on “Take The Blue Pill And Go Forth

  1. This reminds me of ByPic. What do people think of it? Recently I wanted a 32bit PIC to ditch into a one-off project after desiging it on a uno32.

    I couldn’t find anything so I repurposed a cheap bypic I found on ebay.

    Otherwise it looks like a PoS (despite me never actually using ByPic).

    I’d definitely take Forth over that weird C, BASIC love child.

  2. Oooh, I’ll be in Shenzhen next week, need to grab one of these…

    Thanks, Al for posting these Forth articles and keeping it on the radar! Forth is definitely worth the effort to at least learn and understand what it’s all about, even if you end up back in the land of C/Python/etc. It’ll give you a fresh perspective and insights to make you a better programmer no matter which language you choose. Maybe, just maybe you won’t wanna go back… :)

    Forth is definitely alive and well, and it’s nice there are a variety of options to get started and explore. We use Forth professionally (where time=money and the modest investment in a commercial Forth package has paid for itself an order of magnitude times over)–it’s so efficient, productive, and dare I say FUN! To have that interactivity on the target MCU has us totally hooked and we usually can’t wait for the next Forth-based project to begin!

  3. Having used Forth in the far past, I cannot recommend it given that nowadays you have a good choice of much more powerful and easier to use languages for microcontrollers. Forth was great for its very low footprint but it is not an intuitive language for most people. If you like to play with programming languages for fun or if you are very resource constrained, it may work for you but for most people it is better to stick with more conventional languages.

    1. “Intuitive” is a myth. Everything relies on the background knowledge and assumptions the user makes. Just because you find X “intuitive” and Y “clunky” says more about your knowledge than the relative qualities of X and Y.

      This is why a good programmer should learn new languages every so often, and especially those that aren’t like the ones they normally use. It provides you with tools and techniques you may never have considered possible, simply because you never learned to think those ways before.

      1. Forth isn’t a new language, it’s probably older than you. I cut my teeth on Fortran IV writing data processing code for AF flight test data back in the early 80’s. Forth was old then and only hard core nerds were into it. I was one of them.

        Some languages are really not intuitive and Forth is one of them. It’ was popular in the late seventies to the early 90’s for because it worked on memory constrained micros and it was easy to bring up. A decent programmer could easily write up a sub-routine threaded Forth interpreter for a small micro with just a cross assembler. You should try it, it’s fun and you’ll learn a lot.

        However as micros got more memory and C cross compilers got better and faster, it did away with the need for Forth in many instances. This is why the language has almost vanished from the programming world.

        Forth also had other issues:
        No standard, everyone rolled their own because it was so easy. ANS94 exists but no one follows it.
        It is basically a write once language in the vein of APL and J.
        It doesn’t lend itself to being readable
        It doesn’t scale to larger projects.

        1. I will be honest. I think unreadable Forth is in the developer, not the language. If you do it right it can be VERY readable (especially at the top level). It is hard to make a language that you can’t write obscure code in. Some languages just put more of the onus on you. Same for say, “object orientation” — you can write OOP code in C–you just have to make yourself do it. You can write non-OOP in C++, too. It is just easier to write OOP in C++. You can even write OOP assembly, if you choose to do so.

          http://www.drdobbs.com/embedded-systems/object-oriented-programming-in-assembly/184408319

          For example.

          1. thank you! I write OO in C all the time (sometimes to the annoyance of coworkers). And when OO was a new vogue in the 80s, it came naturally to me because of my device driver work, which was all assembly, since device drivers naturally benefit from API abstraction and instance data encapsulation.

          2. I actually taught a class to coworkers on the old Space Station Freedom project (not the ISS) about how to write OOP because someone promised NASA we would and no one knew what it meant ;-) And that was with a non-OOP language. One of my favorite lectures I used to give classes was on how ActiveX did OOP at the binary level. Really well done if you understand it, but not many bothered to. IT was all wrapped up in the MS languages so you didn’t notice.

            The meat of that lecture was: What are the 3 things you really want from an OOP system? Code reuse, encapsulation, and polymorphism. Sometimes people say, “What about inheritance?” But that’s just what people use to get those other things. In ActiveX, you can “inherit” from a C++ object even though you are writing in C#, etc. All binary.

          3. It may be that polymorphism is just a type of inheritance and that you can do OOP without it just fine. You do have to have the inheritance. It may simply be that inheritance is the required feature and that polymophism is just an optional higher level pattern.

            Actually that should obviously be true if you consider the way languages with native OOP features differ on the subject. Everybody has inheritance, not everybody has polymorphism, and they can’t even agree on what it means anyways. Obviously you need some sort of way to define interfaces, and attach them to something so that code reuse can happen in the OOP style. But it makes little difference to the OOP-ness of the code if you use polymophism, duck typing, or some other method of composing interfaces.

          4. Nope. You don’t need inheritance. Look at how active x does it. Think Java interfaces even though Java supports both methods. You don’t need inheritance. You need reuse and polymorphism. Most systems get that via inheritance and most people think that’s the goal. But it isn’t.

        2. New for the *programmer*, duh. I didn’t say anything about the age of the language. It’s not about making it a primary language, just about broadening your knowledge base.

          A language “fails” as much for marketing reasons as for technical ones. We don’t use C/Java/Javascript because they’re the best languages in existence.

    2. Yep.

      I coded in Forth back in the 80’s and 90’s and dropped it for better HLL’s, C and Pascal that didn’t suffer from Forth’s problems. Such as it’s lack of readability and non-intuitive nature. and that it didn’t scale well.

      Unless you’re really, really good at writing highly factored code(which most aren’t), and like thinking in terms of keeping track of stack manipulations and doing RPN . Forth isn’t for you.

      1. “Unless you’re really, really good at writing highly factored code(which most aren’t), and like thinking in terms of keeping track of stack manipulations and doing RPN . Forth isn’t for you.”

        Or want to develop these skills, which have use across a wide variety of contexts, in which case forth is a good platform to learn with.

  4. As soon as I see the word “pill,” I know that operation requires tinfoil. And if not, the forums certainly would. ;)

    A decade ago it would have sounded like a movie reference, but these days it is a very political thing to say, and the politics are a very narrow niche.

    Forth is really really popular these days, especially with retired and semi-retired blog authors. These guys love their Forth, surely. What is really missing from the articles though is an actual reason why it would benefit new people to learn it. If there is no reason, you’re just promoting whatever you like, that might be more of an anti-pattern than good advice. There are lots of things that are a great choice if you made it, but that would be horrible advice to somebody that didn’t already know they wanted to make that choice.

    When they try to talk about the reasons, they end up just listing some features (that involve tradeoffs) without actually getting to any clear reasons.

    Even people wanting to say something nice about Forth in the comments usually concede that of course you’ll be back to C pretty quick, but they promise a “new perspective.” Haskell might provide a new perspective, but Forth is only really new for people who never tried to torture themselves by using Reverse Polish Notation in math class. I’ll concede right there that for some accounting tasks it takes less button-presses on a calculator in RPN mode. You can save multiple seconds per day under the right circumstances.

    Yeah, it was often the first thing to run on new platforms, because that testing was the job of the main author! They just want to know that everything works, they don’t care what software language you use, so it works great in that scenario. But it might not have the same value when you’re a new user.

    1. Call me when you can bring up a self-hosted interactive C environment on a small micro. I would respond to the rest of it, but honestly don’t know where to start. You clearly have your mind set. Based on other comments and my personal experience I will be glad to leave you bewildered by RPN and untainted by different perspectives. Your earlier misunderstanding of OOP is all I need to see the value of your point of view.

      If i’m picking between the semi retired blog authors and the self righteous self appointed arbiter of All That is Good, I think I’ll stick with the bloggers.

      Hackaday: Can we get a mute button in comments or at least an up vote/down vote like SlashDot so we can filter out this kind of crap?

    2. There are many different languages – they all have their purpose – otherwise they would be dead.
      Forth started close to the hardware and in many cases this has not changed – so Embedded Applications.
      Some aspects:
      In commercial applications that I know about, Forth is just be used for the rapid prototyping stage – even if then afterwards the proven result is converted to C or the language of choice in the relevant OEM.
      But the time saved to prove and demonstrate the prototype quickly is worth the additional step and cost.
      Time to Market.
      You cannot beat Forth in this interactive development environment. You always stay in the edit-compile-test loop, as compile time is probably 1 second – just enough time to have a quick sip of coffee to stay awake ….
      And it seems you are talking to the wrong people.
      Forth has its place and as we all know, some like it and some hate it – not much different to football.
      By the way, the interactive Forth advantages are now available for Mixed Language Programming – via MPE’;s Sockpuppet. But this is obviously limited to the people who work in such applications.
      Just try it out, for low level entry there is the Forth Bookshelf at https://www.amazon.co.uk/Juergen-Pintaske/e/B00N8HVEZM, and especially here the A Start With Forth, as quite a bit of the contents is available at https://wiki.forth-ev.de/doku.php/en:projects:a-start-with-forth:start0 . Some parts do not even need installation – just try Nick’s easyForth – a Javascript.
      And there are many more books available which cover the more demanding requirements.

      1. I am commenting here, since this is the first reply that used the word “embedded”. I found that Forth, like Basic, was typically interpretive and not compiled and “burned” into code.

        Like many, I found Forth to be cryptic, but it was compact and worked great for older systems like my S-100 Z80 or 6809, both limited to 64KB of code/data space. I cut my teeth on Fortran IV (using the WATFOR and WATFIV precompilers that prevented a lot of wasted mainframe time), then toyed with various dialects of Basic (another mostly interpreted language like Forth), and numerous assembly languages. True, you can do anything in any language, but there are limits. Something only becomes “intuitive” once it has been used so much that it becomes second nature.

        On older systems, if you had the space for the interpreter, then you could interactively play with Basic or Forth. Back then (circa late 70’s early 80’s), memory was expensive. Flash did not exist and it’s precursor, UV EPROM, had a very slow turn-around time. UPSs were a dream, so if power was lost, all the code had to be reloaded. Forth could be embedded, but more typically the program was running in SRAM without any kind of backup. Using a Forth based Z80 assembler “dictionary” was abysmal.

        My college senior project was a homemade programmer for an 256 byte Intel 1702A UV EPROM (with +5V & -9V supplies and requiring -46V programming pulses!) on a mostly homemade Intel 8008 “PC” (a Radio Electronics Mark IV CPU PC) with a125KHz clock and custom 2KB SRAM board. This was like the agony and the ecstasy — fun getting there, but way too much work! All the hand-written assembly code was entered via a homebrew hexpad (replacing the original binary paddle board switch entry like the old minis), plus I had to manually enter the data for the UV EPROM. I’m not masochistic, so I really don’t miss those days.

        I was never a fan of Turbo Pascal, since Wirth eliminated forward references in order to make Pascal a one-pass compiler and required nested procedures like PL-M. I finally hit my stride when I got to use C. I got my first copy of DeSmet C for my clone PC using the NEC V20 with dual-5 1/2″ floppies. That was so much better than the two-pass paper tape assembler I had to use at work a few years before to write assembly code for a MOSTEK 3870 uC or the GE TimeShare with Teletype for writing assembly code for the 4-bit TMS-1000. When I started using C, I never looked back at Forth or Basic. Try writing a big program in Forth using a team of programmers! And the Basic I used predated Bill’s silly “upgrades” creating Visual Basic.

        It’s a lot more fun now, and way less painful.

        1. I am quite surprised about your answer. I had assumed you had done due diligence and checked the Professional Forth Products, see at MPE for example http://www.mpeforth.com/: copied from there: MPE consultancy services stem from both hardware and software, and keep us in tune with the requirements of users, so helping us to improve our products. Projects range from small embedded systems to supporting a Windows project with over 1,100,000 lines of Forth source code. We use our software products every day.
          And at http://www.mpeforth.com/news-gossip-and-rumour/ a few applications that can be talked about- where presumably the NDA in place allows for this.
          Similar information you can find at the Forth INC. website.
          If you want to concentrate on the people who like to use Forth or like to write Forth compilers this is a second group.
          A Third group is the Free Spirit Programmers Group. They use whatever language is needed for their consultancy work – a mix of languages as needed for the project with a very simple target: Get the job done quickly using the toolbox best known and probably best suited.
          And this stuff about which language is best or suits you best is just unusual and self centered – which is fine: it is like saying URDU is better than English. It probably is, if you just know this language well – then English is just a sequence of ASCII letters as any other language – or add Chinese and Japanese as other non-urdu examples.
          For Mixed language Programming – using Forth bridges this gap and links different programming languages. It is probably for complex tasks – but what would be the alternative solution then?
          See http://www.mpeforth.com/news-gossip-and-rumour/mpe-in-the-press/
          Do not get me wrong – there are many communities for different reasons – but if you comment it should be visible which group you are addressing.

          1. Sorry. my intent was not to offend anyone, but simply to state what was my obviously ancient introduction and use of Forth. I’m sure that there has been progress in Forth as there has been with EVERYTHING, in both HW and SW realms. If one is conversant in any language, that should be their language of choice. I’m not a big believer in exploring new languages simply as a replacement for what already works well — always use what is best for YOU (unless a specific language as dictated by a job, and then one might explore alternative employment). Exploring new languages can open the mind to new ways of thinking and prod creativity, which is always beneficial. It’s like the Haiku — one doesn’t need to be proficient in Japanese to enjoy and emulate that construction in English or in any other language one has proficiency. And nothing is perfect. Nothing.

          2. Thank you very much for the clarification. There is too much negativity around.
            Just 2 hours ago, actually, I received the first kit for a VEEEEEEEEERY small Forth. Compiling from PC into an ATTINY 2313. This chip just has 20 DIL Pins, 2k Flash and not much RAM and EEPROM. Details – unfortunately in German only for now – but the Forth Vocabulary is understandable anyway. See https://wiki.forth-ev.de/doku.php/attiny – the board and the documentation.
            And yes, I have MSP and STARM boards – but this here is different.
            And hopefully, Ken Boak’s SIMPL will be running on the same chip as well soon;
            This activity is Small Systems and Forth.
            It might actually be possible to modify the compiler and then compile into the ATTINY13a – 8 pins, 1k Flash – very limited, but this would be programmable directly via the Internet as done in the
            Sparrow system see https://www.amazon.co.uk/Cheepit-Sparrow-Programming-Mobile-Phone-ebook/dp/B074G54KRR/ref=asap_bc?ie=UTF8
            or some free documentation under Dokumentation at http://www.ak-modul-bus.de/cgi-bin/iboshop.cgi?showd20!0,694364494580736,Sparrow

  5. It always freaks me out when Hackaday shows people doing similar things that I’m doing. I started writing my own FORTH in ARM assembler a few days ago in UAL so arm32 and thumb. With the intention for multi-core support, so definitely not STM32. But similar enough that my eyebrows rose when I went through my RSS feed of Hackaday.

    1. Imagine that there are 100 fringe languages, and 1% of Hackaday readers playing with one or two of them. *Statistics, birthday paradox, something something* Nope, not strange at all!

      Seriously, I’ve been using Mecrisp-Stellaris Forth for most of my hobby stuff for the last year and enjoying it very much. Coupled with the Maple clone boards (that the STM32 Arduino community calls the “blue pill” boards b/c there were also red ones for a while), it’s a fantastic environment for iterative development. Once you get your head around it — but that’s the other half of the fun.

  6. I believe more and more, that people work in the language that fits their state machine called brain. So some like Forth – other detest it. This whole thing about RPN is a myth – you do it every day at your desk: work coming in from the left, either processed directly, or put onto a stack. Want to memorize and use later: a Variable. Boss calls: return address onto the return stack. Return and continue. Finished work going out on the right hand side. Try it yourself. from the distance. A Start With Forth as eBook https://www.amazon.co.uk/Start-Forth-2017-Bits-Bites-ebook/dp/B073NMX1XP/ref=sr_1_13?s=books&ie=UTF8&qid=1499278235&sr=1-13 and most of it for free at https://wiki.forth-ev.de/doku.php/en:projects:a-start-with-forth:start0 . Switch on your virtual LED or try to understand how Nick’s Snake game works in easyFORTH – mostly no hardware required as was the target..

    1. Have been using 4th for over 35 years, (astronomy instruments, diverse electronics dev. for labs and industry controls , and later in cnc machines ) before Forth the only choice for programming fast routines for mP was to use Assembler. So for somebody who worked in Assembler and specially Macro-assembler Forth was a real joy and fantastic playground ! People programming in C or Pascal needed 2 or 3 times more time in compilation and testing than us, they were passengers in a jet , when a Forth programmer was sitting in the cockpit with acces to the yoke pedals and all the instruments , with access to all registers and peripherals in real time and without intermediates !
      Forth for a hardware engineer is like the film “Tron”, a gate to the inner of the machine were you could see any
      bit and byte coming flying arround and win a fight and tame the beast. :) What some folks fear from Forth is because they were never faced to real time applications , they were never confronted with machine code, they did never experimentes with direct video access, with high sampling rates, or with small microprocessors with limited resources. If they were they never could complain about this brilliant tool.

      1. Peter, this is a brilliant piece of text. It is so good, that I would like to ask for your permission, if I could use it as part of the introduction to A Start With Forth Part 2 ( part one you can see at https://www.amazon.co.uk/Start-Forth-2017-Bits-Bites-ebook/dp/B073NMX1XP/ref=sr_1_13?s=books&ie=UTF8&qid=1499278235&sr=1-13) which will be mostly about hardware, processors ARM, MSP430 … and the software around them. The text would be very slightly edited, and I would prefer to have your full name for the quote, but as is is fine as well if you prefer, or we can email via epldfpga@aol.com.

        1. Dear Edgard, thanks for your compliments Of course you have my permission ! I have seen some typing errors, so please correct them before. I send you by email, this text again. Hope with that to transmit our enthusiasm to more people that want to learn Forth.

Leave a Reply to OstracusCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.