#ifndef BIWAK_H
#define BIWAK_H
//----------------------------------------------------------------------------
///
/// \brief  Biwak general functions
///
///         See implementation for further information.
///
//----------------------------------------------------------------------------


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


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

#include <stdint.h>
#include <stdio.h>            // FILE, hmm, better move initStdout()
#include <lepto/lepto.h>      // IS_ENABED

#if ! defined(BIWAK_CONFIGURED)
   #error   BIWAK_CONFIGURED not defined. The configuration header was probably \
            not involved. If the file is present make sure the macro \
            'BIWAK_CONFIGURED' is defined in the configuration header.
#endif

#if defined( CONFIG_BIWAK_LOG_CODE )
   #error   BIWAK_LOG_CODE is obsolete. Use CONFIG_BIWAK_LOG_CUSTOM_CHANNEL if \
            you want to send log codes e.g. via CAN.
#endif


// Sleep:   Cortex Core stopped, peripherals kept running.
//          Any peripheral interrupt acknowledged by the nested vectored
//          interrupt controller (NVIC) can wake up the device from Sleep mode.
// Stop mode: all clocks are stopped, regulator running, regulator in low power
//          mode
//          EXTI can wake up device or specific programmed communication
//          periphery
// Sleep:   1.2V domain powered off (mode not available on STM32F3x8 devices).
//          Exit: WKUP pin rising edge, RTC alarm (Alarm A and Alarm B), RTC wakeup,
//          tamper event, time-stamp event, external reset in NRST pin, IWDG reset.
// WDI: any interrupt
#define BIWAK_SLEEP_MODE   PWR_SLEEPENTRY_WFI
#define BIWAK_STOP_MODE    PWR_STOPENTRY_WFI

#define BIWAK_CODE   0x42
/*
typedef char sstr[40];
extern sstr globalBuf[];
extern int gbPos;
extern const int gbCount;
*/

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

#if defined MCU_PLATFORM_STM32
   // 64Bit highly slows down usage of course. No efford. 32 Bit works well for
   // Temp and Ir.
   typedef int32_t hrtimer_t;
   typedef int32_t lrtimer_t;
   typedef uint32_t hrtimer_unsigned_t;
   //#define LRTIMER_INVAL   ((lrtimer_t)0x80000000)
   //#define HRTIMER_INVAL   ((hrtimer_t)0x80000000)
   #define LRTIMER_MAX     (LRTIMER_INVAL-1)
   #define HRTIMER_MAX     (HRTIMER_INVAL-1)
#else
   typedef int64_t hrtimer_t;
   typedef int64_t lrtimer_t;
   #define LRTIMER_INVAL   ((lrtimer_t)0x8000000000000000)
   #define HRTIMER_INVAL   ((hrtimer_t)0x8000000000000000)
   //typedef int64_t useconds_t;
#endif

#if defined MCU_PLATFORM_STM32
#else
    /*
    class qtplatform;
    extern qtplatform *pQtplatform;
    #define qtp (*pQtplatform)
    */
   #define MCU_REV_X86
   #define MCU_PLATFORM_PC
#endif

//int getSystemTicker(void);
void biwakInit();
void biwakInitNoClock();
void biwakDeInit();
void biwakEventLoop();

class CFlash;
class CTimer;
class CUart;

//void biwakPrintFirmwareInfo();
void biwakPrintBaseInfo();
void biwakPrintCompilerInfo( );
void biwakPrintClockInfo();
void biwakPrintMemoryBaseInfo( );
void biwakPrintMemoryInfo();
void biwakPrintFlashInfo();
void biwakPrintTimings( );
void biwakPrintDateInfo( );
class CIOStream;
void initStdout(FILE *file, CIOStream *io);
void setStdio(CIOStream *io);
void systemReset();
//class CRtc;
//void setLibcRtc(CRtc &io);

#if IS_ENABLED( CONFIG_BIWAK_RESETCOUNTER )
   // extern uint32_t &resetCounter;
   int resetCounter();
   void clearResetCounter();
#endif


//---fin----------------------------------------------------------------------
#endif // ? ! BIWAK_H
