//=================================================================================================
//
// Company:       Paul Scherrer Institut
//                5232 Villigen PSI
//                Switzerland
//
//-------------------------------------------------------------------------------------------------
//
// Project:       Peltier Controller V2
// Author:        Noah Piqué (noah.pique@psi.ch)
//
//-------------------------------------------------------------------------------------------------
//
// Module:        Analog Ports Input
// Filename:      ANPI_AnalogPortsIn.c
// Date:          Handled by Subversion (version control system)
// Revision:      Handled by Subversion (version control system)
// History:       Handled by Subversion (version control system)
//
//-------------------------------------------------------------------------------------------------
//
// Description:   This source file contains all functions dealing with the analog input ports
//
//=================================================================================================



//=================================================================================================
// Section:       INCLUDES
// Description:   List of required include files.
//=================================================================================================

#include "../PDEF_ProjectDefinitions.h"
#include "ANPI_AnalogPortsIn.h"

#include "PECO_PeltierController.h"

//Application
//#include "../Application/ELOG_ErrorLogger.h"

// Toolbox
#include "../Toolbox/UTIL_Utility.h"

// include STM32 drivers
#include "stm32l4xx_hal.h"

#include "cmsis_os2.h" 

//=================================================================================================
// Section:       DEFINITIONS
// Description:   Definition of local constants (visible by this module only).
//=================================================================================================

#define ADC_RES         (4096)                       // ADC resolution: 12 bits
#define NR_OF_ADCS   		5                            // number of internal adc channels

// definitions of internal adc
#define INT_ADC_REF_HI  (3.3f)                        // HI int. reference voltage for conversion
#define INT_ADC_REF_LO  (0.0f)                        // LO int. reference voltage for conversion
#define INT_ADC_REF    (INT_ADC_REF_HI-INT_ADC_REF_LO)// int. reference voltage for conversion

#define BUFFER_SIZE                         NR_OF_ADCS *  2
#define BUFFER_HALF_SIZE                    NR_OF_ADCS

#define ANPI_ADC_HALF_COMPLETE              ((U32)1<<0)
#define ANPI_ADC_FULL_COMPLETE              ((U32)1<<1)

#define ANPI_FLAGS_ALL                      ( ANPI_ADC_HALF_COMPLETE | ANPI_ADC_FULL_COMPLETE )

#define OVERSAMPLING_DIVISOR 16.0f // calculated with parameters from hardware oversampling
                                // 6 bits(64x) - 2 bit shift = 4bit -> 16x

//=================================================================================================
// Section:       MACROS
// Description:   Definition of local macros (visible by this module only).
//=================================================================================================



//=================================================================================================
// Section:       ENUMERATIONS
// Description:   Definition of local enumerations (visible by this module only).
//=================================================================================================

// the number of ADCs
typedef enum
{    
  eADC1   = 0,
  eNumberOfADCs,
} EnADC;

//=================================================================================================
// Section:       STRUCTURES
// Description:   Definition of local Structures (visible by this module only).
//=================================================================================================

typedef struct
{
  ADC_TypeDef*  pstADC;
} StADCInit;                   // struct for internal ACD


//=================================================================================================
// Section:       LOCAL VARIABLES
// Description:   Definition of local variables (visible by this module only).
//=================================================================================================

LOCAL FLOAT m_aflValues[ANPI_eInNumberOfInputs];          // values

LOCAL U16 m_au16ADCDataBuffer[eNumberOfADCs][BUFFER_SIZE];
LOCAL U32 m_au32ADCRawData[eNumberOfADCs*NR_OF_ADCS] = {0}; // raw adc values

LOCAL osThreadId_t        m_pstThreadID       = NULL;
LOCAL osEventFlagsId_t    m_pstEventID  		  = NULL;
LOCAL osMutexId_t         m_pstMutexID        = NULL;

//=================================================================================================
// Section:       LOCAL CONSTANTS
// Description:   Definition of local constants (visible by this module only).
//=================================================================================================

// conversion factors for the values
// Order must fit enumeration "ANPI_EnAnalogInput"
LOCAL CONST FLOAT m_aflConversionFactor[ANPI_eInNumberOfInputs] =
{
  34.103f * 1.0f / ADC_RES * INT_ADC_REF,           // 00 ANPI_eControlVoltage
  10.0f / ADC_RES * INT_ADC_REF,          					// 01 ANPI_eSupplyVoltage24V 
  -(1.0f / (2.0f / 10.0f)) / ADC_RES * INT_ADC_REF,        	  // 02 ANPI_eSupplyCurrent24V
  34.103f * 1.0f / ADC_RES * INT_ADC_REF,  					// 03 ANPI_eOutputVoltage
  (1.0f / (2.0f / 10.0f)) / ADC_RES * INT_ADC_REF,        		// 04 ANPI_eOutputCurrent
};

// offsets for the values before it gets multiplied
// Order must fit enumeration "ANPI_EnAnalogInput"
LOCAL CONST FLOAT m_aflOffset1[ANPI_eInNumberOfInputs] =
{
  0.0f,                 // 00 ANPI_eControlVoltage
  0.0f,                 // 01 ANPI_eSupplyVoltage24V
  1.65f * ADC_RES / INT_ADC_REF,               // 02 ANPI_eSupplyCurrent24V
  0.0f,                 // 03 ANPI_eOutputVoltage
  1.65f * ADC_RES / INT_ADC_REF, 						  // 04 ANPI_eOutputCurrent
};

// offsets for the values after it gets multiplied
// Order must fit enumeration "ANPI_EnAnalogInput"
LOCAL CONST FLOAT m_aflOffset2[ANPI_eInNumberOfInputs] =
{
  20.088f,                 // 00 ANPI_eControlVoltage
  0.0f,                 // 01 ANPI_eSupplyVoltage24V
  0.0f,               // 02 ANPI_eSupplyCurrent24V
  20.088f,                 // 03 ANPI_eOutputVoltage
  0.0f, 						  // 04 ANPI_eOutputCurrent
};

// initial values. Order must fit enumeration "ANPI_EnAnalogInput"
LOCAL CONST FLOAT m_afInitValues[ANPI_eInNumberOfInputs] = 
{
  0.0f,     // 00 ANPI_eControlVoltage
  0.0f,    // 01 ANPI_eSupplyVoltage24V
  0.0f,     // 02 ANPI_eSupplyCurrent24V
  0.0f,     // 03 ANPI_eOutputVoltage
  0.0f,     // 04 ANPI_eOutputCurrent
};

// configuration data for the ADC
// order must fit enumeration EnADC
LOCAL CONST StADCInit m_astADCInit[eNumberOfADCs] =
{ 
  {ADC1},   // 00 eADC1
};
// inputs are connected to the following ADCs
// ANPI_eControlVoltage         ADC1, Channel 8
// ANPI_eSupplyVoltage24V     	ADC1, Channel 6
// ANPI_eSupplyCurrent24V    		ADC1, Channel 16
// ANPI_eOutputVoltage       		ADC1, Channel 7
// ANPI_eOutputCurrent          ADC1, Channel 15

LOCAL CONST osThreadAttr_t stTaskAttribute =
{
  "ANPI_Thread",                    // name of the thread
  osThreadDetached,                 // attribute bits
  NULL,                             // memory for control block
  0,                                // size of provided memory for control block
  NULL,                             // memory for stack
  1024,                          // size of stack
  osPriorityBelowNormal,            // initial thread priority (default: osPriorityNormal)
  0,                                // TrustZone module identifier
  0,                                // reserved (must be 0)
};

LOCAL CONST osEventFlagsAttr_t stEventAttribute =
{ 
  "ANPI_Event_Flags",               // name of the event flags 
  0,                                // attribute bits 
  NULL,                             // memory for control block
  0,                                // size of provided memory for control block
};

LOCAL CONST osMutexAttr_t m_stMutexAttr =
{
  "ANPI_Mutex",                         // human readable mutex name
  osMutexRecursive | osMutexPrioInherit,    // attr_bits
  NULL,                                     // memory for control block   
  0U                                        // size for control block
};

//=================================================================================================
// Section:       LOCAL FUNCTIONS (PROTOTYPES)
// Description:   Definition of local functions (visible by this module only).
//=================================================================================================

PRIVATE VOID ANPI_vTask( PVOID arg );

//=================================================================================================
// Section:       EXTERNAL FUNCTIONS
// Description:   Definition of external (global) functions.
//=================================================================================================



//=================================================================================================
// Section:       EXTERNAL VARIABLES
// Description:   Definition of external (global) variables.
//=================================================================================================

extern ADC_HandleTypeDef hadc1;


//=================================================================================================
// Section:       GLOBAL FUNCTIONS
// Description:   Definition (implementation) of global functions.
//=================================================================================================

//-------------------------------------------------------------------------------------------------
// Function:      ANPI_boInitializeModule
// Description:   Initializes the module. Function must be called once immediately after power-up.
// Parameters:    None
// Returns:       Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL ANPI_boInitializeModule( VOID )
{
  BOOL boOK = TRUE;
  
  // set the init values
  UTIL_vMemCopyU32( (PU32)m_afInitValues, (PU32)m_aflValues, sizeof(m_aflValues)/sizeof(FLOAT) );
  
  boOK &= ((m_pstThreadID = osThreadNew( ANPI_vTask, NULL, &stTaskAttribute )) == NULL ) ? FALSE : TRUE;
  boOK &= ((m_pstEventID = osEventFlagsNew( &stEventAttribute )) == NULL) ? FALSE : TRUE;
  
  boOK &= ((m_pstMutexID = osMutexNew( &m_stMutexAttr )) == NULL) ? FALSE : TRUE;

  return( boOK );
}

//-------------------------------------------------------------------------------------------------
// Function:      ANPI_vTask
// Description:   ANPI_vTask
// Parameters:    None
// Returns:       None
//-------------------------------------------------------------------------------------------------
VOID ANPI_vTask( PVOID arg )
{
  U32 u32Flags;
  U16 u16Offset;
  
  osDelay( 1 ); // Wait 1ms to have a Valid Value
  
  HAL_ADC_Start_DMA( &hadc1, (PU32)&m_au16ADCDataBuffer[eADC1][0], BUFFER_SIZE);
  

  while ( TRUE )
  {
    u32Flags = osEventFlagsWait( m_pstEventID, ANPI_FLAGS_ALL, osFlagsWaitAny, osWaitForever );

    if( u32Flags & ANPI_ADC_FULL_COMPLETE )
    {
      u16Offset = BUFFER_HALF_SIZE;
    }
    
    if( u32Flags & ANPI_ADC_HALF_COMPLETE )
    {
      u16Offset = 0;
    }
    
    // reset the sum for calculating the mean
    memset( m_au32ADCRawData, 0, sizeof(m_au32ADCRawData) );
    

    // ... multiply by the conversion factor and add the offset
    osMutexAcquire( m_pstMutexID, osWaitForever );                                  // aquire mutex

    // save the values in the buffer...
    for(U16 u16Cnt = 0; u16Cnt < BUFFER_HALF_SIZE; u16Cnt++ )
    {    
      m_au32ADCRawData[ u16Cnt ] = m_au16ADCDataBuffer[eADC1][u16Cnt + u16Offset];
    } 

    // ... multiply by the conversion factor and add the offset
    
    for(U16 u16Cnt = 0; u16Cnt < ANPI_eInNumberOfInputs; u16Cnt++ )
    {
      if(u16Cnt == ANPI_eOutputVoltage && !PECO_isEnabled()){
        m_aflValues[u16Cnt] = 0.0f;
        continue;
      }
      m_aflValues[u16Cnt] = ((((FLOAT)m_au32ADCRawData[u16Cnt] / OVERSAMPLING_DIVISOR) - (FLOAT)m_aflOffset1[u16Cnt] ) *
														(FLOAT)m_aflConversionFactor[u16Cnt]) - (FLOAT)m_aflOffset2[u16Cnt];
    }
    
    osMutexRelease( m_pstMutexID );                   															// release mutex
  }
}

//-------------------------------------------------------------------------------------------------
// Function:      HAL_ADC_ConvCpltCallback
// Description:   Handles the ADC interrupts
// Parameters:    None
// Returns:       None
//-------------------------------------------------------------------------------------------------
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  osEventFlagsSet( m_pstEventID, ANPI_ADC_FULL_COMPLETE );
}

//-------------------------------------------------------------------------------------------------
// Function:      HAL_ADC_ConvHalfCpltCallback
// Description:   Handles the ADC interrupts
// Parameters:    None
// Returns:       None
//-------------------------------------------------------------------------------------------------
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
{
  osEventFlagsSet( m_pstEventID, ANPI_ADC_HALF_COMPLETE );

}

//-------------------------------------------------------------------------------------------------
// Function:      ANPI_flGetInputValue
// Description:   Gets the value of the analog input
// Parameters:    ANPI_EnAnalogInput enInput    Analog input to read
// Returns:       FLOAT flValue                 Value from ADC in V
//-------------------------------------------------------------------------------------------------
FLOAT ANPI_flGetInputValue( ANPI_EnAnalogInput enInput )
{
  
  osMutexAcquire( m_pstMutexID, osWaitForever );                                  // aquire mutex
  FLOAT flValue = m_aflValues[enInput];
  osMutexRelease( m_pstMutexID );                   															// release mutex

  return( flValue );
}

//=================================================================================================
// Section:       LOCAL FUNCTIONS
// Descriptionn:  Definition (implementation) of local functions.
//=================================================================================================

//-------------------------------------------------------------------------------------------------
// Function:      HAL_ADC_ErrorCallback
// Description:   Error Callback for the ADC
// Parameters:    ADC_HandleTypeDef* hadc 
// Returns:       None
//-------------------------------------------------------------------------------------------------
void HAL_ADC_ErrorCallback( ADC_HandleTypeDef* hadc )
{
  if( hadc->ErrorCode == HAL_ADC_ERROR_NONE )
  {
    
  }
  
  if( hadc->ErrorCode == HAL_ADC_ERROR_INTERNAL )
  {
    
  }
  if( hadc->ErrorCode == HAL_ADC_ERROR_OVR )
  {
    //ELOG_ADDLOG( ELOG_eADCOverrunError, NULL );
  }
  if( hadc->ErrorCode == HAL_ADC_ERROR_DMA )
  {
//    ELOG_ADDLOG( ELOG_eDMAHTransferError, NULL );
  }
  
  // check rx dma transfer error
  if( hadc->DMA_Handle->ErrorCode & HAL_DMA_ERROR_TE )
  {
//    ELOG_ADDLOG( ELOG_eDMAHTransferError, NULL );
  } 
  
}
