summaryrefslogtreecommitdiff
path: root/stm/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'stm/main.c')
-rw-r--r--stm/main.c230
1 files changed, 19 insertions, 211 deletions
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");