exmc_nandflash.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*!
  2. \file exmc_nandflash.h
  3. \brief the header file of nandflash(GD9FU1G8F2AMG) driver
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #ifndef EXMC_NANDFLASH_H
  33. #define EXMC_NANDFLASH_H
  34. #include "gd32f30x.h"
  35. /* NAND area definition */
  36. /* A16 = CLE high command area */
  37. #define EXMC_CMD_AREA (uint32_t)(1<<16)
  38. /* A17 = ALE high address area */
  39. #define EXMC_ADDR_AREA (uint32_t)(1<<17)
  40. /* data area */
  41. #define EXMC_DATA_AREA ((uint32_t)0x0000000)
  42. /* NAND memory command (GD9FU1G8F2AMG) */
  43. #define NAND_CMD_READ1_1ST ((uint8_t)0x00)
  44. #define NAND_CMD_READ1_2ND ((uint8_t)0x30)
  45. #define NAND_CMD_WRITE_1ST ((uint8_t)0x80)
  46. #define NAND_CMD_WRITE_2ND ((uint8_t)0x10)
  47. #define NAND_CMD_ERASE_1ST ((uint8_t)0x60)
  48. #define NAND_CMD_ERASE_2ND ((uint8_t)0xD0)
  49. #define NAND_CMD_READID ((uint8_t)0x90)
  50. #define NAND_CMD_STATUS ((uint8_t)0x70)
  51. #define NAND_CMD_LOCK_STATUS ((uint8_t)0x7A)
  52. #define NAND_CMD_RESET ((uint8_t)0xFF)
  53. /* NAND memory status */
  54. #define NAND_BUSY ((uint8_t)0x00)
  55. #define NAND_ERROR ((uint8_t)0x01)
  56. #define NAND_READY ((uint8_t)0x40)
  57. #define NAND_TIMEOUT_ERROR ((uint8_t)0x80)
  58. /* NAND memory parameters */
  59. /* NAND zone count */
  60. #define NAND_ZONE_COUNT ((uint16_t)0x0001)
  61. /* 1024 block per zone */
  62. #define NAND_ZONE_SIZE ((uint16_t)0x0400)
  63. /* 64 pages per block */
  64. #define NAND_BLOCK_SIZE ((uint16_t)0x0040)
  65. /* 2 * 1024 bytes per page */
  66. #define NAND_PAGE_SIZE ((uint16_t)0x0800)
  67. /* last 128 bytes as spare area */
  68. #define NAND_SPARE_AREA_SIZE ((uint16_t)0x0080)
  69. /* total page size = page size + spare are size */
  70. #define NAND_PAGE_TOTAL_SIZE (NAND_PAGE_SIZE + NAND_SPARE_AREA_SIZE)
  71. /* max read and write address */
  72. #define NAND_MAX_ADDRESS (((NAND_ZONE_COUNT*NAND_ZONE_SIZE)*NAND_BLOCK_SIZE)*NAND_PAGE_SIZE)
  73. /* block count */
  74. #define NAND_BLOCK_COUNT 1024
  75. /* NAND memory address computation */
  76. #define ADDR_1ST_CYCLE(ADDR) (uint8_t)((ADDR)& 0xFF)
  77. #define ADDR_2ND_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF00) >> 8)
  78. #define ADDR_3RD_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF0000) >> 16)
  79. #define ADDR_4TH_CYCLE(ADDR) (uint8_t)(((ADDR)& 0xFF000000) >> 24)
  80. /* define return value of functions */
  81. #define NAND_OK 0
  82. #define NAND_FAIL 1
  83. /* NAND id structure */
  84. typedef struct
  85. {
  86. uint8_t maker_id;
  87. uint8_t device_id;
  88. uint8_t third_id;
  89. uint8_t fourth_id;
  90. }nand_id_struct;
  91. typedef struct
  92. {
  93. uint16_t zone;
  94. uint16_t block;
  95. uint16_t page;
  96. uint16_t page_in_offset;
  97. } nand_address_struct;
  98. /* function declarations */
  99. /* nand flash peripheral initialize */
  100. void exmc_nandflash_init(void);
  101. /* read NAND flash ID */
  102. void nand_read_id(nand_id_struct* nand_id);
  103. /* write the spare area information for the specified pages addresses */
  104. uint8_t exmc_nand_writespare(uint8_t *pbuffer, nand_address_struct address, uint16_t bytecount);
  105. /* read the spare area information for the specified pages addresses */
  106. uint8_t exmc_nand_readspare(uint8_t *pbuffer, nand_address_struct address, uint16_t bytecount);
  107. /* reset nand flash */
  108. uint8_t nand_reset(void);
  109. /* write the main area information for the specified logic addresses */
  110. uint8_t nand_write(uint32_t memaddr, uint8_t *pwritebuf, uint16_t bytecount);
  111. /* read the main area information for the specified logic addresses */
  112. uint8_t nand_read(uint32_t mem_addr, uint8_t *preadbuf, uint16_t bytecount);
  113. /* format nand flash */
  114. uint8_t nand_format(void);
  115. /* fill the buffer with specified value */
  116. void fill_buffer_nand(uint8_t *pbuffer, uint16_t buffer_lenght, uint32_t value);
  117. #endif /* EXMC_NANDFLASH_H */