summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal
diff options
context:
space:
mode:
Diffstat (limited to 'stmhal')
-rw-r--r--stmhal/file.c10
-rw-r--r--stmhal/file.h2
-rw-r--r--stmhal/main.c8
-rw-r--r--stmhal/modos.c63
-rw-r--r--stmhal/modpyb.c10
-rw-r--r--stmhal/modtime.c36
-rw-r--r--stmhal/mpconfigport.h2
-rw-r--r--stmhal/portmodules.h5
-rw-r--r--stmhal/printf.c172
-rw-r--r--stmhal/pybstdio.c44
-rw-r--r--stmhal/pybstdio.h5
-rw-r--r--stmhal/pyexec.c3
-rw-r--r--stmhal/qstrdefsport.h2
-rw-r--r--stmhal/readline.c3
-rw-r--r--stmhal/uart.c6
-rw-r--r--stmhal/uart.h3
-rw-r--r--stmhal/usb.c4
-rw-r--r--stmhal/usb.h1
18 files changed, 143 insertions, 236 deletions
diff --git a/stmhal/file.c b/stmhal/file.c
index 2d33a8f7b4..c1a2924903 100644
--- a/stmhal/file.c
+++ b/stmhal/file.c
@@ -41,7 +41,7 @@ extern const mp_obj_type_t mp_type_fileio;
extern const mp_obj_type_t mp_type_textio;
// this table converts from FRESULT to POSIX errno
-STATIC const byte fresult_to_errno_table[] = {
+const byte fresult_to_errno_table[20] = {
[FR_OK] = 0,
[FR_DISK_ERR] = EIO,
[FR_INT_ERR] = EIO,
@@ -95,6 +95,13 @@ STATIC mp_int_t file_obj_write(mp_obj_t self_in, const void *buf, mp_uint_t size
return sz_out;
}
+STATIC mp_obj_t file_obj_flush(mp_obj_t self_in) {
+ pyb_file_obj_t *self = self_in;
+ f_sync(&self->fp);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_flush_obj, file_obj_flush);
+
mp_obj_t file_obj_close(mp_obj_t self_in) {
pyb_file_obj_t *self = self_in;
f_close(&self->fp);
@@ -218,6 +225,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
{ MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj},
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&file_obj_flush_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_seek), (mp_obj_t)&file_obj_seek_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&file_obj_tell_obj },
diff --git a/stmhal/file.h b/stmhal/file.h
index 350785f74c..7522a337dc 100644
--- a/stmhal/file.h
+++ b/stmhal/file.h
@@ -24,4 +24,6 @@
* THE SOFTWARE.
*/
+const byte fresult_to_errno_table[20];
+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_open_obj);
diff --git a/stmhal/main.c b/stmhal/main.c
index ae4ff5141c..9e1798b3b9 100644
--- a/stmhal/main.c
+++ b/stmhal/main.c
@@ -43,7 +43,6 @@
#include "stackctrl.h"
#include "gc.h"
#include "gccollect.h"
-#include "pybstdio.h"
#include "readline.h"
#include "pyexec.h"
#include "i2c.h"
@@ -64,6 +63,7 @@
#include "servo.h"
#include "dac.h"
#include "pybwlan.h"
+#include "pybstdio.h"
void SystemClock_Config(void);
@@ -311,12 +311,10 @@ soft_reset:
MP_OBJ_NEW_SMALL_INT(PYB_UART_6),
MP_OBJ_NEW_SMALL_INT(115200),
};
- pyb_uart_global_debug = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type,
- MP_ARRAY_SIZE(args),
- 0, args);
+ pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
}
#else
- pyb_uart_global_debug = NULL;
+ pyb_stdio_uart = NULL;
#endif
// Micro Python init
diff --git a/stmhal/modos.c b/stmhal/modos.c
index dd6fd67ec9..35979ae972 100644
--- a/stmhal/modos.c
+++ b/stmhal/modos.c
@@ -32,10 +32,12 @@
#include "misc.h"
#include "qstr.h"
#include "obj.h"
+#include "objtuple.h"
#include "systick.h"
#include "rng.h"
#include "storage.h"
#include "ff.h"
+#include "file.h"
#include "portmodules.h"
#if _USE_LFN
@@ -107,8 +109,7 @@ STATIC mp_obj_t os_listdir(uint n_args, const mp_obj_t *args) {
return dir_list;
}
-
-MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
STATIC mp_obj_t os_mkdir(mp_obj_t path_o) {
const char *path = mp_obj_str_get_str(path_o);
@@ -123,8 +124,7 @@ STATIC mp_obj_t os_mkdir(mp_obj_t path_o) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
}
}
-
-MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
STATIC mp_obj_t os_remove(mp_obj_t path_o) {
const char *path = mp_obj_str_get_str(path_o);
@@ -137,8 +137,7 @@ STATIC mp_obj_t os_remove(mp_obj_t path_o) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
}
}
-
-MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
STATIC mp_obj_t os_rmdir(mp_obj_t path_o) {
const char *path = mp_obj_str_get_str(path_o);
@@ -151,15 +150,57 @@ STATIC mp_obj_t os_rmdir(mp_obj_t path_o) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing directory '%s'", path));
}
}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
+
+STATIC mp_obj_t os_stat(mp_obj_t path_in) {
+ const char *path = mp_obj_str_get_str(path_in);
+
+ FILINFO fno;
+#if _USE_LFN
+ fno.lfname = NULL;
+ fno.lfsize = 0;
+#endif
-MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
+ FRESULT res = f_stat(path, &fno);
+ if (res != FR_OK) {
+ nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
+ }
+
+ mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
+ mp_int_t mode = 0;
+ if (fno.fattrib & AM_DIR) {
+ mode |= 0x4000; // stat.S_IFDIR
+ } else {
+ mode |= 0x8000; // stat.S_IFREG
+ }
+ mp_int_t seconds = mod_time_seconds_since_2000(
+ 1980 + ((fno.fdate >> 9) & 0x7f),
+ (fno.fdate >> 5) & 0x0f,
+ fno.fdate & 0x1f,
+ (fno.ftime >> 11) & 0x1f,
+ (fno.ftime >> 5) & 0x3f,
+ 2 * (fno.ftime & 0x1f)
+ );
+ t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
+ t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
+ t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
+ t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
+ t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
+ t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
+ t->items[6] = MP_OBJ_NEW_SMALL_INT(fno.fsize); // st_size
+ t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
+ t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
+ t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
+
+ return t;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
STATIC mp_obj_t os_sync(void) {
storage_flush();
return mp_const_none;
}
-
-MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
+STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
STATIC mp_obj_t os_urandom(mp_obj_t num) {
mp_int_t n = mp_obj_get_int(num);
@@ -170,8 +211,7 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
}
return mp_obj_str_builder_end(o);
}
-
-MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
STATIC const mp_map_elem_t os_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_os) },
@@ -180,6 +220,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&os_sync_obj },
diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c
index 8b594332b7..38a680da24 100644
--- a/stmhal/modpyb.c
+++ b/stmhal/modpyb.c
@@ -37,7 +37,6 @@
#include "gc.h"
#include "gccollect.h"
#include "systick.h"
-#include "pybstdio.h"
#include "pyexec.h"
#include "led.h"
#include "pin.h"
@@ -57,6 +56,7 @@
#include "dac.h"
#include "lcd.h"
#include "usb.h"
+#include "pybstdio.h"
#include "ff.h"
#include "portmodules.h"
@@ -307,16 +307,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc);
/// Get or set the UART object that the REPL is repeated on.
STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
if (n_args == 0) {
- if (pyb_uart_global_debug == NULL) {
+ if (pyb_stdio_uart == NULL) {
return mp_const_none;
} else {
- return pyb_uart_global_debug;
+ return pyb_stdio_uart;
}
} else {
if (args[0] == mp_const_none) {
- pyb_uart_global_debug = NULL;
+ pyb_stdio_uart = NULL;
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
- pyb_uart_global_debug = args[0];
+ pyb_stdio_uart = args[0];
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
}
diff --git a/stmhal/modtime.c b/stmhal/modtime.c
index ea4e3210a6..f13bb15f84 100644
--- a/stmhal/modtime.c
+++ b/stmhal/modtime.c
@@ -34,22 +34,36 @@
#include "portmodules.h"
#include "rtc.h"
-STATIC const uint days_since_jan1[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
+STATIC const uint16_t days_since_jan1[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
-STATIC bool is_leap_year(uint year) {
+STATIC bool is_leap_year(mp_uint_t year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
// compute the day of the year, between 1 and 366
// month should be between 1 and 12, date should start at 1
-STATIC uint year_day(uint year, uint month, uint date) {
- uint yday = days_since_jan1[month - 1] + date;
+mp_uint_t mod_time_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date) {
+ mp_uint_t yday = days_since_jan1[month - 1] + date;
if (month >= 3 && is_leap_year(year)) {
yday += 1;
}
return yday;
}
+// returns the number of seconds, as an integer, since 1/1/2000
+mp_uint_t mod_time_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
+ return
+ second
+ + minute * 60
+ + hour * 3600
+ + (mod_time_year_day(year, month, date) - 1
+ + ((year - 2000 + 3) / 4) // add a day each 4 years starting with 2001
+ - ((year - 2000 + 99) / 100) // subtract a day each 100 years starting with 2001
+ + ((year - 2000 + 399) / 400) // add a day each 400 years starting with 2001
+ ) * 86400
+ + (year - 2000) * 31536000;
+}
+
// returns time stored in RTC as: (year, month, date, hour, minute, second, weekday)
// weekday is 0-6 for Mon-Sun
STATIC mp_obj_t time_localtime(void) {
@@ -67,7 +81,7 @@ STATIC mp_obj_t time_localtime(void) {
mp_obj_new_int(time.Minutes),
mp_obj_new_int(time.Seconds),
mp_obj_new_int(date.WeekDay - 1),
- mp_obj_new_int(year_day(2000 + date.Year, date.Month, date.Date)),
+ mp_obj_new_int(mod_time_year_day(2000 + date.Year, date.Month, date.Date)),
};
return mp_obj_new_tuple(8, tuple);
}
@@ -95,17 +109,7 @@ STATIC mp_obj_t time_time(void) {
RTC_TimeTypeDef time;
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN);
- return mp_obj_new_int(
- time.Seconds
- + time.Minutes * 60
- + time.Hours * 3600
- + (year_day(2000 + date.Year, date.Month, date.Date) - 1
- + ((date.Year + 3) / 4) // add a day each 4 years starting with 2001
- - ((date.Year + 99) / 100) // subtract a day each 100 years starting with 2001
- + ((date.Year + 399) / 400) // add a day each 400 years starting with 2001
- ) * 86400
- + date.Year * 31536000
- );
+ return mp_obj_new_int(mod_time_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds));
}
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h
index 00afa989cc..95f142ca48 100644
--- a/stmhal/mpconfigport.h
+++ b/stmhal/mpconfigport.h
@@ -44,7 +44,7 @@
*/
#define MICROPY_ENABLE_LFN (1)
#define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
-#define MICROPY_PY_BUILTINS_STR_UNICODE (0)
+#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
#define MICROPY_PY_BUILTINS_FROZENSET (1)
#define MICROPY_PY_SYS_EXIT (1)
#define MICROPY_PY_SYS_STDFILES (1)
diff --git a/stmhal/portmodules.h b/stmhal/portmodules.h
index f38feafe78..d8a3d8f879 100644
--- a/stmhal/portmodules.h
+++ b/stmhal/portmodules.h
@@ -28,3 +28,8 @@ extern const mp_obj_module_t os_module;
extern const mp_obj_module_t pyb_module;
extern const mp_obj_module_t stm_module;
extern const mp_obj_module_t time_module;
+
+// additional helper functions exported by the modules
+
+mp_uint_t mod_time_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date);
+mp_uint_t mod_time_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second);
diff --git a/stmhal/printf.c b/stmhal/printf.c
index c4731aa88d..db611f3d97 100644
--- a/stmhal/printf.c
+++ b/stmhal/printf.c
@@ -40,184 +40,30 @@
#endif
#include "uart.h"
#include "usb.h"
+#include "pybstdio.h"
#if MICROPY_PY_BUILTINS_FLOAT
#include "formatfloat.h"
#endif
-void pfenv_prints(const pfenv_t *pfenv, const char *str) {
- pfenv->print_strn(pfenv->data, str, strlen(str));
-}
-
-int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
- int chrs = 0;
- for (;;) {
- {
- const char *f = fmt;
- while (*f != '\0' && *f != '%') {
- ++f; // XXX UTF8 advance char
- }
- if (f > fmt) {
- pfenv->print_strn(pfenv->data, fmt, f - fmt);
- chrs += f - fmt;
- fmt = f;
- }
- }
+int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args);
- if (*fmt == '\0') {
- break;
- }
-
- // move past % character
- ++fmt;
-
- // parse flags, if they exist
- int flags = 0;
- char fill = ' ';
- while (*fmt != '\0') {
- if (*fmt == '-') flags |= PF_FLAG_LEFT_ADJUST;
- else if (*fmt == '+') flags |= PF_FLAG_SHOW_SIGN;
- else if (*fmt == ' ') flags |= PF_FLAG_SPACE_SIGN;
- else if (*fmt == '!') flags |= PF_FLAG_NO_TRAILZ;
- else if (*fmt == '0') {
- flags |= PF_FLAG_PAD_AFTER_SIGN;
- fill = '0';
- } else break;
- ++fmt;
- }
-
- // parse width, if it exists
- int width = 0;
- for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
- width = width * 10 + *fmt - '0';
- }
-
- // parse precision, if it exists
- int prec = -1;
- if (*fmt == '.') {
- ++fmt;
- if (*fmt == '*') {
- ++fmt;
- prec = va_arg(args, int);
- } else {
- prec = 0;
- for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
- prec = prec * 10 + *fmt - '0';
- }
- }
- if (prec < 0) {
- prec = 0;
- }
- }
-
- // parse long specifiers (current not used)
- //bool long_arg = false;
- if (*fmt == 'l') {
- ++fmt;
- //long_arg = true;
- }
-
- if (*fmt == '\0') {
- break;
- }
-
- switch (*fmt) {
- case 'b':
- if (va_arg(args, int)) {
- chrs += pfenv_print_strn(pfenv, "true", 4, flags, fill, width);
- } else {
- chrs += pfenv_print_strn(pfenv, "false", 5, flags, fill, width);
- }
- break;
- case 'c':
- {
- char str = va_arg(args, int);
- chrs += pfenv_print_strn(pfenv, &str, 1, flags, fill, width);
- break;
- }
- case 's':
- {
- const char *str = va_arg(args, const char*);
- if (str) {
- if (prec < 0) {
- prec = strlen(str);
- }
- chrs += pfenv_print_strn(pfenv, str, prec, flags, fill, width);
- } else {
- chrs += pfenv_print_strn(pfenv, "(null)", 6, flags, fill, width);
- }
- break;
- }
- case 'u':
- chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 10, 'a', flags, fill, width);
- break;
- case 'd':
- chrs += pfenv_print_int(pfenv, va_arg(args, int), 1, 10, 'a', flags, fill, width);
- break;
- case 'x':
- case 'p': // ?
- chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'a', flags, fill, width);
- break;
- case 'X':
- case 'P': // ?
- chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'A', flags, fill, width);
- break;
-#if MICROPY_PY_BUILTINS_FLOAT
- case 'e':
- case 'E':
- case 'f':
- case 'F':
- case 'g':
- case 'G':
- {
-#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
- mp_float_t f = va_arg(args, double);
- chrs += pfenv_print_float(pfenv, f, *fmt, flags, fill, width, prec);
-#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
- // Currently pfenv_print_float uses snprintf, so if you want
- // to use pfenv_print_float with doubles then you'll need
- // fix it to not use snprintf first. Otherwise you'll have
- // inifinite recursion.
-#error Calling pfenv_print_float with double not supported from within printf
-#else
-#error Unknown MICROPY FLOAT IMPL
-#endif
- break;
- }
-#endif
- default:
- pfenv->print_strn(pfenv->data, fmt, 1);
- chrs += 1;
- break;
- }
- ++fmt;
- }
- return chrs;
-}
-
-STATIC void stdout_print_strn(void *data, const char *str, unsigned int len) {
- // TODO this needs to be replaced with a proper stdio interface ala CPython
- // send stdout to UART and USB CDC VCP
- if (pyb_uart_global_debug != PYB_UART_NONE) {
- uart_tx_strn_cooked(pyb_uart_global_debug, str, len);
- }
- if (usb_vcp_is_enabled()) {
- usb_vcp_send_strn_cooked(str, len);
- }
+STATIC void stdout_print_strn(void *dummy_env, const char *str, unsigned int len) {
+ stdout_tx_strn_cooked(str, len);
}
-static const pfenv_t pfenv_stdout = {0, stdout_print_strn};
+STATIC const pfenv_t pfenv_stdout = {0, stdout_print_strn};
int printf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
- int ret = pfenv_printf(&pfenv_stdout, fmt, ap);
+ int ret = pfenv_vprintf(&pfenv_stdout, fmt, ap);
va_end(ap);
return ret;
}
int vprintf(const char *fmt, va_list ap) {
- return pfenv_printf(&pfenv_stdout, fmt, ap);
+ return pfenv_vprintf(&pfenv_stdout, fmt, ap);
}
#if MICROPY_DEBUG_PRINTERS
@@ -225,7 +71,7 @@ int DEBUG_printf(const char *fmt, ...) {
(void)stream;
va_list ap;
va_start(ap, fmt);
- int ret = pfenv_printf(&pfenv_stdout, fmt, ap);
+ int ret = pfenv_vprintf(&pfenv_stdout, fmt, ap);
va_end(ap);
return ret;
}
@@ -268,7 +114,7 @@ int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
pfenv_t pfenv;
pfenv.data = &strn_pfenv;
pfenv.print_strn = strn_print_strn;
- int len = pfenv_printf(&pfenv, fmt, ap);
+ int len = pfenv_vprintf(&pfenv, fmt, ap);
// add terminating null byte
if (size > 0) {
if (strn_pfenv.remain == 0) {
diff --git a/stmhal/pybstdio.c b/stmhal/pybstdio.c
index 2a4386e097..9846ce0453 100644
--- a/stmhal/pybstdio.c
+++ b/stmhal/pybstdio.c
@@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdint.h>
+#include <string.h>
#include "mpconfig.h"
#include "misc.h"
@@ -34,31 +35,41 @@
#include "obj.h"
#include "stream.h"
#include MICROPY_HAL_H
-#include "pybstdio.h"
#include "usb.h"
#include "uart.h"
+#include "pybstdio.h"
// TODO make stdin, stdout and stderr writable objects so they can
-// be changed by Python code.
+// be changed by Python code. This requires some changes, as these
+// objects are in a read-only module (py/modsys.c).
+
+// stdio is repeated on this UART object if it's not null
+pyb_uart_obj_t *pyb_stdio_uart = NULL;
void stdout_tx_str(const char *str) {
- if (pyb_uart_global_debug != PYB_UART_NONE) {
- uart_tx_str(pyb_uart_global_debug, str);
- }
-#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
- lcd_print_str(str);
-#endif
- usb_vcp_send_str(str);
+ stdout_tx_strn(str, strlen(str));
}
-void stdout_tx_strn(const char *str, uint len) {
- if (pyb_uart_global_debug != PYB_UART_NONE) {
- uart_tx_strn(pyb_uart_global_debug, str, len);
+void stdout_tx_strn(const char *str, mp_uint_t len) {
+ if (pyb_stdio_uart != PYB_UART_NONE) {
+ uart_tx_strn(pyb_stdio_uart, str, len);
}
#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
lcd_print_strn(str, len);
#endif
- usb_vcp_send_strn(str, len);
+ if (usb_vcp_is_enabled()) {
+ usb_vcp_send_strn(str, len);
+ }
+}
+
+void stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
+ // send stdout to UART and USB CDC VCP
+ if (pyb_stdio_uart != PYB_UART_NONE) {
+ uart_tx_strn_cooked(pyb_stdio_uart, str, len);
+ }
+ if (usb_vcp_is_enabled()) {
+ usb_vcp_send_strn_cooked(str, len);
+ }
}
int stdin_rx_chr(void) {
@@ -74,14 +85,13 @@ int stdin_rx_chr(void) {
#endif
if (usb_vcp_rx_num() != 0) {
return usb_vcp_rx_get();
- } else if (pyb_uart_global_debug != PYB_UART_NONE && uart_rx_any(pyb_uart_global_debug)) {
- return uart_rx_char(pyb_uart_global_debug);
+ } else if (pyb_stdio_uart != PYB_UART_NONE && uart_rx_any(pyb_stdio_uart)) {
+ return uart_rx_char(pyb_stdio_uart);
}
__WFI();
}
}
-
/******************************************************************************/
// Micro Python bindings
@@ -120,7 +130,7 @@ STATIC mp_int_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *err
STATIC mp_int_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
pyb_stdio_obj_t *self = self_in;
if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
- stdout_tx_strn(buf, size);
+ stdout_tx_strn_cooked(buf, size);
*errcode = 0;
return size;
} else {
diff --git a/stmhal/pybstdio.h b/stmhal/pybstdio.h
index 5beab893da..cdb7acfe59 100644
--- a/stmhal/pybstdio.h
+++ b/stmhal/pybstdio.h
@@ -24,6 +24,9 @@
* THE SOFTWARE.
*/
+extern pyb_uart_obj_t *pyb_stdio_uart;
+
void stdout_tx_str(const char *str);
-void stdout_tx_strn(const char *str, uint len);
+void stdout_tx_strn(const char *str, mp_uint_t len);
+void stdout_tx_strn_cooked(const char *str, mp_uint_t len);
int stdin_rx_chr(void);
diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c
index 93f4d62a96..cac6f0cf02 100644
--- a/stmhal/pyexec.c
+++ b/stmhal/pyexec.c
@@ -44,10 +44,11 @@
#include "gccollect.h"
#include MICROPY_HAL_H
#include "systick.h"
-#include "pybstdio.h"
#include "readline.h"
#include "pyexec.h"
#include "usb.h"
+#include "uart.h"
+#include "pybstdio.h"
#include "genhdr/py-version.h"
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h
index eb22d616b3..8d277fdedc 100644
--- a/stmhal/qstrdefsport.h
+++ b/stmhal/qstrdefsport.h
@@ -61,6 +61,7 @@ Q(rng)
Q(SD)
Q(SDcard)
Q(FileIO)
+Q(flush)
// Entries for sys.path
Q(0:/)
Q(0:/lib)
@@ -238,6 +239,7 @@ Q(remove)
Q(rmdir)
Q(unlink)
Q(sep)
+Q(stat)
Q(urandom)
// for time module
diff --git a/stmhal/readline.c b/stmhal/readline.c
index 0703dcf4ea..14c9b9fb96 100644
--- a/stmhal/readline.c
+++ b/stmhal/readline.c
@@ -34,9 +34,10 @@
#include "misc.h"
#include "obj.h"
#include MICROPY_HAL_H
-#include "pybstdio.h"
#include "readline.h"
#include "usb.h"
+#include "uart.h"
+#include "pybstdio.h"
#if 0 // print debugging info
#define DEBUG_PRINT (1)
diff --git a/stmhal/uart.c b/stmhal/uart.c
index 9436c2938b..0418e8f3d0 100644
--- a/stmhal/uart.c
+++ b/stmhal/uart.c
@@ -65,8 +65,6 @@ struct _pyb_uart_obj_t {
UART_HandleTypeDef uart;
};
-pyb_uart_obj_t *pyb_uart_global_debug = NULL;
-
// assumes Init parameters have been set up correctly
bool uart_init2(pyb_uart_obj_t *uart_obj) {
USART_TypeDef *UARTx = NULL;
@@ -219,10 +217,6 @@ void uart_tx_char(pyb_uart_obj_t *uart_obj, int c) {
HAL_UART_Transmit(&uart_obj->uart, &ch, 1, 100000);
}
-void uart_tx_str(pyb_uart_obj_t *uart_obj, const char *str) {
- HAL_UART_Transmit(&uart_obj->uart, (uint8_t*)str, strlen(str), 100000);
-}
-
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len) {
HAL_UART_Transmit(&uart_obj->uart, (uint8_t*)str, len, 100000);
}
diff --git a/stmhal/uart.h b/stmhal/uart.h
index 13ac92cbfa..7499473c16 100644
--- a/stmhal/uart.h
+++ b/stmhal/uart.h
@@ -43,14 +43,11 @@ typedef enum {
} pyb_uart_t;
typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
-
-extern pyb_uart_obj_t *pyb_uart_global_debug;
extern const mp_obj_type_t pyb_uart_type;
bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate);
bool uart_rx_any(pyb_uart_obj_t *uart_obj);
int uart_rx_char(pyb_uart_obj_t *uart_obj);
-void uart_tx_str(pyb_uart_obj_t *uart_obj, const char *str);
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);
diff --git a/stmhal/usb.c b/stmhal/usb.c
index 50848f4555..85dd2b4eaf 100644
--- a/stmhal/usb.c
+++ b/stmhal/usb.c
@@ -107,10 +107,6 @@ char usb_vcp_rx_get(void) {
return USBD_CDC_RxGet();
}
-void usb_vcp_send_str(const char *str) {
- usb_vcp_send_strn(str, strlen(str));
-}
-
void usb_vcp_send_strn(const char *str, int len) {
#ifdef USE_DEVICE_MODE
if (dev_is_enabled) {
diff --git a/stmhal/usb.h b/stmhal/usb.h
index 3bd30ba92c..4eb29c9dee 100644
--- a/stmhal/usb.h
+++ b/stmhal/usb.h
@@ -48,7 +48,6 @@ bool usb_vcp_is_connected(void);
void usb_vcp_set_interrupt_char(int c);
int usb_vcp_rx_num(void);
char usb_vcp_rx_get(void);
-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, ?