Diving deep into STM32F103's Mar 2016

Mecrisp Forth 2.2.2 has been flashed onto a new series of boards here at JeeLabs, all with an STM32F103 µC, but of different sizes and with different features on-board.

Haoyu Core Board One

Well, it’s called a HY-STM32F1xxCore144 Core/Dev Board, but “Core Board One” sounds nicer:

This board has a µC with a huge number of pins in a 144-TQFP package, most of which are brought out on 0.1” headers (two dual-row 30-pin, for a total of 120).

Not that all of them can be used freely, but that’s because the board is covered on both sides with some massive memory chips:

That’s a lot of memory, compared to most little µC boards out there.

The reason to use this board, was to learn more about the “FSMC” controller (the same as used in a previous article for fast TFT LCD access). It takes a few dozen lines of Forth code to set up, but once done, all those 8 MB of PSRAM memory become standard RAM in terms of software, all mapped into addresses 0x60000000 to 0x607FFFFF. It’s not quite as fast as the built-in 64 KB SRAM, but pretty close - more than enough for data storage (and only a fraction slower than built-in flash memory for program execution). Also great for DMA-based massive data capture.

Apart from setup (”psram-init”), there is no API: PSRAM simply looks like extra memory.

The second type of memory is NAND flash. It too needs very little code, but behaves differently: more like the TFT LCD access mode, in that you get two addresses in memory to talk to the chip: one to send commands to, the other to read/and write data. NAND flash is accessed in pages, and is very fast to read, but somewhat slower to write - very much like an SD card, in fact.

The API for this NAND flash memory is:

: nand-init ( -- u )  \ init NAND flash access, return chip info: $409500F1
: nand-erase ( page -- f )  \ erase a block of 256 flash pages
: nand-write ( page addr -- f )  \ write one 512-byte flash page
: nand-read ( page addr -- )  \ read one 512-byte flash page

As with built-in flash memory, pages have to be erased before they can be re-written.

NOR flash hasn’t been tried yet. It’s different from NAND flash in that the entire memory also gets mapped into the µC’s address space, like SRAM, and offers fast random read & exec access. Writing and erasing requires special code, which works in pages - so NOR flash is like the middle ground between SRAM / PSRAM on the one hand, and NAND flash / SD cards on the other.


This board from Olimex has several nice features:

That last one was the reason to try this board. Here is a first version of some code to initialise an SD card (in bit-banged SPI mode), and read data off it. And this is a first test to mount a FAT16-formatted card, read its root directory, and access data in one of the files.

Here is a transcript of a quick test, with a 2 GB µSD card and some files:

sdtry #0 1 #55 1 #41 1 #55 1 #41 0 
17 0 23 
17 0 14 
17 0 12 
20004C50   60 02 00 00 40 00 00 00   84 00 00 00 41 2E 00 5F   `...@... ....A.._
20004C60   00 2E 00 54 00 72 00 0F   00 7F 61 00 73 00 68 00   ...T.r.. ..a.s.h.
20004C70   65 00 73 00 00 00 00 00   FF FF FF FF 7E 31 20 20   e.s..... ....~1  
20004C80   20 20 20 20 54 52 41 22   00 C0 89 23 6E 48 6E 48       TRA" ...#nHnH
20004C90   00 00 89 23 6E 48 03 00   00 10 00 00 41 42 43 44   ...#nH.. ....ABCD

LFN: ._.Trashes. #1 64 
     ~1      .TRA at: 3 
     ABCDEFGH.TXT at: 14 
LFN: .Trashes. #1 64 
     TRASHE~1.    at: 2 
LFN: 00. #2 64 
LFN: .Spotlight-V1 #1 0 
     SPOTLI~1.    at: 4  ok.
14 x 20004C5C 
17 0 12 
20004C50   60 02 00 00 40 00 00 00   84 00 00 00 4D 6F 6E 20   `...@... ....Mon 
20004C60   4D 61 72 20 31 34 20 31   31 3A 31 32 3A 31 34 20   Mar 14 1 1:12:14 
20004C70   43 45 54 20 32 30 31 36   0A 00 00 00 00 00 00 00   CET 2016 ........
20004C80   00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00   ........ ........
20004C90   00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00   ........ ........

The “Trashes” and “Spotlight” files are hidden stuff Mac OSX insists on putting on everything it touches. The only non-hidden file in there is “ABCDEFGH.TXT”.

The code figures out where the FAT and root directory blocks are on the µSD card, shows the first 64 bytes of the disk definition block, some filename entries it found (including shreds of “Long FileName” entries, intermixed with the rest), and then reads and dumps some bytes from cluster 14, which corresponds to that 29-byte “ABCDEFGH.TXT” file on the card.

Note that the dump is aligned on a 16-byte boundary, but the read buffer starts at 0x20004C5C.

WaveShare Port103Z

This is one of many development boards available from WaveShare:

It has the same 144-pin STM32F103ZE µC as the Yellow Blue Board above, with 512 KB flash and 64 KB RAM memory. This one was loaded up with Mecrisp Forth mostly because it brings out every single pin, so it’s very easy to try out all the hardware functions available on the STM32F103 “High Density” devices.

There’s a 32,768 Hz crystal on the back of the board, so we can try out the Real-Time Clock (RTC) functionality of this chip. Here is some code for this, and this is the API it exposes:

: +rtc ( -- )  \ restart internal RTC using attached 32,768 Hz crystal
: now! ( u -- )  \ set current time
: now ( -- u )  \ return current time in seconds

There’s no calendar functionality, the built-in hardware simply counts seconds. Since it’s a 32-bit counter, it could easily track “Unix time”, i.e. seconds since Jan 1st, 1970, 0:00 UTC.

If you attach a 3V coin cell between the µC’s “Vbat” and “GND” (and remove a jumper that ties it to Vcc), the internal clock will continue ticking when power to the rest of the µC is off. To regain access, just call “+rtc” after power-up. The counter will then become readable with “now” again.

So many boards…

These are just a few examples of the things you can do with Mecrisp Forth on a large range of ARM boards. They illustrate that the amount of Forth code required to access fairly complex hardware periperhals inside the µC is often susprisingly small. But note also that once such code has been written, the API exposed by those newly-defined “words” can be extremely simple.

The code areas for each of the above boards are all in the Embello repository on GitHub, and are called cbo, oxs, and wpz, respectively. Most of the common code can be found in the flib area.

Weblog © Jean-Claude Wippler. Generated by Hugo.