30ee7019ca
Fixes for stmhal USB mass storage, lwIP bindings and VFS regressions This release provides an important fix for the USB mass storage device in the stmhal port by implementing the SCSI SYNCHRONIZE_CACHE command, which is now require by some Operating Systems. There are also fixes for the lwIP bindings to improve non-blocking sockets and error codes. The VFS has some regressions fixed including the ability to statvfs the root. All changes are listed below. py core: - modbuiltins: add core-provided version of input() function - objstr: catch case of negative "maxsplit" arg to str.rsplit() - persistentcode: allow to compile with complex numbers disabled - objstr: allow to compile with obj-repr D, and unicode disabled - modsys: allow to compile with obj-repr D and PY_ATTRTUPLE disabled - provide mp_decode_uint_skip() to help reduce stack usage - makeqstrdefs.py: make script run correctly with Python 2.6 - objstringio: if created from immutable object, follow copy on write policy extmod: - modlwip: connect: for non-blocking mode, return EINPROGRESS - modlwip: fix error codes for duplicate calls to connect() - modlwip: accept: fix error code for non-blocking mode - vfs: allow to statvfs the root directory - vfs: allow "buffering" and "encoding" args to VFS's open() - modframebuf: fix signed/unsigned comparison pendantic warning lib: - libm: use isfinite instead of finitef, for C99 compatibility - utils/interrupt_char: remove support for KBD_EXCEPTION disabled tests: - basics/string_rsplit: add tests for negative "maxsplit" argument - float: convert "sys.exit()" to "raise SystemExit" - float/builtin_float_minmax: PEP8 fixes - basics: convert "sys.exit()" to "raise SystemExit" - convert remaining "sys.exit()" to "raise SystemExit" unix port: - convert to use core-provided version of built-in import() - Makefile: replace references to make with $(MAKE) windows port: - convert to use core-provided version of built-in import() qemu-arm port: - Makefile: adjust object-file lists to get correct dependencies - enable micropython.mem_*() functions to allow more tests stmhal port: - boards: enable DAC for NUCLEO_F767ZI board - add support for NUCLEO_F446RE board - pass USB handler as parameter to allow more than one USB handler - usb: use local USB handler variable in Start-of-Frame handler - usb: make state for USB device private to top-level USB driver - usbdev: for MSC implement SCSI SYNCHRONIZE_CACHE command - convert from using stmhal's input() to core provided version cc3200 port: - convert from using stmhal's input() to core provided version teensy port: - convert from using stmhal's input() to core provided version esp8266 port: - Makefile: replace references to make with $(MAKE) - Makefile: add clean-modules target - convert from using stmhal's input() to core provided version zephyr port: - modusocket: getaddrinfo: Fix mp_obj_len() usage - define MICROPY_PY_SYS_PLATFORM (to "zephyr") - machine_pin: use native Zephyr types for Zephyr API calls docs: - machine.Pin: remove out_value() method - machine.Pin: add on() and off() methods - esp8266: consistently replace Pin.high/low methods with .on/off - esp8266/quickref: polish Pin.on()/off() examples - network: move confusingly-named cc3200 Server class to its reference - uos: deconditionalize, remove minor port-specific details - uos: move cc3200 port legacy VFS mounting functions to its ref doc - machine: sort machine classes in logical order, not alphabetically - network: first step to describe standard network class interface examples: - embedding: use core-provided KeyboardInterrupt object
233 lines
7.7 KiB
C
233 lines
7.7 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2014 Damien P. George
|
|
* Copyright (c) 2016-2017 Paul Sokolovsky
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "py/nlr.h"
|
|
#include "py/objlist.h"
|
|
#include "py/runtime0.h"
|
|
#include "py/runtime.h"
|
|
#include "py/smallint.h"
|
|
|
|
#if MICROPY_PY_UTIMEQ
|
|
|
|
#define MODULO MICROPY_PY_UTIME_TICKS_PERIOD
|
|
|
|
#define DEBUG 0
|
|
|
|
// the algorithm here is modelled on CPython's heapq.py
|
|
|
|
struct qentry {
|
|
mp_uint_t time;
|
|
mp_uint_t id;
|
|
mp_obj_t callback;
|
|
mp_obj_t args;
|
|
};
|
|
|
|
typedef struct _mp_obj_utimeq_t {
|
|
mp_obj_base_t base;
|
|
mp_uint_t alloc;
|
|
mp_uint_t len;
|
|
struct qentry items[];
|
|
} mp_obj_utimeq_t;
|
|
|
|
STATIC mp_uint_t utimeq_id;
|
|
|
|
STATIC mp_obj_utimeq_t *get_heap(mp_obj_t heap_in) {
|
|
return MP_OBJ_TO_PTR(heap_in);
|
|
}
|
|
|
|
STATIC bool time_less_than(struct qentry *item, struct qentry *parent) {
|
|
mp_uint_t item_tm = item->time;
|
|
mp_uint_t parent_tm = parent->time;
|
|
mp_uint_t res = parent_tm - item_tm;
|
|
if (res == 0) {
|
|
// TODO: This actually should use the same "ring" logic
|
|
// as for time, to avoid artifacts when id's overflow.
|
|
return item->id < parent->id;
|
|
}
|
|
if ((mp_int_t)res < 0) {
|
|
res += MODULO;
|
|
}
|
|
return res && res < (MODULO / 2);
|
|
}
|
|
|
|
STATIC mp_obj_t utimeq_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
|
mp_uint_t alloc = mp_obj_get_int(args[0]);
|
|
mp_obj_utimeq_t *o = m_new_obj_var(mp_obj_utimeq_t, struct qentry, alloc);
|
|
o->base.type = type;
|
|
memset(o->items, 0, sizeof(*o->items) * alloc);
|
|
o->alloc = alloc;
|
|
o->len = 0;
|
|
return MP_OBJ_FROM_PTR(o);
|
|
}
|
|
|
|
STATIC void heap_siftdown(mp_obj_utimeq_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
|
|
struct qentry item = heap->items[pos];
|
|
while (pos > start_pos) {
|
|
mp_uint_t parent_pos = (pos - 1) >> 1;
|
|
struct qentry *parent = &heap->items[parent_pos];
|
|
bool lessthan = time_less_than(&item, parent);
|
|
if (lessthan) {
|
|
heap->items[pos] = *parent;
|
|
pos = parent_pos;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
heap->items[pos] = item;
|
|
}
|
|
|
|
STATIC void heap_siftup(mp_obj_utimeq_t *heap, mp_uint_t pos) {
|
|
mp_uint_t start_pos = pos;
|
|
mp_uint_t end_pos = heap->len;
|
|
struct qentry item = heap->items[pos];
|
|
for (mp_uint_t child_pos = 2 * pos + 1; child_pos < end_pos; child_pos = 2 * pos + 1) {
|
|
// choose right child if it's <= left child
|
|
if (child_pos + 1 < end_pos) {
|
|
bool lessthan = time_less_than(&heap->items[child_pos], &heap->items[child_pos + 1]);
|
|
if (!lessthan) {
|
|
child_pos += 1;
|
|
}
|
|
}
|
|
// bubble up the smaller child
|
|
heap->items[pos] = heap->items[child_pos];
|
|
pos = child_pos;
|
|
}
|
|
heap->items[pos] = item;
|
|
heap_siftdown(heap, start_pos, pos);
|
|
}
|
|
|
|
STATIC mp_obj_t mod_utimeq_heappush(size_t n_args, const mp_obj_t *args) {
|
|
(void)n_args;
|
|
mp_obj_t heap_in = args[0];
|
|
mp_obj_utimeq_t *heap = get_heap(heap_in);
|
|
if (heap->len == heap->alloc) {
|
|
mp_raise_IndexError("queue overflow");
|
|
}
|
|
mp_uint_t l = heap->len;
|
|
heap->items[l].time = MP_OBJ_SMALL_INT_VALUE(args[1]);
|
|
heap->items[l].id = utimeq_id++;
|
|
heap->items[l].callback = args[2];
|
|
heap->items[l].args = args[3];
|
|
heap_siftdown(heap, 0, heap->len);
|
|
heap->len++;
|
|
return mp_const_none;
|
|
}
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_utimeq_heappush_obj, 4, 4, mod_utimeq_heappush);
|
|
|
|
STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) {
|
|
mp_obj_utimeq_t *heap = get_heap(heap_in);
|
|
if (heap->len == 0) {
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
|
|
}
|
|
mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref);
|
|
if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 3) {
|
|
mp_raise_TypeError("");
|
|
}
|
|
|
|
struct qentry *item = &heap->items[0];
|
|
ret->items[0] = MP_OBJ_NEW_SMALL_INT(item->time);
|
|
ret->items[1] = item->callback;
|
|
ret->items[2] = item->args;
|
|
heap->len -= 1;
|
|
heap->items[0] = heap->items[heap->len];
|
|
heap->items[heap->len].callback = MP_OBJ_NULL; // so we don't retain a pointer
|
|
heap->items[heap->len].args = MP_OBJ_NULL;
|
|
if (heap->len) {
|
|
heap_siftup(heap, 0);
|
|
}
|
|
return mp_const_none;
|
|
}
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop);
|
|
|
|
STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) {
|
|
mp_obj_utimeq_t *heap = get_heap(heap_in);
|
|
if (heap->len == 0) {
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
|
|
}
|
|
|
|
struct qentry *item = &heap->items[0];
|
|
return MP_OBJ_NEW_SMALL_INT(item->time);
|
|
}
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_peektime_obj, mod_utimeq_peektime);
|
|
|
|
#if DEBUG
|
|
STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) {
|
|
mp_obj_utimeq_t *heap = get_heap(heap_in);
|
|
for (int i = 0; i < heap->len; i++) {
|
|
printf(UINT_FMT "\t%p\t%p\n", heap->items[i].time,
|
|
MP_OBJ_TO_PTR(heap->items[i].callback), MP_OBJ_TO_PTR(heap->items[i].args));
|
|
}
|
|
return mp_const_none;
|
|
}
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_dump_obj, mod_utimeq_dump);
|
|
#endif
|
|
|
|
STATIC mp_obj_t utimeq_unary_op(mp_uint_t op, mp_obj_t self_in) {
|
|
mp_obj_utimeq_t *self = MP_OBJ_TO_PTR(self_in);
|
|
switch (op) {
|
|
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
|
|
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
|
|
default: return MP_OBJ_NULL; // op not supported
|
|
}
|
|
}
|
|
|
|
STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = {
|
|
{ MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&mod_utimeq_heappush_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&mod_utimeq_heappop_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_peektime), MP_ROM_PTR(&mod_utimeq_peektime_obj) },
|
|
#if DEBUG
|
|
{ MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_utimeq_dump_obj) },
|
|
#endif
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(utimeq_locals_dict, utimeq_locals_dict_table);
|
|
|
|
STATIC const mp_obj_type_t utimeq_type = {
|
|
{ &mp_type_type },
|
|
.name = MP_QSTR_utimeq,
|
|
.make_new = utimeq_make_new,
|
|
.unary_op = utimeq_unary_op,
|
|
.locals_dict = (void*)&utimeq_locals_dict,
|
|
};
|
|
|
|
STATIC const mp_rom_map_elem_t mp_module_utimeq_globals_table[] = {
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utimeq) },
|
|
{ MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&utimeq_type) },
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_utimeq_globals, mp_module_utimeq_globals_table);
|
|
|
|
const mp_obj_module_t mp_module_utimeq = {
|
|
.base = { &mp_type_module },
|
|
.globals = (mp_obj_dict_t*)&mp_module_utimeq_globals,
|
|
};
|
|
|
|
#endif //MICROPY_PY_UTIMEQ
|