Skip to content

Commit 258804a

Browse files
committed
atmel-samd: Add limited time module support.
1 parent 0c4f9b8 commit 258804a

File tree

4 files changed

+109
-29
lines changed

4 files changed

+109
-29
lines changed

atmel-samd/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ SRC_ASF = $(addprefix asf/sam0/,\
109109

110110
SRC_C = \
111111
main.c \
112+
modutime.c \
112113
mphalport.c \
113114
uart.c \
114115
asf/common/services/sleepmgr/samd/sleepmgr.c \
@@ -124,11 +125,16 @@ SRC_C = \
124125
lib/utils/printf.c \
125126
lib/utils/pyexec.c \
126127
lib/libc/string0.c \
127-
lib/mp-readline/readline.c \
128-
$(BUILD)/_frozen_mpy.c \
128+
lib/mp-readline/readline.c
129+
130+
SRC_AUTOGEN = \
131+
$(BUILD)/_frozen_mpy.c \
129132

130133
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
131134
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
135+
OBJ += $(addprefix $(BUILD)/, $(SRC_AUTOGEN:.c=.o))
136+
137+
SRC_QSTR += $(SRC_C)
132138

133139
ifeq ($(CROSS), 1)
134140
all: $(BUILD)/firmware.bin

atmel-samd/modutime.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
* Copyright (c) 2015 Josef Gajdusek
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <stdio.h>
29+
#include <string.h>
30+
31+
#include "py/nlr.h"
32+
#include "py/obj.h"
33+
#include "py/gc.h"
34+
#include "py/runtime.h"
35+
#include "py/mphal.h"
36+
#include "py/smallint.h"
37+
38+
/// \module time - time related functions
39+
///
40+
/// The `time` module provides functions for getting the current time and date,
41+
/// and for sleeping.
42+
43+
/// \function sleep(seconds)
44+
/// Sleep for the given number of seconds.
45+
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
46+
#if MICROPY_PY_BUILTINS_FLOAT
47+
mp_hal_delay_ms(1000 * mp_obj_get_float(seconds_o));
48+
#else
49+
mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
50+
#endif
51+
return mp_const_none;
52+
}
53+
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
54+
55+
STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
56+
mp_hal_delay_ms(mp_obj_get_int(arg));
57+
return mp_const_none;
58+
}
59+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms);
60+
61+
STATIC mp_obj_t time_sleep_us(mp_obj_t arg) {
62+
mp_hal_delay_us(mp_obj_get_int(arg));
63+
return mp_const_none;
64+
}
65+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us);
66+
67+
STATIC const mp_map_elem_t time_module_globals_table[] = {
68+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) },
69+
70+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
71+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj },
72+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj },
73+
};
74+
75+
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
76+
77+
const mp_obj_module_t utime_module = {
78+
.base = { &mp_type_module },
79+
.name = MP_QSTR_utime,
80+
.globals = (mp_obj_dict_t*)&time_module_globals,
81+
};

atmel-samd/mpconfigport.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
5454
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)
5555

56+
#define MICROPY_MODULE_WEAK_LINKS (1)
57+
5658
// type definitions for the specific machine
5759

5860
#define BYTES_PER_WORD (4)
@@ -72,10 +74,16 @@ typedef long mp_off_t;
7274

7375
#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
7476

75-
// extra built in names to add to the global namespace
76-
extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
77-
#define MICROPY_PORT_BUILTINS \
78-
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
77+
// extra built in modules to add to the list of known ones
78+
extern const struct _mp_obj_module_t machine_module;
79+
extern const struct _mp_obj_module_t utime_module;
80+
81+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_umachine), (mp_obj_t)&machine_module },
82+
#define MICROPY_PORT_BUILTIN_MODULES \
83+
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module } \
84+
85+
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
86+
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&utime_module } \
7987

8088

8189
// board specific definitions

atmel-samd/mphalport.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#include <string.h>
22

33
#include "asf/common/services/usb/class/cdc/device/udi_cdc.h"
4+
#include "asf/common2/services/delay/delay.h"
45
#include "asf/sam0/drivers/sercom/usart/usart.h"
56
#include "py/mphal.h"
67
#include "py/mpstate.h"
78

89
#include "mpconfigboard.h"
910

10-
#include "asf/common2/services/delay/delay.h"
11-
#include "asf/sam0/drivers/port/port.h"
1211
static volatile bool mp_cdc_enabled = false;
1312
extern struct usart_module usart_instance;
1413

@@ -58,24 +57,10 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
5857
#endif
5958
}
6059

61-
//void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
62-
// send stdout to UART and USB CDC VCP
63-
// if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
64-
// uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len);
65-
// }
66-
// if (usb_vcp_is_enabled()) {
67-
// usb_vcp_send_strn_cooked(str, len);
68-
// }
69-
//}
70-
//
71-
// void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
72-
// }
73-
//
74-
// void mp_hal_gpio_config(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alt) {
75-
// //mp_hal_gpio_clock_enable(gpio);
76-
// }
77-
//
78-
// bool mp_hal_gpio_set_af(const pin_obj_t *pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) {
79-
// //mp_hal_gpio_clock_enable(pin->gpio);
80-
//
81-
// }
60+
void mp_hal_delay_ms(mp_uint_t delay) {
61+
delay_ms(delay);
62+
}
63+
64+
void mp_hal_delay_us(mp_uint_t delay) {
65+
delay_us(delay);
66+
}

0 commit comments

Comments
 (0)