diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml
new file mode 100644
index 0000000..1a638ea
--- /dev/null
+++ b/.github/workflows/compile.yml
@@ -0,0 +1,32 @@
+name: Build binaries
+
+on:
+ - push
+ - workflow_dispatch
+
+jobs:
+ build:
+ runs-on: ubuntu-20.04
+
+ steps:
+ - uses: actions/checkout@v3
+ with:
+ submodules: recursive
+ - name: Setup arm-none-eabi-gcc
+ uses: carlosperate/arm-none-eabi-gcc-action@v1
+ with:
+ release: 11.2-2022.02
+ - name: Build libopencm3
+ run: make lib
+ - name: Make main binary
+ run: make -C src
+ - name: Consolidate binaries
+ run: |
+ mkdir dist
+ cp src/main.elf dist/
+ cp src/main.bin dist/
+ - name: Archive produced binaries
+ uses: actions/upload-artifact@v3
+ with:
+ name: mcv4-binaries
+ path: dist/*
diff --git a/.gitignore b/.gitignore
index d24b6ab..2a65cc5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,20 @@
+*~
*.o
-*.elf
+*.d
*.bin
+*.elf
+*.hex
+*.srec
+*.list
*.map
-depend
+*.stylecheck
+generated.*.ld
+.gdb_history
+.DS_Store
+
+# Project files from idea/netbeans/eclipse
+nbproject/
+.idea/
+.project
+
mcv4.conf
diff --git a/.gitmodules b/.gitmodules
index 29e93ae..c4a0df1 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,3 @@
[submodule "libopencm3"]
path = libopencm3
- url = git://github.com/libopencm3/libopencm3.git
+ url = https://github.com/libopencm3/libopencm3.git
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..153d416
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,165 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc.
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+ 0. Additional Definitions.
+
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 84cfe78..72eb9a8 100644
--- a/Makefile
+++ b/Makefile
@@ -1,67 +1,95 @@
-FW_VER=3
-
-TOOLCHAIN_DIR = ./libopencm3
-PREFIX = arm-none-eabi
-CC = $(PREFIX)-gcc
-LD = $(PREFIX)-gcc
-SIZE = $(PREFIX)-size
-GDB = $(PREFIX)-gdb
-OBJCOPY = $(PREFIX)-objcopy
-OOCD = openocd
-
-LDSCRIPT = stm32-mcv4.ld
-OOCD_BOARD = oocd/mcv4.cfg
-
-CFLAGS += -mcpu=cortex-m3 -mthumb -msoft-float -DSTM32F1 \
- -Wall -Wextra -O0 -std=gnu99 -g -fno-common \
- -I$(TOOLCHAIN_DIR)/include -DFW_VER=$(FW_VER)
-LDFLAGS += -lc -lm -L$(TOOLCHAIN_DIR)/lib/thumb/cortex-m3 -L$(TOOLCHAIN_DIR)/lib \
- -L$(TOOLCHAIN_DIR)/lib/stm32/f1 -lnosys -T$(LDSCRIPT) \
- -nostartfiles -Wl,--gc-sections,-Map=mcv4.map -mcpu=cortex-m3 \
- -mthumb -march=armv7-m -mfix-cortex-m3-ldrd -msoft-float
-
-O_FILES = main.o led.o output.o usart.o analogue.o fw_ver.o
-TEST_O_FILES = test.o led.o output.o
-
-all: mcv4.bin mcv4_test.bin
-
-test: mcv4_test.bin
-
-include depend
-
-mcv4.elf: $(O_FILES) $(LD_SCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f1.a
- $(LD) -o $@ $(O_FILES) $(LDFLAGS) -lopencm3_stm32f1
- $(SIZE) $@
-
-mcv4_test.elf: $(TEST_O_FILES) $(LD_SCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f1.a
- $(LD) -o $@ $(TEST_O_FILES) $(LDFLAGS) -lopencm3_stm32f1
- $(SIZE) $@
-
-%.bin: %.elf
- $(OBJCOPY) -O binary $< $@
-
-depend: *.c
- rm -f depend
- for file in $^; do \
- $(CC) $(CFLAGS) -MM $$file -o - >> $@ ; \
- done ;
-
-.PHONY: all test clean flash
-
-flash: mcv4.elf
- $(OOCD) -f "$(OOCD_BOARD)" \
- -c "init" \
- -c "reset init" \
- -c "stm32f1x mass_erase 0" \
- -c "flash write_image $<" \
- -c "reset" \
- -c "shutdown"
-
-debug: mcv4.elf
- $(OOCD) -f "$(OOCD_BOARD)" \
- -c "init" \
- # -c "reset halt" &
- $(GDB) mcv4.elf
-
-clean:
- -rm -f mcv4.elf depend *.o
+##
+## This file is part of the libopencm3 project.
+##
+## Copyright (C) 2009 Uwe Hermann
+##
+## This library is free software: you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License as published by
+## the Free Software Foundation, either version 3 of the License, or
+## (at your option) any later version.
+##
+## This library is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public License
+## along with this library. If not, see .
+##
+
+PREFIX ?= arm-none-eabi-
+
+TARGETS := src
+
+# Be silent per default, but 'make V=1' will show all compiler calls.
+ifneq ($(V),1)
+Q := @
+# Do not print "Entering directory ...".
+MAKEFLAGS += --no-print-directory
+endif
+
+OPENCM3_DIR ?= $(realpath libopencm3)
+EXAMPLE_RULES = bin
+
+all: build
+
+bin: EXAMPLE_RULES += bin
+hex: EXAMPLE_RULES += hex
+srec: EXAMPLE_RULES += srec
+list: EXAMPLE_RULES += list
+images: EXAMPLE_RULES += images
+
+bin: build
+hex: build
+srec: build
+list: build
+images: build
+
+build: lib examples
+
+lib:
+ $(Q)if [ ! "`ls -A $(OPENCM3_DIR)`" ] ; then \
+ printf "######## ERROR ########\n"; \
+ printf "\tlibopencm3 is not initialized.\n"; \
+ printf "\tPlease run:\n"; \
+ printf "\t$$ git submodule init\n"; \
+ printf "\t$$ git submodule update\n"; \
+ printf "\tbefore running make.\n"; \
+ printf "######## ERROR ########\n"; \
+ exit 1; \
+ fi
+ $(Q)$(MAKE) -C $(OPENCM3_DIR)
+
+EXAMPLE_DIRS:=$(sort $(dir $(wildcard $(addsuffix /Makefile,$(TARGETS)))))
+$(EXAMPLE_DIRS): lib
+ @printf " BUILD $@\n";
+ $(Q)$(MAKE) --directory=$@ OPENCM3_DIR=$(OPENCM3_DIR) $(EXAMPLE_RULES)
+
+examples: $(EXAMPLE_DIRS)
+ $(Q)true
+
+examplesclean: $(EXAMPLE_DIRS:=.clean)
+
+clean: examplesclean styleclean
+ $(Q)$(MAKE) -C libopencm3 clean
+
+stylecheck: $(EXAMPLE_DIRS:=.stylecheck)
+styleclean: $(EXAMPLE_DIRS:=.styleclean)
+
+
+%.clean:
+ $(Q)if [ -d $* ]; then \
+ printf " CLEAN $*\n"; \
+ $(MAKE) -C $* clean OPENCM3_DIR=$(OPENCM3_DIR) || exit $?; \
+ fi;
+
+%.styleclean:
+ $(Q)$(MAKE) -C $* styleclean OPENCM3_DIR=$(OPENCM3_DIR)
+
+%.stylecheck:
+ $(Q)$(MAKE) -C $* stylecheck OPENCM3_DIR=$(OPENCM3_DIR)
+
+
+.PHONY: build lib examples $(EXAMPLE_DIRS) install clean stylecheck styleclean \
+ bin hex srec list images
+
diff --git a/README.md b/README.md
index 10f32b3..6f192ed 100644
--- a/README.md
+++ b/README.md
@@ -1,101 +1,70 @@
Motor Board v4 Firmware
=======================
-Prerequisites
--------------
+## Instructions
-These instructions assume that you are using a version of Linux, but should also work on BSD-derived OSes (including OS X). Windows users may wish to try [Cygwin][cygwin], create a Linux VM (although flashing the board from a VM may require some thought), or consider their life choices.
+Using a posix system, you require `make`, the `arm-none-eabi` toolchain and `git`.
+Before attempting to build anything initialise all the submodules.
+```shell
+$ git submodule update --init --recursive
+```
-* `git` can be found in your distribution's repositories, or [online][git].
-* A working C compiler, such as `gcc` or `clang`, also from your distribution's repositories.
-* `make`, also from your distribution's repositories.
-* The `arm-none-eabi` toolchain. This may be in your distribution's repositories, otherwise download it [from Launchpad][toolchain] and follow the instructions in the README.
-* `stm32flash`, which can be found [on Google Code][stm32flash]. After downloading, build and install it:
+To build the main binary, run:
+```shell
+$ make
+```
+The binary will then be at `src/main.bin`.
+This will also build the library libopencm3 the first time you run it.
- $ make
- $ sudo make install
+This can be flashed to an attached motor board that is in bootloader using:
+```shell
+$ make -C src prog
+```
+To use the `prog` command you need to install stm32flash. This is a cross-platform utility that may be in your operating system's package manager, otherwise it can be downloaded from [their website](https://sourceforge.net/p/stm32flash/wiki/Home/).
-Instructions
-------------
+To enter the bootloader the pushbutton on the board can be pressed with the 12V input connected. Whilst 12V power is present the board will remain in bootloader.
-1. From a shell, `cd` into the directory where you cloned the repository and download the `libopencm3` submodule:
+### Finding the board
- $ git submodule update --init
+With the pyserial library, the serial port can be identified using the `pyserial-ports --verbose` command.
-2. Determine your `arm-none-eabi` toolchain prefix, unless it is in your `$PATH`. If you downloaded it from the link above, it will be the path to the `bin` directory of the extracted folder, followed by `arm-none-eabi`.
-
-3. Build `libopencm3`:
-
- $ cd libopencm3
- $ make PREFIX=
-
-4. Change back to the project directory and build the firmware:
-
- $ cd ..
- $ make PREFIX=
-
-5. Connect the motor board to your machine via USB. The USB Power Light (green, next to the connector) should turn on.
-
-6. Connect the motor board to a power source. The main power light (green, in the middle of the board) should turn on. If using the SR power board, you will have to run a program to turn on the motor rail.
-
-7. Press the firmware button on the side of the motor board with a long thin object.
-
-8. To test connectivity, run the following command, where X is found by running `ls /dev/ttyUSB*`:
-
- $ stm32flash /dev/ttyUSB
-
-9. Load the firmware onto the motor board:
-
- $ stm32flash -w mcv4.bin -v /dev/ttyUSB
-
## Controls
The motor board is largely controlled over the serial interface. There is one physical push button on the board that puts the microprocessor into a mode in which new firmware can be installed.
+### LEDs
+
+There are 4 LEDs controlled by the MCU, 2 blue and 2 red located around the input choke.
+The blue LED is lit when the respective channel is drawing over 5 amps.
+The red LED is lit when the respective H-bridge has reported a fault.
+
## USB interface
Unlike other SR v4 boards, the STM32 on the motor board does not directly communicate over USB. Instead there is an FTDI USB serial interface chip.
The FTDI Chip has vendor ID `0403` and product ID `6001`. It can be further filtered by the USB `product` field.
-Given that the appropriate drivers are available on your computer, it should appear as a standard serial interface. You should open this interface at a baud rate of `1000000bps`.
+Given that the appropriate drivers are available on your computer, it should appear as a standard serial interface. You should open this interface at a baud rate of `115200bps`.
### Serial Commands
+Commands can be sent to the board via the serial or USB serial interface. Each command is its own line and is terminated with a new line.
-8 bit commands should be sent over the serial interface to the board.
-
-The following commands are supported:
-
-| Decimal Value | Unicode| Purpose |
-|---------------|--------|---------------------------------|
-| 0 | `\x00` | Reset the STM32 |
-| 1 | `\x01` | Get the firmware version string |
-| 2 | `\x02` | Set Motor 0 value |
-| 3 | `\x03` | Set Motor 1 value |
-| 4 | `\x04` | Reset into USB bootloader |
-
-
-### Setting Motor Speeds
-
-The value of a motor can be set by sending the appropriate command, and then a second byte as follows:
-
-The argument byte is a standard 8-bit signed number, where a few values are reserved for the special cases of no-op, coast and brake.
-
-| Decimal Value | Purpose |
-|---------------|-----------------|
-| 0 | No-op |
-| 1 | Coast the motor |
-| 2 | Brake the motor |
-
-### Firmware String
-
-When the firmware string is requested from the motor board, you will receive bytes of the following format over the serial interface: `MCV4B:3\n`.
-
-The number after the colon represents the firmware version of the board.
+Action | Description | Command | Parameter Description | Return | Return Parameters
+--- | --- | --- | --- | --- | ---
+Identify | Get the board type and version | *IDN? | - | Student Robotics:MBv4B:\:\ | \ \
+Status | Get board status | *STATUS? | - | \