1
0

sfud_def.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * This file is part of the Serial Flash Universal Driver Library.
  3. *
  4. * Copyright (c) 2016, 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.0.6"
  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_MANUFACTURER_DEVICE_ID
  105. #define SFUD_CMD_MANUFACTURER_DEVICE_ID 0x90
  106. #endif
  107. #ifndef SFUD_CMD_JEDEC_ID
  108. #define SFUD_CMD_JEDEC_ID 0x9F
  109. #endif
  110. #ifndef SFUD_CMD_READ_UNIQUE_ID
  111. #define SFUD_CMD_READ_UNIQUE_ID 0x4B
  112. #endif
  113. #ifndef SFUD_CMD_READ_SFDP_REGISTER
  114. #define SFUD_CMD_READ_SFDP_REGISTER 0x5A
  115. #endif
  116. #ifndef SFUD_CMD_ENABLE_RESET
  117. #define SFUD_CMD_ENABLE_RESET 0x66
  118. #endif
  119. #ifndef SFUD_CMD_RESET
  120. #define SFUD_CMD_RESET 0x99
  121. #endif
  122. #ifndef SFUD_CMD_ENTER_4B_ADDRESS_MODE
  123. #define SFUD_CMD_ENTER_4B_ADDRESS_MODE 0xB7
  124. #endif
  125. #ifndef SFUD_CMD_EXIT_4B_ADDRESS_MODE
  126. #define SFUD_CMD_EXIT_4B_ADDRESS_MODE 0xE9
  127. #endif
  128. #ifndef SFUD_WRITE_MAX_PAGE_SIZE
  129. #define SFUD_WRITE_MAX_PAGE_SIZE 256
  130. #endif
  131. /* send dummy data for read data */
  132. #ifndef SFUD_DUMMY_DATA
  133. #define SFUD_DUMMY_DATA 0xFF
  134. #endif
  135. /* maximum number of erase type support on JESD216 (V1.0) */
  136. #define SFUD_SFDP_ERASE_TYPE_MAX_NUM 4
  137. /**
  138. * status register bits
  139. */
  140. enum {
  141. SFUD_STATUS_REGISTER_BUSY = (1 << 0), /**< busing */
  142. SFUD_STATUS_REGISTER_WEL = (1 << 1), /**< write enable latch */
  143. SFUD_STATUS_REGISTER_SRP = (1 << 7), /**< status register protect */
  144. };
  145. /**
  146. * error code
  147. */
  148. typedef enum {
  149. SFUD_SUCCESS = 0, /**< success */
  150. SFUD_ERR_NOT_FOUND = 1, /**< not found or not supported */
  151. SFUD_ERR_WRITE = 2, /**< write error */
  152. SFUD_ERR_READ = 3, /**< read error */
  153. SFUD_ERR_TIMEOUT = 4, /**< timeout error */
  154. SFUD_ERR_ADDR_OUT_OF_BOUND = 5, /**< address is out of flash bound */
  155. } sfud_err;
  156. /* SPI bus write read data function type */
  157. typedef sfud_err (*spi_write_read_func)(const uint8_t *write_buf, size_t write_size, uint8_t *read_buf, size_t read_size);
  158. #ifdef SFUD_USING_SFDP
  159. /**
  160. * the SFDP (Serial Flash Discoverable Parameters) parameter info which used on this library
  161. */
  162. typedef struct {
  163. bool available; /**< available when read SFDP OK */
  164. uint8_t major_rev; /**< SFDP Major Revision */
  165. uint8_t minor_rev; /**< SFDP Minor Revision */
  166. uint16_t write_gran; /**< write granularity (bytes) */
  167. uint8_t erase_4k; /**< 4 kilobyte erase is supported throughout the device */
  168. uint8_t erase_4k_cmd; /**< 4 Kilobyte erase command */
  169. bool sr_is_non_vola; /**< status register is supports non-volatile */
  170. uint8_t vola_sr_we_cmd; /**< volatile status register write enable command */
  171. bool addr_3_byte; /**< supports 3-Byte addressing */
  172. bool addr_4_byte; /**< supports 4-Byte addressing */
  173. uint32_t capacity; /**< flash capacity (bytes) */
  174. struct {
  175. uint32_t size; /**< erase sector size (bytes). 0x00: not available */
  176. uint8_t cmd; /**< erase command */
  177. } eraser[SFUD_SFDP_ERASE_TYPE_MAX_NUM]; /**< supported eraser types table */
  178. //TODO lots of fast read-related stuff (like modes supported and number of wait states/dummy cycles needed in each)
  179. } sfud_sfdp, *sfud_sfdp_t;
  180. #endif
  181. /**
  182. * SPI device
  183. */
  184. typedef struct __sfud_spi {
  185. /* SPI device name */
  186. char *name;
  187. /* SPI bus write read data function */
  188. sfud_err (*wr)(const struct __sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  189. size_t read_size);
  190. /* lock SPI bus */
  191. void (*lock)(const struct __sfud_spi *spi);
  192. /* unlock SPI bus */
  193. void (*unlock)(const struct __sfud_spi *spi);
  194. /* some user data */
  195. void *user_data;
  196. } sfud_spi, *sfud_spi_t;
  197. /**
  198. * serial flash device
  199. */
  200. typedef struct {
  201. char *name; /**< serial flash name */
  202. size_t index; /**< index of flash device information table @see flash_table */
  203. sfud_flash_chip chip; /**< flash chip information */
  204. sfud_spi spi; /**< SPI device */
  205. bool init_ok; /**< initialize OK flag */
  206. bool addr_in_4_byte; /**< flash is in 4-Byte addressing */
  207. struct {
  208. void (*delay)(void); /**< every retry's delay */
  209. size_t times; /**< default times for error retry */
  210. } retry;
  211. void *user_data; /**< some user data */
  212. #ifdef SFUD_USING_SFDP
  213. sfud_sfdp sfdp; /**< serial flash discoverable parameters by JEDEC standard */
  214. #endif
  215. } sfud_flash, *sfud_flash_t;
  216. #ifdef __cplusplus
  217. }
  218. #endif
  219. #endif /* _SFUD_DEF_H_ */