fat: use uart from aversive
authorOlivier Matz <zer0@droids-corp.org>
Thu, 26 Jun 2014 18:32:48 +0000 (20:32 +0200)
committerOlivier Matz <zer0@droids-corp.org>
Thu, 26 Jun 2014 18:32:48 +0000 (20:32 +0200)
sd_main.c
uart.c [deleted file]
uart.h [deleted file]
uart_config.h

index d2ee6c5..06bd231 100644 (file)
--- a/sd_main.c
+++ b/sd_main.c
 #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
@@ -211,21 +216,19 @@ static uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct*
 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;
        }
@@ -261,8 +264,8 @@ int main()
                                      );
            if(!partition)
            {
-#if DEBUG
-               uart_puts_p(PSTR("opening partition failed\n"));
+#if SD_DEBUG
+               printf_P(PSTR("opening partition failed\n"));
 #endif
                continue;
            }
@@ -272,8 +275,8 @@ int main()
        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;
        }
@@ -285,8 +288,8 @@ int main()
        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;
        }
@@ -299,8 +302,7 @@ int main()
        while(1)
        {
            /* print prompt */
-           uart_putc('>');
-           uart_putc(' ');
+               printf_P(PSTR("> "));
 
            /* read command */
            char* command = buffer;
@@ -331,9 +333,7 @@ int main()
                    }
                }
 
-               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)
            {
@@ -343,12 +343,11 @@ int main()
                {
                    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)
@@ -361,9 +360,7 @@ int main()
                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;
                }
 
@@ -373,14 +370,12 @@ int main()
                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;
                }
 
@@ -389,7 +384,7 @@ int main()
            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)
@@ -405,9 +400,7 @@ int main()
                        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)
            {
@@ -418,9 +411,7 @@ int main()
                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)
@@ -445,9 +436,7 @@ int main()
                        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)
            {
@@ -468,18 +457,14 @@ int main()
                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;
@@ -490,8 +475,7 @@ int main()
                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));
@@ -501,7 +485,7 @@ int main()
                    /* 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;
                    }
                }
@@ -517,9 +501,7 @@ int main()
                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
@@ -527,14 +509,12 @@ int main()
            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);
            }
        }
 
@@ -551,6 +531,15 @@ int main()
     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);
@@ -558,7 +547,7 @@ uint8_t read_line(char* buffer, uint8_t 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)
        {
@@ -568,14 +557,14 @@ uint8_t read_line(char* buffer, uint8_t buffer_length)
            --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')
        {
@@ -635,20 +624,19 @@ uint8_t print_disk_info(const struct fat_fs_struct* fs)
     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;
 }
diff --git a/uart.c b/uart.c
deleted file mode 100644 (file)
index 3d218a5..0000000
--- a/uart.c
+++ /dev/null
@@ -1,198 +0,0 @@
-
-/*
- * Copyright (c) 2006-2012 by Roland Riegel <feedback@roland-riegel.de>
- *
- * This file is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
-
-#include <stdio.h>
-#include <avr/interrupt.h>
-#include <avr/io.h>
-#include <avr/pgmspace.h>
-#include <avr/sfr_defs.h>
-#include <avr/sleep.h>
-
-#include "uart.h"
-
-/* some mcus have multiple uarts */
-#ifdef UDR0
-#define UBRRH UBRR0H
-#define UBRRL UBRR0L
-#define UDR UDR0
-
-#define UCSRA UCSR0A
-#define UDRE UDRE0
-#define RXC RXC0
-
-#define UCSRB UCSR0B
-#define RXEN RXEN0
-#define TXEN TXEN0
-#define RXCIE RXCIE0
-
-#define UCSRC UCSR0C
-#define URSEL
-#define UCSZ0 UCSZ00
-#define UCSZ1 UCSZ01
-#define UCSRC_SELECT 0
-#else
-#define UCSRC_SELECT (1 << URSEL)
-#endif
-
-#ifndef USART_RXC_vect
-#if defined(UART0_RX_vect)
-#define USART_RXC_vect UART0_RX_vect
-#elif defined(UART_RX_vect)
-#define USART_RXC_vect UART_RX_vect
-#elif defined(USART0_RX_vect)
-#define USART_RXC_vect USART0_RX_vect
-#elif defined(USART_RX_vect)
-#define USART_RXC_vect USART_RX_vect
-#elif defined(USART0_RXC_vect)
-#define USART_RXC_vect USART0_RXC_vect
-#elif defined(USART_RXC_vect)
-#define USART_RXC_vect USART_RXC_vect
-#else
-#error "Uart receive complete interrupt not defined!"
-#endif
-#endif
-
-#define BAUD 9600UL
-#define UBRRVAL (F_CPU/(BAUD*16)-1)
-#define USE_SLEEP 1
-
-void uart_init()
-{
-    /* set baud rate */
-    UBRRH = UBRRVAL >> 8;
-    UBRRL = UBRRVAL & 0xff;
-    /* set frame format: 8 bit, no parity, 1 bit */
-    UCSRC = UCSRC_SELECT | (1 << UCSZ1) | (1 << UCSZ0);
-    /* enable serial receiver and transmitter */
-#if !USE_SLEEP
-    UCSRB = (1 << RXEN) | (1 << TXEN);
-#else
-    UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
-#endif
-}
-
-void uart_putc(uint8_t c)
-{
-    if(c == '\n')
-       uart_putc('\r');
-
-    /* wait until transmit buffer is empty */
-    while(!(UCSRA & (1 << UDRE)));
-
-    /* send next byte */
-    UDR = c;
-}
-
-void uart_putc_hex(uint8_t b)
-{
-    /* upper nibble */
-    if((b >> 4) < 0x0a)
-       uart_putc((b >> 4) + '0');
-    else
-       uart_putc((b >> 4) - 0x0a + 'a');
-
-    /* lower nibble */
-    if((b & 0x0f) < 0x0a)
-       uart_putc((b & 0x0f) + '0');
-    else
-       uart_putc((b & 0x0f) - 0x0a + 'a');
-}
-
-void uart_putw_hex(uint16_t w)
-{
-    uart_putc_hex((uint8_t) (w >> 8));
-    uart_putc_hex((uint8_t) (w & 0xff));
-}
-
-void uart_putdw_hex(uint32_t dw)
-{
-    uart_putw_hex((uint16_t) (dw >> 16));
-    uart_putw_hex((uint16_t) (dw & 0xffff));
-}
-
-void uart_putw_dec(uint16_t w)
-{
-    uint16_t num = 10000;
-    uint8_t started = 0;
-
-    while(num > 0)
-    {
-       uint8_t b = w / num;
-       if(b > 0 || started || num == 1)
-       {
-           uart_putc('0' + b);
-           started = 1;
-       }
-       w -= b * num;
-
-       num /= 10;
-    }
-}
-
-void uart_putdw_dec(uint32_t dw)
-{
-    uint32_t num = 1000000000;
-    uint8_t started = 0;
-
-    while(num > 0)
-    {
-       uint8_t b = dw / num;
-       if(b > 0 || started || num == 1)
-       {
-           uart_putc('0' + b);
-           started = 1;
-       }
-       dw -= b * num;
-
-       num /= 10;
-    }
-}
-
-void uart_puts(const char* str)
-{
-    while(*str)
-       uart_putc(*str++);
-}
-
-void uart_puts_p(PGM_P str)
-{
-    while(1)
-    {
-       uint8_t b = pgm_read_byte_near(str++);
-       if(!b)
-           break;
-
-       uart_putc(b);
-    }
-}
-
-uint8_t uart_getc()
-{
-    /* wait until receive buffer is full */
-#if USE_SLEEP
-    uint8_t sreg = SREG;
-    sei();
-
-    while(!(UCSRA & (1 << RXC)))
-       sleep_mode();
-
-    SREG = sreg;
-#else
-    while(!(UCSRA & (1 << RXC)));
-#endif
-
-    uint8_t b = UDR;
-    if(b == '\r')
-       b = '\n';
-
-    return b;
-}
-
-EMPTY_INTERRUPT(USART_RXC_vect)
-
diff --git a/uart.h b/uart.h
deleted file mode 100644 (file)
index d94d93d..0000000
--- a/uart.h
+++ /dev/null
@@ -1,42 +0,0 @@
-
-/*
- * Copyright (c) 2006-2012 by Roland Riegel <feedback@roland-riegel.de>
- *
- * This file is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
-
-#ifndef UART_H
-#define UART_H
-
-#include <stdint.h>
-#include <avr/pgmspace.h>
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
-void uart_init();
-
-void uart_putc(uint8_t c);
-
-void uart_putc_hex(uint8_t b);
-void uart_putw_hex(uint16_t w);
-void uart_putdw_hex(uint32_t dw);
-
-void uart_putw_dec(uint16_t w);
-void uart_putdw_dec(uint32_t dw);
-
-void uart_puts(const char* str);
-void uart_puts_p(PGM_P str);
-
-uint8_t uart_getc();
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
-
index 4822653..3785e57 100644 (file)
@@ -15,7 +15,7 @@
  *  along with this program; if not, write to the Free Software\r
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
  *\r
- *  Revision : $Id: uart_config.h,v 1.2.8.1 2006-11-26 21:06:06 zer0 Exp $\r
+ *  Revision : $Id: uart_config.h,v 1.5 2009-11-08 17:24:33 zer0 Exp $\r
  *\r
  */\r
 \r
 #define UART0_INTERRUPT_ENABLED  1\r
 \r
 #define UART0_BAUDRATE 57600\r
-//#define UART0_BAUDRATE 115200\r
 \r
 /*\r
  * if you enable this, the maximum baudrate you can reach is\r
  * higher, but the precision is lower.\r
  */\r
-//#define UART0_USE_DOUBLE_SPEED 0\r
 #define UART0_USE_DOUBLE_SPEED 1\r
 \r
-#define UART0_RX_FIFO_SIZE 4\r
-#define UART0_TX_FIFO_SIZE 4\r
-//#define UART0_NBITS 5\r
-//#define UART0_NBITS 6\r
-//#define UART0_NBITS 7\r
+#define UART0_RX_FIFO_SIZE 64\r
+#define UART0_TX_FIFO_SIZE 127\r
 #define UART0_NBITS 8\r
-//#define UART0_NBITS 9\r
 \r
 #define UART0_PARITY UART_PARTITY_NONE\r
-//#define UART0_PARITY UART_PARTITY_ODD\r
-//#define UART0_PARITY UART_PARTITY_EVEN\r
 \r
 #define UART0_STOP_BIT UART_STOP_BITS_1\r
-//#define UART0_STOP_BIT UART_STOP_BITS_2\r
 \r
+/* .... same for uart 1, 2, 3 ... */\r
+\r
+/*\r
+ * UART1 definitions\r
+ */\r
+\r
+/* compile uart1 fonctions, undefine it to pass compilation */\r
+#define UART1_COMPILE\r
+\r
+/* enable uart1 if == 1, disable if == 0 */\r
+#define UART1_ENABLED  1\r
+\r
+/* enable uart1 interrupts if == 1, disable if == 0 */\r
+#define UART1_INTERRUPT_ENABLED  1\r
+\r
+#define UART1_BAUDRATE 57600\r
+\r
+/*\r
+ * if you enable this, the maximum baudrate you can reach is\r
+ * higher, but the precision is lower.\r
+ */\r
+#define UART1_USE_DOUBLE_SPEED 1\r
+\r
+#define UART1_RX_FIFO_SIZE 64\r
+#define UART1_TX_FIFO_SIZE 127\r
+#define UART1_NBITS 8\r
 \r
+#define UART1_PARITY UART_PARTITY_NONE\r
 \r
+#define UART1_STOP_BIT UART_STOP_BITS_1\r
 \r
 /* .... same for uart 1, 2, 3 ... */\r
 \r