Read max data rating of sd. Add fuction to read the SSR sd register. Add read block...
[bertos.git] / bertos / drv / sd.h
index 42c1d7df1bed9cc579610a8066eac4011e784ee4..813e65a7124c15416be948b25f15d1533cf285b3 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_fat.h"
+#include "cfg/cfg_sd.h"
 
-#include <kern/kfile.h>
+#include <io/kfile.h>
+#include <io/kblock.h>
 
 #include <fs/fatfs/diskio.h>
 
-bool sd_test(void);
+#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    max_data_rate; ///< Step rate, usec
+    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 SDssr
+{
+       uint8_t    bus_width;
+       uint8_t    card_type;
+       uint8_t    speed_class;
+       uint8_t    au_size;
+       uint8_t    erase_size;
+} SDssr;
+
+
+#define SD_READY_FOR_DATA         BV(8)
+#define SD_CARD_IS_LOCKED         BV(25)
+
+#define SD_SEND_CID_RCA     0
+#define SD_SEND_ALL_CID   BV(0)
+
+#endif
+
+
+#define SD_UNBUFFERED     BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed.
 
 /**
- * Initializes the SD driver.
- *
- * \param _fd A pointer to a kfile where the SD will read/write to.
- * \return true if initialization succeds, false otherwise.
+ * SD Card context structure.
  */
-bool sd_init(KFile *_fd);
+typedef struct Sd
+{
 
-#if CONFIG_FAT_DRIVES == 1
+       KBlock b;   ///< KBlock base class
+       KFile *ch;  ///< SPI communication channel
+       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.
 
-       /**
-        * Same as sd_disk_status.
-        *
-        * Card initialization must be done with sd_init.
-        */
-       #define sd_disk_initialize disk_initialize
+       #if CPU_CM3_SAM3X8
+       SDcid cid;
+       SDcsd csd;
+       SDssr ssr;
+       uint32_t addr;
+       uint32_t status;
+       #endif
+
+} Sd;
+
+bool sd_initUnbuf(Sd *sd, KFile *ch);
+bool sd_initBuf(Sd *sd, KFile *ch);
+
+
+#if CPU_CM3_SAM3X8
+
+void sd_dumpCsd(Sd *sd);
+void sd_dumpCid(Sd *sd);
+void sd_dumpSsr(Sd *sd);
+
+void sd_sendInit(void);
+void sd_goIdle(void);
+int sd_sendIfCond(void);
+int sd_sendAppOpCond(void);
+
+int sd_getCid(Sd *sd, uint32_t addr, uint8_t flag);
+int sd_getCsd(Sd *sd);
+int sd_getSrc(Sd *sd);
+
+int sd_appStatus(Sd *sd);
+int sd_getRelativeAddr(Sd *sd);
+
+int sd_getStatus(Sd *sd, uint32_t *buf, size_t words);
+
+int sd_selectCard(Sd *sd);
+int sd_deSelectCard(Sd *sd);
+int sd_setBusWidth(Sd *sd, size_t len);
+int sd_set_BlockLen(Sd *sd, size_t len);
+void sd_setHightSpeed(Sd *sd);
+int sd_readSingleBlock(Sd *sd, size_t index, uint32_t *buf, size_t words);
+
+
+INLINE int sd_setBus4bit(Sd *sd)
+{
+       return sd_setBusWidth(sd, 4);
+}
+
+INLINE int sd_setBus1bit(Sd *sd)
+{
+       return sd_setBusWidth(sd, 1);
+}
+
+#endif
+
+
+#if CONFIG_SD_OLD_INIT
+       #if !(ARCH & ARCH_NIGHTTEST)
+               #warning "Deprecated: this API will be removed in the next major release,"
+               #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()."
+       #endif
 
        /**
-        * Return the status of the disk.
+        * Initializes the SD driver.
         *
-        * \param drv The number of the drive to initialize. Currently only drive 0 is allowed.
-        * \return RES_OK if the sd card was correctly initialized by a previous call to sd_init(), STA_NOINIT otherwise.
-        */
-       #define sd_disk_status     disk_status
-       /**
-        * Read \a count sectors from SD card.
+        * \param ch A pointer to a SPI channel where the SD will read/write to.
+        *
+        * \return true if initialization succeds, false otherwise.
         *
-        * \param drv The drive number to read from. Only 0 is supported.
-        * \param buf A buffer to store read data. You can get sector size using sd_disk_ioctl.
-        * \param sector Start sector number.
-        * \param count The number of sectors to read.
-        * \return RES_OK if the function succeded, RES_ERROR if any error occurred, RES_NOTRDY if the disk is not initialized.
+        * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and
+        *       use the new one instead.
         *
-        * \sa diskio.h
+        * \see CONFIG_SD_OLD_INIT.
         */
-       #define sd_disk_read       disk_read
-
-       #if     CONFIG_FAT_FS_READONLY == 0
-
-               /**
-                * Write \a count sectors to SD card.
-                *
-                * \param drv The drive number to read from. Only 0 is supported.
-                * \param buf The data to be written.
-                * \param sector Start sector number.
-                * \param count The number of sectors to write.
-                * \return RES_OK if the function succeded, RES_ERROR if any error occurred, RES_NOTRDY if the disk is not initialized.
-                *
-                * \sa diskio.h
-                */
-               #define sd_disk_write      disk_write
-       #endif
+       #define sd_init(ch) {static struct Sd sd; sd_initUnbuf(&sd, (ch));}
+
+#else
 
        /**
-        * Interface to send device independant commands to the device.
+        * Initializes the SD driver.
+        *
+        * \param sd The SD KBlock context.
+        * \param ch A pointer to a SPI channel where the SD will read/write to.
+        * \param buffered Set to true if you want the KBlock to be buffered,
+        *        to false otherwise. The FatFs module does not require the device
+        *        to be buffered because it has an internal cache. This will save
+        *        512 bytes of RAM in this case.
         *
-        * \sa diskio.h and related documentation for further explanations.
+        * \return true if initialization succeds, false otherwise.
         */
-       #define sd_disk_ioctl      disk_ioctl
+       #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_initUnbuf((sd), (ch)) : sd_initBuf((sd), (ch)))
 
-#endif /* CONFIG_FAT_DRIVES == 1 */
+#endif
+
+
+#define KBT_SD MAKE_ID('S', 'D', 'B', 'K')
+
+bool sd_test(Sd *sd);
+void sd_writeTest(Sd *sd);
+
+INLINE Sd *SD_CAST(KBlock *b)
+{
+       ASSERT(b->priv.type == KBT_SD);
+       return (Sd *)b;
+}
 
 #endif /* DRV_SD_H */
+