diff options
29 files changed, 826 insertions, 57 deletions
@@ -132,28 +132,45 @@ void mp_set_init(mp_set_t *set, int n) { set->table = m_new0(mp_obj_t, set->alloc); } -mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) { - int hash = mp_obj_hash(index); - assert(set->alloc); /* FIXME: if alloc is ever 0 when doing a lookup, this'll fail: */ - int pos = hash % set->alloc; +static void mp_set_rehash(mp_set_t *set) { + int old_alloc = set->alloc; + mp_obj_t *old_table = set->table; + set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1); + set->used = 0; + set->table = m_new0(mp_obj_t, set->alloc); + for (int i = 0; i < old_alloc; i++) { + if (old_table[i] != NULL) { + mp_set_lookup(set, old_table[i], true); + } + } + m_del(mp_obj_t, old_table, old_alloc); +} + +mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) { + int hash; + int pos; + if (set->alloc == 0) { + if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) { + mp_set_rehash(set); + } else { + return NULL; + } + } + if (lookup_kind & MP_MAP_LOOKUP_FIRST) { + hash = 0; + pos = 0; + } else { + hash = mp_obj_hash(index);; + pos = hash % set->alloc; + } for (;;) { mp_obj_t elem = set->table[pos]; if (elem == MP_OBJ_NULL) { // not in table - if (add_if_not_found) { + if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) { if (set->used + 1 >= set->alloc) { // not enough room in table, rehash it - int old_alloc = set->alloc; - mp_obj_t *old_table = set->table; - set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1); - set->used = 0; - set->table = m_new(mp_obj_t, set->alloc); - for (int i = 0; i < old_alloc; i++) { - if (old_table[i] != NULL) { - mp_set_lookup(set, old_table[i], true); - } - } - m_del(mp_obj_t, old_table, old_alloc); + mp_set_rehash(set); // restart the search for the new element pos = hash % set->alloc; } else { @@ -161,11 +178,17 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) { set->table[pos] = index; return index; } + } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) { + pos++; } else { return MP_OBJ_NULL; } - } else if (mp_obj_equal(elem, index)) { + } else if (lookup_kind & MP_MAP_LOOKUP_FIRST || mp_obj_equal(elem, index)) { // found it + if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) { + set->used--; + set->table[pos] = NULL; + } return elem; } else { // not yet found, keep searching in this table @@ -173,3 +196,13 @@ mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found) { } } } + +void mp_set_clear(mp_set_t *set) { + set->used = 0; + machine_uint_t a = set->alloc; + set->alloc = 0; + set->table = m_renew(mp_obj_t, set->table, a, set->alloc); + for (uint i=0; i<set->alloc; i++) { + set->table[i] = NULL; + } +} @@ -19,9 +19,10 @@ typedef struct _mp_set_t { } mp_set_t; typedef enum _mp_map_lookup_kind_t { - MP_MAP_LOOKUP, - MP_MAP_LOOKUP_ADD_IF_NOT_FOUND, - MP_MAP_LOOKUP_REMOVE_IF_FOUND, + MP_MAP_LOOKUP, // 0 + MP_MAP_LOOKUP_ADD_IF_NOT_FOUND, // 1 + MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2 + MP_MAP_LOOKUP_FIRST = 4, } mp_map_lookup_kind_t; int get_doubling_prime_greater_or_equal_to(int x); @@ -31,4 +32,5 @@ mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t void mp_map_clear(mp_map_t *map); void mp_set_init(mp_set_t *set, int n); -mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, bool add_if_not_found); +mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind); +void mp_set_clear(mp_set_t *set); diff --git a/py/objset.c b/py/objset.c index 67dab11dfb..e41f2c47f4 100644 --- a/py/objset.c +++ b/py/objset.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <stdint.h> +#include <string.h> #include <assert.h> #include "nlr.h" @@ -8,6 +9,7 @@ #include "mpqstr.h" #include "obj.h" #include "runtime.h" +#include "runtime0.h" #include "map.h" typedef struct _mp_obj_set_t { @@ -15,8 +17,20 @@ typedef struct _mp_obj_set_t { mp_set_t set; } mp_obj_set_t; +typedef struct _mp_obj_set_it_t { + mp_obj_base_t base; + mp_obj_set_t *set; + machine_uint_t cur; +} mp_obj_set_it_t; + +static mp_obj_t set_it_iternext(mp_obj_t self_in); + void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { mp_obj_set_t *self = self_in; + if (self->set.used == 0) { + print(env, "set()"); + return; + } bool first = true; print(env, "{"); for (int i = 0; i < self->set.alloc; i++) { @@ -54,11 +68,382 @@ static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) } } +const mp_obj_type_t set_it_type = { + { &mp_const_type }, + "set_iterator", + .iternext = set_it_iternext, +}; + +static mp_obj_t set_it_iternext(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_it_type)); + mp_obj_set_it_t *self = self_in; + machine_uint_t max = self->set->set.alloc; + mp_obj_t *table = self->set->set.table; + + for (machine_uint_t i = self->cur; i < max; i++) { + if (table[i] != NULL) { + self->cur = i + 1; + return table[i]; + } + } + + return mp_const_stop_iteration; +} + +static mp_obj_t set_getiter(mp_obj_t set_in) { + mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t); + o->base.type = &set_it_type; + o->set = (mp_obj_set_t *)set_in; + o->cur = 0; + return o; +} + + +/******************************************************************************/ +/* set methods */ + +static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add); + +static mp_obj_t set_clear(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + mp_set_clear(&self->set); + + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear); + +static mp_obj_t set_copy(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + mp_obj_set_t *other = m_new_obj(mp_obj_set_t); + other->base.type = &set_type; + mp_set_init(&other->set, self->set.alloc - 1); + other->set.used = self->set.used; + memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t)); + + return other; +} +static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy); + +static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard); + +static mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) { + assert(n_args > 0); + assert(MP_OBJ_IS_TYPE(args[0], &set_type)); + mp_obj_set_t *self; + if (update) { + self = args[0]; + } else { + self = set_copy(args[0]); + } + + + for (int i = 1; i < n_args; i++) { + mp_obj_t other = args[i]; + if (self == other) { + set_clear(self); + } else { + mp_obj_t iter = rt_getiter(other); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + set_discard(self, next); + } + } + } + + return self; +} + +static mp_obj_t set_diff(int n_args, const mp_obj_t *args) { + return set_diff_int(n_args, args, false); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff); + +static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) { + set_diff_int(n_args, args, true); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update); + +static mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + if (self_in == other) { + return update ? mp_const_none : set_copy(self_in); + } + + mp_obj_set_t *self = self_in; + mp_obj_set_t *out = mp_obj_new_set(0, NULL); + + mp_obj_t iter = rt_getiter(other); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { + set_add(out, next); + } + } + + if (update) { + m_del(mp_obj_t, self->set.table, self->set.alloc); + self->set.alloc = out->set.alloc; + self->set.used = out->set.used; + self->set.table = out->set.table; + } + + return update ? mp_const_none : out; +} + +static mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) { + return set_intersect_int(self_in, other, false); +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect); + +static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) { + return set_intersect_int(self_in, other, true); +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update); + +static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + mp_obj_t iter = rt_getiter(other); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { + return mp_const_false; + } + } + return mp_const_true; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint); + +static mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) { + mp_obj_set_t *self; + bool cleanup_self = false; + if (MP_OBJ_IS_TYPE(self_in, &set_type)) { + self = self_in; + } else { + self = set_make_new(NULL, 1, &self_in); + cleanup_self = true; + } + + mp_obj_set_t *other; + bool cleanup_other = false; + if (MP_OBJ_IS_TYPE(other_in, &set_type)) { + other = other_in; + } else { + other = set_make_new(NULL, 1, &other_in); + cleanup_other = true; + } + bool out = true; + if (proper && self->set.used == other->set.used) { + out = false; + } else { + mp_obj_t iter = set_getiter(self); + mp_obj_t next; + while ((next = set_it_iternext(iter)) != mp_const_stop_iteration) { + if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) { + out = false; + break; + } + } + } + if (cleanup_self) { + set_clear(self); + } + if (cleanup_other) { + set_clear(other); + } + return MP_BOOL(out); +} +static mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset_internal(self_in, other_in, false); +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset); + +static mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset_internal(self_in, other_in, true); +} + +static mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset_internal(other_in, self_in, false); +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset); + +static mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) { + return set_issubset_internal(other_in, self_in, true); +} + +static mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + if (!MP_OBJ_IS_TYPE(other_in, &set_type)) { + return mp_const_false; + } + mp_obj_set_t *other = other_in; + if (self->set.used != other->set.used) { + return mp_const_false; + } + return set_issubset(self_in, other_in); +} + +static mp_obj_t set_pop(mp_obj_t self_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + + if (self->set.used == 0) { + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "pop from an empty set")); + } + mp_obj_t obj = mp_set_lookup(&self->set, NULL, + MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST); + return obj; +} +static MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop); + +static mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) { + nlr_jump(mp_obj_new_exception(MP_QSTR_KeyError)); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove); + +static mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = self_in; + mp_obj_t iter = rt_getiter(other_in); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update); + +static mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + self_in = set_copy(self_in); + set_symmetric_difference_update(self_in, other_in); + return self_in; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference); + +static void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) { + mp_obj_t iter = rt_getiter(other_in); + mp_obj_t next; + while ((next = rt_iternext(iter)) != mp_const_stop_iteration) { + mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); + } +} + +static mp_obj_t set_update(int n_args, const mp_obj_t *args) { + assert(n_args > 0); + assert(MP_OBJ_IS_TYPE(args[0], &set_type)); + + for (int i = 1; i < n_args; i++) { + set_update_int(args[0], args[i]); + } + + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update); + +static mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) { + assert(MP_OBJ_IS_TYPE(self_in, &set_type)); + mp_obj_set_t *self = set_copy(self_in); + set_update_int(self, other_in); + return self; +} +static MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union); + + +static mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { + mp_obj_t args[] = {lhs, rhs}; + switch (op) { + case RT_BINARY_OP_OR: + return set_union(lhs, rhs); + case RT_BINARY_OP_XOR: + return set_symmetric_difference(lhs, rhs); + case RT_BINARY_OP_AND: + return set_intersect(lhs, rhs); + case RT_BINARY_OP_SUBTRACT: + return set_diff(2, args); + case RT_BINARY_OP_INPLACE_OR: + return set_union(lhs, rhs); + case RT_BINARY_OP_INPLACE_XOR: + return set_symmetric_difference(lhs, rhs); + case RT_BINARY_OP_INPLACE_AND: + return set_intersect(lhs, rhs); + case RT_BINARY_OP_INPLACE_SUBTRACT: + return set_diff(2, args); + case RT_COMPARE_OP_LESS: + return set_issubset_proper(lhs, rhs); + case RT_COMPARE_OP_MORE: + return set_issuperset_proper(lhs, rhs); + case RT_COMPARE_OP_EQUAL: + return set_equal(lhs, rhs); + case RT_COMPARE_OP_LESS_EQUAL: + return set_issubset(lhs, rhs); + case RT_COMPARE_OP_MORE_EQUAL: + return set_issuperset(lhs, rhs); + case RT_COMPARE_OP_NOT_EQUAL: + return MP_BOOL(set_equal(lhs, rhs) == mp_const_false); + default: + // op not supported + return NULL; + } +} + +/******************************************************************************/ +/* set constructors & public C API */ + + +static const mp_method_t set_type_methods[] = { + { "add", &set_add_obj }, + { "clear", &set_clear_obj }, + { "copy", &set_copy_obj }, + { "discard", &set_discard_obj }, + { "difference", &set_diff_obj }, + { "difference_update", &set_diff_update_obj }, + { "intersection", &set_intersect_obj }, + { "intersection_update", &set_intersect_update_obj }, + { "isdisjoint", &set_isdisjoint_obj }, + { "issubset", &set_issubset_obj }, + { "issuperset", &set_issuperset_obj }, + { "pop", &set_pop_obj }, + { "remove", &set_remove_obj }, + { "symmetric_difference", &set_symmetric_difference_obj }, + { "symmetric_difference_update", &set_symmetric_difference_update_obj }, + { "union", &set_union_obj }, + { "update", &set_update_obj }, + { NULL, NULL }, // end-of-list sentinel +}; + const mp_obj_type_t set_type = { { &mp_const_type }, "set", .print = set_print, .make_new = set_make_new, + .binary_op = set_binary_op, + .getiter = set_getiter, + .methods = set_type_methods, }; mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { @@ -66,7 +451,7 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { o->base.type = &set_type; mp_set_init(&o->set, n_args); for (int i = 0; i < n_args; i++) { - mp_set_lookup(&o->set, items[i], true); + mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); } return o; } @@ -74,5 +459,5 @@ mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) { assert(MP_OBJ_IS_TYPE(self_in, &set_type)); mp_obj_set_t *self = self_in; - mp_set_lookup(&self->set, item, true); + mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); } diff --git a/stm/Makefile b/stm/Makefile index 2fecd03bad..2facff81ce 100644 --- a/stm/Makefile +++ b/stm/Makefile @@ -105,6 +105,14 @@ SRC_STM = \ # usb_hcd.c \ # usb_hcd_int.c \ # usb_otg.c \ +# usbh_core.c \ +# usbh_hcs.c \ +# usbh_stdreq.c \ +# usbh_ioreq.c \ +# usbh_usr.c \ +# usbh_hid_core.c \ +# usbh_hid_mouse.c \ +# usbh_hid_keybd.c \ SRC_CC3K = \ cc3000_common.c \ diff --git a/stm/lib/usb_bsp.c b/stm/lib/usb_bsp.c index c4839bdf96..e88e31db0e 100644 --- a/stm/lib/usb_bsp.c +++ b/stm/lib/usb_bsp.c @@ -117,6 +117,17 @@ void USB_OTG_BSP_Init(USB_OTG_CORE_HANDLE *pdev) { GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
+ /*
+ // Configure ID pin (only in host mode)
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
+ GPIO_Init(GPIOA, &GPIO_InitStructure);
+ GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_OTG_FS);
+ */
+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE);
}
@@ -138,6 +149,84 @@ void USB_OTG_BSP_EnableInterrupt(USB_OTG_CORE_HANDLE *pdev) { }
/**
+* @brief BSP_Drive_VBUS
+* Drives the Vbus signal through IO
+* @param state : VBUS states
+* @retval None
+*/
+
+void USB_OTG_BSP_DriveVBUS(USB_OTG_CORE_HANDLE *pdev, uint8_t state) {
+ //printf("DriveVBUS %p %u\n", pdev, state);
+ /*
+ On-chip 5 V VBUS generation is not supported. For this reason, a charge pump
+ or, if 5 V are available on the application board, a basic power switch, must
+ be added externally to drive the 5 V VBUS line. The external charge pump can
+ be driven by any GPIO output. When the application decides to power on VBUS
+ using the chosen GPIO, it must also set the port power bit in the host port
+ control and status register (PPWR bit in OTG_FS_HPRT).
+
+ Bit 12 PPWR: Port power
+ The application uses this field to control power to this port, and the core
+ clears this bit on an overcurrent condition.
+ */
+#if 0 // not implemented
+#ifndef USE_USB_OTG_HS
+ if (0 == state) {
+ /* DISABLE is needed on output of the Power Switch */
+ GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
+ } else {
+ /*ENABLE the Power Switch by driving the Enable LOW */
+ GPIO_ResetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
+ }
+#endif
+#endif
+}
+
+/**
+ * @brief USB_OTG_BSP_ConfigVBUS
+ * Configures the IO for the Vbus and OverCurrent
+ * @param None
+ * @retval None
+ */
+
+void USB_OTG_BSP_ConfigVBUS(USB_OTG_CORE_HANDLE *pdev) {
+ //printf("ConfigVBUS %p\n", pdev);
+#if 0 // not implemented
+#ifdef USE_USB_OTG_FS
+ GPIO_InitTypeDef GPIO_InitStructure;
+
+#ifdef USE_STM3210C_EVAL
+ RCC_APB2PeriphClockCmd(HOST_POWERSW_PORT_RCC, ENABLE);
+
+
+ /* Configure Power Switch Vbus Pin */
+ GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
+ GPIO_Init(HOST_POWERSW_PORT, &GPIO_InitStructure);
+#else
+ #ifdef USE_USB_OTG_FS
+ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOH , ENABLE);
+
+ GPIO_InitStructure.GPIO_Pin = HOST_POWERSW_VBUS;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
+ GPIO_Init(HOST_POWERSW_PORT,&GPIO_InitStructure);
+ #endif
+#endif
+
+ /* By Default, DISABLE is needed on output of the Power Switch */
+ GPIO_SetBits(HOST_POWERSW_PORT, HOST_POWERSW_VBUS);
+
+ USB_OTG_BSP_mDelay(200); /* Delay is need for stabilising the Vbus Low
+ in Reset Condition, when Vbus=1 and Reset-button is pressed by user */
+#endif
+#endif
+}
+
+/**
* @brief USB_OTG_BSP_uDelay
* This function provides delay time in micro sec
* @param usec : Value of delay required in micro sec
diff --git a/stm/lib/usb_conf.h b/stm/lib/usb_conf.h index 5e736f91e2..a51a23a84e 100644 --- a/stm/lib/usb_conf.h +++ b/stm/lib/usb_conf.h @@ -181,10 +181,65 @@ /****************** USB OTG MISC CONFIGURATION ********************************/
#define VBUS_SENSING_ENABLED
+/* BEGIN host specific stuff */
+
+/*******************************************************************************
+* FIFO Size Configuration in Host mode
+*
+* (i) Receive data FIFO size = (Largest Packet Size / 4) + 1 or
+* 2x (Largest Packet Size / 4) + 1, If a
+* high-bandwidth channel or multiple isochronous
+* channels are enabled
+*
+* (ii) For the host nonperiodic Transmit FIFO is the largest maximum packet size
+* for all supported nonperiodic OUT channels. Typically, a space
+* corresponding to two Largest Packet Size is recommended.
+*
+* (iii) The minimum amount of RAM required for Host periodic Transmit FIFO is
+* the largest maximum packet size for all supported periodic OUT channels.
+* If there is at least one High Bandwidth Isochronous OUT endpoint,
+* then the space must be at least two times the maximum packet size for
+* that channel.
+*******************************************************************************/
+
+/****************** USB OTG HS CONFIGURATION (for host) ***********************/
+#ifdef USB_OTG_HS_CORE
+ #define RX_FIFO_HS_SIZE 512
+ #define TXH_NP_HS_FIFOSIZ 256
+ #define TXH_P_HS_FIFOSIZ 256
+
+// #define USB_OTG_HS_LOW_PWR_MGMT_SUPPORT
+// #define USB_OTG_HS_SOF_OUTPUT_ENABLED
+
+// #define USB_OTG_INTERNAL_VBUS_ENABLED
+#define USB_OTG_EXTERNAL_VBUS_ENABLED
+
+ #ifdef USE_ULPI_PHY
+ #define USB_OTG_ULPI_PHY_ENABLED
+ #endif
+ #ifdef USE_EMBEDDED_PHY
+ #define USB_OTG_EMBEDDED_PHY_ENABLED
+ #endif
+ #define USB_OTG_HS_INTERNAL_DMA_ENABLED
+// #define USB_OTG_HS_DEDICATED_EP1_ENABLED
+#endif
+
+/****************** USB OTG FS CONFIGURATION (for host) ***********************/
+#ifdef USB_OTG_FS_CORE
+ //#define RX_FIFO_FS_SIZE 128 // already defined for device (and it's the same)
+ #define TXH_NP_FS_FIFOSIZ 96
+ #define TXH_P_FS_FIFOSIZ 96
+
+// #define USB_OTG_FS_LOW_PWR_MGMT_SUPPORT
+// #define USB_OTG_FS_SOF_OUTPUT_ENABLED
+#endif
+
+/* END host specific stuff */
+
/****************** USB OTG MODE CONFIGURATION ********************************/
-//#define USE_HOST_MODE
+//#define USE_HOST_MODE // set in Makefile
#define USE_DEVICE_MODE
-//#define USE_OTG_MODE
+//#define USE_OTG_MODE // set in Makefile
#ifndef USB_OTG_FS_CORE
#ifndef USB_OTG_HS_CORE
diff --git a/stm/lib/usb_core.c b/stm/lib/usb_core.c index 4b28c085f2..b0cce88046 100644 --- a/stm/lib/usb_core.c +++ b/stm/lib/usb_core.c @@ -617,7 +617,7 @@ USB_OTG_STS USB_OTG_CoreInitHost(USB_OTG_CORE_HANDLE *pdev) USB_OTG_HCFG_TypeDef hcfg;
#ifdef USE_OTG_MODE
- USB_OTG_OTGCTL_TypeDef gotgctl;
+ USB_OTG_GOTGCTL_TypeDef gotgctl;
#endif
uint32_t i = 0;
diff --git a/stm/main.c b/stm/main.c index 44db47f13b..cdc4432c09 100644 --- a/stm/main.c +++ b/stm/main.c @@ -307,7 +307,9 @@ char *strdup(const char *str) { static const char *readline_hist[READLINE_HIST_SIZE] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; void stdout_tx_str(const char *str) { - //usart_tx_str(str); // disabled because usart is a Python object and we now need specify which USART port + if (pyb_usart_global_debug != PYB_USART_NONE) { + usart_tx_str(pyb_usart_global_debug, str); + } usb_vcp_send_str(str); } @@ -322,10 +324,10 @@ int readline(vstr_t *line, const char *prompt) { if (usb_vcp_rx_any() != 0) { c = usb_vcp_rx_get(); break; - } /*else if (usart_rx_any()) { // disabled because usart is a Python object and we now need specify which USART port - c = usart_rx_char(); + } else if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) { + c = usart_rx_char(pyb_usart_global_debug); break; - }*/ + } sys_tick_delay_ms(1); if (storage_needs_flush()) { storage_flush(); @@ -775,7 +777,9 @@ int main(void) { switch_init(); storage_init(); - //usart_init(); disabled while wi-fi is enabled; also disabled because now usart is a proper Python object + // uncomment these 2 lines if you want REPL on USART_6 (or another usart) as well as on USB VCP + //pyb_usart_global_debug = PYB_USART_6; + //usart_init(pyb_usart_global_debug, 115200); int first_soft_reset = true; @@ -937,6 +941,9 @@ soft_reset: // USB usb_init(); + // USB host; not working! + //pyb_usbh_init(); + // MMA if (first_soft_reset) { // init and reset address to zero diff --git a/stm/printf.c b/stm/printf.c index fa94269257..c0fa82e1b0 100644 --- a/stm/printf.c +++ b/stm/printf.c @@ -206,6 +206,21 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { case 'P': // ? chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'A', flags, width); break; + case 'g': + { + // This is a very hacky approach to printing floats. Micropython + // uses %g when using print, and I just wanted to see somthing + // usable. I expect that this will be replaced with something + // more appropriate. + char dot = '.'; + double d = va_arg(args, double); + int left = (int)d; + int right = (int)((d - (double)(int)d) * 1000000.0); + chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); + chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); + chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); + break; + } default: pfenv->print_strn(pfenv->data, fmt, 1); chrs += 1; @@ -220,14 +235,10 @@ void stdout_print_strn(void *data, const char *str, unsigned int len) { // send stdout to USART, USB CDC VCP, and LCD if nothing else bool any = false; - // TODO should have a setting for which USART port to send to -#if 0 // if 0'd out so that we're not calling functions with the wrong arguments - if (usart_is_enabled()) { - usart_tx_strn_cooked(str, len); + if (pyb_usart_global_debug != PYB_USART_NONE) { + usart_tx_strn_cooked(pyb_usart_global_debug, str, len); any = true; } -#endif - if (usb_vcp_is_enabled()) { usb_vcp_send_strn_cooked(str, len); any = true; diff --git a/stm/stm32fxxx_it.c b/stm/stm32fxxx_it.c index e254ccc899..c1a9a0702f 100644 --- a/stm/stm32fxxx_it.c +++ b/stm/stm32fxxx_it.c @@ -31,6 +31,7 @@ #include "stm32fxxx_it.h" #include "stm32f4xx_exti.h" #include "usb_core.h" +//#include "usb_hcd_int.h" // for usb host mode only //#include "usbd_core.h" //#include "usbd_cdc_core.h" @@ -197,7 +198,8 @@ void OTG_HS_IRQHandler(void) void OTG_FS_IRQHandler(void) #endif { - USBD_OTG_ISR_Handler (&USB_OTG_dev); + USBD_OTG_ISR_Handler (&USB_OTG_dev); // device mode + //USBH_OTG_ISR_Handler (&USB_OTG_dev); // host mode FIXME } #ifdef USB_OTG_HS_DEDICATED_EP1_ENABLED diff --git a/stm/usart.c b/stm/usart.c index bc5bfc7369..5f47ec788a 100644 --- a/stm/usart.c +++ b/stm/usart.c @@ -8,20 +8,14 @@ #include "obj.h" #include "usart.h" -static bool is_enabled; - -typedef enum { - PYB_USART_1 = 1, - PYB_USART_2 = 2, - PYB_USART_3 = 3, - PYB_USART_6 = 4, - PYB_USART_MAX = 4, -} pyb_usart_t; +pyb_usart_t pyb_usart_global_debug = PYB_USART_NONE; static USART_TypeDef *usart_get_base(pyb_usart_t usart_id) { USART_TypeDef *USARTx=NULL; switch (usart_id) { + case PYB_USART_NONE: + break; case PYB_USART_1: USARTx = USART1; break; @@ -52,6 +46,9 @@ void usart_init(pyb_usart_t usart_id, uint32_t baudrate) { void (*RCC_APBxPeriphClockCmd)(uint32_t, FunctionalState)=NULL; switch (usart_id) { + case PYB_USART_NONE: + return; + case PYB_USART_1: USARTx = USART1; @@ -128,16 +125,13 @@ void usart_init(pyb_usart_t usart_id, uint32_t baudrate) { USART_Cmd(USARTx, ENABLE); } -bool usart_is_enabled(void) { - return is_enabled; -} - -bool usart_rx_any(void) { - return USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == SET; +bool usart_rx_any(pyb_usart_t usart_id) { + USART_TypeDef *USARTx = usart_get_base(usart_id); + return USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == SET; } int usart_rx_char(pyb_usart_t usart_id) { - USART_TypeDef *USARTx= usart_get_base(usart_id); + USART_TypeDef *USARTx = usart_get_base(usart_id); return USART_ReceiveData(USARTx); } @@ -176,8 +170,8 @@ typedef struct _pyb_usart_obj_t { } pyb_usart_obj_t; static mp_obj_t usart_obj_status(mp_obj_t self_in) { - // TODO make it check the correct USART port! - if (usart_rx_any()) { + pyb_usart_obj_t *self = self_in; + if (usart_rx_any(self->usart_id)) { return mp_const_true; } else { return mp_const_false; diff --git a/stm/usart.h b/stm/usart.h index 15ed419fe7..541cb757c8 100644 --- a/stm/usart.h +++ b/stm/usart.h @@ -1 +1,18 @@ +typedef enum { + PYB_USART_NONE = 0, + PYB_USART_1 = 1, + PYB_USART_2 = 2, + PYB_USART_3 = 3, + PYB_USART_6 = 4, + PYB_USART_MAX = 4, +} pyb_usart_t; + +extern pyb_usart_t pyb_usart_global_debug; + +void usart_init(pyb_usart_t usart_id, uint32_t baudrate); +bool usart_rx_any(pyb_usart_t usart_id); +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); @@ -80,7 +80,7 @@ void usb_vcp_send_strn(const char *str, int len) { } } -#include "lib/usbd_conf.h" +#include "usbd_conf.h" /* These are external variables imported from CDC core to be used for IN transfer management. */ @@ -105,3 +105,30 @@ void usb_vcp_send_strn_cooked(const char *str, int len) { void usb_hid_send_report(uint8_t *buf) { USBD_HID_SendReport(&USB_OTG_dev, buf, 4); } + +/******************************************************************************/ +// code for experimental USB OTG support + +#ifdef USE_HOST_MODE + +#include "lib-otg/usbh_core.h" +#include "lib-otg/usbh_usr.h" +#include "lib-otg/usbh_hid_core.h" + +__ALIGN_BEGIN USBH_HOST USB_Host __ALIGN_END ; + +static int host_is_enabled = 0; +void pyb_usbh_init(void) { + if (!host_is_enabled) { + // only init USBH once in the device's power-lifetime + /* Init Host Library */ + USBH_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USB_Host, &HID_cb, &USR_Callbacks); + } + host_is_enabled = 1; +} + +void pyb_usbh_process(void) { + USBH_Process(&USB_OTG_dev, &USB_Host); +} + +#endif // USE_HOST_MODE @@ -6,3 +6,6 @@ void usb_vcp_send_str(const char* str); void usb_vcp_send_strn(const char* str, int len); void usb_vcp_send_strn_cooked(const char *str, int len); void usb_hid_send_report(uint8_t *buf); // 4 bytes for mouse: ?, x, y, ? + +void pyb_usbh_init(void); +void pyb_usbh_process(void); diff --git a/tests/basics/tests/set_add.py b/tests/basics/tests/set_add.py new file mode 100644 index 0000000000..f2a372f307 --- /dev/null +++ b/tests/basics/tests/set_add.py @@ -0,0 +1,5 @@ +s = {1, 2, 3, 4} +print(s.add(5)) +l = list(s) +l.sort() +print(l) diff --git a/tests/basics/tests/set_binop.py b/tests/basics/tests/set_binop.py new file mode 100644 index 0000000000..d0d0b8027b --- /dev/null +++ b/tests/basics/tests/set_binop.py @@ -0,0 +1,30 @@ +def r(s): + l = list(s) + l.sort() + return l +sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}] +for s in sets: + for t in sets: + print(s, '|', t, '=', r(s | t)) + print(s, '^', t, '=', r(s ^ t)) + print(s, '&', t, '=', r(s & t)) + print(s, '-', t, '=', r(s - t)) + u = s.copy() + u |= t + print(s, "|=", t, '-->', r(u)) + u = s.copy() + u ^= t + print(s, "^=", t, '-->', r(u)) + u = s.copy() + u &= t + print(s, "&=", t, "-->", r(u)) + u = s.copy() + u -= t + print(s, "-=", t, "-->", r(u)) + + print(s, '==', t, '=', s == t) + print(s, '!=', t, '=', s != t) + print(s, '>', t, '=', s > t) + print(s, '>=', t, '=', s >= t) + print(s, '<', t, '=', s < t) + print(s, '<=', t, '=', s <= t) diff --git a/tests/basics/tests/set_clear.py b/tests/basics/tests/set_clear.py new file mode 100644 index 0000000000..6fda93f0fb --- /dev/null +++ b/tests/basics/tests/set_clear.py @@ -0,0 +1,3 @@ +s = {1, 2, 3, 4} +print(s.clear()) +print(list(s)) diff --git a/tests/basics/tests/set_copy.py b/tests/basics/tests/set_copy.py new file mode 100644 index 0000000000..2ea308b0db --- /dev/null +++ b/tests/basics/tests/set_copy.py @@ -0,0 +1,8 @@ +s = {1, 2, 3, 4} +t = s.copy() +s.add(5) +t.add(7) +for i in s, t: + l = list(i) + l.sort() + print(l) diff --git a/tests/basics/tests/set_difference.py b/tests/basics/tests/set_difference.py new file mode 100644 index 0000000000..26976116f3 --- /dev/null +++ b/tests/basics/tests/set_difference.py @@ -0,0 +1,21 @@ +def report(s): + l = list(s) + l.sort() + print(l) + +l = [1, 2, 3, 4] +s = set(l) +outs = [s.difference(), + s.difference({1}), + s.difference({1}, [1, 2]), + s.difference({1}, {1, 2}, {2, 3})] +for out in outs: + report(out) + +s = set(l) +print(s.difference_update()) +report(s) +print(s.difference_update({1})) +report(s) +print(s.difference_update({1}, [2])) +report(s) diff --git a/tests/basics/tests/set_discard.py b/tests/basics/tests/set_discard.py new file mode 100644 index 0000000000..baac26413c --- /dev/null +++ b/tests/basics/tests/set_discard.py @@ -0,0 +1,3 @@ +s = {1, 2} +print(s.discard(1)) +print(list(s)) diff --git a/tests/basics/tests/set_intersection.py b/tests/basics/tests/set_intersection.py new file mode 100644 index 0000000000..6f3dfc7414 --- /dev/null +++ b/tests/basics/tests/set_intersection.py @@ -0,0 +1,12 @@ +def report(s): + l = list(s) + l.sort() + print(l) + +s = {1, 2, 3, 4} +report(s) +report(s.intersection({1, 3})) +report(s.intersection([3, 4])) + +print(s.intersection_update([1])) +report(s) diff --git a/tests/basics/tests/set_isdisjoint.py b/tests/basics/tests/set_isdisjoint.py new file mode 100644 index 0000000000..7fb7e769bb --- /dev/null +++ b/tests/basics/tests/set_isdisjoint.py @@ -0,0 +1,6 @@ +s = {1, 2, 3, 4} +print(s.isdisjoint({1})) +print(s.isdisjoint([2])) +print(s.isdisjoint([])) +print(s.isdisjoint({7,8,9,10})) +print(s.isdisjoint([7,8,9,1])) diff --git a/tests/basics/tests/set_isfooset.py b/tests/basics/tests/set_isfooset.py new file mode 100644 index 0000000000..ce7952cd2c --- /dev/null +++ b/tests/basics/tests/set_isfooset.py @@ -0,0 +1,5 @@ +sets = [set(), {1}, {1, 2, 3}, {3, 4, 5}, {5, 6, 7}] +for i in sets: + for j in sets: + print(i.issubset(j)) + print(i.issuperset(j)) diff --git a/tests/basics/tests/set_iter.py b/tests/basics/tests/set_iter.py new file mode 100644 index 0000000000..2960177303 --- /dev/null +++ b/tests/basics/tests/set_iter.py @@ -0,0 +1,5 @@ +s = {1, 2, 3, 4} +l = list(s) +l.sort() +print(l) + diff --git a/tests/basics/tests/set_pop.py b/tests/basics/tests/set_pop.py new file mode 100644 index 0000000000..0cd478ce25 --- /dev/null +++ b/tests/basics/tests/set_pop.py @@ -0,0 +1,9 @@ +s = {1} +print(s.pop()) +try: + print(s.pop(), "!!!") +except KeyError: + pass +else: + print("Failed to raise KeyError") + diff --git a/tests/basics/tests/set_remove.py b/tests/basics/tests/set_remove.py new file mode 100644 index 0000000000..208ab137f3 --- /dev/null +++ b/tests/basics/tests/set_remove.py @@ -0,0 +1,9 @@ +s = {1} +print(s.remove(1)) +print(list(s)) +try: + print(s.remove(1), "!!!") +except KeyError: + pass +else: + print("failed to raise KeyError") diff --git a/tests/basics/tests/set_symmetric_difference.py b/tests/basics/tests/set_symmetric_difference.py new file mode 100644 index 0000000000..acf298385a --- /dev/null +++ b/tests/basics/tests/set_symmetric_difference.py @@ -0,0 +1,7 @@ +print({1,2}.symmetric_difference({2,3})) +print({1,2}.symmetric_difference([2,3])) +s = {1,2} +print(s.symmetric_difference_update({2,3})) +l = list(s) +l.sort() +print(l) diff --git a/tests/basics/tests/set_union.py b/tests/basics/tests/set_union.py new file mode 100644 index 0000000000..2adcc972c0 --- /dev/null +++ b/tests/basics/tests/set_union.py @@ -0,0 +1 @@ +print({1}.union({2})) diff --git a/tests/basics/tests/set_update.py b/tests/basics/tests/set_update.py new file mode 100644 index 0000000000..78cd763560 --- /dev/null +++ b/tests/basics/tests/set_update.py @@ -0,0 +1,12 @@ +def report(s): + l = list(s) + l.sort() + print(l) + +s = {1} +s.update() +report(s) +s.update([2]) +report(s) +s.update([1,3], [2,2,4]) +report(s) |