From 2d4c475d1d9a67d03193366a6a402d00de7f414a Mon Sep 17 00:00:00 2001
From: Giovanni Di Sirio <gdisirio@gmail.com>
Date: Tue, 14 Jun 2016 09:26:16 +0000
Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@9625
 35acf78f-673a-0410-8e92-d51de3d6d3f4

---
 os/hal/lib/peripherals/flash/hal_flash.c         | 40 +++++++++++++++++++++
 os/hal/lib/peripherals/flash/hal_flash.h         | 44 +++++++++++++-----------
 os/hal/lib/peripherals/flash/hal_jesd216_flash.c | 16 ++++-----
 os/hal/lib/peripherals/flash/hal_jesd216_flash.h | 15 ++++----
 4 files changed, 79 insertions(+), 36 deletions(-)

(limited to 'os/hal/lib/peripherals/flash')

diff --git a/os/hal/lib/peripherals/flash/hal_flash.c b/os/hal/lib/peripherals/flash/hal_flash.c
index b65ca3768..439a9c6aa 100644
--- a/os/hal/lib/peripherals/flash/hal_flash.c
+++ b/os/hal/lib/peripherals/flash/hal_flash.c
@@ -72,4 +72,44 @@ flash_error_t flashWaitErase(BaseFlash *devp) {
   }
 }
 
+/**
+ * @brief   Returns the offset of a sector.
+ */
+flash_offset_t flashGetSectorOffset(BaseFlash *devp,
+                                    flash_sector_t sector) {
+  flash_offset_t offset;
+  const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
+
+  osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
+
+  if (descriptor->sectors != NULL) {
+    offset = descriptor->sectors[sector].offset;
+  }
+  else {
+    offset = (flash_offset_t)sector * (flash_offset_t)descriptor->sectors_size;
+  }
+
+  return offset;
+}
+
+/**
+ * @brief   Returns the size of a sector.
+ */
+uint32_t flashGetSectorSize(BaseFlash *devp,
+                            flash_sector_t sector) {
+  uint32_t size;
+  const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
+
+  osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
+
+  if (descriptor->sectors != NULL) {
+    size = descriptor->sectors[sector].size;
+  }
+  else {
+    size = descriptor->sectors_size;
+  }
+
+  return size;
+}
+
 /** @} */
diff --git a/os/hal/lib/peripherals/flash/hal_flash.h b/os/hal/lib/peripherals/flash/hal_flash.h
index b15c18b25..f7066e2ef 100644
--- a/os/hal/lib/peripherals/flash/hal_flash.h
+++ b/os/hal/lib/peripherals/flash/hal_flash.h
@@ -78,9 +78,9 @@ typedef enum {
 } flash_error_t;
 
 /**
- * @brief   Type of a flash address.
+ * @brief   Type of a flash offset.
  */
-typedef uint32_t flash_address_t;
+typedef uint32_t flash_offset_t;
 
 /**
  * @brief   Type of a flash sector number.
@@ -92,13 +92,13 @@ typedef uint32_t flash_sector_t;
  */
 typedef struct {
   /**
-   * @brief         Sector address.
+   * @brief         Sector offset.
    */
-  flash_address_t       address;
+  flash_offset_t        offset;
   /**
    * @brief         Sector size.
    */
-  size_t                size;
+  uint32_t              size;
 } flash_sector_descriptor_t;
 
 /**
@@ -112,7 +112,7 @@ typedef struct {
   /**
    * @brief     Size of write page.
    */
-  size_t                page_size;
+  uint32_t              page_size;
   /**
    * @brief     Number of sectors in the device.
    */
@@ -125,15 +125,15 @@ typedef struct {
   const flash_sector_descriptor_t *sectors;
   /**
    * @brief     Size of sectors for devices with uniform sector size.
-   * @note      If zero then the device has non uniform sectos described
+   * @note      If zero then the device has non uniform sectors described
    *            by the @p sectors array.
    */
-  size_t                sectors_size;
+  uint32_t              sectors_size;
   /**
    * @brief     Flash address if memory mapped or zero.
    * @note      Conventionally, non memory mapped devices have address zero.
    */
-  flash_address_t       address;
+  flash_offset_t        address;
 } flash_descriptor_t;
 
 /**
@@ -144,11 +144,11 @@ typedef struct {
   /* Get flash device attributes.*/                                         \
   const flash_descriptor_t * (*get_descriptor)(void *instance);             \
   /* Read operation.*/                                                      \
-  flash_error_t (*read)(void *instance, flash_address_t addr,               \
-                        uint8_t *rp, size_t n);                             \
+  flash_error_t (*read)(void *instance, flash_offset_t offset,              \
+                        size_t n, uint8_t *rp);                             \
   /* Program operation.*/                                                   \
-  flash_error_t (*program)(void *instance, flash_address_t addr,            \
-                           const uint8_t *pp, size_t n);                    \
+  flash_error_t (*program)(void *instance, flash_offset_t offset,           \
+                           size_t n, const uint8_t *pp);                    \
   /* Erase whole flash device.*/                                            \
   flash_error_t (*start_erase_all)(void *instance);                         \
   /* Erase single sector.*/                                                 \
@@ -211,9 +211,9 @@ typedef struct {
  * @brief   Read operation.
  *
  * @param[in] ip        pointer to a @p BaseFlash or derived class
- * @param[in] addr      flash address
- * @param[out] rp       pointer to the data buffer
+ * @param[in] offset    flash offset
  * @param[in] n         number of bytes to be read
+ * @param[out] rp       pointer to the data buffer
  * @return              An error code.
  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
@@ -221,16 +221,16 @@ typedef struct {
  *
  * @api
  */
-#define flashRead(ip, addr, rp, n)                                          \
-  (ip)->vmt->read(ip, addr, rp, n)
+#define flashRead(ip, offset, n, rp)                                        \
+  (ip)->vmt->read(ip, offset, n, rp)
 
 /**
  * @brief   Program operation.
  *
  * @param[in] ip        pointer to a @p BaseFlash or derived class
- * @param[in] addr      flash address
- * @param[in] wp        pointer to the data buffer
+ * @param[in] offset    flash offset
  * @param[in] n         number of bytes to be programmed
+ * @param[in] wp        pointer to the data buffer
  * @return              An error code.
  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
@@ -238,8 +238,8 @@ typedef struct {
  *
  * @api
  */
-#define flashProgram(ip, addr, pp, n)                                       \
-  (ip)->vmt->program(ip, addr, pp, n)
+#define flashProgram(ip, offset, n, pp)                                     \
+  (ip)->vmt->program(ip, offset, n, pp)
 
 /**
  * @brief   Starts a whole-device erase operation.
@@ -308,6 +308,8 @@ typedef struct {
 extern "C" {
 #endif
   flash_error_t flashWaitErase(BaseFlash *devp);
+  flash_offset_t flashGetSectorOffset(BaseFlash *devp, flash_sector_t sector);
+  uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector);
 #ifdef __cplusplus
 }
 #endif
diff --git a/os/hal/lib/peripherals/flash/hal_jesd216_flash.c b/os/hal/lib/peripherals/flash/hal_jesd216_flash.c
index cd422f53b..9cc986c04 100644
--- a/os/hal/lib/peripherals/flash/hal_jesd216_flash.c
+++ b/os/hal/lib/peripherals/flash/hal_jesd216_flash.c
@@ -157,7 +157,7 @@ void jesd216_cmd_send(BUSDriver *busp,
 
 void jesd216_cmd_addr(BUSDriver *busp,
                       uint32_t cmd,
-                      flash_address_t addr) {
+                      flash_offset_t offset) {
 #if JESD216_BUS_MODE != JESD216_BUS_MODE_SPI
   qspi_command_t mode;
 
@@ -176,7 +176,7 @@ void jesd216_cmd_addr(BUSDriver *busp,
              QSPI_CFG_ADDR_SIZE_24;
 
 #endif
-  mode.addr = addr;
+  mode.addr = offset;
   mode.alt  = 0U;
   qspiCommand(busp, &mode);
 #else
@@ -194,7 +194,7 @@ void jesd216_cmd_addr(BUSDriver *busp,
 
 void jesd216_cmd_addr_send(BUSDriver *busp,
                            uint32_t cmd,
-                           flash_address_t addr,
+                           flash_offset_t offset,
                            size_t n,
                            const uint8_t *p) {
 #if JESD216_BUS_MODE != JESD216_BUS_MODE_SPI
@@ -223,7 +223,7 @@ void jesd216_cmd_addr_send(BUSDriver *busp,
     mode .cfg |= QSPI_CFG_ADDR_SIZE_32;
   }
 
-  mode.addr = addr;
+  mode.addr = offset;
   mode.alt  = 0U;
   qspiSend(busp, &mode, n, p);
 #else
@@ -242,7 +242,7 @@ void jesd216_cmd_addr_send(BUSDriver *busp,
 
 void jesd216_cmd_addr_receive(BUSDriver *busp,
                               uint32_t cmd,
-                              flash_address_t addr,
+                              flash_offset_t offset,
                               size_t n,
                               uint8_t *p) {
 #if JESD216_BUS_MODE != JESD216_BUS_MODE_SPI
@@ -272,7 +272,7 @@ void jesd216_cmd_addr_receive(BUSDriver *busp,
     mode .cfg |= QSPI_CFG_ADDR_SIZE_32;
   }
 
-  mode.addr = addr;
+  mode.addr = offset;
   mode.alt  = 0U;
   qspiReceive(busp, &mode, n, p);
 #else
@@ -292,7 +292,7 @@ void jesd216_cmd_addr_receive(BUSDriver *busp,
 #if (JESD216_BUS_MODE != JESD216_BUS_MODE_SPI) || defined(__DOXYGEN__)
 void jesd216_cmd_addr_dummy_receive(BUSDriver *busp,
                                     uint32_t cmd,
-                                    flash_address_t addr,
+                                    flash_offset_t offset,
                                     uint8_t dummy,
                                     size_t n,
                                     uint8_t *p) {
@@ -324,7 +324,7 @@ void jesd216_cmd_addr_dummy_receive(BUSDriver *busp,
     mode .cfg |= QSPI_CFG_ADDR_SIZE_32;
   }
 
-  mode.addr = addr;
+  mode.addr = offset;
   mode.alt  = 0U;
   qspiReceive(busp, &mode, n, p);
 }
diff --git a/os/hal/lib/peripherals/flash/hal_jesd216_flash.h b/os/hal/lib/peripherals/flash/hal_jesd216_flash.h
index 1cff3cc32..371892ec1 100644
--- a/os/hal/lib/peripherals/flash/hal_jesd216_flash.h
+++ b/os/hal/lib/peripherals/flash/hal_jesd216_flash.h
@@ -141,9 +141,10 @@
  */
 #define _jesd216_flash_methods_alone                                        \
   /* Read SFDP.*/                                                           \
-  flash_error_t (*read_sfdp)(void *instance, uint8_t *rp,                   \
-                 flash_address_t addr,                                      \
-                 size_t n);
+  flash_error_t (*read_sfdp)(void *instance,                                \
+                 flash_offset_t offset,                                     \
+                 size_t n,                                                  \
+                 uint8_t *rp);
 
 /**
  * @brief   @p JESD215Flash specific methods with inherited ones.
@@ -198,14 +199,14 @@ extern "C" {
                            size_t n, uint8_t *p);
   void jesd216_cmd_send(BUSDriver *busp, uint32_t cmd,
                         size_t n, const uint8_t *p);
-  void jesd216_cmd_addr(BUSDriver *busp, uint32_t cmd, flash_address_t addr);
+  void jesd216_cmd_addr(BUSDriver *busp, uint32_t cmd, flash_offset_t offset);
   void jesd216_cmd_addr_send(BUSDriver *busp, uint32_t cmd,
-                             flash_address_t addr, size_t n, const uint8_t *p);
+                             flash_offset_t offset, size_t n, const uint8_t *p);
   void jesd216_cmd_addr_receive(BUSDriver *busp, uint32_t cmd,
-                                flash_address_t addr, size_t n, uint8_t *p);
+                                flash_offset_t offset, size_t n, uint8_t *p);
 #if JESD216_BUS_MODE != JESD216_BUS_MODE_SPI
   void jesd216_cmd_addr_dummy_receive(BUSDriver *busp, uint32_t cmd,
-                                      flash_address_t addr, uint8_t dummy,
+                                      flash_offset_t offset, uint8_t dummy,
                                       size_t n, uint8_t *p);
 #endif /* JESD216_BUS_MODE != JESD216_BUS_MODE_SPI */
 #if JESD216_SHARED_BUS == TRUE
-- 
cgit v1.2.3