got code working to download each piece of the cape code separately
diff --git a/bbb_cape/src/flasher/.gitignore b/bbb_cape/src/flasher/.gitignore
new file mode 100644
index 0000000..0c665e0
--- /dev/null
+++ b/bbb_cape/src/flasher/.gitignore
@@ -0,0 +1 @@
+stm32flash/
diff --git a/bbb_cape/src/flasher/0001-actually-calculate-and-send-a-checksum-for-individua.patch b/bbb_cape/src/flasher/0001-actually-calculate-and-send-a-checksum-for-individua.patch
new file mode 100644
index 0000000..d137326
--- /dev/null
+++ b/bbb_cape/src/flasher/0001-actually-calculate-and-send-a-checksum-for-individua.patch
@@ -0,0 +1,43 @@
+From f0eaeff48adec36922c610402557e623849fa447 Mon Sep 17 00:00:00 2001
+From: Brian Silverman <brians@dell-inspiron-linux.dlinkrouter>
+Date: Tue, 17 Dec 2013 16:38:20 -0800
+Subject: [PATCH] actually calculate and send a checksum for individual page
+ erases
+
+There was a comment saying that is unnecessary, but apparently it is
+necessary on an STM32F205 with bootloader version 0x31.
+---
+ stm32.c | 10 +++++++---
+ 1 file changed, 7 insertions(+), 3 deletions(-)
+
+diff --git a/stm32.c b/stm32.c
+index 1af3650..7034c6f 100644
+--- a/stm32.c
++++ b/stm32.c
+@@ -429,8 +429,12 @@ char stm32_erase_memory(const stm32_t *stm, uint8_t spage, uint8_t pages) {
+ uint8_t pg_byte;
+ uint8_t cs = 0;
+
+- stm32_send_byte(stm, pages >> 8); // Number of pages to be erased, two bytes, MSB first
+- stm32_send_byte(stm, pages & 0xFF);
++ pg_byte = pages >> 8;
++ stm32_send_byte(stm, pg_byte); // Number of pages to be erased, two bytes, MSB first
++ cs ^= pg_byte;
++ pg_byte = pages & 0xFF;
++ stm32_send_byte(stm, pg_byte);
++ cs ^= pg_byte;
+
+ for (pg_num = 0; pg_num <= pages; pg_num++) {
+ pg_byte = pg_num >> 8;
+@@ -440,7 +444,7 @@ char stm32_erase_memory(const stm32_t *stm, uint8_t spage, uint8_t pages) {
+ cs ^= pg_byte;
+ stm32_send_byte(stm, pg_byte);
+ }
+- stm32_send_byte(stm, 0x00); // Ought to need to hand over a valid checksum here...but 0 seems to work!
++ stm32_send_byte(stm, cs);
+
+ if (stm32_read_byte(stm) != STM32_ACK) {
+ fprintf(stderr, "Page-by-page erase failed. Check the maximum pages your device supports.\n");
+--
+1.7.10.4
+
diff --git a/bbb_cape/src/flasher/build.sh b/bbb_cape/src/flasher/build.sh
new file mode 100755
index 0000000..7e3ddd5
--- /dev/null
+++ b/bbb_cape/src/flasher/build.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+set -e
+
+[[ -d stm32flash ]] || ( git clone https://git.gitorious.org/stm32flash/stm32flash.git stm32flash &&
+ cd stm32flash && git checkout 16fbfe6e5854dc36f41712f60b2282cde7571afd && patch -p1 < ../0001-actually-calculate-and-send-a-checksum-for-individua.patch )
+
+../../../aos/build/build.sh atom flasher.gyp no "$@"
diff --git a/bbb_cape/src/flasher/flasher.gyp b/bbb_cape/src/flasher/flasher.gyp
new file mode 100644
index 0000000..c9e190e
--- /dev/null
+++ b/bbb_cape/src/flasher/flasher.gyp
@@ -0,0 +1,37 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'All',
+ 'type': 'none',
+ 'dependencies': [
+ 'stm32_flasher',
+ ],
+ },
+ {
+ 'target_name': 'stm32_flasher',
+ 'type': 'executable',
+ 'sources': [
+ 'stm32_flasher.cc',
+ ],
+ 'dependencies': [
+ 'stm32flash',
+ '<(AOS)/build/aos.gyp:logging',
+ ],
+ },
+ {
+ 'target_name': 'stm32flash',
+ 'type': 'static_library',
+ 'sources': [
+ 'stm32flash/init.c',
+ 'stm32flash/parsers/hex.c',
+ 'stm32flash/serial_common.c',
+ 'stm32flash/serial_platform.c',
+ 'stm32flash/utils.c',
+ 'stm32flash/stm32.c',
+ ],
+ 'cflags': [
+ '-Wno-error',
+ ],
+ },
+ ],
+}
diff --git a/bbb_cape/src/flasher/stm32_flasher.cc b/bbb_cape/src/flasher/stm32_flasher.cc
new file mode 100644
index 0000000..68efd0e
--- /dev/null
+++ b/bbb_cape/src/flasher/stm32_flasher.cc
@@ -0,0 +1,186 @@
+#include <libgen.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <string>
+
+#include "aos/common/logging/logging.h"
+#include "aos/common/logging/logging_impl.h"
+
+extern "C" {
+#include "stm32flash/parsers/parser.h"
+#include "stm32flash/parsers/hex.h"
+#include "stm32flash/serial.h"
+#include "stm32flash/stm32.h"
+#include "stm32flash/init.h"
+}
+
+int main(int argc, char **argv) {
+ ::aos::logging::Init();
+
+ if (argc < 2) {
+ fputs("Need an argument saying which target to download.\n", stderr);
+ return 1;
+ }
+ ::std::string target = argv[1];
+
+ ::std::string device = "/dev/ttyUSB0";
+ serial_baud_t baud_rate = SERIAL_BAUD_57600;
+
+ ::std::string filename =
+ ::std::string(dirname(strdup(argv[0]))) +
+ "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
+
+ int file = open(filename.c_str(), O_RDONLY);
+ if (file == -1) {
+ LOG(FATAL, "open(%s, O_RDONLY) failed with %d: %s\n",
+ filename.c_str(), errno, strerror(errno));
+ }
+
+ uint16_t start_address = 0;
+ {
+ uint8_t buffer[1 /* : */ + 2 /* record size */ + 4 /* address */ +
+ 2 /* record type */];
+ ssize_t bytes = read(file, buffer, sizeof(buffer));
+ if (close(file) == -1) {
+ LOG(FATAL, "close(%d) failed with %d: %s\n",
+ file, errno, strerror(errno));
+ }
+ if (bytes != sizeof(buffer)) {
+ LOG(FATAL, "read %zd bytes from %s instead of %zu\n",
+ bytes, filename.c_str(), sizeof(buffer));
+ }
+ if (buffer[0] != ':' || buffer[7] != '0' || buffer[8] != '0') {
+ LOG(FATAL, "%s isn't a valid hex file that we know how to handle\n",
+ filename.c_str());
+ }
+ for (int i = 0; i < 4; ++i) {
+ uint8_t digit = buffer[3 + i];
+ int value;
+ if (digit >= '0' && digit <= '9') {
+ value = digit - '0';
+ } else if (digit >= 'a' && digit <= 'f') {
+ value = digit - 'a';
+ } else if (digit >= 'A' && digit <= 'F') {
+ value = digit - 'A';
+ } else {
+ LOG(FATAL, "unknown hex digit %c\n", digit);
+ }
+ start_address |= value << (12 - (4 * i));
+ }
+ printf("%x\n", start_address);
+ }
+
+ parser_t *parser = &PARSER_HEX;
+ void *p_st = parser->init();
+ if (p_st == NULL) {
+ LOG(FATAL, "%s parser failed to initialize.\n", parser->name);
+ }
+ if (parser->open(p_st, filename.c_str(), 0) != PARSER_ERR_OK) {
+ LOG(FATAL, "opening file %s failed\n", filename.c_str());
+ }
+
+ serial_t *serial = serial_open(device.c_str());
+ if (serial == NULL) {
+ LOG(FATAL, "failed to open serial port %s because of %d: %s\n",
+ device.c_str(), errno, strerror(errno));
+ }
+ if (serial_setup(serial, baud_rate,
+ SERIAL_BITS_8,
+ SERIAL_PARITY_EVEN,
+ SERIAL_STOPBIT_1) != SERIAL_ERR_OK) {
+ LOG(FATAL, "setting up serial port %s failed because of %d: %s\n",
+ device.c_str(), errno, strerror(errno));
+ }
+ LOG(INFO, "serial configuration: %s\n", serial_get_setup_str(serial));
+
+ if (init_bl_entry(serial, NULL /* GPIO sequence */) == 0) {
+ LOG(FATAL, "init_bl_entry(%p, NULL) failed\n", serial);
+ }
+ stm32_t *stm = stm32_init(serial, true);
+ if (stm == NULL) {
+ LOG(FATAL, "stm32_init(%p, true) failed\n", serial);
+ }
+
+ unsigned int last_byte = parser->size(p_st);
+ unsigned int size = last_byte - start_address;
+ printf("%x\n", size);
+
+ // An array of the sizes of each sector.
+ static const uint32_t kSectorSizes[12] = {0x4000, 0x4000, 0x4000, 0x4000,
+ 0x10000, 0x20000, 0x20000, 0x20000,
+ 0x20000, 0x20000, 0x20000, 0x20000};
+ static const int kNumSectors = sizeof(kSectorSizes) / sizeof(kSectorSizes[0]);
+ // The sector number that we're going to start writing at.
+ int start_sector = 0;
+ for (uint32_t address = 0; start_sector <= kNumSectors;
+ address += kSectorSizes[start_sector++]) {
+ if (start_sector == kNumSectors) {
+ LOG(FATAL, "start address %x is too big\n", start_address);
+ }
+ if (address > start_address) {
+ LOG(FATAL, "start address %x is not on a sector boundary\n",
+ start_address);
+ }
+ if (address == start_address) break;
+ }
+ printf("starting with sector %d\n", start_sector);
+
+ // The first sector that we're not going to erase.
+ int end_sector = 0;
+ for (uint32_t address = 0; end_sector <= kNumSectors;
+ address += kSectorSizes[end_sector++]) {
+ if (address > start_address + size) break;
+ if (end_sector == kNumSectors) {
+ LOG(FATAL, "%x bytes beyond start address of %x is too big\n",
+ size, start_address);
+ }
+ }
+ printf("not erasing sector %d\n", end_sector);
+
+ if (!stm32_erase_memory(stm, start_sector,
+ end_sector - start_sector - 1 /* you send the
+ bootloader N-1 */)) {
+ LOG(FATAL, "failed to erase memory\n");
+ }
+
+ uint32_t kFlashStart = 0x08000000;
+
+ uint8_t buffer[256]; // 256 is the biggest size supported
+ uint32_t completed = 0;
+ while (completed < size) {
+ uint32_t address = start_address + completed;
+ uint32_t length = ::std::min(size - completed, sizeof(buffer));
+ if (parser->read(p_st, buffer, &length) != PARSER_ERR_OK) {
+ LOG(FATAL, "reading file failed\n");
+ }
+ if (length == 0) {
+ LOG(FATAL, "failed to read input file\n");
+ }
+ if (!stm32_write_memory(stm, kFlashStart + address, buffer, length)) {
+ LOG(FATAL, "failed to write memory at address 0x%x\n", address);
+ }
+ uint8_t compare_buffer[sizeof(buffer)];
+ if (!stm32_read_memory(stm, kFlashStart + address, compare_buffer,
+ length)) {
+ LOG(FATAL, "failed to read memory at address 0x%x\n", address);
+ }
+ if (memcmp(buffer, compare_buffer, length) != 0) {
+ LOG(FATAL, "verify from 0x%x to 0x%x failed\n",
+ address, address + length);
+ }
+ completed += length;
+ printf("\rWrote and verified 0x%08x/0x%08x",
+ completed, size);
+ fflush(stdout);
+ }
+ printf("\n");
+
+ if (init_bl_exit(stm, serial, NULL /* GPIO sequence */)) {
+ printf("done!\n");
+ } else {
+ LOG(FATAL, "init_bl_exit failed\n");
+ }
+}