little_flash_define.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef _LITTLE_FLASH_DEFINE_H_
  2. #define _LITTLE_FLASH_DEFINE_H_
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "little_flash_config.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #define LF_SW_VERSION "0.0.1"
  12. struct little_flash;
  13. typedef struct little_flash little_flash_t;
  14. #ifndef LF_PRINTF
  15. #define LF_PRINTF printf
  16. #endif
  17. #ifndef LF_INFO
  18. #define LF_INFO(...) LF_PRINTF(__VA_ARGS__)
  19. #endif
  20. #ifndef LF_ERROR
  21. #define LF_ERROR(...) LF_PRINTF(__VA_ARGS__)
  22. #endif
  23. /* assert for developer. */
  24. #ifdef LF_DEBUG_MODE
  25. #ifndef LF_DEBUG
  26. #define LF_DEBUG(...) LF_PRINTF(__VA_ARGS__)
  27. #endif
  28. #define LF_ASSERT(EXPR) \
  29. if (!(EXPR)) \
  30. { \
  31. LF_PRINTF("(%s) has assert failed at %s.", #EXPR, __FUNCTION__); \
  32. while (1); \
  33. }
  34. #else
  35. #ifndef LF_DEBUG
  36. #define LF_DEBUG(...)
  37. #endif
  38. #define LF_ASSERT(EXPR)
  39. #endif
  40. #ifndef LF_NULL
  41. #define LF_NULL NULL
  42. #endif
  43. #define LF_ENABLE 1
  44. #define LF_DISABLE 0
  45. /**
  46. * status register bits
  47. */
  48. enum {
  49. LF_STATUS_REGISTER_BUSY = (1 << 0), /**< busing */
  50. LF_STATUS_REGISTER_WEL = (1 << 1), /**< write enable latch */
  51. };
  52. /**
  53. * @brief flash type
  54. *
  55. */
  56. #define LF_DRIVER_NOR_FLASH (0)
  57. #define LF_DRIVER_NAND_FLASH (1)
  58. typedef enum {
  59. LF_ERR_OK = 0,
  60. LF_ERR_NO_FLASH,
  61. LF_ERR_TIMEOUT,
  62. LF_ERR_TRANSFER,
  63. LF_ERR_ERASE,
  64. LF_ERR_WRITE,
  65. LF_ERR_READ,
  66. LF_ERR_BUSY,
  67. LF_ERR_BAD_ADDRESS,
  68. LF_ERR_NO_MEM,
  69. }lf_err_t;
  70. #ifndef LF_FLASH_NAME_LEN
  71. #define LF_FLASH_NAME_LEN 16
  72. #endif
  73. typedef struct {
  74. char name[LF_FLASH_NAME_LEN]; /**< flash chip name */
  75. uint8_t manufacturer_id; /**< MANUFACTURER ID */
  76. uint16_t device_id; /**< DEVICE ID (1byte or 2bytes) */
  77. union{ /**< driver type */
  78. struct {
  79. uint16_t type : 2; /**< flash type */
  80. uint16_t :14; /**< reserved */
  81. };
  82. uint16_t driver_type;
  83. };
  84. uint32_t capacity; /**< flash capacity (bytes) */
  85. uint8_t addr_bytes; /**< address bytes 2 3 4*/
  86. uint8_t erase_cmd; /**< erase granularity size block command */
  87. uint32_t erase_size; /**< erase granularity (bytes) */
  88. /* 以下基本可以代码自动推断无需指定 */
  89. uint32_t prog_size; /**< page size (bytes) */
  90. uint32_t read_size; /**< read size (bytes) */
  91. uint32_t retry_times; /**< retry times */
  92. } little_flash_chipinfo_t;
  93. typedef struct{
  94. lf_err_t (*transfer)(little_flash_t *lf,uint8_t *tx_buf, uint32_t tx_len, uint8_t *rx_buf, uint32_t rx_len);
  95. void* user_data;
  96. } little_flash_spi_t;
  97. struct little_flash{
  98. little_flash_chipinfo_t chip_info;
  99. little_flash_spi_t spi;
  100. /* lock */
  101. void (*lock)(little_flash_t *lf);
  102. /* unlock */
  103. void (*unlock)(little_flash_t *lf);
  104. /* wait 10us */
  105. void (*wait_10us)(void);
  106. /* wait ms */
  107. void (*wait_ms)(uint32_t ms);
  108. #ifdef LF_USE_HEAP
  109. /* malloc */
  110. void* (*malloc)(size_t size);
  111. /* free */
  112. void (*free)(void* ptr);
  113. #endif /* LF_USE_HEAP */
  114. /* user data */
  115. void* user_data;
  116. };
  117. #define LF_RETRY_TIMES (500000)
  118. #define LF_NANDFLASH_PAGE_ZISE (2048) /**< NAND flash page size (bytes) */
  119. #define LF_CMD_WRITE_STATUS_REGISTER (0x01)
  120. #define LF_CMD_WRITE_DISABLE (0x04)
  121. #define LF_CMD_READ_STATUS_REGISTER (0x05)
  122. #define LF_CMD_WRITE_ENABLE (0x06)
  123. #define LF_CMD_JEDEC_ID (0x9F)
  124. #define LF_CMD_ENABLE_RESET (0x66)
  125. #define LF_CMD_NORFLASH_RESET (0x99)
  126. #define LF_CMD_BLOCK_ERASE (0xD8)
  127. #define LF_CMD_NANDFLASH_RESET (0xFF)
  128. #define LF_CMD_PROG_DATA (0x02)
  129. #define LF_CMD_READ_DATA (0x03)
  130. #define LF_NANDFLASH_PAGE_DATA_READ (0x13)
  131. #define LF_NANDFLASH_PAGE_PROG_EXEC (0x10)
  132. /* NAND flash status registers */
  133. #define LF_NANDFLASH_STATUS_REGISTER1 (0xA0)
  134. #define LF_NANDFLASH_STATUS_REGISTER2 (0xB0)
  135. #define LF_NANDFLASH_STATUS_REGISTER3 (0xC0)
  136. #define LF_NANDFLASH_STATUS_REGISTER4 (0xD0)
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif /* _LITTLE_FLASH_DEFINE_H_ */