#ifndef BIWAK_QSPI_H_
#define BIWAK_QSPI_H_
//----------------------------------------------------------------------------
///
/// \brief  QSPI
///
///         See implementation files for further information.
//
/// \date   20220707
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


//---Own------------------------------

#include <biwak/biwak.h>
#include <biwak/gpio.hpp>
#include <biwak/output.hpp>


//---Forward declarations-----------------------------------------------------


#if defined (MCU_PLATFORM_STM32)
   #define SPI_HANDLE_TYPE    SPI_HandleTypeDef
   #include <HALWrapper/stm32_dma.h>       // SPI wants that
   #include <HALWrapper/stm32_qspi.h>
#else
   #define QSPI_HandleTypeDef    void
#endif


//---Defines------------------------------------------------------------------


#define BUFFERSIZE         0x20


//---Declaration--------------------------------------------------------------


struct SQSpiConfig;

class CQSpi
{

   private:
      const SQSpiConfig &m_configData;

   public:
      QSPI_HandleTypeDef *m_pHandle;

   private:
      CGpio m_cs;
      CGpio m_sck;
      CGpio m_io[4];

      //ESpi eSpi;
   //public:
      //CGpio gpioSck, gpioMosi, gpioMiso;
      //uint8_t aTxBuffer[BUFFERSIZE]=
      //      "ABCDEFABCDEFABCDEFABCDEFABCDEFX";

      #if defined STM32
      DMA_HandleTypeDef hdma;
      QSPI_CommandTypeDef      m_sCommand{0};
      QSPI_MemoryMappedTypeDef m_sMemMappedCfg{0};
      #endif

   public:

      CQSpi(const SQSpiConfig &configData);
      ~CQSpi();
      int init();
      void commandDefault();
      int memMap();

      int writeEnable( );
      int waitFlag( int mask, int result );
      int autoPollingMemReady( uint32_t Timeout );
      int dummyCyclesCfg();
      int readJedecId();
      int readStatus();
      int writeStatus(uint16_t status);
      int dump(int address);
      int enableHPM();
      int deepPowerDown();
      int wakeUp();
      int command(uint32_t inst);
      int write(int address, const uint8_t *data, int len);
      void interrupt();
};


//---fin----------------------------------------------------------------------
#endif // ? ! BIWAK_QSPI_H_
