Skip to content

Commit c4d7fc7

Browse files
Andrewyxitsarunearunpre-commit-ci-lite[bot]sunghyuneun
authored
MDv6 Ansible Flashing (#3614)
* project vimrc for FZF * filter out .swp files * bootstrap bazel embedded * still working on compilation * wip mdv6 firmware * wip * wip * wip * wip * Add new cross-compiler config * wip move software/motor -> firmware/motor * Updated main from MDV6 spi branch * wip * compiles but doesn't link * both compiles AND links! * add bazel-embedded * wip * wip * perhaps flashing works * wip * wip * add arm toolchain to opt/tbotspython * debug overflow * wip * compiles now * Add compile and linker flags to optimize space usage * Build mdv6 firmware in CI * shouldn't commit this * fix syntax issue in actions file * fix firmware builds * Bazel 8 hail mary * fix alt builds * don't test firmware file * [pre-commit.ci lite] apply automatic fixes * openocd flashing works i think? * [pre-commit.ci lite] apply automatic fixes * modified locations * Replaced with remote * ansible deploy * prototype ansible * cleanup * ci fix * config created * openocd setup * cleanup * reformat * test * testing code * Working flashing script * Got rid of old unnecessary dependency * merged * linking fixed, working state * Fixed Ansible Playbook * Please test this commit * fix paths * more merge * fix merge * [pre-commit.ci lite] apply automatic fixes * Multiplexed Flashing to MD Board * [pre-commit.ci lite] apply automatic fixes * Future UI 2.2 Pinout * [pre-commit.ci lite] apply automatic fixes * Changed python to pre-compiled binary instead of compiling from source * Added OpenOCD install to setup_pi.yml. * Formatting Changes * [pre-commit.ci lite] apply automatic fixes * final push * Added documentation * Fix unintended docs deletion and ansible lint errors * Fix flash_motor_drivers.py missing rich import issue * Format code * Add deploy_motor_firmware to tbots * [pre-commit.ci lite] apply automatic fixes * removed unused pkgs * new cs values * new start stop services * module changed * [pre-commit.ci lite] apply automatic fixes * precommit fixes --------- Co-authored-by: arun <arun.sfc@outlook.com> Co-authored-by: arun <arun@zaber.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: sunghyuneun <gregoryeun@gmail.com> Co-authored-by: Grayson <grayhoang@gmail.com> Co-authored-by: Thunderbots <ubcrobocup@gmail.com>
1 parent 1761a61 commit c4d7fc7

37 files changed

Lines changed: 870 additions & 57 deletions

.github/workflows/main.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ jobs:
9090
cd src
9191
bazel build //software/embedded:thunderloop_main --copt=-O3 --platforms=//toolchains/cc:robot
9292
93+
- name: Motor Firmware build Test
94+
run: |
95+
cd src
96+
bazel build @mdv6_firmware//:mdv6_firmware_main --platforms=//toolchains/cc:motor_board
97+
98+
motor-firmware:
99+
name: Motor Firmware
100+
runs-on: ubuntu-24.04
101+
steps:
102+
- uses: actions/checkout@v4
103+
- uses: ./.github/actions/environment-setup
104+
105+
- name: Build Motor Firmware
106+
run: |
107+
cd src
108+
bazel build @mdv6_firmware//:mdv6_firmware_main --platforms=//toolchains/cc:motor_board
93109
94110
software-tests:
95111
needs: check-and-clear-cache

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repos:
1010
name: fix common trivial code errors
1111
language: system
1212
entry: scripts/lint_and_format.sh
13+
pass_filenames: false
1314

1415
- id: generate_fsm_diagrams
1516
name: generate new FSM diagrams

docs/setup-pi.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ The Raspberry Pi 5 is compatible with many different (primarily Linux-based) ope
1010

1111
### Install OS Using Raspberry Pi Imager
1212

13-
To install the Raspberry Pi OS onto a microSD card, we use the Raspberry Pi Imager program. Install it [here](https://www.raspberrypi.com/software/).
13+
To install the Raspberry Pi OS onto a microSD card, we use the Raspberry Pi Imager program. Install it [here](https://www.raspberrypi.com/software/).
1414

15-
*Note: if using the "Download for Linux" option, you will have to right click on the installed `.AppImage` file and select `Properties` &rarr; `Permissions` &rarr; `Allow Executing File as Program` (this may appear as a checkbox)
15+
*Note 1: If on Linux, download the app image through `Download for Linux (x86_64)`. Using `sudo apt install rpi-imager` installs an older version of the program, which does not properly apply OS customization required in later steps.
16+
17+
*Note 2: if using the "Download for Linux" option, you will have to right click on the installed `.AppImage` file and select `Properties` &rarr; `Permissions` &rarr; `Allow Executing File as Program` (this may appear as a checkbox)
1618

1719
When you open the imager, you should see something like this:
1820

docs/useful-robot-commands.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Table of Contents](#table-of-contents)
88
- [Flashing the robot's compute module](#flashing-the-robots-compute-module)
99
- [Flashing the powerboard](#flashing-the-powerboard)
10+
- [Flashing the Motor Driver Boards from Raspberry Pi](#flashing-the-motor-driver-boards-from-raspberry-pi)
1011
- [Setting up the embedded host](#setting-up-the-embedded-host)
1112
- [Raspberry Pi](#raspberry-pi)
1213
- [Robot Diagnostics](#robot-diagnostics)
@@ -105,6 +106,13 @@ Looking from the back of the robot the reset and boot buttons are on right side
105106
bazel run //software/embedded/ansible:run_ansible --platforms=//toolchains/cc:robot -- --playbook deploy_powerboard.yml --hosts <robot_ip> --ssh_pass <robot_password>
106107
```
107108

109+
## Flashing the Motor Driver Boards from Raspberry Pi
110+
This will flash from our Firmware repository's master branch onto all 4 motor driver boards.
111+
```bash
112+
./tbots.py run run_ansible -- --playbook deploy_motor_firmware.yml --hosts robot@<robot_ip> --ssh_pass <robot_password>
113+
```
114+
115+
108116
## Setting up the embedded host
109117

110118
This section refers to setting up the computer on the robot for the first time. We need to setup dependencies, drivers and necessary configurations.

environment_setup/setup_software.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,8 @@ print_status_msg "Set up ansible-lint"
201201
/opt/tbotspython/bin/ansible-galaxy collection install ansible.posix
202202
print_status_msg "Finished setting up ansible-lint"
203203

204+
print_status_msg "Setting up STM32 cross-compiler"
205+
install_stm32_cross_compiler $g_arch
206+
print_status_msg "Done setting up STM32 cross-compiler"
207+
204208
print_status_msg "Done Software Setup, please reboot for changes to take place"

environment_setup/util.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ install_python_dev_cross_compile_headers() {
125125
rm -rf /tmp/tbots_download_cache/python-3.12.0.tar.xz
126126
}
127127

128+
install_stm32_cross_compiler() {
129+
arch="aarch64"
130+
if is_x86 $1; then
131+
arch="x86_64"
132+
fi
133+
download_link=https://developer.arm.com/-/media/Files/downloads/gnu/14.3.rel1/binrel/arm-gnu-toolchain-14.3.rel1-${arch}-arm-none-eabi.tar.xz
134+
135+
wget -N $download_link -O /tmp/tbots_download_cache/arm-gnu-toolchain.tar.xz
136+
tar -xf /tmp/tbots_download_cache/arm-gnu-toolchain.tar.xz -C /tmp/tbots_download_cache/
137+
sudo mv /tmp/tbots_download_cache/arm-gnu-toolchain-14.3.rel1-${arch}-arm-none-eabi /opt/tbotspython/arm-none-eabi-gcc
138+
rm /tmp/tbots_download_cache/arm-gnu-toolchain.tar.xz
139+
}
140+
128141
install_python_toolchain_headers() {
129142
sudo mkdir -p /opt/tbotspython/py_headers/include/
130143
sudo ln -sfn "$(python3.12-config --includes | awk '{for(i=1;i<=NF;++i) if ($i ~ /^-I/) print substr($i, 3)}' | head -n1)" /opt/tbotspython/py_headers/include/

src/MODULE.bazel

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ http_archive(
194194
url = "https://github.com/saebyn/munkres-cpp/archive/61086fcf5b3a8ad4bda578cdc2d1dca57b548786.tar.gz",
195195
)
196196

197+
# For flashing the motor driver board
198+
http_archive(
199+
name = "bazel_embedded",
200+
sha256 = "91a78efbb0f94479fe9ca2912aced712be9e7028325bada9a73e7d325427b404",
201+
strip_prefix = "bazel-embedded-cf5b240d510313b3383e0cdb550eb9f32be7244e",
202+
url = "https://github.com/bazelembedded/bazel-embedded/archive/cf5b240d510313b3383e0cdb550eb9f32be7244e.zip",
203+
)
204+
205+
openocd = use_extension("//starlark/firmware:open_ocd.bzl", "openocd_extension")
206+
use_repo(openocd, "com_openocd")
207+
197208
http_archive(
198209
name = "LTC4151",
199210
build_file = "@//extlibs:LTC4151.BUILD",
@@ -202,6 +213,16 @@ http_archive(
202213
url = "https://github.com/kerrydwong/LTC4151/archive/729952f10bcdcf359877b6f728565c17a8f17423.tar.gz",
203214
)
204215

216+
git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
217+
218+
git_repository(
219+
name = "mdv6_firmware",
220+
build_file = "@//extlibs:mdv6_firmware.bzl",
221+
# Pinning to the exact commit guarantees Bazel will rebuild when this commit SHA is updated
222+
commit = "97e06b9ce8db1940a033b536cd55b4059943d601", # latest commit on master branch
223+
remote = "https://github.com/UBC-Thunderbots/MDv6_Firmware.git",
224+
)
225+
205226
http_archive(
206227
name = "trinamic",
207228
build_file = "@//extlibs:trinamic.BUILD",
@@ -232,6 +253,7 @@ register_toolchains(
232253
"//toolchains/cc:cc_toolchain_for_k8_aarch64_linux",
233254
"//toolchains/cc:cc_toolchain_for_k8",
234255
"//toolchains/cc:cc_toolchain_for_aarch64",
256+
"//toolchains/cc:cc_toolchain_for_stm32",
235257
)
236258

237259
##############################################
@@ -264,6 +286,12 @@ new_local_repository(
264286
path = "/opt/tbotspython/aarch64-tbots-linux-gnu/",
265287
)
266288

289+
new_local_repository(
290+
name = "motor_board_gcc",
291+
build_file = "@//extlibs:motor_board_gcc.BUILD",
292+
path = "/opt/tbotspython/arm-none-eabi-gcc/",
293+
)
294+
267295
new_local_repository(
268296
name = "py_cc_toolchain_host",
269297
build_file = "@//extlibs:py_cc_toolchain.BUILD",

src/MODULE.bazel.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extlibs/mdv6_firmware.bzl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
load("@bazel_embedded//tools/openocd:defs.bzl", "openocd_flash")
2+
3+
cc_binary(
4+
name = "mdv6_firmware_main",
5+
# Linker file informs the toolchain how much RAM and flash you have as well as its locations on the chip
6+
additional_linker_inputs = [
7+
"STM32CubeIDE/STM32F031C6TX_FLASH.ld",
8+
],
9+
linkopts = ["-T $(location STM32CubeIDE/STM32F031C6TX_FLASH.ld)"],
10+
target_compatible_with = [
11+
"@platforms//cpu:armv6-m",
12+
"@platforms//os:none",
13+
],
14+
deps = [
15+
":mdv6_firmware",
16+
],
17+
)
18+
19+
openocd_flash(
20+
name = "mdv6_firmware_flash",
21+
device_configs = [
22+
# Part of the STM32F0 family
23+
# https://www.st.com/resource/en/reference_manual/rm0091-stm32f0x1stm32f0x2stm32f0x8-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
24+
"target/stm32f0x.cfg",
25+
],
26+
# WARNING! `flash_offset` attribute is broken in the Bazel rule. The .elf file (mdv6_firmware_main.stripped)
27+
# contains the memory mappings already, so this attribute is necessary.
28+
flash_offset = "",
29+
# .stripped strips debug symbols to reduce the size of the binary.
30+
# see: https://bazel.build/reference/be/c-cpp#cc_binary
31+
image = ":mdv6_firmware_main.stripped",
32+
interface_configs = [
33+
"interface/stlink.cfg", # The ST-Link V2 programmer is used to flash the firmware.
34+
],
35+
)
36+
37+
cc_library(
38+
name = "mdv6_firmware",
39+
srcs = glob([
40+
"Core/Src/**/*.c",
41+
"Src/**/*.c",
42+
"STM32CubeIDE/Application/Startup/*.s",
43+
"STM32CubeIDE/Application/User/*.c",
44+
"*.c",
45+
"Drivers/STM32F0xx_HAL_Driver/Src/*.c",
46+
], exclude = [
47+
"Drivers/STM32F0xx_HAL_Driver/Src/*_template.c",
48+
]) + [
49+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/bus_voltage_sensor.c",
50+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/circle_limitation.c",
51+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/digital_output.c",
52+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/feed_forward_ctrl.c",
53+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/ntc_temperature_sensor.c",
54+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/open_loop.c",
55+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/pid_regulator.c",
56+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/pqd_motor_power_measurement.c",
57+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/r_divider_bus_voltage_sensor.c",
58+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/ramp_ext_mngr.c",
59+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/revup_ctrl.c",
60+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/speed_pos_fdbk.c",
61+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Src/virtual_speed_sensor.c",
62+
],
63+
hdrs = glob([
64+
"Core/Inc/**/*.h",
65+
"*.h",
66+
"Inc/**/*.h",
67+
"Drivers/STM32F0xx_HAL_Driver/Inc/**/*.h",
68+
"Drivers/CMSIS/Device/ST/STM32F0xx/Include/**/*.h",
69+
"Drivers/CMSIS/Include/**/*.h",
70+
"MCSDK_v6.4.1-Full/**/*.h",
71+
]),
72+
includes = [
73+
"Core/Inc",
74+
"Drivers/STM32F0xx_HAL_Driver/Inc",
75+
"Drivers/CMSIS/Device/ST/STM32F0xx/Include",
76+
"Drivers/CMSIS/Include",
77+
"MCSDK_v6.4.1-Full/MotorControl/MCSDK/MCLib/Any/Inc",
78+
"Inc",
79+
],
80+
defines = [
81+
# Our MCU is the STM32F0251: https://www.st.com/resource/en/datasheet/stspin32f0251.pdf
82+
"STM32F031x6",
83+
# Use the HAL and Low-Layer APIs.
84+
"USE_HAL_DRIVER",
85+
"USE_FULL_LL_DRIVER",
86+
"ARM_MATH_CM0",
87+
],
88+
alwayslink = True,
89+
)

src/extlibs/motor_board_gcc.BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
filegroup(
4+
name = "includes",
5+
srcs = glob([
6+
"arm-none-eabi/include",
7+
"lib/gcc/arm-none-eabi/14.3.1/include",
8+
]),
9+
)

0 commit comments

Comments
 (0)