Commit 9b7b947b authored by Dave Hylands's avatar Dave Hylands
Browse files

Updated teensy to use common code from stm directory.

Updated mconfigport.h to enable GC
parent 6d6bc9ef
......@@ -30,7 +30,8 @@ SRC_C = \
string0.c \
malloc0.c \
systick.c \
lexerstm.c \
lexerstr.c \
lexerfatfs.c \
led.c \
lcd.c \
servo.c \
......
......@@ -5,29 +5,7 @@
#include "misc.h"
#include "lexer.h"
#include "lexerstm.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);
}
#include "lexerfatfs.h"
unichar file_buf_next_char(mp_lexer_file_buf_t *fb) {
if (fb->pos >= fb->len) {
......
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;
typedef struct _py_lexer_file_buf_t {
FIL fp;
char buf[20];
......@@ -12,5 +5,4 @@ typedef struct _py_lexer_file_buf_t {
uint16_t pos;
} mp_lexer_file_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);
mp_lexer_t *mp_lexer_new_from_file(const char *filename, mp_lexer_file_buf_t *fb);
......@@ -3,7 +3,7 @@
#include "misc.h"
#include "lexer.h"
#include "lexerteensy.h"
#include "lexerstr.h"
unichar str_buf_next_char(mp_lexer_str_buf_t *sb) {
if (sb->src_cur < sb->src_end) {
......@@ -26,8 +26,3 @@ mp_lexer_t *mp_lexer_new_from_str_len(const char *src_name, const char *str, uin
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;
}
......@@ -19,7 +19,8 @@
#include "nlr.h"
#include "misc.h"
#include "lexer.h"
#include "lexerstm.h"
#include "lexerstr.h"
#include "lexerfatfs.h"
#include "parse.h"
#include "obj.h"
#include "compile.h"
......
......@@ -22,14 +22,19 @@ LIBS = -L $(COMPILER_PATH)/../lib/gcc/arm-none-eabi/4.7.2/thumb2 -lgcc
SRC_C = \
main.c \
lexerteensy.c \
lcd.c \
led.c \
lexerfatfs.c \
usart.c \
usb.c \
STM_SRC_C = \
lexerstr.c \
malloc0.c \
printf.c \
string0.c \
usb.c \
SRC_S = \
STM_SRC_S = \
gchelper.s \
PY_O = \
......@@ -90,7 +95,10 @@ SRC_TEENSY = \
usb_serial.c \
yield.c \
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(PY_O) $(SRC_TEENSY:.c=.o))
STM_SRC_C_OBJ = $(STM_SRC_C:.c=.o)
STM_SRC_S_OBJ = $(STM_SRC_S:.s=.o)
OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C_OBJ) $(STM_SRC_S_OBJ) $(PY_O) $(SRC_TEENSY:.c=.o))
#LIB = -lreadline
# the following is needed for BSD
#LIB += -ltermcap
......@@ -117,10 +125,13 @@ $(PROG).elf: $(BUILD) $(OBJ)
$(BUILD):
mkdir -p $@
$(BUILD)/%.o: %.s
$(BUILD)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: ../stm/%.s
$(AS) -o $@ $<
$(BUILD)/%.o: %.c
$(BUILD)/%.o: ../stm/%.c
$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: $(PYSRC)/%.S
......
......@@ -8,13 +8,6 @@ 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
......@@ -23,9 +16,3 @@ 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 "misc.h"
#include "../stm/lcd.h"
void lcd_init(void) {
}
void lcd_print_str(const char *str) {
(void)str;
}
void lcd_print_strn(const char *str, unsigned int len) {
(void)str;
(void)len;
}
......@@ -61,21 +61,17 @@ mp_obj_t led_obj_off(mp_obj_t self_in) {
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_method_t led_methods[] = {
{ "on", &led_obj_on_obj },
{ "off", &led_obj_off_obj },
{ NULL, NULL },
};
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 },
}
.print = led_obj_print,
.methods = led_methods,
};
mp_obj_t pyb_Led(mp_obj_t led_id) {
......
#include <stdint.h>
#include <stdio.h>
#include "misc.h"
#include "lexer.h"
typedef int FIL;
#include "../stm/lexerfatfs.h"
mp_lexer_t *mp_lexer_new_from_file(const char *filename, mp_lexer_file_buf_t *fb) {
printf("import not implemented\n");
return NULL;
}
mp_lexer_t *mp_import_open_file(qstr mod_name) {
printf("import not implemented\n");
return NULL;
}
......@@ -8,7 +8,7 @@
#include "mpconfig.h"
#include "mpqstr.h"
#include "lexer.h"
#include "lexerteensy.h"
#include "../stm/lexerstr.h"
#include "parse.h"
#include "obj.h"
#include "compile.h"
......@@ -378,7 +378,7 @@ void do_repl(void) {
stdout_tx_str("\r\n");
}
void main(void) {
int main(void) {
pinMode(LED_BUILTIN, OUTPUT);
// Wait for host side to get connected
while (!usb_vcp_is_connected()) {
......
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Arduino.h"
extern "C"
{
#include "nlr.h"
#include "misc.h"
#include "mpconfig.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"
}
#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
#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);
}
static elapsedMillis ledTime;
static uint8_t ledState;
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;
ledState = 1;
ledTime = 0;
digitalWrite(LED_BUILTIN, ledState);
for (;;) {
if (ledTime > 200) {
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
ledTime = 0;
}
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();
//}
}
if (escape == 0) {
if (c == 4 && vstr_len(line) == len) {
return 0;
} else if (c == '\r') {
stdout_tx_str("\r\n");
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) {
readline_hist[i] = readline_hist[i - 1];
}
readline_hist[0] = strdup(vstr_str(line));
return 1;
} else if (c == 27) {
escape = true;
} else if (c == 127) {
if (vstr_len(line) > len) {
vstr_cut_tail(line, 1);
stdout_tx_str("\b \b");
}
} else if (32 <= c && c <= 126) {
vstr_add_char(line, c);
stdout_tx_str(line->buf + line->len - 1);
}
} else if (escape == 1) {
if (c == '[') {
escape = 2;
} else {
escape = 0;
}
} else if (escape == 2) {
escape = 0;
if (c == 'A') {
// up arrow
if (hist_num < READLINE_HIST_SIZE && readline_hist[hist_num] != NULL) {
// erase line
for (int i = line->len - len; i > 0; i--) {
stdout_tx_str("\b \b");
}
// set line to history
line->len = len;
vstr_add_str(line, readline_hist[hist_num]);
// draw line
stdout_tx_str(readline_hist[hist_num]);
// increase hist num
hist_num += 1;
}
}
} else {
escape = 0;
}
delay(10);
}
}
void setup(void) {
pinMode(LED_BUILTIN, OUTPUT);
ledState = 1;
digitalWrite(LED_BUILTIN, ledState);
ledTime = 0;
// Wait for host side to get connected
while (!usb_vcp_is_connected()) {
if (ledTime > 100) {
ledState = !ledState;
digitalWrite(LED_BUILTIN, ledState);
ledTime = 0;
}
}
digitalWrite(LED_BUILTIN, 0);
qstr_init();
rt_init();
stdout_tx_str("Micro Python for Teensy 3.1\r\n");
stdout_tx_str("Type \"help()\" for more information.\r\n");
}
void loop(void) {
vstr_t line;
vstr_init(&line);
vstr_reset(&line);
int ret = readline(&line, ">>> ");
if (ret == 0) {
// EOF
return;
}
if (vstr_len(&line) == 0) {
return;
}
if (mp_repl_is_compound_stmt(vstr_str(&line))) {
for (;;) {
vstr_add_char(&line, '\n');
int len = vstr_len(&line);
int ret = readline(&line, "... ");
if (ret == 0 || vstr_len(&line) == len) {
// done entering compound statement
break;
}
}
}
mp_lexer_str_buf_t sb;
mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", vstr_str(&line), vstr_len(&line), false, &sb);
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT);
mp_lexer_free(lex);
if (pn != MP_PARSE_NODE_NULL) {
mp_obj_t module_fun = mp_compile(pn, true);
if (module_fun != mp_const_none) {
nlr_buf_t nlr;
uint32_t start = micros();
if (nlr_push(&nlr) == 0) {
rt_call_function_0(module_fun);
nlr_pop();
// optional timing
if (0) {
uint32_t ticks = micros() - start; // TODO implement a function that does this properly
printf("(took %lu us)\n", ticks);
}
} else {
// uncaught exception
mp_obj_print((mp_obj_t)nlr.ret_val);
printf("\r\n");
}
}
}
}
// for sqrt
#include <math.h>
machine_float_t machine_sqrt(machine_float_t x) {
return sqrt(x);
}
#include <stdint.h>
#include "std.h"
#include "mpconfig.h"
#include "gc.h"
#if 0
static uint32_t mem = 0;
void *malloc(size_t n) {
if (mem == 0) {
extern uint32_t _heap_start;
mem = (uint32_t)&_heap_start; // need to use big ram block so we can execute code from it (is it true that we can't execute from CCM?)
}
void *ptr = (void*)mem;
mem = (mem + n + 3) & (~3);
if (mem > 0x20000000 + 0x18000) {
void __fatal_error(const char*);
__fatal_error("out of memory");
}
return ptr;
}
void free(void *ptr) {
}
void *realloc(void *ptr, size_t n) {
return malloc(n);
}
#endif
void *calloc(size_t sz, size_t n) {
char *ptr = malloc(sz * n);
for (int i = 0; i < sz * n; i++) {
ptr[i] = 0;
}
return ptr;
}
void *malloc(size_t n) {
void *m = gc_alloc(n);
return m;
}
void free(void *ptr) {
gc_free(ptr);
}
void *realloc(void *ptr, size_t n) {
return gc_realloc(ptr, n);
}
void __assert_func(void) {
printf("\nASSERT FAIL!");
for (;;) {
}
}
......@@ -2,11 +2,11 @@
// options to control how Micro Python is built
#define MICROPY_ENABLE_FLOAT (1)
#define MICROPY_EMIT_CPYTHON (0)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (1)
#define MICROPY_EMIT_INLINE_THUMB (1)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_ENABLE_REPL_HELPERS (1)
#define MICROPY_ENABLE_FLOAT (1)
// type definitions for the specific machine
......
#include <stdint.h>
#include <stdarg.h>
#include "std.h"
#include "misc.h"
//#include "lcd.h"
//#include "usart.h"
#include "usb.h"
#define PF_FLAG_LEFT_ADJUST (0x01)
#define PF_FLAG_SHOW_SIGN (0x02)
#define PF_FLAG_SPACE_SIGN (0x04)
#define PF_FLAG_NO_TRAILZ (0x08)
#define PF_FLAG_ZERO_PAD (0x10)
// tricky; we compute pad string by: pad_chars + (flags & PF_FLAG_ZERO_PAD)
#define PF_PAD_SIZE PF_FLAG_ZERO_PAD
static const char *pad_chars = " 0000000000000000";
typedef struct _pfenv_t {
void *data;
void (*print_strn)(void *, const char *str, unsigned int len);
} pfenv_t;
static void print_str_dummy(void *data, const char *str, unsigned int len) {
}
const pfenv_t pfenv_dummy = {0, print_str_dummy};