sfud_def.h 8.8 KB

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