2008-03-27 11:48:28 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free 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.
|
|
|
|
|
|
|
|
ChibiOS/RT 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ch.hpp>
|
2009-06-07 17:53:23 +00:00
|
|
|
#include <pal.h>
|
2008-03-27 12:33:31 +00:00
|
|
|
|
2008-03-27 11:48:28 +00:00
|
|
|
#include <evtimer.h>
|
2008-03-27 12:33:31 +00:00
|
|
|
#include <test.h>
|
2008-03-27 11:48:28 +00:00
|
|
|
|
2009-06-02 13:41:38 +00:00
|
|
|
#include <board.h>
|
2008-03-27 11:48:28 +00:00
|
|
|
#include <lpc214x_serial.h>
|
|
|
|
|
2009-06-07 17:53:23 +00:00
|
|
|
#define BOTH_BUTTONS (PAL_PORT_BIT(PA_BUTTON1) | PAL_PORT_BIT(PA_BUTTON2))
|
|
|
|
|
2008-03-27 11:48:28 +00:00
|
|
|
using namespace chibios_rt;
|
|
|
|
|
|
|
|
/*
|
2008-03-27 14:42:48 +00:00
|
|
|
* LED blink sequences.
|
|
|
|
* NOTE: Sequences must always be terminated by a GOTO instruction.
|
2008-03-28 16:13:17 +00:00
|
|
|
* NOTE: The sequencer language could be easily improved but this is outside
|
|
|
|
* the scope of this demo.
|
2008-03-27 11:48:28 +00:00
|
|
|
*/
|
|
|
|
#define SLEEP 0
|
2008-03-27 12:33:31 +00:00
|
|
|
#define GOTO 1
|
|
|
|
#define STOP 2
|
|
|
|
#define BITCLEAR 3
|
|
|
|
#define BITSET 4
|
2008-03-27 11:48:28 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t action;
|
|
|
|
uint32_t value;
|
2008-03-27 14:42:48 +00:00
|
|
|
} seqop_t;
|
2008-03-27 11:48:28 +00:00
|
|
|
|
2008-03-28 16:13:17 +00:00
|
|
|
// Flashing sequence for LED1.
|
2008-03-27 14:42:48 +00:00
|
|
|
static const seqop_t LED1_sequence[] =
|
2008-03-27 11:48:28 +00:00
|
|
|
{
|
2009-06-07 17:53:23 +00:00
|
|
|
{BITCLEAR, PAL_PORT_BIT(PA_LED1)},
|
2008-03-27 11:48:28 +00:00
|
|
|
{SLEEP, 200},
|
2009-06-07 17:53:23 +00:00
|
|
|
{BITSET, PAL_PORT_BIT(PA_LED1)},
|
2008-03-27 12:33:31 +00:00
|
|
|
{SLEEP, 1800},
|
|
|
|
{GOTO, 0}
|
2008-03-27 11:48:28 +00:00
|
|
|
};
|
|
|
|
|
2008-03-28 16:13:17 +00:00
|
|
|
// Flashing sequence for LED2.
|
2008-03-27 14:42:48 +00:00
|
|
|
static const seqop_t LED2_sequence[] =
|
2008-03-27 11:48:28 +00:00
|
|
|
{
|
|
|
|
{SLEEP, 1000},
|
2009-06-07 17:53:23 +00:00
|
|
|
{BITCLEAR, PAL_PORT_BIT(PA_LED2)},
|
2008-03-27 11:48:28 +00:00
|
|
|
{SLEEP, 200},
|
2009-06-07 17:53:23 +00:00
|
|
|
{BITSET, PAL_PORT_BIT(PA_LED2)},
|
2008-03-27 12:33:31 +00:00
|
|
|
{SLEEP, 1800},
|
|
|
|
{GOTO, 1}
|
2008-03-27 11:48:28 +00:00
|
|
|
};
|
|
|
|
|
2008-03-28 16:13:17 +00:00
|
|
|
// Flashing sequence for LED3.
|
2008-03-27 14:42:48 +00:00
|
|
|
static const seqop_t LED3_sequence[] =
|
2008-03-27 11:48:28 +00:00
|
|
|
{
|
2009-06-07 17:53:23 +00:00
|
|
|
{BITCLEAR, PAL_PORT_BIT(PA_LEDUSB)},
|
2008-03-27 11:48:28 +00:00
|
|
|
{SLEEP, 200},
|
2009-06-07 17:53:23 +00:00
|
|
|
{BITSET, PAL_PORT_BIT(PA_LEDUSB)},
|
2008-03-27 12:33:31 +00:00
|
|
|
{SLEEP, 300},
|
|
|
|
{GOTO, 0}
|
2008-03-27 11:48:28 +00:00
|
|
|
};
|
|
|
|
|
2008-03-28 16:13:17 +00:00
|
|
|
/*
|
2008-03-27 14:42:48 +00:00
|
|
|
* Sequencer thread class. It can drive LEDs or other output pins.
|
2008-03-28 16:13:17 +00:00
|
|
|
* Any sequencer is just an instance of this class, all the details are
|
|
|
|
* totally encapsulated and hidden to the application level.
|
2008-03-27 11:48:28 +00:00
|
|
|
*/
|
2009-05-24 13:38:16 +00:00
|
|
|
class SequencerThread : public EnhancedThread<128> {
|
2008-03-27 11:48:28 +00:00
|
|
|
private:
|
2008-03-27 14:42:48 +00:00
|
|
|
const seqop_t *base, *curr; // Thread local variables.
|
2008-03-27 11:48:28 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual msg_t Main(void) {
|
2008-03-28 11:39:30 +00:00
|
|
|
while (true) {
|
2008-03-27 11:48:28 +00:00
|
|
|
switch(curr->action) {
|
|
|
|
case SLEEP:
|
|
|
|
Sleep(curr->value);
|
|
|
|
break;
|
2008-03-27 12:33:31 +00:00
|
|
|
case GOTO:
|
|
|
|
curr = &base[curr->value];
|
|
|
|
continue;
|
2008-03-27 11:48:28 +00:00
|
|
|
case STOP:
|
|
|
|
return 0;
|
|
|
|
case BITCLEAR:
|
2009-06-07 17:53:23 +00:00
|
|
|
palClearPort(IOPORT_A, curr->value);
|
2008-03-27 11:48:28 +00:00
|
|
|
break;
|
|
|
|
case BITSET:
|
2009-06-07 17:53:23 +00:00
|
|
|
palSetPort(IOPORT_A, curr->value);
|
2008-03-27 11:48:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-03-27 12:33:31 +00:00
|
|
|
curr++;
|
2008-03-27 11:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2009-03-15 09:27:36 +00:00
|
|
|
SequencerThread(const seqop_t *sequence) : EnhancedThread<128>("sequencer") {
|
2008-03-27 11:48:28 +00:00
|
|
|
|
|
|
|
base = curr = sequence;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-24 13:38:16 +00:00
|
|
|
/*
|
|
|
|
* Tester thread class. This thread executes the test suite.
|
|
|
|
*/
|
|
|
|
class TesterThread : public EnhancedThread<128> {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual msg_t Main(void) {
|
|
|
|
|
|
|
|
return TestThread(&COM1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
TesterThread(void) : EnhancedThread<128>("tester") {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-03-27 11:48:28 +00:00
|
|
|
/*
|
2008-03-28 16:13:17 +00:00
|
|
|
* Executed as an event handler at 500mS intervals.
|
2008-03-27 11:48:28 +00:00
|
|
|
*/
|
|
|
|
static void TimerHandler(eventid_t id) {
|
|
|
|
|
2009-06-07 17:53:23 +00:00
|
|
|
if (!(palReadPort(IOPORT_A) & BOTH_BUTTONS)) { // Both buttons
|
2009-05-24 13:38:16 +00:00
|
|
|
TesterThread tester;
|
|
|
|
tester.Wait();
|
|
|
|
};
|
2008-03-27 11:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-10-04 12:00:18 +00:00
|
|
|
* Entry point, note, the main() function is already a thread in the system
|
|
|
|
* on entry.
|
2008-03-27 11:48:28 +00:00
|
|
|
*/
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
static const evhandler_t evhndl[] = {
|
|
|
|
TimerHandler
|
|
|
|
};
|
|
|
|
static EvTimer evt;
|
|
|
|
struct EventListener el0;
|
|
|
|
|
2008-03-28 16:13:17 +00:00
|
|
|
evtInit(&evt, 500); // Initializes an event timer.
|
2008-03-27 14:42:48 +00:00
|
|
|
evtStart(&evt); // Starts the event timer.
|
2008-03-28 16:13:17 +00:00
|
|
|
chEvtRegister(&evt.et_es, &el0, 0); // Registers a listener on the source.
|
2008-03-27 11:48:28 +00:00
|
|
|
|
2008-03-27 12:33:31 +00:00
|
|
|
/*
|
2008-12-07 10:21:40 +00:00
|
|
|
* Starts several instances of the SequencerThread class, each one operating
|
2008-03-27 12:33:31 +00:00
|
|
|
* on a different LED.
|
|
|
|
*/
|
2008-03-27 14:42:48 +00:00
|
|
|
SequencerThread blinker1(LED1_sequence);
|
|
|
|
SequencerThread blinker2(LED2_sequence);
|
|
|
|
SequencerThread blinker3(LED3_sequence);
|
2008-03-27 12:33:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Serves timer events.
|
|
|
|
*/
|
|
|
|
while (true)
|
2008-12-07 10:21:40 +00:00
|
|
|
Event::Dispatch(evhndl, Event::WaitOne(ALL_EVENTS));
|
2008-03-27 11:48:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|