spi_flash_chip_driver.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "esp_flash.h"
  16. struct esp_flash_t;
  17. typedef struct esp_flash_t esp_flash_t;
  18. typedef struct spi_flash_chip_t spi_flash_chip_t;
  19. /** @brief SPI flash chip driver definition structure.
  20. *
  21. * The chip driver structure contains chip-specific pointers to functions to perform SPI flash operations, and some
  22. * chip-specific numeric values.
  23. *
  24. * @note This is not a public API. These functions are called from the public API (declared in
  25. * esp_flash.h). They assume the caller has already validated arguments and enabled relevant protections
  26. * (disabling flash cache, prevent concurrent SPI access, etc.)
  27. *
  28. * Do not call chip driver functions directly in other contexts.
  29. *
  30. * A generic driver for generic chips and its related operations are defined in
  31. * spi_flash_chip_generic.h which can be used as building blocks for written
  32. * new/specific SPI flash chip drivers.
  33. *
  34. * @note All of these functions may be called with SPI flash cache disabled, so must only ever access IRAM/DRAM/ROM.
  35. */
  36. struct spi_flash_chip_t {
  37. const char *name; ///< Name of the chip driver
  38. /* Probe to detect if a supported SPI flash chip is found.
  39. *
  40. * Attempts to configure 'chip' with these operations and probes for a matching SPI flash chip.
  41. *
  42. * Auto-detection of a SPI flash chip calls this function in turn on each registered driver (see esp_flash_registered_flash_drivers).
  43. *
  44. * ID - as read by spi_flash_generic_read_id() - is supplied so each probe
  45. * function doesn't need to unnecessarily read ID, but probe is permitted
  46. * to interrogate flash in any non-destructive way.
  47. *
  48. * It is permissible for the driver to modify the 'chip' structure if probing succeeds (specifically, to assign something to the
  49. * driver_data pointer if that is useful for the driver.)
  50. *
  51. * @return ESP_OK if probing was successful, an error otherwise. Driver may
  52. * assume that returning ESP_OK means it has claimed this chip.
  53. */
  54. esp_err_t (*probe)(esp_flash_t *chip, uint32_t flash_id);
  55. esp_err_t (*reset)(esp_flash_t *chip);
  56. /* Detect SPI flash size
  57. *
  58. * Interrogate the chip to detect its size.
  59. */
  60. esp_err_t (*detect_size)(esp_flash_t *chip, uint32_t *size);
  61. /* Erase the entire chip
  62. Caller has verified the chip is not write protected.
  63. */
  64. esp_err_t (*erase_chip)(esp_flash_t *chip);
  65. /* Erase a sector of the chip. Sector size is specified in the 'sector_size' field.
  66. sector_address is an offset in bytes.
  67. Caller has verified that this sector should be non-write-protected.
  68. */
  69. esp_err_t (*erase_sector)(esp_flash_t *chip, uint32_t sector_address);
  70. /* Erase a multi-sector block of the chip. Block size is specified in the 'block_erase_size' field.
  71. sector_address is an offset in bytes.
  72. Caller has verified that this block should be non-write-protected.
  73. */
  74. esp_err_t (*erase_block)(esp_flash_t *chip, uint32_t block_address);
  75. uint32_t sector_size; /* Sector is minimum erase size */
  76. uint32_t block_erase_size; /* Optimal (fastest) block size for multi-sector erases on this chip */
  77. /* Read the write protect status of the entire chip. */
  78. esp_err_t (*get_chip_write_protect)(esp_flash_t *chip, bool *out_write_protected);
  79. /* Set the write protect status of the entire chip. */
  80. esp_err_t (*set_chip_write_protect)(esp_flash_t *chip, bool chip_write_protect);
  81. /* Number of individually write protectable regions on this chip. Range 0-63. */
  82. uint8_t num_protectable_regions;
  83. /* Pointer to an array describing each protectable region. Should have num_protectable_regions elements. */
  84. const esp_flash_region_t *protectable_regions;
  85. /* Get a bitmask describing all protectable regions on the chip. Each bit represents one entry in the
  86. protectable_regions array, ie bit (1<<N) is set then the region at array entry N is write protected. */
  87. esp_err_t (*get_protected_regions)(esp_flash_t *chip, uint64_t *regions);
  88. /* Set protectable regions on the chip. Each bit represents on entry in the protectable regions array. */
  89. esp_err_t (*set_protected_regions)(esp_flash_t *chip, uint64_t regions);
  90. /* Read data from the chip.
  91. *
  92. * 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.
  93. */
  94. esp_err_t (*read)(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
  95. /* Write any amount of data to the chip.
  96. */
  97. esp_err_t (*write)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  98. /* Use the page program command to write data to the chip.
  99. *
  100. * This function is expected to be called by chip->drv->write (if the
  101. * chip->drv->write implementation doesn't call it then it can be left as NULL.)
  102. *
  103. * - The length argument supplied to this function is at most 'page_size' bytes.
  104. *
  105. * - The region between 'address' and 'address + length' will not cross a page_size aligned boundary (the write
  106. * implementation is expected to split such a write into two before calling page_program.)
  107. */
  108. esp_err_t (*program_page)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  109. /* Page size as written by the page_program function. Usually 256 bytes. */
  110. uint32_t page_size;
  111. /* Perform an encrypted write to the chip, using internal flash encryption hardware. */
  112. esp_err_t (*write_encrypted)(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  113. /* 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.
  114. timeout_ms should be a timeout (in milliseconds) before the function returns ESP_ERR_TIMEOUT. This is useful to avoid hanging
  115. if the chip is otherwise unresponsive (ie returns all 0xFF or similar.)
  116. */
  117. esp_err_t (*wait_idle)(esp_flash_t *chip, unsigned timeout_ms);
  118. /* Configure both the SPI host and the chip for the read mode specified in chip->read_mode.
  119. *
  120. * This function is called by the higher-level API before the 'read' function is called.
  121. *
  122. * Can return ESP_ERR_FLASH_UNSUPPORTED_HOST or ESP_ERR_FLASH_UNSUPPORTED_CHIP if the specified mode is unsupported.
  123. */
  124. esp_err_t (*set_io_mode)(esp_flash_t *chip);
  125. /*
  126. * Get whether the Quad Enable (QE) is set. (*out_io_mode)=SPI_FLASH_QOUT if
  127. * enabled, otherwise disabled
  128. */
  129. esp_err_t (*get_io_mode)(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode);
  130. };
  131. /* Pointer to an array of pointers to all known drivers for flash chips. This array is used
  132. by esp_flash_init() to detect the flash chip driver, if none is supplied by the caller.
  133. Array is terminated with a NULL pointer.
  134. This pointer can be overwritten with a pointer to a new array, to update the list of known flash chips.
  135. */
  136. extern const spi_flash_chip_t **esp_flash_registered_chips;