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 13:32:06 2014 +0000
Revision:
2:0df9688c9a11
Parent:
1:540008bb92a2
Updated libraries.

Who changed what in which revision?

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