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>
|
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
|
|
|
|
|
|
|
#include <lpc214x.h>
|
|
|
|
#include <lpc214x_serial.h>
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
{BITCLEAR, 0x00000400},
|
|
|
|
{SLEEP, 200},
|
|
|
|
{BITSET, 0x00000400},
|
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},
|
|
|
|
{BITCLEAR, 0x00000800},
|
|
|
|
{SLEEP, 200},
|
|
|
|
{BITSET, 0x00000800},
|
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
|
|
|
{
|
|
|
|
{BITCLEAR, 0x80000000},
|
|
|
|
{SLEEP, 200},
|
|
|
|
{BITSET, 0x80000000},
|
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-02-21 11:14:24 +00:00
|
|
|
class SequencerThread : 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:
|
|
|
|
IO0CLR = curr->value;
|
|
|
|
break;
|
|
|
|
case BITSET:
|
|
|
|
IO0SET = curr->value;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
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) {
|
|
|
|
|
|
|
|
if (!(IO0PIN & 0x00018000)) // Both buttons
|
|
|
|
TestThread(&COM1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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;
|
|
|
|
}
|