atmel-samd: Support raw repl and soft reset to support ampy.

Closes #1. Also adds TX and RX led support on the Arduino Zero.
This commit is contained in:
Scott Shawcroft 2016-09-15 17:01:19 -07:00
parent 7d8929c470
commit 7fd84e93f4
6 changed files with 108 additions and 66 deletions

View File

@ -150,6 +150,7 @@ SRC_C = \
asf/sam0/utils/cmsis/samd21/source/gcc/startup_samd21.c \ asf/sam0/utils/cmsis/samd21/source/gcc/startup_samd21.c \
asf/sam0/utils/cmsis/samd21/source/system_samd21.c \ asf/sam0/utils/cmsis/samd21/source/system_samd21.c \
asf/sam0/utils/syscalls/gcc/syscalls.c \ asf/sam0/utils/syscalls/gcc/syscalls.c \
boards/$(BOARD)/init.c \
boards/$(BOARD)/pins.c \ boards/$(BOARD)/pins.c \
lib/fatfs/ff.c \ lib/fatfs/ff.c \
lib/fatfs/option/ccsbcs.c \ lib/fatfs/option/ccsbcs.c \

View File

@ -8,14 +8,24 @@
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/ */
#include <asf.h> #include "board.h"
#include <board.h> #include "conf_board.h"
#include <conf_board.h> #include "mpconfigboard.h"
#include "asf/sam0/drivers/port/port.h"
void board_init(void) void board_init(void)
{ {
/* This function is meant to contain board-specific initialization code /* This function is meant to contain board-specific initialization code
* for, e.g., the I/O pins. The initialization can rely on application- * for, e.g., the I/O pins. The initialization can rely on application-
* specific board configuration, found in conf_board.h. * specific board configuration, found in conf_board.h.
*/ */
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(MICROPY_HW_LED_TX, &pin_conf);
port_pin_set_output_level(MICROPY_HW_LED_TX, true);
port_pin_set_config(MICROPY_HW_LED_RX, &pin_conf);
port_pin_set_output_level(MICROPY_HW_LED_RX, true);
} }

View File

@ -5,3 +5,6 @@
#define MICROPY_HW_BOARD_NAME "Arduino Zero" #define MICROPY_HW_BOARD_NAME "Arduino Zero"
#define MICROPY_HW_MCU_NAME "samd21g18" #define MICROPY_HW_MCU_NAME "samd21g18"
#define MICROPY_HW_LED_TX PIN_PA27
#define MICROPY_HW_LED_RX PIN_PB03

View File

@ -8,9 +8,8 @@
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/ */
#include <asf.h> #include "board.h"
#include <board.h> #include "conf_board.h"
#include <conf_board.h>
void board_init(void) void board_init(void)
{ {

View File

@ -19,6 +19,7 @@
#include "asf/sam0/drivers/port/port.h" #include "asf/sam0/drivers/port/port.h"
#include "asf/sam0/drivers/sercom/usart/usart.h" #include "asf/sam0/drivers/sercom/usart/usart.h"
#include "asf/sam0/drivers/system/system.h" #include "asf/sam0/drivers/system/system.h"
#include <board.h>
#include "mpconfigboard.h" #include "mpconfigboard.h"
#include "modmachine_pin.h" #include "modmachine_pin.h"
@ -158,16 +159,7 @@ void init_flash_fs() {
static char *stack_top; static char *stack_top;
static char heap[8192]; static char heap[8192];
int main(int argc, char **argv) { void reset_mp() {
// initialise the cpu and peripherals
#if MICROPY_MIN_USE_SAMD21_MCU
void samd21_init(void);
samd21_init();
#endif
int stack_dummy;
stack_top = (char*)&stack_dummy;
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
gc_init(heap, heap + sizeof(heap)); gc_init(heap, heap + sizeof(heap));
#endif #endif
@ -176,11 +168,22 @@ int main(int argc, char **argv) {
MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt); MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
pin_init0(); pin_init0();
}
int main(int argc, char **argv) {
// initialise the cpu and peripherals
#if MICROPY_MIN_USE_SAMD21_MCU
void samd21_init(void);
samd21_init();
#endif
// Initialise the local flash filesystem. // Initialise the local flash filesystem.
// Create it if needed, mount in on /flash, and set it as current dir. // Create it if needed, mount in on /flash, and set it as current dir.
init_flash_fs(); init_flash_fs();
int stack_dummy;
reset_mp();
#if MICROPY_REPL_EVENT_DRIVEN #if MICROPY_REPL_EVENT_DRIVEN
pyexec_event_repl_init(); pyexec_event_repl_init();
for (;;) { for (;;) {
@ -190,10 +193,24 @@ int main(int argc, char **argv) {
} }
} }
#else #else
pyexec_friendly_repl(); // Main script is finished, so now go into REPL mode.
// The REPL mode can change, or it can request a soft reset.
int exit_code = 0;
for (;;) {
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
exit_code = pyexec_raw_repl();
} else {
exit_code = pyexec_friendly_repl();
}
if (exit_code == PYEXEC_FORCED_EXIT) {
mp_hal_stdout_tx_str("soft reboot\r\n");
stack_top = (char*)&stack_dummy;
reset_mp();
} else if (exit_code != 0) {
break;
}
}
#endif #endif
//do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
//do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
mp_deinit(); mp_deinit();
return 0; return 0;
} }
@ -239,33 +256,34 @@ void MP_WEAK __assert_func(const char *file, int line, const char *func, const c
struct usart_module usart_instance; struct usart_module usart_instance;
void samd21_init(void) { void samd21_init(void) {
irq_initialize_vectors();
cpu_irq_enable();
irq_initialize_vectors(); // Initialize the sleep manager
cpu_irq_enable(); sleepmgr_init();
// Initialize the sleep manager system_init();
sleepmgr_init();
system_init(); delay_init();
delay_init(); board_init();
// Uncomment to init PIN_PA17 for debugging. // Uncomment to init PIN_PA17 for debugging.
// struct port_config pin_conf; // struct port_config pin_conf;
// port_get_config_defaults(&pin_conf); // port_get_config_defaults(&pin_conf);
// //
// pin_conf.direction = PORT_PIN_DIR_OUTPUT; // pin_conf.direction = PORT_PIN_DIR_OUTPUT;
// port_pin_set_config(MICROPY_HW_LED1, &pin_conf); // port_pin_set_config(MICROPY_HW_LED1, &pin_conf);
// port_pin_set_output_level(MICROPY_HW_LED1, false); // port_pin_set_output_level(MICROPY_HW_LED1, false);
#ifdef USB_REPL #ifdef USB_REPL
udc_start(); udc_start();
#endif #endif
// TODO(tannewt): Switch to proper pyb based UARTs. // TODO(tannewt): Switch to proper pyb based UARTs.
#ifdef UART_REPL #ifdef UART_REPL
configure_usart(); configure_usart();
#endif #endif
} }
#endif #endif

View File

@ -2,6 +2,7 @@
#include "asf/common/services/usb/class/cdc/device/udi_cdc.h" #include "asf/common/services/usb/class/cdc/device/udi_cdc.h"
#include "asf/common2/services/delay/delay.h" #include "asf/common2/services/delay/delay.h"
#include "asf/sam0/drivers/port/port.h"
#include "asf/sam0/drivers/sercom/usart/usart.h" #include "asf/sam0/drivers/sercom/usart/usart.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "py/mpstate.h" #include "py/mpstate.h"
@ -104,34 +105,44 @@ int receive_usb() {
} }
int mp_hal_stdin_rx_chr(void) { int mp_hal_stdin_rx_chr(void) {
for (;;) { for (;;) {
#ifdef USB_REPL #ifdef USB_REPL
if (mp_cdc_enabled && usb_rx_count > 0) { if (mp_cdc_enabled && usb_rx_count > 0) {
return receive_usb(); #ifdef MICROPY_HW_LED_RX
port_pin_toggle_output_level(MICROPY_HW_LED_RX);
#endif
return receive_usb();
}
#endif
#ifdef UART_REPL
uint16_t temp;
if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
#ifdef MICROPY_HW_LED_RX
port_pin_toggle_output_level(MICROPY_HW_LED_RX);
#endif
return temp;
}
#endif
// TODO(tannewt): Figure out how we can sleep while waiting for input and
// add it here. The current UART implementation doesn't cause a wake.
//__WFI();
} }
#endif
#ifdef UART_REPL
uint16_t temp;
if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {
return temp;
}
#endif
// TODO(tannewt): Figure out how we can sleep while waiting for input and
// add it here. The current UART implementation doesn't cause a wake.
//__WFI();
}
} }
void mp_hal_stdout_tx_strn(const char *str, size_t len) { void mp_hal_stdout_tx_strn(const char *str, size_t len) {
#ifdef UART_REPL #ifdef MICROPY_HW_LED_TX
usart_write_buffer_wait(&usart_instance, (uint8_t*) str, len); port_pin_toggle_output_level(MICROPY_HW_LED_TX);
#endif #endif
#ifdef USB_REPL #ifdef UART_REPL
if (mp_cdc_enabled && udi_cdc_is_tx_ready()) { usart_write_buffer_wait(&usart_instance, (uint8_t*) str, len);
udi_cdc_write_buf(str, len); #endif
}
#endif #ifdef USB_REPL
if (mp_cdc_enabled && udi_cdc_is_tx_ready()) {
udi_cdc_write_buf(str, len);
}
#endif
} }
void mp_hal_set_interrupt_char(int c) { void mp_hal_set_interrupt_char(int c) {