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:
Frank Voorburg 2017-07-26 09:59:15 +00:00
parent 6f7b2381d1
commit 3696d4674b
4 changed files with 1178 additions and 415 deletions

View File

@ -1,208 +1,234 @@
/************************************************************************************//** /************************************************************************************//**
* \file candriver.c * \file candriver.c
* \brief Generic CAN driver source file. * \brief Generic CAN driver source file.
* \ingroup CanDriver * \ingroup CanDriver
* \internal * \internal
*---------------------------------------------------------------------------------------- *----------------------------------------------------------------------------------------
* C O P Y R I G H T * C O P Y R I G H T
*---------------------------------------------------------------------------------------- *----------------------------------------------------------------------------------------
* Copyright (c) 2017 by Feaser http://www.feaser.com All rights reserved * Copyright (c) 2017 by Feaser http://www.feaser.com All rights reserved
* *
*---------------------------------------------------------------------------------------- *----------------------------------------------------------------------------------------
* L I C E N S E * L I C E N S E
*---------------------------------------------------------------------------------------- *----------------------------------------------------------------------------------------
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or * 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 * 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 * Software Foundation, either version 3 of the License, or (at your option) any later
* version. * version.
* *
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * 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 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details. * 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 * 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. * should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
* *
* \endinternal * \endinternal
****************************************************************************************/ ****************************************************************************************/
/**************************************************************************************** /****************************************************************************************
* Include files * Include files
****************************************************************************************/ ****************************************************************************************/
#include <assert.h> /* for assertions */ #include <assert.h> /* for assertions */
#include <stdint.h> /* for standard integer types */ #include <stdint.h> /* for standard integer types */
#include <stddef.h> /* for NULL declaration */ #include <stddef.h> /* for NULL declaration */
#include <stdbool.h> /* for boolean type */ #include <stdbool.h> /* for boolean type */
#include "candriver.h" /* Generic CAN driver module */ #include "candriver.h" /* Generic CAN driver module */
/**************************************************************************************** /****************************************************************************************
* Local data declarations * Local data declarations
****************************************************************************************/ ****************************************************************************************/
/** \brief Pointer to the CAN interface that is linked. */ /** \brief Pointer to the CAN interface that is linked. */
static tCanInterface const * canIfPtr; static tCanInterface const * canIfPtr;
/** \brief Flag to store the connection status. */ /** \brief Flag to store the connection status. */
static bool canConnected; static bool canConnected;
/************************************************************************************//** /************************************************************************************//**
** \brief Initializes the CAN module. Typically called once at program startup. ** \brief Initializes the CAN module. Typically called once at program startup.
** \param settings Pointer to the CAN module settings. ** \param settings Pointer to the CAN module settings.
** \param interface Pointer to the CAN interface to link. ** \param interface Pointer to the CAN interface to link.
** **
****************************************************************************************/ ****************************************************************************************/
void CanInit(tCanSettings const * settings, tCanInterface const * const interface) void CanInit(tCanSettings const * settings, tCanInterface const * const interface)
{ {
/* Initialize locals. */ /* Initialize locals. */
canIfPtr = NULL; canIfPtr = NULL;
canConnected = false; canConnected = false;
/* Check parameters. */ /* Check parameters. */
assert(settings != NULL); assert(settings != NULL);
assert(interface != NULL); assert(interface != NULL);
/* Only continue with valid parameters. */ /* Only continue with valid parameters. */
if ( (settings != NULL) && (interface != NULL) ) /*lint !e774 */ if ( (settings != NULL) && (interface != NULL) ) /*lint !e774 */
{ {
/* Link the CAN interface. */ /* Link the CAN interface. */
canIfPtr = interface; canIfPtr = interface;
/* Initialize the CAN interface. */ /* Initialize the CAN interface. */
canIfPtr->Init(settings); canIfPtr->Init(settings);
} }
} /*** end of CanInit ***/ } /*** end of CanInit ***/
/************************************************************************************//** /************************************************************************************//**
** \brief Terminates the CAN module. Typically called once at program cleanup. ** \brief Terminates the CAN module. Typically called once at program cleanup.
** **
****************************************************************************************/ ****************************************************************************************/
void CanTerminate(void) void CanTerminate(void)
{ {
/* Make sure a valid CAN interface is linked. */ /* Make sure a valid CAN interface is linked. */
assert(canIfPtr != NULL); assert(canIfPtr != NULL);
/* Only continue with a valid CAN interface. */ /* Only continue with a valid CAN interface. */
if (canIfPtr != NULL) /*lint !e774 */ if (canIfPtr != NULL) /*lint !e774 */
{ {
/* Make sure to disconnect first. */ /* Make sure to disconnect first. */
CanDisconnect(); CanDisconnect();
/* Terminate the CAN interface. */ /* Terminate the CAN interface. */
canIfPtr->Terminate(); canIfPtr->Terminate();
/* Unlink the CAN interface. */ /* Unlink the CAN interface. */
canIfPtr = NULL; canIfPtr = NULL;
} }
} /*** end of CanTerminate ***/ } /*** end of CanTerminate ***/
/************************************************************************************//** /************************************************************************************//**
** \brief Connects the CAN module. ** \brief Connects the CAN module.
** \return True if connected, false otherwise. ** \return True if connected, false otherwise.
** **
****************************************************************************************/ ****************************************************************************************/
bool CanConnect(void) bool CanConnect(void)
{ {
bool result = false; bool result = false;
/* Make sure a valid CAN interface is linked. */ /* Make sure a valid CAN interface is linked. */
assert(canIfPtr != NULL); assert(canIfPtr != NULL);
/* Only continue with a valid CAN interface. */ /* Only continue with a valid CAN interface. */
if (canIfPtr != NULL) /*lint !e774 */ if (canIfPtr != NULL) /*lint !e774 */
{ {
/* Make sure that the CAN module is in the disconnected state. */ /* Make sure that the CAN module is in the disconnected state. */
CanDisconnect(); CanDisconnect();
/* Connect the CAN interface. */ /* Connect the CAN interface. */
result = canIfPtr->Connect(); result = canIfPtr->Connect();
/* Update the connection state. */ /* Update the connection state. */
canConnected = result; canConnected = result;
} }
/* Give the result back to the caller. */ /* Give the result back to the caller. */
return result; return result;
} /*** end of CanConnect ***/ } /*** end of CanConnect ***/
/************************************************************************************//** /************************************************************************************//**
** \brief Disconnects the CAN module. ** \brief Disconnects the CAN module.
** **
****************************************************************************************/ ****************************************************************************************/
void CanDisconnect(void) void CanDisconnect(void)
{ {
/* Make sure a valid CAN interface is linked. */ /* Make sure a valid CAN interface is linked. */
assert(canIfPtr != NULL); assert(canIfPtr != NULL);
/* Only continue with a valid CAN interface. */ /* Only continue with a valid CAN interface. */
if (canIfPtr != NULL) /*lint !e774 */ if (canIfPtr != NULL) /*lint !e774 */
{ {
/* Only disconnect if actually connected. */ /* Only disconnect if actually connected. */
if (canConnected) if (canConnected)
{ {
/* Disconnect CAN interface. */ /* Disconnect CAN interface. */
canIfPtr->Disconnect(); canIfPtr->Disconnect();
/* Update the connection state. */ /* Update the connection state. */
canConnected = false; canConnected = false;
} }
} }
} /*** end of CanDisconnect ***/ } /*** end of CanDisconnect ***/
/************************************************************************************//** /************************************************************************************//**
** \brief Obtains the connection state of the CAN module. ** \brief Obtains the connection state of the CAN module.
** \return True if connected, false otherwise. ** \return True if connected, false otherwise.
** **
****************************************************************************************/ ****************************************************************************************/
bool CanIsConnected(void) bool CanIsConnected(void)
{ {
/* Obtain and return the connection state to the caller. */ /* Obtain and return the connection state to the caller. */
return canConnected; return canConnected;
} /*** end of CanIsConnected ***/ } /*** end of CanIsConnected ***/
/************************************************************************************//** /************************************************************************************//**
** \brief Submits a message for transmission on the CAN bus. ** \brief Submits a message for transmission on the CAN bus.
** \param msg Pointer to CAN message structure. ** \param msg Pointer to CAN message structure.
** \return True if successful, false otherwise. ** \return True if successful, false otherwise.
** **
****************************************************************************************/ ****************************************************************************************/
bool CanTransmit(tCanMsg const * msg) bool CanTransmit(tCanMsg const * msg)
{ {
bool result = false; bool result = false;
/* Make sure a valid CAN interface is linked. */ /* Make sure a valid CAN interface is linked. */
assert(canIfPtr != NULL); assert(canIfPtr != NULL);
/* Only continue with a valid CAN interface. */ /* Only continue with a valid CAN interface. */
if (canIfPtr != NULL) /*lint !e774 */ if (canIfPtr != NULL) /*lint !e774 */
{ {
/* Only transmit the message if connected. */ /* Only transmit the message if connected. */
if (canConnected) if (canConnected)
{ {
result = canIfPtr->Transmit(msg); result = canIfPtr->Transmit(msg);
} }
} }
/* Give the result back to the caller. */ /* Give the result back to the caller. */
return result; return result;
} /*** end of CanTransmit ***/ } /*** end of CanTransmit ***/
/************************************************************************************//** /************************************************************************************//**
** \brief Registers the event callback functions that should be called by the CAN ** \brief Checks if a bus off or bus heavy situation occurred.
** module. ** \return True if a bus error situation was detected, false otherwise.
** \param events Pointer to structure with event callback function pointers. **
** ****************************************************************************************/
****************************************************************************************/ bool CanIsBusError(void)
void CanRegisterEvents(tCanEvents const * events) {
{ bool result = false;
/* Make sure a valid CAN interface is linked. */
assert(canIfPtr != NULL); /* Make sure a valid CAN interface is linked. */
assert(canIfPtr != NULL);
/* Only continue with a valid CAN interface. */
if (canIfPtr != NULL) /*lint !e774 */ /* Only continue with a valid CAN interface. */
{ if (canIfPtr != NULL) /*lint !e774 */
/* Register the events with the CAN interface. */ {
canIfPtr->RegisterEvents(events); /* Only check for bus error if connected. */
} if (canConnected)
} /*** end of CanRegisterEvents ***/ {
result = canIfPtr->IsBusError();
}
/*********************************** end of candriver.c ********************************/ }
/* 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 ********************************/

View File

@ -90,13 +90,13 @@ typedef struct t_can_msg
* .mask = 0x00000000 * .mask = 0x00000000
* Example 2: Receive only CAN identifier 0x124 (11-bit or 29-bit) * Example 2: Receive only CAN identifier 0x124 (11-bit or 29-bit)
* .code = 0x00000124 * .code = 0x00000124
* .mask = 0x00000124 * .mask = 0x1fffffff
* Example 3: Receive only CAN identifier 0x124 (11-bit) * Example 3: Receive only CAN identifier 0x124 (11-bit)
* .code = 0x00000124 * .code = 0x00000124
* .mask = 0x80000124 * .mask = 0x9fffffff
* Example 4: Receive only CAN identifier 0x124 (29-bit) * Example 4: Receive only CAN identifier 0x124 (29-bit)
* .code = 0x80000124 * .code = 0x80000124
* .mask = 0x80000124 * .mask = 0x9fffffff
*/ */
typedef struct t_can_settings typedef struct t_can_settings
{ {
@ -129,6 +129,8 @@ typedef struct t_can_interface
void (*Disconnect) (void); void (*Disconnect) (void);
/** \brief Submits a CAN message for transmission. */ /** \brief Submits a CAN message for transmission. */
bool (*Transmit) (tCanMsg const * msg); 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. */ /** \brief Registers the event callback functions. */
void (*RegisterEvents) (tCanEvents const * events); void (*RegisterEvents) (tCanEvents const * events);
} tCanInterface; } tCanInterface;
@ -143,6 +145,7 @@ bool CanConnect(void);
void CanDisconnect(void); void CanDisconnect(void);
bool CanIsConnected(void); bool CanIsConnected(void);
bool CanTransmit(tCanMsg const * msg); bool CanTransmit(tCanMsg const * msg);
bool CanIsBusError(void);
void CanRegisterEvents(tCanEvents const * events); void CanRegisterEvents(tCanEvents const * events);
#ifdef __cplusplus #ifdef __cplusplus

File diff suppressed because it is too large Load Diff

Binary file not shown.