Clean up, and continue the develop.
[bertos.git] / bertos / drv / sd.h
index 368e0d4fff975d6d375257004ed01c1f2f3e4fca..c9eff3c0926a00cb0ca40174c2cdb6d82d373c8c 100644 (file)
  *
  * \brief Function library for secure digital memory.
  *
- * Right now, the interface for these function is the one defined in diskio.h from
- * the FatFS module.
  *
  * \author Francesco Sacchi <batt@develer.com>
  *
  * $WIZ$ module_name = "sd"
- * $WIZ$ module_depends = "kfile", "timer"
+ * $WIZ$ module_depends = "kfile", "timer", "kblock"
  * $WIZ$ module_hw = "bertos/hw/hw_sd.h"
  * $WIZ$ module_configuration = "bertos/cfg/cfg_sd.h"
  */
 #ifndef DRV_SD_H
 #define DRV_SD_H
 
+#include "cfg/cfg_sd.h"
+
 #include <io/kfile.h>
 #include <io/kblock.h>
 
 #include <fs/fatfs/diskio.h>
 
-#include "cfg/cfg_sd.h"
+#if CPU_CM3_SAM3X8
 
+typedef struct SDcid
+{
+    uint8_t        manfid;
+    uint8_t        prod_name[8];
+    uint32_t       serial;
+    uint16_t       oemid;
+    uint32_t       year_off;
+    uint8_t        m_rev;
+    uint8_t        l_rev;
+}SDcid;
+
+typedef struct SDcsd
+{
+       uint8_t     structure;
+    uint8_t     ccc;          ///< Card command classes
+    uint32_t    erase_size;  ///< The size of an erasable sector, in write block len
+       uint32_t    capacity;     ///< Card size in byte
+    uint32_t    blk_len;      ///< Block data size len in byte
+       uint32_t    blk_num;      ///< Number of block in card
+       uint32_t        write_blk_bits; ///< Max write block length in bits
+       uint32_t        read_blk_bits;  ///< Max read block length in bits
+    uint8_t     read_partial:1,
+                read_misalign:1,
+                write_partial:1,
+                write_misalign:1;
+} SDcsd;
+
+
+typedef struct SDAddr
+{
+       uint32_t rca;
+       uint32_t status;
+} SDAddr;
+
+int sd_decode_csd(SDcsd *csd, uint32_t *resp, size_t len);
+void sd_dump_csd(SDcsd *csd);
+void sd_decode_cid(SDcid *cid, uint32_t *resp, size_t len);
+void sd_dump_cid(SDcid *cid);
+void sd_send_init(void);
+void sd_go_idle(void);
+int sd_send_if_cond(void);
+int sd_send_app_op_cond(void);
+int sd_get_cid(uint32_t *resp, size_t len);
+int sd_get_csd(uint32_t *resp, size_t len);
+int sd_app_status(uint32_t *resp, size_t len);
+int sd_send_relative_addr(uint32_t *resp, size_t len);
+void sd_decode_addr(SDAddr *addr, uint32_t *resp, size_t len);
+void sd_dump_addr(SDAddr *addr);
+#endif
 
+
+#define SD_UNBUFFERED     BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed.
+
+/**
+ * SD Card context structure.
+ */
 typedef struct Sd
 {
-       KBlock b;
+       KBlock b;   ///< KBlock base class
        KFile *ch;  ///< SPI communication channel
-       uint16_t r1;
+       uint16_t r1;  ///< Last status data received from SD
+       uint16_t tranfer_len; ///< Lenght for the read/write commands, cached in order to increase speed.
 } Sd;
 
 bool sd_initUnbuf(Sd *sd, KFile *ch);
@@ -79,6 +135,8 @@ bool sd_initBuf(Sd *sd, KFile *ch);
         *
         * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and
         *       use the new one instead.
+        *
+        * \see CONFIG_SD_OLD_INIT.
         */
        #define sd_init(ch) {static struct Sd sd; sd_initUnbuf(&sd, (ch));}
 
@@ -96,7 +154,7 @@ bool sd_initBuf(Sd *sd, KFile *ch);
         *
         * \return true if initialization succeds, false otherwise.
         */
-       #define sd_init(sd, ch, buffered) ((buffered) ? sd_initBuf((sd), (ch)) : sd_initUnbuf((sd), (ch)))
+       #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_initUnbuf((sd), (ch)) : sd_initBuf((sd), (ch)))
 
 #endif
 
@@ -112,5 +170,4 @@ INLINE Sd *SD_CAST(KBlock *b)
        return (Sd *)b;
 }
 
-
 #endif /* DRV_SD_H */