Commit 02bc882c authored by Damien George's avatar Damien George
Browse files

stmhal: Add file.flush and os.stat.

parent 5467186b
......@@ -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 },
......
......@@ -24,4 +24,6 @@
* THE SOFTWARE.
*/
const byte fresult_to_errno_table[20];
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_open_obj);
......@@ -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 },
......
......@@ -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);
......
......@@ -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);
......@@ -61,6 +61,7 @@ Q(rng)
Q(SD)
Q(SDcard)
Q(FileIO)
Q(flush)
// Entries for sys.path
Q(0:/)
Q(0:/lib)
......@@ -237,6 +238,7 @@ Q(remove)
Q(rmdir)
Q(unlink)
Q(sep)
Q(stat)
Q(urandom)
// for time module
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment