Skip to content

Commit f4c2922

Browse files
committed
【添加】rtthread.list_device() 功能和 os.mkfs() 功能
1 parent cfc398a commit f4c2922

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

port/genhdr/qstrdefs.generated.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,4 +763,5 @@ QDEF(MP_QSTR_PERIODIC, (const byte*)"\x0a\x08" "PERIODIC")
763763
QDEF(MP_QSTR_period, (const byte*)"\xa0\x06" "period")
764764
QDEF(MP_QSTR_set_color, (const byte*)"\x25\x09" "set_color")
765765
QDEF(MP_QSTR_file_crc32, (const byte*)"\x6f\x0a" "file_crc32")
766+
QDEF(MP_QSTR_list_device, (const byte*)"\x20\x0b" "list_device")
766767
// This file was automatically generated by makeqstrdata.py

port/modpyb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
9797
{ MP_ROM_QSTR(MP_QSTR_elapsed_micros), MP_ROM_PTR(&pyb_elapsed_micros_obj) },
9898
{ MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
9999
{ MP_ROM_QSTR(MP_QSTR_udelay), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
100-
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_os_mount_obj) },
100+
// { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_os_mount_obj) },
101101

102102
// { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
103103

port/modrtthread.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#if MICROPY_PY_RTTHREAD
2929

3030
#include <rtthread.h>
31+
#include <string.h>
3132

3233
#include "py/runtime.h"
3334

@@ -61,18 +62,39 @@ STATIC mp_obj_t mod_stacks_analyze(void) {
6162
}
6263
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_stacks_analyze_obj, mod_stacks_analyze);
6364

64-
STATIC mp_obj_t mp_os_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
65-
//TODO
66-
MP_RTT_NOT_IMPL_PRINT;
67-
return mp_const_none;
65+
STATIC mp_obj_t mod_list_device(void) {
66+
struct rt_device *device;
67+
struct rt_list_node *node;
68+
rt_ubase_t level;
69+
70+
struct rt_object_information *info = rt_object_get_information(RT_Object_Class_Device);
71+
struct rt_list_node *list = &info->object_list;
72+
mp_obj_t mp_list = mp_obj_new_list(0, NULL);
73+
74+
rt_enter_critical();
75+
76+
for (node = list->next; node != list; node = node->next)
77+
{
78+
device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
79+
80+
mp_obj_tuple_t *t = mp_obj_new_tuple(2, NULL);
81+
t->items[0] = mp_obj_new_str(device->parent.name, strlen((char *)device->parent.name));
82+
t->items[1] = MP_OBJ_NEW_SMALL_INT((device->type <= RT_Device_Class_Unknown) ? device->type : RT_Device_Class_Unknown);
83+
mp_obj_list_append(mp_list, MP_OBJ_FROM_PTR(t));
84+
}
85+
86+
rt_exit_critical();
87+
88+
return mp_list;
6889
}
69-
MP_DEFINE_CONST_FUN_OBJ_KW(mp_os_mount_obj, 2, mp_os_mount);
90+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_list_device_obj, mod_list_device);
7091

7192
STATIC const mp_rom_map_elem_t mp_module_rtthread_globals_table[] = {
7293
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rtthread) },
7394
{ MP_ROM_QSTR(MP_QSTR_is_preempt_thread), MP_ROM_PTR(&mod_is_preempt_thread_obj) },
7495
{ MP_ROM_QSTR(MP_QSTR_current_tid), MP_ROM_PTR(&mod_current_tid_obj) },
7596
{ MP_ROM_QSTR(MP_QSTR_stacks_analyze), MP_ROM_PTR(&mod_stacks_analyze_obj) },
97+
{ MP_ROM_QSTR(MP_QSTR_list_device), MP_ROM_PTR(&mod_list_device_obj) },
7698
};
7799

78100
STATIC MP_DEFINE_CONST_DICT(mp_module_rtthread_globals, mp_module_rtthread_globals_table);

port/moduos.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
124124
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) },
125125
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_posix_mount_obj) },
126126
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_posix_umount_obj) },
127+
{ MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&mp_posix_mkfs_obj) },
127128
//{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
128129
};
129130

port/moduos_file.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ mp_obj_t mp_posix_umount(mp_obj_t mnt_in) {
4848
}
4949
MP_DEFINE_CONST_FUN_OBJ_1(mp_posix_umount_obj, mp_posix_umount);
5050

51+
mp_obj_t mp_posix_mkfs(size_t n_args, const mp_obj_t *args) {
52+
53+
int result = RT_EOK;
54+
char *type = "elm"; /* use the default file system type as 'fatfs' */
55+
56+
if (n_args == 1)
57+
{
58+
result = dfs_mkfs(type, mp_obj_str_get_str(args[0]));
59+
}else if (n_args == 2)
60+
{
61+
type = (char *)mp_obj_str_get_str(args[0]);
62+
result = dfs_mkfs(type, mp_obj_str_get_str(args[1]));
63+
}
64+
65+
if (result != RT_EOK)
66+
{
67+
mp_raise_ValueError("mkfs failed, please check filesystem type and device name.");
68+
}
69+
70+
return mp_const_none;
71+
}
72+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_posix_mkfs_obj, 1, 2, mp_posix_mkfs);
73+
5174
mp_obj_t mp_posix_chdir(mp_obj_t path_in) {
5275
const char *changepath = mp_obj_str_get_str(path_in);
5376
if (chdir(changepath) != 0) {

port/moduos_file.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mp_obj_t mp_posix_rmdir(uint n_args, const mp_obj_t *arg);
5353
mp_obj_t mp_posix_stat(mp_obj_t path_in);
5454
mp_obj_t mp_posix_statvfs(mp_obj_t path_in);
5555
mp_obj_t mp_posix_file_crc32(mp_obj_t path_in);
56+
mp_obj_t mp_posix_mkfs(size_t n_args, const mp_obj_t *args);
5657

5758
MP_DECLARE_CONST_FUN_OBJ_KW(mp_posix_mount_obj);
5859
MP_DECLARE_CONST_FUN_OBJ_1(mp_posix_umount_obj);
@@ -67,5 +68,5 @@ MP_DECLARE_CONST_FUN_OBJ_VAR(mp_posix_rmdir_obj);
6768
MP_DECLARE_CONST_FUN_OBJ_1(mp_posix_stat_obj);
6869
MP_DECLARE_CONST_FUN_OBJ_1(mp_posix_statvfs_obj);
6970
MP_DECLARE_CONST_FUN_OBJ_1(mp_posix_file_crc32_obj);
70-
71+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_posix_mkfs_obj);
7172
#endif // MICROPY_INCLUDED_PY_MODUOS_FILE_H

0 commit comments

Comments
 (0)