From 258804ab16b96035eeb99c8caee6055781083007 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 24 Aug 2016 13:17:55 -0700 Subject: [PATCH] atmel-samd: Add limited time module support. --- atmel-samd/Makefile | 10 ++++- atmel-samd/modutime.c | 81 +++++++++++++++++++++++++++++++++++++++ atmel-samd/mpconfigport.h | 16 ++++++-- atmel-samd/mphalport.c | 31 ++++----------- 4 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 atmel-samd/modutime.c diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 4069e50206..015caece85 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -109,6 +109,7 @@ SRC_ASF = $(addprefix asf/sam0/,\ SRC_C = \ main.c \ + modutime.c \ mphalport.c \ uart.c \ asf/common/services/sleepmgr/samd/sleepmgr.c \ @@ -124,11 +125,16 @@ SRC_C = \ lib/utils/printf.c \ lib/utils/pyexec.c \ lib/libc/string0.c \ - lib/mp-readline/readline.c \ - $(BUILD)/_frozen_mpy.c \ + lib/mp-readline/readline.c + +SRC_AUTOGEN = \ + $(BUILD)/_frozen_mpy.c \ OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_AUTOGEN:.c=.o)) + +SRC_QSTR += $(SRC_C) ifeq ($(CROSS), 1) all: $(BUILD)/firmware.bin diff --git a/atmel-samd/modutime.c b/atmel-samd/modutime.c new file mode 100644 index 0000000000..33f8fadc6e --- /dev/null +++ b/atmel-samd/modutime.c @@ -0,0 +1,81 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2015 Josef Gajdusek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +#include "py/nlr.h" +#include "py/obj.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/smallint.h" + +/// \module time - time related functions +/// +/// The `time` module provides functions for getting the current time and date, +/// and for sleeping. + +/// \function sleep(seconds) +/// Sleep for the given number of seconds. +STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { + #if MICROPY_PY_BUILTINS_FLOAT + mp_hal_delay_ms(1000 * mp_obj_get_float(seconds_o)); + #else + mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o)); + #endif + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); + +STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) { + mp_hal_delay_ms(mp_obj_get_int(arg)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms); + +STATIC mp_obj_t time_sleep_us(mp_obj_t arg) { + mp_hal_delay_us(mp_obj_get_int(arg)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us); + +STATIC const mp_map_elem_t time_module_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); + +const mp_obj_module_t utime_module = { + .base = { &mp_type_module }, + .name = MP_QSTR_utime, + .globals = (mp_obj_dict_t*)&time_module_globals, +}; diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index f989ff256f..44bd57048b 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -53,6 +53,8 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) +#define MICROPY_MODULE_WEAK_LINKS (1) + // type definitions for the specific machine #define BYTES_PER_WORD (4) @@ -72,10 +74,16 @@ typedef long mp_off_t; #define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) -// extra built in names to add to the global namespace -extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj; -#define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, +// extra built in modules to add to the list of known ones +extern const struct _mp_obj_module_t machine_module; +extern const struct _mp_obj_module_t utime_module; + + //{ MP_OBJ_NEW_QSTR(MP_QSTR_umachine), (mp_obj_t)&machine_module }, +#define MICROPY_PORT_BUILTIN_MODULES \ + { MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module } \ + +#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ + { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&utime_module } \ // board specific definitions diff --git a/atmel-samd/mphalport.c b/atmel-samd/mphalport.c index 5f21eda01f..90ecbeef3c 100644 --- a/atmel-samd/mphalport.c +++ b/atmel-samd/mphalport.c @@ -1,14 +1,13 @@ #include #include "asf/common/services/usb/class/cdc/device/udi_cdc.h" +#include "asf/common2/services/delay/delay.h" #include "asf/sam0/drivers/sercom/usart/usart.h" #include "py/mphal.h" #include "py/mpstate.h" #include "mpconfigboard.h" -#include "asf/common2/services/delay/delay.h" -#include "asf/sam0/drivers/port/port.h" static volatile bool mp_cdc_enabled = false; extern struct usart_module usart_instance; @@ -58,24 +57,10 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { #endif } -//void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { - // send stdout to UART and USB CDC VCP - // if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { - // uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len); - // } - // if (usb_vcp_is_enabled()) { - // usb_vcp_send_strn_cooked(str, len); - // } -//} -// -// void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) { -// } -// -// void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt) { -// //mp_hal_gpio_clock_enable(gpio); -// } -// -// bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) { -// //mp_hal_gpio_clock_enable(pin->gpio); -// -// } +void mp_hal_delay_ms(mp_uint_t delay) { + delay_ms(delay); +} + +void mp_hal_delay_us(mp_uint_t delay) { + delay_us(delay); +}