Update preset.
[bertos.git] / bertos / drv / flash.h
index 509627428fcb183e2c66e6978a5c51fd69d50fb9..d9cf8070448fe38572a2b4edf4dad4b105ad3e37 100644 (file)
 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
 * -->
 *
+* \defgroup drv_emb_flash Embedded flash driver
+* \ingroup drivers
+* \{
 *
 * \brief Embedded flash for cpu.
 *
+* This module allows to access in reading and writing to the internal
+* flash memory of the micro. It is a block device, so it must be
+* accessed using the KBlock interface functions (see kblock.h).
+*
+* Once you have opened the flash for writing, you may want to use
+* kblock_trim() to avoid overwriting data on other flash banks.
+*
+* Example usage:
+* \code
+* Flash fls;
+* flash_init(&fls, 0);
+* // enable access only on desired blocks
+* // start block = 50, num blocks = 20
+* kblock_trim(&fls.blk, 50, 20);
+* // ...
+* // now write to the flash
+* // block number is automatically converted
+* kblock_write(&fls.blk, 0, buf, 0, 128);
+* \endcode
+*
 * \author Francesco Sacchi <batt@develer.com>
 * \author Daniele Basile <asterix@develer.com>
 *
 * $WIZ$ module_name = "flash"
-* $WIZ$ module_depends = "kfile"
+* $WIZ$ module_depends = "kfile", "kfile_block", "kblock"
+* $WIZ$ module_configuration = "bertos/cfg/cfg_emb_flash.h"
 */
 
 #ifndef DRV_FLASH_H
 #define DRV_FLASH_H
 
+#include "cfg/cfg_emb_flash.h"
+
 #include <cfg/macros.h>
+#include <cfg/compiler.h>
 
 #include <io/kblock.h>
+#include <io/kfile.h>
+#include <io/kfile_block.h>
 
-/*
- * Embedded flash error flags
+#include <cpu/attr.h>
+
+#if COMPILER_C99
+       #define flash_init(...)           PP_CAT(flash_init_, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
+#else
+       /**
+        * Init function for flash driver.
+        *
+        * This macro cannot fail, so no error conditions are reported.
+        *
+        * This macro expands to
+        *  - flash_init_2(Flash *fls, flags), the new KBlock API
+        *  - flash_init_1(Flash *fls), old API, provided for compatibility
+        *
+        * Do NOT use the above functions directly, use flash_init() instead.
+        * Disable old API if you are not upgrading an existing project.
+        */
+       #define flash_init(args...)       PP_CAT(flash_init_, COUNT_PARMS(args)) (args)
+#endif
+
+/**
+ * \name Embedded flash error values
+ * \{
  */
 #define FLASH_WR_OK             0     ///< Write ok.
 #define FLASH_NOT_ERASED     BV(1)    ///< Flash memory was not erased before to write it.
 #define FLASH_WR_PROTECT     BV(2)    ///< Write not allowed the flash memory was protected.
-#define FLASH_WR_TIMEOUT     BV(3)    ///<
+#define FLASH_WR_TIMEOUT     BV(3)    ///< Timeout while writing
+#define FLASH_WR_ERR         BV(4)    ///< Invalid command and/or a bad keywords
+/** \} */
 
 struct FlashHardware;
 
 /**
- * EmbFlash KFile context structure.
+ * EmbFlash KBlock context structure.
  */
 typedef struct Flash
 {
-       KBlock blk;
+       KBlock blk;                  ///< KBlock context
        struct FlashHardware *hw;
+       #if !CONFIG_FLASH_DISABLE_OLD_API
+       union {
+               KFile fd;
+               KFileBlock fdblk;
+       } DEPRECATED;
+       #endif /* !CONFIG_FLASH_DISABLE_OLD_API */
 } Flash;
 
-/**
+/*
  * ID for FLASH
  */
 #define KBT_FLASH MAKE_ID('F', 'L', 'A', 'S')
 
 /**
-* Convert + ASSERT from generic KFile to Flash.
+* Convert + ASSERT from generic KBlock to Flash.
 */
 INLINE Flash *FLASH_CAST(KBlock *fls)
 {
@@ -79,79 +137,35 @@ INLINE Flash *FLASH_CAST(KBlock *fls)
        return (Flash *)fls;
 }
 
-void flash_hw_init(Flash *fls);
-void flash_hw_initUnbuffered(Flash *fls);
+void flash_hw_init(Flash *fls, int flags);
+void flash_hw_initUnbuffered(Flash *fls, int flags);
 
 #include CPU_HEADER(flash)
 
-INLINE void flash_init(Flash *fls)
-{
-       flash_hw_init(fls);
-}
-
-INLINE void flash_initUnbuffered(Flash *fls)
-{
-       flash_hw_initUnbuffered(fls);
-}
-
-#if 0
 /**
- * EmbFlash KFile context structure.
+ * \name Flash init flags
+ * \{
  */
-typedef struct Flash
-{
-       /**
-        * File descriptor.
-        */
-       KFile fd;
-
-       /**
-        * Flag for checking if current page is modified.
-        */
-       bool page_dirty;
-
-       /**
-        * Current buffered page.
-        */
-       page_t curr_page;
-
-       /**
-        * Temporary buffer cointaing data block to
-        * write on flash.
-        */
-       uint8_t page_buf[FLASH_PAGE_SIZE];
-} Flash;
-
-/**
-* ID for FLASH
-*/
-#define KFT_FLASH MAKE_ID('F', 'L', 'A', 'S')
+#define FLASH_WRITE_ONCE   BV(0) ///< Allow only one write per block.
+#define FLASH_UNBUFFERED   BV(1) ///< Open flash memory disabling page caching, no modification and partial write are allowed.
+/** \} */
 
 /**
-* Convert + ASSERT from generic KFile to Flash.
-*/
-INLINE Flash * FLASH_CAST(KFile *fd)
-{
-       ASSERT(fd->_type == KFT_FLASH);
-       return (Flash *)fd;
-}
-
-
-MOD_DEFINE(flash);
-
-/**
- *
- * Initialize PWM hw.
+ * Initialize \a fls Flash context structure.
+ * \param fls Flash context structure
+ * \param flags A combination of flash init flags
  */
-INLINE void flash_init(Flash *fd)
-{
-       flash_hw_init(fd);
+#define flash_init_2(fls, flags)    (flags & FLASH_UNBUFFERED) ? \
+                                                                               flash_hw_initUnbuffered(fls, flags) : flash_hw_init(fls, flags)
 
-       MOD_INIT(flash);
+#if !CONFIG_FLASH_DISABLE_OLD_API
+INLINE DEPRECATED void flash_init_1(Flash *fls)
+{
+       flash_hw_init(fls, 0);
+       kfileblock_init(&fls->fdblk, &fls->blk);
 }
+#endif /* !CONFIG_FLASH_DISABLE_OLD_API */
 
-#endif
-
+/** \} */
 
 #endif /* DRV_FLASH_H */
-