Skip to content

Commit

Permalink
Merge pull request #138 from eiln/eileen/bootloader
Browse files Browse the repository at this point in the history
[READY] Bootloader Upgrade
  • Loading branch information
eiln authored Dec 3, 2024
2 parents 50ea3bd + c825de2 commit 6ab0109
Show file tree
Hide file tree
Showing 16 changed files with 525 additions and 519 deletions.
28 changes: 14 additions & 14 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"-d",
"${workspaceFolder}/source/a_box"
],
"preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "main"
},
{ "name": "BL App - Main Module",
Expand All @@ -208,7 +208,7 @@
"-d",
"${workspaceFolder}/source/main_module"
],
"preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "main"
},
{ "name": "BL App - Dashboard",
Expand All @@ -227,7 +227,7 @@
"-d",
"${workspaceFolder}/source/dashboard"
],
"preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "main"
},
{ "name": "BL App - Torque Vectoring",
Expand All @@ -246,7 +246,7 @@
"-d",
"${workspaceFolder}/source/torque_vector"
],
"preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "main"
},
{ "name": "BL App - A_Box",
Expand All @@ -265,7 +265,7 @@
"-d",
"${workspaceFolder}/source/a_box"
],
"preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "main"
},
{ "name": "BL App - PDU",
Expand All @@ -284,7 +284,7 @@
"-d",
"${workspaceFolder}/source/pdu"
],
"preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "main"
},
{ "name": "BL App - DAQ",
Expand All @@ -303,7 +303,7 @@
"-d",
"${workspaceFolder}/source/daq"
],
"preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "main"
},
{ "name": "BL App - l4_testing",
Expand Down Expand Up @@ -341,7 +341,7 @@
"-d",
"${workspaceFolder}/source/bootloader"
],
// "preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "Reset_Handler"
},
{
Expand All @@ -361,7 +361,7 @@
"-d",
"${workspaceFolder}/source/bootloader"
],
// "preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "Reset_Handler"
},
{ "name": "BL Bootloader - Torque Vectoring",
Expand All @@ -380,7 +380,7 @@
"-d",
"${workspaceFolder}/source/bootloader"
],
// "preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "Reset_Handler"
},
{
Expand All @@ -400,7 +400,7 @@
"-d",
"${workspaceFolder}/source/bootloader"
],
// "preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "Reset_Handler"
},
{ "name": "BL Bootloader - PDU",
Expand All @@ -419,7 +419,7 @@
"-d",
"${workspaceFolder}/source/bootloader"
],
// "preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "Reset_Handler"
},
{ "name": "BL Bootloader - DAQ",
Expand All @@ -438,7 +438,7 @@
"-d",
"${workspaceFolder}/source/bootloader"
],
// "preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "Reset_Handler"
},
{
Expand All @@ -458,7 +458,7 @@
"-d",
"${workspaceFolder}/source/bootloader"
],
// "preLaunchTask": "build",
"preLaunchTask": "build_bl",
"runToEntryPoint": "Reset_Handler"
},
]
Expand Down
8 changes: 8 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
"isDefault": true
}
},
{
"label": "build_bl",
"type": "shell",
"command": "python3 per_build.py --bootloader",
"group": {
"kind": "build",
}
},
{
"label": "Submodule Update",
"group": "none",
Expand Down
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ if(ret EQUAL "1")
endif()

# Check for bootloader generation
option(BOOTLOADER_BUILD "Build the bootlaoder components" OFF)
if (NOT BOOTLOADER_BUILD)
message(STATUS "Not building applications for bootloaders.")
else()
Expand Down
4 changes: 2 additions & 2 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function(postbuild_target TARGET_NAME)
)

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND arm-none-eabi-objcopy -S -O ihex --gap-fill 0 ${TARGET_NAME} ${COMPONENT_OUTPUT_DIR}/${OUTPUT_FILE_NAME}.hex
COMMENT "Generateing HEX file"
COMMAND arm-none-eabi-objcopy -S -O ihex ${TARGET_NAME} ${COMPONENT_OUTPUT_DIR}/${OUTPUT_FILE_NAME}.hex
COMMENT "Generating HEX file"
)

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
Expand Down
10 changes: 6 additions & 4 deletions common/bootloader/bootloader_common.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @file bootloader_common.h
* @author Adam Busch ([email protected])
* @brief Common bootloader functions for all firmware components.
* @brief Common bootloader functions for all firmware components.
* Useful for entering the bootloader to download new firmware
* @version 0.1
* @date 2022-03-10
*
*
* @copyright Copyright (c) 2022
*
*
*/
#ifndef _BOOTLOADER_COMMON_H_
#define _BOOTLOADER_COMMON_H_
Expand All @@ -27,7 +27,9 @@ typedef enum
{
BLCMD_START = 0x1, /* Request to start firmware download */
BLCMD_CRC = 0x3, /* Final CRC-32b check of firmware */
BLCMD_RST = 0x5 /* Request for reset */
BLCMD_CRC_BACKUP = 0x2,
BLCMD_JUMP = 0x4, /* Request to start firmware download */
BLCMD_RST = 0x5, /* Request for reset */
} BLCmd_t;

typedef enum {
Expand Down
2 changes: 1 addition & 1 deletion common/linker/STM32F407VGTx_FLASH_APP.ld
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ MEMORY
{
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
FLASH (rx) : ORIGIN = 0x8004000, LENGTH = 1008K
FLASH (rx) : ORIGIN = 0x8008000, LENGTH = 992K
}

/* Sections */
Expand Down
4 changes: 2 additions & 2 deletions common/linker/STM32F407VGTx_FLASH_BL.ld
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ _estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */
_Min_Heap_Size = 0x200; /* required amount of heap */
_Min_Stack_Size = 0x400; /* required amount of stack */

PROVIDE(_eboot_flash = 0x08004000); /* End of the bootloader flash, this is where the application begins*/
PROVIDE(_eboot_flash = 0x08008000); /* End of the bootloader flash, this is where the application begins*/

/* Memories definition */
MEMORY
{
CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 16K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 32K
}

/* Sections */
Expand Down
75 changes: 75 additions & 0 deletions common/phal_F4_F7/crc/crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @file crc.c
* @author Eileen Yoon ([email protected])
* @brief Hardware CRC32 w/ software fallback
* @version 0.1
* @date 2024-11-25
*
* @copyright Copyright (c) 2024
*
*/

#include "crc.h"

void PHAL_CRC32_Reset(void)
{
// CRC initializaion
#if defined(STM32L496xx) || defined(STM32L432xx) || defined(STM32F732xx)
RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN; // Clock the CRC peripheral
CRC->INIT = 0xFFFFFFFF; // Reset initial value
CRC->CR &= ~CRC_CR_POLYSIZE_Msk; // Set 32 bit (00)
CRC->POL = 0x04C11DB7; // CRC-32b (Ethernet Polynomial)
CRC->CR |= CRC_CR_RESET; // Reset CRC
#else
RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN; // F4 only supports CRC-32b
CRC->CR = CRC_CR_RESET;
#endif
}

void PHAL_CRC32_Init(void)
{
PHAL_CRC32_Reset();
}

uint32_t PHAL_CRC32_Calculate(uint32_t *data, uint32_t count)
{
PHAL_CRC32_Reset();
__DSB();

for (uint32_t i = 0; i < count; i++)
CRC->DR = data[i];

return CRC->DR;
}

static const uint32_t crc32b_LUT[16] = {
0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9,
0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005,
0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6, 0x2B4BCB61,
0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD,
};

static inline uint32_t update_crc(uint32_t crc, uint32_t data)
{
crc = crc ^ data; // Apply all 32-bits

// Process 32-bits, 4 at a time, or 8 rounds
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];
crc = (crc << 4) ^ crc32b_LUT[crc >> 28];

return crc;
}

uint32_t PHAL_CRC32_CalculateSW(uint32_t *data, uint32_t count)
{
uint32_t crc = 0xFFFFFFFF;
for (uint32_t i = 0; i < count; i++)
crc = update_crc(crc, data[i]);
return crc;
}
31 changes: 31 additions & 0 deletions common/phal_F4_F7/crc/crc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @file crc.h
* @author Eileen Yoon ([email protected])
* @brief Hardware CRC32 w/ software fallback
* @version 0.1
* @date 2024-11-25
*
* @copyright Copyright (c) 2024
*
*/

#ifndef _PHAL_CRC_H
#define _PHAL_CRC_H

#if defined(STM32F407xx)
#include "stm32f4xx.h"
#include "system_stm32f4xx.h"
#elif defined(STM32F732xx)
#include "stm32f7xx.h"
#include "system_stm32f7xx.h"
#else
#error "Please define a MCU arch"
#endif

void PHAL_CRC32_Reset(void);

uint32_t PHAL_CRC32_Calculate(uint32_t *data, uint32_t count);

uint32_t PHAL_CRC32_CalculateSW(uint32_t *data, uint32_t count);

#endif // _PHAL_CRC_H
Loading

0 comments on commit 6ab0109

Please sign in to comment.