Skip to content

Commit c09d734

Browse files
initial setup
0 parents  commit c09d734

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+9272
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "ardupilot"]
2+
path = ardupilot
3+
url = https://github.com/ardupilot/ardupilot

AP_Bootloader/AP_Bootloader.cpp

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
This program is free software: you can redistribute it and/or modify
3+
it under the terms of the GNU General Public License as published by
4+
the Free Software Foundation, either version 3 of the License, or
5+
(at your option) any later version.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
/*
16+
ArduPilot bootloader. This implements the same protocol originally
17+
developed for PX4, but builds on top of the ChibiOS HAL
18+
19+
It does not use the full AP_HAL API in order to keep the firmware
20+
size below the maximum of 16kByte required for F4 based
21+
boards. Instead it uses the ChibiOS APIs directly
22+
*/
23+
24+
#include <AP_HAL/AP_HAL.h>
25+
#include "ch.h"
26+
#include "hal.h"
27+
#include "hwdef.h"
28+
#include <AP_HAL_ChibiOS/hwdef/common/usbcfg.h>
29+
#include <AP_HAL_ChibiOS/hwdef/common/stm32_util.h>
30+
#include <AP_HAL_ChibiOS/hwdef/common/watchdog.h>
31+
#include "support.h"
32+
#include "bl_protocol.h"
33+
#include "can.h"
34+
#include <stdio.h>
35+
#if EXTERNAL_PROG_FLASH_MB
36+
#include <AP_FlashIface/AP_FlashIface_JEDEC.h>
37+
#endif
38+
39+
extern "C" {
40+
int main(void);
41+
}
42+
43+
struct boardinfo board_info = {
44+
.board_type = APJ_BOARD_ID,
45+
.board_rev = 0,
46+
.fw_size = (BOARD_FLASH_SIZE - (FLASH_BOOTLOADER_LOAD_KB + FLASH_RESERVE_END_KB + APP_START_OFFSET_KB))*1024,
47+
.extf_size = (EXTERNAL_PROG_FLASH_MB * 1024 * 1024)
48+
};
49+
50+
#ifndef HAL_BOOTLOADER_TIMEOUT
51+
#define HAL_BOOTLOADER_TIMEOUT 5000
52+
#endif
53+
54+
#ifndef HAL_STAY_IN_BOOTLOADER_VALUE
55+
#define HAL_STAY_IN_BOOTLOADER_VALUE 0
56+
#endif
57+
58+
#if EXTERNAL_PROG_FLASH_MB
59+
AP_FlashIface_JEDEC ext_flash;
60+
#endif
61+
62+
int main(void)
63+
{
64+
if (BOARD_FLASH_SIZE > 1024 && check_limit_flash_1M()) {
65+
board_info.fw_size = (1024 - (FLASH_BOOTLOADER_LOAD_KB + APP_START_OFFSET_KB))*1024;
66+
}
67+
68+
bool try_boot = false;
69+
uint32_t timeout = HAL_BOOTLOADER_TIMEOUT;
70+
71+
#ifdef HAL_BOARD_AP_PERIPH_ZUBAXGNSS
72+
// setup remapping register for ZubaxGNSS
73+
uint32_t mapr = AFIO->MAPR;
74+
mapr &= ~AFIO_MAPR_SWJ_CFG;
75+
mapr |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
76+
AFIO->MAPR = mapr | AFIO_MAPR_CAN_REMAP_REMAP2 | AFIO_MAPR_SPI3_REMAP;
77+
#endif
78+
79+
#ifndef NO_FASTBOOT
80+
enum rtc_boot_magic m = check_fast_reboot();
81+
bool was_watchdog = stm32_was_watchdog_reset();
82+
if (was_watchdog) {
83+
try_boot = true;
84+
timeout = 0;
85+
} else if (m == RTC_BOOT_HOLD) {
86+
timeout = 0;
87+
} else if (m == RTC_BOOT_FAST) {
88+
try_boot = true;
89+
timeout = 0;
90+
}
91+
#if HAL_USE_CAN == TRUE || HAL_NUM_CAN_IFACES
92+
else if ((m & 0xFFFFFF00) == RTC_BOOT_CANBL) {
93+
try_boot = false;
94+
timeout = 10000;
95+
can_set_node_id(m & 0xFF);
96+
}
97+
can_check_update();
98+
if (!can_check_firmware()) {
99+
// bad firmware CRC, don't try and boot
100+
timeout = 0;
101+
try_boot = false;
102+
}
103+
#ifndef BOOTLOADER_DEV_LIST
104+
else if (timeout != 0) {
105+
// fast boot for good firmware
106+
try_boot = true;
107+
timeout = 1000;
108+
}
109+
#endif
110+
if (was_watchdog && m != RTC_BOOT_FWOK) {
111+
// we've had a watchdog within 30s of booting main CAN
112+
// firmware. We will stay in bootloader to allow the user to
113+
// load a fixed firmware
114+
stm32_watchdog_clear_reason();
115+
try_boot = false;
116+
timeout = 0;
117+
}
118+
#endif
119+
#if defined(HAL_GPIO_PIN_VBUS) && defined(HAL_ENABLE_VBUS_CHECK)
120+
#if HAL_USE_SERIAL_USB == TRUE
121+
else if (palReadLine(HAL_GPIO_PIN_VBUS) == 0) {
122+
try_boot = true;
123+
timeout = 0;
124+
}
125+
#endif
126+
#endif
127+
128+
// if we fail to boot properly we want to pause in bootloader to give
129+
// a chance to load new app code
130+
set_fast_reboot(RTC_BOOT_OFF);
131+
#endif
132+
133+
#ifdef HAL_GPIO_PIN_STAY_IN_BOOTLOADER
134+
// optional "stay in bootloader" pin
135+
if (palReadLine(HAL_GPIO_PIN_STAY_IN_BOOTLOADER) == HAL_STAY_IN_BOOTLOADER_VALUE) {
136+
try_boot = false;
137+
timeout = 0;
138+
}
139+
#endif
140+
141+
if (try_boot) {
142+
jump_to_app();
143+
}
144+
145+
#if defined(BOOTLOADER_DEV_LIST)
146+
init_uarts();
147+
#endif
148+
#if HAL_USE_CAN == TRUE || HAL_NUM_CAN_IFACES
149+
can_start();
150+
#endif
151+
flash_init();
152+
153+
154+
#if EXTERNAL_PROG_FLASH_MB
155+
while (!ext_flash.init()) {
156+
// keep trying until we get it working
157+
// there's no future without it
158+
chThdSleep(1000);
159+
}
160+
#endif
161+
162+
#if defined(BOOTLOADER_DEV_LIST)
163+
while (true) {
164+
bootloader(timeout);
165+
jump_to_app();
166+
}
167+
#else
168+
// CAN only
169+
while (true) {
170+
uint32_t t0 = AP_HAL::millis();
171+
while (timeout == 0 || AP_HAL::millis() - t0 <= timeout) {
172+
can_update();
173+
chThdSleep(chTimeMS2I(1));
174+
}
175+
jump_to_app();
176+
}
177+
#endif
178+
}
179+
180+

AP_Bootloader/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ArduPilot Bootloader
2+
3+
This is the bootloader used for STM32 boards for ArduPilot. To build
4+
the bootloader do this:
5+
6+
```bash
7+
./waf configure --board BOARDNAME --bootloader
8+
./waf bootloader
9+
```
10+
11+
the bootloader will be in build/BOARDNAME/bin. If you have the
12+
intelhex module installed it will build in both bin format and hex
13+
format. Both are usually uploaded with DFU. The elf file will be in
14+
build/BOARDNAME/AP_Bootloader for loading with gdb.
15+
16+
The --bootloader option tells waf to get the hardware config from
17+
the hwdef-bl.dat file for the board. It will look in
18+
libraries/AP_HAL_CHibiOS/hwdef/BOARDNAME/hwdef-bl.dat
19+
20+
The bootloader protocol is compatible with that used by the PX4
21+
project for boards like the Pixhawk. For compatibility purposes we
22+
maintain a list of board IDs in the board_types.txt file in this
23+
directory.
24+
25+
the board IDs in that file match the APJ_BOARD_ID in the hwdef.dat and
26+
hwdef-bl.dat files
27+
28+
The bootloader can load from USB or UARTs. The list of devices to load
29+
from is given in the SERIAL_ORDER option in hwdef-bl.dat

AP_Bootloader/app_comms.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
application -> bootloader communication structure This is put into
3+
the start of RAM by AP_Periph to facilitate firmware upload with
4+
UAVCAN
5+
*/
6+
7+
#pragma once
8+
9+
#define APP_BOOTLOADER_COMMS_MAGIC 0xc544ad9a
10+
11+
struct app_bootloader_comms {
12+
uint32_t magic;
13+
uint32_t reserved[4];
14+
uint8_t server_node_id;
15+
uint8_t my_node_id;
16+
uint8_t path[201];
17+
};
18+
19+
#ifndef FW_MAJOR
20+
#define FW_MAJOR 0
21+
#define FW_MINOR 0
22+
#endif
23+
24+
/*
25+
the app_descriptor stored in flash in the main firmware and is used
26+
by the bootloader to confirm that the firmware is not corrupt and is
27+
suitable for this board. The build dependent values in this structure
28+
are filled in by set_app_descriptor() in the waf build
29+
*/
30+
struct app_descriptor {
31+
uint8_t sig[8] = { 0x40, 0xa2, 0xe4, 0xf1, 0x64, 0x68, 0x91, 0x06 };
32+
// crc1 is the crc32 from firmware start to start of image_crc1
33+
uint32_t image_crc1 = 0;
34+
// crc2 is the crc32 from the start of version_major to the end of the firmware
35+
uint32_t image_crc2 = 0;
36+
// total size of firmware image in bytes
37+
uint32_t image_size = 0;
38+
uint32_t git_hash = 0;
39+
// software version number
40+
uint8_t version_major = FW_MAJOR;
41+
uint8_t version_minor = FW_MINOR;
42+
// APJ_BOARD_ID (hardware version). This is also used in CAN NodeInfo
43+
// with high byte in HardwareVersion.major and low byte in HardwareVersion.minor
44+
uint16_t board_id = APJ_BOARD_ID;
45+
uint8_t reserved[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
46+
};
47+
48+
#define APP_DESCRIPTOR_TOTAL_LENGTH 36
49+
static_assert(sizeof(app_descriptor) == APP_DESCRIPTOR_TOTAL_LENGTH, "app_descriptor incorrect length");

0 commit comments

Comments
 (0)