2014-01-06 03:20:11 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "mpconfig.h"
|
2014-07-15 01:19:27 -04:00
|
|
|
#include "misc.h"
|
2014-01-15 01:23:56 -05:00
|
|
|
#include "qstr.h"
|
2014-06-16 01:33:14 -04:00
|
|
|
#include "nlr.h"
|
2014-01-06 03:20:11 -05:00
|
|
|
#include "lexer.h"
|
2014-01-08 04:00:22 -05:00
|
|
|
#include "lexermemzip.h"
|
2014-01-06 03:20:11 -05:00
|
|
|
#include "parse.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime.h"
|
2014-06-16 01:33:14 -04:00
|
|
|
#include "gc.h"
|
|
|
|
#include "gccollect.h"
|
|
|
|
#include "pyexec.h"
|
|
|
|
#include "readline.h"
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
2014-07-02 08:42:37 -04:00
|
|
|
#include MICROPY_HAL_H
|
2014-06-16 01:33:14 -04:00
|
|
|
|
2014-01-15 01:23:56 -05:00
|
|
|
#include "servo.h"
|
2014-01-06 03:20:11 -05:00
|
|
|
#include "usb.h"
|
|
|
|
#include "led.h"
|
2014-08-03 00:28:32 -04:00
|
|
|
#include "uart.h"
|
2014-07-22 10:57:36 -04:00
|
|
|
#include "pin.h"
|
2014-08-03 00:28:32 -04:00
|
|
|
#include "pybstdio.h"
|
2014-01-06 03:20:11 -05:00
|
|
|
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
extern uint32_t _heap_start;
|
2014-01-06 03:20:11 -05:00
|
|
|
|
2014-01-08 04:00:22 -05:00
|
|
|
void flash_error(int n) {
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
led_state(PYB_LED_BUILTIN, 1);
|
|
|
|
delay(250);
|
|
|
|
led_state(PYB_LED_BUILTIN, 0);
|
|
|
|
delay(250);
|
2014-01-06 03:20:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 12:38:24 -04:00
|
|
|
void NORETURN __fatal_error(const char *msg) {
|
2014-06-16 01:33:14 -04:00
|
|
|
for (volatile uint delay = 0; delay < 10000000; delay++) {
|
|
|
|
}
|
|
|
|
led_state(1, 1);
|
|
|
|
led_state(2, 1);
|
|
|
|
led_state(3, 1);
|
|
|
|
led_state(4, 1);
|
|
|
|
stdout_tx_strn("\nFATAL ERROR:\n", 14);
|
|
|
|
stdout_tx_strn(msg, strlen(msg));
|
|
|
|
for (uint i = 0;;) {
|
|
|
|
led_toggle(((i++) & 3) + 1);
|
|
|
|
for (volatile uint delay = 0; delay < 10000000; delay++) {
|
|
|
|
}
|
|
|
|
if (i >= 16) {
|
|
|
|
// to conserve power
|
|
|
|
__WFI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nlr_jump_fail(void *val) {
|
|
|
|
printf("FATAL: uncaught exception %p\n", val);
|
|
|
|
__fatal_error("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void __assert_func(const char *file, int line, const char *func, const char *expr) {
|
|
|
|
|
|
|
|
printf("Assertion failed: %s, file %s, line %d\n", expr, file, line);
|
|
|
|
__fatal_error("");
|
|
|
|
}
|
2014-01-06 03:20:11 -05:00
|
|
|
|
2014-01-15 01:23:56 -05:00
|
|
|
mp_obj_t pyb_analog_read(mp_obj_t pin_obj) {
|
|
|
|
uint pin = mp_obj_get_int(pin_obj);
|
|
|
|
int val = analogRead(pin);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t pyb_analog_write(mp_obj_t pin_obj, mp_obj_t val_obj) {
|
|
|
|
uint pin = mp_obj_get_int(pin_obj);
|
|
|
|
int val = mp_obj_get_int(val_obj);
|
|
|
|
analogWrite(pin, val);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t pyb_analog_write_resolution(mp_obj_t res_obj) {
|
|
|
|
int res = mp_obj_get_int(res_obj);
|
|
|
|
analogWriteResolution(res);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t pyb_analog_write_frequency(mp_obj_t pin_obj, mp_obj_t freq_obj) {
|
|
|
|
uint pin = mp_obj_get_int(pin_obj);
|
|
|
|
int freq = mp_obj_get_int(freq_obj);
|
|
|
|
analogWriteFrequency(pin, freq);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
#if 0
|
2014-01-06 03:20:11 -05:00
|
|
|
// get lots of info about the board
|
|
|
|
static mp_obj_t pyb_info(void) {
|
|
|
|
// get and print unique id; 96 bits
|
|
|
|
{
|
|
|
|
byte *id = (byte*)0x40048058;
|
|
|
|
printf("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get and print clock speeds
|
|
|
|
printf("CPU=%u\nBUS=%u\nMEM=%u\n", F_CPU, F_BUS, F_MEM);
|
|
|
|
|
|
|
|
// to print info about memory
|
|
|
|
{
|
|
|
|
printf("_sdata=%p\n", &_sdata);
|
|
|
|
printf("_edata=%p\n", &_edata);
|
|
|
|
printf("_sbss=%p\n", &_sbss);
|
|
|
|
printf("_ebss=%p\n", &_ebss);
|
|
|
|
printf("_estack=%p\n", &_estack);
|
|
|
|
printf("_etext=%p\n", &_etext);
|
|
|
|
printf("_heap_start=%p\n", &_heap_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// GC info
|
|
|
|
{
|
|
|
|
gc_info_t info;
|
|
|
|
gc_info(&info);
|
|
|
|
printf("GC:\n");
|
2014-06-16 01:33:14 -04:00
|
|
|
printf(" %u total\n", info.total);
|
|
|
|
printf(" %u used %u free\n", info.used, info.free);
|
|
|
|
printf(" 1=%u 2=%u m=%u\n", info.num_1block, info.num_2block, info.max_block);
|
2014-01-06 03:20:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// free space on flash
|
|
|
|
{
|
|
|
|
DWORD nclst;
|
|
|
|
FATFS *fatfs;
|
|
|
|
f_getfree("0:", &nclst, &fatfs);
|
|
|
|
printf("LFS free: %u bytes\n", (uint)(nclst * fatfs->csize * 512));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
#endif
|
|
|
|
|
2014-01-06 03:20:11 -05:00
|
|
|
#define RAM_START (0x1FFF8000) // fixed for chip
|
|
|
|
#define HEAP_END (0x20006000) // tunable
|
|
|
|
#define RAM_END (0x20008000) // fixed for chip
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
#if 0
|
2014-01-06 03:20:11 -05:00
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
void gc_helper_get_regs_and_clean_stack(mp_uint_t *regs, mp_uint_t heap_end);
|
2014-01-06 03:20:11 -05:00
|
|
|
|
|
|
|
mp_obj_t pyb_gc(void) {
|
|
|
|
gc_collect();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t pyb_gpio(int n_args, mp_obj_t *args) {
|
|
|
|
//assert(1 <= n_args && n_args <= 2);
|
|
|
|
|
|
|
|
uint pin = mp_obj_get_int(args[0]);
|
|
|
|
if (pin > CORE_NUM_DIGITAL) {
|
|
|
|
goto pin_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_args == 1) {
|
|
|
|
// get pin
|
|
|
|
pinMode(pin, INPUT);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(digitalRead(pin));
|
|
|
|
}
|
|
|
|
|
|
|
|
// set pin
|
|
|
|
pinMode(pin, OUTPUT);
|
2014-03-30 08:35:08 -04:00
|
|
|
digitalWrite(pin, mp_obj_is_true(args[1]));
|
2014-01-06 03:20:11 -05:00
|
|
|
return mp_const_none;
|
|
|
|
|
|
|
|
pin_error:
|
2014-06-16 01:33:14 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %d does not exist", pin));
|
2014-01-06 03:20:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_gpio_obj, 1, 2, pyb_gpio);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
|
|
|
|
mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
|
|
|
|
uint8_t data[4];
|
|
|
|
data[0] = mp_obj_get_int(items[0]);
|
|
|
|
data[1] = mp_obj_get_int(items[1]);
|
|
|
|
data[2] = mp_obj_get_int(items[2]);
|
|
|
|
data[3] = mp_obj_get_int(items[3]);
|
|
|
|
usb_hid_send_report(data);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
#endif // 0
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_config_source_dir = MP_OBJ_NULL;
|
|
|
|
STATIC mp_obj_t pyb_config_main = MP_OBJ_NULL;
|
|
|
|
STATIC mp_obj_t pyb_config_usb_mode = MP_OBJ_NULL;
|
2014-01-08 04:00:22 -05:00
|
|
|
|
|
|
|
mp_obj_t pyb_source_dir(mp_obj_t source_dir) {
|
2014-01-15 01:23:56 -05:00
|
|
|
if (MP_OBJ_IS_STR(source_dir)) {
|
|
|
|
pyb_config_source_dir = source_dir;
|
|
|
|
}
|
2014-01-08 04:00:22 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_source_dir_obj, pyb_source_dir);
|
|
|
|
|
2014-01-08 04:00:22 -05:00
|
|
|
mp_obj_t pyb_main(mp_obj_t main) {
|
2014-01-15 01:23:56 -05:00
|
|
|
if (MP_OBJ_IS_STR(main)) {
|
|
|
|
pyb_config_main = main;
|
|
|
|
}
|
2014-01-08 04:00:22 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_main_obj, pyb_main);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_usb_mode(mp_obj_t usb_mode) {
|
|
|
|
if (MP_OBJ_IS_STR(usb_mode)) {
|
|
|
|
pyb_config_usb_mode = usb_mode;
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_usb_mode_obj, pyb_usb_mode);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
2014-01-06 03:20:11 -05:00
|
|
|
mp_obj_t pyb_delay(mp_obj_t count) {
|
|
|
|
delay(mp_obj_get_int(count));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t pyb_led(mp_obj_t state) {
|
2014-03-30 08:35:08 -04:00
|
|
|
led_state(PYB_LED_BUILTIN, mp_obj_is_true(state));
|
2014-01-06 03:20:11 -05:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
#endif // 0
|
2014-01-08 04:00:22 -05:00
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
#if 0
|
2014-01-06 03:20:11 -05:00
|
|
|
char *strdup(const char *str) {
|
|
|
|
uint32_t len = strlen(str);
|
|
|
|
char *s2 = m_new(char, len + 1);
|
|
|
|
memcpy(s2, str, len);
|
|
|
|
s2[len] = 0;
|
|
|
|
return s2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-07 12:49:42 -05:00
|
|
|
int main(void) {
|
2014-01-15 01:23:56 -05:00
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
delay(1000);
|
2014-01-06 03:20:11 -05:00
|
|
|
|
|
|
|
led_init();
|
|
|
|
|
|
|
|
// int first_soft_reset = true;
|
|
|
|
|
|
|
|
soft_reset:
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
led_state(PYB_LED_BUILTIN, 1);
|
|
|
|
|
2014-01-06 03:20:11 -05:00
|
|
|
// GC init
|
|
|
|
gc_init(&_heap_start, (void*)HEAP_END);
|
|
|
|
|
2014-08-04 05:05:16 -04:00
|
|
|
// Micro Python init
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_init();
|
2014-01-06 03:20:11 -05:00
|
|
|
|
2014-08-04 12:27:29 -04:00
|
|
|
readline_init0();
|
2014-06-16 01:33:14 -04:00
|
|
|
|
2014-07-22 10:57:36 -04:00
|
|
|
pin_init0();
|
2014-06-16 01:33:14 -04:00
|
|
|
|
|
|
|
#if 0
|
2014-01-06 03:20:11 -05:00
|
|
|
// add some functions to the python namespace
|
|
|
|
{
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_store_name(MP_QSTR_help, mp_make_function_n(0, pyb_help));
|
2014-01-15 01:23:56 -05:00
|
|
|
mp_obj_t m = mp_obj_new_module(MP_QSTR_pyb);
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_store_attr(m, MP_QSTR_info, mp_make_function_n(0, pyb_info));
|
|
|
|
mp_store_attr(m, MP_QSTR_source_dir, mp_make_function_n(1, pyb_source_dir));
|
|
|
|
mp_store_attr(m, MP_QSTR_main, mp_make_function_n(1, pyb_main));
|
|
|
|
mp_store_attr(m, MP_QSTR_gc, mp_make_function_n(0, pyb_gc));
|
|
|
|
mp_store_attr(m, MP_QSTR_delay, mp_make_function_n(1, pyb_delay));
|
|
|
|
mp_store_attr(m, MP_QSTR_led, mp_make_function_n(1, pyb_led));
|
2014-06-16 01:33:14 -04:00
|
|
|
mp_store_attr(m, MP_QSTR_LED, (mp_obj_t)&pyb_led_type);
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_store_attr(m, MP_QSTR_analogRead, mp_make_function_n(1, pyb_analog_read));
|
|
|
|
mp_store_attr(m, MP_QSTR_analogWrite, mp_make_function_n(2, pyb_analog_write));
|
|
|
|
mp_store_attr(m, MP_QSTR_analogWriteResolution, mp_make_function_n(1, pyb_analog_write_resolution));
|
|
|
|
mp_store_attr(m, MP_QSTR_analogWriteFrequency, mp_make_function_n(2, pyb_analog_write_frequency));
|
|
|
|
|
|
|
|
mp_store_attr(m, MP_QSTR_gpio, (mp_obj_t)&pyb_gpio_obj);
|
|
|
|
mp_store_attr(m, MP_QSTR_Servo, mp_make_function_n(0, pyb_Servo));
|
|
|
|
mp_store_name(MP_QSTR_pyb, m);
|
2014-01-06 03:20:11 -05:00
|
|
|
}
|
2014-06-16 01:33:14 -04:00
|
|
|
#endif
|
2014-01-06 03:20:11 -05:00
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
if (!pyexec_file("/boot.py")) {
|
2014-01-08 04:00:22 -05:00
|
|
|
flash_error(4);
|
|
|
|
}
|
|
|
|
|
2014-01-06 03:20:11 -05:00
|
|
|
// Turn bootup LED off
|
|
|
|
led_state(PYB_LED_BUILTIN, 0);
|
|
|
|
|
2014-01-08 04:00:22 -05:00
|
|
|
// run main script
|
|
|
|
{
|
|
|
|
vstr_t *vstr = vstr_new();
|
|
|
|
vstr_add_str(vstr, "/");
|
2014-01-15 01:23:56 -05:00
|
|
|
if (pyb_config_main == MP_OBJ_NULL) {
|
2014-01-08 04:00:22 -05:00
|
|
|
vstr_add_str(vstr, "main.py");
|
|
|
|
} else {
|
2014-01-15 01:23:56 -05:00
|
|
|
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
|
2014-01-08 04:00:22 -05:00
|
|
|
}
|
2014-06-16 01:33:14 -04:00
|
|
|
if (!pyexec_file(vstr_str(vstr))) {
|
2014-01-08 04:00:22 -05:00
|
|
|
flash_error(3);
|
|
|
|
}
|
|
|
|
vstr_free(vstr);
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
// enter REPL
|
|
|
|
// REPL mode can change, or it can request a soft reset
|
|
|
|
for (;;) {
|
|
|
|
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
|
|
|
|
if (pyexec_raw_repl() != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pyexec_friendly_repl() != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-06 03:20:11 -05:00
|
|
|
|
|
|
|
printf("PYB: soft reboot\n");
|
|
|
|
|
|
|
|
// first_soft_reset = false;
|
|
|
|
goto soft_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stub out __libc_init_array. It's called by mk20dx128.c and is used to call
|
|
|
|
// global C++ constructors. Since this is a C-only projects, we don't need to
|
|
|
|
// call constructors.
|
|
|
|
void __libc_init_array(void) {
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
// ultoa is used by usb_init_serialnumber. Normally ultoa would be provided
|
|
|
|
// by nonstd.c from the teensy core, but it conflicts with some of the
|
|
|
|
// MicroPython functions in string0.c, so we provide ultoa here.
|
2014-01-06 03:20:11 -05:00
|
|
|
char * ultoa(unsigned long val, char *buf, int radix)
|
|
|
|
{
|
|
|
|
unsigned digit;
|
|
|
|
int i=0, j;
|
|
|
|
char t;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
digit = val % radix;
|
|
|
|
buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10);
|
|
|
|
val /= radix;
|
|
|
|
if (val == 0) break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
buf[i + 1] = 0;
|
|
|
|
for (j=0; j < i; j++, i--) {
|
|
|
|
t = buf[j];
|
|
|
|
buf[j] = buf[i];
|
|
|
|
buf[i] = t;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
2014-06-16 01:33:14 -04:00
|
|
|
|
|
|
|
STATIC NORETURN mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
|
|
|
|
int rc = 0;
|
|
|
|
if (n_args > 0) {
|
|
|
|
rc = mp_obj_get_int(args[0]);
|
|
|
|
}
|
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
|