88 * 2018-09-25 misonyo first edition.
99 */
1010/*
11- * 程序清单:这是一个SD卡设备的使用例程
12- * 例程导出了 sd_sample 命令到控制终端
13- * 命令调用格式:sd_sample sd0
14- * 命令解释:命令第二个参数是要使用的SD设备的名称,为空则使用例程默认的SD设备。
15- * 程序功能:程序会产生一个块大小的随机数,然后写入SD卡中,然后在读取这部分写入的数据。
16- * 对比写入和读出的数据是否一致,一致则表示程序运行正确。
11+ * 程序清单:这是一个SD卡设备的使用例程
12+ * 例程导出了 sd_sample 命令到控制终端
13+ * 命令调用格式:sd_sample sd0
14+ * 命令解释:命令第二个参数是要使用的SD设备的名称,为空则使用例程默认的SD设备。
15+ * 程序功能:程序会产生一个块大小的随机数,然后写入SD卡中,然后在读取这部分写入的数据。
16+ * 对比写入和读出的数据是否一致,一致则表示程序运行正确。
1717*/
1818
1919#include <rtthread.h>
2525void fill_buffer (rt_uint8_t * buff , rt_uint32_t buff_length )
2626{
2727 rt_uint32_t index ;
28- /* 往缓冲区填充随机数 */
28+ /* 往缓冲区填充随机数 */
2929 for (index = 0 ; index < buff_length ; index ++ )
3030 {
3131 buff [index ] = ((rt_uint8_t )rand ()) & 0xff ;
@@ -40,7 +40,7 @@ static int sd_sample(int argc, char *argv[])
4040 rt_uint8_t * write_buff , * read_buff ;
4141 struct rt_device_blk_geometry geo ;
4242 rt_uint8_t block_num ;
43- /* 判断命令行参数是否给定了设备名称 */
43+ /* 判断命令行参数是否给定了设备名称 */
4444 if (argc == 2 )
4545 {
4646 rt_strncpy (sd_name , argv [1 ], RT_NAME_MAX );
@@ -49,14 +49,14 @@ static int sd_sample(int argc, char *argv[])
4949 {
5050 rt_strncpy (sd_name , SD_DEVICE_NAME , RT_NAME_MAX );
5151 }
52- /* 查找设备获取设备句柄 */
52+ /* 查找设备获取设备句柄 */
5353 sd_device = rt_device_find (sd_name );
5454 if (sd_device == RT_NULL )
5555 {
5656 rt_kprintf ("find device %s failed!\n" , sd_name );
5757 return RT_ERROR ;
5858 }
59- /* 打开设备 */
59+ /* 打开设备 */
6060 ret = rt_device_open (sd_device , RT_DEVICE_OFLAG_RDWR );
6161 if (ret != RT_EOK )
6262 {
@@ -65,7 +65,7 @@ static int sd_sample(int argc, char *argv[])
6565 }
6666
6767 rt_memset (& geo , 0 , sizeof (geo ));
68- /* 获取块设备信息 */
68+ /* 获取块设备信息 */
6969 ret = rt_device_control (sd_device , RT_DEVICE_CTRL_BLK_GETGEOME , & geo );
7070 if (ret != RT_EOK )
7171 {
@@ -76,7 +76,7 @@ static int sd_sample(int argc, char *argv[])
7676 rt_kprintf ("sector size : %d byte\n" , geo .bytes_per_sector );
7777 rt_kprintf ("sector count : %d \n" , geo .sector_count );
7878 rt_kprintf ("block size : %d byte\n" , geo .block_size );
79- /* 准备读写缓冲区空间,大小为一个块 */
79+ /* 准备读写缓冲区空间,大小为一个块 */
8080 read_buff = rt_malloc (geo .block_size );
8181 if (read_buff == RT_NULL )
8282 {
@@ -91,24 +91,24 @@ static int sd_sample(int argc, char *argv[])
9191 return RT_ERROR ;
9292 }
9393
94- /* 填充写数据缓冲区,为写操作做准备 */
94+ /* 填充写数据缓冲区,为写操作做准备 */
9595 fill_buffer (write_buff , geo .block_size );
9696
97- /* 把写数据缓冲的数据写入SD卡中,大小为一个块,size参数以块为单位 */
97+ /* 把写数据缓冲的数据写入SD卡中,大小为一个块,size参数以块为单位 */
9898 block_num = rt_device_write (sd_device , 0 , write_buff , 1 );
9999 if (1 != block_num )
100100 {
101101 rt_kprintf ("write device %s failed!\n" , sd_name );
102102 }
103103
104- /* 从SD卡中读出数据,并保存在读数据缓冲区中 */
104+ /* 从SD卡中读出数据,并保存在读数据缓冲区中 */
105105 block_num = rt_device_read (sd_device , 0 , read_buff , 1 );
106106 if (1 != block_num )
107107 {
108108 rt_kprintf ("read %s device failed!\n" , sd_name );
109109 }
110110
111- /* 比较写数据缓冲区和读数据缓冲区的内容是否完全一致 */
111+ /* 比较写数据缓冲区和读数据缓冲区的内容是否完全一致 */
112112 if (rt_memcmp (write_buff , read_buff , geo .block_size ) == 0 )
113113 {
114114 rt_kprintf ("Block test OK!\n" );
@@ -117,11 +117,11 @@ static int sd_sample(int argc, char *argv[])
117117 {
118118 rt_kprintf ("Block test Fail!\n" );
119119 }
120- /* 释放缓冲区空间 */
120+ /* 释放缓冲区空间 */
121121 rt_free (read_buff );
122122 rt_free (write_buff );
123123
124124 return RT_EOK ;
125125}
126- /* 导出到 msh 命令列表中 */
126+ /* 导出到 msh 命令列表中 */
127127MSH_CMD_EXPORT (sd_sample , sd device sample );
0 commit comments