Commit 297446e7 authored by Dave Hylands's avatar Dave Hylands
Browse files

Initial support for Teensy 3.1

parent e03c0533
ifeq ($(ARDUINO),)
$(error Please define ARDUINO (where TeensyDuino is installed))
endif
TOOLS_PATH = $(ARDUINO)/hardware/tools
COMPILER_PATH = $(TOOLS_PATH)/arm-none-eabi/bin
CORE_PATH = $(ARDUINO)/hardware/teensy/cores/teensy3
PYSRC=../py
BUILD=build
AS = $(COMPILER_PATH)/arm-none-eabi-as
CC = $(COMPILER_PATH)/arm-none-eabi-gcc
LD = $(COMPILER_PATH)/arm-none-eabi-ld
OBJCOPY = $(COMPILER_PATH)/arm-none-eabi-objcopy
SIZE = $(COMPILER_PATH)/arm-none-eabi-size
CFLAGS_TEENSY = -DF_CPU=96000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH -D__MK20DX256__
CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -fsingle-precision-constant -Wdouble-promotion $(CFLAGS_TEENSY)
CFLAGS = -I. -I$(PYSRC) -I$(CORE_PATH) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4)
LDFLAGS = -nostdlib -T mk20dx256.ld
LIBS = -L $(COMPILER_PATH)/../lib/gcc/arm-none-eabi/4.7.2/thumb2 -lgcc
SRC_C = \
main.c \
lexerteensy.c \
led.c \
malloc0.c \
printf.c \
string0.c \
usb.c \
SRC_S = \
gchelper.s \
PY_O = \
nlrthumb.o \
gc.o \
malloc.o \
qstr.o \
vstr.o \
unicode.o \
lexer.o \
parse.o \
scope.o \
compile.o \
emitcommon.o \
emitpass1.o \
emitbc.o \
asmthumb.o \
emitnthumb.o \
emitinlinethumb.o \
runtime.o \
map.o \
obj.o \
objbool.o \
objboundmeth.o \
objcell.o \
objclass.o \
objclosure.o \
objcomplex.o \
objdict.o \
objexcept.o \
objfloat.o \
objfun.o \
objgenerator.o \
objinstance.o \
objint.o \
objlist.o \
objmodule.o \
objnone.o \
objrange.o \
objset.o \
objslice.o \
objstr.o \
objtuple.o \
objtype.o \
builtin.o \
builtinimport.o \
vm.o \
showbc.o \
repl.o \
SRC_TEENSY = \
mk20dx128.c \
pins_teensy.c \
analog.c \
usb_desc.c \
usb_dev.c \
usb_mem.c \
usb_serial.c \
yield.c \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(PY_O) $(SRC_TEENSY:.c=.o))
#LIB = -lreadline
# the following is needed for BSD
#LIB += -ltermcap
PROG = micropython
all: hex
hex: $(PROG).hex
post_compile: $(PROG).hex
$(TOOLS_PATH)/teensy_post_compile -file="$(basename $<)" -path="$(CURDIR)" -tools="$(TOOLS_PATH)"
reboot:
-$(TOOLS_PATH)/teensy_reboot
upload: post_compile reboot
$(PROG).elf: $(BUILD) $(OBJ)
$(CC) $(LDFLAGS) -o "$@" -Wl,-Map,$(PROG).map $(OBJ) $(LIBS)
%.hex: %.elf
$(SIZE) "$<"
$(OBJCOPY) -O ihex -R .eeprom "$<" "$@"
$(BUILD):
mkdir -p $@
$(BUILD)/%.o: %.s
$(AS) -o $@ $<
$(BUILD)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.S
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(CORE_PATH)/%.c
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
# optimising gc for speed; 5ms down to 4ms
$(BUILD)/gc.o: $(PYSRC)/gc.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
$(BUILD)/vm.o: $(PYSRC)/vm.c
$(CC) $(CFLAGS) -O3 -c -o $@ $<
$(BUILD)/main.o: mpconfigport.h
$(BUILD)/parse.o: $(PYSRC)/grammar.h
$(BUILD)/compile.o: $(PYSRC)/grammar.h
$(BUILD)/emitbc.o: $(PYSRC)/emit.h
clean:
/bin/rm -rf $(BUILD)
/bin/rm -f $(PROG).elf $(PROG).hex $(PROG).map
.PHONY: clean
Build Instructions for Teensy 3.1
This assumes that you have TeensyDuino installed and set the ARUINO environment
variable pointing to the where Arduino with TeensyDuino is installed.
```
cd teensy
ARDUINO=~/arduino-1.0.5 make
```
To build the loader
```
cd teensy/loader
make
```
To upload micropython to the Teensy 3.1.
Press the Program button on the Teensy 3.1
```
make upload
```
Currently, the python prompt is through the USB serial interface.
The LED will blink (100 msec on/100 msec off) while waiting for the USB Serial
device to be configured, and will blink (200 msec on/200 msec off) while
sitting at the readline prompt.
Currently, there is no I/O support configured (no GPIO, ADC, etc).
.syntax unified
.cpu cortex-m4
.thumb
.text
.align 2
@ void gc_helper_get_regs_and_clean_stack(r0=uint regs[10], r1=heap_end)
.global gc_helper_get_regs_and_clean_stack
.thumb
.thumb_func
.type gc_helper_get_regs_and_clean_stack, %function
gc_helper_get_regs_and_clean_stack:
@ store registers into given array
str r4, [r0], #4
str r5, [r0], #4
str r6, [r0], #4
str r7, [r0], #4
str r8, [r0], #4
str r9, [r0], #4
str r10, [r0], #4
str r11, [r0], #4
str r12, [r0], #4
str r13, [r0], #4
@ clean the stack from given pointer up to current sp
movs r0, #0
mov r2, sp
b.n .entry
.loop:
str r0, [r1], #4
.entry:
cmp r1, r2
bcc.n .loop
bx lr
.size gc_helper_get_regs_and_clean_stack, .-gc_helper_get_regs_and_clean_stack
#include <stdio.h>
#include "misc.h"
#include "mpconfig.h"
#include "obj.h"
#include "led.h"
#include "Arduino.h"
void led_init(void) {
}
void led_state(pyb_led_t led, int state) {
uint8_t pin;
if (led == 0) {
pin = LED_BUILTIN;
} else {
return;
}
digitalWrite(pin, state);
}
void led_toggle(pyb_led_t led) {
uint8_t pin;
if (led == 0) {
pin = LED_BUILTIN;
} else {
return;
}
digitalWrite(pin, !digitalRead(pin));
}
/******************************************************************************/
/* Micro Python bindings */
typedef struct _pyb_led_obj_t {
mp_obj_base_t base;
uint led_id;
} pyb_led_obj_t;
void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
pyb_led_obj_t *self = self_in;
print(env, "<LED %lu>", self->led_id);
}
mp_obj_t led_obj_on(mp_obj_t self_in) {
pyb_led_obj_t *self = self_in;
led_state(self->led_id, 1);
return mp_const_none;
}
mp_obj_t led_obj_off(mp_obj_t self_in) {
pyb_led_obj_t *self = self_in;
led_state(self->led_id, 0);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);
static const mp_obj_type_t led_obj_type = {
{ &mp_const_type },
"Led",
led_obj_print, // print
NULL, // make_new
NULL, // call_n
NULL, // unary_op
NULL, // binary_op
NULL, // getiter
NULL, // iternext
{ // method list
{ "on", &led_obj_on_obj },
{ "off", &led_obj_off_obj },
{ NULL, NULL },
}
};
mp_obj_t pyb_Led(mp_obj_t led_id) {
pyb_led_obj_t *o = m_new_obj(pyb_led_obj_t);
o->base.type = &led_obj_type;
o->led_id = mp_obj_get_int(led_id);
return o;
}
typedef enum {
PYB_LED_BUILTIN = 0,
} pyb_led_t;
void led_init(void);
void led_state(pyb_led_t led, int state);
void led_toggle(pyb_led_t led);
mp_obj_t pyb_Led(mp_obj_t led_id);
#include <stdint.h>
#include <stdio.h>
#include "misc.h"
#include "lexer.h"
#include "lexerteensy.h"
unichar str_buf_next_char(mp_lexer_str_buf_t *sb) {
if (sb->src_cur < sb->src_end) {
return *sb->src_cur++;
} else {
return MP_LEXER_CHAR_EOF;
}
}
void str_buf_free(mp_lexer_str_buf_t *sb) {
if (sb->free) {
m_del(char, (char*)sb->src_beg, 0 /* don't know allocated size of src */);
}
}
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, bool free_str, mp_lexer_str_buf_t *sb) {
sb->free = free_str;
sb->src_beg = str;
sb->src_cur = str;
sb->src_end = str + len;
return mp_lexer_new(src_name, sb, (mp_lexer_stream_next_char_t)str_buf_next_char, (mp_lexer_stream_close_t)str_buf_free);
}
mp_lexer_t *mp_import_open_file(qstr mod_name) {
printf("import not implemented\n");
return NULL;
}
typedef struct _py_lexer_str_buf_t {
bool free; // free src_beg when done
const char *src_beg; // beginning of source
const char *src_cur; // current location in source
const char *src_end; // end (exclusive) of source
} mp_lexer_str_buf_t;
mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uint len, bool free_str, mp_lexer_str_buf_t *sb);
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "mpqstr.h"
#include "lexer.h"
#include "lexerteensy.h"
#include "parse.h"
#include "obj.h"
#include "compile.h"
#include "runtime0.h"
#include "runtime.h"
#include "repl.h"
#include "usb.h"
#include "gc.h"
#include "led.h"
#include "Arduino.h"
extern uint32_t _heap_start;
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#if 0
static char *str_join(const char *s1, int sep_char, const char *s2) {
int l1 = strlen(s1);
int l2 = strlen(s2);
char *s = m_new(char, l1 + l2 + 2);
memcpy(s, s1, l1);
if (sep_char != 0) {
s[l1] = sep_char;
l1 += 1;
}
memcpy(s + l1, s2, l2);
s[l1 + l2] = 0;
return s;
}
static char *prompt(char *p) {
#ifdef USE_READLINE
char *line = readline(p);
if (line) {
add_history(line);
}
#else
static char buf[256];
fputs(p, stdout);
char *s = fgets(buf, sizeof(buf), stdin);
if (!s) {
return NULL;
}
int l = strlen(buf);
if (buf[l - 1] == '\n') {
buf[l - 1] = 0;
} else {
l++;
}
char *line = m_new(char, l);
memcpy(line, buf, l);
#endif
return line;
}
#endif
static const char *help_text =
"Welcome to Micro Python!\n\n"
"This is a *very* early version of Micro Python and has minimal functionality.\n\n"
"Specific commands for the board:\n"
" pyb.info() -- print some general information\n"
" pyb.gc() -- run the garbage collector\n"
" pyb.delay(<n>) -- wait for n milliseconds\n"
" pyb.Led(<n>) -- create Led object for LED n (n=0)\n"
" Led methods: on(), off()\n"
" pyb.gpio(<pin>) -- read gpio pin\n"
" pyb.gpio(<pin>, <val>) -- set gpio pin\n"
#if 0
" pyb.Servo(<n>) -- create Servo object for servo n (n=1,2,3,4)\n"
" Servo methods: angle(<x>)\n"
" pyb.switch() -- return True/False if switch pressed or not\n"
" pyb.accel() -- get accelerometer values\n"
" pyb.rand() -- get a 16-bit random number\n"
#endif
;
// get some help about available functions
static mp_obj_t pyb_help(void) {
printf("%s", help_text);
return mp_const_none;
}
// 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
{
extern void *_sdata;
extern void *_edata;
extern void *_sbss;
extern void *_ebss;
extern void *_estack;
extern void *_etext;
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");
printf(" %lu total\n", info.total);
printf(" %lu used %lu free\n", info.used, info.free);
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
}
#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;
}
#define RAM_START (0x1FFF8000) // fixed for chip
#define HEAP_END (0x20006000) // tunable
#define RAM_END (0x20008000) // fixed for chip
void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
void gc_collect(void) {
uint32_t start = micros();
gc_collect_start();
gc_collect_root((void**)RAM_START, (((uint32_t)&_heap_start) - RAM_START) / 4);
machine_uint_t regs[10];
gc_helper_get_regs_and_clean_stack(regs, HEAP_END);
gc_collect_root((void**)HEAP_END, (RAM_END - HEAP_END) / 4); // will trace regs since they now live in this function on the stack
gc_collect_end();
uint32_t ticks = micros() - start; // TODO implement a function that does this properly
if (0) {
// print GC info
gc_info_t info;
gc_info(&info);
printf("GC@%lu %luus\n", start, ticks);
printf(" %lu total\n", info.total);
printf(" %lu used %lu free\n", info.used, info.free);
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
}
}
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);
digitalWrite(pin, rt_is_true(args[1]));
return mp_const_none;
pin_error:
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_ValueError, "pin %d does not exist", (void *)(machine_uint_t)pin));
}
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
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) {
led_state(PYB_LED_BUILTIN, rt_is_true(state));
return state;
}
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;
}
#define READLINE_HIST_SIZE (8)
static const char *readline_hist[READLINE_HIST_SIZE] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
void stdout_tx_str(const char *str) {
// usart_tx_str(str);
usb_vcp_send_str(str);
}
int readline(vstr_t *line, const char *prompt) {
stdout_tx_str(prompt);
int len = vstr_len(line);
int escape = 0;
int hist_num = 0;
for (;;) {
char c;
for (;;) {
if (usb_vcp_rx_any() != 0) {
c = usb_vcp_rx_get();
break;
#if 0
} else if (usart_rx_any()) {
c = usart_rx_char();
break;
#endif
}
//delay(1);
//if (storage_needs_flush()) {
// storage_flush();
//}
}