|
@@ -8,12 +8,12 @@
|
|
|
* 2018-09-25 misonyo first edition.
|
|
* 2018-09-25 misonyo first edition.
|
|
|
*/
|
|
*/
|
|
|
/*
|
|
/*
|
|
|
- * 程序清单:这是一个SD卡设备的使用例程
|
|
|
|
|
- * 例程导出了 sd_sample 命令到控制终端
|
|
|
|
|
- * 命令调用格式:sd_sample sd0
|
|
|
|
|
- * 命令解释:命令第二个参数是要使用的SD设备的名称,为空则使用例程默认的SD设备。
|
|
|
|
|
- * 程序功能:程序会产生一个块大小的随机数,然后写入SD卡中,然后在读取这部分写入的数据。
|
|
|
|
|
- * 对比写入和读出的数据是否一致,一致则表示程序运行正确。
|
|
|
|
|
|
|
+ * 绋嬪簭娓呭崟锛氳繖鏄�竴涓猄D鍗¤�澶囩殑浣跨敤渚嬬▼
|
|
|
|
|
+ * 渚嬬▼瀵煎嚭浜� sd_sample 鍛戒护鍒版帶鍒剁粓绔�
|
|
|
|
|
+ * 鍛戒护璋冪敤鏍煎紡锛歴d_sample sd0
|
|
|
|
|
+ * 鍛戒护瑙i噴锛氬懡浠ょ�浜屼釜鍙傛暟鏄��浣跨敤鐨凷D璁惧�鐨勫悕绉帮紝涓虹┖鍒欎娇鐢ㄤ緥绋嬮粯璁ょ殑SD璁惧�銆�
|
|
|
|
|
+ * 绋嬪簭鍔熻兘锛氱▼搴忎細浜х敓涓€涓�潡澶у皬鐨勯殢鏈烘暟锛岀劧鍚庡啓鍏�D鍗′腑锛岀劧鍚庡湪璇诲彇杩欓儴鍒嗗啓鍏ョ殑鏁版嵁銆�
|
|
|
|
|
+ * 瀵规瘮鍐欏叆鍜岃�鍑虹殑鏁版嵁鏄�惁涓€鑷达紝涓€鑷村垯琛ㄧず绋嬪簭杩愯�姝g‘銆�
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
#include <rtthread.h>
|
|
@@ -25,7 +25,7 @@
|
|
|
void fill_buffer(rt_uint8_t *buff, rt_uint32_t buff_length)
|
|
void fill_buffer(rt_uint8_t *buff, rt_uint32_t buff_length)
|
|
|
{
|
|
{
|
|
|
rt_uint32_t index;
|
|
rt_uint32_t index;
|
|
|
- /* 往缓冲区填充随机数 */
|
|
|
|
|
|
|
+ /* 寰€缂撳啿鍖哄~鍏呴殢鏈烘暟 */
|
|
|
for (index = 0; index < buff_length; index++)
|
|
for (index = 0; index < buff_length; index++)
|
|
|
{
|
|
{
|
|
|
buff[index] = ((rt_uint8_t)rand()) & 0xff;
|
|
buff[index] = ((rt_uint8_t)rand()) & 0xff;
|
|
@@ -40,7 +40,7 @@ static int sd_sample(int argc, char *argv[])
|
|
|
rt_uint8_t *write_buff, *read_buff;
|
|
rt_uint8_t *write_buff, *read_buff;
|
|
|
struct rt_device_blk_geometry geo;
|
|
struct rt_device_blk_geometry geo;
|
|
|
rt_uint8_t block_num;
|
|
rt_uint8_t block_num;
|
|
|
- /* 判断命令行参数是否给定了设备名称 */
|
|
|
|
|
|
|
+ /* 鍒ゆ柇鍛戒护琛屽弬鏁版槸鍚︾粰瀹氫簡璁惧�鍚嶇О */
|
|
|
if (argc == 2)
|
|
if (argc == 2)
|
|
|
{
|
|
{
|
|
|
rt_strncpy(sd_name, argv[1], RT_NAME_MAX);
|
|
rt_strncpy(sd_name, argv[1], RT_NAME_MAX);
|
|
@@ -49,14 +49,14 @@ static int sd_sample(int argc, char *argv[])
|
|
|
{
|
|
{
|
|
|
rt_strncpy(sd_name, SD_DEVICE_NAME, RT_NAME_MAX);
|
|
rt_strncpy(sd_name, SD_DEVICE_NAME, RT_NAME_MAX);
|
|
|
}
|
|
}
|
|
|
- /* 查找设备获取设备句柄 */
|
|
|
|
|
|
|
+ /* 鏌ユ壘璁惧�鑾峰彇璁惧�鍙ユ焺 */
|
|
|
sd_device = rt_device_find(sd_name);
|
|
sd_device = rt_device_find(sd_name);
|
|
|
if (sd_device == RT_NULL)
|
|
if (sd_device == RT_NULL)
|
|
|
{
|
|
{
|
|
|
rt_kprintf("find device %s failed!\n", sd_name);
|
|
rt_kprintf("find device %s failed!\n", sd_name);
|
|
|
return RT_ERROR;
|
|
return RT_ERROR;
|
|
|
}
|
|
}
|
|
|
- /* 打开设备 */
|
|
|
|
|
|
|
+ /* 鎵撳紑璁惧� */
|
|
|
ret = rt_device_open(sd_device, RT_DEVICE_OFLAG_RDWR);
|
|
ret = rt_device_open(sd_device, RT_DEVICE_OFLAG_RDWR);
|
|
|
if (ret != RT_EOK)
|
|
if (ret != RT_EOK)
|
|
|
{
|
|
{
|
|
@@ -65,7 +65,7 @@ static int sd_sample(int argc, char *argv[])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
rt_memset(&geo, 0, sizeof(geo));
|
|
rt_memset(&geo, 0, sizeof(geo));
|
|
|
- /* 获取块设备信息 */
|
|
|
|
|
|
|
+ /* 鑾峰彇鍧楄�澶囦俊鎭� */
|
|
|
ret = rt_device_control(sd_device, RT_DEVICE_CTRL_BLK_GETGEOME, &geo);
|
|
ret = rt_device_control(sd_device, RT_DEVICE_CTRL_BLK_GETGEOME, &geo);
|
|
|
if (ret != RT_EOK)
|
|
if (ret != RT_EOK)
|
|
|
{
|
|
{
|
|
@@ -76,7 +76,7 @@ static int sd_sample(int argc, char *argv[])
|
|
|
rt_kprintf("sector size : %d byte\n", geo.bytes_per_sector);
|
|
rt_kprintf("sector size : %d byte\n", geo.bytes_per_sector);
|
|
|
rt_kprintf("sector count : %d \n", geo.sector_count);
|
|
rt_kprintf("sector count : %d \n", geo.sector_count);
|
|
|
rt_kprintf("block size : %d byte\n", geo.block_size);
|
|
rt_kprintf("block size : %d byte\n", geo.block_size);
|
|
|
- /* 准备读写缓冲区空间,大小为一个块 */
|
|
|
|
|
|
|
+ /* 鍑嗗�璇诲啓缂撳啿鍖虹┖闂达紝澶у皬涓轰竴涓�潡 */
|
|
|
read_buff = rt_malloc(geo.block_size);
|
|
read_buff = rt_malloc(geo.block_size);
|
|
|
if (read_buff == RT_NULL)
|
|
if (read_buff == RT_NULL)
|
|
|
{
|
|
{
|
|
@@ -91,24 +91,24 @@ static int sd_sample(int argc, char *argv[])
|
|
|
return RT_ERROR;
|
|
return RT_ERROR;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /* 填充写数据缓冲区,为写操作做准备 */
|
|
|
|
|
|
|
+ /* 濉�厖鍐欐暟鎹�紦鍐插尯锛屼负鍐欐搷浣滃仛鍑嗗� */
|
|
|
fill_buffer(write_buff, geo.block_size);
|
|
fill_buffer(write_buff, geo.block_size);
|
|
|
|
|
|
|
|
- /* 把写数据缓冲的数据写入SD卡中,大小为一个块,size参数以块为单位 */
|
|
|
|
|
|
|
+ /* 鎶婂啓鏁版嵁缂撳啿鐨勬暟鎹�啓鍏�D鍗′腑锛屽ぇ灏忎负涓€涓�潡锛宻ize鍙傛暟浠ュ潡涓哄崟浣� */
|
|
|
block_num = rt_device_write(sd_device, 0, write_buff, 1);
|
|
block_num = rt_device_write(sd_device, 0, write_buff, 1);
|
|
|
if (1 != block_num)
|
|
if (1 != block_num)
|
|
|
{
|
|
{
|
|
|
rt_kprintf("write device %s failed!\n", sd_name);
|
|
rt_kprintf("write device %s failed!\n", sd_name);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /* 从SD卡中读出数据,并保存在读数据缓冲区中 */
|
|
|
|
|
|
|
+ /* 浠嶴D鍗′腑璇诲嚭鏁版嵁锛屽苟淇濆瓨鍦ㄨ�鏁版嵁缂撳啿鍖轰腑 */
|
|
|
block_num = rt_device_read(sd_device, 0, read_buff, 1);
|
|
block_num = rt_device_read(sd_device, 0, read_buff, 1);
|
|
|
if (1 != block_num)
|
|
if (1 != block_num)
|
|
|
{
|
|
{
|
|
|
rt_kprintf("read %s device failed!\n", sd_name);
|
|
rt_kprintf("read %s device failed!\n", sd_name);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /* 比较写数据缓冲区和读数据缓冲区的内容是否完全一致 */
|
|
|
|
|
|
|
+ /* 姣旇緝鍐欐暟鎹�紦鍐插尯鍜岃�鏁版嵁缂撳啿鍖虹殑鍐呭�鏄�惁瀹屽叏涓€鑷� */
|
|
|
if (rt_memcmp(write_buff, read_buff, geo.block_size) == 0)
|
|
if (rt_memcmp(write_buff, read_buff, geo.block_size) == 0)
|
|
|
{
|
|
{
|
|
|
rt_kprintf("Block test OK!\n");
|
|
rt_kprintf("Block test OK!\n");
|
|
@@ -117,11 +117,11 @@ static int sd_sample(int argc, char *argv[])
|
|
|
{
|
|
{
|
|
|
rt_kprintf("Block test Fail!\n");
|
|
rt_kprintf("Block test Fail!\n");
|
|
|
}
|
|
}
|
|
|
- /* 释放缓冲区空间 */
|
|
|
|
|
|
|
+ /* 閲婃斁缂撳啿鍖虹┖闂� */
|
|
|
rt_free(read_buff);
|
|
rt_free(read_buff);
|
|
|
rt_free(write_buff);
|
|
rt_free(write_buff);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
return RT_EOK;
|
|
|
}
|
|
}
|
|
|
-/* 导出到 msh 命令列表中 */
|
|
|
|
|
|
|
+/* 瀵煎嚭鍒� msh 鍛戒护鍒楄〃涓� */
|
|
|
MSH_CMD_EXPORT(sd_sample, sd device sample);
|
|
MSH_CMD_EXPORT(sd_sample, sd device sample);
|