Skip to content

Commit

Permalink
Add locking
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Sep 28, 2019
1 parent 20c5ef2 commit 8e3fc92
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/simwin/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int main(void)
}

FlsEmu_DeInit();

XcpHw_Deinit();
XcpTl_DeInit();

return 0;
Expand Down
12 changes: 6 additions & 6 deletions examples/simwin/xcp_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@
/*
** Platform Specific Options.
*/
#define XCP_ENTER_CRITICAL()
#define XCP_LEAVE_CRITICAL()
#define XCP_TL_ENTER_CRITICAL()
#define XCP_TL_LEAVE_CRITICAL()
#define XCP_DAQ_ENTER_CRITICAL()
#define XCP_DAQ_LEAVE_CRITICAL()
#define XCP_ENTER_CRITICAL() XcpHw_AcquireLock(XCP_HW_LOCK_XCP)
#define XCP_LEAVE_CRITICAL() XcpHw_ReleaseLock(XCP_HW_LOCK_XCP)
#define XCP_TL_ENTER_CRITICAL() XcpHw_AcquireLock(XCP_HW_LOCK_TL)
#define XCP_TL_LEAVE_CRITICAL() XcpHw_ReleaseLock(XCP_HW_LOCK_TL)
#define XCP_DAQ_ENTER_CRITICAL() XcpHw_AcquireLock(XCP_HW_LOCK_DAQ)
#define XCP_DAQ_LEAVE_CRITICAL() XcpHw_ReleaseLock(XCP_HW_LOCK_DAQ)
#define XCP_STIM_ENTER_CRITICAL()
#define XCP_STIM_LEAVE_CRITICAL()
#define XCP_PGM_ENTER_CRITICAL()
Expand Down
13 changes: 11 additions & 2 deletions inc/xcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,15 @@ extern "C"
*/
#define XCP_ON_CAN_IS_EXTENDED_IDENTIFIER(i) (((i) & XCP_ON_CAN_EXT_IDENTIFIER) == XCP_ON_CAN_EXT_IDENTIFIER)
#define XCP_ON_CAN_STRIP_IDENTIFIER(i) ((i) & (~XCP_ON_CAN_EXT_IDENTIFIER))


/*
**
*/
#define XCP_HW_LOCK_XCP (0)
#define XCP_HW_LOCK_TL (1)
#define XCP_HW_LOCK_DAQ (2)

#define XCP_HW_LOCK_COUNT (3)

/*
** Global Types.
Expand Down Expand Up @@ -863,7 +871,8 @@ Xcp_MemoryMappingResultType Xcp_HookFunction_AddressMapper(Xcp_MtaType * dst, Xc
/*
** Hardware dependent stuff.
*/
void XcpHw_Init(void);
void XcpHw_Init(void);
void XcpHw_Deinit(void);
uint32_t XcpHw_GetTimerCounter(void);

extern Xcp_PDUType Xcp_PduIn;
Expand Down
7 changes: 7 additions & 0 deletions inc/xcp_hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ extern "C"
#endif /* XCP_EXTERN_C_GUARDS */


/*
** Global Defines.
*/


typedef struct tagXcpHw_OptionsType {
bool ipv6;
bool tcp;
Expand All @@ -42,6 +47,8 @@ typedef struct tagXcpHw_OptionsType {
void XcpHw_GetCommandLineOptions(XcpHw_OptionsType * options);

void Win_ErrorMsg(char * const function, unsigned errorCode);
void XcpHw_AcquireLock(uint8_t lockIdx);
void XcpHw_ReleaseLock(uint8_t lockIdx);

#if XCP_ENABLE_EXTERN_C_GUARDS == XCP_ON
#if defined(__cplusplus)
Expand Down
56 changes: 53 additions & 3 deletions src/hw/win/hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ typedef struct tagHwStateType {


/*
** Local Variables.
** Local Function Prototypes.
*/
static HwStateType HwState = {0};

static void XcpHw_InitLocks(void);
static void XcpHw_DeinitLocks();

/*
** External Function Prototypes.
*/
void XcpTl_PrintConnectionInformation(void);

static void DisplayHelp(void);
Expand All @@ -81,6 +84,13 @@ bool XcpHw_MainFunction(HANDLE * quit_event);
#if (defined(_DEBUG)) || (XCP_BUILD_TYPE == XCP_DEBUG_BUILD)
void exitFunc(void);

/*
** Local Variables.
*/
static HwStateType HwState = {0};
static CRITICAL_SECTION XcpHw_Locks[XCP_HW_LOCK_COUNT];


void exitFunc(void)
{
printf("Exiting %s...\n", __argv[0]);
Expand All @@ -101,6 +111,12 @@ void XcpHw_Init(void)
//_setmode(_fileno(stdout), _O_WTEXT); /* Permit Unicode output on console */
//_setmode(_fileno(stdout), _O_U8TEXT); /* Permit Unicode output on console */
QueryPerformanceFrequency(&HwState.TicksPerSecond);
XcpHw_InitLocks();
}

void XcpHw_Deinit(void)
{
XcpHw_DeinitLocks();
}

uint32_t XcpHw_GetTimerCounter(void)
Expand Down Expand Up @@ -140,6 +156,39 @@ uint32_t XcpHw_GetTimerCounter(void)
#endif // XCP_DAQ_TIMESTAMP_SIZE
}

static void XcpHw_InitLocks(void)
{
uint8_t idx = UINT8(0);

for (idx = UINT8(0); idx < XCP_HW_LOCK_COUNT; ++idx) {
InitializeCriticalSection(&XcpHw_Locks[idx]);
}
}

static void XcpHw_DeinitLocks(void)
{
uint8_t idx = UINT8(0);

for (idx = UINT8(0); idx < XCP_HW_LOCK_COUNT; ++idx) {
DeleteCriticalSection(&XcpHw_Locks[idx]);
}
}

void XcpHw_AcquireLock(uint8_t lockIdx)
{
if (lockIdx >= XCP_HW_LOCK_COUNT) {
return;
}
EnterCriticalSection(&XcpHw_Locks[lockIdx]);
}

void XcpHw_ReleaseLock(uint8_t lockIdx)
{
if (lockIdx >= XCP_HW_LOCK_COUNT) {
return;
}
LeaveCriticalSection(&XcpHw_Locks[lockIdx]);
}

void Win_ErrorMsg(char * const fun, unsigned errorCode)
{
Expand Down Expand Up @@ -171,6 +220,7 @@ DWORD XcpHw_UIThread(LPVOID param)
ExitThread(0);
}


void XcpTl_PostQuitMessage();

bool XcpHw_MainFunction(HANDLE * quit_event)
Expand Down
3 changes: 1 addition & 2 deletions src/xcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ void Xcp_Init(void)
{
Xcp_ConnectionState = XCP_DISCONNECTED;

XcpHw_Init();
XcpUtl_MemSet(&Xcp_State, UINT8(0), (uint32_t)sizeof(Xcp_StateType));
Xcp_State.busy = (bool)XCP_FALSE;

Expand Down Expand Up @@ -629,8 +630,6 @@ void Xcp_Init(void)
Xcp_State.statistics.crosSend = UINT32(0);
Xcp_State.statistics.ctosReceived = UINT32(0);
#endif /* XCP_ENABLE_STATISTICS */

XcpHw_Init();
XcpTl_Init();

#if (XCP_ENABLE_BUILD_CHECKSUM == XCP_ON) && (XCP_CHECKSUM_CHUNKED_CALCULATION == XCP_ON)
Expand Down

0 comments on commit 8e3fc92

Please sign in to comment.