Skip to content
Snippets Groups Projects
Commit 17f45d41 authored by Rachel Dowdall's avatar Rachel Dowdall
Browse files

Merge remote-tracking branch 'upstream/master'

parents 300c8bd4 da8d21e0
Branches
No related tags found
No related merge requests found
...@@ -375,28 +375,6 @@ STATIC mp_obj_t mp_builtin_sorted(uint n_args, const mp_obj_t *args, mp_map_t *k ...@@ -375,28 +375,6 @@ STATIC mp_obj_t mp_builtin_sorted(uint n_args, const mp_obj_t *args, mp_map_t *k
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted); MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
STATIC mp_obj_t mp_builtin_str(mp_obj_t o_in) {
vstr_t *vstr = vstr_new();
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, o_in, PRINT_STR);
mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
vstr_free(vstr);
return s;
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_str_obj, mp_builtin_str);
// TODO: This should be type, this is just quick CPython compat hack
STATIC mp_obj_t mp_builtin_bytes(uint n_args, const mp_obj_t *args) {
if (!MP_OBJ_IS_QSTR(args[0]) && !MP_OBJ_IS_TYPE(args[0], &str_type)) {
assert(0);
}
// Currently, MicroPython strings are mix between CPython byte and unicode
// strings. So, conversion is null so far.
return args[0];
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_bytes_obj, 1, 3, mp_builtin_bytes);
STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) { STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
return mp_obj_new_int((machine_int_t)o_in); return mp_obj_new_int((machine_int_t)o_in);
} }
......
...@@ -46,9 +46,9 @@ MATH_FUN_1(fabs, fabs) ...@@ -46,9 +46,9 @@ MATH_FUN_1(fabs, fabs)
MATH_FUN_1(floor, floor) //TODO: delegate to x.__floor__() if x is not a float MATH_FUN_1(floor, floor) //TODO: delegate to x.__floor__() if x is not a float
MATH_FUN_2(fmod, fmod) MATH_FUN_2(fmod, fmod)
//MATH_FUN_1(frexp, frexp) //MATH_FUN_1(frexp, frexp)
MATH_FUN_1(isfinite, isfinite) //MATH_FUN_1(isfinite, isfinite)
MATH_FUN_1(isinf, isinf) //MATH_FUN_1(isinf, isinf)
MATH_FUN_1(isnan, isnan) //MATH_FUN_1(isnan, isnan)
MATH_FUN_1(trunc, trunc) MATH_FUN_1(trunc, trunc)
//TODO: factorial, fsum, frexp, ldexp, modf //TODO: factorial, fsum, frexp, ldexp, modf
...@@ -83,9 +83,9 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = { ...@@ -83,9 +83,9 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&mp_math_floor_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&mp_math_floor_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_fmod), (mp_obj_t)&mp_math_fmod_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_fmod), (mp_obj_t)&mp_math_fmod_obj },
//{ MP_OBJ_NEW_QSTR(MP_QSTR_frexp), (mp_obj_t)&mp_math_frexp_obj }, //{ MP_OBJ_NEW_QSTR(MP_QSTR_frexp), (mp_obj_t)&mp_math_frexp_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_math_isfinite_obj }, //{ MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_math_isfinite_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_math_isinf_obj }, //{ MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_math_isinf_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_math_isnan_obj }, //{ MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_math_isnan_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj },
}; };
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "mpconfig.h" #include "mpconfig.h"
#include "qstr.h" #include "qstr.h"
#include "obj.h" #include "obj.h"
#include "parsenum.h"
#include "runtime0.h" #include "runtime0.h"
#include "map.h" #include "map.h"
...@@ -36,15 +37,20 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const ...@@ -36,15 +37,20 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
return mp_obj_new_complex(0, 0); return mp_obj_new_complex(0, 0);
case 1: case 1:
// TODO allow string as first arg and parse it if (MP_OBJ_IS_STR(args[0])) {
if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) { // a string, parse it
uint l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_decimal(s, l, true, true);
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
// a complex, just return it
return args[0]; return args[0];
} else { } else {
// something else, try to cast it to a complex
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
} }
case 2: case 2: {
{
mp_float_t real, imag; mp_float_t real, imag;
if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) { if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
mp_obj_complex_get(args[0], &real, &imag); mp_obj_complex_get(args[0], &real, &imag);
......
...@@ -39,10 +39,12 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m ...@@ -39,10 +39,12 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
// a string, parse it // a string, parse it
uint l; uint l;
const char *s = mp_obj_str_get_data(args[0], &l); const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_decimal(s, l); return mp_parse_num_decimal(s, l, false, false);
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) { } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
// a float, just return it
return args[0]; return args[0];
} else { } else {
// something else, try to cast it to a float
return mp_obj_new_float(mp_obj_get_float(args[0])); return mp_obj_new_float(mp_obj_get_float(args[0]));
} }
......
...@@ -14,9 +14,11 @@ typedef struct _mp_obj_str_t { ...@@ -14,9 +14,11 @@ typedef struct _mp_obj_str_t {
mp_obj_base_t base; mp_obj_base_t base;
machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c) machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c)
machine_uint_t len : 16; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte machine_uint_t len : 16; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
byte data[]; const byte *data;
} mp_obj_str_t; } mp_obj_str_t;
const mp_obj_t mp_const_empty_bytes;
// use this macro to extract the string hash // use this macro to extract the string hash
#define GET_STR_HASH(str_obj_in, str_hash) uint str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t*)str_obj_in)->hash; } #define GET_STR_HASH(str_obj_in, str_hash) uint str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t*)str_obj_in)->hash; }
...@@ -28,6 +30,7 @@ typedef struct _mp_obj_str_t { ...@@ -28,6 +30,7 @@ typedef struct _mp_obj_str_t {
STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str); STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len);
/******************************************************************************/ /******************************************************************************/
/* str */ /* str */
...@@ -78,6 +81,109 @@ STATIC void str_print(void (*print)(void *env, const char *fmt, ...), void *env, ...@@ -78,6 +81,109 @@ STATIC void str_print(void (*print)(void *env, const char *fmt, ...), void *env,
} }
} }
STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
switch (n_args) {
case 0:
return MP_OBJ_NEW_QSTR(MP_QSTR_);
case 1:
{
vstr_t *vstr = vstr_new();
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, args[0], PRINT_STR);
mp_obj_t s = mp_obj_new_str((byte*)vstr->buf, vstr->len, false);
vstr_free(vstr);
return s;
}
case 2:
case 3:
{
// TODO: validate 2nd/3rd args
if (!MP_OBJ_IS_TYPE(args[0], &bytes_type)) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "bytes expected"));
}
GET_STR_DATA_LEN(args[0], str_data, str_len);
GET_STR_HASH(args[0], str_hash);
mp_obj_str_t *o = str_new(&str_type, NULL, str_len);
o->data = str_data;
o->hash = str_hash;
return o;
}
default:
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "str takes at most 3 arguments"));
}
}
STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
if (n_args == 0) {
return mp_const_empty_bytes;
}
if (MP_OBJ_IS_STR(args[0])) {
if (n_args < 2 || n_args > 3) {
goto wrong_args;
}
GET_STR_DATA_LEN(args[0], str_data, str_len);
GET_STR_HASH(args[0], str_hash);
mp_obj_str_t *o = str_new(&bytes_type, NULL, str_len);
o->data = str_data;
o->hash = str_hash;
return o;
}
if (n_args > 1) {
goto wrong_args;
}
if (MP_OBJ_IS_SMALL_INT(args[0])) {
uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
byte *data;
mp_obj_t o = mp_obj_str_builder_start(&bytes_type, len, &data);
memset(data, 0, len);
return mp_obj_str_builder_end(o);
}
int len;
byte *data;
vstr_t *vstr = NULL;
mp_obj_t o = NULL;
// Try to create array of exact len if initializer len is known
mp_obj_t len_in = mp_obj_len_maybe(args[0]);
if (len_in == MP_OBJ_NULL) {
len = -1;
vstr = vstr_new();
} else {
len = MP_OBJ_SMALL_INT_VALUE(len_in);
o = mp_obj_str_builder_start(&bytes_type, len, &data);
}
mp_obj_t iterable = rt_getiter(args[0]);
mp_obj_t item;
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
if (len == -1) {
vstr_add_char(vstr, MP_OBJ_SMALL_INT_VALUE(item));
} else {
*data++ = MP_OBJ_SMALL_INT_VALUE(item);
}
}
if (len == -1) {
vstr_shrink(vstr);
// TODO: Optimize, borrow buffer from vstr
len = vstr_len(vstr);
o = mp_obj_str_builder_start(&bytes_type, len, &data);
memcpy(data, vstr_str(vstr), len);
vstr_free(vstr);
}
return mp_obj_str_builder_end(o);
wrong_args:
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "wrong number of arguments"));
}
// like strstr but with specified length and allows \0 bytes // like strstr but with specified length and allows \0 bytes
// TODO replace with something more efficient/standard // TODO replace with something more efficient/standard
STATIC const byte *find_subbytes(const byte *haystack, uint hlen, const byte *needle, uint nlen) { STATIC const byte *find_subbytes(const byte *haystack, uint hlen, const byte *needle, uint nlen) {
...@@ -520,6 +626,62 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) { ...@@ -520,6 +626,62 @@ STATIC mp_obj_t str_count(uint n_args, const mp_obj_t *args) {
return MP_OBJ_NEW_SMALL_INT(num_occurrences); return MP_OBJ_NEW_SMALL_INT(num_occurrences);
} }
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t direction) {
assert(MP_OBJ_IS_STR(self_in));
if (!MP_OBJ_IS_STR(arg)) {
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"Can't convert '%s' object to str implicitly", mp_obj_get_type_str(arg)));
}
GET_STR_DATA_LEN(self_in, str, str_len);
GET_STR_DATA_LEN(arg, sep, sep_len);
if (sep_len == 0) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "empty separator"));
}
mp_obj_t result[] = {MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_), MP_OBJ_NEW_QSTR(MP_QSTR_)};
if (direction > 0) {
result[0] = self_in;
} else {
result[2] = self_in;
}
if (str_len >= sep_len) {
machine_uint_t str_index, str_index_end;
if (direction > 0) {
str_index = 0;
str_index_end = str_len - sep_len;
} else {
str_index = str_len - sep_len;
str_index_end = 0;
}
for (;;) {
if (memcmp(&str[str_index], sep, sep_len) == 0) {
result[0] = mp_obj_new_str(str, str_index, false);
result[1] = arg;
result[2] = mp_obj_new_str(str + str_index + sep_len, str_len - str_index - sep_len, false);
break;
}
if (str_index == str_index_end) {
break;
}
str_index += direction;
}
}
return mp_obj_new_tuple(3, result);
}
STATIC mp_obj_t str_partition(mp_obj_t self_in, mp_obj_t arg) {
return str_partitioner(self_in, arg, 1);
}
STATIC mp_obj_t str_rpartition(mp_obj_t self_in, mp_obj_t arg) {
return str_partitioner(self_in, arg, -1);
}
STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) { STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {
if (flags == BUFFER_READ) { if (flags == BUFFER_READ) {
GET_STR_DATA_LEN(self_in, str_data, str_len); GET_STR_DATA_LEN(self_in, str_data, str_len);
...@@ -542,6 +704,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip); ...@@ -542,6 +704,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_strip_obj, 1, 2, str_strip);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_partition_obj, str_partition);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(str_rpartition_obj, str_rpartition);
STATIC const mp_method_t str_type_methods[] = { STATIC const mp_method_t str_type_methods[] = {
{ "find", &str_find_obj }, { "find", &str_find_obj },
...@@ -552,6 +716,8 @@ STATIC const mp_method_t str_type_methods[] = { ...@@ -552,6 +716,8 @@ STATIC const mp_method_t str_type_methods[] = {
{ "format", &str_format_obj }, { "format", &str_format_obj },
{ "replace", &str_replace_obj }, { "replace", &str_replace_obj },
{ "count", &str_count_obj }, { "count", &str_count_obj },
{ "partition", &str_partition_obj },
{ "rpartition", &str_rpartition_obj },
{ NULL, NULL }, // end-of-list sentinel { NULL, NULL }, // end-of-list sentinel
}; };
...@@ -559,6 +725,7 @@ const mp_obj_type_t str_type = { ...@@ -559,6 +725,7 @@ const mp_obj_type_t str_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_str, .name = MP_QSTR_str,
.print = str_print, .print = str_print,
.make_new = str_make_new,
.binary_op = str_binary_op, .binary_op = str_binary_op,
.getiter = mp_obj_new_str_iterator, .getiter = mp_obj_new_str_iterator,
.methods = str_type_methods, .methods = str_type_methods,
...@@ -570,34 +737,45 @@ const mp_obj_type_t bytes_type = { ...@@ -570,34 +737,45 @@ const mp_obj_type_t bytes_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_bytes, .name = MP_QSTR_bytes,
.print = str_print, .print = str_print,
.make_new = bytes_make_new,
.binary_op = str_binary_op, .binary_op = str_binary_op,
.getiter = mp_obj_new_bytes_iterator, .getiter = mp_obj_new_bytes_iterator,
.methods = str_type_methods, .methods = str_type_methods,
}; };
// the zero-length bytes
STATIC const mp_obj_str_t empty_bytes_obj = {{&bytes_type}, 0, 0, NULL};
const mp_obj_t mp_const_empty_bytes = (mp_obj_t)&empty_bytes_obj;
mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) { mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1); mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
o->base.type = type; o->base.type = type;
o->len = len; o->len = len;
*data = o->data; byte *p = m_new(byte, len + 1);
o->data = p;
*data = p;
return o; return o;
} }
mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) { mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
assert(MP_OBJ_IS_STR(o_in));
mp_obj_str_t *o = o_in; mp_obj_str_t *o = o_in;
o->hash = qstr_compute_hash(o->data, o->len); o->hash = qstr_compute_hash(o->data, o->len);
o->data[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings byte *p = (byte*)o->data;
p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
return o; return o;
} }
STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) { STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1); mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
o->base.type = type; o->base.type = type;
o->hash = qstr_compute_hash(data, len);
o->len = len; o->len = len;
memcpy(o->data, data, len * sizeof(byte)); if (data) {
o->data[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings o->hash = qstr_compute_hash(data, len);
byte *p = m_new(byte, len + 1);
o->data = p;
memcpy(p, data, len * sizeof(byte));
p[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
}
return o; return o;
} }
......
...@@ -9,90 +9,147 @@ ...@@ -9,90 +9,147 @@
#include "parsenumbase.h" #include "parsenumbase.h"
#include "parsenum.h" #include "parsenum.h"
#if defined(UNIX) #if MICROPY_ENABLE_FLOAT
#include <math.h>
#include <ctype.h> #endif
#include <errno.h>
mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
// TODO at the moment we ignore len; we should honour it! const char *restrict top = str + len;
// TODO detect integer overflow and return bignum bool neg = false;
int c, neg = 0;
const char *p = str;
char *num;
long found;
// check radix base // check radix base
if ((base != 0 && base < 2) || base > 36) { if ((base != 0 && base < 2) || base > 36) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36")); nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "ValueError: int() arg 2 must be >=2 and <= 36"));
} }
// skip surrounded whitespace
while (isspace((c = *(p++)))); // skip leading space
if (c == 0) { for (; str < top && unichar_isspace(*str); str++) {
goto value_error; }
// parse optional sign
if (str < top) {
if (*str == '+') {
str++;
} else if (*str == '-') {
str++;
neg = true;
} }
// preced sign
if (c == '+' || c == '-') {
neg = - (c == '-');
} else {
p--;
} }
len -= p - str; // parse optional base prefix
int skip = mp_parse_num_base(p, len, &base); str += mp_parse_num_base(str, top - str, &base);
p += skip;
len -= skip;
errno = 0; // string should be an integer number
found = strtol(p, &num, base); machine_int_t int_val = 0;
if (errno) { const char *restrict str_val_start = str;
goto value_error; for (; str < top; str++) {
} else if (found && *(num) == 0) { machine_int_t old_val = int_val;
goto done; int dig = *str;
} else if (found || num != p) { if (unichar_isdigit(dig) && dig - '0' < base) {
goto check_tail_space; // 0-9 digit
int_val = base * int_val + dig - '0';
} else if (base == 16) {
dig |= 0x20;
if ('a' <= dig && dig <= 'f') {
// a-f hex digit
int_val = base * int_val + dig - 'a' + 10;
} else { } else {
goto value_error; // unknown character
break;
}
} else {
// unknown character
break;
}
if (int_val < old_val) {
// If new value became less than previous, it's overflow
goto overflow;
} else if ((old_val ^ int_val) & WORD_MSBIT_HIGH) {
// If signed number changed sign - it's overflow
goto overflow;
}
} }
check_tail_space: // check we parsed something
if (*(num) != 0) { if (str == str_val_start) {
while (isspace((c = *(num++))));
if (c != 0) {
goto value_error; goto value_error;
} }
// negate value if needed
if (neg) {
int_val = -int_val;
}
// skip trailing space
for (; str < top && unichar_isspace(*str); str++) {
}
// check we reached the end of the string
if (str != top) {
goto value_error;
} }
done: // return the object
return MP_OBJ_NEW_SMALL_INT((found ^ neg) - neg); return MP_OBJ_NEW_SMALL_INT(int_val);
value_error: value_error:
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, str)); nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid literal for int() with base %d: '%s'", base, str));
}
#else /* defined(UNIX) */
mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) { overflow:
// TODO port strtol to stm // TODO reparse using bignum
return MP_OBJ_NEW_SMALL_INT(0); nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "overflow parsing integer"));
} }
#endif /* defined(UNIX) */
#define PARSE_DEC_IN_INTG (1) #define PARSE_DEC_IN_INTG (1)
#define PARSE_DEC_IN_FRAC (2) #define PARSE_DEC_IN_FRAC (2)
#define PARSE_DEC_IN_EXP (3) #define PARSE_DEC_IN_EXP (3)
mp_obj_t mp_parse_num_decimal(const char *str, uint len) { mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex) {
#if MICROPY_ENABLE_FLOAT #if MICROPY_ENABLE_FLOAT
int in = PARSE_DEC_IN_INTG; const char *top = str + len;
mp_float_t dec_val = 0; mp_float_t dec_val = 0;
bool dec_neg = false;
bool imag = false;
// skip leading space
for (; str < top && unichar_isspace(*str); str++) {
}
// parse optional sign
if (str < top) {
if (*str == '+') {
str++;
} else if (*str == '-') {
str++;
dec_neg = true;
}
}
// determine what the string is
if (str < top && (str[0] | 0x20) == 'i') {
// string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
// inf
str += 3;
dec_val = INFINITY;
if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
// infinity
str += 5;
}
}
} else if (str < top && (str[0] | 0x20) == 'n') {
// string starts with 'n', should be 'nan' (case insensitive)
if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
// NaN
str += 3;
dec_val = MICROPY_FLOAT_C_FUN(nan)("");
}
} else {
// string should be a decimal number
int in = PARSE_DEC_IN_INTG;
bool exp_neg = false; bool exp_neg = false;
int exp_val = 0; int exp_val = 0;
int exp_extra = 0; int exp_extra = 0;
bool imag = false;
const char *top = str + len;
for (; str < top; str++) { for (; str < top; str++) {
int dig = *str; int dig = *str;
if ('0' <= dig && dig <= '9') { if ('0' <= dig && dig <= '9') {
...@@ -107,7 +164,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len) { ...@@ -107,7 +164,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len) {
} }
} else if (in == PARSE_DEC_IN_INTG && dig == '.') { } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
in = PARSE_DEC_IN_FRAC; in = PARSE_DEC_IN_FRAC;
} else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) { } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
in = PARSE_DEC_IN_EXP; in = PARSE_DEC_IN_EXP;
if (str[1] == '+') { if (str[1] == '+') {
str++; str++;
...@@ -115,7 +172,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len) { ...@@ -115,7 +172,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len) {
str++; str++;
exp_neg = true; exp_neg = true;
} }
} else if (dig == 'J' || dig == 'j') { } else if (allow_imag && (dig | 0x20) == 'j') {
str++; str++;
imag = true; imag = true;
break; break;
...@@ -124,24 +181,45 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len) { ...@@ -124,24 +181,45 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len) {
break; break;
} }
} }
if (*str != 0) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); // work out the exponent
}
if (exp_neg) { if (exp_neg) {
exp_val = -exp_val; exp_val = -exp_val;
} }
exp_val += exp_extra; exp_val += exp_extra;
// apply the exponent
for (; exp_val > 0; exp_val--) { for (; exp_val > 0; exp_val--) {
dec_val *= 10; dec_val *= 10;
} }
for (; exp_val < 0; exp_val++) { for (; exp_val < 0; exp_val++) {
dec_val *= 0.1; dec_val *= 0.1;
} }
}
// negate value if needed
if (dec_neg) {
dec_val = -dec_val;
}
// skip trailing space
for (; str < top && unichar_isspace(*str); str++) {
}
// check we reached the end of the string
if (str != top) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
}
// return the object
if (imag) { if (imag) {
return mp_obj_new_complex(0, dec_val); return mp_obj_new_complex(0, dec_val);
} else if (force_complex) {
return mp_obj_new_complex(dec_val, 0);
} else { } else {
return mp_obj_new_float(dec_val); return mp_obj_new_float(dec_val);
} }
#else #else
nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported")); nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported"));
#endif #endif
......
mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base); mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base);
mp_obj_t mp_parse_num_decimal(const char *str, uint len); mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool force_complex);
...@@ -89,6 +89,7 @@ STATIC const mp_builtin_elem_t builtin_table[] = { ...@@ -89,6 +89,7 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
// built-in types // built-in types
{ MP_QSTR_bool, (mp_obj_t)&bool_type }, { MP_QSTR_bool, (mp_obj_t)&bool_type },
{ MP_QSTR_bytes, (mp_obj_t)&bytes_type },
#if MICROPY_ENABLE_FLOAT #if MICROPY_ENABLE_FLOAT
{ MP_QSTR_complex, (mp_obj_t)&mp_type_complex }, { MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
#endif #endif
...@@ -102,6 +103,7 @@ STATIC const mp_builtin_elem_t builtin_table[] = { ...@@ -102,6 +103,7 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
{ MP_QSTR_list, (mp_obj_t)&list_type }, { MP_QSTR_list, (mp_obj_t)&list_type },
{ MP_QSTR_map, (mp_obj_t)&map_type }, { MP_QSTR_map, (mp_obj_t)&map_type },
{ MP_QSTR_set, (mp_obj_t)&set_type }, { MP_QSTR_set, (mp_obj_t)&set_type },
{ MP_QSTR_str, (mp_obj_t)&str_type },
{ MP_QSTR_super, (mp_obj_t)&super_type }, { MP_QSTR_super, (mp_obj_t)&super_type },
{ MP_QSTR_tuple, (mp_obj_t)&tuple_type }, { MP_QSTR_tuple, (mp_obj_t)&tuple_type },
{ MP_QSTR_type, (mp_obj_t)&mp_type_type }, { MP_QSTR_type, (mp_obj_t)&mp_type_type },
...@@ -114,7 +116,6 @@ STATIC const mp_builtin_elem_t builtin_table[] = { ...@@ -114,7 +116,6 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
{ MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj }, { MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
{ MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj }, { MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
{ MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj }, { MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
{ MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj },
{ MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj }, { MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
{ MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj }, { MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
{ MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj }, { MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
...@@ -137,7 +138,6 @@ STATIC const mp_builtin_elem_t builtin_table[] = { ...@@ -137,7 +138,6 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
{ MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj }, { MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
{ MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj }, { MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
{ MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj }, { MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
{ MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj },
{ MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj }, { MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
// built-in exceptions // built-in exceptions
...@@ -376,7 +376,7 @@ mp_obj_t rt_load_const_dec(qstr qstr) { ...@@ -376,7 +376,7 @@ mp_obj_t rt_load_const_dec(qstr qstr) {
DEBUG_OP_printf("load '%s'\n", qstr_str(qstr)); DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
uint len; uint len;
const byte* data = qstr_data(qstr, &len); const byte* data = qstr_data(qstr, &len);
return mp_parse_num_decimal((const char*)data, len); return mp_parse_num_decimal((const char*)data, len, true, false);
} }
mp_obj_t rt_load_const_str(qstr qstr) { mp_obj_t rt_load_const_str(qstr qstr) {
......
...@@ -394,6 +394,10 @@ void mp_byte_code_print(const byte *ip, int len) { ...@@ -394,6 +394,10 @@ void mp_byte_code_print(const byte *ip, int len) {
printf("YIELD_VALUE"); printf("YIELD_VALUE");
break; break;
case MP_BC_YIELD_FROM:
printf("YIELD_FROM");
break;
case MP_BC_IMPORT_NAME: case MP_BC_IMPORT_NAME:
DECODE_QSTR; DECODE_QSTR;
printf("IMPORT_NAME %s", qstr_str(qstr)); printf("IMPORT_NAME %s", qstr_str(qstr));
......
...@@ -44,6 +44,10 @@ float acosf(float x) { return 0.0; } ...@@ -44,6 +44,10 @@ float acosf(float x) { return 0.0; }
float asinf(float x) { return 0.0; } float asinf(float x) { return 0.0; }
float atanf(float x) { return 0.0; } float atanf(float x) { return 0.0; }
float atan2f(float x, float y) { return 0.0; } float atan2f(float x, float y) { return 0.0; }
float ceilf(float x) { return 0.0; }
float floorf(float x) { return 0.0; }
float truncf(float x) { return 0.0; }
float fmodf(float x, float y) { return 0.0; }
/*****************************************************************************/ /*****************************************************************************/
// from musl-0.9.15 libm.h // from musl-0.9.15 libm.h
......
...@@ -21,7 +21,7 @@ INC += -I$(PY_SRC) ...@@ -21,7 +21,7 @@ INC += -I$(PY_SRC)
INC += -I$(CMSIS_DIR)/inc INC += -I$(CMSIS_DIR)/inc
INC += -I$(CMSIS_DIR)/devinc INC += -I$(CMSIS_DIR)/devinc
INC += -I$(HAL_DIR)/inc INC += -I$(HAL_DIR)/inc
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc/inc INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc/inc -I$(USBDEV_DIR)/class/msc/inc
#INC += -I$(USBHOST_DIR) #INC += -I$(USBHOST_DIR)
INC += -I$(FATFS_DIR)/src INC += -I$(FATFS_DIR)/src
#INC += -I$(CC3K_DIR) #INC += -I$(CC3K_DIR)
...@@ -56,8 +56,10 @@ SRC_C = \ ...@@ -56,8 +56,10 @@ SRC_C = \
stm32f4xx_it.c \ stm32f4xx_it.c \
stm32f4xx_hal_msp.c \ stm32f4xx_hal_msp.c \
usbd_conf.c \ usbd_conf.c \
usbd_desc.c \ usbd_desc_vcp.c \
usbd_cdc_interface.c \ usbd_cdc_interface.c \
usbd_desc_msc.c \
usbd_msc_storage.c \
pendsv.c \ pendsv.c \
systick.c \ systick.c \
led.c \ led.c \
...@@ -84,9 +86,9 @@ SRC_C = \ ...@@ -84,9 +86,9 @@ SRC_C = \
sdcard.c \ sdcard.c \
diskio.c \ diskio.c \
lcd.c \ lcd.c \
accel.c \
# servo.c \ # servo.c \
# accel.c \
# timer.c \ # timer.c \
# audio.c \ # audio.c \
# i2c.c \ # i2c.c \
...@@ -104,6 +106,7 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\ ...@@ -104,6 +106,7 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
stm32f4xx_hal_flash.c \ stm32f4xx_hal_flash.c \
stm32f4xx_hal_flash_ex.c \ stm32f4xx_hal_flash_ex.c \
stm32f4xx_hal_gpio.c \ stm32f4xx_hal_gpio.c \
stm32f4xx_hal_i2c.c \
stm32f4xx_hal_pcd.c \ stm32f4xx_hal_pcd.c \
stm32f4xx_hal_rcc.c \ stm32f4xx_hal_rcc.c \
stm32f4xx_hal_rcc_ex.c \ stm32f4xx_hal_rcc_ex.c \
...@@ -122,6 +125,10 @@ SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\ ...@@ -122,6 +125,10 @@ SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\
core/src/usbd_ctlreq.c \ core/src/usbd_ctlreq.c \
core/src/usbd_ioreq.c \ core/src/usbd_ioreq.c \
class/cdc/src/usbd_cdc.c \ class/cdc/src/usbd_cdc.c \
class/msc/src/usbd_msc.c \
class/msc/src/usbd_msc_bot.c \
class/msc/src/usbd_msc_scsi.c \
class/msc/src/usbd_msc_data.c \
) )
# usbd_core.c \ # usbd_core.c \
......
#include <stdio.h>
#include <string.h>
#include <stm32f4xx_hal.h>
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
#include "accel.h"
#define MMA_ADDR (0x98)
#define MMA_REG_MODE (7)
STATIC I2C_HandleTypeDef I2cHandle;
void accel_init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// PB5 is connected to AVDD; pull high to enable MMA accel device
GPIOB->BSRRH = GPIO_PIN_5; // turn off AVDD
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
// wait 20ms, then turn on AVDD, then wait 20ms again
HAL_Delay(20);
GPIOB->BSRRL = GPIO_PIN_5;
HAL_Delay(20);
// PB6=SCL, PB7=SDA
GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
// enable the I2C1 clock
__I2C1_CLK_ENABLE();
// set up the I2C1 device
memset(&I2cHandle, 0, sizeof(I2C_HandleTypeDef));
I2cHandle.Instance = I2C1;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.ClockSpeed = 400000;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
I2cHandle.Init.OwnAddress1 = 0xfe; // unused
I2cHandle.Init.OwnAddress2 = 0xfe; // unused
if (HAL_I2C_Init(&I2cHandle) != HAL_OK) {
// init error
printf("accel_init: HAL_I2C_Init failed\n");
return;
}
HAL_StatusTypeDef status;
//printf("IsDeviceReady\n");
for (int i = 0; i < 10; i++) {
status = HAL_I2C_IsDeviceReady(&I2cHandle, MMA_ADDR, 10, 200);
//printf(" got %d\n", status);
if (status == HAL_OK) {
break;
}
}
//printf("MemWrite\n");
uint8_t data[1];
data[0] = 1; // active mode
status = HAL_I2C_Mem_Write(&I2cHandle, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
//printf(" got %d\n", status);
}
/******************************************************************************/
/* Micro Python bindings */
int accel_buf[12];
mp_obj_t pyb_accel_read(void) {
for (int i = 0; i <= 6; i += 3) {
accel_buf[0 + i] = accel_buf[0 + i + 3];
accel_buf[1 + i] = accel_buf[1 + i + 3];
accel_buf[2 + i] = accel_buf[2 + i + 3];
}
uint8_t data_[4];
HAL_I2C_Mem_Read(&I2cHandle, MMA_ADDR, 0, I2C_MEMADD_SIZE_8BIT, data_, 4, 200);
accel_buf[9] = data_[0] & 0x3f; if (accel_buf[9] & 0x20) accel_buf[9] |= ~0x1f;
accel_buf[10] = data_[1] & 0x3f; if (accel_buf[10] & 0x20) accel_buf[10] |= ~0x1f;
accel_buf[11] = data_[2] & 0x3f; if (accel_buf[11] & 0x20) accel_buf[11] |= ~0x1f;
int jolt_info = data_[3];
mp_obj_t data[4];
data[0] = mp_obj_new_int(accel_buf[0] + accel_buf[3] + accel_buf[6] + accel_buf[9]);
data[1] = mp_obj_new_int(accel_buf[1] + accel_buf[4] + accel_buf[7] + accel_buf[10]);
data[2] = mp_obj_new_int(accel_buf[2] + accel_buf[5] + accel_buf[8] + accel_buf[11]);
data[3] = mp_obj_new_int(jolt_info);
return rt_build_tuple(4, data);
}
MP_DEFINE_CONST_FUN_OBJ_0(pyb_accel_read_obj, pyb_accel_read);
/*
mp_obj_t pyb_accel_read_all(void) {
mp_obj_t data[11];
accel_start(MMA_ADDR, 1);
accel_send_byte(0);
accel_restart(MMA_ADDR, 0);
for (int i = 0; i <= 9; i++) {
data[i] = mp_obj_new_int(accel_read_ack());
}
data[10] = mp_obj_new_int(accel_read_nack());
return rt_build_tuple(11, data);
}
MP_DEFINE_CONST_FUN_OBJ_0(pyb_accel_read_all_obj, pyb_accel_read_all);
mp_obj_t pyb_accel_write_mode(mp_obj_t o_int, mp_obj_t o_mode) {
accel_start(MMA_ADDR, 1);
accel_send_byte(6); // start at int
accel_send_byte(mp_obj_get_int(o_int));
accel_send_byte(mp_obj_get_int(o_mode));
accel_stop();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_write_mode_obj, pyb_accel_write_mode);
*/
void accel_init(void);
void accel_restart(uint8_t addr, int write);
void accel_start(uint8_t addr, int write);
void accel_send_byte(uint8_t data);
uint8_t accel_read_ack(void);
uint8_t accel_read_nack(void);
void accel_stop(void);
MP_DECLARE_CONST_FUN_OBJ(pyb_accel_read_obj);
MP_DECLARE_CONST_FUN_OBJ(pyb_accel_read_all_obj);
MP_DECLARE_CONST_FUN_OBJ(pyb_accel_write_mode_obj);
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "qstr.h" #include "qstr.h"
#include "lexer.h" #include "lexer.h"
#if 0
#include "ff.h" #include "ff.h"
#endif
mp_import_stat_t mp_import_stat(const char *path) { mp_import_stat_t mp_import_stat(const char *path) {
#if 0
FILINFO fno; FILINFO fno;
#if _USE_LFN
fno.lfname = NULL;
fno.lfsize = 0;
#endif
FRESULT res = f_stat(path, &fno); FRESULT res = f_stat(path, &fno);
if (res == FR_OK) { if (res == FR_OK) {
if ((fno.fattrib & AM_DIR) != 0) { if ((fno.fattrib & AM_DIR) != 0) {
...@@ -19,6 +21,5 @@ mp_import_stat_t mp_import_stat(const char *path) { ...@@ -19,6 +21,5 @@ mp_import_stat_t mp_import_stat(const char *path) {
return MP_IMPORT_STAT_FILE; return MP_IMPORT_STAT_FILE;
} }
} }
#endif
return MP_IMPORT_STAT_NO_EXIST; return MP_IMPORT_STAT_NO_EXIST;
} }
...@@ -73,6 +73,14 @@ void led_toggle(pyb_led_t led) { ...@@ -73,6 +73,14 @@ void led_toggle(pyb_led_t led) {
} }
} }
void led_debug(int n, int delay) {
led_state(1, n & 1);
led_state(2, n & 2);
led_state(3, n & 4);
led_state(4, n & 8);
HAL_Delay(delay);
}
/******************************************************************************/ /******************************************************************************/
/* Micro Python bindings */ /* Micro Python bindings */
......
...@@ -19,5 +19,6 @@ typedef enum { ...@@ -19,5 +19,6 @@ typedef enum {
void led_init(void); void led_init(void);
void led_state(pyb_led_t led, int state); void led_state(pyb_led_t led, int state);
void led_toggle(pyb_led_t led); void led_toggle(pyb_led_t led);
void led_debug(int value, int delay);
MP_DECLARE_CONST_FUN_OBJ(pyb_Led_obj); MP_DECLARE_CONST_FUN_OBJ(pyb_Led_obj);
...@@ -33,9 +33,9 @@ ...@@ -33,9 +33,9 @@
#include "sdcard.h" #include "sdcard.h"
#include "ff.h" #include "ff.h"
#include "lcd.h" #include "lcd.h"
#include "accel.h"
#if 0 #if 0
#include "servo.h" #include "servo.h"
#include "accel.h"
#include "timer.h" #include "timer.h"
#include "pybwlan.h" #include "pybwlan.h"
#include "pin.h" #include "pin.h"
...@@ -170,22 +170,6 @@ int main(void) { ...@@ -170,22 +170,6 @@ int main(void) {
// enable the CCM RAM // enable the CCM RAM
__CCMDATARAMEN_CLK_ENABLE(); __CCMDATARAMEN_CLK_ENABLE();
// some test code to flash LEDs
led_init();
led_state(0, 1);
led_state(1, 0);
led_state(2, 1);
#if 0
for (;;) {
HAL_Delay(500);
led_state(1, 1);
HAL_Delay(500);
led_state(1, 0);
}
#endif
#if 0 #if 0
#if defined(NETDUINO_PLUS_2) #if defined(NETDUINO_PLUS_2)
{ {
...@@ -348,6 +332,11 @@ soft_reset: ...@@ -348,6 +332,11 @@ soft_reset:
// make sure we have a /boot.py // make sure we have a /boot.py
{ {
FILINFO fno; FILINFO fno;
#if _USE_LFN
fno.lfname = NULL;
fno.lfsize = 0;
#endif
led_debug(0, 500);
FRESULT res = f_stat("0:/boot.py", &fno); FRESULT res = f_stat("0:/boot.py", &fno);
if (res == FR_OK) { if (res == FR_OK) {
if (fno.fattrib & AM_DIR) { if (fno.fattrib & AM_DIR) {
...@@ -382,18 +371,13 @@ soft_reset: ...@@ -382,18 +371,13 @@ soft_reset:
flash_error(4); flash_error(4);
} }
if (first_soft_reset) {
#if 0
#if MICROPY_HW_HAS_MMA7660
// MMA accel: init and reset address to zero
accel_init();
#endif
#endif
}
// turn boot-up LED off // turn boot-up LED off
led_state(PYB_LED_GREEN, 0); led_state(PYB_LED_GREEN, 0);
#if defined(USE_DEVICE_MODE)
usbd_storage_medium_kind_t usbd_medium_kind = USBD_STORAGE_MEDIUM_FLASH;
#endif
#if MICROPY_HW_HAS_SDCARD #if MICROPY_HW_HAS_SDCARD
// if an SD card is present then mount it on 1:/ // if an SD card is present then mount it on 1:/
if (sdcard_is_present()) { if (sdcard_is_present()) {
...@@ -403,8 +387,8 @@ soft_reset: ...@@ -403,8 +387,8 @@ soft_reset:
} else { } else {
if (first_soft_reset) { if (first_soft_reset) {
// use SD card as medium for the USB MSD // use SD card as medium for the USB MSD
#if 0 #if defined(USE_DEVICE_MODE)
usbd_storage_select_medium(USBD_STORAGE_MEDIUM_SDCARD); usbd_medium_kind = USBD_STORAGE_MEDIUM_SDCARD;
#endif #endif
} }
} }
...@@ -416,7 +400,12 @@ soft_reset: ...@@ -416,7 +400,12 @@ soft_reset:
pyb_usb_host_init(); pyb_usb_host_init();
#elif defined(USE_DEVICE_MODE) #elif defined(USE_DEVICE_MODE)
// USB device // USB device
pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC); pyb_usb_dev_init(USBD_DEVICE_MSC, usbd_medium_kind);
#endif
#if MICROPY_HW_HAS_MMA7660
// MMA accel: init and reset
accel_init();
#endif #endif
// run main script // run main script
......
...@@ -44,6 +44,10 @@ float acosf(float x) { return 0.0; } ...@@ -44,6 +44,10 @@ float acosf(float x) { return 0.0; }
float asinf(float x) { return 0.0; } float asinf(float x) { return 0.0; }
float atanf(float x) { return 0.0; } float atanf(float x) { return 0.0; }
float atan2f(float x, float y) { return 0.0; } float atan2f(float x, float y) { return 0.0; }
float ceilf(float x) { return 0.0; }
float floorf(float x) { return 0.0; }
float truncf(float x) { return 0.0; }
float fmodf(float x, float y) { return 0.0; }
/*****************************************************************************/ /*****************************************************************************/
// from musl-0.9.15 libm.h // from musl-0.9.15 libm.h
......
...@@ -21,10 +21,10 @@ ...@@ -21,10 +21,10 @@
#include "usart.h" #include "usart.h"
#include "storage.h" #include "storage.h"
#include "sdcard.h" #include "sdcard.h"
#include "accel.h"
#if 0 #if 0
#include "servo.h" #include "servo.h"
#include "usb.h" #include "usb.h"
#include "accel.h"
#include "i2c.h" #include "i2c.h"
#include "adc.h" #include "adc.h"
#include "audio.h" #include "audio.h"
...@@ -256,13 +256,13 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { ...@@ -256,13 +256,13 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj },
#endif #endif
#if 0
#if MICROPY_HW_HAS_MMA7660 #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), (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_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 }, //{ MP_OBJ_NEW_QSTR(MP_QSTR_accel_mode), (mp_obj_t)&pyb_accel_write_mode_obj },
#endif #endif
#if 0
{ MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj },
#endif #endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_Led), (mp_obj_t)&pyb_Led_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_Led), (mp_obj_t)&pyb_Led_obj },
......
#include <string.h> #include <string.h>
/*
#include "usb_core.h"
#include "usbd_cdc_core.h"
#include "usbd_pyb_core.h"
#include "usbd_usr.h"
*/
#include "usbd_core.h" #include "usbd_core.h"
#include "usbd_desc.h" #include "usbd_desc.h"
#include "usbd_cdc.h" #include "usbd_cdc.h"
#include "usbd_cdc_interface.h" #include "usbd_cdc_interface.h"
#include "usbd_msc.h"
#include "usbd_msc_storage.h"
#include "misc.h" #include "misc.h"
#include "mpconfig.h" #include "mpconfig.h"
#include "qstr.h" #include "qstr.h"
#include "obj.h" #include "obj.h"
//#include "pendsv.h"
#include "usb.h" #include "usb.h"
#ifdef USE_DEVICE_MODE #ifdef USE_DEVICE_MODE
...@@ -26,12 +21,12 @@ static int dev_is_enabled = 0; ...@@ -26,12 +21,12 @@ static int dev_is_enabled = 0;
uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */ uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL; mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
void pyb_usb_dev_init(int usb_dev_type) { void pyb_usb_dev_init(usbd_device_kind_t device_kind, usbd_storage_medium_kind_t medium_kind) {
#ifdef USE_DEVICE_MODE #ifdef USE_DEVICE_MODE
if (!dev_is_enabled) { if (!dev_is_enabled) {
// only init USB once in the device's power-lifetime // only init USB once in the device's power-lifetime
switch (usb_dev_type) { switch (device_kind) {
case PYB_USB_DEV_VCP_MSC: case USBD_DEVICE_CDC:
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc, // XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
// so the memory is invalid after a soft reset (which resets the GC). // so the memory is invalid after a soft reset (which resets the GC).
USBD_Init(&hUSBDDevice, &VCP_Desc, 0); USBD_Init(&hUSBDDevice, &VCP_Desc, 0);
...@@ -41,7 +36,20 @@ void pyb_usb_dev_init(int usb_dev_type) { ...@@ -41,7 +36,20 @@ void pyb_usb_dev_init(int usb_dev_type) {
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb); //USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
break; break;
case PYB_USB_DEV_HID: case USBD_DEVICE_MSC:
// XXX USBD_CDC_Init (called by one of these functions below) uses malloc,
// so the memory is invalid after a soft reset (which resets the GC).
USBD_Init(&hUSBDDevice, &MSC_Desc, 0);
USBD_RegisterClass(&hUSBDDevice, &USBD_MSC);
if (medium_kind == USBD_STORAGE_MEDIUM_FLASH) {
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
} else {
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
}
USBD_Start(&hUSBDDevice);
break;
case USBD_DEVICE_HID:
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb); //USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
// TODO // TODO
break; break;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment