From ad9d89a7563826a493bfec4e2164f9df0e8e997b Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Mon, 14 Jan 2019 16:43:49 +0100 Subject: [PATCH] [ion/f730] Shutdown external flash --- ion/src/f730/device.cpp | 1 + ion/src/f730/external_flash.cpp | 33 +++++++++++++++++++++++++++++++-- ion/src/f730/external_flash.h | 7 ++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/ion/src/f730/device.cpp b/ion/src/f730/device.cpp index 1958d8e4c..06956b4ed 100644 --- a/ion/src/f730/device.cpp +++ b/ion/src/f730/device.cpp @@ -228,6 +228,7 @@ void shutdownPeripherals(bool keepLEDAwake) { #if USE_SD_CARD SDCard::Device::shutdown(); #endif + ExternalFlash::Device::shutdown(); USB::Device::shutdown(); Battery::Device::shutdown(); if (!keepLEDAwake) { diff --git a/ion/src/f730/external_flash.cpp b/ion/src/f730/external_flash.cpp index 0ebe55c17..46a3c99b1 100644 --- a/ion/src/f730/external_flash.cpp +++ b/ion/src/f730/external_flash.cpp @@ -1,4 +1,5 @@ #include "external_flash.h" +#include "timing.h" namespace Ion { namespace ExternalFlash { @@ -205,6 +206,12 @@ void init() { initChip(); } +void shutdown() { + shutdownChip(); + shutdownQSPI(); + shutdownGPIO(); +} + void initGPIO() { for(const GPIOPin & g : QSPIPins) { g.group().OSPEEDR()->setOutputSpeed(g.pin(), GPIO::OSPEEDR::OutputSpeed::High); @@ -213,9 +220,17 @@ void initGPIO() { } } +void shutdownGPIO() { + for(const GPIOPin & g : QSPIPins) { + g.group().OSPEEDR()->setOutputSpeed(g.pin(), GPIO::OSPEEDR::OutputSpeed::Low); + g.group().MODER()->setMode(g.pin(), GPIO::MODER::Mode::Analog); + g.group().PUPDR()->setPull(g.pin(), GPIO::PUPDR::Pull::None); + } +} + void initQSPI() { // Enable QUADSPI AHB3 peripheral clock - RCC.AHB3ENR()->setQSPIEN(true); + RCC.AHB3ENR()->setQSPIEN(true); // TODO: move in Device::initClocks // Configure controller for target device class QUADSPI::DCR dcr(0); dcr.setFSIZE(NumberOfAddressBitsInChip - 1); @@ -228,10 +243,15 @@ void initQSPI() { QUADSPI.CR()->set(cr); } +void shutdownQSPI() { + RCC.AHB3ENR()->setQSPIEN(false); // TODO: move in Device::shutdownClocks +} + void initChip() { + static bool firstPass = true; /* The chip initially expects commands in SPI mode. We need to use SPI to tell * it to switch to QPI. */ - if (DefaultOperatingMode == QUADSPI::CCR::OperatingMode::Quad) { + if (firstPass && DefaultOperatingMode == QUADSPI::CCR::OperatingMode::Quad) { send_command(Command::WriteEnable, QUADSPI::CCR::OperatingMode::Single); ExternalFlashStatusRegister::StatusRegister2 statusRegister2(0); statusRegister2.setQE(true); @@ -252,10 +272,19 @@ void initChip() { readParameters.setP5(true); send_write_command(Command::SetReadParameters, reinterpret_cast(FlashAddressSpaceSize), reinterpret_cast(&readParameters), sizeof(readParameters)); } + firstPass = false; + } else { + // TODO } set_as_memory_mapped(); } +void shutdownChip() { + unset_memory_mapped_mode(); + send_command(Command::DeepPowerDown); + Timing::usleep(100); // TODO should be 3us when usleep adjusted +} + int SectorAtAddress(uint32_t address) { int i = address >> NumberOfAddressBitsIn64KbyteBlock; if (i >= NumberOfSectors) { diff --git a/ion/src/f730/external_flash.h b/ion/src/f730/external_flash.h index 1b102027d..2f4af8b01 100644 --- a/ion/src/f730/external_flash.h +++ b/ion/src/f730/external_flash.h @@ -36,8 +36,11 @@ void init(); void shutdown(); void initGPIO(); +void shutdownGPIO(); void initQSPI(); +void shutdownQSPI(); void initChip(); +void shutdownChip(); void MassErase(); @@ -61,7 +64,9 @@ enum class Command : uint8_t { // Erase the whole chip or a 64-Kbyte block as being "1" ChipErase = 0xC7, Erase64KbyteBlock = 0xD8, - SetReadParameters = 0xC0 + SetReadParameters = 0xC0, + DeepPowerDown = 0xB9, + ReleaseDeepPowerDown = 0xAB }; constexpr static uint32_t QSPIBaseAddress = 0x90000000;