At the end of the[first part][2]of this article I promised to write something about_interfaces_ . I don’t want to write here a complete or even brief lecture about the interfaces. Instead, I’ll show a simple example how to define and use an interface, and then, how to take advantage of ubiquitous_io.Writer_interface. There will also be a few words about_reflection_and_semihosting_ .
Interfaces are a crucial part of Go language. If you want to learn more about them, I suggest to read[Effective Go][3]and[Russ Cox article][4].
### Concurrent Blinky – revisited
When you read the code of previous examples you probably noticed a counterintuitive way to turn the LED on or off. The_Set_method was used to turn the LED off and the_Clear_method was used to turn the LED on. This is due to driving the LEDs in open-drain configuration. What we can do to make the code less confusing? Let’s define the_LED_type with_On_and_Off_methods:
```
type LED struct {
pin gpio.Pin
}
func (led LED) On() {
led.pin.Clear()
}
func (led LED) Off() {
led.pin.Set()
}
```
Now we can simply call`led.On()`and`led.Off()`which no longer raises any doubts.
In all previous examples I tried to use the same open-drain configuration to don’t complicate the code. But in the last example, it would be easier for me to connect the third LED between GND and PA3 pins and configure PA3 in push-pull mode. The next example will use a LED connected this way.
But our new_LED_type doesn’t support the push-pull configuration. In fact, we should call it_OpenDrainLED_and define another_PushPullLED_type:
```
type PushPullLED struct {
pin gpio.Pin
}
func (led PushPullLED) On() {
led.pin.Set()
}
func (led PushPullLED) Off() {
led.pin.Clear()
}
```
Note, that both types have the same methods that work the same. It would be nice if the code that operates on LEDs could use both types, without paying attention to which one it uses at the moment. The_interface type_comes to help:
We’ve defined_LED_interface that has two methods:_On_and_Off_ . The_PushPullLED_and_OpenDrainLED_ types represent two ways of driving LEDs. We also defined two_Make__*LED_functions which act as constructors. Both types implement the_LED_interface, so the values of these types can be assigned to the variables of type_LED_ :
```
led1 = MakeOpenDrainLED(gpio.A.Pin(4))
led2 = MakePushPullLED(gpio.A.Pin(3))
```
In this case the assignability is checked at compile time. After the assignment the_led1_variable contains`OpenDrainLED{gpio.A.Pin(4)}`and a pointer to the method set of the_OpenDrainLED_type. The`led1.On()`call roughly corresponds to the following C code:
```
led1.methods->On(led1.value)
```
As you can see, this is quite inexpensive abstraction if only consider the function call overhead.
But any assigment to an interface causes to include a lot of information about the assigned type. There can be a lot information in case of complex type which consists of many other types:
```
$ egc
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
10356 196 212 10764 2a0c cortexm0.elf
```
If we don’t use[reflection][5]we can save some bytes by avoid to include the names of types and struct fields:
```
$ egc -nf -nt
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
10312 196 212 10720 29e0 cortexm0.elf
```
The resulted binary still contains some necessary information about types and full information about all exported methods (with names). This information is need for checking assignability at runtime, mainly when you assign one value stored in the interface variable to any other variable.
We can also remove type and field names from imported packages by recompiling them all:
```
$ cd $HOME/emgo
$ ./clean.sh
$ cd $HOME/firstemgo
$ egc -nf -nt
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
10272 196 212 10680 29b8 cortexm0.elf
```
Let’s load this program to see does it work as expected. This time we’ll use the[st-flash][6]command:
It seems that the_st-flash_works a bit unreliably with this board (often requires reseting the ST-LINK dongle). Additionally, the current version doesn’t issue the reset command over SWD (uses only NRST signal). The software reset isn’t realiable however it usually works and lack of it introduces inconvenience. For this board-programmer pair the_OpenOCD_works much better.
### UART
UART (Universal Aynchronous Receiver-Transmitter) is still one of the most important peripherals of today’s microcontrollers. Its advantage is unique combination of the following properties:
* relatively high speed,
* only two signal lines (even one in case of half-duplex communication),
* symmetry of roles,
* synchronous in-band signaling about new data (start bit),
* accurate timing inside transmitted word.
This causes that UART, originally intedned to transmit asynchronous messages consisting of 7-9 bit words, is also used to efficiently implement various other phisical protocols such as used by[WS28xx LEDs][7]or[1-wire][8]devices.
However, we will use the UART in its usual role: to printing text messages from our program.
You can find this code slightly complicated but for now there is no simpler UART driver in STM32 HAL (simple polling driver will be probably useful in some cases). The_usart.Driver_is efficient driver that uses DMA and interrupts to ofload the CPU.
STM32 USART peripheral provides traditional UART and its synchronous version. To use it as output we have to connect its Tx signal to the right GPIO pin:
```
tx.Setup(&gpio.Config{Mode: gpio.Alt})
tx.SetAltFunc(gpio.USART1_AF1)
```
The_usart.Driver_is configured in Tx-only mode (rxdma and rxbuf are set to nil):
We use its_WriteString_method to print the famous sentence. Let’s clean everything and compile this program:
```
$ cd $HOME/emgo
$ ./clean.sh
$ cd $HOME/firstemgo
$ egc
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
12728 236 176 13140 3354 cortexm0.elf
```
To see something you need an UART peripheral in your PC.
**Do not use RS232 port or USB to RS232 converter!**
The STM32 family uses 3.3 V logic but RS232 can produce from -15 V to +15 V which will probably demage your MCU. You need USB to UART converter that uses 3.3 V logic. Popular converters are based on FT232 or CP2102 chips.
You also need some terminal emulator program (I prefer[picocom][9]). Flash the new image, run the terminal emulator and press the reset button a few times:
Open On-Chip Debugger 0.10.0+dev-00319-g8f1f912a (2018-03-07-19:20)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
debug_level: 0
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
adapter speed: 950 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0xc1000000 pc: 0x080016f4 msp: 0x20000a20
adapter speed: 4000 kHz
** Programming Started **
auto erase enabled
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000003a msp: 0x20000a20
wrote 13312 bytes from file cortexm0.elf in 1.020185s (12.743 KiB/s)
** Programming Finished **
adapter speed: 950 kHz
$
$ picocom -b 115200 /dev/ttyUSB0
picocom v3.1
port is : /dev/ttyUSB0
flowcontrol : none
baudrate is : 115200
parity is : none
databits are : 8
stopbits are : 1
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
hangup is : no
nolock is : no
send_cmd is : sz -vv
receive_cmd is : rz -vv -E
imap is :
omap is :
emap is : crcrlf,delbs,
logfile is : none
initstring : none
exit_after is : not set
exit is : no
Type [C-a] [C-h] to see available commands
Terminal ready
Hello, World!
Hello, World!
Hello, World!
```
Every press of the reset button produces new “Hello, World!” line. Everything works as expected.
To see bi-directional UART code for this MCU check out[this example][10].
### io.Writer
The_io.Writer_interface is probably the second most commonly used interface type in Go, right after the_error_interface. Its definition looks like this:
```
type Writer interface {
Write(p []byte) (n int, err error)
}
```
_usart.Driver_implements_io.Writer_so we can replace:
```
tts.WriteString("Hello, World!\r\n")
```
with
```
io.WriteString(tts, "Hello, World!\r\n")
```
Additionally you need to add the_io_package to the_import_section.
The declaration of_io.WriteString_function looks as follows:
```
func WriteString(w Writer, s string) (n int, err error)
```
As you can see, the_io.WriteString_allows to write strings using any type that implements_io.Writer_ interface. Internally it check does the underlying type has_WriteString_method and uses it instead of_Write_if available.
Let’s compile the modified program:
```
$ egc
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
15456 320 248 16024 3e98 cortexm0.elf
```
As you can see,_io.WriteString_causes a significant increase in the size of the binary: 15776 - 12964 = 2812 bytes. There isn’t too much space left on the Flash. What caused such a drastic increase in size?
we can print all symbols ordered by its size for both cases. By filtering and analyzing the obtained data (awk, diff) we can find about 80 new symbols. The ten largest are:
```
> 00000062 T stm32$hal$usart$Driver$DisableRx
> 00000072 T stm32$hal$usart$Driver$RxDMAISR
> 00000076 T internal$Type$Implements
> 00000080 T stm32$hal$usart$Driver$EnableRx
> 00000084 t errors$New
> 00000096 R $8$stm32$hal$usart$Driver$$
> 00000100 T stm32$hal$usart$Error$Error
> 00000360 T io$WriteString
> 00000660 T stm32$hal$usart$Driver$Read
```
So, even though we don’t use the_usart.Driver.Read_method it was compiled in, same as_DisableRx_ ,_RxDMAISR_ ,_EnableRx_and other not mentioned above. Unfortunately, if you assign something to the interface, its full method set is required (with all dependences). This isn’t a problem for a large programs that use most of the methods anyway. But for our simple one it’s a huge burden.
We’re already close to the limits of our MCU but let’s try to print some numbers (you need to replace_io_ package with_strconv_in_import_section):
```
func main() {
a := 12
b := -123
tts.WriteString("a = ")
strconv.WriteInt(tts, a, 10, 0, 0)
tts.WriteString("\r\n")
tts.WriteString("b = ")
strconv.WriteInt(tts, b, 10, 0, 0)
tts.WriteString("\r\n")
tts.WriteString("hex(a) = ")
strconv.WriteInt(tts, a, 16, 0, 0)
tts.WriteString("\r\n")
tts.WriteString("hex(b) = ")
strconv.WriteInt(tts, b, 16, 0, 0)
tts.WriteString("\r\n")
}
```
As in the case of_io.WriteString_function, the first argument of the_strconv.WriteInt_is of type_io.Writer_ .
```
$ egc
/usr/local/arm/bin/arm-none-eabi-ld: /home/michal/firstemgo/cortexm0.elf section `.rodata' will not fit in region `Flash'
/usr/local/arm/bin/arm-none-eabi-ld: region `Flash' overflowed by 692 bytes
exit status 1
```
This time we’ve run out of space. Let’s try to slim down the information about types:
```
$ cd $HOME/emgo
$ ./clean.sh
$ cd $HOME/firstemgo
$ egc -nf -nt
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
15876 316 320 16512 4080 cortexm0.elf
```
It was close, but we fit. Let’s load and run this code:
```
a = 12
b = -123
hex(a) = c
hex(b) = -7b
```
The_strconv_package in Emgo is quite different from its archetype in Go. It is intended for direct use to write formatted numbers and in many cases can replace heavy_fmt_package. That’s why the function names start with_Write_instead of_Format_and have additional two parameters. Below is an example of their use:
```
func main() {
b := -123
strconv.WriteInt(tts, b, 10, 0, 0)
tts.WriteString("\r\n")
strconv.WriteInt(tts, b, 10, 6, ' ')
tts.WriteString("\r\n")
strconv.WriteInt(tts, b, 10, 6, '0')
tts.WriteString("\r\n")
strconv.WriteInt(tts, b, 10, 6, '.')
tts.WriteString("\r\n")
strconv.WriteInt(tts, b, 10, -6, ' ')
tts.WriteString("\r\n")
strconv.WriteInt(tts, b, 10, -6, '0')
tts.WriteString("\r\n")
strconv.WriteInt(tts, b, 10, -6, '.')
tts.WriteString("\r\n")
}
```
There is its output:
```
-123
-123
-00123
..-123
-123
-123
-123..
```
### Unix streams and Morse code
Thanks to the fact that most of the functions that write something use_io.Writer_instead of concrete type (eg._FILE_in C) we get a functionality similar to_Unix streams_ . In Unix we can easily combine simple commands to perform larger tasks. For example, we can write text to the file this way:
```
echo "Hello, World!" > file.txt
```
The`>`operator writes the output stream of the preceding command to the file. There is also`|`operator that connects output and input streams of adjacent commands.
Thanks to the streams we can easily convert/filter output of any command. For example, to convert all letters to uppercase we can filter the echo’s output through_tr_command:
```
echo "Hello, World!" | tr a-z A-Z > file.txt
```
To show the analogy between_io.Writer_and Unix streams let’s write our:
Let’s create a simple encoder that encodes the text written to it using Morse coding:
```
type MorseWriter struct {
W io.Writer
}
func (w *MorseWriter) Write(s []byte) (int, error) {
var buf [8]byte
for n, c := range s {
switch {
case c == '\n':
c = ' ' // Replace new lines with spaces.
case 'a' <= c && c <= 'z':
c -= 'a' - 'A' // Convert to upper case.
}
if c < ' ' || 'Z' <c{
continue // c is outside ASCII [' ', 'Z']
}
var symbol morseSymbol
if c == ' ' {
symbol.length = 1
buf[0] = ' '
} else {
symbol = morseSymbols[c-'!']
for i := uint(0); i <uint(symbol.length);i++{
if (symbol.code>>i)&1 != 0 {
buf[i] = '-'
} else {
buf[i] = '.'
}
}
}
buf[symbol.length] = ' '
if _, err := w.W.Write(buf[:symbol.length+1]); err != nil {
return n, err
}
}
return len(s), nil
}
type morseSymbol struct {
code, length byte
}
//emgo:const
var morseSymbols = [...]morseSymbol{
{1<<0|1<<1|1<<2,4},//!---.
{1<<1|1<<4,6},//".-..-.
{}, // #
{1<<3|1<<6,7},//$...-..-
// Some code omitted...
{1<<0|1<<3,4},//X-..-
{1<<0|1<<2|1<<3,4},//Y-.--
{1<<0|1<<1,4},//Z--..
}
```
You can find the full_morseSymbols_array[here][11]. The`//emgo:const`directive ensures that_morseSymbols_ array won’t be copied to the RAM.
Now we can print our sentence in two ways:
```
func main() {
s := "Hello, World!\r\n"
mw := &MorseWriter{tts}
io.WriteString(tts, s)
io.WriteString(mw, s)
}
```
We use the pointer to the_MorseWriter_`&MorseWriter{tts}`instead os simple`MorseWriter{tts}`value beacuse the_MorseWriter_is to big to fit into an interface variable.
Emgo, unlike Go, doesn’t dynamically allocate memory for value stored in interface variable. The interface type has limited size, equal to the size of three pointers (to fit_slice_ ) or two_float64_(to fit_complex128_ ), what is bigger. It can directly store values of all basic types and small structs/arrays but for bigger values you must use pointers.
The_Blinky_is hardware equivalent of_Hello, World!_program. Once we have a Morse encoder we can easly combine both to obtain the_Ultimate Blinky_program:
In the above example I omitted the definition of_MorseWriter_type because it was shown earlier. The full version is available[here][12]. Let’s compile it and run:
Yes, Emgo supports[reflection][13]. The_reflect_package isn’t complete yet but that what is done is enough to implement_fmt.Print_family of functions. Let’s see what can we do on our small MCU.
To reduce memory usage we will use[semihosting][14]as standard output. For convenience, we also write simple_println_function which to some extent mimics_fmt.Println_ .
The_semihosting.OpenFile_function allows to open/create file on the host side. The special path_:tt_ corresponds to host’s standard output.
The_println_function accepts arbitrary number of arguments, each of arbitrary type:
```
func println(args ...interface{})
```
It’s possible because any type implements the empty interface_interface{}_ . The_println_uses[type switch][15]to print strings, integers and booleans:
```
switch v := a.(type) {
case string:
stdout.WriteString(v)
case int:
strconv.WriteInt(stdout, v, 10, 0, 0)
case bool:
strconv.WriteBool(stdout, v, 't', 0, 0)
case stringer:
stdout.WriteString(v.String())
default:
stdout.WriteString("%unknown")
}
```
Additionally it supports any type that implements_stringer_interface, that is, any type that has_String()_ method. In any_case_clause the_v_variable has the right type, same as listed after_case_keyword.
The`reflect.ValueOf(p)`returns_p_in the form that allows to analyze its type and content programmatically. As you can see, we can even dereference pointers using`v.Elem()`and print all struct fields with their names.
Let’s try to compile this code. For now let’s see what will come out if compiled without type and field names:
```
$ egc -nt -nf
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
16028 216 312 16556 40ac cortexm0.elf
```
Only 140 free bytes left on the Flash. Let’s load it using OpenOCD with semihosting enabled:
```
$ openocd -d0 -f interface/stlink.cfg -f target/stm32f0x.cfg -c 'init; program cortexm0.elf; arm semihosting enable; reset run'
Open On-Chip Debugger 0.10.0+dev-00319-g8f1f912a (2018-03-07-19:20)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
debug_level: 0
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
adapter speed: 950 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0xc1000000 pc: 0x08002338 msp: 0x20000a20
adapter speed: 4000 kHz
** Programming Started **
auto erase enabled
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000003a msp: 0x20000a20
wrote 16384 bytes from file cortexm0.elf in 0.700133s (22.853 KiB/s)
** Programming Finished **
semihosting is enabled
adapter speed: 950 kHz
kind(p) = ptr
kind(*p) = struct
type(*p) =
*p = {
X. : -123
X. : true
}
```
If you’ve actually run this code, you noticed that semihosting is slow, especially if you write a byte after byte (buffering helps).
As you can see, there is no type name for`*p`and all struct fields have the same_X._name. Let’s compile this program again, this time without_-nt -nf_options:
```
$ egc
$ arm-none-eabi-size cortexm0.elf
text data bss dec hex filename
16052 216 312 16580 40c4 cortexm0.elf
```
Now the type and field names have been included but only these defined in~~_main.go_ file~~_main_package. The output of our program looks as follows:
```
kind(p) = ptr
kind(*p) = struct
type(*p) = S
*p = {
A : -123
B : true
}
```
Reflection is a crucial part of any easy to use serialization library and serialization~~algorithms~~like[JSON][16]gain in importance in the IOT era.
This is where I finish the second part of this article. I think there is a chance for the third part, more entertaining, where we connect to this board various interesting devices. If this board won’t carry them, we replace it with something a little bigger.