1
0

sfud_def.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <stdint.h>
  32. #include <stdbool.h>
  33. #include <sfud_cfg.h>
  34. #include "sfud_flash_def.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /* debug print function. Must be implement by user. */
  39. #ifdef SFUD_DEBUG_MODE
  40. #ifndef SFUD_DEBUG
  41. #define SFUD_DEBUG(...) sfud_log_debug(__FILE__, __LINE__, __VA_ARGS__)
  42. #endif /* SFUD_DEBUG */
  43. #else
  44. #define SFUD_DEBUG(...)
  45. #endif /* SFUD_DEBUG_MODE */
  46. #ifndef SFUD_INFO
  47. #define SFUD_INFO(...) sfud_log_info(__VA_ARGS__)
  48. #endif
  49. /* assert for developer. */
  50. #ifdef SFUD_DEBUG_MODE
  51. #define SFUD_ASSERT(EXPR) \
  52. if (!(EXPR)) \
  53. { \
  54. SFUD_DEBUG("(%s) has assert failed at %s.", #EXPR, __FUNCTION__); \
  55. while (1); \
  56. }
  57. #else
  58. #define SFUD_ASSERT(EXPR)
  59. #endif
  60. /**
  61. * retry process
  62. *
  63. * @param delay delay function for every retry. NULL will not delay for every retry.
  64. * @param retry retry counts
  65. * @param result SFUD_ERR_TIMEOUT: retry timeout
  66. */
  67. #define SFUD_RETRY_PROCESS(delay, retry, result) \
  68. void (*__delay_temp)(void) = (void (*)(void))delay; \
  69. if (retry == 0) {result = SFUD_ERR_TIMEOUT;break;} \
  70. else {if (__delay_temp) {__delay_temp();} retry --;}
  71. /* software version number */
  72. #define SFUD_SW_VERSION "1.0.4"
  73. /*
  74. * all defined supported command
  75. */
  76. #ifndef SFUD_CMD_WRITE_ENABLE
  77. #define SFUD_CMD_WRITE_ENABLE 0x06
  78. #endif
  79. #ifndef SFUD_CMD_WRITE_DISABLE
  80. #define SFUD_CMD_WRITE_DISABLE 0x04
  81. #endif
  82. #ifndef SFUD_CMD_READ_STATUS_REGISTER
  83. #define SFUD_CMD_READ_STATUS_REGISTER 0x05
  84. #endif
  85. #ifndef SFUD_VOLATILE_SR_WRITE_ENABLE
  86. #define SFUD_VOLATILE_SR_WRITE_ENABLE 0x50
  87. #endif
  88. #ifndef SFUD_CMD_WRITE_STATUS_REGISTER
  89. #define SFUD_CMD_WRITE_STATUS_REGISTER 0x01
  90. #endif
  91. #ifndef SFUD_CMD_PAGE_PROGRAM
  92. #define SFUD_CMD_PAGE_PROGRAM 0x02
  93. #endif
  94. #ifndef SFUD_CMD_AAI_WORD_PROGRAM
  95. #define SFUD_CMD_AAI_WORD_PROGRAM 0xAD
  96. #endif
  97. #ifndef SFUD_CMD_ERASE_CHIP
  98. #define SFUD_CMD_ERASE_CHIP 0xC7
  99. #endif
  100. #ifndef SFUD_CMD_READ_DATA
  101. #define SFUD_CMD_READ_DATA 0x03
  102. #endif
  103. #ifndef SFUD_CMD_MANUFACTURER_DEVICE_ID
  104. #define SFUD_CMD_MANUFACTURER_DEVICE_ID 0x90
  105. #endif
  106. #ifndef SFUD_CMD_JEDEC_ID
  107. #define SFUD_CMD_JEDEC_ID 0x9F
  108. #endif
  109. #ifndef SFUD_CMD_READ_UNIQUE_ID
  110. #define SFUD_CMD_READ_UNIQUE_ID 0x4B
  111. #endif
  112. #ifndef SFUD_CMD_READ_SFDP_REGISTER
  113. #define SFUD_CMD_READ_SFDP_REGISTER 0x5A
  114. #endif
  115. #ifndef SFUD_CMD_ENABLE_RESET
  116. #define SFUD_CMD_ENABLE_RESET 0x66
  117. #endif
  118. #ifndef SFUD_CMD_RESET
  119. #define SFUD_CMD_RESET 0x99
  120. #endif
  121. #ifndef SFUD_CMD_ENTER_4B_ADDRESS_MODE
  122. #define SFUD_CMD_ENTER_4B_ADDRESS_MODE 0xB7
  123. #endif
  124. #ifndef SFUD_CMD_EXIT_4B_ADDRESS_MODE
  125. #define SFUD_CMD_EXIT_4B_ADDRESS_MODE 0xE9
  126. #endif
  127. #ifndef SFUD_WRITE_MAX_PAGE_SIZE
  128. #define SFUD_WRITE_MAX_PAGE_SIZE 256
  129. #endif
  130. /* send dummy data for read data */
  131. #ifndef SFUD_DUMMY_DATA
  132. #define SFUD_DUMMY_DATA 0xFF
  133. #endif
  134. /* maximum number of erase type support on JESD216 (V1.0) */
  135. #define SFUD_SFDP_ERASE_TYPE_MAX_NUM 4
  136. /**
  137. * status register bits
  138. */
  139. enum {
  140. SFUD_STATUS_REGISTER_BUSY = (1 << 0), /**< busing */
  141. SFUD_STATUS_REGISTER_WEL = (1 << 1), /**< write enable latch */
  142. SFUD_STATUS_REGISTER_SRP = (1 << 7), /**< status register protect */
  143. };
  144. /**
  145. * error code
  146. */
  147. typedef enum {
  148. SFUD_SUCCESS = 0, /**< success */
  149. SFUD_ERR_NOT_FOUND = 1, /**< not found or not supported */
  150. SFUD_ERR_WRITE = 2, /**< write error */
  151. SFUD_ERR_READ = 3, /**< read error */
  152. SFUD_ERR_TIMEOUT = 4, /**< timeout error */
  153. SFUD_ERR_ADDR_OUT_OF_BOUND = 5, /**< address is out of flash bound */
  154. } sfud_err;
  155. /* SPI bus write read data function type */
  156. typedef sfud_err (*spi_write_read_func)(const uint8_t *write_buf, size_t write_size, uint8_t *read_buf, size_t read_size);
  157. #ifdef SFUD_USING_SFDP
  158. /**
  159. * the SFDP (Serial Flash Discoverable Parameters) parameter info which used on this library
  160. */
  161. typedef struct {
  162. bool available; /**< available when read SFDP OK */
  163. uint8_t major_rev; /**< SFDP Major Revision */
  164. uint8_t minor_rev; /**< SFDP Minor Revision */
  165. uint16_t write_gran; /**< write granularity (bytes) */
  166. uint8_t erase_4k; /**< 4 kilobyte erase is supported throughout the device */
  167. uint8_t erase_4k_cmd; /**< 4 Kilobyte erase command */
  168. bool sr_is_non_vola; /**< status register is supports non-volatile */
  169. uint8_t vola_sr_we_cmd; /**< volatile status register write enable command */
  170. bool addr_3_byte; /**< supports 3-Byte addressing */
  171. bool addr_4_byte; /**< supports 4-Byte addressing */
  172. uint32_t capacity; /**< flash capacity (bytes) */
  173. struct {
  174. uint32_t size; /**< erase sector size (bytes). 0x00: not available */
  175. uint8_t cmd; /**< erase command */
  176. } eraser[SFUD_SFDP_ERASE_TYPE_MAX_NUM]; /**< supported eraser types table */
  177. //TODO lots of fast read-related stuff (like modes supported and number of wait states/dummy cycles needed in each)
  178. } sfud_sfdp, *sfud_sfdp_t;
  179. #endif
  180. /**
  181. * SPI device
  182. */
  183. typedef struct __sfud_spi {
  184. /* SPI device name */
  185. char *name;
  186. /* SPI bus write read data function */
  187. sfud_err (*wr)(const struct __sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  188. size_t read_size);
  189. /* lock SPI bus */
  190. void (*lock)(const struct __sfud_spi *spi);
  191. /* unlock SPI bus */
  192. void (*unlock)(const struct __sfud_spi *spi);
  193. /* some user data */
  194. void *user_data;
  195. } sfud_spi, *sfud_spi_t;
  196. /**
  197. * serial flash device
  198. */
  199. typedef struct {
  200. char *name; /**< serial flash name */
  201. size_t index; /**< index of flash device information table @see flash_table */
  202. sfud_flash_chip chip; /**< flash chip information */
  203. sfud_spi spi; /**< SPI device */
  204. bool init_ok; /**< initialize OK flag */
  205. bool addr_in_4_byte; /**< flash is in 4-Byte addressing */
  206. struct {
  207. void (*delay)(void); /**< every retry's delay */
  208. size_t times; /**< default times for error retry */
  209. } retry;
  210. void *user_data; /**< some user data */
  211. #ifdef SFUD_USING_SFDP
  212. sfud_sfdp sfdp; /**< serial flash discoverable parameters by JEDEC standard */
  213. #endif
  214. } sfud_flash, *sfud_flash_t;
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. #endif /* _SFUD_DEF_H_ */