Skip to content

Commit

Permalink
New Code Style (#1415)
Browse files Browse the repository at this point in the history
### Changelist 
<!-- Give a list of the changes covered in this PR. This will help both
you and the reviewer keep this PR within scope. -->

### Testing Done
<!-- Outline the testing that was done to demonstrate the changes are
solid. This could be unit tests, integration tests, testing on the car,
etc. Include relevant code snippets, screenshots, etc as needed. -->

### Resolved Tickets
<!-- Link any tickets that this PR resolves. -->
  • Loading branch information
Lucien950 authored Jan 19, 2025
1 parent f26130c commit 9f45826
Show file tree
Hide file tree
Showing 127 changed files with 1,948 additions and 2,131 deletions.
83 changes: 41 additions & 42 deletions firmware/boot/bootloader.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
#include "bootloader.h"
#include "bootloaderConfig.h"
#include "main.h"

#include <stdint.h>
#include <string.h>

#include "cmsis_gcc.h"
#include "cmsis_os.h"
#include "hw_hal.h"
#include "io_can.h"

#include "app_commitInfo.h"
#include "io_canQueue.h"
#include "io_log.h"
#include "hw_flash.h"

#include "hw_hardFaultHandler.h"
#include "hw_flash.h"
#include "hw_utils.h"
#include "hw_crc.h"
#include "main.h"
#include "hw_gpio.h"
#include "app_commitInfo.h"
#include "hw_hal.h"
#include "io_can.h"

extern CRC_HandleTypeDef hcrc;
extern TIM_HandleTypeDef htim6;

// Need these to be created an initialized elsewhere
extern CanHandle can;

void canRxQueueOverflowCallBack(const uint32_t unused)
{
UNUSED(unused);
BREAK_IF_DEBUGGER_CONNECTED();
}

void canTxQueueOverflowCallBack(const uint32_t unused)
{
UNUSED(unused);
BREAK_IF_DEBUGGER_CONNECTED();
}

// App code block. Start/size included from the linker script.
extern uint32_t __app_metadata_start__;
extern uint32_t __app_metadata_size__;
extern uint32_t __app_metadata_start__; // NOLINT(*-reserved-identifier)
extern uint32_t __app_metadata_size__; // NOLINT(*-reserved-identifier)

// App metadata block. Start/size included from the linker script.
extern uint32_t __app_code_start__;
extern uint32_t __app_code_size__;
extern uint32_t __app_code_start__; // NOLINT(*-reserved-identifier)
extern uint32_t __app_code_size__; // NOLINT(*-reserved-identifier)

// Info needed by the bootloader to boot safely. Currently takes up the the first kB
// of flash allocated to the app.
Expand All @@ -41,18 +60,6 @@ typedef enum
BOOT_STATUS_NO_APP
} BootStatus;

static void canRxOverflow(uint32_t unused)
{
UNUSED(unused);
BREAK_IF_DEBUGGER_CONNECTED();
}

static void canTxOverflow(uint32_t unused)
{
UNUSED(unused);
BREAK_IF_DEBUGGER_CONNECTED();
}

_Noreturn static void modifyStackPointerAndStartApp(const uint32_t *address)
{
// Disable interrupts before jumping.
Expand Down Expand Up @@ -110,24 +117,19 @@ static BootStatus verifyAppCodeChecksum(void)
return BOOT_STATUS_NO_APP;
}

Metadata *metadata = (Metadata *)&__app_metadata_start__;
const Metadata *metadata = (Metadata *)&__app_metadata_start__;
if (metadata->size_bytes > (uint32_t)&__app_code_size__)
{
// App binary size field is invalid.
return BOOT_STATUS_APP_INVALID;
}

uint32_t calculated_checksum = hw_crc_calculate(&__app_code_start__, metadata->size_bytes / 4);
const uint32_t calculated_checksum = hw_crc_calculate(&__app_code_start__, metadata->size_bytes / 4);
return calculated_checksum == metadata->checksum ? BOOT_STATUS_APP_VALID : BOOT_STATUS_APP_INVALID;
}

static const CanConfig can_config = {
.rx_msg_filter = NULL,
.rx_overflow_callback = canRxOverflow,
.tx_overflow_callback = canTxOverflow,
};

#ifndef BOOT_AUTO
#include "hw_gpio.h"
static const Gpio bootloader_pin = {
.port = nBOOT_EN_GPIO_Port,
.pin = nBOOT_EN_Pin,
Expand All @@ -149,7 +151,6 @@ void bootloader_init(void)
// HW-level CAN should be initialized in main.c, since it is MCU-specific.
hw_hardFaultHandler_init();
hw_crc_init(&hcrc);
io_can_init(&can_config);

// This order is important! The bootloader starts the app when the bootloader
// enable pin is high, which is caused by pullup resistors internal to each
Expand Down Expand Up @@ -183,8 +184,7 @@ _Noreturn void bootloader_runInterfaceTask(void)
{
for (;;)
{
CanMsg command;
io_can_popRxMsgFromQueue(&command);
const CanMsg command = io_canQueue_popRx();

if (command.std_id == START_UPDATE_ID)
{
Expand All @@ -193,21 +193,21 @@ _Noreturn void bootloader_runInterfaceTask(void)
update_in_progress = true;

// Send ACK message that programming has started.
CanMsg reply = { .std_id = UPDATE_ACK_ID, .dlc = 0 };
io_can_pushTxMsgToQueue(&reply);
const CanMsg reply = { .std_id = UPDATE_ACK_ID, .dlc = 0 };
io_canQueue_pushTx(&reply);
}
else if (command.std_id == ERASE_SECTOR_ID && update_in_progress)
{
// Erase a flash sector.
uint8_t sector = command.data[0];
const uint8_t sector = command.data[0];
hw_flash_eraseSector(sector);

// Erasing sectors takes a while, so reply when finished.
CanMsg reply = {
.std_id = ERASE_SECTOR_COMPLETE_ID,
.dlc = 0,
};
io_can_pushTxMsgToQueue(&reply);
io_canQueue_pushTx(&reply);
}
else if (command.std_id == PROGRAM_ID && update_in_progress)
{
Expand All @@ -224,7 +224,7 @@ _Noreturn void bootloader_runInterfaceTask(void)
.dlc = 1,
};
reply.data[0] = (uint8_t)verifyAppCodeChecksum();
io_can_pushTxMsgToQueue(&reply);
io_canQueue_pushTx(&reply);

// Verify command doubles as exit programming state command.
update_in_progress = false;
Expand All @@ -245,7 +245,7 @@ _Noreturn void bootloader_runTickTask(void)
status_msg.data[2] = (uint8_t)((0x00ff0000 & GIT_COMMIT_HASH) >> 16);
status_msg.data[3] = (uint8_t)((0xff000000 & GIT_COMMIT_HASH) >> 24);
status_msg.data[4] = (uint8_t)(verifyAppCodeChecksum() << 1) | GIT_COMMIT_CLEAN;
io_can_pushTxMsgToQueue(&status_msg);
io_canQueue_pushTx(&status_msg);

bootloader_boardSpecific_tick();

Expand All @@ -258,9 +258,8 @@ _Noreturn void bootloader_runCanTxTask(void)
{
for (;;)
{
CanMsg tx_msg;
io_can_popTxMsgFromQueue(&tx_msg);
io_can_transmitMsgFromQueue(&tx_msg);
CanMsg tx_msg = io_canQueue_popTx();
io_can_transmit(&can, &tx_msg);
}
}

Expand Down
8 changes: 5 additions & 3 deletions firmware/cmake/bootlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ function(stm32f4_boot_binary

# Add shared files.
list(APPEND SRCS
"${SHARED_IO_INCLUDE_DIR}/io_canQueue.c"
"${SHARED_IO_INCLUDE_DIR}/io_can.c"
"${SHARED_IO_INCLUDE_DIR}/io_time.c"
"${SHARED_HW_INCLUDE_DIR}/hw_flash.c"
"${SHARED_HW_INCLUDE_DIR}/hw_crc.c"
"${SHARED_HW_INCLUDE_DIR}/hw_can.c"
"${SHARED_HW_INCLUDE_DIR}/hw_gpio.c"
"${SHARED_HW_INCLUDE_DIR}/hw_hardFaultHandler.c"
"${SHARED_HW_INCLUDE_DIR}/hw_crc.c"
Expand Down Expand Up @@ -131,10 +132,11 @@ function(stm32h7_boot_binary

# Add shared files.
list(APPEND SRCS
"${SHARED_IO_INCLUDE_DIR}/io_can.c"
"${SHARED_IO_INCLUDE_DIR}/io_canQueue.c"
"${SHARED_IO_INCLUDE_DIR}/io_fdcan.c"
"${SHARED_IO_INCLUDE_DIR}/io_time.c"
"${SHARED_HW_INCLUDE_DIR}/hw_flash.c"
"${SHARED_HW_INCLUDE_DIR}/hw_crc.c"
"${SHARED_HW_INCLUDE_DIR}/hw_fdcan.c"
"${SHARED_HW_INCLUDE_DIR}/hw_gpio.c"
"${SHARED_HW_INCLUDE_DIR}/hw_hardFaultHandler.c"
"${SHARED_HW_INCLUDE_DIR}/hw_crc.c"
Expand Down
11 changes: 4 additions & 7 deletions firmware/cmake/shared.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ function(commit_info_library
OUTPUT_PATH
)
commit_info_generate_sources(${BIND_TARGET} ${OUTPUT_PATH})
IF("${TARGET}" STREQUAL "binary")
IF(${TARGET} STREQUAL "binary")
embedded_interface_library(
"${LIB_NAME}"
"${COMMIT_INFO_SRC}"
"${COMMIT_INFO_INCLUDE_DIR}"
FALSE
)
ELSEIF("${TARGET}" STREQUAL "test")
ELSEIF(${TARGET} STREQUAL "test")
get_filename_component(HEADER_DIR "${HEADER_OUTPUT_PATH}" DIRECTORY)
# TODO make this an interface library as well
add_library(
"${LIB_NAME}" INTERFACE
"${COMMIT_INFO_SRC}"
)
add_library(${LIB_NAME} INTERFACE)
target_sources(${LIB_NAME} INTERFACE ${COMMIT_INFO_SRC})
target_include_directories("${LIB_NAME}" INTERFACE "${HEADER_DIR}")
ENDIF()
endfunction()
Expand Down
6 changes: 3 additions & 3 deletions firmware/dev/h7dev/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ set(LINKER_SCRIPT "${LINKER_DIR}/stm32h733vgtx/stm32h733vgtx_app.ld")

file(GLOB_RECURSE EMBEDDED_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")
list(APPEND EMBEDDED_SRCS
"${SHARED_IO_INCLUDE_DIR}/io_can.c"
"${SHARED_IO_INCLUDE_DIR}/io_canLogging.c"
"${SHARED_IO_INCLUDE_DIR}/io_canQueue.c"
"${SHARED_IO_INCLUDE_DIR}/io_fdcan.c"
"${SHARED_IO_INCLUDE_DIR}/io_canLoggingQueue.c"
"${SHARED_IO_INCLUDE_DIR}/io_fileSystem_logfs.c"
"${SHARED_HW_INCLUDE_DIR}/hw_hardFaultHandler.c"
"${SHARED_HW_INCLUDE_DIR}/hw_fdcan.c"
"${SHARED_HW_INCLUDE_DIR}/hw_bootup.c"
"${SHARED_HW_INCLUDE_DIR}/hw_uart.c"
"${SHARED_HW_INCLUDE_DIR}/hw_sd.c"
Expand Down
6 changes: 3 additions & 3 deletions firmware/dev/h7dev/boot/cubemx/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bootloader.h"
#include "hw_can.h"
#include "io_canQueue.h"
#include "io_can.h"
/* USER CODE END Includes */

Expand Down Expand Up @@ -104,7 +104,7 @@ void runCanTxTask(void *argument);

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
CanHandle can = { .can = &hfdcan2, .can_msg_received_callback = io_can_pushRxMsgToQueue };
CanHandle can = { .hcan = &hfdcan2 };
/* USER CODE END 0 */

/**
Expand Down Expand Up @@ -138,7 +138,7 @@ int main(void)
MX_CRC_Init();
MX_FDCAN2_Init();
/* USER CODE BEGIN 2 */
hw_can_init(&can);
io_can_init(&can);
bootloader_init();
/* USER CODE END 2 */

Expand Down
Loading

0 comments on commit 9f45826

Please sign in to comment.