Skip to content

Commit 02bc882

Browse files
committed
stmhal: Add file.flush and os.stat.
1 parent 5467186 commit 02bc882

6 files changed

Lines changed: 90 additions & 28 deletions

File tree

stmhal/file.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extern const mp_obj_type_t mp_type_fileio;
4141
extern const mp_obj_type_t mp_type_textio;
4242

4343
// this table converts from FRESULT to POSIX errno
44-
STATIC const byte fresult_to_errno_table[] = {
44+
const byte fresult_to_errno_table[20] = {
4545
[FR_OK] = 0,
4646
[FR_DISK_ERR] = EIO,
4747
[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
9595
return sz_out;
9696
}
9797

98+
STATIC mp_obj_t file_obj_flush(mp_obj_t self_in) {
99+
pyb_file_obj_t *self = self_in;
100+
f_sync(&self->fp);
101+
return mp_const_none;
102+
}
103+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(file_obj_flush_obj, file_obj_flush);
104+
98105
mp_obj_t file_obj_close(mp_obj_t self_in) {
99106
pyb_file_obj_t *self = self_in;
100107
f_close(&self->fp);
@@ -218,6 +225,7 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
218225
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
219226
{ MP_OBJ_NEW_QSTR(MP_QSTR_readlines), (mp_obj_t)&mp_stream_unbuffered_readlines_obj},
220227
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
228+
{ MP_OBJ_NEW_QSTR(MP_QSTR_flush), (mp_obj_t)&file_obj_flush_obj },
221229
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
222230
{ MP_OBJ_NEW_QSTR(MP_QSTR_seek), (mp_obj_t)&file_obj_seek_obj },
223231
{ MP_OBJ_NEW_QSTR(MP_QSTR_tell), (mp_obj_t)&file_obj_tell_obj },

stmhal/file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
const byte fresult_to_errno_table[20];
28+
2729
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_open_obj);

stmhal/modos.c

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
#include "misc.h"
3333
#include "qstr.h"
3434
#include "obj.h"
35+
#include "objtuple.h"
3536
#include "systick.h"
3637
#include "rng.h"
3738
#include "storage.h"
3839
#include "ff.h"
40+
#include "file.h"
3941
#include "portmodules.h"
4042

4143
#if _USE_LFN
@@ -107,8 +109,7 @@ STATIC mp_obj_t os_listdir(uint n_args, const mp_obj_t *args) {
107109

108110
return dir_list;
109111
}
110-
111-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
112+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
112113

113114
STATIC mp_obj_t os_mkdir(mp_obj_t path_o) {
114115
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) {
123124
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
124125
}
125126
}
126-
127-
MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
127+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
128128

129129
STATIC mp_obj_t os_remove(mp_obj_t path_o) {
130130
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) {
137137
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
138138
}
139139
}
140-
141-
MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
140+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
142141

143142
STATIC mp_obj_t os_rmdir(mp_obj_t path_o) {
144143
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) {
151150
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing directory '%s'", path));
152151
}
153152
}
153+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
154+
155+
STATIC mp_obj_t os_stat(mp_obj_t path_in) {
156+
const char *path = mp_obj_str_get_str(path_in);
157+
158+
FILINFO fno;
159+
#if _USE_LFN
160+
fno.lfname = NULL;
161+
fno.lfsize = 0;
162+
#endif
154163

155-
MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
164+
FRESULT res = f_stat(path, &fno);
165+
if (res != FR_OK) {
166+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
167+
}
168+
169+
mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL);
170+
mp_int_t mode = 0;
171+
if (fno.fattrib & AM_DIR) {
172+
mode |= 0x4000; // stat.S_IFDIR
173+
} else {
174+
mode |= 0x8000; // stat.S_IFREG
175+
}
176+
mp_int_t seconds = mod_time_seconds_since_2000(
177+
1980 + ((fno.fdate >> 9) & 0x7f),
178+
(fno.fdate >> 5) & 0x0f,
179+
fno.fdate & 0x1f,
180+
(fno.ftime >> 11) & 0x1f,
181+
(fno.ftime >> 5) & 0x3f,
182+
2 * (fno.ftime & 0x1f)
183+
);
184+
t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
185+
t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
186+
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
187+
t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
188+
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
189+
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
190+
t->items[6] = MP_OBJ_NEW_SMALL_INT(fno.fsize); // st_size
191+
t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
192+
t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
193+
t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
194+
195+
return t;
196+
}
197+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
156198

157199
STATIC mp_obj_t os_sync(void) {
158200
storage_flush();
159201
return mp_const_none;
160202
}
161-
162-
MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
203+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
163204

164205
STATIC mp_obj_t os_urandom(mp_obj_t num) {
165206
mp_int_t n = mp_obj_get_int(num);
@@ -170,8 +211,7 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
170211
}
171212
return mp_obj_str_builder_end(o);
172213
}
173-
174-
MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
214+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
175215

176216
STATIC const mp_map_elem_t os_module_globals_table[] = {
177217
{ 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[] = {
180220
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj },
181221
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj },
182222
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj },
223+
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
183224
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove
184225

185226
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&os_sync_obj },

stmhal/modtime.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,36 @@
3434
#include "portmodules.h"
3535
#include "rtc.h"
3636

37-
STATIC const uint days_since_jan1[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
37+
STATIC const uint16_t days_since_jan1[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3838

39-
STATIC bool is_leap_year(uint year) {
39+
STATIC bool is_leap_year(mp_uint_t year) {
4040
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
4141
}
4242

4343
// compute the day of the year, between 1 and 366
4444
// month should be between 1 and 12, date should start at 1
45-
STATIC uint year_day(uint year, uint month, uint date) {
46-
uint yday = days_since_jan1[month - 1] + date;
45+
mp_uint_t mod_time_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date) {
46+
mp_uint_t yday = days_since_jan1[month - 1] + date;
4747
if (month >= 3 && is_leap_year(year)) {
4848
yday += 1;
4949
}
5050
return yday;
5151
}
5252

53+
// returns the number of seconds, as an integer, since 1/1/2000
54+
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) {
55+
return
56+
second
57+
+ minute * 60
58+
+ hour * 3600
59+
+ (mod_time_year_day(year, month, date) - 1
60+
+ ((year - 2000 + 3) / 4) // add a day each 4 years starting with 2001
61+
- ((year - 2000 + 99) / 100) // subtract a day each 100 years starting with 2001
62+
+ ((year - 2000 + 399) / 400) // add a day each 400 years starting with 2001
63+
) * 86400
64+
+ (year - 2000) * 31536000;
65+
}
66+
5367
// returns time stored in RTC as: (year, month, date, hour, minute, second, weekday)
5468
// weekday is 0-6 for Mon-Sun
5569
STATIC mp_obj_t time_localtime(void) {
@@ -67,7 +81,7 @@ STATIC mp_obj_t time_localtime(void) {
6781
mp_obj_new_int(time.Minutes),
6882
mp_obj_new_int(time.Seconds),
6983
mp_obj_new_int(date.WeekDay - 1),
70-
mp_obj_new_int(year_day(2000 + date.Year, date.Month, date.Date)),
84+
mp_obj_new_int(mod_time_year_day(2000 + date.Year, date.Month, date.Date)),
7185
};
7286
return mp_obj_new_tuple(8, tuple);
7387
}
@@ -95,17 +109,7 @@ STATIC mp_obj_t time_time(void) {
95109
RTC_TimeTypeDef time;
96110
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
97111
HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN);
98-
return mp_obj_new_int(
99-
time.Seconds
100-
+ time.Minutes * 60
101-
+ time.Hours * 3600
102-
+ (year_day(2000 + date.Year, date.Month, date.Date) - 1
103-
+ ((date.Year + 3) / 4) // add a day each 4 years starting with 2001
104-
- ((date.Year + 99) / 100) // subtract a day each 100 years starting with 2001
105-
+ ((date.Year + 399) / 400) // add a day each 400 years starting with 2001
106-
) * 86400
107-
+ date.Year * 31536000
108-
);
112+
return mp_obj_new_int(mod_time_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds));
109113
}
110114
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
111115

stmhal/portmodules.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ extern const mp_obj_module_t os_module;
2828
extern const mp_obj_module_t pyb_module;
2929
extern const mp_obj_module_t stm_module;
3030
extern const mp_obj_module_t time_module;
31+
32+
// additional helper functions exported by the modules
33+
34+
mp_uint_t mod_time_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date);
35+
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);

stmhal/qstrdefsport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Q(rng)
6161
Q(SD)
6262
Q(SDcard)
6363
Q(FileIO)
64+
Q(flush)
6465
// Entries for sys.path
6566
Q(0:/)
6667
Q(0:/lib)
@@ -237,6 +238,7 @@ Q(remove)
237238
Q(rmdir)
238239
Q(unlink)
239240
Q(sep)
241+
Q(stat)
240242
Q(urandom)
241243

242244
// for time module

0 commit comments

Comments
 (0)