spi_flash_chip_driver.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_flash.h"
  8. #include "esp_attr.h"
  9. struct esp_flash_t;
  10. typedef struct esp_flash_t esp_flash_t;
  11. typedef struct spi_flash_chip_t spi_flash_chip_t;
  12. /** Timeout configurations for flash operations, all in us */
  13. typedef struct {
  14. uint32_t idle_timeout; ///< Default timeout for other commands to be sent by host and get done by flash
  15. uint32_t chip_erase_timeout; ///< Timeout for chip erase operation
  16. uint32_t block_erase_timeout; ///< Timeout for block erase operation
  17. uint32_t sector_erase_timeout; ///< Timeout for sector erase operation
  18. uint32_t page_program_timeout; ///< Timeout for page program operation
  19. } flash_chip_op_timeout_t;
  20. typedef enum {
  21. SPI_FLASH_REG_STATUS = 1,
  22. } spi_flash_register_t;
  23. typedef enum {
  24. SPI_FLASH_CHIP_CAP_SUSPEND = BIT(0), ///< Flash chip support suspend feature.
  25. SPI_FLASH_CHIP_CAP_32MB_SUPPORT = BIT(1), ///< Flash chip driver support flash size larger than 32M Bytes.
  26. SPI_FLASH_CHIP_CAP_UNIQUE_ID = BIT(2), ///< Flash chip driver support read the flash unique id.
  27. } spi_flash_caps_t;
  28. /** @brief SPI flash chip driver definition structure.
  29. *
  30. * The chip driver structure contains chip-specific pointers to functions to perform SPI flash operations, and some
  31. * chip-specific numeric values.
  32. *
  33. * @note This is not a public API. These functions are called from the public API (declared in
  34. * esp_flash.h). They assume the caller has already validated arguments and enabled relevant protections
  35. * (disabling flash cache, prevent concurrent SPI access, etc.)
  36. *
  37. * Do not call chip driver functions directly in other contexts.
  38. *
  39. * A generic driver for generic chips and its related operations are defined in
  40. * spi_flash_chip_generic.h which can be used as building blocks for written
  41. * new/specific SPI flash chip drivers.
  42. *
  43. * @note All of these functions may be called with SPI flash cache disabled, so must only ever access IRAM/DRAM/ROM.
  44. */
  45. struct spi_flash_chip_t {
  46. const char *name; ///< Name of the chip driver
  47. const flash_chip_op_timeout_t *timeout; ///< Timeout configuration for this chip
  48. /* Probe to detect if a supported SPI flash chip is found.
  49. *
  50. * Attempts to configure 'chip' with these operations and probes for a matching SPI flash chip.
  51. *
  52. * Auto-detection of a SPI flash chip calls this function in turn on each registered driver (see esp_flash_registered_flash_drivers).
  53. *
  54. * ID - as read by spi_flash_generic_read_id() - is supplied so each probe
  55. * function doesn't need to unnecessarily read ID, but probe is permitted
  56. * to interrogate flash in any non-destructive way.
  57. *
  58. * It is permissible for the driver to modify the 'chip' structure if probing succeeds (specifically, to assign something to the
  59. * driver_data pointer if that is useful for the driver.)
  60. *
  61. * @return ESP_OK if probing was successful, an error otherwise. Driver may
  62. * assume that returning ESP_OK means it has claimed this chip.
  63. */
  64. esp_err_t (*probe)(esp_flash_t *chip, uint32_t flash_id);
  65. esp_err_t (*reset)(esp_flash_t *chip);
  66. /* Detect SPI flash size
  67. *
  68. * Interrogate the chip to detect its size.
  69. */
  70. esp_err_t (*detect_size)(esp_flash_t *chip, uint32_t *size);
  71. /* Erase the entire chip
  72. Caller has verified the chip is not write protected.
  73. */
  74. esp_err_t (*erase_chip)(esp_flash_t *chip);
  75. /* Erase a sector of the chip. Sector size is specified in the 'sector_size' field.
  76. sector_address is an offset in bytes.
  77. Caller has verified that this sector should be non-write-protected.
  78. */
  79. esp_err_t (*erase_sector)(esp_flash_t *chip, uint32_t sector_address);
  80. /* Erase a multi-sector block of the chip. Block size is specified in the 'block_erase_size' field.
  81. sector_address is an offset in bytes.
  82. Caller has verified that this block should be non-write-protected.
  83. */
  84. esp_err_t (*erase_block)(esp_flash_t *chip, uint32_t block_address);
  85. uint32_t sector_size; /* Sector is minimum erase size */
  86. uint32_t block_erase_size; /* Optimal (fastest) block size for multi-sector erases on this chip */
  87. /* Read the write protect status of the entire chip. */
  88. esp_err_t (*get_chip_write_protect)(esp_flash_t *chip, bool *out_write_protected);
  89. /* Set the write protect status of the entire chip. */
  90. esp_err_t (*set_chip_write_protect)(esp_flash_t *chip, bool chip_write_protect);
  91. /* Number of individually write protectable regions on this chip. Range 0-63. */
  92. uint8_t num_protectable_regions;
  93. /* Pointer to an array describing each protectable region. Should have num_protectable_regions elements. */
  94. const esp_flash_region_t *protectable_regions;
  95. /* Get a bitmask describing all protectable regions on the chip. Each bit represents one entry in the
  96. protectable_regions array, ie bit (1<<N) is set then the region at array entry N is write protected. */
  97. esp_err_t (*get_protected_regions)(esp_flash_t *chip, uint64_t *regions);
  98. /* Set protectable regions on the chip. Each bit represents on entry in the protectable regions array. */
  99. esp_err_t (*set_protected_regions)(esp_flash_t *chip, uint64_t regions);
  100. /* Read data from the chip.
  101. *
  102. * Before calling this function, the caller will have called chip->drv->set_read_mode(chip) in order to configure the chip's read mode correctly.
  103. */
  104. esp_err_t (*read)(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
  105. /* Write any amount of data to the chip.
  106. */
  107. esp_err_t (*write)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  108. /* Use the page program command to write data to the chip.
  109. *
  110. * This function is expected to be called by chip->drv->write (if the
  111. * chip->drv->write implementation doesn't call it then it can be left as NULL.)
  112. *
  113. * - The length argument supplied to this function is at most 'page_size' bytes.
  114. *
  115. * - The region between 'address' and 'address + length' will not cross a page_size aligned boundary (the write
  116. * implementation is expected to split such a write into two before calling page_program.)
  117. */
  118. esp_err_t (*program_page)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  119. /* Page size as written by the page_program function. Usually 256 bytes. */
  120. uint32_t page_size;
  121. /* Perform an encrypted write to the chip, using internal flash encryption hardware. */
  122. esp_err_t (*write_encrypted)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  123. /* Wait for the SPI flash chip to be idle (any write operation to be complete.) This function is both called from the higher-level API functions, and from other functions in this structure.
  124. timeout_ms should be a timeout (in milliseconds) before the function returns ESP_ERR_TIMEOUT. This is useful to avoid hanging
  125. if the chip is otherwise unresponsive (ie returns all 0xFF or similar.)
  126. */
  127. esp_err_t (*wait_idle)(esp_flash_t *chip, uint32_t timeout_us);
  128. /* Configure both the SPI host and the chip for the read mode specified in chip->read_mode.
  129. *
  130. * This function is called by the higher-level API before the 'read' function is called.
  131. *
  132. * Can return ESP_ERR_FLASH_UNSUPPORTED_HOST or ESP_ERR_FLASH_UNSUPPORTED_CHIP if the specified mode is unsupported.
  133. */
  134. esp_err_t (*set_io_mode)(esp_flash_t *chip);
  135. /*
  136. * Get whether the Quad Enable (QE) is set. (*out_io_mode)=SPI_FLASH_QOUT if
  137. * enabled, otherwise disabled
  138. */
  139. esp_err_t (*get_io_mode)(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode);
  140. /*
  141. * Read the chip ID. Called when chip driver is set, but we want to know the exact chip id (to
  142. * get the size, etc.).
  143. */
  144. esp_err_t (*read_id)(esp_flash_t *chip, uint32_t* out_chip_id);
  145. /*
  146. * Read the requested register (status, etc.).
  147. */
  148. esp_err_t (*read_reg)(esp_flash_t *chip, spi_flash_register_t reg_id, uint32_t* out_reg);
  149. /** Yield to other tasks. Called during erase operations. */
  150. esp_err_t (*yield)(esp_flash_t *chip, uint32_t wip);
  151. /** Setup flash suspend configuration. */
  152. esp_err_t (*sus_setup)(esp_flash_t *chip);
  153. /**
  154. * Read the chip unique ID.
  155. */
  156. esp_err_t (*read_unique_id)(esp_flash_t *chip, uint64_t* flash_unique_id);
  157. /**
  158. * Get the capabilities of the flash chip. See SPI_FLASH_CHIP_CAP_* macros as reference.
  159. */
  160. spi_flash_caps_t (*get_chip_caps)(esp_flash_t *chip);
  161. /**
  162. * Configure the host registers to use the specified read mode set in the ``chip->read_mode``.
  163. */
  164. esp_err_t (*config_host_io_mode)(esp_flash_t *chip, uint32_t flags);
  165. };
  166. /* Pointer to an array of pointers to all known drivers for flash chips. This array is used
  167. by esp_flash_init() to detect the flash chip driver, if none is supplied by the caller.
  168. Array is terminated with a NULL pointer.
  169. This pointer can be overwritten with a pointer to a new array, to update the list of known flash chips.
  170. */
  171. extern const spi_flash_chip_t **esp_flash_registered_chips;