Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemensElflein committed Oct 29, 2024
1 parent 1a7c85e commit a784e2a
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 60 deletions.
10 changes: 0 additions & 10 deletions bootloader/.idea/runConfigurations/Unnamed.xml

This file was deleted.

5 changes: 3 additions & 2 deletions bootloader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

# Compiler options here.
ifeq ($(USE_OPT),)
USE_OPT = -O1 -ggdb -fomit-frame-pointer -falign-functions=16
# USE_OPT = -O0 -ggdb -fomit-frame-pointer -falign-functions=16
USE_OPT = -O3 -fomit-frame-pointer -falign-functions=16
endif

# C specific options here (added to USE_OPT).
Expand Down Expand Up @@ -66,7 +67,7 @@ endif

# Enables the use of FPU (no, softfp, hard).
ifeq ($(USE_FPU),)
USE_FPU = no
USE_FPU = hard
endif

# FPU-related options.
Expand Down
2 changes: 1 addition & 1 deletion bootloader/boards/XCORE/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@
#define VAL_GPIOF_PUPDR (PIN_PUPDR_PULLUP(GPIOF_HEARTBEAT_LED_RED) |\
PIN_PUPDR_PULLUP(GPIOF_HEARTBEAT_LED_GREEN) |\
PIN_PUPDR_PULLUP(GPIOF_HEARTBEAT_LED_BLUE) |\
PIN_PUPDR_PULLUP(GPIOF_RESET_PHY) | \
PIN_PUPDR_FLOATING(GPIOF_RESET_PHY) | \
PIN_PUPDR_PULLUP(GPIOF_PIN4) | \
PIN_PUPDR_PULLUP(GPIOF_PIN5) | \
PIN_PUPDR_FLOATING(GPIOF_OCTOSPI_IO3) |\
Expand Down
28 changes: 22 additions & 6 deletions bootloader/boards/XCORE/board_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define BOARD_PHY_ADDRESS 31
#define BOARD_PHY_RESET() \
do { \
uint8_t tries = 0; \
uint32_t i = 0; \
while (1) { \
mii_write(&ETHD1, 0x1f, 0xFA00); \
Expand All @@ -15,6 +16,24 @@
while (i-- > 0) { \
asm("nop"); \
}; \
tries++; \
if (tries == 100) { \
mii_write(&ETHD1, 0x1F, 0xFA00); \
mii_write(&ETHD1, 0x01, 0xFFFF); \
i = STM32_SYS_CK / 100; \
while (i-- > 0) { \
asm("nop"); \
}; \
} \
if (tries > 200) { \
palClearLine(LINE_RESET_PHY); \
i = STM32_SYS_CK / 100; \
while (i-- > 0) { \
asm("nop"); \
}; \
palSetLine(LINE_RESET_PHY); \
tries = 0; \
} \
} \
i = STM32_SYS_CK / 100; \
while (i-- > 0) { \
Expand All @@ -27,17 +46,14 @@
mii_write(&ETHD1, 0x00, 0x2A05); \
} while (0)

#define BOARD_HAS_RGB_STATUS 0
#define BOARD_HAS_RGB_HEARTBEAT 0
#define BOARD_HAS_RGB_STATUS 1
#define BOARD_HAS_RGB_HEARTBEAT 1
#define BOARD_STATUS_LED_INVERTED
#define BOARD_HEARTBEAT_LED_INVERTED

#define BOARD_HAS_EEPROM 1
#define EEPROM_DEVICE_ADDRESS 0b1010011

#define BOARD_VERSION_MAJOR 1
#define BOARD_VERSION_MINOR 1
#define BOARD_VERSION_PATCH 0
#define CARRIER_EEPROM_DEVICE_ADDRESS 0b1010000

// Define the fallback IP settings for this board (if DHCP fails)
// 10.0.0.254
Expand Down
2 changes: 1 addition & 1 deletion bootloader/boards/XCORE/cfg/board.chcfg
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@
Type="OpenDrain"
Level="High"
Speed="Minimum"
Resistor="PullUp"
Resistor="Floating"
Mode="Output"
Alternate="0" />
<pin4
Expand Down
12 changes: 7 additions & 5 deletions bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void link_up(struct netif *netif) {
}
}

bool jump_to_user_program(void) {
void jump_to_user_program(void) {
RTC->BKP0R = 0xB0043D;
#ifdef BOARD_HAS_EEPROM

Expand All @@ -31,16 +31,16 @@ bool jump_to_user_program(void) {
if (!ID_EEPROM_GetBootloaderInfo(&info)) {
// If we don't have a valid image, stay in bootloader and allow bootloading
// again
return false;
return;
}

if (info.image_present != 1) {
return false;
return;
}

// Check, if image size is valid
if (info.image_size > PROGRAM_FLASH_SIZE_BYTES) {
return false;
return;
}

// If we have a valid info in our EEPROM, verify the hash before booting
Expand All @@ -56,7 +56,7 @@ bool jump_to_user_program(void) {
(BOOT_ADDRESS >> 16) << SYSCFG_UR2_BOOT_ADD0_Pos);
NVIC_SystemReset();
} else {
return false;
return;
}

#else
Expand Down Expand Up @@ -97,6 +97,7 @@ int main(void) {
InitHeartbeat();
InitStatusLed();

/*
// True, when the board was started via power up. If we have a valid image,
// directly jump to it without waiting.
bool is_started_from_power_up = RTC->BKP0R != 0xB0043D;
Expand All @@ -108,6 +109,7 @@ int main(void) {
// If fail, we bootload as usual.
jump_to_user_program();
}
*/

SetStatusLedMode(LED_MODE_BLINK_FAST);

Expand Down
27 changes: 20 additions & 7 deletions bootloader/src/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#define GREETING_MESSAGE "BOOTLOADER VERSION:xcore-boot v1.0"
#define BOARD_MSG "BOARD:"
#define VERSION_MSG "HARDWARE VERSION:"
#define CARRIER_BOARD_MSG "CARRIER BOARD INFO:"
#define CARRIER_BOARD_MSG "CARRIER BOARD:"
#define CARRIER_BOARD_VERSION_MSG "CARRIER BOARD VERSION:"
#define BOOTLOADER_PORT 8007

// 256bits need to be written at a single time and the buffer needs to be
Expand Down Expand Up @@ -129,14 +130,26 @@ static THD_FUNCTION(bootloader_thread, arg) {
SEND(connfd, GREETING_MESSAGE);
SEND(connfd, "\n");
SEND(connfd, BOARD_MSG);
write(connfd, BOARD_NAME, strlen(BOARD_NAME));
write(connfd, board_info.board_id,
strnlen(board_info.board_id, sizeof(board_info.board_id)));
SEND(connfd, "\n");
SEND(connfd, VERSION_MSG);
SEND(connfd, "0");
SEND(connfd, "\n");
char version_buffer[50] = {0};
chsnprintf(version_buffer, sizeof(version_buffer), "%d.%d.%d\n",
board_info.version_major, board_info.version_minor,
board_info.version_patch);
SEND(connfd, version_buffer);
SEND(connfd, CARRIER_BOARD_MSG);
SEND(connfd, "N/A");
write(connfd, carrier_board_info.board_id,
strnlen(carrier_board_info.board_id,
sizeof(carrier_board_info.board_id)));
SEND(connfd, "\n");
SEND(connfd, CARRIER_BOARD_VERSION_MSG);
chsnprintf(version_buffer, sizeof(version_buffer), "%d.%d.%d\n",
carrier_board_info.version_major,
carrier_board_info.version_minor,
carrier_board_info.version_patch);
SEND(connfd, version_buffer);

SEND(connfd, "SEND HASH\n");

Expand Down Expand Up @@ -174,7 +187,7 @@ static THD_FUNCTION(bootloader_thread, arg) {
continue;
}

bool hash_ok = parseSHA256(buffer, 64, hash);
bool hash_ok = parseSHA256((const char *)buffer, 64, hash);
if (!hash_ok) {
ok = false;
continue;
Expand All @@ -191,7 +204,7 @@ static THD_FUNCTION(bootloader_thread, arg) {
}
// Put 0 termination
buffer[buffer_fill - 1] = 0;
image_size = strtol(buffer, NULL, 10);
image_size = strtol((const char *)buffer, NULL, 10);
chsnprintf(out_buffer, sizeof(out_buffer), ">Image size: %u\n",
image_size);
write(connfd, out_buffer, strlen(out_buffer));
Expand Down
2 changes: 1 addition & 1 deletion bootloader/src/bootloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

#define TARGET_FLASH_ADDRESS 0x8020000

void InitBootloaderThread();
void InitBootloaderThread(void);

#endif // BOOTLOADER_H
10 changes: 9 additions & 1 deletion bootloader/src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
// This prevents the device from rebooting while flashing
mutex_t reboot_mutex;

struct board_info board_info = {0};
struct carrier_board_info carrier_board_info = {0};

EVENTSOURCE_DECL(netif_events);

void InitGlobals() { chMtxObjectInit(&reboot_mutex); }
void InitGlobals() {
chMtxObjectInit(&reboot_mutex);

ID_EEPROM_GetBoardInfo(&board_info);
ID_EEPROM_GetCarrierBoardInfo(&carrier_board_info);
}
7 changes: 6 additions & 1 deletion bootloader/src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef GLOBALS_H
#define GLOBALS_H

#include <id_eeprom.h>

#include "ch.h"

#define NETIF_EVENT_ADDRESS_VALID 1
Expand All @@ -15,6 +17,9 @@ extern mutex_t reboot_mutex;

extern event_source_t netif_events;

void InitGlobals();
extern struct board_info board_info;
extern struct carrier_board_info carrier_board_info;

void InitGlobals(void);

#endif // GLOBALS_H
29 changes: 15 additions & 14 deletions bootloader/src/heartbeat.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ static volatile uint8_t blink_state = 0;

static virtual_timer_t heartbeat_timer;

static void heartbeat_timer_cb(void *arg) {
static void heartbeat_timer_cb(struct ch_virtual_timer *tp, void *arg) {
(void)arg;
(void)tp;
bool elapsed =
TIME_I2MS(chVTTimeElapsedSinceX(last_idle_tick)) > HEARTBEAT_RECENT_MS;
switch (blink_state) {
case 0:
if (elapsed) {
#ifndef BOARD_HEARTBEAT_LED_INVERTED
palSetLine(LINE_HEARTBEAT_LED_RED);
palSetLine(LINE_HEARTBEAT_LED_GREEN);
palClearLine(LINE_HEARTBEAT_LED_BLUE);
#else
palClearLine(LINE_HEARTBEAT_LED_RED);
palClearLine(LINE_HEARTBEAT_LED_GREEN);
palSetLine(LINE_HEARTBEAT_LED_BLUE);
#endif
} else {
#ifndef BOARD_HEARTBEAT_LED_INVERTED
palClearLine(LINE_HEARTBEAT_LED_GREEN);
palSetLine(LINE_HEARTBEAT_LED_BLUE);
palClearLine(LINE_HEARTBEAT_LED_RED);
#else
palSetLine(LINE_HEARTBEAT_LED_GREEN);
palClearLine(LINE_HEARTBEAT_LED_BLUE);
palSetLine(LINE_HEARTBEAT_LED_RED);
#endif
}
Expand All @@ -44,10 +45,10 @@ static void heartbeat_timer_cb(void *arg) {
break;
case 1:
#ifndef BOARD_HEARTBEAT_LED_INVERTED
palSetLine(LINE_HEARTBEAT_LED_GREEN);
palClearLine(LINE_HEARTBEAT_LED_BLUE);
palClearLine(LINE_HEARTBEAT_LED_RED);
#else
palClearLine(LINE_HEARTBEAT_LED_GREEN);
palSetLine(LINE_HEARTBEAT_LED_BLUE);
palSetLine(LINE_HEARTBEAT_LED_RED);
#endif
chSysLockFromISR();
Expand All @@ -59,17 +60,17 @@ static void heartbeat_timer_cb(void *arg) {
if (elapsed) {
#ifndef BOARD_HEARTBEAT_LED_INVERTED
palSetLine(LINE_HEARTBEAT_LED_RED);
palSetLine(LINE_HEARTBEAT_LED_GREEN);
palClearLine(LINE_HEARTBEAT_LED_BLUE);
#else
palClearLine(LINE_HEARTBEAT_LED_RED);
palClearLine(LINE_HEARTBEAT_LED_GREEN);
palSetLine(LINE_HEARTBEAT_LED_BLUE);
#endif
} else {
#ifndef BOARD_HEARTBEAT_LED_INVERTED
palClearLine(LINE_HEARTBEAT_LED_GREEN);
palSetLine(LINE_HEARTBEAT_LED_BLUE);
palClearLine(LINE_HEARTBEAT_LED_RED);
#else
palSetLine(LINE_HEARTBEAT_LED_GREEN);
palClearLine(LINE_HEARTBEAT_LED_BLUE);
palSetLine(LINE_HEARTBEAT_LED_RED);
#endif
}
Expand All @@ -80,10 +81,10 @@ static void heartbeat_timer_cb(void *arg) {
break;
default:
#ifndef BOARD_HEARTBEAT_LED_INVERTED
palSetLine(LINE_HEARTBEAT_LED_GREEN);
palClearLine(LINE_HEARTBEAT_LED_BLUE);
palClearLine(LINE_HEARTBEAT_LED_RED);
#else
palClearLine(LINE_HEARTBEAT_LED_GREEN);
palSetLine(LINE_HEARTBEAT_LED_BLUE);
palSetLine(LINE_HEARTBEAT_LED_RED);
#endif

Expand All @@ -97,5 +98,5 @@ static void heartbeat_timer_cb(void *arg) {

void InitHeartbeat() {
chVTObjectInit(&heartbeat_timer);
heartbeat_timer_cb(NULL);
heartbeat_timer_cb(NULL, NULL);
}
2 changes: 1 addition & 1 deletion bootloader/src/heartbeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
* Initializes the timer task for showing the heartbeat (to show the RTOS hasn't
* crashed)
*/
void InitHeartbeat();
void InitHeartbeat(void);

#endif // HEARTBEAT_H
Loading

0 comments on commit a784e2a

Please sign in to comment.