2014-01-06 08:20:11 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-01-01 21:13:30 +00:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/parse.h"
|
|
|
|
#include "py/lexer.h"
|
|
|
|
#include "py/runtime.h"
|
2015-01-01 23:30:53 +00:00
|
|
|
#include "py/stackctrl.h"
|
2015-01-01 21:13:30 +00:00
|
|
|
#include "py/gc.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2014-06-16 06:33:14 +01:00
|
|
|
#include "gccollect.h"
|
2015-11-09 13:13:09 +00:00
|
|
|
#include "lib/utils/pyexec.h"
|
2014-06-16 06:33:14 +01:00
|
|
|
#include "readline.h"
|
2015-01-01 21:13:30 +00:00
|
|
|
#include "lexermemzip.h"
|
2014-06-16 06:33:14 +01:00
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2014-01-15 06:23:56 +00:00
|
|
|
#include "servo.h"
|
2014-01-06 08:20:11 +00:00
|
|
|
#include "led.h"
|
2014-08-03 05:28:32 +01:00
|
|
|
#include "uart.h"
|
2014-07-22 15:57:36 +01:00
|
|
|
#include "pin.h"
|
2014-01-06 08:20:11 +00:00
|
|
|
|
2014-06-16 06:33:14 +01:00
|
|
|
extern uint32_t _heap_start;
|
2014-01-06 08:20:11 +00:00
|
|
|
|
2014-01-08 09:00:22 +00: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 08:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 17:38:24 +01:00
|
|
|
void NORETURN __fatal_error(const char *msg) {
|
2014-06-16 06:33:14 +01: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);
|
2015-02-13 15:04:53 +00:00
|
|
|
mp_hal_stdout_tx_strn("\nFATAL ERROR:\n", 14);
|
|
|
|
mp_hal_stdout_tx_strn(msg, strlen(msg));
|
2014-06-16 06:33:14 +01:00
|
|
|
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 08:20:11 +00:00
|
|
|
|
2014-01-15 06:23:56 +00: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 06:33:14 +01:00
|
|
|
#if 0
|
2014-01-06 08:20:11 +00: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 06:33:14 +01: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 08:20:11 +00: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 06:33:14 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-06 08:20:11 +00:00
|
|
|
#define RAM_START (0x1FFF8000) // fixed for chip
|
|
|
|
#define HEAP_END (0x20006000) // tunable
|
|
|
|
#define RAM_END (0x20008000) // fixed for chip
|
|
|
|
|
2014-06-16 06:33:14 +01:00
|
|
|
#if 0
|
2014-01-06 08:20:11 +00:00
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
void gc_helper_get_regs_and_clean_stack(mp_uint_t *regs, mp_uint_t heap_end);
|
2014-01-06 08:20:11 +00: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 13:35:08 +01:00
|
|
|
digitalWrite(pin, mp_obj_is_true(args[1]));
|
2014-01-06 08:20:11 +00:00
|
|
|
return mp_const_none;
|
|
|
|
|
|
|
|
pin_error:
|
2014-06-16 06:33:14 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %d does not exist", pin));
|
2014-01-06 08:20:11 +00: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 06:33:14 +01: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 09:00:22 +00:00
|
|
|
|
|
|
|
mp_obj_t pyb_source_dir(mp_obj_t source_dir) {
|
2014-01-15 06:23:56 +00:00
|
|
|
if (MP_OBJ_IS_STR(source_dir)) {
|
|
|
|
pyb_config_source_dir = source_dir;
|
|
|
|
}
|
2014-01-08 09:00:22 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-06-16 06:33:14 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_source_dir_obj, pyb_source_dir);
|
|
|
|
|
2014-01-08 09:00:22 +00:00
|
|
|
mp_obj_t pyb_main(mp_obj_t main) {
|
2014-01-15 06:23:56 +00:00
|
|
|
if (MP_OBJ_IS_STR(main)) {
|
|
|
|
pyb_config_main = main;
|
|
|
|
}
|
2014-01-08 09:00:22 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-06-16 06:33:14 +01: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 08:20:11 +00: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 13:35:08 +01:00
|
|
|
led_state(PYB_LED_BUILTIN, mp_obj_is_true(state));
|
2014-01-06 08:20:11 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2014-06-16 06:33:14 +01:00
|
|
|
#endif // 0
|
2014-01-08 09:00:22 +00:00
|
|
|
|
2014-06-16 06:33:14 +01:00
|
|
|
#if 0
|
2014-01-06 08:20:11 +00: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 17:49:42 +00:00
|
|
|
int main(void) {
|
2014-09-28 18:17:44 +01:00
|
|
|
// TODO: Put this in a more common initialization function.
|
|
|
|
// Turn on STKALIGN which keeps the stack 8-byte aligned for interrupts
|
|
|
|
// (per EABI)
|
|
|
|
#define SCB_CCR_STKALIGN (1 << 9)
|
|
|
|
SCB_CCR |= SCB_CCR_STKALIGN;
|
2014-01-06 08:20:11 +00:00
|
|
|
|
2016-01-28 23:05:53 +00:00
|
|
|
mp_stack_ctrl_init();
|
2015-01-01 23:30:53 +00:00
|
|
|
mp_stack_set_limit(10240);
|
|
|
|
|
2014-09-28 18:17:44 +01:00
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
2014-01-06 08:20:11 +00:00
|
|
|
led_init();
|
|
|
|
|
|
|
|
// int first_soft_reset = true;
|
|
|
|
|
|
|
|
soft_reset:
|
|
|
|
|
2014-06-16 06:33:14 +01:00
|
|
|
led_state(PYB_LED_BUILTIN, 1);
|
|
|
|
|
2014-01-06 08:20:11 +00:00
|
|
|
// GC init
|
|
|
|
gc_init(&_heap_start, (void*)HEAP_END);
|
|
|
|
|
2014-08-04 10:05:16 +01:00
|
|
|
// Micro Python init
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_init();
|
2014-09-15 15:53:09 +01:00
|
|
|
mp_obj_list_init(mp_sys_path, 0);
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
|
|
|
|
mp_obj_list_init(mp_sys_argv, 0);
|
2014-01-06 08:20:11 +00:00
|
|
|
|
2014-08-04 17:27:29 +01:00
|
|
|
readline_init0();
|
2014-06-16 06:33:14 +01:00
|
|
|
|
2014-07-22 15:57:36 +01:00
|
|
|
pin_init0();
|
2014-06-16 06:33:14 +01:00
|
|
|
|
|
|
|
#if 0
|
2014-01-06 08:20:11 +00:00
|
|
|
// add some functions to the python namespace
|
|
|
|
{
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_store_name(MP_QSTR_help, mp_make_function_n(0, pyb_help));
|
2014-01-15 06:23:56 +00:00
|
|
|
mp_obj_t m = mp_obj_new_module(MP_QSTR_pyb);
|
2014-03-30 13:35:08 +01: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 06:33:14 +01:00
|
|
|
mp_store_attr(m, MP_QSTR_LED, (mp_obj_t)&pyb_led_type);
|
2014-03-30 13:35:08 +01: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 08:20:11 +00:00
|
|
|
}
|
2014-06-16 06:33:14 +01:00
|
|
|
#endif
|
2014-01-06 08:20:11 +00:00
|
|
|
|
2015-11-01 05:55:13 +00:00
|
|
|
#if MICROPY_MODULE_FROZEN
|
2016-08-20 05:09:52 +01:00
|
|
|
pyexec_frozen_module("boot.py");
|
2015-11-01 05:55:13 +00:00
|
|
|
#else
|
2014-06-16 06:33:14 +01:00
|
|
|
if (!pyexec_file("/boot.py")) {
|
2014-01-08 09:00:22 +00:00
|
|
|
flash_error(4);
|
|
|
|
}
|
2015-11-01 05:55:13 +00:00
|
|
|
#endif
|
2014-01-08 09:00:22 +00:00
|
|
|
|
2014-01-06 08:20:11 +00:00
|
|
|
// Turn bootup LED off
|
|
|
|
led_state(PYB_LED_BUILTIN, 0);
|
|
|
|
|
2014-01-08 09:00:22 +00:00
|
|
|
// run main script
|
2015-11-01 05:55:13 +00:00
|
|
|
#if MICROPY_MODULE_FROZEN
|
2016-08-20 05:09:52 +01:00
|
|
|
pyexec_frozen_module("main.py");
|
2015-11-01 05:55:13 +00:00
|
|
|
#else
|
2014-01-08 09:00:22 +00:00
|
|
|
{
|
|
|
|
vstr_t *vstr = vstr_new();
|
|
|
|
vstr_add_str(vstr, "/");
|
2014-01-15 06:23:56 +00:00
|
|
|
if (pyb_config_main == MP_OBJ_NULL) {
|
2014-01-08 09:00:22 +00:00
|
|
|
vstr_add_str(vstr, "main.py");
|
|
|
|
} else {
|
2014-01-15 06:23:56 +00:00
|
|
|
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
|
2014-01-08 09:00:22 +00:00
|
|
|
}
|
2015-01-29 13:57:23 +00:00
|
|
|
if (!pyexec_file(vstr_null_terminated_str(vstr))) {
|
2014-01-08 09:00:22 +00:00
|
|
|
flash_error(3);
|
|
|
|
}
|
|
|
|
vstr_free(vstr);
|
|
|
|
}
|
2015-11-01 05:55:13 +00:00
|
|
|
#endif
|
2014-01-08 09:00:22 +00:00
|
|
|
|
2014-06-16 06:33:14 +01: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 08:20:11 +00: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 06:33:14 +01: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 08:20:11 +00: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;
|
|
|
|
}
|