mirror of https://github.com/FOME-Tech/openblt.git
Refs #316. Completed implementation of the Peak PCAN-USB interface module.
git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@312 5dc33758-31d5-4daf-9ae8-b24bf3d40d73
This commit is contained in:
parent
6f7b2381d1
commit
3696d4674b
|
@ -1,208 +1,234 @@
|
|||
/************************************************************************************//**
|
||||
* \file candriver.c
|
||||
* \brief Generic CAN driver source file.
|
||||
* \ingroup CanDriver
|
||||
* \internal
|
||||
*----------------------------------------------------------------------------------------
|
||||
* C O P Y R I G H T
|
||||
*----------------------------------------------------------------------------------------
|
||||
* Copyright (c) 2017 by Feaser http://www.feaser.com All rights reserved
|
||||
*
|
||||
*----------------------------------------------------------------------------------------
|
||||
* L I C E N S E
|
||||
*----------------------------------------------------------------------------------------
|
||||
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You have received a copy of the GNU General Public License along with OpenBLT. It
|
||||
* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
|
||||
*
|
||||
* \endinternal
|
||||
****************************************************************************************/
|
||||
|
||||
/****************************************************************************************
|
||||
* Include files
|
||||
****************************************************************************************/
|
||||
#include <assert.h> /* for assertions */
|
||||
#include <stdint.h> /* for standard integer types */
|
||||
#include <stddef.h> /* for NULL declaration */
|
||||
#include <stdbool.h> /* for boolean type */
|
||||
#include "candriver.h" /* Generic CAN driver module */
|
||||
|
||||
|
||||
/****************************************************************************************
|
||||
* Local data declarations
|
||||
****************************************************************************************/
|
||||
/** \brief Pointer to the CAN interface that is linked. */
|
||||
static tCanInterface const * canIfPtr;
|
||||
|
||||
/** \brief Flag to store the connection status. */
|
||||
static bool canConnected;
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Initializes the CAN module. Typically called once at program startup.
|
||||
** \param settings Pointer to the CAN module settings.
|
||||
** \param interface Pointer to the CAN interface to link.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanInit(tCanSettings const * settings, tCanInterface const * const interface)
|
||||
{
|
||||
/* Initialize locals. */
|
||||
canIfPtr = NULL;
|
||||
canConnected = false;
|
||||
|
||||
/* Check parameters. */
|
||||
assert(settings != NULL);
|
||||
assert(interface != NULL);
|
||||
|
||||
/* Only continue with valid parameters. */
|
||||
if ( (settings != NULL) && (interface != NULL) ) /*lint !e774 */
|
||||
{
|
||||
/* Link the CAN interface. */
|
||||
canIfPtr = interface;
|
||||
/* Initialize the CAN interface. */
|
||||
canIfPtr->Init(settings);
|
||||
}
|
||||
} /*** end of CanInit ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Terminates the CAN module. Typically called once at program cleanup.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanTerminate(void)
|
||||
{
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Make sure to disconnect first. */
|
||||
CanDisconnect();
|
||||
/* Terminate the CAN interface. */
|
||||
canIfPtr->Terminate();
|
||||
/* Unlink the CAN interface. */
|
||||
canIfPtr = NULL;
|
||||
}
|
||||
} /*** end of CanTerminate ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Connects the CAN module.
|
||||
** \return True if connected, false otherwise.
|
||||
**
|
||||
****************************************************************************************/
|
||||
bool CanConnect(void)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Make sure that the CAN module is in the disconnected state. */
|
||||
CanDisconnect();
|
||||
/* Connect the CAN interface. */
|
||||
result = canIfPtr->Connect();
|
||||
/* Update the connection state. */
|
||||
canConnected = result;
|
||||
}
|
||||
/* Give the result back to the caller. */
|
||||
return result;
|
||||
} /*** end of CanConnect ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Disconnects the CAN module.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanDisconnect(void)
|
||||
{
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Only disconnect if actually connected. */
|
||||
if (canConnected)
|
||||
{
|
||||
/* Disconnect CAN interface. */
|
||||
canIfPtr->Disconnect();
|
||||
/* Update the connection state. */
|
||||
canConnected = false;
|
||||
}
|
||||
}
|
||||
} /*** end of CanDisconnect ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Obtains the connection state of the CAN module.
|
||||
** \return True if connected, false otherwise.
|
||||
**
|
||||
****************************************************************************************/
|
||||
bool CanIsConnected(void)
|
||||
{
|
||||
/* Obtain and return the connection state to the caller. */
|
||||
return canConnected;
|
||||
} /*** end of CanIsConnected ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Submits a message for transmission on the CAN bus.
|
||||
** \param msg Pointer to CAN message structure.
|
||||
** \return True if successful, false otherwise.
|
||||
**
|
||||
****************************************************************************************/
|
||||
bool CanTransmit(tCanMsg const * msg)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Only transmit the message if connected. */
|
||||
if (canConnected)
|
||||
{
|
||||
result = canIfPtr->Transmit(msg);
|
||||
}
|
||||
}
|
||||
/* Give the result back to the caller. */
|
||||
return result;
|
||||
} /*** end of CanTransmit ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Registers the event callback functions that should be called by the CAN
|
||||
** module.
|
||||
** \param events Pointer to structure with event callback function pointers.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanRegisterEvents(tCanEvents const * events)
|
||||
{
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Register the events with the CAN interface. */
|
||||
canIfPtr->RegisterEvents(events);
|
||||
}
|
||||
} /*** end of CanRegisterEvents ***/
|
||||
|
||||
|
||||
/*********************************** end of candriver.c ********************************/
|
||||
/************************************************************************************//**
|
||||
* \file candriver.c
|
||||
* \brief Generic CAN driver source file.
|
||||
* \ingroup CanDriver
|
||||
* \internal
|
||||
*----------------------------------------------------------------------------------------
|
||||
* C O P Y R I G H T
|
||||
*----------------------------------------------------------------------------------------
|
||||
* Copyright (c) 2017 by Feaser http://www.feaser.com All rights reserved
|
||||
*
|
||||
*----------------------------------------------------------------------------------------
|
||||
* L I C E N S E
|
||||
*----------------------------------------------------------------------------------------
|
||||
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, either version 3 of the License, or (at your option) any later
|
||||
* version.
|
||||
*
|
||||
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You have received a copy of the GNU General Public License along with OpenBLT. It
|
||||
* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
|
||||
*
|
||||
* \endinternal
|
||||
****************************************************************************************/
|
||||
|
||||
/****************************************************************************************
|
||||
* Include files
|
||||
****************************************************************************************/
|
||||
#include <assert.h> /* for assertions */
|
||||
#include <stdint.h> /* for standard integer types */
|
||||
#include <stddef.h> /* for NULL declaration */
|
||||
#include <stdbool.h> /* for boolean type */
|
||||
#include "candriver.h" /* Generic CAN driver module */
|
||||
|
||||
|
||||
/****************************************************************************************
|
||||
* Local data declarations
|
||||
****************************************************************************************/
|
||||
/** \brief Pointer to the CAN interface that is linked. */
|
||||
static tCanInterface const * canIfPtr;
|
||||
|
||||
/** \brief Flag to store the connection status. */
|
||||
static bool canConnected;
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Initializes the CAN module. Typically called once at program startup.
|
||||
** \param settings Pointer to the CAN module settings.
|
||||
** \param interface Pointer to the CAN interface to link.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanInit(tCanSettings const * settings, tCanInterface const * const interface)
|
||||
{
|
||||
/* Initialize locals. */
|
||||
canIfPtr = NULL;
|
||||
canConnected = false;
|
||||
|
||||
/* Check parameters. */
|
||||
assert(settings != NULL);
|
||||
assert(interface != NULL);
|
||||
|
||||
/* Only continue with valid parameters. */
|
||||
if ( (settings != NULL) && (interface != NULL) ) /*lint !e774 */
|
||||
{
|
||||
/* Link the CAN interface. */
|
||||
canIfPtr = interface;
|
||||
/* Initialize the CAN interface. */
|
||||
canIfPtr->Init(settings);
|
||||
}
|
||||
} /*** end of CanInit ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Terminates the CAN module. Typically called once at program cleanup.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanTerminate(void)
|
||||
{
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Make sure to disconnect first. */
|
||||
CanDisconnect();
|
||||
/* Terminate the CAN interface. */
|
||||
canIfPtr->Terminate();
|
||||
/* Unlink the CAN interface. */
|
||||
canIfPtr = NULL;
|
||||
}
|
||||
} /*** end of CanTerminate ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Connects the CAN module.
|
||||
** \return True if connected, false otherwise.
|
||||
**
|
||||
****************************************************************************************/
|
||||
bool CanConnect(void)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Make sure that the CAN module is in the disconnected state. */
|
||||
CanDisconnect();
|
||||
/* Connect the CAN interface. */
|
||||
result = canIfPtr->Connect();
|
||||
/* Update the connection state. */
|
||||
canConnected = result;
|
||||
}
|
||||
/* Give the result back to the caller. */
|
||||
return result;
|
||||
} /*** end of CanConnect ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Disconnects the CAN module.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanDisconnect(void)
|
||||
{
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Only disconnect if actually connected. */
|
||||
if (canConnected)
|
||||
{
|
||||
/* Disconnect CAN interface. */
|
||||
canIfPtr->Disconnect();
|
||||
/* Update the connection state. */
|
||||
canConnected = false;
|
||||
}
|
||||
}
|
||||
} /*** end of CanDisconnect ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Obtains the connection state of the CAN module.
|
||||
** \return True if connected, false otherwise.
|
||||
**
|
||||
****************************************************************************************/
|
||||
bool CanIsConnected(void)
|
||||
{
|
||||
/* Obtain and return the connection state to the caller. */
|
||||
return canConnected;
|
||||
} /*** end of CanIsConnected ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Submits a message for transmission on the CAN bus.
|
||||
** \param msg Pointer to CAN message structure.
|
||||
** \return True if successful, false otherwise.
|
||||
**
|
||||
****************************************************************************************/
|
||||
bool CanTransmit(tCanMsg const * msg)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Only transmit the message if connected. */
|
||||
if (canConnected)
|
||||
{
|
||||
result = canIfPtr->Transmit(msg);
|
||||
}
|
||||
}
|
||||
/* Give the result back to the caller. */
|
||||
return result;
|
||||
} /*** end of CanTransmit ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Checks if a bus off or bus heavy situation occurred.
|
||||
** \return True if a bus error situation was detected, false otherwise.
|
||||
**
|
||||
****************************************************************************************/
|
||||
bool CanIsBusError(void)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Only check for bus error if connected. */
|
||||
if (canConnected)
|
||||
{
|
||||
result = canIfPtr->IsBusError();
|
||||
}
|
||||
}
|
||||
/* Give the result back to the caller. */
|
||||
return result;
|
||||
} /*** end of CanIsBusError ***/
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
** \brief Registers the event callback functions that should be called by the CAN
|
||||
** module.
|
||||
** \param events Pointer to structure with event callback function pointers.
|
||||
**
|
||||
****************************************************************************************/
|
||||
void CanRegisterEvents(tCanEvents const * events)
|
||||
{
|
||||
/* Make sure a valid CAN interface is linked. */
|
||||
assert(canIfPtr != NULL);
|
||||
|
||||
/* Only continue with a valid CAN interface. */
|
||||
if (canIfPtr != NULL) /*lint !e774 */
|
||||
{
|
||||
/* Register the events with the CAN interface. */
|
||||
canIfPtr->RegisterEvents(events);
|
||||
}
|
||||
} /*** end of CanRegisterEvents ***/
|
||||
|
||||
|
||||
/*********************************** end of candriver.c ********************************/
|
||||
|
|
|
@ -90,13 +90,13 @@ typedef struct t_can_msg
|
|||
* .mask = 0x00000000
|
||||
* Example 2: Receive only CAN identifier 0x124 (11-bit or 29-bit)
|
||||
* .code = 0x00000124
|
||||
* .mask = 0x00000124
|
||||
* .mask = 0x1fffffff
|
||||
* Example 3: Receive only CAN identifier 0x124 (11-bit)
|
||||
* .code = 0x00000124
|
||||
* .mask = 0x80000124
|
||||
* .mask = 0x9fffffff
|
||||
* Example 4: Receive only CAN identifier 0x124 (29-bit)
|
||||
* .code = 0x80000124
|
||||
* .mask = 0x80000124
|
||||
* .mask = 0x9fffffff
|
||||
*/
|
||||
typedef struct t_can_settings
|
||||
{
|
||||
|
@ -129,6 +129,8 @@ typedef struct t_can_interface
|
|||
void (*Disconnect) (void);
|
||||
/** \brief Submits a CAN message for transmission. */
|
||||
bool (*Transmit) (tCanMsg const * msg);
|
||||
/** \brief Check if a bus off and/or bus heavy situation occurred. */
|
||||
bool (*IsBusError) (void);
|
||||
/** \brief Registers the event callback functions. */
|
||||
void (*RegisterEvents) (tCanEvents const * events);
|
||||
} tCanInterface;
|
||||
|
@ -143,6 +145,7 @@ bool CanConnect(void);
|
|||
void CanDisconnect(void);
|
||||
bool CanIsConnected(void);
|
||||
bool CanTransmit(tCanMsg const * msg);
|
||||
bool CanIsBusError(void);
|
||||
void CanRegisterEvents(tCanEvents const * events);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Loading…
Reference in New Issue