TECware/Core/Drivers/CAND_CanDriver.c

239 lines
9.0 KiB
C

//=================================================================================================
//
// Company: Paul Scherrer Institut
// 5232 Villigen PSI
// Switzerland
//
//-------------------------------------------------------------------------------------------------
//
// Project: Peltier Controller V2
// Author: Noah Piqué (noah.pique@psi.ch)
//
//-------------------------------------------------------------------------------------------------
//
// Module: Can Driver
// Filename: CAND_CanDriver.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"
// Toolbox
#include "../Toolbox/UTIL_Utility.h"
// Driver
#include "DIPO_DigitalPorts.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 CAND_SHIFT 0x0 // Shift address space (0-3)
#define CAND_BOARD_GROUP 0x03 // Board group (0-127 / 0x00-0x7F)
#define CAND_PRIVATE 1
#define CAND_PUBLIC 0
#define MASTER_ID ((CAND_SHIFT<<6) | 0x0000)
//=================================================================================================
// 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;
CAN_FilterTypeDef stFilter = {
(CAND_SHIFT<<6)|(CAND_PUBLIC<<5)|(CAND_BOARD_GROUP<<1), // FilterIdHigh
0x00, // FilterIdLow
(CAND_SHIFT<<6)|(CAND_PRIVATE<<5)|(CAND_BOARD_GROUP<<1), // FilterMaskIdHigh
0x00, // FilterMaskIdLow
CAN_FILTER_FIFO0, // FilterFIFOAssignment
0x00, // FilterBank
CAN_FILTERMODE_IDLIST, // FilterMode
CAN_FILTERSCALE_32BIT, // FilterScale
CAN_FILTER_ENABLE, // FilterActivation
0x00 // SlaveStartFilterBank
};
//=================================================================================================
// 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;
U8 u8BoardId = 0x00;
u8BoardId |= DIPO_boGetInput(DIPO_eADR0) << 0;
u8BoardId |= DIPO_boGetInput(DIPO_eADR1) << 1;
u8BoardId |= DIPO_boGetInput(DIPO_eADR2) << 2;
//u8BoardId |= DIPO_boGetInput(DIPO_eADR0) << 3; // TODO: New HW
stFilter.FilterIdHigh |= u8BoardId >> 3;
stFilter.FilterIdLow |= u8BoardId << 5;
boOK &= ( HAL_CAN_ConfigFilter(&hcan1, &stFilter) == HAL_OK ) ? TRUE : FALSE;
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( PU8 pu8Buffer, U8 u8Len ){
BOOL boOK = TRUE;
if( u8Len > 8 ) return FALSE;
CAN_TxHeaderTypeDef header = {
MASTER_ID,
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.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;
// TODO: Can Error Handling
}