// STM32F103xB: only high density
#if ! ( defined(STM32F103xx) || defined(STM32L1) || defined(STM32G0) )
//-----------------------------------------------------------------------------
///
/// \brief  DAC
///
///         Blah blah blub
///
/// \date   20150117
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


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


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

#include <biwak/biwak.h>         // exception
#include <biwak/gpio.hpp>
#include <biwak/rcc.h>           // rccDacClockEnable
#include <biwak/handler.h>           // __TIM6_DAC_IRQHandler()
#include <HALWrapper/stm32_tsc.h>
#include <biwak/tsc.h>
#include <biwak/nvic.h>          // ePeripheral
#include <lepto/log.h>
#include <biwak/exception.h>


//---Implementation------------------------------------------------------------

#if ! defined(STM32F4)

CTsc::CTsc( EGpioPeripheral eGpioPeripheralCap, EGpioPin eGpioPinCap,
            EGpioPeripheral eGpioPeripheralIo, EGpioPin eGpioPinIo
            /*, EGpioPeripheral _eGpioPeripheral1, EGpioPin _eGpioPin1
            , CDmaChannel *dmaChannel1, ETimer _eTimer1
            , EGpioPeripheral _eGpioPeripheral2, EGpioPin _eGpioPin2
            , CDmaChannel *dmaChannel2, ETimer _eTimer2*/
            )
      //:gpioCap(eGpioPeripheralCap, eGpioPinCap, eGpioTypeAfCap, eAltFuncTsc)
      //,gpioIo(eGpioPeripheralIo, eGpioPinIo, eGpioTypeAfTsc, eAltFuncTsc)
      :gpioCap(eGpioPeripheralCap, eGpioPinCap, gpioModeAfCap, 0 )
      ,gpioIo(eGpioPeripheralIo, eGpioPinIo, gpioModeAfTsc, 0 )
{
   m_pHandler=new TSC_HandleTypeDef;

   m_pHandler->Instance = TSC;

   if (HAL_TSC_DeInit( m_pHandler ) != HAL_OK)
   {
      // Could not deinit TSC
      exception();
   }
   rccTscClockEnable(m_pHandler->Instance);
   m_pHandler->Init.AcquisitionMode         = TSC_ACQ_MODE_NORMAL;
   m_pHandler->Init.CTPulseHighLength       = TSC_CTPH_2CYCLES;
   m_pHandler->Init.CTPulseLowLength        = TSC_CTPL_2CYCLES;
   m_pHandler->Init.IODefaultMode           = /*TSC_IODEF_IN_FLOAT*/TSC_IODEF_OUT_PP_LOW;
   m_pHandler->Init.MaxCountInterrupt       = DISABLE;
   m_pHandler->Init.MaxCountValue           = TSC_MCV_8191;
   m_pHandler->Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV64;
   m_pHandler->Init.SpreadSpectrum          = DISABLE;
   m_pHandler->Init.SpreadSpectrumDeviation = 127;
   m_pHandler->Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1;
   m_pHandler->Init.SynchroPinPolarity      = TSC_SYNC_POLARITY_FALLING;
   /* All channel, shield and sampling IOs must be declared below */
   /*
   m_pHandler->Init.ChannelIOs              = (TSC_GROUP1_IO3 );
   m_pHandler->Init.SamplingIOs             = (TSC_GROUP1_IO4 );
   m_pHandler->Init.ShieldIOs               = 0;
   */

   m_pHandler->Init.ChannelIOs              = (TSC_GROUP4_IO1 );
   m_pHandler->Init.SamplingIOs             = (TSC_GROUP4_IO2 );
   m_pHandler->Init.ShieldIOs               = 0;

   if (HAL_TSC_Init( m_pHandler ) != HAL_OK)
   {
      // Could not init TSC
      exception();
   }

   /* Init STMTouch driver */
   //TSL_user_Init();
   //TSL_obj_GroupInit(&MyObjGroup); /* Init Objects */
   HAL_TSC_IODischarge(m_pHandler, ENABLE);

   //usleep(1000);
   //HAL_TSC_IODischarge(m_pHandler, DISABLE);

   //TSL_Init(MyBanks); /* Init timing and acquisition modules */
   if ( HAL_TSC_Start(m_pHandler) != HAL_OK)
   {
      // Could not start TSC
      exception();
   }

   init();
}


uint32_t CTsc::getValue()
{
   return(HAL_TSC_GroupGetValue(m_pHandler, TSC_GROUP4_IDX));
}

uint32_t CTsc::getStatus()
{
   //return(HAL_TSC_GroupGetStatus(m_pHandler, 0));
   return(HAL_TSC_GetState(m_pHandler));
}

void CTsc::init()
{
   //TSC_IOConfigTypeDef config;
   //config.ChannelIOs
   //HAL_TSC_IOConfig(m_pHandler, &config);
}

void CTsc::aquire()
{
   //HAL_TSC_Stop(m_pHandler);
   HAL_TSC_Start(m_pHandler);
   //usleep(USECS_PER_SEC);
   poll();
}

void CTsc::poll()
{
   HAL_TSC_PollForAcquisition(m_pHandler);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"

IRQ_Funcion_imp(TSC_IRQHandler)
{
   exception( );
}

#pragma GCC diagnostic pop

#endif // ? ! defined(STM32F4)

//---fin-----------------------------------------------------------------------
#endif // ? ! defined(STM32F103xB)
