250 lines
8.4 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
2015-09-29 11:34:38 -04:00
// Product: DPP example (console)
2015-12-31 14:56:37 -05:00
// Last Updated for Version: 5.6.0
// Date of the Last Update: 2015-12-26
2012-08-14 18:00:48 -04:00
//
// Q u a n t u m L e a P s
// ---------------------------
// innovating embedded systems
//
2015-09-29 11:34:38 -04:00
// Copyright (C) Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:00:48 -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:00:48 -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
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Contact information:
2015-09-29 11:34:38 -04:00
// http://www.state-machine.com
// mailto:info@state-machine.com
2013-10-10 20:01:51 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
#include "qpcpp.h"
2012-08-14 18:00:48 -04:00
#include "dpp.h"
#include "bsp.h"
#include <conio.h>
#include <stdio.h>
2015-09-29 11:34:38 -04:00
#ifdef Q_SPY
#include <time.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // Win32 API
#include "qspy.h" // QSPY interface
#endif
2013-10-10 20:01:51 -04:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
namespace DPP {
Q_DEFINE_THIS_FILE
// local variables -----------------------------------------------------------
2015-05-14 16:05:04 -04:00
static uint32_t l_rnd; // random seed
2012-08-14 18:00:48 -04:00
#ifdef Q_SPY
enum {
PHILO_STAT = QP::QS_USER
};
2015-09-29 11:34:38 -04:00
static bool l_isRunning;
2012-08-14 18:00:48 -04:00
static uint8_t const l_clock_tick = 0U;
#endif
//............................................................................
void BSP_init(void) {
printf("Dining Philosopher Problem example"
2015-06-06 18:30:55 -04:00
"\nQP %s\n"
2015-05-14 16:05:04 -04:00
"Press 'p' to pause\n"
"Press 's' to serve\n"
2012-08-14 18:00:48 -04:00
"Press ESC to quit...\n",
2015-06-06 18:30:55 -04:00
QP::versionStr);
2012-08-14 18:00:48 -04:00
BSP_randomSeed(1234U);
Q_ALLEGE(QS_INIT((void *)0));
2015-05-14 16:05:04 -04:00
QS_OBJ_DICTIONARY(&l_clock_tick); // must be called *after* QF::init()
2012-08-14 18:00:48 -04:00
QS_USR_DICTIONARY(PHILO_STAT);
}
//............................................................................
void BSP_terminate(int16_t result) {
(void)result;
2015-09-29 11:34:38 -04:00
#ifdef Q_SPY
l_isRunning = false; // stop the QS output thread
#endif
2015-05-14 16:05:04 -04:00
QP::QF::stop(); // stop the main "ticker thread"
2012-08-14 18:00:48 -04:00
}
//............................................................................
void BSP_displayPhilStat(uint8_t n, char const *stat) {
printf("Philosopher %2d is %s\n", (int)n, stat);
2015-05-14 16:05:04 -04:00
QS_BEGIN(PHILO_STAT, AO_Philo[n]) // application-specific record begin
QS_U8(1, n); // Philosopher number
QS_STR(stat); // Philosopher status
2012-08-14 18:00:48 -04:00
QS_END()
}
//............................................................................
void BSP_displayPaused(uint8_t paused) {
printf("Paused is %s\n", paused ? "ON" : "OFF");
}
//............................................................................
2015-05-14 16:05:04 -04:00
uint32_t BSP_random(void) { // a very cheap pseudo-random-number generator
2012-08-14 18:00:48 -04:00
// "Super-Duper" Linear Congruential Generator (LCG)
// LCG(2^32, 3*7*11*13*23, 0, seed)
//
2015-05-14 16:05:04 -04:00
l_rnd = l_rnd * (3U*7U*11U*13U*23U);
2012-08-14 18:00:48 -04:00
return l_rnd >> 8;
}
//............................................................................
void BSP_randomSeed(uint32_t seed) {
l_rnd = seed;
}
} // namespace DPP
2015-09-29 11:34:38 -04:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
namespace QP {
//............................................................................
void QF::onStartup(void) {
2015-05-14 16:05:04 -04:00
QF_setTickRate(DPP::BSP_TICKS_PER_SEC); // set the desired tick rate
2012-08-14 18:00:48 -04:00
}
//............................................................................
void QF::onCleanup(void) {
}
//............................................................................
void QF_onClockTick(void) {
2015-05-14 16:05:04 -04:00
QF::TICK_X(0U, &DPP::l_clock_tick); // process time events at rate 0
2013-12-30 17:41:15 -05:00
2015-05-14 16:05:04 -04:00
if (_kbhit()) { // any key pressed?
2012-08-14 18:00:48 -04:00
int ch = _getch();
2015-05-14 16:05:04 -04:00
if (ch == '\33') { // see if the ESC key pressed
2012-08-14 18:00:48 -04:00
QF::PUBLISH(Q_NEW(QEvt, DPP::TERMINATE_SIG), &DPP::l_clock_tick);
}
else if (ch == 'p') {
QF::PUBLISH(Q_NEW(QEvt, DPP::PAUSE_SIG), &DPP::l_clock_tick);
}
2015-05-14 16:05:04 -04:00
else if (ch == 's') {
QF::PUBLISH(Q_NEW(QEvt, DPP::SERVE_SIG), &DPP::l_clock_tick);
}
2012-08-14 18:00:48 -04:00
}
}
//............................................................................
2015-12-31 14:56:37 -05:00
extern "C" void Q_onAssert(char const * const module, int loc) {
2015-09-29 11:34:38 -04:00
QS_ASSERTION(module, loc, 10000U); // report assertion to QS
fprintf(stderr, "Assertion failed in %s, location %d", module, loc);
QP::QF::stop();
2012-08-14 18:00:48 -04:00
}
//----------------------------------------------------------------------------
2015-05-14 16:05:04 -04:00
#ifdef Q_SPY // define QS callbacks
2012-08-14 18:00:48 -04:00
//............................................................................
2015-05-14 16:05:04 -04:00
static DWORD WINAPI idleThread(LPVOID par) { // signature for CreateThread()
2012-08-14 18:00:48 -04:00
(void)par;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
2015-09-29 11:34:38 -04:00
DPP::l_isRunning = true;
while (DPP::l_isRunning) {
2012-08-14 18:00:48 -04:00
uint16_t nBytes = 256;
uint8_t const *block;
QF_CRIT_ENTRY(dummy);
block = QS::getBlock(&nBytes);
QF_CRIT_EXIT(dummy);
if (block != (uint8_t *)0) {
QSPY_parse(block, nBytes);
}
2015-05-14 16:05:04 -04:00
Sleep(50); // wait for a while
2012-08-14 18:00:48 -04:00
}
2015-05-14 16:05:04 -04:00
return 0; // return success
2012-08-14 18:00:48 -04:00
}
//............................................................................
bool QS::onStartup(void const *arg) {
2015-05-14 16:05:04 -04:00
static uint8_t qsBuf[4*1024]; // 4K buffer for Quantum Spy
2012-08-14 18:00:48 -04:00
initBuf(qsBuf, sizeof(qsBuf));
(void)arg;
2013-10-10 20:01:51 -04:00
QSPY_config(QP_VERSION, // version
2012-08-14 18:00:48 -04:00
QS_OBJ_PTR_SIZE, // objPtrSize
QS_FUN_PTR_SIZE, // funPtrSize
QS_TIME_SIZE, // tstampSize
Q_SIGNAL_SIZE, // sigSize,
QF_EVENT_SIZ_SIZE, // evtSize
QF_EQUEUE_CTR_SIZE, // queueCtrSize
QF_MPOOL_CTR_SIZE, // poolCtrSize
QF_MPOOL_SIZ_SIZE, // poolBlkSize
QF_TIMEEVT_CTR_SIZE,// tevtCtrSize
(void *)0, // matFile,
(void *)0,
(QSPY_CustParseFun)0); // customized parser function
2015-05-14 16:05:04 -04:00
// set up the QS filters...
QS_FILTER_ON(QS_QEP_STATE_ENTRY);
QS_FILTER_ON(QS_QEP_STATE_EXIT);
QS_FILTER_ON(QS_QEP_STATE_INIT);
QS_FILTER_ON(QS_QEP_INIT_TRAN);
QS_FILTER_ON(QS_QEP_INTERN_TRAN);
QS_FILTER_ON(QS_QEP_TRAN);
QS_FILTER_ON(QS_QEP_IGNORED);
QS_FILTER_ON(QS_QEP_DISPATCH);
QS_FILTER_ON(QS_QEP_UNHANDLED);
2015-09-29 11:34:38 -04:00
QS_FILTER_ON(QS_QF_ACTIVE_POST_FIFO);
QS_FILTER_ON(QS_QF_ACTIVE_POST_LIFO);
QS_FILTER_ON(QS_QF_PUBLISH);
2012-08-14 18:00:48 -04:00
2015-09-29 11:34:38 -04:00
QS_FILTER_ON(DPP::PHILO_STAT);
2012-08-14 18:00:48 -04:00
return CreateThread(NULL, 1024, &idleThread, (void *)0, 0, NULL)
2015-05-14 16:05:04 -04:00
!= (HANDLE)0; // return the status of creating the idle thread
2012-08-14 18:00:48 -04:00
}
//............................................................................
void QS::onCleanup(void) {
2015-09-29 11:34:38 -04:00
DPP::l_isRunning = false;
2012-08-14 18:00:48 -04:00
QSPY_stop();
}
//............................................................................
void QS::onFlush(void) {
2015-05-14 16:05:04 -04:00
for (;;) {
uint16_t nBytes = 1024U;
uint8_t const *block;
QF_CRIT_ENTRY(dummy);
block = getBlock(&nBytes);
QF_CRIT_EXIT(dummy);
if (block != static_cast<uint8_t const *>(0)) {
QSPY_parse(block, nBytes);
nBytes = 1024U;
}
else {
break;
}
2012-08-14 18:00:48 -04:00
}
}
//............................................................................
QSTimeCtr QS::onGetTime(void) {
return (QSTimeCtr)clock();
}
//............................................................................
2015-09-29 11:34:38 -04:00
extern "C" void QSPY_onPrintLn(void) {
2012-08-14 18:00:48 -04:00
fputs(QSPY_line, stdout);
fputc('\n', stdout);
}
2015-05-14 16:05:04 -04:00
#endif // Q_SPY
2012-08-14 18:00:48 -04:00
//----------------------------------------------------------------------------
} // namespace QP