You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.6 KiB

  1. /*
  2. Copyright (C) 2021 Westberry Technology (ChangZhou) Corp., Ltd
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. /* All the following default configurations are based on MX25L4006E Nor FLASH. */
  16. /*
  17. The slave select pin of the FLASH.
  18. This needs to be a normal GPIO pin_t value, such as B14.
  19. */
  20. #ifndef EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN
  21. # error "No chip select pin defined -- missing EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN"
  22. #endif
  23. /*
  24. The clock divisor for SPI to ensure that the MCU is within the
  25. specifications of the FLASH chip. Generally this will be PCLK divided by
  26. the intended divisor -- check your clock settings and the datasheet of
  27. your FLASH.
  28. */
  29. #ifndef EXTERNAL_FLASH_SPI_CLOCK_DIVISOR
  30. # ifdef __AVR__
  31. # define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 4
  32. # else
  33. # define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 8
  34. # endif
  35. #endif
  36. /*
  37. The SPI mode to communicate with the FLASH.
  38. */
  39. #ifndef EXTERNAL_FLASH_SPI_MODE
  40. # define EXTERNAL_FLASH_SPI_MODE 0
  41. #endif
  42. /*
  43. Whether or not the SPI communication between the MCU and FLASH should be
  44. LSB-first.
  45. */
  46. #ifndef EXTERNAL_FLASH_SPI_LSBFIRST
  47. # define EXTERNAL_FLASH_SPI_LSBFIRST false
  48. #endif
  49. /*
  50. The Flash address size in bytes, as specified in datasheet.
  51. */
  52. #ifndef EXTERNAL_FLASH_ADDRESS_SIZE
  53. # define EXTERNAL_FLASH_ADDRESS_SIZE 3
  54. #endif
  55. /*
  56. The page size of the FLASH in bytes, as specified in the datasheet.
  57. */
  58. #ifndef EXTERNAL_FLASH_PAGE_SIZE
  59. # define EXTERNAL_FLASH_PAGE_SIZE 256
  60. #endif
  61. /*
  62. The sector size of the FLASH in bytes, as specified in the datasheet.
  63. */
  64. #ifndef EXTERNAL_FLASH_SECTOR_SIZE
  65. # define EXTERNAL_FLASH_SECTOR_SIZE (4 * 1024)
  66. #endif
  67. /*
  68. The block size of the FLASH in bytes, as specified in the datasheet.
  69. */
  70. #ifndef EXTERNAL_FLASH_BLOCK_SIZE
  71. # define EXTERNAL_FLASH_BLOCK_SIZE (64 * 1024)
  72. #endif
  73. /*
  74. The total size of the FLASH in bytes, as specified in the datasheet.
  75. */
  76. #ifndef EXTERNAL_FLASH_SIZE
  77. # define EXTERNAL_FLASH_SIZE (512 * 1024)
  78. #endif
  79. /*
  80. The block count of the FLASH, calculated by total FLASH size and block size.
  81. */
  82. #define EXTERNAL_FLASH_BLOCK_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_BLOCK_SIZE))
  83. /*
  84. The sector count of the FLASH, calculated by total FLASH size and sector size.
  85. */
  86. #define EXTERNAL_FLASH_SECTOR_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_SECTOR_SIZE))
  87. /*
  88. The page count of the FLASH, calculated by total FLASH size and page size.
  89. */
  90. #define EXTERNAL_FLASH_PAGE_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_PAGE_SIZE))
  91. typedef int16_t flash_status_t;
  92. #define FLASH_STATUS_SUCCESS (0)
  93. #define FLASH_STATUS_ERROR (-1)
  94. #define FLASH_STATUS_TIMEOUT (-2)
  95. #define FLASH_STATUS_BAD_ADDRESS (-3)
  96. #ifdef __cplusplus
  97. extern "C" {
  98. #endif
  99. #include <stdint.h>
  100. void flash_init(void);
  101. flash_status_t flash_erase_chip(void);
  102. flash_status_t flash_erase_block(uint32_t addr);
  103. flash_status_t flash_erase_sector(uint32_t addr);
  104. flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len);
  105. flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len);
  106. #ifdef __cplusplus
  107. }
  108. #endif