The CJMCU-8223 is a small breakout board featuring an NRF51822 and an LIS3DH accelerometer. It is shown above attached to an SSD1306 Oled display. Programs are loaded on to it using an ST-Link debugger clone.
There are many different ways to program this chip but I decided to develop code for it using the online compiler at mbed.org. This environment allows you write and compile code within your browser. Compiled programs are dropped in to your downloads directory in the form of “hex” files. These downloads can then be written to your target MCU using your preferred debugger. The advantage of this approach is that you don’t have to set up a toolchain on your PC and, you can avail of lots of ready made libraries and example code.
This particular example provides two Gatt services over BLE. An LEDService allows a connected bluetooth device to control the blue LED on the breadboard. An accelerometer service allows the connected device read the accelerometer values (scaled up by a factor of 1000 to avoid floating point printing). The code for this example can be found via this link over at mbed.org.
When you compile this code, the hex file delivered to your PC needs to be uploaded to the NRF51822. This is done as follows:
In one command window (terminal) start openocd as shown here:
Leaving that window running, open a new one and run telnet entering the commands shown below.
Once you have done this, type reset into the telnet session and the board should start running.
You can interact with the board using BLE Scanner on your phone or maybe work with node.js or python to develop a PC based application.
If you need to do some debugging, it is possible to export the program from the mbed environment into a format that is compatible with a number of toolchains (including gcc/gdb). I have tried this and it works pretty well although you may need to disable interrupts on the MCU while doing this as the debugger can get confused if there is bluetooth activity during the debug session.
This example uses a 4th order Butterworth low pass filter that was designed in GNU Octave. The sampling rate was set to 200kHz and the cut-off frequency was set to 20kHz. The filter output at 20kHz is shown below and, as expected, shows an attenuation of 0.7 (approx the square root of 2).
Various attempts were made to optimize the performance of the filter. The execution time was measured by flipping an output bit either side of the filter code. An oscilloscope trace of this output is below.
As can be seen, the execution time is 1.78 microseconds. This is pretty quick given that floating point numbers are being used. I found that my attempts to manually improve the performance made no significant difference compared to what the compiler’s optimizer could do. I also found that gcc’s -O2 optimization setting produced a faster filter than -O3. The filter shuffles data in the input and output delay lines. This may be considered less than optimal but, given that the order of the filter is low, it probably would make little difference to use circular buffers (and manage buffer state etc).
Code can be downloaded here on Github and should be easily compiled on Linux/Windows/Mac
The STM32L476 Discovery board has an LSM303 Accelerometer/Compass IC and an L3GD20 gyroscope attached to the MCU using an SPI bus and some chip select lines. I wanted to experiment with them with a view to putting together a balancing robot. Supporting code for the following was needed for this:
an SPI interface
periodic interrupts to pace data capture
Rather than build a complex Makefile I went with a simple shell script (or batch file if you prefer) with the following commands:
Note: your PATH environment variable must include the directory where arm-none-eabi-gcc is located.
The resulting main.bin file can then be copied to the virtual disk presented by the mbed interface on the STM32L476 discovery board. (The program waits for you to press the centre joystick button before starting).`
Serial communications is carried out over the built-in ST-Link USB-Serial emulator so no additional hardware is needed (9600,n,8,1).
I have begun working the STM32L476 Discovery board taking a “Bare metal” approach. It is a great board with some nice peripherals. Code will be built up over time over here
To compile this code you need the a cross compiler for ARM that works on your system. You don’t need a fancy debugger or complicated software : the board has an mbed interface so you can just copy the program you develop to the board as if it was a removable disk.