4141#include "sdcard.h"
4242#include "portmodules.h"
4343
44+ /// \module os - basic "operating system" services
45+ ///
46+ /// The `os` module contains functions for filesystem access and `urandom`.
47+ ///
48+ /// The filesystem has `/` as the root directory, and the available physical
49+ /// drives are accessible from here. They are currently:
50+ ///
51+ /// /flash -- the internal flash filesystem
52+ /// /sd -- the SD card (if it exists)
53+ ///
54+ /// On boot up, the current directory is `/flash` if no SD card is inserted,
55+ /// otherwise it is `/sd`.
56+
4457#if _USE_LFN
4558static char lfn [_MAX_LFN + 1 ]; /* Buffer to store the LFN */
4659#endif
@@ -54,6 +67,8 @@ STATIC bool sd_in_root(void) {
5467#endif
5568}
5669
70+ /// \function chdir(path)
71+ /// Change current directory.
5772STATIC mp_obj_t os_chdir (mp_obj_t path_in ) {
5873 const char * path ;
5974 path = mp_obj_str_get_str (path_in );
@@ -73,6 +88,8 @@ STATIC mp_obj_t os_chdir(mp_obj_t path_in) {
7388}
7489STATIC MP_DEFINE_CONST_FUN_OBJ_1 (os_chdir_obj , os_chdir );
7590
91+ /// \function getcwd()
92+ /// Get the current directory.
7693STATIC mp_obj_t os_getcwd (void ) {
7794 char buf [MICROPY_ALLOC_PATH_MAX + 1 ];
7895 FRESULT res = f_getcwd (buf , sizeof buf );
@@ -85,6 +102,8 @@ STATIC mp_obj_t os_getcwd(void) {
85102}
86103STATIC MP_DEFINE_CONST_FUN_OBJ_0 (os_getcwd_obj , os_getcwd );
87104
105+ /// \function listdir([dir])
106+ /// With no argument, list the current directory. Otherwise list the given directory.
88107STATIC mp_obj_t os_listdir (uint n_args , const mp_obj_t * args ) {
89108 bool is_str_type = true;
90109 const char * path ;
@@ -161,6 +180,8 @@ STATIC mp_obj_t os_listdir(uint n_args, const mp_obj_t *args) {
161180}
162181STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (os_listdir_obj , 0 , 1 , os_listdir );
163182
183+ /// \function mkdir(path)
184+ /// Create a new directory.
164185STATIC mp_obj_t os_mkdir (mp_obj_t path_o ) {
165186 const char * path = mp_obj_str_get_str (path_o );
166187 FRESULT res = f_mkdir (path );
@@ -176,6 +197,8 @@ STATIC mp_obj_t os_mkdir(mp_obj_t path_o) {
176197}
177198STATIC MP_DEFINE_CONST_FUN_OBJ_1 (os_mkdir_obj , os_mkdir );
178199
200+ /// \function remove(path)
201+ /// Remove a file.
179202STATIC mp_obj_t os_remove (mp_obj_t path_o ) {
180203 const char * path = mp_obj_str_get_str (path_o );
181204 // TODO check that path is actually a file before trying to unlink it
@@ -189,6 +212,8 @@ STATIC mp_obj_t os_remove(mp_obj_t path_o) {
189212}
190213STATIC MP_DEFINE_CONST_FUN_OBJ_1 (os_remove_obj , os_remove );
191214
215+ /// \function rmdir(path)
216+ /// Remove a directory.
192217STATIC mp_obj_t os_rmdir (mp_obj_t path_o ) {
193218 const char * path = mp_obj_str_get_str (path_o );
194219 // TODO check that path is actually a directory before trying to unlink it
@@ -217,6 +242,8 @@ STATIC bool path_equal(const char *path, const char *path_canonical) {
217242 return * path == '\0' ;
218243}
219244
245+ /// \function stat(path)
246+ /// Get the status of a file or directory.
220247STATIC mp_obj_t os_stat (mp_obj_t path_in ) {
221248 const char * path = mp_obj_str_get_str (path_in );
222249
@@ -278,13 +305,18 @@ STATIC mp_obj_t os_stat(mp_obj_t path_in) {
278305}
279306STATIC MP_DEFINE_CONST_FUN_OBJ_1 (os_stat_obj , os_stat );
280307
308+ /// \function sync()
309+ /// Sync all filesystems.
281310STATIC mp_obj_t os_sync (void ) {
282311 storage_flush ();
283312 return mp_const_none ;
284313}
285314STATIC MP_DEFINE_CONST_FUN_OBJ_0 (os_sync_obj , os_sync );
286315
287316#if MICROPY_HW_ENABLE_RNG
317+ /// \function urandom(n)
318+ /// Return a bytes object with n random bytes, generated by the hardware
319+ /// random number generator.
288320STATIC mp_obj_t os_urandom (mp_obj_t num ) {
289321 mp_int_t n = mp_obj_get_int (num );
290322 byte * data ;
@@ -311,6 +343,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
311343
312344 { MP_OBJ_NEW_QSTR (MP_QSTR_sync ), (mp_obj_t )& os_sync_obj },
313345
346+ /// \constant sep - separation character used in paths
314347 { MP_OBJ_NEW_QSTR (MP_QSTR_sep ), MP_OBJ_NEW_QSTR (MP_QSTR__slash_ ) },
315348
316349#if MICROPY_HW_ENABLE_RNG
0 commit comments