259 lines
9.8 KiB
C
259 lines
9.8 KiB
C
//=================================================================================================
|
|
//
|
|
// Company: Paul Scherrer Institut
|
|
// 5232 Villigen PSI
|
|
// Switzerland
|
|
//
|
|
//-------------------------------------------------------------------------------------------------
|
|
//
|
|
// Project: Peltier Controller V2
|
|
// Author: Noah Piqué (noah.pique@psi.ch)
|
|
//
|
|
//-------------------------------------------------------------------------------------------------
|
|
//
|
|
// Module: Temp
|
|
// Filename: TEMP_Temperature.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 temperature readout
|
|
//
|
|
//=================================================================================================
|
|
|
|
|
|
|
|
//=================================================================================================
|
|
// Section: INCLUDES
|
|
// Description: List of required include files.
|
|
//=================================================================================================
|
|
|
|
#include "../Application/VARH_VariableHandler.h"
|
|
#include "../PDEF_ProjectDefinitions.h"
|
|
#include "TEMP_Temperature.h"
|
|
|
|
// Application
|
|
//#include "../Application/ELOG_ErrorLogger.h"
|
|
//#include "../Application/RTOS_RealTimeOs.h"
|
|
|
|
// Toolbox
|
|
//#include "../Toolbox/ASRT_Assert.h"
|
|
#include "../Toolbox/UTIL_Utility.h"
|
|
|
|
// Drivers
|
|
|
|
#include "DIPO_DigitalPorts.h"
|
|
#include "SPID_SpiDriver.h"
|
|
#include "ADCD_ADCDriver.h"
|
|
|
|
// include STM32 drivers
|
|
#include "stm32l4xx_hal.h"
|
|
|
|
#include <math.h>
|
|
|
|
#include "cmsis_os2.h"
|
|
|
|
//=================================================================================================
|
|
// Section: DEFINITIONS
|
|
// Description: Definition of local constants (visible by this module only).
|
|
//=================================================================================================
|
|
|
|
#define REFRESH_MS 100
|
|
|
|
//=================================================================================================
|
|
// Section: MACROS
|
|
// Description: Definition of local macros (visible by this module only).
|
|
//=================================================================================================
|
|
|
|
//=================================================================================================
|
|
// Section: ENUMERATIONS
|
|
// Description: Definition of local enumerations (visible by this module only).
|
|
//=================================================================================================
|
|
|
|
//=================================================================================================
|
|
// Section: STRUCTURES
|
|
// Description: Definition of local Structures (visible by this module only).
|
|
//=================================================================================================
|
|
|
|
//=================================================================================================
|
|
// Section: LOCAL FUNCTIONS (PROTOTYPES)
|
|
// Description: Definition of local functions (visible by this module only).
|
|
//=================================================================================================
|
|
|
|
PRIVATE FLOAT flConvertADCData( U16 dbRTemp );
|
|
PRIVATE VOID vTempTask( PVOID arg );
|
|
|
|
//=================================================================================================
|
|
// Section: LOCAL CONSTANTS
|
|
// Description: Definition of local constants (visible by this module only).
|
|
//=================================================================================================
|
|
|
|
|
|
LOCAL CONST osThreadAttr_t stTaskAttribute =
|
|
{
|
|
"TEMP_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
|
|
osPriorityNormal, // initial thread priority (default: osPriorityNormal)
|
|
0, // TrustZone module identifier
|
|
0, // reserved (must be 0)
|
|
};
|
|
|
|
//=================================================================================================
|
|
// Section: LOCAL VARIABLES
|
|
// Description: Definition of local variables (visible by this module only).
|
|
//=================================================================================================
|
|
|
|
LOCAL osThreadId_t m_pstThreadID = NULL;
|
|
|
|
//=================================================================================================
|
|
// Section: EXTERNAL FUNCTIONS
|
|
// Description: Definition of external (global) functions.
|
|
//=================================================================================================
|
|
|
|
|
|
|
|
//=================================================================================================
|
|
// Section: EXTERNAL VARIABLES
|
|
// Description: Definition of external (global) variables.
|
|
//=================================================================================================
|
|
|
|
|
|
|
|
//=================================================================================================
|
|
// Section: GLOBAL FUNCTIONS
|
|
// Description: Definition (implementation) of global functions.
|
|
//=================================================================================================
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
// Function: TEMP_boInitializeModule
|
|
// Description: Initializes the module. Function must be called once immediately after power-up.
|
|
// This function is thread save
|
|
// Parameters: None
|
|
// Returns: Boolean TRUE if successful
|
|
//-------------------------------------------------------------------------------------------------
|
|
BOOL TEMP_boInitializeModule( VOID )
|
|
{
|
|
BOOL boOK = TRUE;
|
|
|
|
boOK &= ((m_pstThreadID = osThreadNew( vTempTask, NULL, &stTaskAttribute )) == NULL ) ? FALSE : TRUE;
|
|
|
|
return( boOK );
|
|
}
|
|
|
|
//=================================================================================================
|
|
// Section: LOCAL FUNCTIONS
|
|
// Descriptionn: Definition (implementation) of local functions.
|
|
//=================================================================================================
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
// Function: vTempTask
|
|
// Description: vTempTask
|
|
// Parameters: None
|
|
// Returns: None
|
|
//-------------------------------------------------------------------------------------------------
|
|
PRIVATE VOID vTempTask( PVOID arg )
|
|
{
|
|
UNUSED( arg );
|
|
U32 u32Flags;
|
|
|
|
BOOL boOK = TRUE;
|
|
U8 error;
|
|
U16 u16ADC_data[ADCD_eNumberOfTemps];
|
|
FLOAT flTempData[ADCD_eNumberOfTemps];
|
|
|
|
osDelay(10);
|
|
|
|
while( TRUE )
|
|
{
|
|
|
|
|
|
//u32Flags = osEventFlagsWait( m_pstEventID, EVENT_REFRESH, osFlagsWaitAny, osWaitForever );
|
|
|
|
//if( u32Flags & EVENT_REFRESH )
|
|
//{
|
|
boOK &= ADCD_dReadData(ADCD_eHot, &error, &u16ADC_data[ADCD_eHot]);
|
|
boOK &= ADCD_dReadData(ADCD_eCold, &error, &u16ADC_data[ADCD_eCold]);
|
|
if( boOK )
|
|
{
|
|
flTempData[ADCD_eHot] = flConvertADCData( u16ADC_data[ADCD_eHot] );
|
|
flTempData[ADCD_eCold] = flConvertADCData( u16ADC_data[ADCD_eCold] );
|
|
VARH_vSetVariableData( VARH_eTemp_H, (VARH_UVariable)flTempData[ADCD_eHot] );
|
|
VARH_vSetVariableData( VARH_eTemp_C, (VARH_UVariable)flTempData[ADCD_eCold] );
|
|
VARH_vSetVariableData( VARH_eTemp_Diff, (VARH_UVariable)(flTempData[ADCD_eHot] - flTempData[ADCD_eCold]) );
|
|
|
|
}
|
|
//}
|
|
|
|
|
|
|
|
osDelay(REFRESH_MS);
|
|
|
|
|
|
|
|
|
|
// wait for ADC conversions completed
|
|
// u32Flags = osEventFlagsWait( m_pstEventID, OS_EVENT_ADC_COMPLETED_FLAG, osFlagsWaitAll , ADC_TIMEOUT_MS );
|
|
// boTimeout = (u32Flags == osFlagsErrorTimeout) ? TRUE : FALSE;
|
|
// boError = FALSE;
|
|
//
|
|
// // check if we have a timeout
|
|
// if( boTimeout )
|
|
// {
|
|
// //ELOG_ADD_LOG( ELOG_eADCTimeout );
|
|
// }
|
|
//
|
|
// // read ADC data
|
|
// if( !(boSuccess = boReadADCData() ) )
|
|
// {
|
|
// //ELOG_ADD_LOG( ELOG_eADCReadingDataFailed );
|
|
// }
|
|
//// vIncrementChannel();
|
|
//
|
|
// // reinit ADC on error, on spi failure or when timeout reached
|
|
// if( boTimeout || !boSuccess )
|
|
// {
|
|
// boError = TRUE;
|
|
//// vADCInit();
|
|
// }
|
|
|
|
// // send event to worker task, if all channels completed and no error
|
|
// if( m_stADCData.u8Cannel == 0 && !boError )
|
|
// {
|
|
// // send event to tasks
|
|
// for( U16 u16Cnt = 0; u16Cnt < NUMBER_OF_EVENT_CALLBACKS; u16Cnt++ )
|
|
// {
|
|
// if( m_apfnEventCallback[u16Cnt] != NULL )
|
|
// {
|
|
// m_apfnEventCallback[u16Cnt]( m_apvCallbackArgument[u16Cnt] );
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
// Function: flConvertADCData
|
|
// Description: Converts resistor value to temperature data
|
|
// Parameters: U16 u16RTemp
|
|
// Returns: U16, temperature in Celcius
|
|
//-------------------------------------------------------------------------------------------------
|
|
PRIVATE FLOAT flConvertADCData( U16 u16RTemp )
|
|
{
|
|
|
|
FLOAT u16R = u16RTemp / 8192.0f;
|
|
|
|
FLOAT flT = 9.9714f * u16R;
|
|
flT += 235.904f;
|
|
flT *= u16R;
|
|
flT += -245.876f;
|
|
|
|
return( flT );
|
|
}
|