Skip to content

Commit

Permalink
Add support for flashing >1M using flash_multi.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeckmann committed Nov 27, 2020
1 parent 3b9080e commit cd2b029
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
16 changes: 14 additions & 2 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ __attribute__((used)) __attribute__((section (".persistent"))) uint32_t program_
// This can be read by openocd to see when programming is done
__attribute__((used)) __attribute__((section (".persistent"))) uint32_t program_done;

// Control if chip should be erased or not
__attribute__((used)) __attribute__((section (".persistent"))) uint32_t program_erase;

// Size of the flash
// TODO: Make configurable from openocd
// Number of address bits. 20=1M, 24=16M
uint32_t program_device_size = 20;


/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
Expand Down Expand Up @@ -154,7 +163,10 @@ int main(void)
// Set to 0 if you only want to enable flash read memory mapping
#if 1
OSPI_NOR_WriteEnable(&hospi1);
OSPI_ChipErase(&hospi1);

if (program_erase) {
OSPI_ChipErase(&hospi1);
}

uint32_t ram_address = 0x24000000;
uint8_t *ram = (uint8_t*)ram_address;
Expand Down Expand Up @@ -284,7 +296,7 @@ static void MX_OCTOSPI1_Init(void)
hospi1.Init.FifoThreshold = 4;
hospi1.Init.DualQuad = HAL_OSPI_DUALQUAD_DISABLE;
hospi1.Init.MemoryType = HAL_OSPI_MEMTYPE_MACRONIX;
hospi1.Init.DeviceSize = 20;
hospi1.Init.DeviceSize = program_device_size; // Number of address bits. 20=1M, 24=16M
hospi1.Init.ChipSelectHighTime = 2;
hospi1.Init.FreeRunningClock = HAL_OSPI_FREERUNCLK_DISABLE;
hospi1.Init.ClockMode = HAL_OSPI_CLOCK_MODE_0;
Expand Down
12 changes: 11 additions & 1 deletion flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ ELF=${DIR}/build/gw_base.elf
ADDRESS=0
SIZE=$((1024 * 1024))
MAGIC="0xdeadbeef"
ERASE=1

if [[ $# -lt 1 ]]; then
echo "Usage: flash.sh <binary to flash> [address in flash] [size]"
echo "Usage: flash.sh <binary to flash> [address in flash] [size] [erase=1]"
echo "Note! Destination address must be aligned to 256 bytes."
echo "'address in flash': Where to program to. 0x000000 is the start of the flash. "
echo "'size': Size of the binary to flash. Ideally aligned to 256 bytes."
echo "'erase': If '0', chip erase will be skipped. Default '1'."
exit
fi

Expand All @@ -24,6 +28,10 @@ if [[ $# -gt 2 ]]; then
SIZE=$3
fi

if [[ $# -gt 3 ]]; then
ERASE=$4
fi

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

function get_symbol {
Expand All @@ -38,6 +46,7 @@ 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"))
VAR_program_done=$(printf '0x%08x\n' $(get_symbol "program_done"))
VAR_program_erase=$(printf '0x%08x\n' $(get_symbol "program_erase"))


echo "Loading image into RAM..."
Expand All @@ -53,6 +62,7 @@ openocd -f ${DIR}/adapter_config.cfg \
-c "mww ${VAR_program_size} ${SIZE}" \
-c "mww ${VAR_program_address} ${ADDRESS}" \
-c "mww ${VAR_program_magic} ${MAGIC}" \
-c "mww ${VAR_program_erase} ${ERASE}" \
-c "echo \"Starting flash process\";" \
-c "resume; exit;"

Expand Down
63 changes: 63 additions & 0 deletions flash_multi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

set -e

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

if [[ $# -lt 1 ]]; then
echo "Usage: flash_multi.sh <binary to flash>"
echo "Note! This will cut the binary in 1M chunks and flash them to 0x000000 and onwards"
exit
fi

IMAGE=$1

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

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

FILESIZE=$(stat -c%s $IMAGE)
CHUNKS=$(( FILESIZE / (1024*1024) ))
SIZE=$((FILESIZE))

echo $CHUNKS

ERASE=1
i=0
while [[ $SIZE -gt 0 ]]; do
ADDRESS_HEX=$(printf "0x%08x" $(( i * 1024 * 1024 )))
if [[ $SIZE -ge $((1024*1024)) ]]; then
echo less
CHUNK_SIZE=$((1024*1024))
else
echo else
CHUNK_SIZE=${SIZE}
fi
SIZE_HEX=$(printf "0x%08x" ${CHUNK_SIZE})

TMPFILE=$(mktemp /tmp/flash_chunk.XXXXXX)
if [[ ! -e $TMPFILE ]]; then
echo "Can't create tempfile!"
exit 1
fi

echo "Preparing chunk $i in file ${TMPFILE}"
dd if=${IMAGE} of=${TMPFILE} bs=1024 count=$(( CHUNK_SIZE / 1024 )) skip=$(( i * 1024 ))

echo "Flashing!"
${DIR}/flash.sh ${TMPFILE} ${ADDRESS_HEX} ${SIZE_HEX} ${ERASE}

# Skip erase the following iterations
ERASE=0

rm -f ${TMPFILE}

SIZE=$(( SIZE - 1024*1024 ))
i=$(( i + 1 ))
done

0 comments on commit cd2b029

Please sign in to comment.