STM32F1xx micro-controller C firmware template
Go to file
King Kévin 2d7b8306cd doc: fix doc 2017-08-19 13:33:00 +02:00
fatfs fatfs: enable Long File Name 2017-08-17 16:00:28 +02:00
lib doc: fix doc 2017-08-19 13:33:00 +02:00
libopencm3@ad5ec6af08 add libopencm3 and STM32duino-bootloader submodules dependencies 2016-01-15 15:36:00 +01:00
.gitignore ignore generated documentation 2016-08-14 19:26:46 +02:00
.gitmodules remove STM32duino-bootloader 2016-08-14 19:03:17 +02:00
Doxyfile add changes from spark abacus project 2016-10-23 17:42:27 +02:00
Makefile add Bluetooth <-> Forumslader forwarding 2017-08-16 18:17:15 +02:00
README.md application: add Bluetooth commands to list, remove, and dump log files 2017-08-19 13:13:56 +02:00
application.c doc: fix doc 2017-08-19 13:33:00 +02:00
application.ld add DFU bootloader 2017-04-15 13:51:24 +02:00
bootloader.c add DFU bootloader 2017-04-15 13:51:24 +02:00
bootloader.ld add DFU bootloader 2017-04-15 13:51:24 +02:00
global.c add core board methods 2017-04-03 13:03:29 +02:00
global.h add DMA SPI difinitions (unsused) 2017-06-28 15:41:20 +02:00

README.md

The forumslogger (or Forumslader-logger) is a small man-in-the-middle board for logging Forumslader automatiklader data.

project

summary

The Forumslader Automatiklader is a power manager for bicycles. It used the power generated by a dynamo to charge Li-Ion battery and can also provide 12V (for lights) and 5V (to charge phones over USB). The Automatiklader can also be enhanced with a bluetooth module, allowing a phone to log precise speed (using the dynamo activity), battery charge, and other values using the forumslader Andoird app.

Sometimes you don't have your phone around to log the data though, but you still want the data to be recorded for later evaluation. This project covers this case, always logging the Forumslader data no matter if a phone is present or not.

technology

The phone connects to the Forumslader using Bluetooth. The app uses the Bluetooth Serial Port Profile (SPP) to transfer data (see protocol section for an example of data sent by the forumslader). The Forumslader board uses an off-the-shelf Bluetooth module.

Thus instead of using Bluetooth to get the Forumslader data (like the phone would do), we remove the Forumslader Bluetooth module, connect our logger board directly to the underlying serial port, and our board will have a Bluetooth module so to forward the data again to the phone. Since our board sits between the Forumslader and phone it can log the data all the time.

The logged data is saved onto a micro-SD card for virtually unlimited storage space. Each time the Forumslader wakes up and starts sending data a new file with the current date: forumslogger_YYYY-MM-DD_hh:mm:ss.txt. The internal Real Time Clock (RTC) is used to provide date and time, which is used in the file name of the logged data.

In addition to the Forumslader a GPS module is added. This allows to automatically set the correct time in the RTC (once the GPS has a fix). The GPS data is also logged (in the same file). This also adds timestamps to the Forumslader data (every second the GPS sends the UTC time) and log the actual route cycled. While GPS also provides speed, the Forumslader data based on the dynamo is much more precise (similar applies to the distance).

On top of the Forumslader commands, the logger provides additional commands to list, retrieve, and delete the logged data files (starting with $FLL,, for Forums-Lader-Logger).

  • $FLL,LS;: will list the log files. Example
$FLL,LS,BOL;
$FLL,LS,forumslogger_2017-08-19_04-30-41.txt;
$FLL,LS,forumslogger_2017-08-17_10-11-55.txt;
$FLL,LS,EOL;
  • to remove a file, use $FLL,RM,forumslogger_YYYY-MM-DD_hh:mm:ss.txt;. This will return $FLL,RM,OK; or $FLL,RM,ERROR;
  • to retrieve/dump a log file, use $FLL,CAT,forumslogger_YYYY-MM-DD_hh:mm:ss.txt;. Example:
$FLL,CAT,forumslogger_2017-08-19_02-55-16.txt;
$FLL,CAT,BOF;
$GPTXT,01,01,02,u-blox ag - www.u-blox.com*50
$GPTXT,01,01,02,HW  UBX-G60xx  00040007 FF7FFFFFp*53
$GPTXT,01,01,02,ROM CORE 7.03 (45969) Mar 17 2011 16:18:34*59
$GPTXT,01,01,02,ANTSTATUS=DONTKNOW*33
...
$FLL,CAT,EOF;

bluetooth

The forumslogger uses a common HC-05 Bluetooth module for communication with the user. Accordingly it is necessary to configure this Bluetooth module. For that enter the AT control mode:

  • connect the Bluetooth module to a USB to UART converter
  • press and hold the button on the module
  • plug the USB to UART converter to a computer
  • the module should blink every two seconds, indicating it entered the AT control mode
  • connect to the module using the USB to UART serial port with a baud rate of 38400 (8N1) Alternative war to enter the AT control mode are described here.

Once connect configure the Bluetooth module:

  • enter AT to check if the UART communication is working. The module should respond with OK
  • change default PIN for a tiny bit of better security: AT+PSWD=1337
  • change the normal mode baud rate: AT+UART=115200,0,0 (the Forumslader uses 9600 but there is no good technical reason to keep this slow speed, and the forumslogger uses 115200)
  • set the device name: AT+NAME=forumslogger (different to Forumslader to avoid confusion)

Now connect to Bluetooth module to the forumlogger. When powered the module will rapidly blink (in normal mode). Once paired and connected the module will double blink every 3 seconds.

protocol

The forumslader data is NMEA-0183 formatted (without checksum).

The app sends

$FLT,5;

  • 4: when switching to about tab (probably asking for $FLV)
  • 5: when switching to statistics tab (probably asking for $FL5)
  • 6: reset tour counter
  • 7: reset day counter
  • 9: next parameters sets wheel circumference in mm and pole pairs
  • 10: next parameter sets the altitude offset in .1 m

The Forumslader sends

$FLV,500290515,5.01; provides the versions 50029051 is the firmware of the FL/Forumlader (shown is About in Forumslader App) 5.01 is the firmware of the BT/Bluetooth (shown is About in Forumslader App)

$FLB,850,101652,219,0; provides measurements 850 is the temperature in .1 C (799 is the maximum valid/displayed value) 101652 is the pressure in .01 hP 219 is the altitude in .1 m 0 is the gradient in .1 %

$FLC,0,11360,102,200,2965,79; provides tour data 0 indicates the following data is for the tour 11360 is the climbed altitude in .1 m 102 is the maximum gradient in .1 % 200 is the maximum temperature in .1 C 2965 is the maximum altitude in .1 m 79 is the maximum speed

$FLC,1,8670,69,200,2965,79; provides day data 1 indicates the following data is for the day 8670 is the climbed altitude in .1 m 69 is the maximum gradient in .1 % 200 is the maximum temperature in .1 C 2965 is the maximum altitude in .1 m 79 is the maximum speed

$FLC,2,211888,-138,200,-138,200; provides total, tour, and day data 2 indicates the following data is for the total, tour, and day 211888 is the total climbed altitude in .1 m -138 is the minimum tour gradient in .1 % 200 is the minimum tour temperature in .1 C -138 is the minimum day gradient in .1 % 200 is the minimum day temperature in .1 C

$FLC,3,4846,258,174,896,0; provides energy data 3 indicates the following data is for the energy 4846 is the total FL energy in .1 Wh 258 is the tour FL energy in .1 Wh 174 is the day FL energy in .1 Wh 896 is the raw data FL_SAVEDATABT 0 is ??? (it doesn't show up in the statistics)

$FLC,4,232,226,9,10,0; provides tour, and day data 4 indicates the following data is for the tour, and day 232 is the average day speed 226 is the average tour speed 9 is the average day gradient in .1 % 10 is the average tour gradient in .1 % 0 is ??? (it doesn't show up in the statistics)

$FLC,5,66262,70,718,31,21673785; provides measurements 5 indicates the following data is for measurements 66262 is the FL_STARTCOUNTER 70 is the FL5 Acuu state of charge in % 718 is the Full charge capacity in mAh 31 is the Charge cycle count 21673785 is the Coulomb-counter

$FL5,00e000,1,13,3833,3837,3837,-22,0,301,1,240,1438,7862,16629; provides global information 00e000 is the FL_STATUS in hex (switch to status tab to get meaning of bits) 1 is the FL switching step 13 is the Impluse in Hz 3833 is the Cell1 voltage in mV 3837 is the Cell2 voltage in mV 3837 is the Cell3 voltage in mV -22 is the Accu current in mA 0 is the Consumer current in mA 301 is the FL temperature in F 1 is the FL_PROGCONSUMER (0: Auto, 1: On, 2: Off, 3: Power) 240 is ??? 1438 is the FL_MICROPULSECOUNTER (used to calculate the distance) 7862 is the FL_PULSECOUNTER (used to calculate the distance) 16629 is the FL_TIMECOUNTER in minutes since FL start (used in Total. Tour and day are calculated using the offsets)

$FLP,2250,14,490,7343,15421,7236,15138,377,30; provided the stored values 2250 is the Wheel circumference in mm (FL_WHEELSIZEBT) 14 is the pole pairs (FL_PLOESBT) 490 is the altitude offset in .1 m (FL_ALTITTUDEOFFSET/10) 7343 is the FL_DAYPULSEOFFSET 15421 is the FL_DAYTIMEOFFSET 7236 is the FL_TOURPULSEOFFSET 15138 is the FL_TOURTIMEOFFSET 377 is the Coulomb-coefficient 30 is ???

the speed in km/h is calculated using the following formula: (value/FL_PLOESBT)*FL_WHEELSIZEBT/(2500/9)

board

The firmware is for a STM32 F1 series micro-controller (i.e. STM32F103C8T6). Currently a core board is used for development since it provides a micro-SD card slot. In the end a blue pill will be used so it can fit in the bicycle frame.

Which board is used is defined in the Makefile. This is required to map the user LED and button provided on the board

connections

Connect the peripherals the following way (STM32F10X signal; STM32F10X pin; peripheral pin; peripheral signal; comment):

  • USART1: forumslader (remove the Bluetooth module)
  • USART2: Bluetooth module (i.e. HC-05)
  • USART3: GPS module (i.e. u.blox NEO-6M with small patch antenna)
  • SPI1: micro-SD card slot

All pins are configured using defines in the corresponding source code.

code

dependencies

The source code uses the libopencm3 library. The projects is already a git submodules. To initialize and it you just need to run once: git submodule init and git submodule update.

To read and write files on the FAT formatted micro-SD card the FatFs library is used, The files are already included in the project.

firmware

To compile the firmware run make.

documentation

To generate doxygen documentation run make doc.

flash

There are two firmware images: bootloader and application. The bootloader image allows to flash the application over USB using the DFU protocol. The bootloader is started first and immediately jumps to the application if it is valid and the DFU mode is not forced (i.e. by pressing the user button on the board or requesting a DFU detach in the application). The main application should be implemented in application.c. It is up to the application to advertise USB DFU support (i.e. as does the provided USB CDC ACM example).

The bootloader image will be flashed using SWD (Serial Wire Debug). For that you need an SWD adapter. The Makefile uses a Black Magic Probe (per default), or a ST-Link V2 along OpenOCD software. To flash the booltoader using SWD run make flash_booloader.

Once the bootloader flashed it is possible to flash the application over USB using the DFU protocol by running make flash. To force the bootloader to start the DFU mode press the user button or short a pin, depending on the board. It is also possible to flash the application using SWD by running make flash_application.

debug

SWD also allows to debug the code running on the micro-controller using GDB. To start the debugging session run make debug.

USB

The firmware offers serial communication over USART1 and USB (using the CDC ACM device class).

You can also reset the board by setting the serial width to 5 bits over USB. To reset the board run make reset. This only works if provided USB CDC ACM is running correctly and the micro-controller isn't stuck.