⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 AVR单片机上跑的MMC卡加FAT16文件系统的例程
💻 C
📖 第 1 页 / 共 2 页
字号:
            /* print file contents */            uint8_t buffer[32];            uint32_t offset = 0;            while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0)            {                printf_P(PSTR("%08lx: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n"),                         offset,                         buffer[0],                         buffer[1],                         buffer[2],                         buffer[3],                         buffer[4],                         buffer[5],                         buffer[6],                         buffer[7],
                         buffer[8],
                         buffer[9],                         buffer[10],                         buffer[11],                         buffer[12],                         buffer[13],                         buffer[14],                         buffer[15],                         buffer[16],
                         buffer[17],                         buffer[18],                         buffer[19],                         buffer[20],                         buffer[21],                         buffer[22],                         buffer[23],                         buffer[24],
                         buffer[25],                         buffer[26],                         buffer[27],                         buffer[28],                         buffer[29],                         buffer[30],                         buffer[31]                        );                offset += 32;            }            fat16_close_file(fd);        }
        else if(strncmp("test ", command, 5) == 0)        {            command += 5;            if(command[0] == '\0')                continue;                        /* search file in current directory and open it */            struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command);            if(!fd)            {                printf_P(PSTR("error opening %s\n"), command);                continue;            }            /* print file contents */            uint8_t buffer[32];

            while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0);            fat16_close_file(fd);
            printf_P(PSTR("test finsih!\n"));        }
        else if(strcmp("unicode", command) == 0)        {			uint16_t gbk = 0;            if(unicode2gbk(cp936,40660,&gbk)) {
	            printf_P(PSTR("%8u \n"), gbk);
			}

            if(unicode2gbk(cp936,20661,&gbk)) {
	            printf_P(PSTR("%8u \n"), gbk);
			}
            if(unicode2gbk(cp936,40660,&gbk)) {
	            printf_P(PSTR("%8u \n"), gbk);
			}

            if(unicode2gbk(cp936,20661,&gbk)) {
	            printf_P(PSTR("%8u \n"), gbk);
			}        }#if FAT16_WRITE_SUPPORT        else if(strncmp("rm ", command, 3) == 0)        {            command += 3;            if(command[0] == '\0')                continue;                        struct fat16_dir_entry_struct file_entry;            if(find_file_in_dir(fs, dd, command, &file_entry))            {                if(fat16_delete_file(fs, &file_entry))                    continue;            }            printf_P(PSTR("error deleting file: %s\n"), command);        }        else if(strncmp("touch ", command, 6) == 0)        {            command += 6;            if(command[0] == '\0')                continue;            struct fat16_dir_entry_struct file_entry;            if(!fat16_create_file(dd, command, &file_entry))                printf_P(PSTR("error creating file: %s\n"), command);        }        else if(strncmp("write ", command, 6) == 0)        {            command += 6;            if(command[0] == '\0')                continue;            char* offset_value = command;            while(*offset_value != ' ' && *offset_value != '\0')                ++offset_value;            if(*offset_value == ' ')                *offset_value++ = '\0';            else                continue;            /* search file in current directory and open it */            struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command);            if(!fd)            {                printf_P(PSTR("error opening %s\n"), command);                continue;            }            int32_t offset = strtol(offset_value, 0, 0);            if(!fat16_seek_file(fd, &offset, FAT16_SEEK_SET))            {                printf_P(PSTR("error seeking on %s\n"), command);                fat16_close_file(fd);                continue;            }            /* read text from the shell and write it to the file */            uint8_t data_len;            while((data_len = read_line(buffer, sizeof(buffer))))            {                if(fat16_write_file(fd, (uint8_t*) buffer, data_len) != data_len)                {                    printf_P(PSTR("error writing to file\n"));                    break;                }            }            fat16_close_file(fd);        }#endif        else        {            printf_P(PSTR("unknown command: %s\n"), command);        }    }
	fat16_close_file(cp936);
    /* close file system */    fat16_close(fs);    /* close partition */    partition_close(partition);        return 0;}uint8_t read_line(char* buffer, uint8_t buffer_length){    memset(buffer, 0, buffer_length);    uint8_t read_length = 0;    while(read_length < buffer_length - 1)    {        uint8_t c = uart_getc();        uart_putc(c);        if(c != '\n')        {            buffer[read_length] = c;        }        else        {            buffer[read_length] = '\0';            break;        }                ++read_length;    }    return read_length;}uint8_t find_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name, struct fat16_dir_entry_struct* dir_entry){    while(fat16_read_dir(dd, dir_entry))    {        if(strcasecmp(dir_entry->long_name, name) == 0 || strcasecmp(dir_entry->short_name, name) == 0)        {            fat16_reset_dir(dd);            return 1;        }    }    return 0;}struct fat16_file_struct* open_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name){    struct fat16_dir_entry_struct file_entry;    if(!find_file_in_dir(fs, dd, name, &file_entry))        return 0;    return fat16_open_file(fs, &file_entry);}
uint8_t unicode2gbk_str(struct fat16_file_struct* cp936, uint8_t* unicode_name, uint8_t* gbk_name) {
	uint8_t name_len = 64;
	uint16_t unicode;
	uint16_t gbk;
	uint8_t gbk_name_pos = 0;
	for(uint8_t i = 0; i < name_len; i += 2) {
		if(unicode_name[i] == 0 ) {
			if(unicode_name[i + 1] == 0) {
				gbk_name[gbk_name_pos] = 0;
				break;
			}
			gbk_name[gbk_name_pos] = unicode_name[i + 1];
			gbk_name_pos++;
			continue;
		}
		unicode = (uint16_t)(unicode_name[i] << 8) | unicode_name[i + 1];
		if(unicode2gbk(cp936, unicode, &gbk)) {
			gbk_name[gbk_name_pos] = (uint8_t)(gbk >> 8);
			gbk_name_pos++;
			gbk_name[gbk_name_pos] = (uint8_t)gbk;
			gbk_name_pos++;
		}else {
			return 0;
		}
	}
	return 1;
}
uint8_t unicode2gbk(struct fat16_file_struct* cp936, uint16_t unicode, uint16_t* gbk) {
    uint8_t buffer[2];
	uint32_t offset = (uint32_t)unicode << 1;
    if(fat16_seek_file(cp936, &offset, FAT16_SEEK_SET))    {	    if(fat16_read_file(cp936, buffer, sizeof(buffer)) > 0) {
	        (*gbk) = (buffer[0] << 8) | buffer[1];
			return 1;		}    }
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -