diff --git a/ion/src/device/n0100/drivers/config/external_flash.h b/ion/src/device/n0100/drivers/config/external_flash.h index f239ddb72..79d39f768 100644 --- a/ion/src/device/n0100/drivers/config/external_flash.h +++ b/ion/src/device/n0100/drivers/config/external_flash.h @@ -20,7 +20,8 @@ namespace Config { using namespace Regs; -constexpr static uint32_t QSPIBaseAddress = 0; +constexpr static uint32_t StartAddress = 0xFFFFFFFF; +constexpr static uint32_t EndAddress = 0xFFFFFFFF; constexpr static int NumberOfSectors = 0; constexpr static AFGPIOPin Pins[] = {}; diff --git a/ion/src/device/n0100/drivers/config/flash.h b/ion/src/device/n0100/drivers/config/flash.h new file mode 100644 index 000000000..aa23e3135 --- /dev/null +++ b/ion/src/device/n0100/drivers/config/flash.h @@ -0,0 +1,26 @@ +#ifndef ION_DEVICE_N0100_CONFIG_FLASH_H +#define ION_DEVICE_N0100_CONFIG_FLASH_H + +#include + +namespace Ion { +namespace Device { +namespace Flash { +namespace Config { + +constexpr static uint32_t StartAddress = 0x08000000; +constexpr static uint32_t EndAddress = 0x08100000; +constexpr static int NumberOfSectors = 12; +constexpr static uint32_t SectorAddresses[NumberOfSectors+1] = { + 0x08000000, 0x08004000, 0x08008000, 0x0800C000, + 0x08010000, 0x08020000, 0x08040000, 0x08060000, + 0x08080000, 0x080A0000, 0x080C0000, 0x080E0000, + 0x08100000 +}; + +} +} +} +} + +#endif diff --git a/ion/src/device/n0101/drivers/config/external_flash.h b/ion/src/device/n0101/drivers/config/external_flash.h index d16753a00..ea77ac250 100644 --- a/ion/src/device/n0101/drivers/config/external_flash.h +++ b/ion/src/device/n0101/drivers/config/external_flash.h @@ -20,7 +20,8 @@ namespace Config { using namespace Regs; -constexpr static uint32_t QSPIBaseAddress = 0x90000000; +constexpr static uint32_t StartAddress = 0x90000000; +constexpr static uint32_t EndAddress = 0x90800000; constexpr static int NumberOfSectors = 128; constexpr static AFGPIOPin Pins[] = { AFGPIOPin(GPIOB, 2, GPIO::AFR::AlternateFunction::AF9, GPIO::PUPDR::Pull::None, GPIO::OSPEEDR::OutputSpeed::High), diff --git a/ion/src/device/n0101/drivers/config/flash.h b/ion/src/device/n0101/drivers/config/flash.h new file mode 100644 index 000000000..cfd7bbd90 --- /dev/null +++ b/ion/src/device/n0101/drivers/config/flash.h @@ -0,0 +1,24 @@ +#ifndef ION_DEVICE_N0101_CONFIG_FLASH_H +#define ION_DEVICE_N0101_CONFIG_FLASH_H + +#include + +namespace Ion { +namespace Device { +namespace Flash { +namespace Config { + +constexpr static uint32_t StartAddress = 0x08000000; +constexpr static uint32_t EndAddress = 0x08010000; +constexpr static int NumberOfSectors = 4; +constexpr static uint32_t SectorAddresses[NumberOfSectors+1] = { + 0x08000000, 0x08004000, 0x08008000, 0x0800C000, + 0x08010000 +}; + +} +} +} +} + +#endif diff --git a/ion/src/device/shared/drivers/flash.cpp b/ion/src/device/shared/drivers/flash.cpp index 7a3a0db1d..dbd36e71f 100644 --- a/ion/src/device/shared/drivers/flash.cpp +++ b/ion/src/device/shared/drivers/flash.cpp @@ -1,5 +1,6 @@ #include "flash.h" #include +#include #include namespace Ion { @@ -212,14 +213,8 @@ static void flash_memcpy(uint8_t * destination, uint8_t * source, size_t length) } int SectorAtAddress(uint32_t address) { - uint32_t sectorAddresses[NumberOfSectors+1] = { - 0x08000000, 0x08004000, 0x08008000, 0x0800C000, - 0x08010000, 0x08020000, 0x08040000, 0x08060000, - 0x08080000, 0x080A0000, 0x080C0000, 0x080E0000, - 0x08100000 - }; for (int i=0; i= sectorAddresses[i] && address < sectorAddresses[i+1]) { + if (address >= Config::SectorAddresses[i] && address < Config::SectorAddresses[i+1]) { return i; } } diff --git a/ion/src/device/shared/drivers/flash.h b/ion/src/device/shared/drivers/flash.h index b2ae1073c..25935c989 100644 --- a/ion/src/device/shared/drivers/flash.h +++ b/ion/src/device/shared/drivers/flash.h @@ -10,7 +10,7 @@ namespace Flash { void MassErase(); -constexpr int NumberOfSectors = 12; +constexpr int NumberOfSectors = 4; int SectorAtAddress(uint32_t address); void EraseSector(int i); diff --git a/ion/src/device/shared/usb/dfu_interface.cpp b/ion/src/device/shared/usb/dfu_interface.cpp index 5d4f81b01..dc915e71b 100644 --- a/ion/src/device/shared/usb/dfu_interface.cpp +++ b/ion/src/device/shared/usb/dfu_interface.cpp @@ -1,8 +1,10 @@ #include "dfu_interface.h" #include #include -#include "../drivers/flash.h" -#include "../drivers/external_flash.h" +#include +#include +#include +#include namespace Ion { namespace Device { @@ -177,7 +179,7 @@ void DFUInterface::eraseCommand(uint8_t * transferBuffer, uint16_t transferBuffe if (transferBufferLength == 1) { // Mass erase - m_erasePage = Flash::NumberOfSectors + ExternalFlash::Config::NumberOfSectors; + m_erasePage = Flash::Config::NumberOfSectors + ExternalFlash::Config::NumberOfSectors; return; } @@ -189,10 +191,10 @@ void DFUInterface::eraseCommand(uint8_t * transferBuffer, uint16_t transferBuffe + (transferBuffer[3] << 16) + (transferBuffer[4] << 24); - if (eraseAddress >= k_flashStartAddress && eraseAddress <= k_flashEndAddress) { + if (eraseAddress >= Flash::Config::StartAddress && eraseAddress <= Flash::Config::EndAddress) { m_erasePage = Flash::SectorAtAddress(eraseAddress); - } else if (eraseAddress >= k_externalFlashStartAddress && eraseAddress <= k_externalFlashEndAddress) { - m_erasePage = Flash::NumberOfSectors + ExternalFlash::SectorAtAddress(eraseAddress - k_externalFlashStartAddress); + } else if (eraseAddress >= ExternalFlash::Config::StartAddress && eraseAddress <= ExternalFlash::Config::EndAddress) { + m_erasePage = Flash::Config::NumberOfSectors + ExternalFlash::SectorAtAddress(eraseAddress - ExternalFlash::Config::StartAddress); } else { // Unrecognized sector m_state = State::dfuERROR; @@ -207,13 +209,13 @@ void DFUInterface::eraseMemoryIfNeeded() { return; } - if (m_erasePage == Flash::NumberOfSectors + ExternalFlash::Config::NumberOfSectors) { + if (m_erasePage == Flash::Config::NumberOfSectors + ExternalFlash::Config::NumberOfSectors) { Flash::MassErase(); ExternalFlash::MassErase(); - } else if (m_erasePage < Flash::NumberOfSectors) { + } else if (m_erasePage < Flash::Config::NumberOfSectors) { Flash::EraseSector(m_erasePage); } else { - ExternalFlash::EraseSector(m_erasePage - Flash::NumberOfSectors); + ExternalFlash::EraseSector(m_erasePage - Flash::Config::NumberOfSectors); } /* Put an out of range value in m_erasePage to indicate that no erase is @@ -224,15 +226,15 @@ void DFUInterface::eraseMemoryIfNeeded() { } void DFUInterface::writeOnMemory() { - if (m_writeAddress >= k_flashStartAddress && m_writeAddress <= k_flashEndAddress) { + 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) { // 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 >= k_externalFlashStartAddress && m_writeAddress <= k_externalFlashEndAddress) { - ExternalFlash::WriteMemory(reinterpret_cast(m_writeAddress) - k_externalFlashStartAddress, 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 { // Invalid write address m_largeBufferLength = 0; diff --git a/ion/src/device/shared/usb/dfu_interface.h b/ion/src/device/shared/usb/dfu_interface.h index 849fa9d98..fab74834d 100644 --- a/ion/src/device/shared/usb/dfu_interface.h +++ b/ion/src/device/shared/usb/dfu_interface.h @@ -126,12 +126,8 @@ private: /* The Flash and SRAM addresses are in flash.ld. However, dfu_interface is * linked with dfu.ld, so we cannot access the values. */ - constexpr static uint32_t k_flashStartAddress = 0x08000000; - constexpr static uint32_t k_flashEndAddress = 0x08100000; constexpr static uint32_t k_sramStartAddress = 0x20000000; constexpr static uint32_t k_sramEndAddress = 0x20040000; - constexpr static uint32_t k_externalFlashStartAddress = 0x90000000; - constexpr static uint32_t k_externalFlashEndAddress = 0x90800000; // Download and upload bool processDownloadRequest(uint16_t wLength, uint16_t * transferBufferLength); diff --git a/ion/test/external_flash.cpp b/ion/test/external_flash.cpp index 0386eeb5c..72caef964 100644 --- a/ion/test/external_flash.cpp +++ b/ion/test/external_flash.cpp @@ -7,7 +7,7 @@ // Choose some not too uniform data to program and test the external flash memory with. static inline uint8_t expected_value_at(uint8_t * ptr) { - uint32_t address = reinterpret_cast(ptr) - Ion::ExternalFlash::Device::QSPIBaseAddress; + uint32_t address = reinterpret_cast(ptr) - Ion::ExternalFlash::Device::StartAddress; return (address / 0x10000) + (address / 0x100) + address; // Example: the value expected at the address 0x123456 is 0x12 + 0x34 + 0x56. } @@ -31,8 +31,8 @@ static inline void check(volatile T * p, int repeat) { template void test(int accessType, int repeat) { - uint8_t * start = reinterpret_cast(Ion::ExternalFlash::Device::QSPIBaseAddress); - uint8_t * end = reinterpret_cast(Ion::ExternalFlash::Device::QSPIBaseAddress + Ion::ExternalFlash::Device::FlashAddressSpaceSize); + uint8_t * start = reinterpret_cast(Ion::ExternalFlash::Device::StartAddress); + uint8_t * end = reinterpret_cast(Ion::ExternalFlash::Device::StartAddress + Ion::ExternalFlash::Device::FlashAddressSpaceSize); // Forward sequential access if (accessType == 0) { @@ -52,10 +52,10 @@ void test(int accessType, int repeat) { // Random access if (accessType == 2) { - T * endT = reinterpret_cast(Ion::ExternalFlash::Device::QSPIBaseAddress + Ion::ExternalFlash::Device::FlashAddressSpaceSize); + T * endT = reinterpret_cast(Ion::ExternalFlash::Device::StartAddress + Ion::ExternalFlash::Device::FlashAddressSpaceSize); for (size_t i=0; i> (32 - Ion::ExternalFlash::Device::NumberOfAddressBitsInChip); - volatile T * q = reinterpret_cast(randomAddr + Ion::ExternalFlash::Device::QSPIBaseAddress); + volatile T * q = reinterpret_cast(randomAddr + Ion::ExternalFlash::Device::StartAddress); if (q <= endT - 1) { check(q, repeat); } @@ -101,7 +101,7 @@ QUIZ_CASE(ion_ext_flash_program) { for (int page = 0; page < (1<<15); page++) { uint8_t buffer[256]; for (int byte = 0; byte < 256; byte++) { - buffer[byte] = expected_value_at(reinterpret_cast(Ion::ExternalFlash::Device::QSPIBaseAddress + page * 256 + byte)); + buffer[byte] = expected_value_at(reinterpret_cast(Ion::ExternalFlash::Device::StartAddress + page * 256 + byte)); } Ion::ExternalFlash::Device::WriteMemory(reinterpret_cast(page * 256), buffer, 256); }