Skip to content

Commit

Permalink
update components & lwp. (#7888)
Browse files Browse the repository at this point in the history
  • Loading branch information
geniusgogo committed Aug 7, 2023
1 parent 7e7b303 commit 4d20416
Show file tree
Hide file tree
Showing 19 changed files with 968 additions and 80 deletions.
225 changes: 225 additions & 0 deletions components/drivers/sdio/block_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <rtthread.h>
#include <dfs_fs.h>
#include <dfs_file.h>

#include <drivers/mmcsd_core.h>
#include <drivers/gpt.h>
Expand All @@ -25,6 +26,8 @@
static rt_list_t blk_devices = RT_LIST_OBJECT_INIT(blk_devices);

#define BLK_MIN(a, b) ((a) < (b) ? (a) : (b))
#define RT_DEVICE_CTRL_BLK_SSIZEGET 0x1268 /**< get number of bytes per sector */
#define RT_DEVICE_CTRL_ALL_BLK_SSIZEGET 0x80081272 /**< get number of bytes per sector * sector counts*/

struct mmcsd_blk_device
{
Expand Down Expand Up @@ -267,13 +270,24 @@ static rt_err_t rt_mmcsd_close(rt_device_t dev)
static rt_err_t rt_mmcsd_control(rt_device_t dev, int cmd, void *args)
{
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;

switch (cmd)
{
case RT_DEVICE_CTRL_BLK_GETGEOME:
rt_memcpy(args, &blk_dev->geometry, sizeof(struct rt_device_blk_geometry));
break;
case RT_DEVICE_CTRL_BLK_PARTITION:
rt_memcpy(args, &blk_dev->part, sizeof(struct dfs_partition));
break;
case RT_DEVICE_CTRL_BLK_SSIZEGET:
rt_memcpy(args, &blk_dev->geometry.bytes_per_sector, sizeof(rt_uint32_t));
break;
case RT_DEVICE_CTRL_ALL_BLK_SSIZEGET:
{
rt_uint64_t count_mul_per = blk_dev->geometry.bytes_per_sector * blk_dev->geometry.sector_count;
rt_memcpy(args, &count_mul_per, sizeof(rt_uint64_t));
}
break;
default:
break;
}
Expand Down Expand Up @@ -414,6 +428,207 @@ const static struct rt_device_ops mmcsd_blk_ops =
};
#endif

#ifdef RT_USING_DFS_V2

static ssize_t rt_mmcsd_fops_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
{
int result = 0;
rt_device_t dev = (rt_device_t)file->vnode->data;
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
int bytes_per_sector = blk_dev->geometry.bytes_per_sector;
int blk_pos = *pos / bytes_per_sector;
int first_offs = *pos % bytes_per_sector;
char *rbuf;
int rsize = 0;

rbuf = rt_malloc(bytes_per_sector);
if (!rbuf)
{
return 0;
}

/*
** #1: read first unalign block size.
*/
result = rt_mmcsd_read(dev, blk_pos, rbuf, 1);
if (result != 1)
{
rt_free(rbuf);
return 0;
}

if (count > bytes_per_sector - first_offs)
{
rsize = bytes_per_sector - first_offs;
}
else
{
rsize = count;
}
rt_memcpy(buf, rbuf + first_offs, rsize);
blk_pos++;

/*
** #2: read continuous block size.
*/
while (rsize < count)
{
result = rt_mmcsd_read(dev, blk_pos++, rbuf, 1);
if (result != 1)
{
break;
}

if (count - rsize >= bytes_per_sector)
{
rt_memcpy(buf + rsize, rbuf, bytes_per_sector);
rsize += bytes_per_sector;
}
else
{
rt_memcpy(buf + rsize, rbuf, count - rsize);
rsize = count;
}
}

rt_free(rbuf);
*pos += rsize;

return rsize;
}

static int rt_mmcsd_fops_ioctl(struct dfs_file *file, int cmd, void *arg)
{
rt_device_t dev = (rt_device_t)file->vnode->data;

return rt_mmcsd_control(dev,cmd,arg);
}

static int rt_mmcsd_fops_open(struct dfs_file *file)
{
rt_device_t dev = (rt_device_t)file->vnode->data;
rt_mmcsd_control(dev, RT_DEVICE_CTRL_ALL_BLK_SSIZEGET, &file->vnode->size);
return RT_EOK;
}

static int rt_mmcsd_fops_close(struct dfs_file *file)
{
return RT_EOK;
}

static ssize_t rt_mmcsd_fops_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
{
int result = 0;
rt_device_t dev = (rt_device_t)file->vnode->data;
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
int bytes_per_sector = blk_dev->geometry.bytes_per_sector;
int blk_pos = *pos / bytes_per_sector;
int first_offs = *pos % bytes_per_sector;
char *rbuf = 0;
int wsize = 0;

/*
** #1: write first unalign block size.
*/
if (first_offs != 0)
{
if (count > bytes_per_sector - first_offs)
{
wsize = bytes_per_sector - first_offs;
}
else
{
wsize = count;
}

rbuf = rt_malloc(bytes_per_sector);
if (!rbuf)
{
return 0;
}

result = rt_mmcsd_read(dev, blk_pos, rbuf, 1);
if (result != 1)
{
rt_free(rbuf);
return 0;
}

rt_memcpy(rbuf + first_offs, buf, wsize);
result = rt_mmcsd_write(dev, blk_pos, rbuf, 1);
if (result != 1)
{
rt_free(rbuf);
return 0;
}
rt_free(rbuf);
blk_pos += 1;
}

/*
** #2: write continuous block size.
*/
if ((count - wsize) / bytes_per_sector != 0)
{
result = rt_mmcsd_write(dev, blk_pos, buf + wsize, (count - wsize) / bytes_per_sector);
wsize += result * bytes_per_sector;
blk_pos += result;
if (result != (count - wsize) / bytes_per_sector)
{
*pos += wsize;
return wsize;
}
}

/*
** # 3: write last unalign block size.
*/
if ((count - wsize) != 0)
{
rbuf = rt_malloc(bytes_per_sector);
if (rbuf != RT_NULL)
{
result = rt_mmcsd_read(dev, blk_pos, rbuf, 1);
if (result == 1)
{
rt_memcpy(rbuf, buf + wsize, count - wsize);
result = rt_mmcsd_write(dev, blk_pos, rbuf, 1);
if (result == 1)
{
wsize += count - wsize;
}
}

rt_free(rbuf);
}
}

*pos += wsize;
return wsize;
}

static int rt_mmcsd_fops_poll(struct dfs_file *file, struct rt_pollreq *req)
{
int mask = 0;

return mask;
}

const static struct dfs_file_ops mmcsd_blk_fops =
{
rt_mmcsd_fops_open,
rt_mmcsd_fops_close,
rt_mmcsd_fops_ioctl,
rt_mmcsd_fops_read,
rt_mmcsd_fops_write,
RT_NULL,
generic_dfs_lseek,
RT_NULL,
RT_NULL,
rt_mmcsd_fops_poll
};
#endif

rt_int32_t gpt_device_probe(struct rt_mmcsd_card *card)
{
rt_int32_t err = RT_EOK;
Expand Down Expand Up @@ -460,6 +675,11 @@ rt_int32_t gpt_device_probe(struct rt_mmcsd_card *card)

rt_device_register(&(blk_dev->dev), card->host->name,
RT_DEVICE_FLAG_RDWR);
#ifdef RT_USING_POSIX_DEVIO
#ifdef RT_USING_DFS_V2
blk_dev->dev.fops = &mmcsd_blk_fops;
#endif
#endif
rt_list_insert_after(&blk_devices, &blk_dev->list);

for (i = 0; i < RT_GPT_PARTITION_MAX; i++)
Expand Down Expand Up @@ -505,6 +725,11 @@ rt_int32_t gpt_device_probe(struct rt_mmcsd_card *card)

rt_device_register(&(blk_dev->dev), dname,
RT_DEVICE_FLAG_RDWR);
#ifdef RT_USING_POSIX_DEVIO
#ifdef RT_USING_DFS_V2
blk_dev->dev.fops = &mmcsd_blk_fops;
#endif
#endif
rt_list_insert_after(&blk_devices, &blk_dev->list);
}
else
Expand Down
8 changes: 8 additions & 0 deletions components/finsh/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,25 @@ long list_thread(void)
rt_list_t *obj_list[LIST_FIND_OBJ_NR];
rt_list_t *next = (rt_list_t *)RT_NULL;
const char *item_title = "thread";
const size_t tcb_strlen = sizeof(void *) * 2 + 2;
int maxlen;

list_find_init(&find_arg, RT_Object_Class_Thread, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));

maxlen = RT_NAME_MAX;

rt_kprintf("%-*.*s ", tcb_strlen, tcb_strlen, "rt_thread_t");

#ifdef RT_USING_SMP
rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error\n", maxlen, maxlen, item_title);
object_split(tcb_strlen);
rt_kprintf(" ");
object_split(maxlen);
rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- ---\n");
#else
rt_kprintf("%-*.*s pri status sp stack size max used left tick error\n", maxlen, maxlen, item_title);
object_split(tcb_strlen);
rt_kprintf(" ");
object_split(maxlen);
rt_kprintf(" --- ------- ---------- ---------- ------ ---------- ---\n");
#endif /*RT_USING_SMP*/
Expand Down Expand Up @@ -202,6 +209,7 @@ long list_thread(void)
rt_uint8_t stat;
rt_uint8_t *ptr;

rt_kprintf("%p ", thread);
#ifdef RT_USING_SMP
if (thread->oncpu != RT_CPU_DETACHED)
rt_kprintf("%-*.*s %3d %3d %4d ", maxlen, RT_NAME_MAX, thread->parent.name, thread->oncpu, thread->bind_cpu, thread->current_priority);
Expand Down
29 changes: 29 additions & 0 deletions components/finsh/msh_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ MSH_CMD_EXPORT_ALIAS(cmd_mount, mount, mount <device> <mountpoint> <fstype>);
/* unmount the filesystem from the specified mountpoint */
static int cmd_umount(int argc, char **argv)
{
#ifndef RT_USING_DFS_V2
char *path = argv[1];

if (argc != 2)
Expand All @@ -637,6 +638,34 @@ static int cmd_umount(int argc, char **argv)
rt_kprintf("succeed!\n");
return 0;
}
#else
int flags = 0;
char *path = argv[1];

if (argc < 2)
{
rt_kprintf("Usage: unmount [-f] <mountpoint>.\n");
return -1;
}

if (argc > 2)
{
flags = strcmp(argv[1], "-f") == 0 ? MNT_FORCE : 0;
path = argv[2];
}

rt_kprintf("unmount %s ... ", path);
if (dfs_umount(path, flags) < 0)
{
rt_kprintf("failed!\n");
return -1;
}
else
{
rt_kprintf("succeed!\n");
return 0;
}
#endif
}
MSH_CMD_EXPORT_ALIAS(cmd_umount, umount, Unmount the mountpoint);

Expand Down
33 changes: 7 additions & 26 deletions components/libc/posix/io/stdio/libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,20 @@ int libc_system_init(void)
dev_console = rt_console_get_device();
if (dev_console)
{
int fd, ret;
char name[STDIO_DEVICE_NAME_MAX];

rt_snprintf(name, sizeof(name) - 1, "/dev/%s", dev_console->parent.name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';

fd = open(name, O_RDWR);
if (fd >= 0)
{
/* set fd (0, 1, 2) */
ret = sys_dup2(fd, 0);
if (ret != fd)
{
close(fd);
}
sys_dup2(ret, 1);
sys_dup2(ret, 2);

ret = libc_stdio_set_console(dev_console->parent.name, O_RDWR);
if (ret < 0)
{
return -1;
}
}
else
int fd = libc_stdio_set_console(dev_console->parent.name, O_RDWR);
if (fd < 0)
{
return -1;
}
/* set fd (0, 1, 2) */
sys_dup2(fd, 0);
sys_dup2(fd, 1);
sys_dup2(fd, 2);
}
#endif /* RT_USING_POSIX_STDIO */
return 0;
}
INIT_APP_EXPORT(libc_system_init);
INIT_ENV_EXPORT(libc_system_init);

#if defined(RT_USING_POSIX_STDIO) && defined(RT_USING_NEWLIBC)

Expand Down
Loading

0 comments on commit 4d20416

Please sign in to comment.