spi_flash_hal_common.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include <stdlib.h>
  15. #include "hal/spi_flash_hal.h"
  16. #include "string.h"
  17. #include "hal/hal_defs.h"
  18. #include "soc/soc_caps.h"
  19. #include "sdkconfig.h"
  20. #define ADDRESS_MASK_24BIT 0xFFFFFF
  21. #define COMPUTE_DUMMY_CYCLELEN(host, base) ((base) + ((spi_flash_hal_context_t*)host)->extra_dummy)
  22. static inline spi_dev_t *get_spi_dev(spi_flash_host_inst_t *host)
  23. {
  24. return ((spi_flash_hal_context_t*)host)->spi;
  25. }
  26. static inline int get_host_id(spi_flash_host_inst_t* host)
  27. {
  28. spi_dev_t *dev = get_spi_dev(host);
  29. return spi_flash_ll_hw_get_id(dev);
  30. }
  31. void spi_flash_hal_poll_cmd_done(spi_flash_host_inst_t *host)
  32. {
  33. while (!spi_flash_ll_cmd_is_done(get_spi_dev(host))) {
  34. //nop
  35. }
  36. }
  37. esp_err_t spi_flash_hal_device_config(spi_flash_host_inst_t *host)
  38. {
  39. spi_flash_hal_context_t* ctx = (spi_flash_hal_context_t*)host;
  40. spi_dev_t *dev = get_spi_dev(host);
  41. spi_flash_ll_reset(dev);
  42. spi_flash_ll_set_cs_pin(dev, ctx->cs_num);
  43. spi_flash_ll_set_clock(dev, &ctx->clock_conf);
  44. int cs_hold = ctx->cs_hold;
  45. spi_flash_ll_set_hold(dev, cs_hold);
  46. return ESP_OK;
  47. }
  48. esp_err_t spi_flash_hal_configure_host_io_mode(
  49. spi_flash_host_inst_t *host,
  50. uint32_t command,
  51. uint32_t addr_bitlen,
  52. int dummy_cyclelen_base,
  53. esp_flash_io_mode_t io_mode)
  54. {
  55. spi_dev_t *dev = get_spi_dev(host);
  56. int host_id = spi_flash_ll_hw_get_id(dev);
  57. uint32_t extra_bits = io_mode & 0xFFFF0000;
  58. io_mode = io_mode & 0xFFFF;
  59. /*
  60. * Some flash chips, when working under some IO modes (DIO, QIO and OIO in the future), treat
  61. * the first 8 bits of the dummy bits as the bits. When the bits meet some pattern, the chip
  62. * will go into a "continuous (XIP)" mode, where the command field will be skipped in the next
  63. * transaction. We have to output all ones in these cycles because we don't need this feature.
  64. */
  65. bool conf_required = ((extra_bits & SPI_FLASH_CONFIG_CONF_BITS) != 0);
  66. if (!SOC_SPI_PERIPH_SUPPORT_MULTILINE_MODE(host_id) && io_mode > SPI_FLASH_FASTRD) {
  67. return ESP_ERR_NOT_SUPPORTED;
  68. }
  69. #if SOC_SPI_PERIPH_SUPPORT_CONTROL_DUMMY_OUTPUT
  70. // The CONTROL_DUMMY_OUTPUT feature is used to control M7-M0 bits.
  71. spi_flash_ll_set_dummy_out(dev, (conf_required? 1: 0), 1);
  72. #else
  73. // On ESP32, dummy output is not supported. These dummy bits will be moved into the address
  74. // phase (and appended as ones).
  75. if (conf_required) {
  76. int line_width = (io_mode == SPI_FLASH_DIO? 2: 4);
  77. dummy_cyclelen_base -= 4 / line_width;
  78. addr_bitlen += 4; //extra 4 bits indicate the conf bits is included
  79. }
  80. #endif
  81. if (command >= 0x100) {
  82. spi_flash_ll_set_command(dev, command, 16);
  83. } else {
  84. spi_flash_ll_set_command(dev, command, 8);
  85. }
  86. spi_flash_ll_set_addr_bitlen(dev, addr_bitlen);
  87. // Add dummy cycles to compensate for latency of GPIO matrix and external delay, if necessary...
  88. spi_flash_ll_set_dummy(dev, COMPUTE_DUMMY_CYCLELEN(host, dummy_cyclelen_base));
  89. //disable all data phases, enable them later if needed
  90. spi_flash_ll_set_miso_bitlen(dev, 0);
  91. spi_flash_ll_set_mosi_bitlen(dev, 0);
  92. spi_flash_ll_set_read_mode(dev, io_mode);
  93. return ESP_OK;
  94. }
  95. esp_err_t spi_flash_hal_common_command(spi_flash_host_inst_t *host, spi_flash_trans_t *trans)
  96. {
  97. spi_dev_t *dev = get_spi_dev(host);
  98. esp_flash_io_mode_t io_mode = ((spi_flash_hal_context_t*)host)->base_io_mode;
  99. uint16_t command = trans->command;
  100. uint8_t dummy_bitlen = trans->dummy_bitlen;
  101. if ((trans->flags & SPI_FLASH_TRANS_FLAG_IGNORE_BASEIO) != 0) {
  102. io_mode = 0;
  103. }
  104. host->driver->configure_host_io_mode(host, command, trans->address_bitlen, dummy_bitlen, io_mode);
  105. spi_flash_ll_set_usr_address(dev, trans->address, trans->address_bitlen);
  106. //No extra dummy cycles for compensation if no input data
  107. if (trans->miso_len == 0) {
  108. spi_flash_ll_set_dummy(dev, dummy_bitlen);
  109. }
  110. spi_flash_ll_set_mosi_bitlen(dev, trans->mosi_len * 8);
  111. spi_flash_ll_set_buffer_data(dev, trans->mosi_data, trans->mosi_len);
  112. spi_flash_ll_set_miso_bitlen(dev, trans->miso_len * 8);
  113. spi_flash_ll_user_start(dev);
  114. host->driver->poll_cmd_done(host);
  115. spi_flash_ll_get_buffer_data(dev, trans->miso_data, trans->miso_len);
  116. return ESP_OK;
  117. }
  118. esp_err_t spi_flash_hal_read(spi_flash_host_inst_t *host, void *buffer, uint32_t address, uint32_t read_len)
  119. {
  120. spi_dev_t *dev = get_spi_dev(host);
  121. int bitlen = spi_flash_ll_get_addr_bitlen(dev);
  122. //Only 24-bit and 32-bit address are supported. The extra length are for M7-M0, which should be
  123. //filled with ones by the function below
  124. spi_flash_ll_set_usr_address(dev, address, bitlen & (~7));
  125. spi_flash_ll_set_miso_bitlen(dev, read_len * 8);
  126. spi_flash_ll_user_start(dev);
  127. host->driver->poll_cmd_done(host);
  128. spi_flash_ll_get_buffer_data(dev, buffer, read_len);
  129. return ESP_OK;
  130. }