sfud_def.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * This file is part of the Serial Flash Universal Driver Library.
  3. *
  4. * Copyright (c) 2016-2018, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: It is the macro definition head file for this library.
  26. * Created on: 2016-04-23
  27. */
  28. #ifndef _SFUD_DEF_H_
  29. #define _SFUD_DEF_H_
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34. #include <sfud_cfg.h>
  35. #include "sfud_flash_def.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /* debug print function. Must be implement by user. */
  40. #ifdef SFUD_DEBUG_MODE
  41. #ifndef SFUD_DEBUG
  42. #define SFUD_DEBUG(...) sfud_log_debug(__FILE__, __LINE__, __VA_ARGS__)
  43. #endif /* SFUD_DEBUG */
  44. #else
  45. #define SFUD_DEBUG(...)
  46. #endif /* SFUD_DEBUG_MODE */
  47. #ifndef SFUD_INFO
  48. #define SFUD_INFO(...) sfud_log_info(__VA_ARGS__)
  49. #endif
  50. /* assert for developer. */
  51. #ifdef SFUD_DEBUG_MODE
  52. #define SFUD_ASSERT(EXPR) \
  53. if (!(EXPR)) \
  54. { \
  55. SFUD_DEBUG("(%s) has assert failed at %s.", #EXPR, __FUNCTION__); \
  56. while (1); \
  57. }
  58. #else
  59. #define SFUD_ASSERT(EXPR)
  60. #endif
  61. /**
  62. * retry process
  63. *
  64. * @param delay delay function for every retry. NULL will not delay for every retry.
  65. * @param retry retry counts
  66. * @param result SFUD_ERR_TIMEOUT: retry timeout
  67. */
  68. #define SFUD_RETRY_PROCESS(delay, retry, result) \
  69. void (*__delay_temp)(void) = (void (*)(void))delay; \
  70. if (retry == 0) {result = SFUD_ERR_TIMEOUT;break;} \
  71. else {if (__delay_temp) {__delay_temp();} retry --;}
  72. /* software version number */
  73. #define SFUD_SW_VERSION "1.1.0"
  74. /*
  75. * all defined supported command
  76. */
  77. #ifndef SFUD_CMD_WRITE_ENABLE
  78. #define SFUD_CMD_WRITE_ENABLE 0x06
  79. #endif
  80. #ifndef SFUD_CMD_WRITE_DISABLE
  81. #define SFUD_CMD_WRITE_DISABLE 0x04
  82. #endif
  83. #ifndef SFUD_CMD_READ_STATUS_REGISTER
  84. #define SFUD_CMD_READ_STATUS_REGISTER 0x05
  85. #endif
  86. #ifndef SFUD_VOLATILE_SR_WRITE_ENABLE
  87. #define SFUD_VOLATILE_SR_WRITE_ENABLE 0x50
  88. #endif
  89. #ifndef SFUD_CMD_WRITE_STATUS_REGISTER
  90. #define SFUD_CMD_WRITE_STATUS_REGISTER 0x01
  91. #endif
  92. #ifndef SFUD_CMD_PAGE_PROGRAM
  93. #define SFUD_CMD_PAGE_PROGRAM 0x02
  94. #endif
  95. #ifndef SFUD_CMD_AAI_WORD_PROGRAM
  96. #define SFUD_CMD_AAI_WORD_PROGRAM 0xAD
  97. #endif
  98. #ifndef SFUD_CMD_ERASE_CHIP
  99. #define SFUD_CMD_ERASE_CHIP 0xC7
  100. #endif
  101. #ifndef SFUD_CMD_READ_DATA
  102. #define SFUD_CMD_READ_DATA 0x03
  103. #endif
  104. #ifndef SFUD_CMD_DUAL_OUTPUT_READ_DATA
  105. #define SFUD_CMD_DUAL_OUTPUT_READ_DATA 0x3B
  106. #endif
  107. #ifndef SFUD_CMD_DUAL_IO_READ_DATA
  108. #define SFUD_CMD_DUAL_IO_READ_DATA 0xBB
  109. #endif
  110. #ifndef SFUD_CMD_QUAD_IO_READ_DATA
  111. #define SFUD_CMD_QUAD_IO_READ_DATA 0xEB
  112. #endif
  113. #ifndef SFUD_CMD_QUAD_OUTPUT_READ_DATA
  114. #define SFUD_CMD_QUAD_OUTPUT_READ_DATA 0x6B
  115. #endif
  116. #ifndef SFUD_CMD_MANUFACTURER_DEVICE_ID
  117. #define SFUD_CMD_MANUFACTURER_DEVICE_ID 0x90
  118. #endif
  119. #ifndef SFUD_CMD_JEDEC_ID
  120. #define SFUD_CMD_JEDEC_ID 0x9F
  121. #endif
  122. #ifndef SFUD_CMD_READ_UNIQUE_ID
  123. #define SFUD_CMD_READ_UNIQUE_ID 0x4B
  124. #endif
  125. #ifndef SFUD_CMD_READ_SFDP_REGISTER
  126. #define SFUD_CMD_READ_SFDP_REGISTER 0x5A
  127. #endif
  128. #ifndef SFUD_CMD_ENABLE_RESET
  129. #define SFUD_CMD_ENABLE_RESET 0x66
  130. #endif
  131. #ifndef SFUD_CMD_RESET
  132. #define SFUD_CMD_RESET 0x99
  133. #endif
  134. #ifndef SFUD_CMD_ENTER_4B_ADDRESS_MODE
  135. #define SFUD_CMD_ENTER_4B_ADDRESS_MODE 0xB7
  136. #endif
  137. #ifndef SFUD_CMD_EXIT_4B_ADDRESS_MODE
  138. #define SFUD_CMD_EXIT_4B_ADDRESS_MODE 0xE9
  139. #endif
  140. #ifndef SFUD_WRITE_MAX_PAGE_SIZE
  141. #define SFUD_WRITE_MAX_PAGE_SIZE 256
  142. #endif
  143. /* send dummy data for read data */
  144. #ifndef SFUD_DUMMY_DATA
  145. #define SFUD_DUMMY_DATA 0xFF
  146. #endif
  147. /* maximum number of erase type support on JESD216 (V1.0) */
  148. #define SFUD_SFDP_ERASE_TYPE_MAX_NUM 4
  149. /**
  150. * status register bits
  151. */
  152. enum {
  153. SFUD_STATUS_REGISTER_BUSY = (1 << 0), /**< busing */
  154. SFUD_STATUS_REGISTER_WEL = (1 << 1), /**< write enable latch */
  155. SFUD_STATUS_REGISTER_SRP = (1 << 7), /**< status register protect */
  156. };
  157. /**
  158. * error code
  159. */
  160. typedef enum {
  161. SFUD_SUCCESS = 0, /**< success */
  162. SFUD_ERR_NOT_FOUND = 1, /**< not found or not supported */
  163. SFUD_ERR_WRITE = 2, /**< write error */
  164. SFUD_ERR_READ = 3, /**< read error */
  165. SFUD_ERR_TIMEOUT = 4, /**< timeout error */
  166. SFUD_ERR_ADDR_OUT_OF_BOUND = 5, /**< address is out of flash bound */
  167. } sfud_err;
  168. #ifdef SFUD_USING_QSPI
  169. /**
  170. * QSPI flash read cmd format
  171. */
  172. typedef struct {
  173. uint8_t instruction;
  174. uint8_t instruction_lines;
  175. uint8_t address_size;
  176. uint8_t address_lines;
  177. uint8_t alternate_bytes_lines;
  178. uint8_t dummy_cycles;
  179. uint8_t data_lines;
  180. } sfud_qspi_read_cmd_format;
  181. #endif /* SFUD_USING_QSPI */
  182. /* SPI bus write read data function type */
  183. typedef sfud_err (*spi_write_read_func)(const uint8_t *write_buf, size_t write_size, uint8_t *read_buf, size_t read_size);
  184. #ifdef SFUD_USING_SFDP
  185. /**
  186. * the SFDP (Serial Flash Discoverable Parameters) parameter info which used on this library
  187. */
  188. typedef struct {
  189. bool available; /**< available when read SFDP OK */
  190. uint8_t major_rev; /**< SFDP Major Revision */
  191. uint8_t minor_rev; /**< SFDP Minor Revision */
  192. uint16_t write_gran; /**< write granularity (bytes) */
  193. uint8_t erase_4k; /**< 4 kilobyte erase is supported throughout the device */
  194. uint8_t erase_4k_cmd; /**< 4 Kilobyte erase command */
  195. bool sr_is_non_vola; /**< status register is supports non-volatile */
  196. uint8_t vola_sr_we_cmd; /**< volatile status register write enable command */
  197. bool addr_3_byte; /**< supports 3-Byte addressing */
  198. bool addr_4_byte; /**< supports 4-Byte addressing */
  199. uint32_t capacity; /**< flash capacity (bytes) */
  200. struct {
  201. uint32_t size; /**< erase sector size (bytes). 0x00: not available */
  202. uint8_t cmd; /**< erase command */
  203. } eraser[SFUD_SFDP_ERASE_TYPE_MAX_NUM]; /**< supported eraser types table */
  204. //TODO lots of fast read-related stuff (like modes supported and number of wait states/dummy cycles needed in each)
  205. } sfud_sfdp, *sfud_sfdp_t;
  206. #endif
  207. /**
  208. * SPI device
  209. */
  210. typedef struct __sfud_spi {
  211. /* SPI device name */
  212. char *name;
  213. /* SPI bus write read data function */
  214. sfud_err (*wr)(const struct __sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  215. size_t read_size);
  216. #ifdef SFUD_USING_QSPI
  217. /* QSPI fast read function */
  218. sfud_err (*qspi_read)(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  219. uint8_t *read_buf, size_t read_size);
  220. #endif
  221. /* lock SPI bus */
  222. void (*lock)(const struct __sfud_spi *spi);
  223. /* unlock SPI bus */
  224. void (*unlock)(const struct __sfud_spi *spi);
  225. /* some user data */
  226. void *user_data;
  227. } sfud_spi, *sfud_spi_t;
  228. /**
  229. * serial flash device
  230. */
  231. typedef struct {
  232. char *name; /**< serial flash name */
  233. size_t index; /**< index of flash device information table @see flash_table */
  234. sfud_flash_chip chip; /**< flash chip information */
  235. sfud_spi spi; /**< SPI device */
  236. bool init_ok; /**< initialize OK flag */
  237. bool addr_in_4_byte; /**< flash is in 4-Byte addressing */
  238. struct {
  239. void (*delay)(void); /**< every retry's delay */
  240. size_t times; /**< default times for error retry */
  241. } retry;
  242. void *user_data; /**< some user data */
  243. #ifdef SFUD_USING_QSPI
  244. sfud_qspi_read_cmd_format read_cmd_format; /**< fast read cmd format */
  245. #endif
  246. #ifdef SFUD_USING_SFDP
  247. sfud_sfdp sfdp; /**< serial flash discoverable parameters by JEDEC standard */
  248. #endif
  249. } sfud_flash, *sfud_flash_t;
  250. #ifdef __cplusplus
  251. }
  252. #endif
  253. #endif /* _SFUD_DEF_H_ */