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)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_WIN32 -D_CRT_SECURE_NO_WARNINGS")
|
||||
elseif(UNIX)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_LINUX")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_LINUX -pthread")
|
||||
endif(WIN32)
|
||||
|
||||
|
||||
|
|
|
@ -42,7 +42,6 @@
|
|||
#include <net/if.h> /* network interfaces */
|
||||
#include <linux/can.h> /* CAN kernel definitions */
|
||||
#include <linux/can/raw.h> /* CAN raw sockets */
|
||||
#include <linux/can/error.h> /* CAN errors */
|
||||
#include "util.h" /* Utility module */
|
||||
#include "candriver.h" /* Generic CAN driver module */
|
||||
#include "socketcan.h" /* SocketCAN interface */
|
||||
|
@ -104,11 +103,6 @@ static volatile tCanEvents * socketCanEventsList;
|
|||
/** \brief Total number of event entries into the \ref socketCanEventsList list. */
|
||||
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. */
|
||||
static volatile tSocketCanThreadCtrl eventThreadCtrl;
|
||||
|
||||
|
@ -145,7 +139,6 @@ static void SocketCanInit(tCanSettings const * settings)
|
|||
/* Initialize locals. */
|
||||
socketCanEventsList = NULL;
|
||||
socketCanEventsEntries = 0;
|
||||
socketCanErrorDetected = false;
|
||||
/* Reset CAN interface settings. */
|
||||
socketCanSettings.devicename = "";
|
||||
socketCanSettings.channel = 0;
|
||||
|
@ -224,10 +217,6 @@ static bool SocketCanConnect(void)
|
|||
struct ifreq ifr;
|
||||
int32_t flags;
|
||||
struct can_filter rxFilter;
|
||||
can_err_mask_t errMask;
|
||||
|
||||
/* Reset the error flag. */
|
||||
socketCanErrorDetected = false;
|
||||
|
||||
/* Check settings. */
|
||||
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)
|
||||
{
|
||||
/* Start the event thread. */
|
||||
|
@ -350,8 +328,6 @@ static void SocketCanDisconnect(void)
|
|||
SocketCanStopEventThread();
|
||||
/* Close the socket. */
|
||||
close(canSocket);
|
||||
/* Reset the error flag. */
|
||||
socketCanErrorDetected = false;
|
||||
} /*** end of SocketCanDisconnect ***/
|
||||
|
||||
|
||||
|
@ -421,16 +397,15 @@ static bool SocketCanIsBusError(void)
|
|||
{
|
||||
bool result= false;
|
||||
|
||||
/* Read flag to detect bus off or bus heavy. */
|
||||
UtilCriticalSectionEnter();
|
||||
if (socketCanErrorDetected)
|
||||
{
|
||||
/* Update result. */
|
||||
result = true;
|
||||
/* Reset the error flag. */
|
||||
socketCanErrorDetected = false;
|
||||
}
|
||||
UtilCriticalSectionExit();
|
||||
/* The purpose of this function is that when an error is detected, SocketCanConnect()
|
||||
* can be called to reset the error. With SocketCAN a bus off error is reset on
|
||||
* 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
|
||||
* on network link level. For example:
|
||||
* ip link set can0 type can bitrate 500000 restart-ms 100
|
||||
* where "restart-ms <value>" specifies the number of milliseconds after which that
|
||||
* automatic recovery is started.
|
||||
*/
|
||||
|
||||
/* Give the result back to the caller. */
|
||||
return result;
|
||||
|
@ -555,35 +530,11 @@ static void *SocketCanEventThread(void *param)
|
|||
/* Check if CAN frames were received. */
|
||||
while (read(canSocket, &canRxFrame, sizeof(struct can_frame)) == (ssize_t)sizeof(struct can_frame))
|
||||
{
|
||||
/* Ignore remote frames */
|
||||
if (canRxFrame.can_id & CAN_RTR_FLAG)
|
||||
/* Ignore remote frames and error information. */
|
||||
if ( (canRxFrame.can_id & CAN_RTR_FLAG) || (canRxFrame.can_id & CAN_ERR_FLAG) )
|
||||
{
|
||||
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. */
|
||||
else
|
||||
{
|
||||
|
|
|
@ -40,13 +40,10 @@
|
|||
* Local data declarations
|
||||
****************************************************************************************/
|
||||
/** \brief Flag to determine if the critical section object was already initialized. */
|
||||
static bool criticalSectionInitialized = false;
|
||||
|
||||
/** \brief Critical section nesting counter. ***/
|
||||
static uint32_t criticalSectionNesting;
|
||||
static volatile bool criticalSectionInitialized = false;
|
||||
|
||||
/** \brief Critical section object. */
|
||||
static pthread_mutex_t mtxCritSect;
|
||||
static volatile pthread_mutex_t mtxCritSect;
|
||||
|
||||
|
||||
/************************************************************************************//**
|
||||
|
@ -61,9 +58,7 @@ void UtilCriticalSectionInit(void)
|
|||
if (!criticalSectionInitialized)
|
||||
{
|
||||
/* Initialize the critical section object. */
|
||||
(void)pthread_mutex_init(&mtxCritSect, NULL);
|
||||
/* Reset nesting counter. */
|
||||
criticalSectionNesting = 0;
|
||||
(void)pthread_mutex_init((pthread_mutex_t *)&mtxCritSect, NULL);
|
||||
/* Set initialized flag. */
|
||||
criticalSectionInitialized = true;
|
||||
}
|
||||
|
@ -84,10 +79,8 @@ void UtilCriticalSectionTerminate(void)
|
|||
{
|
||||
/* Reset the initialized flag. */
|
||||
criticalSectionInitialized = false;
|
||||
/* Reset nesting counter. */
|
||||
criticalSectionNesting = 0;
|
||||
/* Delete the critical section object. */
|
||||
(void)pthread_mutex_destroy(&mtxCritSect);
|
||||
(void)pthread_mutex_destroy((pthread_mutex_t *)&mtxCritSect);
|
||||
}
|
||||
} /*** end of UtilCriticalSectionTerminate ***/
|
||||
|
||||
|
@ -106,13 +99,7 @@ void UtilCriticalSectionEnter(void)
|
|||
/* Only continue if actually initialized. */
|
||||
if (criticalSectionInitialized)
|
||||
{
|
||||
/* Enter the critical section if not already entered. */
|
||||
if (criticalSectionNesting == 0)
|
||||
{
|
||||
(void)pthread_mutex_lock(&mtxCritSect);
|
||||
}
|
||||
/* Increment nesting counter. */
|
||||
criticalSectionNesting++; /*lint !e456 */
|
||||
(void)pthread_mutex_lock((pthread_mutex_t *)&mtxCritSect);
|
||||
}
|
||||
} /*** end of UtilCriticalSectionEnter ***/ /*lint !e456 !e454 */
|
||||
|
||||
|
@ -130,19 +117,7 @@ void UtilCriticalSectionExit(void)
|
|||
/* Only continue if actually initialized. */
|
||||
if (criticalSectionInitialized)
|
||||
{
|
||||
/* Sanity check. */
|
||||
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 */
|
||||
}
|
||||
}
|
||||
(void)pthread_mutex_unlock((pthread_mutex_t *)&mtxCritSect); /*lint !e455 */
|
||||
}
|
||||
} /*** end of UtilCriticalSectionExit ***/
|
||||
|
||||
|
|
|
@ -40,10 +40,7 @@
|
|||
* Local data declarations
|
||||
****************************************************************************************/
|
||||
/** \brief Flag to determine if the critical section object was already initialized. */
|
||||
static bool criticalSectionInitialized = false;
|
||||
|
||||
/** \brief Critical section nesting counter. ***/
|
||||
static uint32_t criticalSectionNesting;
|
||||
static volatile bool criticalSectionInitialized = false;
|
||||
|
||||
/** \brief Critical section object. */
|
||||
static CRITICAL_SECTION criticalSection;
|
||||
|
@ -61,9 +58,7 @@ void UtilCriticalSectionInit(void)
|
|||
if (!criticalSectionInitialized)
|
||||
{
|
||||
/* Initialize the critical section object. */
|
||||
InitializeCriticalSection(&criticalSection);
|
||||
/* Reset nesting counter. */
|
||||
criticalSectionNesting = 0;
|
||||
InitializeCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||
/* Set initialized flag. */
|
||||
criticalSectionInitialized = true;
|
||||
}
|
||||
|
@ -84,10 +79,8 @@ void UtilCriticalSectionTerminate(void)
|
|||
{
|
||||
/* Reset the initialized flag. */
|
||||
criticalSectionInitialized = false;
|
||||
/* Reset nesting counter. */
|
||||
criticalSectionNesting = 0;
|
||||
/* Delete the critical section object. */
|
||||
DeleteCriticalSection(&criticalSection);
|
||||
DeleteCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||
}
|
||||
} /*** end of UtilCriticalSectionTerminate ***/
|
||||
|
||||
|
@ -106,13 +99,7 @@ void UtilCriticalSectionEnter(void)
|
|||
/* Only continue if actually initialized. */
|
||||
if (criticalSectionInitialized)
|
||||
{
|
||||
/* Enter the critical section if not already entered. */
|
||||
if (criticalSectionNesting == 0)
|
||||
{
|
||||
EnterCriticalSection(&criticalSection);
|
||||
}
|
||||
/* Increment nesting counter. */
|
||||
criticalSectionNesting++;
|
||||
EnterCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||
}
|
||||
} /*** end of UtilCriticalSectionEnter ***/
|
||||
|
||||
|
@ -130,19 +117,7 @@ void UtilCriticalSectionExit(void)
|
|||
/* Only continue if actually initialized. */
|
||||
if (criticalSectionInitialized)
|
||||
{
|
||||
/* Sanity check. */
|
||||
assert(criticalSectionNesting > 0);
|
||||
|
||||
/* Decrement nesting counter if it is valid. */
|
||||
if (criticalSectionNesting > 0)
|
||||
{
|
||||
criticalSectionNesting--;
|
||||
/* Leave the critical section. */
|
||||
if (criticalSectionNesting == 0)
|
||||
{
|
||||
LeaveCriticalSection(&criticalSection);
|
||||
}
|
||||
}
|
||||
LeaveCriticalSection((CRITICAL_SECTION *)&criticalSection);
|
||||
}
|
||||
} /*** end of UtilCriticalSectionExit ***/
|
||||
|
||||
|
|
Loading…
Reference in New Issue