#include "partition.h"
#include "sd_raw.h"
#include "sd_raw_config.h"
-#include "uart.h"
-#define DEBUG 1
+#include <uart.h>
+#include <stdio.h>
+#include <aversive/error.h>
+#include "cmdline.h"
+
+
+#define SD_DEBUG 1
/**
* \mainpage MMC/SD/SDHC card library
static struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name);
static uint8_t print_disk_info(const struct fat_fs_struct* fs);
-int main()
+int sd_main(void)
{
/* we will just use ordinary idle mode */
set_sleep_mode(SLEEP_MODE_IDLE);
- /* setup uart */
- uart_init();
-
while(1)
{
+ printf_P(PSTR("init\n"));
/* setup sd card slot */
if(!sd_raw_init())
{
-#if DEBUG
- uart_puts_p(PSTR("MMC/SD initialization failed\n"));
+#if SD_DEBUG
+ printf_P(PSTR("MMC/SD initialization failed\n"));
#endif
continue;
}
);
if(!partition)
{
-#if DEBUG
- uart_puts_p(PSTR("opening partition failed\n"));
+#if SD_DEBUG
+ printf_P(PSTR("opening partition failed\n"));
#endif
continue;
}
struct fat_fs_struct* fs = fat_open(partition);
if(!fs)
{
-#if DEBUG
- uart_puts_p(PSTR("opening filesystem failed\n"));
+#if SD_DEBUG
+ printf_P(PSTR("opening filesystem failed\n"));
#endif
continue;
}
struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
if(!dd)
{
-#if DEBUG
- uart_puts_p(PSTR("opening root directory failed\n"));
+#if SD_DEBUG
+ printf_P(PSTR("opening root directory failed\n"));
#endif
continue;
}
while(1)
{
/* print prompt */
- uart_putc('>');
- uart_putc(' ');
+ printf_P(PSTR("> "));
/* read command */
char* command = buffer;
}
}
- uart_puts_p(PSTR("directory not found: "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("directory not found: %s\n"), command);
}
else if(strcmp_P(command, PSTR("ls")) == 0)
{
{
uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4;
- uart_puts(dir_entry.long_name);
- uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ');
+ printf_P(PSTR("%s%c"), dir_entry.long_name,
+ dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ');
while(spaces--)
- uart_putc(' ');
- uart_putdw_dec(dir_entry.file_size);
- uart_putc('\n');
+ uart_send(CMDLINE_UART, ' ');
+ printf_P(PSTR("%d\n"), dir_entry.file_size);
}
}
else if(strncmp_P(command, PSTR("cat "), 4) == 0)
struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
if(!fd)
{
- uart_puts_p(PSTR("error opening "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("error opening: %s\n"), command);
continue;
}
intptr_t count;
while((count = fat_read_file(fd, buffer, sizeof(buffer))) > 0)
{
- uart_putdw_hex(offset);
- uart_putc(':');
+ printf_P(PSTR("%"PRIu32":"), offset);
for(intptr_t i = 0; i < count; ++i)
{
- uart_putc(' ');
- uart_putc_hex(buffer[i]);
+ printf_P(PSTR(" %x"), buffer[i]);
}
- uart_putc('\n');
+ printf_P(PSTR("\n"));
offset += 8;
}
else if(strcmp_P(command, PSTR("disk")) == 0)
{
if(!print_disk_info(fs))
- uart_puts_p(PSTR("error reading disk info\n"));
+ printf_P(PSTR("error reading disk info\n"));
}
#if FAT_WRITE_SUPPORT
else if(strncmp_P(command, PSTR("rm "), 3) == 0)
continue;
}
- uart_puts_p(PSTR("error deleting file: "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("error deleting file: %s\n"), command);
}
else if(strncmp_P(command, PSTR("touch "), 6) == 0)
{
struct fat_dir_entry_struct file_entry;
if(!fat_create_file(dd, command, &file_entry))
{
- uart_puts_p(PSTR("error creating file: "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("error creating file: %s\n"), command);
}
}
else if(strncmp_P(command, PSTR("mv "), 3) == 0)
continue;
}
- uart_puts_p(PSTR("error moving file: "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("error moving file: %s\n"), command);
}
else if(strncmp_P(command, PSTR("write "), 6) == 0)
{
struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
if(!fd)
{
- uart_puts_p(PSTR("error opening "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("error opening %s\n"), command);
continue;
}
int32_t offset = strtolong(offset_value);
if(!fat_seek_file(fd, &offset, FAT_SEEK_SET))
{
- uart_puts_p(PSTR("error seeking on "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("error seeking on %s\n"), command);
fat_close_file(fd);
continue;
while(1)
{
/* give a different prompt */
- uart_putc('<');
- uart_putc(' ');
+ printf_P(PSTR("< "));
/* read one line of text */
data_len = read_line(buffer, sizeof(buffer));
/* write text to file */
if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
{
- uart_puts_p(PSTR("error writing to file\n"));
+ printf_P(PSTR("error writing to file\n"));
break;
}
}
struct fat_dir_entry_struct dir_entry;
if(!fat_create_dir(dd, command, &dir_entry))
{
- uart_puts_p(PSTR("error creating directory: "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("error creating directory: %s\n"), command);
}
}
#endif
else if(strcmp_P(command, PSTR("sync")) == 0)
{
if(!sd_raw_sync())
- uart_puts_p(PSTR("error syncing disk\n"));
+ printf_P(PSTR("error syncing disk\n"));
}
#endif
else
{
- uart_puts_p(PSTR("unknown command: "));
- uart_puts(command);
- uart_putc('\n');
+ printf_P(PSTR("unknown command: %s\n"), command);
}
}
return 0;
}
+uint8_t uart_getc(void)
+{
+ uint8_t b = uart_recv(CMDLINE_UART);
+ if(b == '\r')
+ b = '\n';
+
+ return b;
+}
+
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();
+ uint8_t c = uart_getc();
if(c == 0x08 || c == 0x7f)
{
--read_length;
buffer[read_length] = '\0';
- uart_putc(0x08);
- uart_putc(' ');
- uart_putc(0x08);
+ uart_send(CMDLINE_UART, 0x08);
+ uart_send(CMDLINE_UART, ' ');
+ uart_send(CMDLINE_UART, 0x08);
continue;
}
- uart_putc(c);
+ uart_send(CMDLINE_UART, c);
if(c == '\n')
{
if(!sd_raw_get_info(&disk_info))
return 0;
- uart_puts_p(PSTR("manuf: 0x")); uart_putc_hex(disk_info.manufacturer); uart_putc('\n');
- uart_puts_p(PSTR("oem: ")); uart_puts((char*) disk_info.oem); uart_putc('\n');
- uart_puts_p(PSTR("prod: ")); uart_puts((char*) disk_info.product); uart_putc('\n');
- uart_puts_p(PSTR("rev: ")); uart_putc_hex(disk_info.revision); uart_putc('\n');
- uart_puts_p(PSTR("serial: 0x")); uart_putdw_hex(disk_info.serial); uart_putc('\n');
- uart_puts_p(PSTR("date: ")); uart_putw_dec(disk_info.manufacturing_month); uart_putc('/');
- uart_putw_dec(disk_info.manufacturing_year); uart_putc('\n');
- uart_puts_p(PSTR("size: ")); uart_putdw_dec(disk_info.capacity / 1024 / 1024); uart_puts_p(PSTR("MB\n"));
- uart_puts_p(PSTR("copy: ")); uart_putw_dec(disk_info.flag_copy); uart_putc('\n');
- uart_puts_p(PSTR("wr.pr.: ")); uart_putw_dec(disk_info.flag_write_protect_temp); uart_putc('/');
- uart_putw_dec(disk_info.flag_write_protect); uart_putc('\n');
- uart_puts_p(PSTR("format: ")); uart_putw_dec(disk_info.format); uart_putc('\n');
- uart_puts_p(PSTR("free: ")); uart_putdw_dec(fat_get_fs_free(fs)); uart_putc('/');
- uart_putdw_dec(fat_get_fs_size(fs)); uart_putc('\n');
+ printf_P(PSTR("manuf: 0x%x\n"), disk_info.manufacturer);
+ printf_P(PSTR("oem: %s\n"), disk_info.oem);
+ printf_P(PSTR("prod: %s\n"), disk_info.product);
+ printf_P(PSTR("rev: 0x%x\n"), disk_info.revision);
+ printf_P(PSTR("serial: 0x%x\n"), disk_info.serial);
+ printf_P(PSTR("date: 0x%d 0x%d\n"), disk_info.manufacturing_month,
+ disk_info.manufacturing_year);
+ printf_P(PSTR("size: %"PRIu32"\n"), disk_info.capacity / 1024 / 1024);
+ printf_P(PSTR("copy: 0x%x\n"), disk_info.flag_copy);
+ printf_P(PSTR("wr.pr.: 0x%x 0x%x\n"), disk_info.flag_write_protect_temp,
+ disk_info.flag_write_protect);
+ printf_P(PSTR("format: %d\n"), disk_info.format);
+ printf_P(PSTR("free: %d %d\n"), fat_get_fs_free(fs), fat_get_fs_size(fs));
return 1;
}