From c3d35c6ac7348e8dc9072bb67b2c62294764fe00 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Sat, 8 Mar 2014 16:40:08 +0000
Subject: [PATCH] stm: Put pyb module in ROM.

---
 stm/Makefile       |   1 +
 stm/exti.c         |   8 +-
 stm/exti.h         |   4 +-
 stm/gpio.c         |  11 --
 stm/gpio.h         |   7 +-
 stm/main.c         | 230 +++---------------------------------
 stm/pin.h          |   2 +-
 stm/pin_map.c      |   3 +-
 stm/pybmodule.c    | 288 +++++++++++++++++++++++++++++++++++++++++++++
 stm/pybmodule.h    |   1 +
 stm/pyexec.c       |   2 +
 stm/pyexec.h       |   2 +-
 stm/qstrdefsport.h |  11 ++
 stm/servo.c        |  22 ++--
 stm/servo.h        |   6 +-
 stm/usart.c        |   8 +-
 stm/usart.h        |   2 +-
 17 files changed, 356 insertions(+), 252 deletions(-)
 create mode 100644 stm/pybmodule.c
 create mode 100644 stm/pybmodule.h

diff --git a/stm/Makefile b/stm/Makefile
index 1675c22ab..024d6d12e 100644
--- a/stm/Makefile
+++ b/stm/Makefile
@@ -79,6 +79,7 @@ SRC_C = \
 	pin_map.c \
 	exti.c \
 	usrsw.c \
+	pybmodule.c \
 #	pybwlan.c \
 
 SRC_S = \
diff --git a/stm/exti.c b/stm/exti.c
index 08a5da6b2..fa21eae8a 100644
--- a/stm/exti.c
+++ b/stm/exti.c
@@ -313,14 +313,14 @@ static const mp_obj_type_t exti_meta_obj_type = {
     .load_attr = exti_load_attr,
 };
 
-static const mp_obj_type_t exti_obj_type = {
+const mp_obj_type_t exti_obj_type = {
     { &exti_meta_obj_type },
     .name = MP_QSTR_Exti,
     .print = exti_obj_print,
     .methods = exti_methods,
 };
 
-void exti_init_early(void) {
+void exti_init(void) {
     for (exti_vector_t *v = exti_vector; v < &exti_vector[EXTI_NUM_VECTORS]; v++) {
         v->callback_obj = mp_const_none;
         v->param = NULL;
@@ -328,10 +328,6 @@ void exti_init_early(void) {
     }
 }
 
-void exti_init(mp_obj_t mod) {
-    rt_store_attr(mod, MP_QSTR_Exti, (mp_obj_t)&exti_obj_type);
-}
-
 static void Handle_EXTI_Irq(uint32_t line) {
     if (EXTI_PR_BB(line)) {
         EXTI_PR_BB(line) = 1; // Clears bit
diff --git a/stm/exti.h b/stm/exti.h
index 39d49d4af..619e44fd3 100644
--- a/stm/exti.h
+++ b/stm/exti.h
@@ -13,8 +13,7 @@
 
 #define EXTI_NUM_VECTORS  23
 
-void exti_init_early(void);
-void exti_init(mp_obj_t mod);
+void exti_init(void);
 
 uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp_obj_t callback_obj, void *param);
 
@@ -27,3 +26,4 @@ typedef struct {
   void *param;
 } exti_t;
 
+extern const mp_obj_type_t exti_obj_type;
diff --git a/stm/gpio.c b/stm/gpio.c
index e3db4086e..19e7098ac 100644
--- a/stm/gpio.c
+++ b/stm/gpio.c
@@ -92,14 +92,3 @@ mp_obj_t pyb_gpio_output(mp_obj_t arg_pin, mp_obj_t arg_mode) {
 }
 
 MP_DEFINE_CONST_FUN_OBJ_2(pyb_gpio_output_obj, pyb_gpio_output);
-
-void gpio_init(mp_obj_t mod) {
-    rt_store_attr(mod, MP_QSTR_gpio, (mp_obj_t)&pyb_gpio_obj);
-    rt_store_attr(mod, MP_QSTR_gpio_in, (mp_obj_t)&pyb_gpio_input_obj);
-    rt_store_attr(mod, MP_QSTR_gpio_out, (mp_obj_t)&pyb_gpio_output_obj);
-    rt_store_attr(mod, qstr_from_str("PULL_NONE"), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_NOPULL));
-    rt_store_attr(mod, qstr_from_str("PULL_UP"), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_UP));
-    rt_store_attr(mod, qstr_from_str("PULL_DOWN"), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_DOWN));
-    rt_store_attr(mod, qstr_from_str("PUSH_PULL"), MP_OBJ_NEW_SMALL_INT(GPIO_OType_PP));
-    rt_store_attr(mod, qstr_from_str("OPEN_DRAIN"), MP_OBJ_NEW_SMALL_INT(GPIO_OType_OD));
-}
diff --git a/stm/gpio.h b/stm/gpio.h
index 1071c9ef0..f48e95347 100644
--- a/stm/gpio.h
+++ b/stm/gpio.h
@@ -1,4 +1,5 @@
-mp_obj_t pyb_gpio(uint n_args, mp_obj_t *args);
 mp_obj_t pyb_gpio_input(mp_obj_t arg_pin, mp_obj_t arg_mode);
-mp_obj_t pyb_gpio_output(mp_obj_t arg_pin, mp_obj_t arg_mode);
-void gpio_init(mp_obj_t mod);
+
+MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_obj);
+MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_input_obj);
+MP_DECLARE_CONST_FUN_OBJ(pyb_gpio_output_obj);
diff --git a/stm/main.c b/stm/main.c
index bab8933b1..8279a9b7f 100644
--- a/stm/main.c
+++ b/stm/main.c
@@ -34,24 +34,20 @@
 #include "pendsv.h"
 #include "pyexec.h"
 #include "led.h"
-#include "gpio.h"
 #include "servo.h"
 #include "lcd.h"
 #include "storage.h"
 #include "sdcard.h"
 #include "accel.h"
-#include "usart.h"
 #include "usb.h"
 #include "timer.h"
-#include "audio.h"
 #include "pybwlan.h"
-#include "i2c.h"
 #include "usrsw.h"
-#include "adc.h"
 #include "rtc.h"
 #include "file.h"
 #include "pin.h"
 #include "exti.h"
+#include "pybmodule.h"
 
 int errno;
 
@@ -82,43 +78,26 @@ void __fatal_error(const char *msg) {
     }
 }
 
-static mp_obj_t pyb_config_source_dir = MP_OBJ_NULL;
-static mp_obj_t pyb_config_main = MP_OBJ_NULL;
+STATIC mp_obj_t pyb_config_source_dir = MP_OBJ_NULL;
+STATIC mp_obj_t pyb_config_main = MP_OBJ_NULL;
 
-mp_obj_t pyb_source_dir(mp_obj_t source_dir) {
+STATIC mp_obj_t pyb_source_dir(mp_obj_t source_dir) {
     if (MP_OBJ_IS_STR(source_dir)) {
         pyb_config_source_dir = source_dir;
     }
     return mp_const_none;
 }
 
-mp_obj_t pyb_main(mp_obj_t main) {
+MP_DEFINE_CONST_FUN_OBJ_1(pyb_source_dir_obj, pyb_source_dir);
+
+STATIC mp_obj_t pyb_main(mp_obj_t main) {
     if (MP_OBJ_IS_STR(main)) {
         pyb_config_main = main;
     }
     return mp_const_none;
 }
 
-// sync all file systems
-mp_obj_t pyb_sync(void) {
-    storage_flush();
-    return mp_const_none;
-}
-
-mp_obj_t pyb_delay(mp_obj_t count) {
-    sys_tick_delay_ms(mp_obj_get_int(count));
-    return mp_const_none;
-}
-
-mp_obj_t pyb_udelay(mp_obj_t usec) {
-    uint32_t count = 0;
-    const uint32_t utime = (168 * mp_obj_get_int(usec) / 5);
-    for (;;) {
-        if (++count > utime) {
-            return mp_const_none;
-        }
-    }
-}
+MP_DEFINE_CONST_FUN_OBJ_1(pyb_main_obj, pyb_main);
 
 void fatality(void) {
     led_state(PYB_LED_R1, 1);
@@ -151,6 +130,7 @@ static const char *help_text =
 "    pyb.gc()       -- run the garbage collector\n"
 "    pyb.repl_info(<val>) -- enable/disable printing of info after each command\n"
 "    pyb.delay(<n>) -- wait for n milliseconds\n"
+"    pyb.udelay(<n>) -- wait for n microseconds\n"
 "    pyb.Led(<n>)   -- create Led object for LED n (n=1,2)\n"
 "                      Led methods: on(), off()\n"
 "    pyb.Servo(<n>) -- create Servo object for servo n (n=1,2,3,4)\n"
@@ -170,129 +150,6 @@ static mp_obj_t pyb_help(void) {
     return mp_const_none;
 }
 
-// get lots of info about the board
-static mp_obj_t pyb_info(void) {
-    // get and print unique id; 96 bits
-    {
-        byte *id = (byte*)0x1fff7a10;
-        printf("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11]);
-    }
-
-    // get and print clock speeds
-    // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
-    {
-        RCC_ClocksTypeDef rcc_clocks;
-        RCC_GetClocksFreq(&rcc_clocks);
-        printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n", rcc_clocks.SYSCLK_Frequency, rcc_clocks.HCLK_Frequency, rcc_clocks.PCLK1_Frequency, rcc_clocks.PCLK2_Frequency);
-    }
-
-    // to print info about memory
-    {
-        printf("_text_end=%p\n", &_text_end);
-        printf("_data_start_init=%p\n", &_data_start_init);
-        printf("_data_start=%p\n", &_data_start);
-        printf("_data_end=%p\n", &_data_end);
-        printf("_bss_start=%p\n", &_bss_start);
-        printf("_bss_end=%p\n", &_bss_end);
-        printf("_stack_end=%p\n", &_stack_end);
-        printf("_ram_start=%p\n", &_ram_start);
-        printf("_heap_start=%p\n", &_heap_start);
-        printf("_heap_end=%p\n", &_heap_end);
-        printf("_ram_end=%p\n", &_ram_end);
-    }
-
-    // qstr info
-    {
-        uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
-        qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
-        printf("qstr:\n  n_pool=%u\n  n_qstr=%u\n  n_str_data_bytes=%u\n  n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
-    }
-
-    // GC info
-    {
-        gc_info_t info;
-        gc_info(&info);
-        printf("GC:\n");
-        printf("  %lu total\n", info.total);
-        printf("  %lu : %lu\n", info.used, info.free);
-        printf("  1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
-    }
-
-    // free space on flash
-    {
-        DWORD nclst;
-        FATFS *fatfs;
-        f_getfree("0:", &nclst, &fatfs);
-        printf("LFS free: %u bytes\n", (uint)(nclst * fatfs->csize * 512));
-    }
-
-    return mp_const_none;
-}
-
-static void SYSCLKConfig_STOP(void) {
-    /* After wake-up from STOP reconfigure the system clock */
-    /* Enable HSE */
-    RCC_HSEConfig(RCC_HSE_ON);
-
-    /* Wait till HSE is ready */
-    while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) {
-    }
-
-    /* Enable PLL */
-    RCC_PLLCmd(ENABLE);
-
-    /* Wait till PLL is ready */
-    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {
-    }
-
-    /* Select PLL as system clock source */
-    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
-
-    /* Wait till PLL is used as system clock source */
-    while (RCC_GetSYSCLKSource() != 0x08) {
-    }
-}
-
-static mp_obj_t pyb_stop(void) {
-    PWR_EnterSTANDBYMode();
-    //PWR_FlashPowerDownCmd(ENABLE); don't know what the logic is with this
-
-    /* Enter Stop Mode */
-    PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
-
-    /* Configures system clock after wake-up from STOP: enable HSE, PLL and select 
-     *        PLL as system clock source (HSE and PLL are disabled in STOP mode) */
-    SYSCLKConfig_STOP();
-
-    //PWR_FlashPowerDownCmd(DISABLE);
-
-    return mp_const_none;
-}
-
-static mp_obj_t pyb_standby(void) {
-    PWR_EnterSTANDBYMode();
-    return mp_const_none;
-}
-
-mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
-    mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
-    uint8_t data[4];
-    data[0] = mp_obj_get_int(items[0]);
-    data[1] = mp_obj_get_int(items[1]);
-    data[2] = mp_obj_get_int(items[2]);
-    data[3] = mp_obj_get_int(items[3]);
-    usb_hid_send_report(data);
-    return mp_const_none;
-}
-
-mp_obj_t pyb_rng_get(void) {
-    return mp_obj_new_int(RNG_GetRandomNumber() >> 16);
-}
-
-mp_obj_t pyb_millis(void) {
-    return mp_obj_new_int(sys_tick_counter);
-}
-
 int main(void) {
     // TODO disable JTAG
 
@@ -382,7 +239,8 @@ soft_reset:
     def_path[2] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib);
     sys_path = mp_obj_new_list(3, def_path);
 
-    exti_init_early();
+    exti_init();
+
 #if MICROPY_HW_HAS_SWITCH
     switch_init();
 #endif
@@ -408,65 +266,15 @@ soft_reset:
     RNG_Cmd(ENABLE);
 #endif
 
-    // add some functions to the python namespace
-    {
-        rt_store_name(MP_QSTR_help, rt_make_function_n(0, pyb_help));
-
-        mp_obj_t m = mp_obj_new_module(MP_QSTR_pyb);
-        rt_store_attr(m, MP_QSTR_info, rt_make_function_n(0, pyb_info));
-        rt_store_attr(m, MP_QSTR_gc, (mp_obj_t)&pyb_gc_obj);
-        rt_store_attr(m, qstr_from_str("repl_info"), rt_make_function_n(1, pyb_set_repl_info));
-#if MICROPY_HW_HAS_SDCARD
-        rt_store_attr(m, qstr_from_str("SD"), (mp_obj_t)&pyb_sdcard_obj);
-#endif
-        rt_store_attr(m, MP_QSTR_stop, rt_make_function_n(0, pyb_stop));
-        rt_store_attr(m, MP_QSTR_standby, rt_make_function_n(0, pyb_standby));
-        rt_store_attr(m, MP_QSTR_source_dir, rt_make_function_n(1, pyb_source_dir));
-        rt_store_attr(m, MP_QSTR_main, rt_make_function_n(1, pyb_main));
-        rt_store_attr(m, MP_QSTR_sync, rt_make_function_n(0, pyb_sync));
-        rt_store_attr(m, MP_QSTR_delay, rt_make_function_n(1, pyb_delay));
-        rt_store_attr(m, MP_QSTR_udelay, rt_make_function_n(1, pyb_udelay));
-#if MICROPY_HW_HAS_SWITCH
-        rt_store_attr(m, MP_QSTR_switch, (mp_obj_t)&pyb_switch_obj);
-#endif
-#if MICROPY_HW_ENABLE_SERVO
-        rt_store_attr(m, MP_QSTR_servo, rt_make_function_n(2, pyb_servo_set));
-#endif
-        rt_store_attr(m, MP_QSTR_pwm, rt_make_function_n(2, pyb_pwm_set));
-#if MICROPY_HW_HAS_MMA7660
-        rt_store_attr(m, MP_QSTR_accel, (mp_obj_t)&pyb_accel_read_obj);
-        rt_store_attr(m, MP_QSTR_accel_read, (mp_obj_t)&pyb_accel_read_all_obj);
-        rt_store_attr(m, MP_QSTR_accel_mode, (mp_obj_t)&pyb_accel_write_mode_obj);
-#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, (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));
-#endif
-        rt_store_attr(m, MP_QSTR_Led, (mp_obj_t)&pyb_Led_obj);
-#if MICROPY_HW_ENABLE_SERVO
-        rt_store_attr(m, MP_QSTR_Servo, rt_make_function_n(1, pyb_Servo));
-#endif
-        rt_store_attr(m, MP_QSTR_I2C, rt_make_function_n(2, pyb_I2C));
-        rt_store_attr(m, MP_QSTR_Usart, rt_make_function_n(2, pyb_Usart));
-        rt_store_attr(m, qstr_from_str("ADC_all"), (mp_obj_t)&pyb_ADC_all_obj);
-        rt_store_attr(m, MP_QSTR_ADC, (mp_obj_t)&pyb_ADC_obj);
-        rt_store_attr(m, qstr_from_str("millis"), rt_make_function_n(0, pyb_millis));
-
-#if MICROPY_HW_ENABLE_AUDIO
-        rt_store_attr(m, qstr_from_str("Audio"), (mp_obj_t)&pyb_Audio_obj);
-#endif
+    pin_map_init();
 
-        pin_map_init(m);
-        gpio_init(m);
-        exti_init(m);
-        rt_store_name(MP_QSTR_pyb, m);
+    // add some functions to the builtin Python namespace
+    rt_store_name(MP_QSTR_help, rt_make_function_n(0, pyb_help));
+    rt_store_name(MP_QSTR_open, rt_make_function_n(2, pyb_io_open));
 
-        rt_store_name(MP_QSTR_open, rt_make_function_n(2, pyb_io_open));
-    }
+    // we pre-import the pyb module
+    // probably shouldn't do this, so we are compatible with CPython
+    rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
 
     // check if user switch held (initiates reset of filesystem)
     bool reset_filesystem = false;
@@ -659,7 +467,7 @@ soft_reset:
     pyexec_repl();
 
     printf("PYB: sync filesystems\n");
-    pyb_sync();
+    storage_flush();
 
     printf("PYB: soft reboot\n");
 
diff --git a/stm/pin.h b/stm/pin.h
index 5cfd9fb0f..d5d769058 100644
--- a/stm/pin.h
+++ b/stm/pin.h
@@ -110,7 +110,7 @@ extern const pin_named_pins_obj_t pin_cpu_pins_obj;
 const pin_obj_t *pin_find_named_pin(const pin_named_pin_t *pins, const char *name);
 const pin_af_obj_t *pin_find_af(const pin_obj_t *pin, uint8_t fn, uint8_t unit, uint8_t pin_type);
 
-void pin_map_init(mp_obj_t mod);
+void pin_map_init(void);
 
 // C function for mapping python pin identifier into an ordinal pin number.
 const pin_obj_t *pin_map_user_obj(mp_obj_t user_obj);
diff --git a/stm/pin_map.c b/stm/pin_map.c
index bd2cfb9e9..e4b1624a3 100644
--- a/stm/pin_map.c
+++ b/stm/pin_map.c
@@ -142,9 +142,8 @@ static const pin_map_obj_t pin_map_obj_init = {
 
 pin_map_obj_t pin_map_obj;
 
-void pin_map_init(mp_obj_t mod) {
+void pin_map_init(void) {
     pin_map_obj = pin_map_obj_init;
-    rt_store_attr(mod, MP_QSTR_Pin, (mp_obj_t)&pin_map_obj);
 }
 
 // C API used to convert a user-supplied pin name  into an ordinal pin number.
diff --git a/stm/pybmodule.c b/stm/pybmodule.c
new file mode 100644
index 000000000..ed705af73
--- /dev/null
+++ b/stm/pybmodule.c
@@ -0,0 +1,288 @@
+#include <stdint.h>
+#include <stdio.h>
+
+#include <stm32f4xx.h>
+#include <stm32f4xx_rcc.h>
+
+#include "misc.h"
+#include "ff.h"
+#include "mpconfig.h"
+#include "qstr.h"
+#include "obj.h"
+#include "map.h"
+#include "gc.h"
+#include "gccollect.h"
+#include "systick.h"
+#include "rtc.h"
+#include "pyexec.h"
+#include "servo.h"
+#include "storage.h"
+#include "usb.h"
+#include "usrsw.h"
+#include "sdcard.h"
+#include "accel.h"
+#include "led.h"
+#include "i2c.h"
+#include "usart.h"
+#include "adc.h"
+#include "audio.h"
+#include "pin.h"
+#include "gpio.h"
+#include "exti.h"
+#include "pybmodule.h"
+
+// get lots of info about the board
+STATIC mp_obj_t pyb_info(void) {
+    // get and print unique id; 96 bits
+    {
+        byte *id = (byte*)0x1fff7a10;
+        printf("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11]);
+    }
+
+    // get and print clock speeds
+    // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
+    {
+        RCC_ClocksTypeDef rcc_clocks;
+        RCC_GetClocksFreq(&rcc_clocks);
+        printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n", rcc_clocks.SYSCLK_Frequency, rcc_clocks.HCLK_Frequency, rcc_clocks.PCLK1_Frequency, rcc_clocks.PCLK2_Frequency);
+    }
+
+    // to print info about memory
+    {
+        printf("_text_end=%p\n", &_text_end);
+        printf("_data_start_init=%p\n", &_data_start_init);
+        printf("_data_start=%p\n", &_data_start);
+        printf("_data_end=%p\n", &_data_end);
+        printf("_bss_start=%p\n", &_bss_start);
+        printf("_bss_end=%p\n", &_bss_end);
+        printf("_stack_end=%p\n", &_stack_end);
+        printf("_ram_start=%p\n", &_ram_start);
+        printf("_heap_start=%p\n", &_heap_start);
+        printf("_heap_end=%p\n", &_heap_end);
+        printf("_ram_end=%p\n", &_ram_end);
+    }
+
+    // qstr info
+    {
+        uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
+        qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
+        printf("qstr:\n  n_pool=%u\n  n_qstr=%u\n  n_str_data_bytes=%u\n  n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
+    }
+
+    // GC info
+    {
+        gc_info_t info;
+        gc_info(&info);
+        printf("GC:\n");
+        printf("  %lu total\n", info.total);
+        printf("  %lu : %lu\n", info.used, info.free);
+        printf("  1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
+    }
+
+    // free space on flash
+    {
+        DWORD nclst;
+        FATFS *fatfs;
+        f_getfree("0:", &nclst, &fatfs);
+        printf("LFS free: %u bytes\n", (uint)(nclst * fatfs->csize * 512));
+    }
+
+    return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_info_obj, pyb_info);
+
+// sync all file systems
+STATIC mp_obj_t pyb_sync(void) {
+    storage_flush();
+    return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
+
+STATIC mp_obj_t pyb_millis(void) {
+    return mp_obj_new_int(sys_tick_counter);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
+
+STATIC mp_obj_t pyb_delay(mp_obj_t count) {
+    sys_tick_delay_ms(mp_obj_get_int(count));
+    return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
+
+STATIC mp_obj_t pyb_udelay(mp_obj_t usec) {
+    uint32_t count = 0;
+    const uint32_t utime = (168 * mp_obj_get_int(usec) / 5);
+    for (;;) {
+        if (++count > utime) {
+            return mp_const_none;
+        }
+    }
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
+
+STATIC mp_obj_t pyb_rng_get(void) {
+    return mp_obj_new_int(RNG_GetRandomNumber() >> 16);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_rng_get_obj, pyb_rng_get);
+
+STATIC void SYSCLKConfig_STOP(void) {
+    /* After wake-up from STOP reconfigure the system clock */
+    /* Enable HSE */
+    RCC_HSEConfig(RCC_HSE_ON);
+
+    /* Wait till HSE is ready */
+    while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) {
+    }
+
+    /* Enable PLL */
+    RCC_PLLCmd(ENABLE);
+
+    /* Wait till PLL is ready */
+    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {
+    }
+
+    /* Select PLL as system clock source */
+    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
+
+    /* Wait till PLL is used as system clock source */
+    while (RCC_GetSYSCLKSource() != 0x08) {
+    }
+}
+
+STATIC mp_obj_t pyb_stop(void) {
+    PWR_EnterSTANDBYMode();
+    //PWR_FlashPowerDownCmd(ENABLE); don't know what the logic is with this
+
+    /* Enter Stop Mode */
+    PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
+
+    /* Configures system clock after wake-up from STOP: enable HSE, PLL and select 
+     *        PLL as system clock source (HSE and PLL are disabled in STOP mode) */
+    SYSCLKConfig_STOP();
+
+    //PWR_FlashPowerDownCmd(DISABLE);
+
+    return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_0(pyb_stop_obj, pyb_stop);
+
+STATIC mp_obj_t pyb_standby(void) {
+    PWR_EnterSTANDBYMode();
+    return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby);
+
+STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
+    mp_obj_t *items = mp_obj_get_array_fixed_n(arg, 4);
+    uint8_t data[4];
+    data[0] = mp_obj_get_int(items[0]);
+    data[1] = mp_obj_get_int(items[1]);
+    data[2] = mp_obj_get_int(items[2]);
+    data[3] = mp_obj_get_int(items[3]);
+    usb_hid_send_report(data);
+    return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
+
+MP_DEFINE_CONST_FUN_OBJ_2(pyb_I2C_obj, pyb_I2C); // TODO put this in i2c.c
+
+MP_DECLARE_CONST_FUN_OBJ(pyb_source_dir_obj); // defined in main.c
+MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
+
+STATIC const mp_map_elem_t pyb_module_globals_table[] = {
+    { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) },
+
+    { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_gc), (mp_obj_t)&pyb_gc_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_repl_info), (mp_obj_t)&pyb_set_repl_info_obj },
+
+    { MP_OBJ_NEW_QSTR(MP_QSTR_stop), (mp_obj_t)&pyb_stop_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_standby), (mp_obj_t)&pyb_standby_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_source_dir), (mp_obj_t)&pyb_source_dir_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_main), (mp_obj_t)&pyb_main_obj },
+
+    { MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },
+
+#if MICROPY_HW_ENABLE_RNG
+    { MP_OBJ_NEW_QSTR(MP_QSTR_rand), (mp_obj_t)&pyb_rng_get_obj },
+#endif
+
+#if MICROPY_HW_ENABLE_RTC
+    { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&pyb_rtc_read_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_rtc_info), (mp_obj_t)&pyb_rtc_info_obj },
+#endif
+
+#if MICROPY_HW_ENABLE_SERVO
+    { MP_OBJ_NEW_QSTR(MP_QSTR_pwm), (mp_obj_t)&pyb_pwm_set_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_servo), (mp_obj_t)&pyb_servo_set_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_Servo), (mp_obj_t)&pyb_Servo_obj },
+#endif
+
+#if MICROPY_HW_HAS_SWITCH
+    { MP_OBJ_NEW_QSTR(MP_QSTR_switch), (mp_obj_t)&pyb_switch_obj },
+#endif
+
+#if MICROPY_HW_HAS_SDCARD
+    { MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj },
+#endif
+
+#if MICROPY_HW_HAS_MMA7660
+    { MP_OBJ_NEW_QSTR(MP_QSTR_accel), (mp_obj_t)&pyb_accel_read_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_accel_read), (mp_obj_t)&pyb_accel_read_all_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_accel_mode), (mp_obj_t)&pyb_accel_write_mode_obj },
+#endif
+
+    { MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj },
+
+    { MP_OBJ_NEW_QSTR(MP_QSTR_Led), (mp_obj_t)&pyb_Led_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_I2C_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_Usart), (mp_obj_t)&pyb_Usart_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_ADC_all), (mp_obj_t)&pyb_ADC_all_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_ADC_obj },
+
+#if MICROPY_HW_ENABLE_AUDIO
+    { MP_OBJ_NEW_QSTR(MP_QSTR_Audio), (mp_obj_t)&pyb_Audio_obj },
+#endif
+
+    // pin mapper
+    { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_map_obj },
+
+    // GPIO bindings
+    { MP_OBJ_NEW_QSTR(MP_QSTR_gpio), (mp_obj_t)&pyb_gpio_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_gpio_in), (mp_obj_t)&pyb_gpio_input_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_gpio_out), (mp_obj_t)&pyb_gpio_output_obj },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_NONE), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_NOPULL) },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_UP) },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(GPIO_PuPd_DOWN) },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_PUSH_PULL), MP_OBJ_NEW_SMALL_INT(GPIO_OType_PP) },
+    { MP_OBJ_NEW_QSTR(MP_QSTR_OPEN_DRAIN), MP_OBJ_NEW_SMALL_INT(GPIO_OType_OD) },
+
+    // EXTI bindings
+    { MP_OBJ_NEW_QSTR(MP_QSTR_Exti), (mp_obj_t)&exti_obj_type },
+};
+
+STATIC const mp_map_t pyb_module_globals = {
+    .all_keys_are_qstrs = 1,
+    .table_is_fixed_array = 1,
+    .used = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
+    .alloc = sizeof(pyb_module_globals_table) / sizeof(mp_map_elem_t),
+    .table = (mp_map_elem_t*)pyb_module_globals_table,
+};
+
+const mp_obj_module_t pyb_module = {
+    .base = { &mp_type_module },
+    .name = MP_QSTR_pyb,
+    .globals = (mp_map_t*)&pyb_module_globals,
+};
diff --git a/stm/pybmodule.h b/stm/pybmodule.h
new file mode 100644
index 000000000..955aaefe3
--- /dev/null
+++ b/stm/pybmodule.h
@@ -0,0 +1 @@
+extern const mp_obj_module_t pyb_module;
diff --git a/stm/pyexec.c b/stm/pyexec.c
index 207f2234f..75fe87c88 100644
--- a/stm/pyexec.c
+++ b/stm/pyexec.c
@@ -317,3 +317,5 @@ mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
     repl_display_debugging_info = mp_obj_get_int(o_value);
     return mp_const_none;
 }
+
+MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info);
diff --git a/stm/pyexec.h b/stm/pyexec.h
index 3d28ec5ce..cad15af6f 100644
--- a/stm/pyexec.h
+++ b/stm/pyexec.h
@@ -2,4 +2,4 @@ void pyexec_raw_repl(void);
 void pyexec_repl(void);
 bool pyexec_file(const char *filename);
 
-mp_obj_t pyb_set_repl_info(mp_obj_t o_value);
+MP_DECLARE_CONST_FUN_OBJ(pyb_set_repl_info_obj);
diff --git a/stm/qstrdefsport.h b/stm/qstrdefsport.h
index 2978f8276..13532892a 100644
--- a/stm/qstrdefsport.h
+++ b/stm/qstrdefsport.h
@@ -10,6 +10,7 @@ Q(source_dir)
 Q(main)
 Q(sync)
 Q(gc)
+Q(repl_info)
 Q(delay)
 Q(udelay)
 Q(switch)
@@ -25,6 +26,7 @@ Q(rand)
 Q(Led)
 Q(LCD)
 Q(Servo)
+Q(SD)
 Q(SDcard)
 Q(I2C)
 Q(gpio)
@@ -32,6 +34,8 @@ Q(gpio_in)
 Q(gpio_out)
 Q(Usart)
 Q(ADC)
+Q(ADC_all)
+Q(Audio)
 Q(open)
 Q(File)
 // Entries for sys.path
@@ -44,3 +48,10 @@ Q(PinAF)
 Q(PinNamed)
 Q(Exti)
 Q(ExtiMeta)
+Q(rtc_info)
+Q(millis)
+Q(PULL_NONE)
+Q(PULL_UP)
+Q(PULL_DOWN)
+Q(PUSH_PULL)
+Q(OPEN_DRAIN)
diff --git a/stm/servo.c b/stm/servo.c
index 4edac0c86..ae266d7f6 100644
--- a/stm/servo.c
+++ b/stm/servo.c
@@ -90,7 +90,7 @@ void servo_init(void) {
 /******************************************************************************/
 /* Micro Python bindings                                                      */
 
-mp_obj_t pyb_servo_set(mp_obj_t port, mp_obj_t value) {
+STATIC mp_obj_t pyb_servo_set(mp_obj_t port, mp_obj_t value) {
     int p = mp_obj_get_int(port);
     int v = mp_obj_get_int(value);
     if (v < 50) { v = 50; }
@@ -104,7 +104,9 @@ mp_obj_t pyb_servo_set(mp_obj_t port, mp_obj_t value) {
     return mp_const_none;
 }
 
-mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse) {
+MP_DEFINE_CONST_FUN_OBJ_2(pyb_servo_set_obj, pyb_servo_set);
+
+STATIC mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse) {
     int pe = mp_obj_get_int(period);
     int pu = mp_obj_get_int(pulse);
     TIM2->ARR = pe;
@@ -112,17 +114,19 @@ mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse) {
     return mp_const_none;
 }
 
+MP_DEFINE_CONST_FUN_OBJ_2(pyb_pwm_set_obj, pyb_pwm_set);
+
 typedef struct _pyb_servo_obj_t {
     mp_obj_base_t base;
     uint servo_id;
 } pyb_servo_obj_t;
 
-static void servo_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
+STATIC void servo_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
     pyb_servo_obj_t *self = self_in;
     print(env, "<Servo %lu>", self->servo_id);
 }
 
-static mp_obj_t servo_obj_angle(mp_obj_t self_in, mp_obj_t angle) {
+STATIC mp_obj_t servo_obj_angle(mp_obj_t self_in, mp_obj_t angle) {
     pyb_servo_obj_t *self = self_in;
 #if MICROPY_ENABLE_FLOAT
     machine_int_t v = 152 + 85.0 * mp_obj_get_float(angle) / 90.0;
@@ -140,23 +144,25 @@ static mp_obj_t servo_obj_angle(mp_obj_t self_in, mp_obj_t angle) {
     return mp_const_none;
 }
 
-static MP_DEFINE_CONST_FUN_OBJ_2(servo_obj_angle_obj, servo_obj_angle);
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(servo_obj_angle_obj, servo_obj_angle);
 
-static const mp_method_t servo_methods[] = {
+STATIC const mp_method_t servo_methods[] = {
     { "angle", &servo_obj_angle_obj },
     { NULL, NULL },
 };
 
-static const mp_obj_type_t servo_obj_type = {
+STATIC const mp_obj_type_t servo_obj_type = {
     { &mp_type_type },
     .name = MP_QSTR_Servo,
     .print = servo_obj_print,
     .methods = servo_methods,
 };
 
-mp_obj_t pyb_Servo(mp_obj_t servo_id) {
+STATIC mp_obj_t pyb_Servo(mp_obj_t servo_id) {
     pyb_servo_obj_t *o = m_new_obj(pyb_servo_obj_t);
     o->base.type = &servo_obj_type;
     o->servo_id = mp_obj_get_int(servo_id);
     return o;
 }
+
+MP_DEFINE_CONST_FUN_OBJ_1(pyb_Servo_obj, pyb_Servo);
diff --git a/stm/servo.h b/stm/servo.h
index 02702e067..bb3edd97a 100644
--- a/stm/servo.h
+++ b/stm/servo.h
@@ -1,5 +1,5 @@
 void servo_init(void);
 
-mp_obj_t pyb_servo_set(mp_obj_t port, mp_obj_t value);
-mp_obj_t pyb_pwm_set(mp_obj_t period, mp_obj_t pulse);
-mp_obj_t pyb_Servo(mp_obj_t servo_id);
+MP_DECLARE_CONST_FUN_OBJ(pyb_servo_set_obj);
+MP_DECLARE_CONST_FUN_OBJ(pyb_pwm_set_obj);
+MP_DECLARE_CONST_FUN_OBJ(pyb_Servo_obj);
diff --git a/stm/usart.c b/stm/usart.c
index 78d9a0fee..ac457b61f 100644
--- a/stm/usart.c
+++ b/stm/usart.c
@@ -235,7 +235,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(usart_obj_rx_char_obj, usart_obj_rx_char);
 static MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_char_obj, usart_obj_tx_char);
 static MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_str_obj, usart_obj_tx_str);
 
-static const mp_method_t usart_methods[] = {
+STATIC const mp_method_t usart_methods[] = {
     { "status", &usart_obj_status_obj },
     { "recv_chr", &usart_obj_rx_char_obj },
     { "send_chr", &usart_obj_tx_char_obj },
@@ -243,14 +243,14 @@ static const mp_method_t usart_methods[] = {
     { NULL, NULL },
 };
 
-static const mp_obj_type_t usart_obj_type = {
+STATIC const mp_obj_type_t usart_obj_type = {
     { &mp_type_type },
     .name = MP_QSTR_Usart,
     .print = usart_obj_print,
     .methods = usart_methods,
 };
 
-mp_obj_t pyb_Usart(mp_obj_t usart_id, mp_obj_t baudrate) {
+STATIC mp_obj_t pyb_Usart(mp_obj_t usart_id, mp_obj_t baudrate) {
     if (mp_obj_get_int(usart_id)>PYB_USART_MAX) {
         return mp_const_none;
     }
@@ -264,3 +264,5 @@ mp_obj_t pyb_Usart(mp_obj_t usart_id, mp_obj_t baudrate) {
     o->is_enabled = true;
     return o;
 }
+
+MP_DEFINE_CONST_FUN_OBJ_2(pyb_Usart_obj, pyb_Usart);
diff --git a/stm/usart.h b/stm/usart.h
index 6ae2a44ae..c641281a7 100644
--- a/stm/usart.h
+++ b/stm/usart.h
@@ -20,4 +20,4 @@ int usart_rx_char(pyb_usart_t usart_id);
 void usart_tx_str(pyb_usart_t usart_id, const char *str);
 void usart_tx_strn_cooked(pyb_usart_t usart_id, const char *str, int len);
 
-mp_obj_t pyb_Usart(mp_obj_t usart_id, mp_obj_t baudrate);
+MP_DECLARE_CONST_FUN_OBJ(pyb_Usart_obj);
-- 
GitLab