From b32f5e2239c6f9aca78bdc547073c10ef87bb64b Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Thu, 30 Jul 2015 09:41:33 -0700 Subject: [PATCH] Generalize method for executing SPI flash image in M4 RAM. --- firmware/application/m4_startup.cpp | 13 ++---- firmware/application/m4_startup.hpp | 6 ++- firmware/application/main.cpp | 4 +- firmware/common/spi_image.hpp | 68 +++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 firmware/common/spi_image.hpp diff --git a/firmware/application/m4_startup.cpp b/firmware/application/m4_startup.cpp index b2ba689e..5a705bff 100644 --- a/firmware/application/m4_startup.cpp +++ b/firmware/application/m4_startup.cpp @@ -23,15 +23,8 @@ #include "hal.h" -#include -#include #include -static constexpr uint32_t m4_text_flash_image_offset = 0x20000; -static constexpr size_t m4_text_size = 0x8000; -static constexpr uint32_t m4_text_flash_base = LPC_SPIFI_DATA_CACHED_BASE + m4_text_flash_image_offset; -static constexpr uint32_t m4_text_ram_base = 0x10080000; - /* TODO: OK, this is cool, but how do I put the M4 to sleep so I can switch to * a different image? Other than asking the old image to sleep while the M0 * makes changes? @@ -39,14 +32,14 @@ static constexpr uint32_t m4_text_ram_base = 0x10080000; * I suppose I could force M4MEMMAP to an invalid memory reason which would * cause an exception and effectively halt the M4. But that feels gross. */ -void m4_init() { +void m4_init(const portapack::spi_flash::region_t from, void* const to) { /* Initialize M4 code RAM */ - std::memcpy((void*)m4_text_ram_base, (void*)m4_text_flash_base, m4_text_size); + std::memcpy(to, from.base_address(), from.size); /* M4 core is assumed to be sleeping with interrupts off, so we can mess * with its address space and RAM without concern. */ - LPC_CREG->M4MEMMAP = m4_text_ram_base; + LPC_CREG->M4MEMMAP = reinterpret_cast(to); /* Reset M4 core */ LPC_RGU->RESET_CTRL[0] = (1 << 13); diff --git a/firmware/application/m4_startup.hpp b/firmware/application/m4_startup.hpp index 00b36e76..b4763156 100644 --- a/firmware/application/m4_startup.hpp +++ b/firmware/application/m4_startup.hpp @@ -22,6 +22,10 @@ #ifndef __M4_STARTUP_H__ #define __M4_STARTUP_H__ -void m4_init(); +#include + +#include "spi_image.hpp" + +void m4_init(const portapack::spi_flash::region_t from, void* const to); #endif/*__M4_STARTUP_H__*/ diff --git a/firmware/application/main.cpp b/firmware/application/main.cpp index 9cd811be..3af69b99 100755 --- a/firmware/application/main.cpp +++ b/firmware/application/main.cpp @@ -59,6 +59,7 @@ using namespace hackrf::one; #include "spi_pp.hpp" #include "m4_startup.hpp" +#include "spi_image.hpp" #include "debug.hpp" #include "led.hpp" @@ -649,7 +650,8 @@ context.message_map[Message::ID::FSKPacket] = [](const Message* const p) { }; m4txevent_interrupt_enable(); - m4_init(); + + m4_init(portapack::spi_flash::baseband, portapack::spi_flash::m4_text_ram_base); while(true) { const auto events = event_dispatcher.wait(); diff --git a/firmware/common/spi_image.hpp b/firmware/common/spi_image.hpp new file mode 100644 index 00000000..afc5053b --- /dev/null +++ b/firmware/common/spi_image.hpp @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc. + * + * This file is part of PortaPack. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __SPI_IMAGE_H__ +#define __SPI_IMAGE_H__ + +#include +#include + +#include "hal.h" + +namespace portapack { +namespace spi_flash { + +struct region_t { + const size_t offset; + const size_t size; + + constexpr const void* base_address() { + return reinterpret_cast(LPC_SPIFI_DATA_CACHED_BASE + offset); + } +}; + +constexpr region_t bootstrap { + .offset = 0x00000, + .size = 0x10000, +}; + +constexpr region_t hackrf { + .offset = 0x10000, + .size = 0x8000, +}; + +constexpr region_t baseband { + .offset = 0x20000, + .size = 0x8000, +}; + +constexpr region_t application { + .offset = 0x40000, + .size = 0x40000, +}; + +// TODO: Refactor into another header that defines memory regions. +constexpr void* m4_text_ram_base = reinterpret_cast(0x10080000); + +} /* namespace spi_flash */ +} /* namespace portapack */ + +#endif/*__SPI_IMAGE_H__*/