#ifndef _LEPTO_FDCAN_H_
#define _LEPTO_FDCAN_H_
//----------------------------------------------------------------------------
//
//
//
//----------------------------------------------------------------------------


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

//#include <stm/stm32_can.h>
#include "lepto/ring.h"
#include <biwak/canMessage.hpp>
#include <biwak/gpio.hpp>    // EGpioPeripheral


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


#if defined (MCU_PLATFORM_STM32) && ! defined ( STM32L1 )
   #define FDCAN_HANDLE_TYPE    FDCAN_HandleTypeDef
   #include <HALWrapper/stm32_can.h>
#else
   #define CAN_HANDLE_TYPE    void
#endif


//---Declarations-------------------------------------------------------------

/*
enum BaudRateT
{
    BAUD_10KBIT,
    BAUD_20KBIT,
    BAUD_50KBIT,
    BAUD_125KBIT,
    BAUD_250KBIT,
    BAUD_500KBIT,
    BAUD_800KBIT,
    BAUD_1MBIT
};
*/

int CAN_Config(void);
//int CAN_driver_init(BaudRateT rate);

struct SFDCanConfig;

//extern const SCanConfig canConfig_CAN1_PA12_PA11;

class CFDCan
{

   private:
      const SFDCanConfig &m_configData;
      FDCAN_HANDLE_TYPE *m_pHandler;
      FDCAN_FilterTypeDef sFilterConfig;

#if defined ( STM32F4 ) || defined( STM32F3 ) || defined( STM32F0 ) || defined( STM32L4 )
      CAN_TxHeaderTypeDef *txHeader;
      CAN_RxHeaderTypeDef *rxHeader;
#endif
      //uint8_t data[0x10];
      uint32_t txMailBox;
      FDCAN_TxHeaderTypeDef TxHeader;
      FDCAN_RxHeaderTypeDef RxHeader;
      uint8_t RxData[0x10];

   private:

      int m_status;
      CGpio gpioTx, gpioRx;
      bool m_enabled;

   public:

      CRing<SCanMessage> buffer;
      CFDCan( const SFDCanConfig &_conTxHeaderfigData);
      ~CFDCan();

      void init();
      void initFilter();
      void initDelays();
      void initHeaders();
      void deinit();

      void putChar(const char ch);
      void puts(const char *str);

      int transmit(const SCanMessage &message);
      int getTransmitStatus(int mailBox);
      int getLastError(void);

      int getStatus()
      {
         return(m_status);
      }

      FDCAN_HANDLE_TYPE *getHandle()
      {
       return(m_pHandler);
      }

      void disable()
      {
         m_enabled=false;
      }

      FDCAN_RxHeaderTypeDef &getRxHeader()
      {
         return(RxHeader);
      }
      uint8_t *getRxData()
      {
         return(RxData);
      }
      void interrupt(void);
};


//---fin-----------------------------------------------------------------------
#endif // ? ! _LEPTO_FDCAN_H_
