From 762f67d99605e6fb05cb2df7e15bdced9d33e7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 10 Dec 2019 11:40:54 +0100 Subject: [PATCH] [ion/device] Flash driver handles Internal and external flash --- .../config/{flash.h => internal_flash.h} | 6 +- ion/src/device/n0100/drivers/reset.cpp | 4 +- .../config/{flash.h => internal_flash.h} | 6 +- ion/src/device/shared/drivers/Makefile | 1 + ion/src/device/shared/drivers/exam_mode.cpp | 1 + ion/src/device/shared/drivers/flash.cpp | 258 ++---------------- ion/src/device/shared/drivers/flash.h | 13 +- .../device/shared/drivers/internal_flash.cpp | 256 +++++++++++++++++ .../device/shared/drivers/internal_flash.h | 27 ++ ion/src/device/shared/usb/Makefile | 1 + ion/src/device/shared/usb/dfu_interface.cpp | 31 +-- ion/src/device/shared/usb/stack/device.cpp | 4 +- 12 files changed, 337 insertions(+), 271 deletions(-) rename ion/src/device/n0100/drivers/config/{flash.h => internal_flash.h} (79%) rename ion/src/device/n0110/drivers/config/{flash.h => internal_flash.h} (75%) create mode 100644 ion/src/device/shared/drivers/internal_flash.cpp create mode 100644 ion/src/device/shared/drivers/internal_flash.h diff --git a/ion/src/device/n0100/drivers/config/flash.h b/ion/src/device/n0100/drivers/config/internal_flash.h similarity index 79% rename from ion/src/device/n0100/drivers/config/flash.h rename to ion/src/device/n0100/drivers/config/internal_flash.h index aa23e3135..002ed92ce 100644 --- a/ion/src/device/n0100/drivers/config/flash.h +++ b/ion/src/device/n0100/drivers/config/internal_flash.h @@ -1,11 +1,11 @@ -#ifndef ION_DEVICE_N0100_CONFIG_FLASH_H -#define ION_DEVICE_N0100_CONFIG_FLASH_H +#ifndef ION_DEVICE_N0100_CONFIG_INTERNAL_FLASH_H +#define ION_DEVICE_N0100_CONFIG_INTERNAL_FLASH_H #include namespace Ion { namespace Device { -namespace Flash { +namespace InternalFlash { namespace Config { constexpr static uint32_t StartAddress = 0x08000000; diff --git a/ion/src/device/n0100/drivers/reset.cpp b/ion/src/device/n0100/drivers/reset.cpp index c64afb379..9bcc93695 100644 --- a/ion/src/device/n0100/drivers/reset.cpp +++ b/ion/src/device/n0100/drivers/reset.cpp @@ -1,5 +1,5 @@ #include -#include "config/flash.h" +#include "config/internal_flash.h" namespace Ion { namespace Device { @@ -10,7 +10,7 @@ void coreWhilePlugged() { * might be plugged in. Doing a full core reset would result in the device * entering the ST DFU bootloader. By performing a jump-reset, we mimic the * core reset without entering ST bootloader.*/ - jump(Flash::Config::StartAddress); + jump(InternalFlash::Config::StartAddress); } } diff --git a/ion/src/device/n0110/drivers/config/flash.h b/ion/src/device/n0110/drivers/config/internal_flash.h similarity index 75% rename from ion/src/device/n0110/drivers/config/flash.h rename to ion/src/device/n0110/drivers/config/internal_flash.h index 2c50a8fcf..fc494c00d 100644 --- a/ion/src/device/n0110/drivers/config/flash.h +++ b/ion/src/device/n0110/drivers/config/internal_flash.h @@ -1,11 +1,11 @@ -#ifndef ION_DEVICE_N0110_CONFIG_FLASH_H -#define ION_DEVICE_N0110_CONFIG_FLASH_H +#ifndef ION_DEVICE_N0110_CONFIG_INTERNAL_FLASH_H +#define ION_DEVICE_N0110_CONFIG_INTERNAL_FLASH_H #include namespace Ion { namespace Device { -namespace Flash { +namespace InternalFlash { namespace Config { constexpr static uint32_t StartAddress = 0x08000000; diff --git a/ion/src/device/shared/drivers/Makefile b/ion/src/device/shared/drivers/Makefile index 4d21b6382..2b3b71efb 100644 --- a/ion/src/device/shared/drivers/Makefile +++ b/ion/src/device/shared/drivers/Makefile @@ -9,6 +9,7 @@ ion_device_src += $(addprefix ion/src/device/shared/drivers/, \ exam_mode.cpp \ external_flash.cpp \ flash.cpp \ + internal_flash.cpp \ keyboard.cpp \ led.cpp \ power.cpp\ diff --git a/ion/src/device/shared/drivers/exam_mode.cpp b/ion/src/device/shared/drivers/exam_mode.cpp index 9d9caa322..3096ad526 100644 --- a/ion/src/device/shared/drivers/exam_mode.cpp +++ b/ion/src/device/shared/drivers/exam_mode.cpp @@ -1,5 +1,6 @@ #include #include "flash.h" +#include namespace Ion { namespace ExamMode { diff --git a/ion/src/device/shared/drivers/flash.cpp b/ion/src/device/shared/drivers/flash.cpp index 2ae24eb97..6f569cd89 100644 --- a/ion/src/device/shared/drivers/flash.cpp +++ b/ion/src/device/shared/drivers/flash.cpp @@ -1,253 +1,53 @@ #include "flash.h" -#include -#include +#include "external_flash.h" +#include "internal_flash.h" +#include +#include #include namespace Ion { namespace Device { namespace Flash { -using namespace Regs; - -static inline void wait() { - /* Issue a DSB instruction to guarantee the completion of a previous access - * to FLASH_CR register or data write operation. (RM0431) */ - Cache::dsb(); - // Wait for pending Flash operations to complete - while (FLASH.SR()->getBSY()) { - } -} - -static void open() { - // Unlock the Flash configuration register if needed - if (FLASH.CR()->getLOCK()) { - FLASH.KEYR()->set(0x45670123); - FLASH.KEYR()->set(0xCDEF89AB); - } - assert(FLASH.CR()->getLOCK() == false); - - // Set the programming parallelism - FLASH.CR()->setPSIZE(MemoryAccessWidth); -} - -static void close() { - // Clear error flags - class FLASH::SR sr(0); - // Error flags are cleared by writing 1 - sr.setERSERR(true); - sr.setPGPERR(true); - sr.setPGAERR(true); - sr.setWRPERR(true); - sr.setEOP(true); - FLASH.SR()->set(sr); - - // Lock the Flash configuration register - assert(!FLASH.CR()->getMER()); - assert(!FLASH.CR()->getSER()); - assert(!FLASH.CR()->getPG()); - FLASH.CR()->setLOCK(true); - - // Purge Data and instruction cache -#if REGS_FLASH_CONFIG_ART - if (FLASH.ACR()->getARTEN()) { - FLASH.ACR()->setARTEN(false); - FLASH.ACR()->setARTRST(true); - FLASH.ACR()->setARTRST(false); - FLASH.ACR()->setARTEN(true); - } -#else - if (FLASH.ACR()->getDCEN()) { - FLASH.ACR()->setDCEN(false); - FLASH.ACR()->setDCRST(true); - FLASH.ACR()->setDCRST(false); - FLASH.ACR()->setDCEN(true); - } - if (FLASH.ACR()->getICEN()) { - FLASH.ACR()->setICEN(false); - FLASH.ACR()->setICRST(true); - FLASH.ACR()->setICRST(false); - FLASH.ACR()->setICEN(true); - } -#endif -} - -// Compile-time log2 -static inline constexpr size_t clog2(size_t input) { - return (input == 1) ? 0 : clog2(input/2)+1; -} - -// Align a pointer to a given type's boundaries -// Returns a value that is lower or equal to input -template -static inline T * align(void * input) { - size_t k = clog2(sizeof(T)); - return reinterpret_cast(reinterpret_cast(input) & ~((1< -static inline T eat(void * ptr) { - T * pointer = *reinterpret_cast(ptr); - T result = *pointer; - *reinterpret_cast(ptr) = pointer+1; - return result; -} - -static inline ptrdiff_t byte_offset(void * p1, void * p2) { - return reinterpret_cast(p2) - reinterpret_cast(p1); -} - -template -static inline T min(T i, T j) { - return (i| - * |-- HeaderDelta ->| - */ - - MemoryAccessType * alignedDestination = align(destination); - ptrdiff_t headerDelta = byte_offset(alignedDestination, destination); - assert(headerDelta >= 0 && headerDelta < static_cast(sizeof(MemoryAccessType))); - - if (headerDelta > 0) { - // At this point, alignedDestination < destination - // We'll then retrieve the current value at alignedDestination, fill it with - // bytes from source, and write it back at alignedDestination. - - // First, retrieve the current value at alignedDestination - MemoryAccessType header = *alignedDestination; - - // Then copy headerLength bytes from source and put them in the header - uint8_t * headerStart = reinterpret_cast(&header); - // Here's where source data shall start being copied in the header - uint8_t * headerDataStart = headerStart + headerDelta; - // And here's where it should end - uint8_t * headerDataEnd = min( - headerStart + sizeof(MemoryAccessType), // Either at the end of the header - headerDataStart + length // or whenever src runs out of data - ); - for (uint8_t * h = headerDataStart; h(&source); - } - - // Then eventually write the header back into the aligned destination - *alignedDestination++ = header; - wait(); - } - - /* Step 2 - Copy the bulk of the data - * At this point, we can use aligned MemoryAccessType pointers. */ - - MemoryAccessType * lastAlignedDestination = align(destination + length); - while (alignedDestination < lastAlignedDestination) { - *alignedDestination++ = eat(&source); - wait(); - } - - /* Step 3 - Copy a footer if needed - * Some unaligned data can be pending at the end. Let's take care of it like - * we did for the header. - * - * _alignedDst _Destination+length - * | | - * --+--------+--------+--------+--------+--------+--------+-- - * | || | | | || | - *---+--------+--------+--------+--------+--------+--------+-- - * |<------------ Footer ------------->| - * |- footerLength ->| - */ - - ptrdiff_t footerLength = byte_offset(alignedDestination, destination + length); - assert(footerLength < static_cast(sizeof(MemoryAccessType))); - if (footerLength > 0) { - assert(alignedDestination == lastAlignedDestination); - - // First, retrieve the current value at alignedDestination - MemoryAccessType footer = *alignedDestination; - - /* Then copy footerLength bytes from source and put them at the beginning of - * the footer */ - uint8_t * footerPointer = reinterpret_cast(&footer); - for (ptrdiff_t i=0; i(&source); - } - - // Then eventually write the footer back into the aligned destination - *alignedDestination = footer; - wait(); - } +int TotalNumberOfSectors() { + return InternalFlash::Config::NumberOfSectors + ExternalFlash::Config::NumberOfSectors; } int SectorAtAddress(uint32_t address) { - for (int i = 0; i < Config::NumberOfSectors; i++) { - if (address >= Config::SectorAddresses[i] && address < Config::SectorAddresses[i+1]) { - return i; - } + if (address >= InternalFlash::Config::StartAddress + && address <= InternalFlash::Config::EndAddress) + { + return InternalFlash::SectorAtAddress(address); + } + if (address >= ExternalFlash::Config::StartAddress + && address <= ExternalFlash::Config::EndAddress) + { + return InternalFlash::Config::NumberOfSectors + ExternalFlash::SectorAtAddress(address - ExternalFlash::Config::StartAddress); } return -1; } void MassErase() { - open(); - FLASH.CR()->setMER(true); - FLASH.CR()->setSTRT(true); - wait(); - FLASH.CR()->setMER(false); - close(); + InternalFlash::MassErase(); + ExternalFlash::MassErase(); } void EraseSector(int i) { - assert(i >= 0 && i < Config::NumberOfSectors); - open(); - FLASH.CR()->setSNB(i); - FLASH.CR()->setSER(true); - FLASH.CR()->setSTRT(true); - wait(); - FLASH.CR()->setSNB(0); - FLASH.CR()->setSER(false); - close(); + assert(i >= 0 && i < TotalNumberOfSectors()); + if (i < InternalFlash::Config::NumberOfSectors) { + InternalFlash::EraseSector(i); + } else { + ExternalFlash::EraseSector(i - InternalFlash::Config::NumberOfSectors); + } } void WriteMemory(uint8_t * destination, uint8_t * source, size_t length) { - open(); - FLASH.CR()->setPG(true); - flash_memcpy(destination, source, length); - FLASH.CR()->setPG(false); - close(); + assert(SectorAtAddress((uint32_t)destination) >= 0); + if (SectorAtAddress((uint32_t)destination) < InternalFlash::Config::NumberOfSectors) { + InternalFlash::WriteMemory(destination, source, length); + } else { + ExternalFlash::WriteMemory(destination - ExternalFlash::Config::StartAddress, source, length); + } } } diff --git a/ion/src/device/shared/drivers/flash.h b/ion/src/device/shared/drivers/flash.h index ebb3ffc4f..4e61b1fa9 100644 --- a/ion/src/device/shared/drivers/flash.h +++ b/ion/src/device/shared/drivers/flash.h @@ -2,24 +2,19 @@ #define ION_DEVICE_SHARED_FLASH_H #include -#include +#include namespace Ion { namespace Device { namespace Flash { -void MassErase(); - +int TotalNumberOfSectors(); int SectorAtAddress(uint32_t address); + +void MassErase(); void EraseSector(int i); - void WriteMemory(uint8_t * destination, uint8_t * source, size_t length); -/* The Device is powered by a 2.8V LDO. This allows us to perform writes to the - * Flash 32 bits at once. */ -constexpr Regs::FLASH::CR::PSIZE MemoryAccessWidth = Regs::FLASH::CR::PSIZE::X32; -typedef uint32_t MemoryAccessType; - } } } diff --git a/ion/src/device/shared/drivers/internal_flash.cpp b/ion/src/device/shared/drivers/internal_flash.cpp new file mode 100644 index 000000000..9ff5e1432 --- /dev/null +++ b/ion/src/device/shared/drivers/internal_flash.cpp @@ -0,0 +1,256 @@ +#include "internal_flash.h" +#include +#include +#include + +namespace Ion { +namespace Device { +namespace InternalFlash { + +using namespace Regs; + +static inline void wait() { + /* Issue a DSB instruction to guarantee the completion of a previous access + * to FLASH_CR register or data write operation. (RM0431) */ + Cache::dsb(); + // Wait for pending Flash operations to complete + while (FLASH.SR()->getBSY()) { + } +} + +static void open() { + // Unlock the Flash configuration register if needed + if (FLASH.CR()->getLOCK()) { + FLASH.KEYR()->set(0x45670123); + FLASH.KEYR()->set(0xCDEF89AB); + } + assert(FLASH.CR()->getLOCK() == false); + + // Set the programming parallelism + FLASH.CR()->setPSIZE(MemoryAccessWidth); +} + +static void close() { + // Clear error flags + class FLASH::SR sr(0); + // Error flags are cleared by writing 1 + sr.setERSERR(true); + sr.setPGPERR(true); + sr.setPGAERR(true); + sr.setWRPERR(true); + sr.setEOP(true); + FLASH.SR()->set(sr); + + // Lock the Flash configuration register + assert(!FLASH.CR()->getMER()); + assert(!FLASH.CR()->getSER()); + assert(!FLASH.CR()->getPG()); + FLASH.CR()->setLOCK(true); + + // Purge Data and instruction cache +#if REGS_FLASH_CONFIG_ART + if (FLASH.ACR()->getARTEN()) { + FLASH.ACR()->setARTEN(false); + FLASH.ACR()->setARTRST(true); + FLASH.ACR()->setARTRST(false); + FLASH.ACR()->setARTEN(true); + } +#else + if (FLASH.ACR()->getDCEN()) { + FLASH.ACR()->setDCEN(false); + FLASH.ACR()->setDCRST(true); + FLASH.ACR()->setDCRST(false); + FLASH.ACR()->setDCEN(true); + } + if (FLASH.ACR()->getICEN()) { + FLASH.ACR()->setICEN(false); + FLASH.ACR()->setICRST(true); + FLASH.ACR()->setICRST(false); + FLASH.ACR()->setICEN(true); + } +#endif +} + +// Compile-time log2 +static inline constexpr size_t clog2(size_t input) { + return (input == 1) ? 0 : clog2(input/2)+1; +} + +// Align a pointer to a given type's boundaries +// Returns a value that is lower or equal to input +template +static inline T * align(void * input) { + size_t k = clog2(sizeof(T)); + return reinterpret_cast(reinterpret_cast(input) & ~((1< +static inline T eat(void * ptr) { + T * pointer = *reinterpret_cast(ptr); + T result = *pointer; + *reinterpret_cast(ptr) = pointer+1; + return result; +} + +static inline ptrdiff_t byte_offset(void * p1, void * p2) { + return reinterpret_cast(p2) - reinterpret_cast(p1); +} + +template +static inline T min(T i, T j) { + return (i| + * |-- HeaderDelta ->| + */ + + MemoryAccessType * alignedDestination = align(destination); + ptrdiff_t headerDelta = byte_offset(alignedDestination, destination); + assert(headerDelta >= 0 && headerDelta < static_cast(sizeof(MemoryAccessType))); + + if (headerDelta > 0) { + // At this point, alignedDestination < destination + // We'll then retrieve the current value at alignedDestination, fill it with + // bytes from source, and write it back at alignedDestination. + + // First, retrieve the current value at alignedDestination + MemoryAccessType header = *alignedDestination; + + // Then copy headerLength bytes from source and put them in the header + uint8_t * headerStart = reinterpret_cast(&header); + // Here's where source data shall start being copied in the header + uint8_t * headerDataStart = headerStart + headerDelta; + // And here's where it should end + uint8_t * headerDataEnd = min( + headerStart + sizeof(MemoryAccessType), // Either at the end of the header + headerDataStart + length // or whenever src runs out of data + ); + for (uint8_t * h = headerDataStart; h(&source); + } + + // Then eventually write the header back into the aligned destination + *alignedDestination++ = header; + wait(); + } + + /* Step 2 - Copy the bulk of the data + * At this point, we can use aligned MemoryAccessType pointers. */ + + MemoryAccessType * lastAlignedDestination = align(destination + length); + while (alignedDestination < lastAlignedDestination) { + *alignedDestination++ = eat(&source); + wait(); + } + + /* Step 3 - Copy a footer if needed + * Some unaligned data can be pending at the end. Let's take care of it like + * we did for the header. + * + * _alignedDst _Destination+length + * | | + * --+--------+--------+--------+--------+--------+--------+-- + * | || | | | || | + *---+--------+--------+--------+--------+--------+--------+-- + * |<------------ Footer ------------->| + * |- footerLength ->| + */ + + ptrdiff_t footerLength = byte_offset(alignedDestination, destination + length); + assert(footerLength < static_cast(sizeof(MemoryAccessType))); + if (footerLength > 0) { + assert(alignedDestination == lastAlignedDestination); + + // First, retrieve the current value at alignedDestination + MemoryAccessType footer = *alignedDestination; + + /* Then copy footerLength bytes from source and put them at the beginning of + * the footer */ + uint8_t * footerPointer = reinterpret_cast(&footer); + for (ptrdiff_t i=0; i(&source); + } + + // Then eventually write the footer back into the aligned destination + *alignedDestination = footer; + wait(); + } +} + +int SectorAtAddress(uint32_t address) { + for (int i = 0; i < Config::NumberOfSectors; i++) { + if (address >= Config::SectorAddresses[i] && address < Config::SectorAddresses[i+1]) { + return i; + } + } + return -1; +} + +void MassErase() { + open(); + FLASH.CR()->setMER(true); + FLASH.CR()->setSTRT(true); + wait(); + FLASH.CR()->setMER(false); + close(); +} + + +void EraseSector(int i) { + assert(i >= 0 && i < Config::NumberOfSectors); + open(); + FLASH.CR()->setSNB(i); + FLASH.CR()->setSER(true); + FLASH.CR()->setSTRT(true); + wait(); + FLASH.CR()->setSNB(0); + FLASH.CR()->setSER(false); + close(); +} + +void WriteMemory(uint8_t * destination, uint8_t * source, size_t length) { + open(); + FLASH.CR()->setPG(true); + flash_memcpy(destination, source, length); + FLASH.CR()->setPG(false); + close(); +} + +} +} +} diff --git a/ion/src/device/shared/drivers/internal_flash.h b/ion/src/device/shared/drivers/internal_flash.h new file mode 100644 index 000000000..befe83b3e --- /dev/null +++ b/ion/src/device/shared/drivers/internal_flash.h @@ -0,0 +1,27 @@ +#ifndef ION_DEVICE_SHARED_INTERNAL_FLASH_H +#define ION_DEVICE_SHARED_INTERNAL_FLASH_H + +#include +#include + +namespace Ion { +namespace Device { +namespace InternalFlash { + +void MassErase(); + +int SectorAtAddress(uint32_t address); +void EraseSector(int i); + +void WriteMemory(uint8_t * destination, uint8_t * source, size_t length); + +/* The Device is powered by a 2.8V LDO. This allows us to perform writes to the + * Flash 32 bits at once. */ +constexpr Regs::FLASH::CR::PSIZE MemoryAccessWidth = Regs::FLASH::CR::PSIZE::X32; +typedef uint32_t MemoryAccessType; + +} +} +} + +#endif diff --git a/ion/src/device/shared/usb/Makefile b/ion/src/device/shared/usb/Makefile index cf2f0cbf7..df9d1f6c2 100644 --- a/ion/src/device/shared/usb/Makefile +++ b/ion/src/device/shared/usb/Makefile @@ -55,6 +55,7 @@ dfu_src += $(addprefix ion/src/device/shared/drivers/, \ events_keyboard_platform.cpp \ external_flash.cpp \ flash.cpp \ + internal_flash.cpp \ keyboard.cpp \ led.cpp \ power.cpp\ diff --git a/ion/src/device/shared/usb/dfu_interface.cpp b/ion/src/device/shared/usb/dfu_interface.cpp index c221dae35..5316f7b46 100644 --- a/ion/src/device/shared/usb/dfu_interface.cpp +++ b/ion/src/device/shared/usb/dfu_interface.cpp @@ -1,18 +1,12 @@ #include "dfu_interface.h" -#include #include #include -#include -#include -#include #include namespace Ion { namespace Device { namespace USB { -using namespace Ion::Device::Regs; - static inline uint32_t minUint32T(uint32_t x, uint32_t y) { return x < y ? x : y; } void DFUInterface::StatusData::push(Channel * c) const { @@ -188,7 +182,7 @@ void DFUInterface::eraseCommand(uint8_t * transferBuffer, uint16_t transferBuffe if (transferBufferLength == 1) { // Mass erase - m_erasePage = Flash::Config::NumberOfSectors + ExternalFlash::Config::NumberOfSectors; + m_erasePage = Flash::TotalNumberOfSectors(); return; } @@ -200,11 +194,8 @@ void DFUInterface::eraseCommand(uint8_t * transferBuffer, uint16_t transferBuffe + (transferBuffer[3] << 16) + (transferBuffer[4] << 24); - if (eraseAddress >= Flash::Config::StartAddress && eraseAddress <= Flash::Config::EndAddress) { - m_erasePage = Flash::SectorAtAddress(eraseAddress); - } else if (eraseAddress >= ExternalFlash::Config::StartAddress && eraseAddress <= ExternalFlash::Config::EndAddress) { - m_erasePage = Flash::Config::NumberOfSectors + ExternalFlash::SectorAtAddress(eraseAddress - ExternalFlash::Config::StartAddress); - } else { + m_erasePage = Flash::SectorAtAddress(eraseAddress); + if (m_erasePage < 0) { // Unrecognized sector m_state = State::dfuERROR; m_status = Status::errTARGET; @@ -218,13 +209,10 @@ void DFUInterface::eraseMemoryIfNeeded() { return; } - if (m_erasePage == Flash::Config::NumberOfSectors + ExternalFlash::Config::NumberOfSectors) { + if (m_erasePage == Flash::TotalNumberOfSectors()) { Flash::MassErase(); - ExternalFlash::MassErase(); - } else if (m_erasePage < Flash::Config::NumberOfSectors) { - Flash::EraseSector(m_erasePage); } else { - ExternalFlash::EraseSector(m_erasePage - Flash::Config::NumberOfSectors); + Flash::EraseSector(m_erasePage); } /* Put an out of range value in m_erasePage to indicate that no erase is @@ -235,15 +223,12 @@ void DFUInterface::eraseMemoryIfNeeded() { } void DFUInterface::writeOnMemory() { - if (m_writeAddress >= Flash::Config::StartAddress && m_writeAddress <= Flash::Config::EndAddress) { - // Write to the Flash memory - Flash::WriteMemory(reinterpret_cast(m_writeAddress), m_largeBuffer, m_largeBufferLength); - } else if (m_writeAddress >= k_sramStartAddress && m_writeAddress <= k_sramEndAddress) { + if (m_writeAddress >= k_sramStartAddress && m_writeAddress <= k_sramEndAddress) { // Write on SRAM // FIXME We should check that we are not overriding the current instructions. memcpy((void *)m_writeAddress, m_largeBuffer, m_largeBufferLength); - } else if (m_writeAddress >= ExternalFlash::Config::StartAddress && m_writeAddress <= ExternalFlash::Config::EndAddress) { - ExternalFlash::WriteMemory(reinterpret_cast(m_writeAddress) - ExternalFlash::Config::StartAddress, m_largeBuffer, m_largeBufferLength); + } else if (Flash::SectorAtAddress(m_writeAddress) >= 0) { + Flash::WriteMemory(reinterpret_cast(m_writeAddress), m_largeBuffer, m_largeBufferLength); } else { // Invalid write address m_largeBufferLength = 0; diff --git a/ion/src/device/shared/usb/stack/device.cpp b/ion/src/device/shared/usb/stack/device.cpp index c0b72dc9e..294b647e1 100644 --- a/ion/src/device/shared/usb/stack/device.cpp +++ b/ion/src/device/shared/usb/stack/device.cpp @@ -1,5 +1,5 @@ #include "device.h" -#include +#include #include #include @@ -92,7 +92,7 @@ void Device::detach() { } void Device::leave(uint32_t leaveAddress) { - if (leaveAddress == Ion::Device::Flash::Config::StartAddress) { + if (leaveAddress == Ion::Device::InternalFlash::Config::StartAddress) { Ion::Device::Reset::coreWhilePlugged(); } else { Ion::Device::Reset::jump(leaveAddress);