// function usocket.getaddrinfo(host, port) STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { mp_uint_t hlen; const char *host = mp_obj_str_get_data(host_in, &hlen); mp_int_t port = mp_obj_get_int(port_in); // find a NIC that can do a name lookup for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); if (nic_type->gethostbyname != NULL) { uint8_t out_ip[MOD_NETWORK_IPADDR_BUF_SIZE]; int ret = nic_type->gethostbyname(nic, host, hlen, out_ip); if (ret != 0) { // TODO CPython raises: socket.gaierror: [Errno -2] Name or service not known nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ret))); } mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL); tuple->items[0] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_AF_INET); tuple->items[1] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_SOCK_STREAM); tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_); tuple->items[4] = mod_network_format_inet_addr(out_ip, port); return mp_obj_new_list(1, (mp_obj_t*)&tuple); } } nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC")); }
STATIC void mp_reset(void) { mp_stack_set_top((void*)0x40000000); mp_stack_set_limit(8192); mp_hal_init(); gc_init(heap, heap + sizeof(heap)); mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash)); mp_obj_list_init(mp_sys_argv, 0); MP_STATE_PORT(term_obj) = MP_OBJ_NULL; MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL; #if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA extern void esp_native_code_init(void); esp_native_code_init(); #endif pin_init0(); readline_init0(); dupterm_task_init(); #if MICROPY_MODULE_FROZEN pyexec_frozen_module("_boot.py"); pyexec_file("boot.py"); if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { pyexec_file("main.py"); } #endif }
static mp_obj_t translate(mp_obj_t words) { mp_uint_t len, outlen; const char *txt = mp_obj_str_get_data(words, &len); // Reciter truncates *output* at about 120 characters. // So to avoid that we must disallow any input that will exceed that. if (len > 80) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "text too long.")); } reciter_memory *mem = m_new(reciter_memory, 1); MP_STATE_PORT(speech_data) = mem; for (mp_uint_t i = 0; i < len; i++) { mem->input[i] = txt[i]; } mem->input[len] = '['; if (!TextToPhonemes(mem)) { MP_STATE_PORT(speech_data) = NULL; nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "could not parse input.")); } for (outlen = 0; outlen < 255; outlen++) { if (mem->input[outlen] == 155) { break; } } mp_obj_t res = mp_obj_new_str_of_type(&mp_type_str, (byte *)mem->input, outlen); // Prevent input becoming invisible to GC due to tail-call optimisation. MP_STATE_PORT(speech_data) = NULL; return res; }MP_DEFINE_CONST_FUN_OBJ_1(translate_obj, translate);
// function usocket.getaddrinfo(host, port) /// \function getaddrinfo(host, port) STATIC mp_obj_t mod_usocket_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { mp_uint_t hlen; const char *host = mp_obj_str_get_data(host_in, &hlen); mp_int_t port = mp_obj_get_int(port_in); // find a NIC that can do a name lookup for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); if (nic_type->gethostbyname != NULL) { // Only IPv4 is supported uint8_t out_ip[MOD_NETWORK_IPV4ADDR_BUF_SIZE]; int32_t result = nic_type->gethostbyname(nic, host, hlen, out_ip, AF_INET); if (result != 0) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(result))); } mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL); tuple->items[0] = MP_OBJ_NEW_SMALL_INT(AF_INET); tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM); tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_); tuple->items[4] = mod_network_format_inet_addr(out_ip, port); return mp_obj_new_list(1, (mp_obj_t*)&tuple); } } nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); }
void mp_irq_disable_all (void) { // re-enable all active callback objects one by one for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) { mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i])); callback_obj->methods->disable(callback_obj->parent); } }
static int call_dupterm_read(void) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { mp_obj_t read_m[3]; mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_read, read_m); read_m[2] = MP_OBJ_NEW_SMALL_INT(1); mp_obj_t res = mp_call_method_n_kw(1, 0, read_m); if (res == mp_const_none) { return -2; } mp_buffer_info_t bufinfo; mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ); if (bufinfo.len == 0) { mp_printf(&mp_plat_print, "dupterm: EOF received, deactivating\n"); MP_STATE_PORT(term_obj) = NULL; return -1; } nlr_pop(); return *(byte*)bufinfo.buf; } else { // Temporarily disable dupterm to avoid infinite recursion mp_obj_t save_term = MP_STATE_PORT(term_obj); MP_STATE_PORT(term_obj) = NULL; mp_printf(&mp_plat_print, "dupterm: "); mp_obj_print_exception(&mp_plat_print, nlr.ret_val); MP_STATE_PORT(term_obj) = save_term; } return -1; }
STATIC void mp_reset(void) { mp_stack_set_top((void*)0x40000000); mp_stack_set_limit(8192); mp_hal_init(); gc_init(heap, heap + sizeof(heap)); mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_)); mp_obj_list_init(mp_sys_argv, 0); #if MICROPY_VFS_FAT memset(MP_STATE_PORT(fs_user_mount), 0, sizeof(MP_STATE_PORT(fs_user_mount))); #endif MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt); MP_STATE_PORT(term_obj) = MP_OBJ_NULL; MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL; pin_init0(); readline_init0(); dupterm_task_init(); #if MICROPY_MODULE_FROZEN pyexec_frozen_module("_boot.py"); pyexec_file("boot.py"); pyexec_file("main.py"); #endif }
/// \classmethod dict([dict]) /// Get or set the pin mapper dictionary. STATIC mp_obj_t pin_map_dict(size_t n_args, const mp_obj_t *args) { if (n_args > 1) { MP_STATE_PORT(pin_class_map_dict) = args[1]; return mp_const_none; } return MP_STATE_PORT(pin_class_map_dict); }
// "path" is the path to lookup; will advance this pointer beyond the volume name. // Returns logical drive number (-1 means invalid path). int ff_get_ldnumber (const TCHAR **path) { if (!(*path)) { return -1; } if (**path != '/') { #if _FS_RPATH return ff_CurrVol; #else return -1; #endif } if (check_path(path, "/flash", 6)) { return PD_FLASH; } else { for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); if (check_path(path, mount_obj->path, mount_obj->pathlen)) { return mount_obj->vol; } } } return -1; }
mp_obj_t microbit_display_show_func(mp_uint_t n_args, const mp_obj_t *args) { // TODO: Support async mode. microbit_display_obj_t *self = (microbit_display_obj_t*)args[0]; // Cancel any animations. MP_STATE_PORT(async_data)[0] = NULL; MP_STATE_PORT(async_data)[1] = NULL; MP_STATE_PORT(async_data)[2] = NULL; if (MP_OBJ_IS_STR(args[1])) { // arg is a string object mp_uint_t len; const char *str = mp_obj_str_get_data(args[1], &len); if (len == 0) { // There are no chars; do nothing. } else if (len == 1) { // A single char; convert to an image and print that. microbit_display_show(self, microbit_image_for_char(str[0])); } else { mp_int_t delay; if (n_args == 3) { delay = mp_obj_get_int(args[2]); } else { delay = MICROBIT_DEFAULT_PRINT_SPEED; } microbit_display_animate(self, args[1], delay, false, false); } } else if (mp_obj_get_type(args[1]) == µbit_image_type) { microbit_display_show(self, (microbit_image_obj_t *)args[1]); } else { nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "expecting an image or a string.")); } return mp_const_none; }
static int call_dupterm_read(void) { if (MP_STATE_PORT(term_obj) == NULL) { return -1; } nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { mp_obj_t read_m[3]; mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_read, read_m); read_m[2] = MP_OBJ_NEW_SMALL_INT(1); mp_obj_t res = mp_call_method_n_kw(1, 0, read_m); if (res == mp_const_none) { return -2; } mp_buffer_info_t bufinfo; mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ); if (bufinfo.len == 0) { mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL); return -1; } nlr_pop(); return *(byte*)bufinfo.buf; } else { mp_uos_deactivate("dupterm: Exception in read() method, deactivating: ", nlr.ret_val); } return -1; }
DSTATUS disk_status ( BYTE pdrv /* Physical drive nmuber (0..) */ ) { switch (pdrv) { case PD_FLASH : // flash is ready return 0; #if MICROPY_HW_HAS_SDCARD case PD_SDCARD: // TODO return STA_PROTECT if SD card is read only return 0; #endif case PD_USER: if (MP_STATE_PORT(fs_user_mount) == NULL) { return STA_NODISK; } if (MP_STATE_PORT(fs_user_mount)->writeblocks[0] == MP_OBJ_NULL) { return STA_PROTECT; } return 0; } return STA_NOINIT; }
DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber (0..) */ ) { switch (pdrv) { case PD_FLASH: storage_init(); return 0; #if MICROPY_HW_HAS_SDCARD case PD_SDCARD: if (!sdcard_power_on()) { return STA_NODISK; } // TODO return STA_PROTECT if SD card is read only return 0; #endif case PD_USER: if (MP_STATE_PORT(fs_user_mount) == NULL) { return STA_NODISK; } if (MP_STATE_PORT(fs_user_mount)->writeblocks[0] == MP_OBJ_NULL) { return STA_PROTECT; } return 0; } return STA_NOINIT; }
DRESULT disk_ioctl ( BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ void *buff /* Buffer to send/receive control data */ ) { switch (pdrv) { case PD_FLASH: switch (cmd) { case CTRL_SYNC: storage_flush(); return RES_OK; case GET_BLOCK_SIZE: *((DWORD*)buff) = 1; // high-level sector erase size in units of the small (512) block size return RES_OK; } break; #if MICROPY_HW_HAS_SDCARD case PD_SDCARD: switch (cmd) { case CTRL_SYNC: return RES_OK; case GET_BLOCK_SIZE: *((DWORD*)buff) = 1; // high-level sector erase size in units of the small (512) block size return RES_OK; } break; #endif case PD_USER: if (MP_STATE_PORT(fs_user_mount) == NULL) { // nothing mounted return RES_ERROR; } switch (cmd) { case CTRL_SYNC: if (MP_STATE_PORT(fs_user_mount)->sync[0] != MP_OBJ_NULL) { mp_call_method_n_kw(0, 0, MP_STATE_PORT(fs_user_mount)->sync); } return RES_OK; case GET_BLOCK_SIZE: *((DWORD*)buff) = 1; // high-level sector erase size in units of the small (512) bl return RES_OK; case GET_SECTOR_COUNT: { mp_obj_t ret = mp_call_method_n_kw(0, 0, MP_STATE_PORT(fs_user_mount)->count); *((DWORD*)buff) = mp_obj_get_int(ret); return RES_OK; } } break; } return RES_PARERR; }
void usb_vcp_set_interrupt_char(int c) { if (pyb_usb_flags & PYB_USB_FLAG_DEV_ENABLED) { if (c != -1) { mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_const_vcp_interrupt)); } USBD_CDC_SetInterrupt(c, MP_STATE_PORT(mp_const_vcp_interrupt)); } }
void mp_hal_stdout_tx_strn(const char *str, size_t len) { if (MP_STATE_PORT(pyb_stdio_uart) != NULL) { uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len); } if (usb_vcp_is_enabled()) { usb_vcp_send_strn(str, len); } }
void can_deinit(void) { for (int i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(pyb_can_obj_all)); i++) { pyb_can_obj_t *can_obj = MP_STATE_PORT(pyb_can_obj_all)[i]; if (can_obj != NULL) { pyb_can_deinit(can_obj); } } }
mp_obj_t microbit_display_show_func(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { // Cancel any animations. MP_STATE_PORT(async_data)[0] = NULL; MP_STATE_PORT(async_data)[1] = NULL; static const mp_arg_t show_allowed_args[] = { { MP_QSTR_image, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
// unregister all interrupt sources void uart_deinit(void) { for (int i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(pyb_uart_obj_all)); i++) { pyb_uart_obj_t *uart_obj = MP_STATE_PORT(pyb_uart_obj_all)[i]; if (uart_obj != NULL) { pyb_uart_deinit(uart_obj); } } }
mp_irq_obj_t *mp_irq_find (mp_obj_t parent) { for (mp_uint_t i = 0; i < MP_STATE_PORT(mp_irq_obj_list).len; i++) { mp_irq_obj_t *callback_obj = ((mp_irq_obj_t *)(MP_STATE_PORT(mp_irq_obj_list).items[i])); if (callback_obj->parent == parent) { return callback_obj; } } return NULL; }
STATIC pyb_uart_obj_t* pyb_uart_find (pyb_uart_id_t uart_id) { for (mp_uint_t i = 0; i < MP_STATE_PORT(pyb_uart_list).len; i++) { pyb_uart_obj_t *self = (pyb_uart_obj_t *)MP_STATE_PORT(pyb_uart_list).items[i]; if (self->uart_id == uart_id) { return self; } } return NULL; }
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_uos_deactivate(const char *msg, mp_obj_t exc) { mp_obj_t term = MP_STATE_PORT(term_obj); MP_STATE_PORT(term_obj) = NULL; mp_printf(&mp_plat_print, msg); if (exc != MP_OBJ_NULL) { mp_obj_print_exception(&mp_plat_print, exc); } mp_stream_close(term); }
mp_obj_t mod_network_find_nic(const uint8_t *ip) { // find a NIC that is suited to given IP address for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; return nic; } nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); }
// unregister all interrupt sources void uart_deinit_all(void) { for (int i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(pyb_uart_obj_all)); i++) { pyb_uart_obj_t *uart_obj = MP_STATE_PORT(pyb_uart_obj_all)[i]; if (uart_obj != NULL && !uart_obj->is_static) { uart_deinit(uart_obj); MP_STATE_PORT(pyb_uart_obj_all)[i] = NULL; } } }
void mod_network_register_nic(mp_obj_t nic) { for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) { // nic already registered return; } } // nic not registered so add to list mp_obj_list_append(MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list)), nic); }
/****************************************************************************** DEFINE PRIVATE FUNCTIONS ******************************************************************************/ STATIC pyb_sleep_obj_t *pyb_sleep_find (mp_obj_t obj) { for (mp_uint_t i = 0; i < MP_STATE_PORT(pyb_sleep_obj_list).len; i++) { // search for the object and then remove it pyb_sleep_obj_t *sleep_obj = ((pyb_sleep_obj_t *)(MP_STATE_PORT(pyb_sleep_obj_list).items[i])); if (sleep_obj->obj == obj) { return sleep_obj; } } return NULL; }
mp_obj_t mod_network_find_nic(const uint8_t *ip) { // find a NIC that is suited to given IP address for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; // TODO check IP suitability here //mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); return nic; } nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available NIC")); }
STATIC void async_stop(void) { async_repeat_iterable = NULL; async_iterator = NULL; async_mode = ASYNC_MODE_STOPPED; async_tick = 0; async_delay = 1000; MP_STATE_PORT(async_data)[0] = NULL; MP_STATE_PORT(async_data)[1] = NULL; MP_STATE_PORT(async_data)[2] = NULL; wakeup_event = true; }
void mp_run(void) { int stack_dummy; stack_top = (char*)&stack_dummy; mp_stack_ctrl_init(); mp_stack_set_limit(1800); // stack is 2k // allocate the heap statically in the bss static uint32_t heap[9820 / 4]; gc_init(heap, (uint8_t*)heap + sizeof(heap)); /* // allocate the heap using system malloc extern void *malloc(int); void *mheap = malloc(2000); gc_init(mheap, (byte*)mheap + 2000); */ /* // allocate the heap statically (will clash with BLE) gc_init((void*)0x20000100, (void*)0x20002000); */ mp_init(); mp_hal_init(); readline_init0(); microbit_init(); if (APPENDED_SCRIPT->header[0] == 'M' && APPENDED_SCRIPT->header[1] == 'P') { // run appended script do_strn(APPENDED_SCRIPT->str, APPENDED_SCRIPT->len); } else if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { // from microbit import * mp_import_all(mp_import_name(MP_QSTR_microbit, mp_const_empty_tuple, MP_OBJ_NEW_SMALL_INT(0))); } for (;;) { if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { if (pyexec_raw_repl() != 0) { break; } } else { if (pyexec_friendly_repl() != 0) { break; } } } mp_hal_stdout_tx_str("soft reboot\r\n"); memset(&MP_STATE_PORT(async_data)[0], 0, sizeof(MP_STATE_PORT(async_data))); MP_STATE_PORT(async_music_data) = NULL; mp_deinit(); }