Skip to content

Commit

Permalink
Add support for address and size
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeckmann committed Nov 26, 2020
1 parent efd8c4e commit 940432c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
7 changes: 4 additions & 3 deletions Core/Src/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ void _OSPI_Program(OSPI_HandleTypeDef *hospi, uint32_t address, uint8_t *buffer

void OSPI_Program(OSPI_HandleTypeDef *hospi, uint32_t address, uint8_t *buffer, size_t buffer_size) {
unsigned iterations = buffer_size / 256;

for(int i=0; i < iterations; i++) {
unsigned dest_page = address / 256;

for(int i = 0; i < iterations; i++) {
OSPI_NOR_WriteEnable(hospi);
_OSPI_Program(hospi, i * 256, buffer + (i * 256), buffer_size > 256 ? 256 : buffer_size);
_OSPI_Program(hospi, (i + dest_page) * 256, buffer + (i * 256), buffer_size > 256 ? 256 : buffer_size);
buffer_size -= 256;
}
}
Expand Down
13 changes: 11 additions & 2 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ SPI_HandleTypeDef hspi2;

/* USER CODE BEGIN PV */

uint16_t audiobuffer[48000] __attribute__((section (".audio")));
uint8_t logbuf[1024 * 4];
uint32_t log_idx;

// This tells the loader how much and where to program in the flash
__attribute__((used)) __attribute__((section (".persistent"))) uint32_t program_size;
__attribute__((used)) __attribute__((section (".persistent"))) uint32_t program_address;
__attribute__((used)) __attribute__((section (".persistent"))) uint32_t program_magic;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
Expand Down Expand Up @@ -126,6 +130,11 @@ int main(void)
MX_NVIC_Init();
/* USER CODE BEGIN 2 */

// Check that the magic has been set correctly
if (program_magic != 0xdeadbeef) {
Error_Handler();
}

// SPI_MODE or QUAD_MODE
quad_mode_t quad_mode = QUAD_MODE;

Expand All @@ -142,7 +151,7 @@ int main(void)

uint32_t ram_address = 0x24000000;
uint8_t *ram = (uint8_t*)ram_address;
OSPI_Program(&hospi1, 0x0, ram, 1024*1024);
OSPI_Program(&hospi1, program_address, ram, program_size);
#endif

OSPI_EnableMemoryMappedMode(&hospi1);
Expand Down
8 changes: 8 additions & 0 deletions STM32H7B0VBTx_FLASH.ld
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ SECTIONS
. = ALIGN(8);
} >DTCMRAM

._persistent :
{
. = ALIGN(8);
*(.persistent)
. = ALIGN(8);
} >DTCMRAM




/* Remove information from the standard libraries */
Expand Down
51 changes: 46 additions & 5 deletions flash.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
#!/bin/bash

set -e
set -ex

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ELF=${DIR}/build/gw_base.elf
ADDRESS=0
SIZE=$((1024 * 1024))
MAGIC="0xdeadbeef"


if [[ $# -ne 1 ]]; then
echo "Usage: flash.sh <binary to flash>"
if [[ $# -lt 1 ]]; then
echo "Usage: flash.sh <binary to flash> [address in flash] [size]"
echo "Note! Destination address must be aligned to 256 bytes."
exit
fi

IMAGE=$1

if [[ $# -gt 1 ]]; then
ADDRESS=$2
fi

if [[ $# -gt 2 ]]; then
SIZE=$3
fi

objdump=${OBJDUMP:-arm-none-eabi-objdump}

function get_symbol {
name=$1
objdump_cmd="${objdump} -t ${ELF}"
size=$(${objdump_cmd} | grep " $name" | cut -d " " -f1 | tr 'a-f' 'A-F')
printf "ibase=16\n${size}\n" | bc
}


VAR_program_size=$(printf '0x%08x\n' $(get_symbol "program_size"))
VAR_program_address=$(printf '0x%08x\n' $(get_symbol "program_address"))
VAR_program_magic=$(printf '0x%08x\n' $(get_symbol "program_magic"))

echo "Loading image into RAM..."
openocd -f ${DIR}/adapter_config.cfg \
-c "echo \"Resetting device\";" \
-c "reset halt;" \
-c "echo \"Programming ELF\";" \
-c "program ${ELF} verify;" \
-c "reset halt;" \
-c "sleep 100;" \
-c "echo \"Loading image into RAM\";" \
-c "load_image ${IMAGE} 0x24000000;" \
-c "mww ${VAR_program_size} ${SIZE}" \
-c "mww ${VAR_program_address} ${ADDRESS}" \
-c "mww ${VAR_program_magic} ${MAGIC}" \
-c "echo \"Starting flash process\";" \
-c "resume;" \

openocd -f ${DIR}/adapter_config.cfg -c "load_image $1 0x24000000" -c "program ${DIR}/build/gw_base.elf; reset; exit"

echo "Please wait til the screen blinks once per second."
echo "(Rapid blinking means an error occured)"

0 comments on commit 940432c

Please sign in to comment.