2017-08-05 00:58:55 +03:00
|
|
|
#include "pxt.h"
|
2020-02-01 04:49:55 +03:00
|
|
|
#include "LowLevelTimer.h"
|
|
|
|
using namespace codal;
|
2017-08-05 00:58:55 +03:00
|
|
|
|
2017-10-21 18:30:01 +03:00
|
|
|
void cpu_clock_init(void);
|
|
|
|
|
2018-04-06 05:08:56 +03:00
|
|
|
PXT_ABI(__aeabi_dadd)
|
|
|
|
PXT_ABI(__aeabi_dcmplt)
|
|
|
|
PXT_ABI(__aeabi_dcmpgt)
|
|
|
|
PXT_ABI(__aeabi_dsub)
|
|
|
|
PXT_ABI(__aeabi_ddiv)
|
|
|
|
PXT_ABI(__aeabi_dmul)
|
|
|
|
|
2020-09-01 15:01:27 +03:00
|
|
|
#ifdef DEVICE_GET_FIBER_LIST_AVAILABLE
|
|
|
|
// newer codal-core has get_fiber_list() but not list_fibers()
|
|
|
|
namespace codal {
|
2020-10-12 19:00:03 +03:00
|
|
|
/*
|
2020-09-01 15:01:27 +03:00
|
|
|
* Return all current fibers.
|
|
|
|
*
|
|
|
|
* @param dest If non-null, it points to an array of pointers to fibers to store results in.
|
|
|
|
*
|
|
|
|
* @return the number of fibers (potentially) stored
|
|
|
|
*/
|
|
|
|
int list_fibers(Fiber **dest) {
|
|
|
|
int i = 0;
|
|
|
|
for (Fiber *fib = codal::get_fiber_list(); fib; fib = fib->next) {
|
|
|
|
if (dest)
|
|
|
|
dest[i] = fib;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace codal
|
|
|
|
#endif
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
namespace pxt {
|
|
|
|
|
2017-12-12 20:28:16 +03:00
|
|
|
void platform_init();
|
|
|
|
void usb_init();
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
// The first two word are used to tell the bootloader that a single reset should start the
|
|
|
|
// bootloader and the MSD device, not us.
|
|
|
|
// The rest is reserved for partial flashing checksums.
|
|
|
|
__attribute__((section(".binmeta"))) __attribute__((used)) const uint32_t pxt_binmeta[] = {
|
|
|
|
0x87eeb07c, 0x87eeb07c, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff,
|
|
|
|
0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff,
|
|
|
|
};
|
|
|
|
|
2017-09-26 20:32:45 +03:00
|
|
|
Event lastEvent;
|
2017-08-05 00:58:55 +03:00
|
|
|
MessageBus devMessageBus;
|
|
|
|
codal::CodalDevice device;
|
|
|
|
|
2018-04-06 05:08:56 +03:00
|
|
|
struct FreeList {
|
|
|
|
FreeList *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void commInit() {
|
|
|
|
int commSize = bytecode[20];
|
2018-11-13 02:34:52 +03:00
|
|
|
if (!commSize)
|
|
|
|
return;
|
2018-04-06 05:08:56 +03:00
|
|
|
|
2020-10-06 20:56:05 +03:00
|
|
|
void *r = app_alloc_at((void *)PXT_COMM_BASE, commSize);
|
|
|
|
DMESG("comm %d -> %p", commSize, r);
|
|
|
|
if (!r)
|
|
|
|
target_panic(20);
|
2018-04-06 05:08:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
static void initCodal() {
|
2017-10-21 18:30:01 +03:00
|
|
|
cpu_clock_init();
|
2017-08-05 00:58:55 +03:00
|
|
|
|
2018-04-06 05:08:56 +03:00
|
|
|
commInit();
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
// Bring up fiber scheduler.
|
|
|
|
scheduler_init(devMessageBus);
|
|
|
|
|
2017-10-21 18:30:01 +03:00
|
|
|
// We probably don't need that - components are initialized when one obtains
|
|
|
|
// the reference to it.
|
2017-10-23 23:48:41 +03:00
|
|
|
// devMessageBus.listen(DEVICE_ID_MESSAGE_BUS_LISTENER, DEVICE_EVT_ANY, this,
|
|
|
|
// &CircuitPlayground::onListenerRegisteredEvent);
|
|
|
|
|
|
|
|
for (int i = 0; i < DEVICE_COMPONENT_COUNT; i++) {
|
|
|
|
if (CodalComponent::components[i])
|
2017-10-21 18:30:01 +03:00
|
|
|
CodalComponent::components[i]->init();
|
|
|
|
}
|
2017-10-23 23:48:41 +03:00
|
|
|
|
2017-12-12 20:28:16 +03:00
|
|
|
usb_init();
|
2018-12-08 02:06:12 +03:00
|
|
|
|
|
|
|
auto led = LOOKUP_PIN(LED);
|
|
|
|
if (led) {
|
|
|
|
led->setDigitalValue(0);
|
|
|
|
}
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// An adapter for the API expected by the run-time.
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// We have the invariant that if [dispatchEvent] is registered against the DAL
|
|
|
|
// for a given event, then [handlersMap] contains a valid entry for that
|
|
|
|
// event.
|
|
|
|
void dispatchEvent(Event e) {
|
|
|
|
lastEvent = e;
|
|
|
|
|
|
|
|
auto curr = findBinding(e.source, e.value);
|
2018-05-17 18:58:58 +03:00
|
|
|
auto value = fromInt(e.value);
|
2019-05-17 18:30:54 +03:00
|
|
|
while (curr) {
|
2018-05-17 18:58:58 +03:00
|
|
|
runAction1(curr->action, value);
|
2019-05-17 18:30:54 +03:00
|
|
|
curr = nextBinding(curr->next, e.source, e.value);
|
|
|
|
}
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
2017-11-08 05:15:30 +03:00
|
|
|
void registerWithDal(int id, int event, Action a, int flags) {
|
2017-08-05 00:58:55 +03:00
|
|
|
// first time?
|
2019-10-04 10:10:55 +03:00
|
|
|
if (!findBinding(id, event)) {
|
2017-11-08 05:15:30 +03:00
|
|
|
devMessageBus.listen(id, event, dispatchEvent, flags);
|
2019-10-04 10:10:55 +03:00
|
|
|
if (event == 0) {
|
|
|
|
// we're registering for all events on given ID
|
|
|
|
// need to remove old listeners for specific events
|
|
|
|
auto curr = findBinding(id, -1);
|
|
|
|
while (curr) {
|
|
|
|
devMessageBus.ignore(id, curr->value, dispatchEvent);
|
|
|
|
curr = nextBinding(curr->next, id, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-05 00:58:55 +03:00
|
|
|
setBinding(id, event, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fiberDone(void *a) {
|
2018-11-13 02:34:52 +03:00
|
|
|
unregisterGCPtr((Action)a);
|
2017-08-05 00:58:55 +03:00
|
|
|
release_fiber();
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:25:27 +03:00
|
|
|
void releaseFiber() {
|
2018-11-13 02:34:52 +03:00
|
|
|
release_fiber();
|
2018-01-26 03:25:27 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 20:32:45 +03:00
|
|
|
void sleep_ms(unsigned ms) {
|
2017-08-05 00:58:55 +03:00
|
|
|
fiber_sleep(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sleep_us(uint64_t us) {
|
2018-09-26 04:26:45 +03:00
|
|
|
target_wait_us(us);
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void forever_stub(void *a) {
|
|
|
|
while (true) {
|
|
|
|
runAction0((Action)a);
|
|
|
|
fiber_sleep(20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void runForever(Action a) {
|
|
|
|
if (a != 0) {
|
2018-11-13 02:34:52 +03:00
|
|
|
registerGCPtr(a);
|
2017-08-05 00:58:55 +03:00
|
|
|
create_fiber(forever_stub, (void *)a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 00:14:46 +03:00
|
|
|
void runInParallel(Action a) {
|
2017-08-05 00:58:55 +03:00
|
|
|
if (a != 0) {
|
2018-11-13 02:34:52 +03:00
|
|
|
registerGCPtr(a);
|
2020-10-06 20:56:05 +03:00
|
|
|
create_fiber((void (*)(void *))(void*)runAction0, (void *)a, fiberDone);
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void waitForEvent(int id, int event) {
|
|
|
|
fiber_wait_for_event(id, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initRuntime() {
|
2019-12-07 06:31:22 +03:00
|
|
|
initSystemTimer();
|
2017-08-05 00:58:55 +03:00
|
|
|
initCodal();
|
2017-12-12 20:28:16 +03:00
|
|
|
platform_init();
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//%
|
2017-09-26 20:32:45 +03:00
|
|
|
unsigned afterProgramPage() {
|
|
|
|
unsigned ptr = (unsigned)&bytecode[0];
|
2017-08-05 00:58:55 +03:00
|
|
|
ptr += programSize();
|
|
|
|
ptr = (ptr + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2019-07-30 03:22:05 +03:00
|
|
|
uint64_t getLongSerialNumber() {
|
|
|
|
return device.getSerialNumber();
|
|
|
|
}
|
|
|
|
|
2017-08-05 00:58:55 +03:00
|
|
|
int current_time_ms() {
|
|
|
|
return system_timer_current_time();
|
|
|
|
}
|
2018-11-13 02:34:52 +03:00
|
|
|
|
2019-08-21 03:35:25 +03:00
|
|
|
uint64_t current_time_us() {
|
|
|
|
return system_timer_current_time_us();
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:34:52 +03:00
|
|
|
ThreadContext *getThreadContext() {
|
2019-02-06 20:16:31 +03:00
|
|
|
if (!currentFiber)
|
|
|
|
return NULL;
|
2018-11-13 02:34:52 +03:00
|
|
|
return (ThreadContext *)currentFiber->user_data;
|
2017-08-05 00:58:55 +03:00
|
|
|
}
|
2018-11-13 02:34:52 +03:00
|
|
|
|
|
|
|
void setThreadContext(ThreadContext *ctx) {
|
|
|
|
currentFiber->user_data = ctx;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:16:09 +03:00
|
|
|
static void *threadAddressFor(codal::Fiber *fib, void *sp) {
|
2018-11-13 02:34:52 +03:00
|
|
|
if (fib == currentFiber)
|
|
|
|
return sp;
|
|
|
|
return (uint8_t *)sp + ((uint8_t *)fib->stack_top - (uint8_t *)tcb_get_stack_base(fib->tcb));
|
|
|
|
}
|
|
|
|
|
2018-12-01 01:21:05 +03:00
|
|
|
void gcProcessStacks(int flags) {
|
2019-02-06 20:16:31 +03:00
|
|
|
// check scheduler is initialized
|
|
|
|
if (!currentFiber) {
|
|
|
|
// make sure we allocate something to at least initalize the memory allocator
|
2019-10-04 10:10:55 +03:00
|
|
|
void *volatile p = xmalloc(1);
|
2019-02-06 20:16:31 +03:00
|
|
|
xfree(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:16:09 +03:00
|
|
|
int numFibers = codal::list_fibers(NULL);
|
2018-12-08 02:06:12 +03:00
|
|
|
codal::Fiber **fibers = (codal::Fiber **)xmalloc(sizeof(codal::Fiber *) * numFibers);
|
|
|
|
int num2 = codal::list_fibers(fibers);
|
|
|
|
if (numFibers != num2)
|
|
|
|
oops(12);
|
2018-12-01 01:21:05 +03:00
|
|
|
int cnt = 0;
|
2018-11-20 02:16:09 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < numFibers; ++i) {
|
|
|
|
auto fib = fibers[i];
|
|
|
|
auto ctx = (ThreadContext *)fib->user_data;
|
|
|
|
if (!ctx)
|
|
|
|
continue;
|
2019-07-30 00:04:07 +03:00
|
|
|
gcProcess(ctx->thrownValue);
|
2018-11-20 02:16:09 +03:00
|
|
|
for (auto seg = &ctx->stack; seg; seg = seg->next) {
|
|
|
|
auto ptr = (TValue *)threadAddressFor(fib, seg->top);
|
|
|
|
auto end = (TValue *)threadAddressFor(fib, seg->bottom);
|
2018-12-01 01:21:05 +03:00
|
|
|
if (flags & 2)
|
|
|
|
DMESG("RS%d:%p/%d", cnt++, ptr, end - ptr);
|
2018-12-08 02:06:12 +03:00
|
|
|
// VLOG("mark: %p - %p", ptr, end);
|
2018-11-20 02:16:09 +03:00
|
|
|
while (ptr < end) {
|
|
|
|
gcProcess(*ptr++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:06:12 +03:00
|
|
|
xfree(fibers);
|
2018-11-20 02:16:09 +03:00
|
|
|
}
|
|
|
|
|
2020-02-01 04:49:55 +03:00
|
|
|
LowLevelTimer *getJACDACTimer() {
|
|
|
|
static LowLevelTimer *jacdacTimer;
|
|
|
|
if (!jacdacTimer) {
|
|
|
|
jacdacTimer = allocateTimer();
|
|
|
|
jacdacTimer->setIRQPriority(1);
|
|
|
|
}
|
|
|
|
return jacdacTimer;
|
|
|
|
}
|
|
|
|
void initSystemTimer() {
|
|
|
|
new CODAL_TIMER(*allocateTimer());
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:34:52 +03:00
|
|
|
} // namespace pxt
|