qpc/ports/win32/qf_port.c

219 lines
8.1 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @file
* @brief QF/C port to Win32 API
2018-03-07 16:48:42 -05:00
* @ingroup ports
2015-04-28 13:45:35 -04:00
* @cond
2014-04-06 11:43:13 -04:00
******************************************************************************
2020-03-17 21:33:20 -04:00
* Last updated for version 6.8.0
* Last updated on 2020-01-23
2012-08-14 18:07:04 -04:00
*
2018-10-25 11:11:36 -04:00
* Q u a n t u m L e a P s
* ------------------------
* Modern Embedded Software
2012-08-14 18:07:04 -04:00
*
2020-03-17 21:33:20 -04:00
* Copyright (C) 2005-2020 Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:07:04 -04:00
*
* This program is open source 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
2012-08-14 18:07:04 -04:00
* (at your option) any later version.
*
* Alternatively, this program may be distributed and modified under the
* terms of Quantum Leaps commercial licenses, which expressly supersede
* the GNU General Public License and are specifically designed for
* licensees interested in retaining the proprietary status of their code.
*
* This program 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 should have received a copy of the GNU General Public License
2019-12-31 15:55:08 -05:00
* along with this program. If not, see <www.gnu.org/licenses/>.
2012-08-14 18:07:04 -04:00
*
* Contact information:
2019-12-31 15:55:08 -05:00
* <www.state-machine.com/licensing>
* <info@state-machine.com>
2014-04-06 11:43:13 -04:00
******************************************************************************
2015-04-28 13:45:35 -04:00
* @endcond
2014-04-06 11:43:13 -04:00
*/
#define QP_IMPL /* this is QP implementation */
#include "qf_port.h" /* QF port */
2015-04-28 13:45:35 -04:00
#include "qf_pkg.h" /* QF package-scope interface */
#include "qassert.h" /* QP embedded systems-friendly assertions */
2014-04-06 11:43:13 -04:00
#ifdef Q_SPY /* QS software tracing enabled? */
2020-03-17 21:33:20 -04:00
#include "qs_port.h" /* QS port */
#include "qs_pkg.h" /* QS package-scope internal interface */
2014-04-06 11:43:13 -04:00
#else
#include "qs_dummy.h" /* disable the QS software tracing */
#endif /* Q_SPY */
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
#include <limits.h> /* limits of dynamic range for integers */
2018-10-25 11:11:36 -04:00
#include <conio.h> /* console input/output */
2015-04-28 13:45:35 -04:00
2012-08-14 18:07:04 -04:00
Q_DEFINE_THIS_MODULE("qf_port")
2015-04-28 13:45:35 -04:00
/* Local objects ===========================================================*/
2012-08-14 18:07:04 -04:00
static CRITICAL_SECTION l_win32CritSect;
2017-08-29 15:59:44 -04:00
static CRITICAL_SECTION l_startupCritSect;
2015-04-28 13:45:35 -04:00
static DWORD l_tickMsec = 10U; /* clock tick in msec (argument for Sleep()) */
2018-10-25 11:11:36 -04:00
static int_t l_tickPrio = 50; /* default priority of the "ticker" thread */
2015-04-28 13:45:35 -04:00
static bool l_isRunning; /* flag indicating when QF is running */
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
static DWORD WINAPI ao_thread(LPVOID arg);
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/* QF functions ============================================================*/
2012-08-14 18:07:04 -04:00
void QF_init(void) {
2016-02-10 16:50:22 -05:00
extern uint_fast8_t QF_maxPool_;
extern QTimeEvt QF_timeEvtHead_[QF_MAX_TICK_RATE];
2012-08-14 18:07:04 -04:00
InitializeCriticalSection(&l_win32CritSect);
2016-02-10 16:50:22 -05:00
2017-08-29 15:59:44 -04:00
/* initialize and enter the startup critical section object to block
* any active objects started before calling QF_run()
*/
InitializeCriticalSection(&l_startupCritSect);
EnterCriticalSection(&l_startupCritSect);
2016-02-10 16:50:22 -05:00
/* clear the internal QF variables, so that the framework can (re)start
* correctly even if the startup code is not called to clear the
* uninitialized data (as is required by the C Standard).
*/
2020-03-17 21:33:20 -04:00
QF_maxPool_ = 0U;
QF_bzero(&QF_timeEvtHead_[0], sizeof(QF_timeEvtHead_));
QF_bzero(&QF_active_[0], sizeof(QF_active_));
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_enterCriticalSection_(void) {
EnterCriticalSection(&l_win32CritSect);
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_leaveCriticalSection_(void) {
LeaveCriticalSection(&l_win32CritSect);
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_stop(void) {
2015-04-28 13:45:35 -04:00
l_isRunning = false; /* terminate the main (ticker) thread */
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2013-12-30 17:37:40 -05:00
int_t QF_run(void) {
2018-10-25 11:11:36 -04:00
int threadPrio = THREAD_PRIORITY_NORMAL;
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
QF_onStartup(); /* application-specific startup callback */
2017-08-29 15:59:44 -04:00
/* leave the startup critical section to unblock any active objects
* started before calling QF_run()
*/
LeaveCriticalSection(&l_startupCritSect);
2015-04-28 13:45:35 -04:00
l_isRunning = true; /* QF is running */
2018-10-25 11:11:36 -04:00
/* set the ticker (this thread) priority according to selection made in
* QF_setTickRate()
2014-04-06 11:43:13 -04:00
*/
2018-10-25 11:11:36 -04:00
if (l_tickPrio < 33) {
threadPrio = THREAD_PRIORITY_BELOW_NORMAL;
}
else if (l_tickPrio > 66) {
threadPrio = THREAD_PRIORITY_ABOVE_NORMAL;
}
SetThreadPriority(GetCurrentThread(), threadPrio);
2015-04-28 13:45:35 -04:00
while (l_isRunning) {
2014-04-06 11:43:13 -04:00
Sleep(l_tickMsec); /* wait for the tick interval */
QF_onClockTick(); /* clock tick callback (must call QF_TICKX()) */
2012-08-14 18:07:04 -04:00
}
2018-10-25 11:11:36 -04:00
2014-04-06 11:43:13 -04:00
QF_onCleanup(); /* cleanup callback */
QS_EXIT(); /* cleanup the QSPY connection */
2017-08-29 15:59:44 -04:00
//DeleteCriticalSection(&l_startupCritSect);
2012-08-14 18:07:04 -04:00
//DeleteCriticalSection(&l_win32CritSect);
2020-03-17 21:33:20 -04:00
return 0; /* return success */
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2018-10-25 11:11:36 -04:00
void QF_setTickRate(uint32_t ticksPerSec, int_t tickPrio) {
2020-03-17 21:33:20 -04:00
Q_REQUIRE_ID(600, ticksPerSec != 0U);
2018-10-25 11:11:36 -04:00
l_tickMsec = 1000UL / ticksPerSec;
l_tickPrio = tickPrio;
}
/****************************************************************************/
2015-04-28 13:45:35 -04:00
void QF_setWin32Prio(QActive *act, int_t win32Prio) {
2019-02-12 12:48:51 -05:00
HANDLE win32thread = (HANDLE)act->thread;
/* thread must be already created, see QActive_start_() */
Q_REQUIRE_ID(700, win32thread != (HANDLE)0);
SetThreadPriority(win32thread, win32Prio);
2015-04-28 13:45:35 -04:00
}
2014-04-06 11:43:13 -04:00
2015-04-28 13:45:35 -04:00
/* QActive functions =======================================================*/
2014-04-06 11:43:13 -04:00
void QActive_start_(QActive * const me, uint_fast8_t prio,
2020-03-17 21:33:20 -04:00
QEvt const * * const qSto, uint_fast16_t const qLen,
void * const stkSto, uint_fast16_t const stkSize,
2019-12-31 15:55:08 -05:00
void const * const par)
2012-08-14 18:07:04 -04:00
{
2020-03-17 21:33:20 -04:00
Q_REQUIRE_ID(800, (0U < prio) /* priority must be in range */
&& (prio <= QF_MAX_ACTIVE)
&& (stkSto == (void *)0)); /* statck storage must NOT...
2015-04-28 13:45:35 -04:00
* ... be provided */
me->prio = prio; /* set QF priority of this AO before adding it to QF */
QF_add_(me); /* make QF aware of this active object */
2016-11-08 12:47:28 -05:00
QEQueue_init(&me->eQueue, qSto, qLen);
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/* create the Win32 "event" to throttle the AO's event queue */
2012-08-14 18:07:04 -04:00
me->osObject = CreateEvent(NULL, FALSE, FALSE, NULL);
2015-04-28 13:45:35 -04:00
2019-10-27 11:57:33 -04:00
QHSM_INIT(&me->super, par); /* the top-most initial tran. (virtual) */
QS_FLUSH(); /* flush the trace buffer to the host */
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/* create a Win32 thread for the AO;
* The thread is created with THREAD_PRIORITY_NORMAL
*/
2020-03-17 21:33:20 -04:00
me->thread = CreateThread(
NULL,
(stkSize < 1024U ? 1024U : stkSize),
&ao_thread,
me,
0,
NULL);
2019-02-12 12:48:51 -05:00
Q_ENSURE_ID(830, me->thread != (HANDLE)0); /* must succeed */
2012-08-14 18:07:04 -04:00
}
2018-10-25 11:11:36 -04:00
/****************************************************************************/
void QF_consoleSetup(void) {
}
/*..........................................................................*/
void QF_consoleCleanup(void) {
}
/*..........................................................................*/
int QF_consoleGetKey(void) {
if (_kbhit()) { /* any key pressed? */
2020-03-17 21:33:20 -04:00
return (int)_getwch();
2018-10-25 11:11:36 -04:00
}
return 0;
}
/*..........................................................................*/
int QF_consoleWaitForKey(void) {
2020-03-17 21:33:20 -04:00
return (int)_getwch();
2018-10-25 11:11:36 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2015-04-28 13:45:35 -04:00
static DWORD WINAPI ao_thread(LPVOID arg) { /* for CreateThread() */
QActive *act = (QActive *)arg;
2017-08-29 15:59:44 -04:00
/* block this thread until the startup critical section is exited
* from QF_run() */
EnterCriticalSection(&l_startupCritSect);
LeaveCriticalSection(&l_startupCritSect);
2019-02-10 21:00:39 -05:00
/* event-loop */
for (;;) { /* for-ever */
2015-04-28 13:45:35 -04:00
QEvt const *e = QActive_get_(act); /* wait for event */
2016-11-30 18:14:20 -05:00
QHSM_DISPATCH(&act->super, e); /* dispatch to the AO's SM */
2014-04-06 11:43:13 -04:00
QF_gc(e); /* check if the event is garbage, and collect it if so */
2019-02-10 21:00:39 -05:00
}
2015-04-28 13:45:35 -04:00
return (DWORD)0; /* return success */
2012-08-14 18:07:04 -04:00
}
2018-10-25 11:11:36 -04:00