TECware/Core/Drivers/CAND_CanDriver.c
2021-10-11 15:48:02 +02:00

215 lines
8.0 KiB
C
Raw Blame History

//=================================================================================================
//
// Company: Paul Scherrer Institut
// 5232 Villigen PSI
// Switzerland
//
//-------------------------------------------------------------------------------------------------
//
// Project: Peltier Controller V2
// Author: Noah Piqu<71> (noah.pique@psi.ch)
//
//-------------------------------------------------------------------------------------------------
//
// Module: Analog Ports Output
// Filename: ANPO_AnalogPortsOut.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 outputs ports
//
//=================================================================================================
//=================================================================================================
// Section: INCLUDES
// Description: List of required include files.
//=================================================================================================
#include "../PDEF_ProjectDefinitions.h"
#include "CAND_CanDriver.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).
//=================================================================================================
//=================================================================================================
// 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 VARIABLES
// Description: Definition of local variables (visible by this module only).
//=================================================================================================
CAND_pfnRxCallback m_pfnRxCallback = NULL;
//=================================================================================================
// Section: LOCAL CONSTANTS
// Description: Definition of local constants (visible by this module only).
//=================================================================================================
//=================================================================================================
// Section: LOCAL FUNCTIONS (PROTOTYPES)
// Description: Definition of local functions (visible by this module only).
//=================================================================================================
//=================================================================================================
// Section: EXTERNAL FUNCTIONS
// Description: Definition of external (global) functions.
//=================================================================================================
//=================================================================================================
// Section: EXTERNAL VARIABLES
// Description: Definition of external (global) variables.
//=================================================================================================
extern CAN_HandleTypeDef hcan1;
//=================================================================================================
// Section: GLOBAL FUNCTIONS
// Description: Definition (implementation) of global functions.
//=================================================================================================
//-------------------------------------------------------------------------------------------------
// Function: CAND_boInitializeModule
// Description: Initializes the module. Function must be called once immediately after power-up.
// Parameters: None
// Returns: Boolean, TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL CAND_boInitializeModule( VOID )
{
BOOL boOK = TRUE;
boOK &= HAL_CAN_Start(&hcan1) == HAL_OK ? TRUE : FALSE;
boOK &= HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) == HAL_OK ? TRUE : FALSE;
return( boOK );
}
//-------------------------------------------------------------------------------------------------
// Function: CAND_boSendMessage
// Description: Send a Message over the CAN Interface
// Parameters: U8 Id
// U8 Length
// PU8 Data Buffer
// Returns: Boolean, TRUE if successful
//-------------------------------------------------------------------------------------------------
BOOL CAND_boSendMessage( U8 u8Id, U8 u8Len, PU8 pu8Buffer ){
BOOL boOK = TRUE;
if( u8Len > 8 ) return FALSE;
CAN_TxHeaderTypeDef header = {
u8Id,
0,
CAN_ID_STD,
CAN_RTR_DATA,
u8Len,
DISABLE
};
boOK &= HAL_CAN_AddTxMessage(&hcan1, &header, pu8Buffer, (PU32)CAN_TX_MAILBOX0 ) == HAL_OK ? TRUE : FALSE;
return boOK;
}
//-------------------------------------------------------------------------------------------------
// Function: CAND_vSetRxCallback
// Description: Send a Message over the CAN Interface
// Parameters: CAND_pfnRxCallback Callback Function
// Returns: none
//-------------------------------------------------------------------------------------------------
VOID CAND_vSetRxCallback( CAND_pfnRxCallback pfnRxCallback ){
m_pfnRxCallback = pfnRxCallback;
}
//=================================================================================================
// Section: LOCAL FUNCTIONS
// Descriptionn: Definition (implementation) of local functions.
//=================================================================================================
//-------------------------------------------------------------------------------------------------
// Function: HAL_CAN_RxFifo0MsgPendingCallback
// Description: HAL Can Rx FIFO Msg Pending Callback
// Parameters: CAN_HandleTypeDef *hcan
// Returns: None
//-------------------------------------------------------------------------------------------------
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan){
CAN_RxHeaderTypeDef header;
U8 au8Data[8];
HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &header, au8Data);
CAND_Message stMessage;
stMessage.u8Id = header.StdId;
stMessage.u8Len = header.DLC;
UTIL_vMemCopy(au8Data,stMessage.au8Data,stMessage.u8Len);
m_pfnRxCallback(stMessage);
}
//-------------------------------------------------------------------------------------------------
// Function: HAL_CAN_ErrorCallback
// Description: HAL Can error callback function
// Parameters: CAN_HandleTypeDef *hcan
// Returns: None
//-------------------------------------------------------------------------------------------------
void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan){
U32 u32Error = hcan->ErrorCode;
}