mirror of https://github.com/FOME-Tech/openblt.git
Refs #316. Improvements in the SocketCAN interface module after testing with BootCommander and XCP on CAN.
git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@323 5dc33758-31d5-4daf-9ae8-b24bf3d40d73
This commit is contained in:
parent
c71b3ebbb4
commit
c324e3b877
|
@ -76,7 +76,7 @@ set(LIBOPENBLT_LIB ${PROJECT_OUTPUT_DIRECTORY})
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_WIN32 -D_CRT_SECURE_NO_WARNINGS")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_WIN32 -D_CRT_SECURE_NO_WARNINGS")
|
||||||
elseif(UNIX)
|
elseif(UNIX)
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_LINUX")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_LINUX -pthread")
|
||||||
endif(WIN32)
|
endif(WIN32)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,6 @@
|
||||||
#include <net/if.h> /* network interfaces */
|
#include <net/if.h> /* network interfaces */
|
||||||
#include <linux/can.h> /* CAN kernel definitions */
|
#include <linux/can.h> /* CAN kernel definitions */
|
||||||
#include <linux/can/raw.h> /* CAN raw sockets */
|
#include <linux/can/raw.h> /* CAN raw sockets */
|
||||||
#include <linux/can/error.h> /* CAN errors */
|
|
||||||
#include "util.h" /* Utility module */
|
#include "util.h" /* Utility module */
|
||||||
#include "candriver.h" /* Generic CAN driver module */
|
#include "candriver.h" /* Generic CAN driver module */
|
||||||
#include "socketcan.h" /* SocketCAN interface */
|
#include "socketcan.h" /* SocketCAN interface */
|
||||||
|
@ -104,11 +103,6 @@ static volatile tCanEvents * socketCanEventsList;
|
||||||
/** \brief Total number of event entries into the \ref socketCanEventsList list. */
|
/** \brief Total number of event entries into the \ref socketCanEventsList list. */
|
||||||
static volatile uint32_t socketCanEventsEntries;
|
static volatile uint32_t socketCanEventsEntries;
|
||||||
|
|
||||||
/** \brief Flag to set in the event thread when either a bus off or bus heavy situation
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
static volatile bool socketCanErrorDetected;
|
|
||||||
|
|
||||||
/** \brief Event thread control. */
|
/** \brief Event thread control. */
|
||||||
static volatile tSocketCanThreadCtrl eventThreadCtrl;
|
static volatile tSocketCanThreadCtrl eventThreadCtrl;
|
||||||
|
|
||||||
|
@ -145,7 +139,6 @@ static void SocketCanInit(tCanSettings const * settings)
|
||||||
/* Initialize locals. */
|
/* Initialize locals. */
|
||||||
socketCanEventsList = NULL;
|
socketCanEventsList = NULL;
|
||||||
socketCanEventsEntries = 0;
|
socketCanEventsEntries = 0;
|
||||||
socketCanErrorDetected = false;
|
|
||||||
/* Reset CAN interface settings. */
|
/* Reset CAN interface settings. */
|
||||||
socketCanSettings.devicename = "";
|
socketCanSettings.devicename = "";
|
||||||
socketCanSettings.channel = 0;
|
socketCanSettings.channel = 0;
|
||||||
|
@ -224,10 +217,6 @@ static bool SocketCanConnect(void)
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
int32_t flags;
|
int32_t flags;
|
||||||
struct can_filter rxFilter;
|
struct can_filter rxFilter;
|
||||||
can_err_mask_t errMask;
|
|
||||||
|
|
||||||
/* Reset the error flag. */
|
|
||||||
socketCanErrorDetected = false;
|
|
||||||
|
|
||||||
/* Check settings. */
|
/* Check settings. */
|
||||||
assert(socketCanSettings.devicename != NULL);
|
assert(socketCanSettings.devicename != NULL);
|
||||||
|
@ -314,17 +303,6 @@ static bool SocketCanConnect(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Configure reception of bus off and other CAN controller status related events. */
|
|
||||||
if (result)
|
|
||||||
{
|
|
||||||
errMask = (CAN_ERR_BUSOFF | CAN_ERR_CRTL);
|
|
||||||
if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &errMask, sizeof(errMask)) != 0)
|
|
||||||
{
|
|
||||||
close(canSocket);
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
/* Start the event thread. */
|
/* Start the event thread. */
|
||||||
|
@ -350,8 +328,6 @@ static void SocketCanDisconnect(void)
|
||||||
SocketCanStopEventThread();
|
SocketCanStopEventThread();
|
||||||
/* Close the socket. */
|
/* Close the socket. */
|
||||||
close(canSocket);
|
close(canSocket);
|
||||||
/* Reset the error flag. */
|
|
||||||
socketCanErrorDetected = false;
|
|
||||||
} /*** end of SocketCanDisconnect ***/
|
} /*** end of SocketCanDisconnect ***/
|
||||||
|
|
||||||
|
|
||||||
|
@ -421,16 +397,15 @@ static bool SocketCanIsBusError(void)
|
||||||
{
|
{
|
||||||
bool result= false;
|
bool result= false;
|
||||||
|
|
||||||
/* Read flag to detect bus off or bus heavy. */
|
/* The purpose of this function is that when an error is detected, SocketCanConnect()
|
||||||
UtilCriticalSectionEnter();
|
* can be called to reset the error. With SocketCAN a bus off error is reset on
|
||||||
if (socketCanErrorDetected)
|
* network link level and not by this interface module. It is therefore not needed to
|
||||||
{
|
* check for the errors here. It is better to configure the automatic bus off recovery
|
||||||
/* Update result. */
|
* on network link level. For example:
|
||||||
result = true;
|
* ip link set can0 type can bitrate 500000 restart-ms 100
|
||||||
/* Reset the error flag. */
|
* where "restart-ms <value>" specifies the number of milliseconds after which that
|
||||||
socketCanErrorDetected = false;
|
* automatic recovery is started.
|
||||||
}
|
*/
|
||||||
UtilCriticalSectionExit();
|
|
||||||
|
|
||||||
/* Give the result back to the caller. */
|
/* Give the result back to the caller. */
|
||||||
return result;
|
return result;
|
||||||
|
@ -555,35 +530,11 @@ static void *SocketCanEventThread(void *param)
|
||||||
/* Check if CAN frames were received. */
|
/* Check if CAN frames were received. */
|
||||||
while (read(canSocket, &canRxFrame, sizeof(struct can_frame)) == (ssize_t)sizeof(struct can_frame))
|
while (read(canSocket, &canRxFrame, sizeof(struct can_frame)) == (ssize_t)sizeof(struct can_frame))
|
||||||
{
|
{
|
||||||
/* Ignore remote frames */
|
/* Ignore remote frames and error information. */
|
||||||
if (canRxFrame.can_id & CAN_RTR_FLAG)
|
if ( (canRxFrame.can_id & CAN_RTR_FLAG) || (canRxFrame.can_id & CAN_ERR_FLAG) )
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* Does the message contain error information? */
|
|
||||||
else if (canRxFrame.can_id & CAN_ERR_FLAG)
|
|
||||||
{
|
|
||||||
/* Was it a bus off event? */
|
|
||||||
if ((canRxFrame.can_id & CAN_ERR_BUSOFF) != 0)
|
|
||||||
{
|
|
||||||
/* Set the error flag. */
|
|
||||||
UtilCriticalSectionEnter();
|
|
||||||
socketCanErrorDetected = true;
|
|
||||||
UtilCriticalSectionExit();
|
|
||||||
}
|
|
||||||
/* Was it a CAN controller event? */
|
|
||||||
else if ((canRxFrame.can_id & CAN_ERR_CRTL) != 0)
|
|
||||||
{
|
|
||||||
/* Is the controller in error passive mode (bus heavy)? */
|
|
||||||
if ((canRxFrame.data[1] & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE)) != 0)
|
|
||||||
{
|
|
||||||
/* Set the error flag. */
|
|
||||||
UtilCriticalSectionEnter();
|
|
||||||
socketCanErrorDetected = true;
|
|
||||||
UtilCriticalSectionExit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* It was a regular CAN message with either 11- or 29-bit identifier. */
|
/* It was a regular CAN message with either 11- or 29-bit identifier. */
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -40,13 +40,10 @@
|
||||||
* Local data declarations
|
* Local data declarations
|
||||||
****************************************************************************************/
|
****************************************************************************************/
|
||||||
/** \brief Flag to determine if the critical section object was already initialized. */
|
/** \brief Flag to determine if the critical section object was already initialized. */
|
||||||
static bool criticalSectionInitialized = false;
|
static volatile bool criticalSectionInitialized = false;
|
||||||
|
|
||||||
/** \brief Critical section nesting counter. ***/
|
|
||||||
static uint32_t criticalSectionNesting;
|
|
||||||
|
|
||||||
/** \brief Critical section object. */
|
/** \brief Critical section object. */
|
||||||
static pthread_mutex_t mtxCritSect;
|
static volatile pthread_mutex_t mtxCritSect;
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************************//**
|
/************************************************************************************//**
|
||||||
|
@ -61,9 +58,7 @@ void UtilCriticalSectionInit(void)
|
||||||
if (!criticalSectionInitialized)
|
if (!criticalSectionInitialized)
|
||||||
{
|
{
|
||||||
/* Initialize the critical section object. */
|
/* Initialize the critical section object. */
|
||||||
(void)pthread_mutex_init(&mtxCritSect, NULL);
|
(void)pthread_mutex_init((pthread_mutex_t *)&mtxCritSect, NULL);
|
||||||
/* Reset nesting counter. */
|
|
||||||
criticalSectionNesting = 0;
|
|
||||||
/* Set initialized flag. */
|
/* Set initialized flag. */
|
||||||
criticalSectionInitialized = true;
|
criticalSectionInitialized = true;
|
||||||
}
|
}
|
||||||
|
@ -84,10 +79,8 @@ void UtilCriticalSectionTerminate(void)
|
||||||
{
|
{
|
||||||
/* Reset the initialized flag. */
|
/* Reset the initialized flag. */
|
||||||
criticalSectionInitialized = false;
|
criticalSectionInitialized = false;
|
||||||
/* Reset nesting counter. */
|
|
||||||
criticalSectionNesting = 0;
|
|
||||||
/* Delete the critical section object. */
|
/* Delete the critical section object. */
|
||||||
(void)pthread_mutex_destroy(&mtxCritSect);
|
(void)pthread_mutex_destroy((pthread_mutex_t *)&mtxCritSect);
|
||||||
}
|
}
|
||||||
} /*** end of UtilCriticalSectionTerminate ***/
|
} /*** end of UtilCriticalSectionTerminate ***/
|
||||||
|
|
||||||
|
@ -106,13 +99,7 @@ void UtilCriticalSectionEnter(void)
|
||||||
/* Only continue if actually initialized. */
|
/* Only continue if actually initialized. */
|
||||||
if (criticalSectionInitialized)
|
if (criticalSectionInitialized)
|
||||||
{
|
{
|
||||||
/* Enter the critical section if not already entered. */
|
(void)pthread_mutex_lock((pthread_mutex_t *)&mtxCritSect);
|
||||||
if (criticalSectionNesting == 0)
|
|
||||||
{
|
|
||||||
(void)pthread_mutex_lock(&mtxCritSect);
|
|
||||||
}
|
|
||||||
/* Increment nesting counter. */
|
|
||||||
criticalSectionNesting++; /*lint !e456 */
|
|
||||||
}
|
}
|
||||||
} /*** end of UtilCriticalSectionEnter ***/ /*lint !e456 !e454 */
|
} /*** end of UtilCriticalSectionEnter ***/ /*lint !e456 !e454 */
|
||||||
|
|
||||||
|
@ -130,19 +117,7 @@ void UtilCriticalSectionExit(void)
|
||||||
/* Only continue if actually initialized. */
|
/* Only continue if actually initialized. */
|
||||||
if (criticalSectionInitialized)
|
if (criticalSectionInitialized)
|
||||||
{
|
{
|
||||||
/* Sanity check. */
|
(void)pthread_mutex_unlock((pthread_mutex_t *)&mtxCritSect); /*lint !e455 */
|
||||||
assert(criticalSectionNesting > 0);
|
|
||||||
|
|
||||||
/* Decrement nesting counter if it is valid. */
|
|
||||||
if (criticalSectionNesting > 0)
|
|
||||||
{
|
|
||||||
criticalSectionNesting--;
|
|
||||||
/* Leave the critical section. */
|
|
||||||
if (criticalSectionNesting == 0)
|
|
||||||
{
|
|
||||||
(void)pthread_mutex_unlock (&mtxCritSect); /*lint !e455 */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} /*** end of UtilCriticalSectionExit ***/
|
} /*** end of UtilCriticalSectionExit ***/
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,7 @@
|
||||||
* Local data declarations
|
* Local data declarations
|
||||||
****************************************************************************************/
|
****************************************************************************************/
|
||||||
/** \brief Flag to determine if the critical section object was already initialized. */
|
/** \brief Flag to determine if the critical section object was already initialized. */
|
||||||
static bool criticalSectionInitialized = false;
|
static volatile bool criticalSectionInitialized = false;
|
||||||
|
|
||||||
/** \brief Critical section nesting counter. ***/
|
|
||||||
static uint32_t criticalSectionNesting;
|
|
||||||
|
|
||||||
/** \brief Critical section object. */
|
/** \brief Critical section object. */
|
||||||
static CRITICAL_SECTION criticalSection;
|
static CRITICAL_SECTION criticalSection;
|
||||||
|
@ -61,9 +58,7 @@ void UtilCriticalSectionInit(void)
|
||||||
if (!criticalSectionInitialized)
|
if (!criticalSectionInitialized)
|
||||||
{
|
{
|
||||||
/* Initialize the critical section object. */
|
/* Initialize the critical section object. */
|
||||||
InitializeCriticalSection(&criticalSection);
|
InitializeCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||||
/* Reset nesting counter. */
|
|
||||||
criticalSectionNesting = 0;
|
|
||||||
/* Set initialized flag. */
|
/* Set initialized flag. */
|
||||||
criticalSectionInitialized = true;
|
criticalSectionInitialized = true;
|
||||||
}
|
}
|
||||||
|
@ -84,10 +79,8 @@ void UtilCriticalSectionTerminate(void)
|
||||||
{
|
{
|
||||||
/* Reset the initialized flag. */
|
/* Reset the initialized flag. */
|
||||||
criticalSectionInitialized = false;
|
criticalSectionInitialized = false;
|
||||||
/* Reset nesting counter. */
|
|
||||||
criticalSectionNesting = 0;
|
|
||||||
/* Delete the critical section object. */
|
/* Delete the critical section object. */
|
||||||
DeleteCriticalSection(&criticalSection);
|
DeleteCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||||
}
|
}
|
||||||
} /*** end of UtilCriticalSectionTerminate ***/
|
} /*** end of UtilCriticalSectionTerminate ***/
|
||||||
|
|
||||||
|
@ -106,13 +99,7 @@ void UtilCriticalSectionEnter(void)
|
||||||
/* Only continue if actually initialized. */
|
/* Only continue if actually initialized. */
|
||||||
if (criticalSectionInitialized)
|
if (criticalSectionInitialized)
|
||||||
{
|
{
|
||||||
/* Enter the critical section if not already entered. */
|
EnterCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||||
if (criticalSectionNesting == 0)
|
|
||||||
{
|
|
||||||
EnterCriticalSection(&criticalSection);
|
|
||||||
}
|
|
||||||
/* Increment nesting counter. */
|
|
||||||
criticalSectionNesting++;
|
|
||||||
}
|
}
|
||||||
} /*** end of UtilCriticalSectionEnter ***/
|
} /*** end of UtilCriticalSectionEnter ***/
|
||||||
|
|
||||||
|
@ -130,19 +117,7 @@ void UtilCriticalSectionExit(void)
|
||||||
/* Only continue if actually initialized. */
|
/* Only continue if actually initialized. */
|
||||||
if (criticalSectionInitialized)
|
if (criticalSectionInitialized)
|
||||||
{
|
{
|
||||||
/* Sanity check. */
|
LeaveCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||||
assert(criticalSectionNesting > 0);
|
|
||||||
|
|
||||||
/* Decrement nesting counter if it is valid. */
|
|
||||||
if (criticalSectionNesting > 0)
|
|
||||||
{
|
|
||||||
criticalSectionNesting--;
|
|
||||||
/* Leave the critical section. */
|
|
||||||
if (criticalSectionNesting == 0)
|
|
||||||
{
|
|
||||||
LeaveCriticalSection(&criticalSection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} /*** end of UtilCriticalSectionExit ***/
|
} /*** end of UtilCriticalSectionExit ***/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue