The art of open hardware portable devices

It’s sort of a press release … for immediate publication (my media friends will get the joke :))

Anyway – I was talking about stm32f030 arm device that was being created. Being busy with creating some libraries (time and display) it took us some time to do this sort of thing (aka letting world know what was created), so here we go.

Ladies and Gentlemen, we are presenting N-Ka.

IMG_4106

As you can see the device sports a TFT display with two buttons at the bottom (if they are not really visible).

The platform is completely open and the scheme will be published in weeks to come. The device includes two clocks, which can be used (you are not obliged to use them :)), but we’d like to use 32.768 kHz clock, to play with time.

Oh and under the display you get access to all the I/O pins, so you can connect the device with outside world and if you don’t need the display you can use some more pins (since few of them are used to communicate with the display).

stm32f030XX and gnu-gcc

A green, glowing SMD (surface-mount device) LE...
A green, glowing SMD (surface-mount device) LED (light-emitting diode) on the Arduino NG board from arduino.cc. The LED is marked with PWR. (Photo credit: Wikipedia)

I’m working with stm32f030xx microcontrollers and obviously needed to program it.

To avoid doing really low level dirty stuff I resorted to using HAL library and tried to do the known Hello world in microcontroller world (blinking LED).

The first thing was to create a library to use. Downloaded the needed files from ST web page. The funny thing is that I needed to hack it a bit since the compilation didn’t went through (got some errors), but at the end the library is created and you can just download it and use it. You need to uncompress the files to a directory next to one with source code (or change paths in Makefile).

After that you just use the regular combination of Makefile and source code file.

# put your *.o targets here, make should handle the rest!

SRCS = main.c

# all the files will be generated with this name (main.elf, main.bin, main.hex, etc)

PROJ_NAME=main

# that's it, no need to change anything below this line!

###################################################

CC=arm-none-eabi-gcc
OBJCOPY=arm-none-eabi-objcopy

CFLAGS  = -g -O2 -Wall -T../libs/STM32F031C6_FLASH.ld --specs=nosys.specs
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m0 -mthumb-interwork
CFLAGS += -mfloat-abi=soft

###################################################

vpath %.c src

ROOT=$(shell pwd)

CFLAGS += -Iinc -I../libs -I../libs/inc
CFLAGS += -I../libs/inc/core -I../libs/inc/peripherals

SRCS += ../libs/startup_stm32f030xc.s  # add startup file to build
SRCS += ../libs/system_stm32f0xx.c

OBJS = $(SRCS:.c=.o)

###################################################

.PHONY: proj    

all: proj	

proj: 	$(PROJ_NAME).elf

$(PROJ_NAME).elf: $(SRCS)
	$(CC) $(CFLAGS) $^ -o $@ -L../libs -lstm32f0
	$(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
	$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin

clean:
	rm -f $(PROJ_NAME).elf
	rm -f $(PROJ_NAME).hex
	rm -f $(PROJ_NAME).bin

And the source code:

/**
 *  Blinky project
 *
 *  @author   Tilen Majerle / modified for stm32f0xx by Bostjan Jerko
 *  @email    tilen@majerle.eu / bostjan@japina.eu
 *  @version  v1.0
 *  @gcc    v4.7 20013qr3 / I don't care :)
 *  @ide    CooCox CoIDE v1.7.6 / regular command line
 */
#include "stm32f0xx.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"

int main(void) {
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  GPIO_InitTypeDef GPIO_InitDef;

  GPIO_InitDef.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
  GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitDef.GPIO_Speed = GPIO_Speed_10MHz;
  //Initialize pins
  GPIO_Init(GPIOA, &GPIO_InitDef);

  volatile int i;
    while (1) {
        // Toggle leds
    	GPIO_SetBits(GPIOA, GPIO_Pin_5);
      	// Waste some time
      	for (i = 0; i < 100000; i++);
		GPIO_ResetBits(GPIOA, GPIO_Pin_5);      	
      	// Waste some time
      	for (i = 0; i < 100000; i++);
    }
}

The LED should be connected to PA5, as you can see from the code (GPIOA and pin 5).

nrf51 blinking LED (“self programming”)

A green, glowing SMD (surface-mount device) LE...
A green, glowing SMD (surface-mount device) LED (light-emitting diode) on the Arduino NG board from arduino.cc. The LED is marked with PWR. (Photo credit: Wikipedia)

It took me some time to start properly playing with nrf51822.

Managed to create my own board and get reading the button and lighting LED without a problem, but getting BLE working was whole new issue, so I decided to first work on my nrf51 programming skills, to really understand inner workings of BLE and then try to switch it on on my board.

This is the first attempt in creating my version of Blinky demo on dongle (and I will obviously try it on my board too).

#include "nrf_delay.h"
#include "boards.h"

#define LED_OUT                       23

int main(void)
{
    // Configure LED-pins as outputs.
    nrf_gpio_cfg_output(LED_OUT);

    // Toggle LEDs.
    while (true)
    {
        nrf_gpio_pin_clear(LED_OUT);
        nrf_delay_ms(500);
        nrf_gpio_pin_set(LED_OUT);
        nrf_delay_ms(500);
    }
}

is the code. Following is the Makefile needed to compile it. You will need to set path to SDK. Variable is named SDK_PATH 🙂

PROJECT_NAME := hello


SDK_PATH := ../../nRF51_SDK_9
export OUTPUT_FILENAME
MAKEFILE_NAME := $(MAKEFILE_LIST)
MAKEFILE_DIR := $(dir $(MAKEFILE_NAME) ) 

TEMPLATE_PATH = $(SDK_PATH)/components/toolchain/gcc
ifeq ($(OS),Windows_NT)
include $(TEMPLATE_PATH)/Makefile.windows
else
include $(TEMPLATE_PATH)/Makefile.posix
endif

MK := mkdir
RM := rm -rf

#echo suspend
ifeq ("$(VERBOSE)","1")
NO_ECHO := 
else
NO_ECHO := @
endif

# Toolchain commands
CC           	:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-gcc"
AS       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-as"
AR       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ar" -r
LD       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ld"
NM       		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-nm"
OBJDUMP  		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objdump"
OBJCOPY  		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objcopy"
SIZE    		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size"

#function for removing duplicates in a list
remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))

#source common to all targets
C_SOURCE_FILES += \
$(SDK_PATH)/components/toolchain/system_nrf51.c \
main.c \
$(SDK_PATH)/components/drivers_nrf/hal/nrf_delay.c \

#assembly files common to all targets
ASM_SOURCE_FILES  = $(SDK_PATH)/components/toolchain/gcc/gcc_startup_nrf51.s

#includes common to all targets
INC_PATHS  = -I$(SDK_PATH)/components/toolchain/gcc
INC_PATHS += -I$(SDK_PATH)/components/toolchain
INC_PATHS += -I$(SDK_PATH)/examples/bsp
INC_PATHS += -I$(SDK_PATH)/components/device
INC_PATHS += -I$(SDK_PATH)/components/drivers_nrf/hal

OBJECT_DIRECTORY = _build
LISTING_DIRECTORY = $(OBJECT_DIRECTORY)
OUTPUT_BINARY_DIRECTORY = $(OBJECT_DIRECTORY)

# Sorting removes duplicates
BUILD_DIRECTORIES := $(sort $(OBJECT_DIRECTORY) $(OUTPUT_BINARY_DIRECTORY) $(LISTING_DIRECTORY) )

#flags common to all targets
CFLAGS  = -DNRF51
CFLAGS += -DBOARD_PCA10031
CFLAGS += -DBSP_DEFINES_ONLY
CFLAGS += -mcpu=cortex-m0
CFLAGS += -mthumb -mabi=aapcs --std=gnu99
CFLAGS += -Wall -Werror -O3
CFLAGS += -mfloat-abi=soft
# keep every function in separate section. This will allow linker to dump unused functions
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
CFLAGS += -fno-builtin --short-enums

# keep every function in separate section. This will allow linker to dump unused functions
LDFLAGS += -Xlinker -Map=$(LISTING_DIRECTORY)/$(OUTPUT_FILENAME).map
LDFLAGS += -mthumb -mabi=aapcs -L $(TEMPLATE_PATH) -T$(LINKER_SCRIPT)
LDFLAGS += -mcpu=cortex-m0
# let linker to dump unused sections
LDFLAGS += -Wl,--gc-sections
# use newlib in nano version
LDFLAGS += --specs=nano.specs -lc -lnosys

# Assembler flags
ASMFLAGS += -x assembler-with-cpp
ASMFLAGS += -DNRF51
ASMFLAGS += -DBOARD_PCA10031
ASMFLAGS += -DBSP_DEFINES_ONLY
#default target - first one defined
default: clean hello

#building all targets
all: clean
	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e cleanobj
	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e hello

#target for printing all targets
help:
	@echo following targets are available:
	@echo 	hello


C_SOURCE_FILE_NAMES = $(notdir $(C_SOURCE_FILES))
C_PATHS = $(call remduplicates, $(dir $(C_SOURCE_FILES) ) )
C_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(C_SOURCE_FILE_NAMES:.c=.o) )

ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SOURCE_FILES))
ASM_PATHS = $(call remduplicates, $(dir $(ASM_SOURCE_FILES) ))
ASM_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(ASM_SOURCE_FILE_NAMES:.s=.o) )

vpath %.c $(C_PATHS)
vpath %.s $(ASM_PATHS)

OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)

hello: OUTPUT_FILENAME := hello
hello: LINKER_SCRIPT=blinky_gcc_nrf51.ld
hello: $(BUILD_DIRECTORIES) $(OBJECTS)
	@echo Linking target: $(OUTPUT_FILENAME).out
	$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e finalize

## Create build directories
$(BUILD_DIRECTORIES):
	echo $(MAKEFILE_NAME)
	$(MK) $@

# Create objects from C SRC files
$(OBJECT_DIRECTORY)/%.o: %.c
	@echo Compiling file: $(notdir $<)
	$(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $<

# Assemble files
$(OBJECT_DIRECTORY)/%.o: %.s
	@echo Compiling file: $(notdir $<)
	$(NO_ECHO)$(CC) $(ASMFLAGS) $(INC_PATHS) -c -o $@ $<


# Link
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out: $(BUILD_DIRECTORIES) $(OBJECTS)
	@echo Linking target: $(OUTPUT_FILENAME).out
	$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out


## Create binary .bin file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	@echo Preparing: $(OUTPUT_FILENAME).bin
	$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin

## Create binary .hex file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	@echo Preparing: $(OUTPUT_FILENAME).hex
	$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex

finalize: genbin genhex echosize

genbin:
	@echo Preparing: $(OUTPUT_FILENAME).bin
	$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin

## Create binary .hex file from the .out file
genhex: 
	@echo Preparing: $(OUTPUT_FILENAME).hex
	$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex

echosize:
	-@echo ""
	$(NO_ECHO)$(SIZE) $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
	-@echo ""

clean:
	$(RM) $(BUILD_DIRECTORIES)

cleanobj:
	$(RM) $(BUILD_DIRECTORIES)/*.o

Using dongle you already get SEGGER (possibility to program the micro controller) and you just use whatever tool you like otherwise I’m not sure what you can use other than SEGGER programmer.

I have tried and failed – at the end decided to just buy a programmer.

 

APF28DEV and SD boot (Armadeus)

 

You need to compile the kernel and root the same way as I wrote previously. This must be done on the Linux, so VirtualBox, Parallels, VMWare or whatever you like to use.

 

After that you can use any OS you like, but I will talk about Mac OSX, because … well, I use it.

 

Let’s roll (use Terminal):

 

  1. You need to install e2fsprogs. (use Homebrew or whatever you like)
  2. Install fuse-ext2. 
  3. Do mkdir /Volumes/disk.
  4. In Disk Utility check disk number (of SD) and create one partition.
  5. Unmount the card
  6. Use sudo mkfs.ext2 /dev/disk? (instead of ? use disk number) – in Homebrew you need to use whole path to it (/usr/local/Cellar/e2fsprogs/…)
  7. sudo fuse-ext2 /dev/disk? /Volumes/disk -o rw+ (instead of ? use disk number)
  8. sudo tar xvf rootfs.tar -C /Volumes/disk
  9. cp apf28-linux.bin /Volumes/disk/boot
  10. sudo umount /Volumes/disk

 

You can use the card to boot from it on apf28dev.

 

According to http://www.armadeus.com/wiki/index.php?title=MultiMediaCard

 

You now have to go to boot menu and use:

 

run mmcboot

 

or to make it boot forever

 

setenv bootcmd run mmcboot
saveenv

 

ARM USB OCD programming

ARM-USB-OCD-01

So I got myself a device to program and debug ARM micro controllers by using JTAG. It’s an Olimex ARM-USB-OCD.

Tried to use OpenOCD on Mac OSX to program LPC2103,  but had troubles getting it recognised. After some googling and testing I finally managed to get it recognised. Here are the findings:

  • Compile OpenOCD with just regular ./configure (you can obviously add –prefix or whatever you want :)) and install it
  • The settings for interface in OpenOCD are using deprecated interface ft2232 so you need to change it a bit.
#
# Olimex ARM-USB-OCD-H
#
# http://www.olimex.com/dev/arm-usb-ocd.html
#

interface ftdi
ftdi_device_desc "Olimex OpenOCD JTAG"
ftdi_vid_pid 0x15ba 0x0003

You need to check data for device description (should be the same as in the device) and PID and VID number. To get that info just click on the apple sign (on the top left side of the screen), select About This Mac and press button System Report. Under USB you will find the device and copy the name (at the top) and Product ID and Vendor ID. Copy that to the interface file.

  • Choose the target you want to program (lpc2103.cfg in my example)

The command is:

 openocd -f olimex-arm-usb-ocd-h.cfg -f lpc2103.cfg

With proper paths, of course.

nrf51 debugging

USB Port Connection
USB Port Connection (Photo credit: Wikipedia)

 

I was playing with nrf51 dongle some more and I stumbled on some troubles, so debugging would be handy. After some playing around I found out you need to add following to Makefile:

CFLAGS += -D__HEAP_SIZE=0
CFLAGS += -DBSP_UART_SUPPORT
ASMFLAGS += -D__HEAP_SIZE=0
ASMFLAGS += -DBLE_STACK_SUPPORT_REQD
ASMFLAGS += -DBSP_UART_SUPPORT

After that you just use whatever you need for debugging in the code (e.g. printf).

The result can be read on serial terminal over the USB port (e.g. minicom). You need to set the speed to 38400).