diff options
| -rw-r--r-- | py/builtinimport.c | 19 | ||||
| -rw-r--r-- | stm/main.c | 3 | ||||
| -rw-r--r-- | stm/rtc.c | 58 | ||||
| -rw-r--r-- | stm/rtc.h | 4 | ||||
| -rw-r--r-- | tests/basics/import-pkg2.py | 18 |
5 files changed, 81 insertions, 21 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index 0a730b031..05b8eead5 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -132,13 +132,18 @@ void do_load(mp_obj_t module_obj, vstr_t *file) { mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { /* printf("import:\n"); - for (int i = 0; i < n; i++) { + for (int i = 0; i < n_args; i++) { printf(" "); - mp_obj_print(args[i]); + mp_obj_print(args[i], PRINT_REPR); printf("\n"); } */ + mp_obj_t fromtuple = mp_const_none; + if (n_args >= 4) { + fromtuple = args[3]; + } + uint mod_len; const char *mod_str = (const char*)mp_obj_str_get_data(args[0], &mod_len); @@ -150,8 +155,11 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { if (p == NULL) { return module_obj; } + // If fromlist is not empty, return leaf module + if (fromtuple != mp_const_none) { + return module_obj; + } // Otherwise, we need to return top-level package - // TODO: subject to fromlist arg qstr pkg_name = qstr_from_strn(mod_str, p - mod_str); return mp_obj_module_get(pkg_name); } @@ -227,6 +235,11 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { assert(0); } + // If fromlist is not empty, return leaf module + if (fromtuple != mp_const_none) { + return module_obj; + } + // Otherwise, we need to return top-level package return top_module_obj; } diff --git a/stm/main.c b/stm/main.c index f82a18692..4fed5850a 100644 --- a/stm/main.c +++ b/stm/main.c @@ -431,7 +431,8 @@ soft_reset: #endif rt_store_attr(m, MP_QSTR_hid, rt_make_function_n(1, pyb_hid_send_report)); #if MICROPY_HW_ENABLE_RTC - rt_store_attr(m, MP_QSTR_time, rt_make_function_n(0, pyb_rtc_read)); + rt_store_attr(m, MP_QSTR_time, (mp_obj_t)&pyb_rtc_read_obj); + rt_store_attr(m, qstr_from_str("rtc_info"), (mp_obj_t)&pyb_rtc_info_obj); #endif #if MICROPY_HW_ENABLE_RNG rt_store_attr(m, MP_QSTR_rand, rt_make_function_n(0, pyb_rng_get)); @@ -5,38 +5,50 @@ #include "mpconfig.h" #include "qstr.h" #include "obj.h" +#include "systick.h" #include "rtc.h" +machine_uint_t rtc_info; + +#define RTC_INFO_USE_EXISTING (0) +#define RTC_INFO_USE_LSE (1) +#define RTC_INFO_USE_LSI (3) + void rtc_init(void) { - /* Enable the PWR clock */ + // Enable the PWR clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); - /* Allow access to RTC */ + // Allow access to RTC PWR_BackupAccessCmd(ENABLE); if (RTC_ReadBackupRegister(RTC_BKP_DR0) == 0x32F2) { // RTC still alive, so don't re-init it // wait for RTC APB register synchronisation RTC_WaitForSynchro(); + rtc_info = RTC_INFO_USE_EXISTING; return; } uint32_t timeout = 10000000; - /* Enable the PWR clock */ + // Enable the PWR clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); - /* Allow access to RTC */ + // Allow access to RTC PWR_BackupAccessCmd(ENABLE); - /* Enable the LSE OSC */ + // Enable the LSE OSC RCC_LSEConfig(RCC_LSE_ON); - /* Wait till LSE is ready */ + // Wait till LSE is ready + machine_uint_t sys_tick = sys_tick_counter; while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--timeout > 0)) { } - /* If LSE timed out, use LSI instead */ + // record how long it took for the RTC to start up + rtc_info = (sys_tick_counter - sys_tick) << 2; + + // If LSE timed out, use LSI instead if (timeout == 0) { // Disable the LSE OSC RCC_LSEConfig(RCC_LSE_OFF); @@ -44,29 +56,35 @@ void rtc_init(void) { // Enable the LSI OSC RCC_LSICmd(ENABLE); - /* Wait till LSI is ready */ + // Wait till LSI is ready while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { } - /* Use LSI as the RTC Clock Source */ + // Use LSI as the RTC Clock Source RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); + + // record that we are using the LSI + rtc_info |= RTC_INFO_USE_LSI; } else { - /* Use LSE as the RTC Clock Source */ + // Use LSE as the RTC Clock Source RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); + + // record that we are using the LSE + rtc_info |= RTC_INFO_USE_LSE; } - /* Note: LSI is around (32KHz), these dividers should work either way */ - /* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/ + // Note: LSI is around (32KHz), these dividers should work either way + // ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1) uint32_t uwSynchPrediv = 0xFF; uint32_t uwAsynchPrediv = 0x7F; - /* Enable the RTC Clock */ + // Enable the RTC Clock RCC_RTCCLKCmd(ENABLE); - /* Wait for RTC APB registers synchronisation */ + // Wait for RTC APB registers synchronisation RTC_WaitForSynchro(); - /* Configure the RTC data register and RTC prescaler */ + // Configure the RTC data register and RTC prescaler RTC_InitTypeDef RTC_InitStructure; RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv; @@ -94,7 +112,13 @@ void rtc_init(void) { } /******************************************************************************/ -/* Micro Python bindings */ +// Micro Python bindings + +mp_obj_t pyb_rtc_info(void) { + return mp_obj_new_int(rtc_info); +} + +MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_info_obj, pyb_rtc_info); mp_obj_t pyb_rtc_read(void) { RTC_TimeTypeDef RTC_TimeStructure; @@ -102,3 +126,5 @@ mp_obj_t pyb_rtc_read(void) { printf("%02d:%02d:%02d\n", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds); return mp_const_none; } + +MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_read_obj, pyb_rtc_read); @@ -1,2 +1,4 @@ void rtc_init(void); -mp_obj_t pyb_rtc_read(void); + +MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_info_obj); +MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_read_obj); diff --git a/tests/basics/import-pkg2.py b/tests/basics/import-pkg2.py new file mode 100644 index 000000000..2e9f34121 --- /dev/null +++ b/tests/basics/import-pkg2.py @@ -0,0 +1,18 @@ +from pkg.mod import foo + +try: + pkg +except NameError: + print("NameError") +try: + pkg.mod +except NameError: + print("NameError") +print(foo()) + +# Import few times, must be same module objects +mod_1 = __import__("pkg.mod", None, None, ("foo",)) +mod_2 = __import__("pkg.mod", None, None, ("foo",)) +print(mod_1 is mod_2) +print(mod_1.foo is mod_2.foo) +print(foo is mod_1.foo) |
