The Squirrel interpreter for FRDM-K64F, extended with a set of classes that provide access to the mbed functionality (currently DigitalIn, DigitalInOut, DigitalOut, InterruptIn, PwmOut, Ticker, Timeout, Timer).

Dependencies:   SQUIRREL3 mbed sqbind-0_99

The Squirrel interpreter for FRDM-K64F.

NOTE: Currently of POC quality.

See http://www.squirrel-lang.org/ for information about the Squirrel language.

Currently the following (a subset of their functionality) mbed classes are available from within Squirrel:

  • DigitalIn
  • DigitalOut
  • DigitalInOut
  • PwmOut
  • Ticker
  • Timeout
  • Timer

In addition, InterruptIn is supported, but interrupts are noted when they occur, but only delivered from the main loop of the interpreter.

See also README.txt in the root of the project.

Committer:
jhnwkmn
Date:
Tue Dec 16 12:54:53 2014 +0000
Revision:
0:6f55c7651ccc
Child:
1:540008bb92a2
Added README.txt;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhnwkmn 0:6f55c7651ccc 1 /*
jhnwkmn 0:6f55c7651ccc 2 @License@
jhnwkmn 0:6f55c7651ccc 3 */
jhnwkmn 0:6f55c7651ccc 4
jhnwkmn 0:6f55c7651ccc 5 #include <sqmbed/interruptin.h>
jhnwkmn 0:6f55c7651ccc 6 #include <sqmbed/pointerstorage.h>
jhnwkmn 0:6f55c7651ccc 7
jhnwkmn 0:6f55c7651ccc 8 namespace
jhnwkmn 0:6f55c7651ccc 9 {
jhnwkmn 0:6f55c7651ccc 10
jhnwkmn 0:6f55c7651ccc 11 class Interrupts : public SqMbed::PointerStorage<SqMbed::InterruptIn>
jhnwkmn 0:6f55c7651ccc 12 {
jhnwkmn 0:6f55c7651ccc 13 public:
jhnwkmn 0:6f55c7651ccc 14 static void notify(PinName pin) {
jhnwkmn 0:6f55c7651ccc 15 // Do not store pointers here. Who knows, maybe the handler creates a
jhnwkmn 0:6f55c7651ccc 16 // new InterruptIn and then both s_nSpace and s_ppInterrupts may change.
jhnwkmn 0:6f55c7651ccc 17 for (uint32_t i = 0; i < s_nSpace; ++i) {
jhnwkmn 0:6f55c7651ccc 18 SqMbed::InterruptIn* pInterrupt = s_ppObjects[i];
jhnwkmn 0:6f55c7651ccc 19
jhnwkmn 0:6f55c7651ccc 20 if (pInterrupt && (pInterrupt->pin() == pin)) {
jhnwkmn 0:6f55c7651ccc 21 pInterrupt->call();
jhnwkmn 0:6f55c7651ccc 22 }
jhnwkmn 0:6f55c7651ccc 23 }
jhnwkmn 0:6f55c7651ccc 24 }
jhnwkmn 0:6f55c7651ccc 25 };
jhnwkmn 0:6f55c7651ccc 26
jhnwkmn 0:6f55c7651ccc 27 }
jhnwkmn 0:6f55c7651ccc 28
jhnwkmn 0:6f55c7651ccc 29 template<>
jhnwkmn 0:6f55c7651ccc 30 struct SqBindAllocator<SqMbed::InterruptIn> {
jhnwkmn 0:6f55c7651ccc 31 static SQBIND_INLINE SqMbed::InterruptIn *construct(HSQUIRRELVM) {
jhnwkmn 0:6f55c7651ccc 32 return NULL; // make it not able to construct
jhnwkmn 0:6f55c7651ccc 33 }
jhnwkmn 0:6f55c7651ccc 34
jhnwkmn 0:6f55c7651ccc 35 static SQBIND_INLINE SqMbed::InterruptIn *copy_construct(const SqMbed::InterruptIn* p_from) {
jhnwkmn 0:6f55c7651ccc 36 return NULL; // make it not able to copy-construct
jhnwkmn 0:6f55c7651ccc 37 }
jhnwkmn 0:6f55c7651ccc 38
jhnwkmn 0:6f55c7651ccc 39 static SQBIND_INLINE bool assign(SqMbed::InterruptIn* p_val, const SqMbed::InterruptIn* p_from) {
jhnwkmn 0:6f55c7651ccc 40 return false; // make it not able to assign
jhnwkmn 0:6f55c7651ccc 41 }
jhnwkmn 0:6f55c7651ccc 42
jhnwkmn 0:6f55c7651ccc 43 static SQBIND_INLINE void destruct(SqMbed::InterruptIn* p_instance) {
jhnwkmn 0:6f55c7651ccc 44 delete p_instance;
jhnwkmn 0:6f55c7651ccc 45 }
jhnwkmn 0:6f55c7651ccc 46
jhnwkmn 0:6f55c7651ccc 47 static SQBIND_INLINE SqMbed::InterruptIn& get_empty() {
jhnwkmn 0:6f55c7651ccc 48 // if someone tries to assign, this will crash.
jhnwkmn 0:6f55c7651ccc 49 // however, this will likely never be called anyway.
jhnwkmn 0:6f55c7651ccc 50 static SqMbed::InterruptIn *crashplease=NULL;
jhnwkmn 0:6f55c7651ccc 51 return *crashplease;
jhnwkmn 0:6f55c7651ccc 52 }
jhnwkmn 0:6f55c7651ccc 53 };
jhnwkmn 0:6f55c7651ccc 54
jhnwkmn 0:6f55c7651ccc 55 namespace SqMbed
jhnwkmn 0:6f55c7651ccc 56 {
jhnwkmn 0:6f55c7651ccc 57
jhnwkmn 0:6f55c7651ccc 58 InterruptIn::InterruptIn(HSQUIRRELVM vm, PinName pin)
jhnwkmn 0:6f55c7651ccc 59 : m_vm(vm)
jhnwkmn 0:6f55c7651ccc 60 , m_pin(pin)
jhnwkmn 0:6f55c7651ccc 61 , m_intIn(pin)
jhnwkmn 0:6f55c7651ccc 62 {
jhnwkmn 0:6f55c7651ccc 63 m_object._type = OT_NULL;
jhnwkmn 0:6f55c7651ccc 64
jhnwkmn 0:6f55c7651ccc 65 Interrupts::add(this);
jhnwkmn 0:6f55c7651ccc 66 }
jhnwkmn 0:6f55c7651ccc 67
jhnwkmn 0:6f55c7651ccc 68 void InterruptIn::rise(HSQOBJECT o)
jhnwkmn 0:6f55c7651ccc 69 {
jhnwkmn 0:6f55c7651ccc 70 if (m_object._type != OT_NULL) {
jhnwkmn 0:6f55c7651ccc 71 sq_release(m_vm, &m_object);
jhnwkmn 0:6f55c7651ccc 72 m_object._type = OT_NULL;
jhnwkmn 0:6f55c7651ccc 73 }
jhnwkmn 0:6f55c7651ccc 74
jhnwkmn 0:6f55c7651ccc 75 m_object = o;
jhnwkmn 0:6f55c7651ccc 76 sq_addref(m_vm, &m_object);
jhnwkmn 0:6f55c7651ccc 77
jhnwkmn 0:6f55c7651ccc 78 m_intIn.rise(this, &InterruptIn::raised);
jhnwkmn 0:6f55c7651ccc 79 }
jhnwkmn 0:6f55c7651ccc 80
jhnwkmn 0:6f55c7651ccc 81 InterruptIn::~InterruptIn()
jhnwkmn 0:6f55c7651ccc 82 {
jhnwkmn 0:6f55c7651ccc 83 Interrupts::remove(this);
jhnwkmn 0:6f55c7651ccc 84 }
jhnwkmn 0:6f55c7651ccc 85
jhnwkmn 0:6f55c7651ccc 86 void InterruptIn::raised()
jhnwkmn 0:6f55c7651ccc 87 {
jhnwkmn 0:6f55c7651ccc 88 InterruptIn::postponeInterrupt(m_pin);
jhnwkmn 0:6f55c7651ccc 89 }
jhnwkmn 0:6f55c7651ccc 90
jhnwkmn 0:6f55c7651ccc 91 void InterruptIn::call()
jhnwkmn 0:6f55c7651ccc 92 {
jhnwkmn 0:6f55c7651ccc 93 int top = sq_gettop(m_vm);
jhnwkmn 0:6f55c7651ccc 94 sq_pushobject(m_vm, m_object); // The function
jhnwkmn 0:6f55c7651ccc 95 sq_pushroottable(m_vm); // This
jhnwkmn 0:6f55c7651ccc 96 sq_call(m_vm, 1, 0, 0); // Only this as argument.
jhnwkmn 0:6f55c7651ccc 97 sq_settop(m_vm, top); // Restore the stack.
jhnwkmn 0:6f55c7651ccc 98 }
jhnwkmn 0:6f55c7651ccc 99
jhnwkmn 0:6f55c7651ccc 100 // static
jhnwkmn 0:6f55c7651ccc 101 void InterruptIn::deliver(PinName pin)
jhnwkmn 0:6f55c7651ccc 102 {
jhnwkmn 0:6f55c7651ccc 103 Interrupts::notify(pin);
jhnwkmn 0:6f55c7651ccc 104 }
jhnwkmn 0:6f55c7651ccc 105
jhnwkmn 0:6f55c7651ccc 106 // static
jhnwkmn 0:6f55c7651ccc 107 void InterruptIn::bind(HSQUIRRELVM vm)
jhnwkmn 0:6f55c7651ccc 108 {
jhnwkmn 0:6f55c7651ccc 109 SqBind<InterruptIn>::init(vm, _SC("InterruptIn"));
jhnwkmn 0:6f55c7651ccc 110 SqBind<InterruptIn>::set_custom_constructor(&InterruptIn::constructor);
jhnwkmn 0:6f55c7651ccc 111
jhnwkmn 0:6f55c7651ccc 112 sqbind_method(vm, "rise", &InterruptIn::rise);
jhnwkmn 0:6f55c7651ccc 113 sqbind_method(vm, "call", &InterruptIn::call);
jhnwkmn 0:6f55c7651ccc 114 }
jhnwkmn 0:6f55c7651ccc 115
jhnwkmn 0:6f55c7651ccc 116 // static
jhnwkmn 0:6f55c7651ccc 117 InterruptIn* InterruptIn::constructor(HSQUIRRELVM vm)
jhnwkmn 0:6f55c7651ccc 118 {
jhnwkmn 0:6f55c7651ccc 119 InterruptIn* pThis = 0;
jhnwkmn 0:6f55c7651ccc 120
jhnwkmn 0:6f55c7651ccc 121 int nParams = sq_gettop(vm);
jhnwkmn 0:6f55c7651ccc 122
jhnwkmn 0:6f55c7651ccc 123 if (nParams == 2) { // Need 1 (sic) params.
jhnwkmn 0:6f55c7651ccc 124 SQInteger i;
jhnwkmn 0:6f55c7651ccc 125
jhnwkmn 0:6f55c7651ccc 126 if (!SQ_FAILED(sq_getinteger(vm, 2, &i))) {
jhnwkmn 0:6f55c7651ccc 127 if (Interrupts::reserve()) {
jhnwkmn 0:6f55c7651ccc 128 pThis = new InterruptIn(vm, static_cast<PinName>(i));
jhnwkmn 0:6f55c7651ccc 129 } else {
jhnwkmn 0:6f55c7651ccc 130 printf("error: Out of memory.\n");
jhnwkmn 0:6f55c7651ccc 131 }
jhnwkmn 0:6f55c7651ccc 132 } else {
jhnwkmn 0:6f55c7651ccc 133 printf("error: Could not get integer.\n");
jhnwkmn 0:6f55c7651ccc 134 }
jhnwkmn 0:6f55c7651ccc 135 } else {
jhnwkmn 0:6f55c7651ccc 136 printf("error: nParams != 2\n");
jhnwkmn 0:6f55c7651ccc 137 }
jhnwkmn 0:6f55c7651ccc 138
jhnwkmn 0:6f55c7651ccc 139 return pThis;
jhnwkmn 0:6f55c7651ccc 140 }
jhnwkmn 0:6f55c7651ccc 141
jhnwkmn 0:6f55c7651ccc 142 }