TECware/Core/Application/MAIN_MainApplication.c
2022-01-20 14:51:37 +01:00

318 lines
12 KiB
C

//=================================================================================================
//
// Company: Paul Scherrer Institut
// 5232 Villigen PSI
// Switzerland
//
//-------------------------------------------------------------------------------------------------
//
// Project: Peltier Controller V2
// Author: Noah Piqué (noah.pique@psi.ch)
//
//-------------------------------------------------------------------------------------------------
//
// Module: Main Application
// Filename: MAIN_MainApplication.c
// Date: Handled by Subversion (version control system)
// Revision: Handled by Subversion (version control system)
// History: Handled by Subversion (version control system)
//
//-------------------------------------------------------------------------------------------------
//
// Description: TEC - Peltier Controller
//
//=================================================================================================
//=================================================================================================
// Section: INCLUDES
// Description: List of required include files.
//=================================================================================================
#include "../PDEF_ProjectDefinitions.h"
#include "MAIN_MainApplication.h"
// Application
#include "VARH_VariableHandler.h"
//#include "ELOG_ErrorLogger.h"
// Drivers
#include "../Drivers/TEMP_Temperature.h"
#include "../Drivers/DIPO_DigitalPorts.h"
#include "../Drivers/CAND_CanDriver.h"
#include "../Drivers/PECO_PeltierController.h"
#include "../Drivers/ANPI_AnalogPortsIn.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 MSG_QUEUE_SIZE 8
#define EVENT_NEW_MESSAGE ((U32)(1<<0))
#define EVENT_TIMER_UPDATE ((U32)(1<<1))
#define EVENT_FLAGS_ALL ( EVENT_NEW_MESSAGE | EVENT_TIMER_UPDATE )
#define MESSAGE_TYPE_READ 0
#define MESSAGE_TYPE_WRITE 1
#define MESSAGE_TYPE_COMMAND 2
//=================================================================================================
// 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 VOID vTask( PVOID arg );
PRIVATE VOID vEventCallback( PVOID pvData );
PRIVATE VOID vMsgRxCallback( CAND_Message stMessage );
//=================================================================================================
// Section: LOCAL VARIABLES
// Description: Definition of local variables (visible by this module only).
//=================================================================================================
LOCAL osThreadId_t m_pstThreadID = NULL;
LOCAL osMessageQueueId_t m_pstCANRxMsgQueueID = NULL;
LOCAL osEventFlagsId_t m_pstEventID = NULL;
LOCAL osTimerId_t m_pstUpdateTimer = NULL;
//=================================================================================================
// Section: LOCAL CONSTANTS
// Description: Definition of local constants (visible by this module only).
//=================================================================================================
LOCAL CONST osThreadAttr_t stTaskAttribute =
{
"MAIN_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)
};
LOCAL CONST osMessageQueueAttr_t stCANRxMsgQueueAttribute =
{
"MAIN_CAN_Rx_Queue", // name of the message queue
0, // attribute bits
NULL, // memory for control block
0, // size of provided memory for control block
NULL, // memory for data storage
0, // size of provided memory for data storage
};
LOCAL CONST osEventFlagsAttr_t stEventAttribute =
{
"MAIN_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 osTimerAttr_t stTimerAttribute =
{
"MAIN_UpdateTimer", // name of the timer
0, // attribute bits
NULL, // memory for control block
0, // size of provided memory for control block
};
//=================================================================================================
// 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: MAIN_boInitializeModule
// Description: Initializes the module. Function must be called once immediately after power-up.
// Parameters: None
// Returns: Boolean TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL MAIN_boInitializeModule( VOID )
{
BOOL boOK = TRUE;
boOK &= ( ( m_pstEventID = osEventFlagsNew( &stEventAttribute ) ) == NULL ) ? FALSE : TRUE;
boOK &= ( ( m_pstThreadID = osThreadNew( vTask, NULL, &stTaskAttribute ) ) == NULL ) ? FALSE : TRUE;
boOK &= ( ( m_pstCANRxMsgQueueID = osMessageQueueNew( MSG_QUEUE_SIZE, sizeof( CAND_Message ), &stCANRxMsgQueueAttribute ) ) == NULL ) ? FALSE : TRUE;
boOK &= ( m_pstUpdateTimer = osTimerNew( vEventCallback, osTimerPeriodic, (PVOID)EVENT_TIMER_UPDATE, &stTimerAttribute ) ) == NULL ? FALSE : TRUE;
boOK &= (osTimerStart( m_pstUpdateTimer, 1000 ) == osOK ) ? TRUE : FALSE;
CAND_vSetRxCallback( vMsgRxCallback );
return( boOK );
}
//=================================================================================================
// Section: LOCAL FUNCTIONS
// Descriptionn: Definition (implementation) of local functions.
//=================================================================================================
//-------------------------------------------------------------------------------------------------
// Function: vMainTask
// Description: Main Application Task
// Parameters: None
// Returns: None
//-------------------------------------------------------------------------------------------------
PRIVATE VOID vTask( PVOID arg )
{
UNUSED( arg );
U32 u32Flags;
U8 au8Buffer[8];
while( TRUE )
{
u32Flags = osEventFlagsWait( m_pstEventID, EVENT_FLAGS_ALL, osFlagsWaitAny, osWaitForever );
if( u32Flags & EVENT_NEW_MESSAGE ) {
// TODO: implement new protocol
CAND_Message stMessage;
osMessageQueueGet( m_pstCANRxMsgQueueID, &stMessage, NULL, 0 );
U8 u8Register = stMessage.au8Data[0];
U8 u8Typ = stMessage.au8Data[1];
VARH_UVariable uData;
UTIL_vMemCopy(&stMessage.au8Data[2], &uData, 4);
if( u8Typ == MESSAGE_TYPE_READ ){
}
if( u8Register == 0x12 ){
S16 s16Voltage = stMessage.au8Data[0] << 8;
s16Voltage |= stMessage.au8Data[1];
VARH_vSetVariableDataFloat( VARH_eControlVoltage, (FLOAT)s16Voltage / 1000 );
} else if( u8Register == 0x13 ){
S16 SupplyVoltage24V = (S16) ( VARH_flGetVariableData( VARH_eSupply_U ) * 1000 );
S16 SupplyCurrent24V = (S16) ( VARH_flGetVariableData( VARH_eSupply_I ) * 1000 );
S16 OutputVoltage = (S16) ( VARH_flGetVariableData( VARH_ePeltier_U ) * 1000 );
S16 OutputCurrent = (S16) ( VARH_flGetVariableData( VARH_ePeltier_I ) * 1000 );
au8Buffer[0] = ( SupplyVoltage24V & 0xFF00 ) >> 8;
au8Buffer[1] = SupplyVoltage24V & 0xFF;
au8Buffer[2] = ( SupplyCurrent24V & 0xFF00 ) >> 8;
au8Buffer[3] = SupplyCurrent24V & 0xFF;
au8Buffer[4] = ( OutputVoltage & 0xFF00 ) >> 8;
au8Buffer[5] = OutputVoltage & 0xff;
au8Buffer[6] = ( OutputCurrent & 0xFF00 ) >> 8;
au8Buffer[7] = OutputCurrent & 0xFF;
CAND_boSendMessage( 0x14, 8, au8Buffer );
} else if( u8Register == 0x15 ){
S16 Cold = (S16) ( VARH_flGetVariableData( VARH_eTemp_C ) * 100 );
S16 Hot = (S16) ( VARH_flGetVariableData( VARH_eTemp_H ) * 100 );
au8Buffer[0] = ( Cold & 0xFF00 ) >> 8;
au8Buffer[1] = ( Cold & 0x00FF );
au8Buffer[2] = ( Hot & 0xFF00 ) >> 8;
au8Buffer[3] = ( Hot & 0x00FF );
CAND_boSendMessage( 0x16, 4, au8Buffer );
} else if( u8Register == 0x17 ){
PECO_Enable( TRUE );
} else if( u8Register == 0x18 ){
PECO_Enable( FALSE );
}
}
if( u32Flags & EVENT_TIMER_UPDATE )
{
DIPO_vToggleOutput( DIPO_eLED );
}
}
}
//-------------------------------------------------------------------------------------------------
// Function: vEventCallback
// Description: Callback for events
// Parameters: None
// Returns: None
//-------------------------------------------------------------------------------------------------
PRIVATE VOID vEventCallback( PVOID pvData )
{
osEventFlagsSet( m_pstEventID, (U32)pvData );
}
//-------------------------------------------------------------------------------------------------
// Function: vEventCallback
// Description: Callback for events
// Parameters: None
// Returns: None
//-------------------------------------------------------------------------------------------------
PRIVATE VOID vMsgRxCallback( CAND_Message stMessage )
{
osMessageQueuePut( m_pstCANRxMsgQueueID, &stMessage, 0, 0 );
osEventFlagsSet( m_pstEventID, EVENT_NEW_MESSAGE );
}