Skip to content

File system.h

File List > external-docs > libDaisy > src > sys > system.h

Go to the documentation of this file

Source Code

#ifndef DSY_SYSTEM_H
#define DSY_SYSTEM_H

#ifndef UNIT_TEST // for unit tests, a dummy implementation is provided below

#include <cstdint>
#include "per/tim.h"

namespace daisy
{
class System
{
  public:
    struct Config
    {
        enum class SysClkFreq
        {
            FREQ_400MHZ,
            FREQ_480MHZ,
        };

        void Defaults()
        {
            cpu_freq    = SysClkFreq::FREQ_400MHZ;
            use_dcache  = true;
            use_icache  = true;
            skip_clocks = false;
        }

        void Boost()
        {
            cpu_freq    = SysClkFreq::FREQ_480MHZ;
            use_dcache  = true;
            use_icache  = true;
            skip_clocks = false;
        }

        SysClkFreq cpu_freq;
        bool       use_dcache;
        bool       use_icache;
        bool       skip_clocks;
    };

    enum MemoryRegion
    {
        INTERNAL_FLASH = 0,
        ITCMRAM,
        DTCMRAM,
        SRAM_D1,
        SRAM_D2,
        SRAM_D3,
        SDRAM,
        QSPI,
        INVALID_ADDRESS,
    };

    struct BootInfo
    {
        enum class Type : uint32_t
        {
            INVALID      = 0x00000000,
            JUMP         = 0xDEADBEEF,
            SKIP_TIMEOUT = 0x5AFEB007,
            INF_TIMEOUT  = 0xB0074EFA,
        } status;
        uint32_t data;
        enum class Version : uint32_t
        {
            LT_v6_0 = 0, // Less than v6.0
            NONE,        // No bootloader present
            v6_0,        // v6.0
            v6_1,        // v6.1 or greater
            LAST
        } version;
    };

    System() {}
    ~System() {}

    void Init();

    void Init(const Config& config);

    void DeInit();

    void JumpToQspi();

    static uint32_t GetNow();

    static uint32_t GetUs();

    static uint32_t GetTick();

    static void Delay(uint32_t delay_ms);

    static void DelayUs(uint32_t delay_us);


    static void DelayTicks(uint32_t delay_ticks);

    enum BootloaderMode
    {
        STM = 0,
        DAISY,
        DAISY_SKIP_TIMEOUT,
        DAISY_INFINITE_TIMEOUT
    };

    static void ResetToBootloader(BootloaderMode mode = BootloaderMode::STM);

    static void InitBackupSram();

    static BootInfo::Version GetBootloaderVersion();

    static uint32_t GetTickFreq();

    static uint32_t GetSysClkFreq();

    static uint32_t GetHClkFreq();

    static uint32_t GetPClk1Freq();

    static uint32_t GetPClk2Freq();

    const Config& GetConfig() const { return cfg_; }

    static MemoryRegion GetProgramMemoryRegion();

    static MemoryRegion GetMemoryRegion(uint32_t address);

    static constexpr uint32_t kQspiBootloaderOffset = 0x40000U;

  private:
    void   ConfigureClocks();
    void   ConfigureMpu();
    Config cfg_;

    static TimerHandle tim_;
};

extern volatile daisy::System::BootInfo boot_info;

} // namespace daisy

#else // ifndef UNIT_TEST

#include <cstdint>
#include "../tests/TestIsolator.h"
namespace daisy
{
class System
{
  public:
    static uint32_t GetNow()
    {
        return testIsolator_.GetStateForCurrentTest()->currentUs_ / 1000;
    }
    static uint32_t GetUs()
    {
        return testIsolator_.GetStateForCurrentTest()->currentUs_;
    }
    static uint32_t GetTick()
    {
        return testIsolator_.GetStateForCurrentTest()->currentTick_;
    }
    static uint32_t GetTickFreq()
    {
        return testIsolator_.GetStateForCurrentTest()->tickFreqHz_;
    }

    static void SetTickForUnitTest(uint32_t tick)
    {
        testIsolator_.GetStateForCurrentTest()->currentTick_ = tick;
    }
    static void SetUsForUnitTest(uint32_t us)
    {
        testIsolator_.GetStateForCurrentTest()->currentUs_ = us;
    }
    static void SetTickFreqForUnitTest(uint32_t freqInHz)
    {
        testIsolator_.GetStateForCurrentTest()->tickFreqHz_ = freqInHz;
    }

  private:
    struct SystemState
    {
        uint32_t currentTick_ = 0;
        uint32_t currentUs_   = 0;
        uint32_t tickFreqHz_  = 0;
    };
    static TestIsolator<SystemState> testIsolator_;
};

} // namespace daisy

#endif // ifndef UNIT_TEST
#endif