2015-04-28 13:45:35 -04:00
|
|
|
/**
|
|
|
|
* @file
|
2018-10-25 11:11:36 -04:00
|
|
|
* @brief QF/C port to Win32 API (single-threaded, like the QV kernel)
|
2018-03-07 16:48:42 -05:00
|
|
|
* @ingroup ports
|
2015-04-28 13:45:35 -04:00
|
|
|
* @cond
|
|
|
|
******************************************************************************
|
2019-02-10 21:00:39 -05:00
|
|
|
* Last updated for version 6.4.0
|
|
|
|
* Last updated on 2019-02-07
|
2015-04-28 13:45:35 -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
|
2015-04-28 13:45:35 -04:00
|
|
|
*
|
2019-02-10 21:00:39 -05:00
|
|
|
* Copyright (C) 2005-2019 Quantum Leaps, LLC. All rights reserved.
|
2015-04-28 13:45:35 -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
|
|
|
|
* (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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Contact information:
|
2018-03-07 16:48:42 -05:00
|
|
|
* https://www.state-machine.com
|
2016-02-10 16:50:22 -05:00
|
|
|
* mailto:info@state-machine.com
|
2015-04-28 13:45:35 -04:00
|
|
|
******************************************************************************
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
#define QP_IMPL /* this is QP implementation */
|
|
|
|
#include "qf_port.h" /* QF port */
|
|
|
|
#include "qf_pkg.h" /* QF package-scope interface */
|
|
|
|
#include "qassert.h" /* QP embedded systems-friendly assertions */
|
|
|
|
#ifdef Q_SPY /* QS software tracing enabled? */
|
|
|
|
#include "qs_port.h" /* include QS port */
|
|
|
|
#else
|
|
|
|
#include "qs_dummy.h" /* disable the QS software tracing */
|
|
|
|
#endif /* Q_SPY */
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
Q_DEFINE_THIS_MODULE("qf_port")
|
|
|
|
|
|
|
|
/* Global objects ==========================================================*/
|
2018-11-22 11:35:21 -05:00
|
|
|
QPSet QV_readySet_; /* QV-ready set of active objects */
|
|
|
|
HANDLE QV_win32Event_; /* Win32 event to signal events */
|
2015-04-28 13:45:35 -04:00
|
|
|
|
|
|
|
/* Local objects ===========================================================*/
|
|
|
|
static CRITICAL_SECTION l_win32CritSect;
|
|
|
|
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 */
|
|
|
|
|
|
|
|
static DWORD WINAPI ticker_thread(LPVOID arg);
|
|
|
|
|
|
|
|
/* QF functions ============================================================*/
|
|
|
|
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];
|
|
|
|
|
2015-04-28 13:45:35 -04:00
|
|
|
InitializeCriticalSection(&l_win32CritSect);
|
|
|
|
QV_win32Event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
|
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).
|
|
|
|
*/
|
|
|
|
QF_maxPool_ = (uint_fast8_t)0;
|
|
|
|
QF_bzero(&QF_timeEvtHead_[0], (uint_fast16_t)sizeof(QF_timeEvtHead_));
|
|
|
|
QF_bzero(&QF_active_[0], (uint_fast16_t)sizeof(QF_active_));
|
2015-04-28 13:45:35 -04:00
|
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
void QF_enterCriticalSection_(void) {
|
|
|
|
EnterCriticalSection(&l_win32CritSect);
|
|
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
void QF_leaveCriticalSection_(void) {
|
|
|
|
LeaveCriticalSection(&l_win32CritSect);
|
|
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
void QF_stop(void) {
|
|
|
|
l_isRunning = false; /* terminate the QV event loop and ticker thread */
|
2015-06-04 22:47:13 -04:00
|
|
|
SetEvent(QV_win32Event_); /* unblock the event-loop so it can terminate */
|
2015-04-28 13:45:35 -04:00
|
|
|
}
|
|
|
|
/****************************************************************************/
|
|
|
|
int_t QF_run(void) {
|
2018-11-22 11:35:21 -05:00
|
|
|
QF_CRIT_STAT_
|
|
|
|
|
2015-04-28 13:45:35 -04:00
|
|
|
QF_onStartup(); /* application-specific startup callback */
|
|
|
|
|
|
|
|
l_isRunning = true; /* QF is running */
|
|
|
|
|
2018-03-07 16:48:42 -05:00
|
|
|
if (l_tickMsec != (uint32_t)0) { /* system clock tick configured? */
|
|
|
|
/* create the ticker thread... */
|
2018-10-25 11:11:36 -04:00
|
|
|
HANDLE ticker = CreateThread(NULL, 1024, &ticker_thread,
|
|
|
|
(void *)0, 0, NULL);
|
2018-03-07 16:48:42 -05:00
|
|
|
Q_ASSERT_ID(310, ticker != (HANDLE)0); /* thread must be created */
|
|
|
|
}
|
2015-04-28 13:45:35 -04:00
|
|
|
|
|
|
|
/* the combined event-loop and background-loop of the QV kernel */
|
2018-11-22 11:35:21 -05:00
|
|
|
QF_CRIT_ENTRY_();
|
2015-04-28 13:45:35 -04:00
|
|
|
while (l_isRunning) {
|
|
|
|
/* find the maximum priority AO ready to run */
|
2016-09-23 12:08:21 -04:00
|
|
|
if (QPSet_notEmpty(&QV_readySet_)) {
|
2018-10-25 11:11:36 -04:00
|
|
|
uint_fast8_t p;
|
|
|
|
QActive *a;
|
|
|
|
QEvt const *e;
|
2015-04-28 13:45:35 -04:00
|
|
|
|
2016-09-23 12:08:21 -04:00
|
|
|
QPSet_findMax(&QV_readySet_, p);
|
2015-04-28 13:45:35 -04:00
|
|
|
a = QF_active_[p];
|
2018-11-22 11:35:21 -05:00
|
|
|
QF_CRIT_EXIT_();
|
2015-04-28 13:45:35 -04:00
|
|
|
|
2017-02-08 20:25:14 -05:00
|
|
|
/* the active object 'a' must still be registered in QF
|
|
|
|
* (e.g., it must not be stopped)
|
|
|
|
*/
|
|
|
|
Q_ASSERT_ID(320, a != (QActive *)0);
|
|
|
|
|
2015-04-28 13:45:35 -04:00
|
|
|
/* perform the run-to-completion (RTS) step...
|
|
|
|
* 1. retrieve the event from the AO's event queue, which by this
|
|
|
|
* time must be non-empty and The "Vanialla" kernel asserts it.
|
|
|
|
* 2. dispatch the event to the AO's state machine.
|
|
|
|
* 3. determine if event is garbage and collect it if so
|
|
|
|
*/
|
|
|
|
e = QActive_get_(a);
|
2016-11-30 18:14:20 -05:00
|
|
|
QHSM_DISPATCH(&a->super, e);
|
2015-04-28 13:45:35 -04:00
|
|
|
QF_gc(e);
|
2016-11-30 18:14:20 -05:00
|
|
|
|
2018-11-22 11:35:21 -05:00
|
|
|
QF_CRIT_ENTRY_();
|
2016-11-30 18:14:20 -05:00
|
|
|
|
|
|
|
if (a->eQueue.frontEvt == (QEvt const *)0) { /* empty queue? */
|
|
|
|
QPSet_remove(&QV_readySet_, p);
|
|
|
|
}
|
2015-04-28 13:45:35 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* the QV kernel in embedded systems calls here the QV_onIdle()
|
|
|
|
* callback. However, the Win32-QV port does not do busy-waiting
|
|
|
|
* for events. Instead, the Win32-QV port efficiently waits until
|
|
|
|
* QP events become available.
|
|
|
|
*/
|
2018-11-22 11:35:21 -05:00
|
|
|
QF_CRIT_EXIT_();
|
2016-11-30 18:14:20 -05:00
|
|
|
|
2015-04-28 13:45:35 -04:00
|
|
|
(void)WaitForSingleObject(QV_win32Event_, (DWORD)INFINITE);
|
2016-11-30 18:14:20 -05:00
|
|
|
|
2018-11-22 11:35:21 -05:00
|
|
|
QF_CRIT_ENTRY_();
|
2015-04-28 13:45:35 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-22 11:35:21 -05:00
|
|
|
QF_CRIT_EXIT_();
|
2015-04-28 13:45:35 -04:00
|
|
|
QF_onCleanup(); /* cleanup callback */
|
|
|
|
QS_EXIT(); /* cleanup the QSPY connection */
|
|
|
|
|
|
|
|
//CloseHandle(QV_win32Event_);
|
|
|
|
//DeleteCriticalSection(&l_win32CritSect);
|
|
|
|
//free all "fudged" event pools...
|
|
|
|
return (int_t)0; /* return success */
|
|
|
|
}
|
|
|
|
/****************************************************************************/
|
2018-10-25 11:11:36 -04:00
|
|
|
void QF_setTickRate(uint32_t ticksPerSec, int_t tickPrio) {
|
2018-03-07 16:48:42 -05:00
|
|
|
if (ticksPerSec != (uint32_t)0) {
|
|
|
|
l_tickMsec = 1000UL / ticksPerSec;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
l_tickMsec = (uint32_t)0; /* means NO system clock tick */
|
|
|
|
}
|
2018-10-25 11:11:36 -04:00
|
|
|
l_tickPrio = tickPrio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*..........................................................................*/
|
|
|
|
void QF_consoleSetup(void) {
|
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
void QF_consoleCleanup(void) {
|
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
int QF_consoleGetKey(void) {
|
|
|
|
if (_kbhit()) { /* any key pressed? */
|
|
|
|
return _getch();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
int QF_consoleWaitForKey(void) {
|
|
|
|
return _getch();
|
2015-04-28 13:45:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* QActive functions =======================================================*/
|
|
|
|
void QActive_start_(QActive * const me, uint_fast8_t prio,
|
|
|
|
QEvt const *qSto[], uint_fast16_t qLen,
|
|
|
|
void *stkSto, uint_fast16_t stkSize,
|
|
|
|
QEvt const *ie)
|
|
|
|
{
|
2018-04-05 17:05:55 -04:00
|
|
|
Q_REQUIRE_ID(600, ((uint_fast8_t)0 < prio) /* priority must be in range */
|
2018-10-25 11:11:36 -04:00
|
|
|
&& (prio <= (uint_fast8_t)QF_MAX_ACTIVE)
|
|
|
|
&& (stkSto == (void *)0)); /* statck storage must NOT...
|
|
|
|
* ... be provided */
|
2015-04-28 13:45:35 -04:00
|
|
|
|
2018-04-05 17:05:55 -04:00
|
|
|
QEQueue_init(&me->eQueue, qSto, qLen);
|
2015-04-28 13:45:35 -04:00
|
|
|
me->prio = prio; /* set QF priority of this AO before adding it to QF */
|
2018-10-25 11:11:36 -04:00
|
|
|
QF_add_(me); /* make QF aware of this AO */
|
2015-04-28 13:45:35 -04:00
|
|
|
|
2016-11-30 18:14:20 -05:00
|
|
|
QHSM_INIT(&me->super, ie); /* take the top-most initial tran. */
|
2018-10-25 11:11:36 -04:00
|
|
|
QS_FLUSH(); /* flush the QS trace buffer to the host */
|
2015-04-28 13:45:35 -04:00
|
|
|
|
2018-10-25 11:11:36 -04:00
|
|
|
(void)stkSize; /* unused parameter */
|
2015-04-28 13:45:35 -04:00
|
|
|
}
|
2018-10-25 11:11:36 -04:00
|
|
|
|
2015-04-28 13:45:35 -04:00
|
|
|
/****************************************************************************/
|
|
|
|
static DWORD WINAPI ticker_thread(LPVOID arg) { /* for CreateThread() */
|
2018-10-25 11:11:36 -04:00
|
|
|
int threadPrio = THREAD_PRIORITY_NORMAL;
|
2015-04-28 13:45:35 -04:00
|
|
|
|
2018-10-25 11:11:36 -04:00
|
|
|
// set the ticker thread priority according to selection made in
|
|
|
|
// QF_setTickRate()
|
|
|
|
//
|
|
|
|
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) {
|
|
|
|
Sleep(l_tickMsec); /* wait for the tick interval */
|
|
|
|
QF_onClockTick(); /* clock tick callback (must call QF_TICK_X()) */
|
|
|
|
}
|
2018-10-25 11:11:36 -04:00
|
|
|
|
|
|
|
(void)arg; /* unused parameter */
|
|
|
|
|
2015-04-28 13:45:35 -04:00
|
|
|
return (DWORD)0; /* return success */
|
|
|
|
}
|
2018-11-22 11:35:21 -05:00
|
|
|
|