main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*!
  2. \file main.c
  3. \brief NAND test demo
  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. #include "gd32f30x.h"
  33. #include <stdio.h>
  34. #include "gd32f307c_eval.h"
  35. #include "exmc_nandflash.h"
  36. #define BUFFER_SIZE (0x100U)
  37. #define NAND_GD_MAKERID (0xC8U)
  38. #define NAND_GD_DEVICEID (0xF1U)
  39. nand_id_struct nand_id;
  40. uint8_t txbuffer[BUFFER_SIZE], rxbuffer[BUFFER_SIZE];
  41. __IO uint32_t writereadstatus = 0, status= 0;
  42. uint32_t j = 0;
  43. uint32_t k = 0;
  44. uint32_t writereadaddr ;
  45. uint16_t zone, block, page, pageoffset;
  46. /*!
  47. \brief main function
  48. \param[in] none
  49. \param[out] none
  50. \retval none
  51. */
  52. int main(void)
  53. {
  54. /* LED initialize */
  55. gd_eval_led_init(LED2);
  56. gd_eval_led_init(LED4);
  57. /* config the USART */
  58. gd_eval_com_init(EVAL_COM0);
  59. /* config the EXMC access mode */
  60. exmc_nandflash_init();
  61. /* read NAND ID */
  62. nand_read_id(&nand_id);
  63. printf("read NAND ID");
  64. /* print NAND ID */
  65. printf("\r\nNand flash ID:0x%X 0x%X 0x%X 0x%X\r\n",nand_id.maker_id,nand_id.device_id,
  66. nand_id.third_id,nand_id.fourth_id);
  67. if((NAND_GD_MAKERID == nand_id.maker_id) && (NAND_GD_DEVICEID == nand_id.device_id))
  68. {
  69. /* set the read and write the address */
  70. zone = 0;
  71. block = 10;
  72. page = 0;
  73. pageoffset = 1100;
  74. writereadaddr = ((zone * NAND_ZONE_SIZE + block) * NAND_BLOCK_SIZE + page)
  75. * NAND_PAGE_SIZE + pageoffset;
  76. /* whether address cross-border */
  77. if((writereadaddr + BUFFER_SIZE ) > NAND_MAX_ADDRESS)
  78. {
  79. printf("\r\naddress cross-border");
  80. /* failure, light on LED4 */
  81. gd_eval_led_on(LED4);
  82. while(1);
  83. }
  84. /* fill writebuffer with 0x00.. */
  85. fill_buffer_nand(txbuffer, BUFFER_SIZE , 0x00);
  86. /* write data to nand flash */
  87. status = nand_write(writereadaddr, txbuffer, BUFFER_SIZE);
  88. if(NAND_OK == status)
  89. {
  90. printf("\r\nwrite data successfully!");
  91. }
  92. else
  93. {
  94. printf("\r\nwrite data failure!");
  95. /* failure, light on LED4 */
  96. gd_eval_led_on(LED4);
  97. while(1);
  98. }
  99. /* read data from nand flash */
  100. status = nand_read(writereadaddr, rxbuffer, BUFFER_SIZE);
  101. if(NAND_OK == status)
  102. {
  103. printf("\r\nread data successfully!");
  104. }
  105. else
  106. {
  107. printf("\r\nread data failure!\n\r");
  108. /* failure, light on LED4 */
  109. gd_eval_led_on(LED4);
  110. while(1);
  111. }
  112. /* Read and write data comparison for equality */
  113. writereadstatus = 0;
  114. for(j = 0; j < BUFFER_SIZE; j++)
  115. {
  116. if(txbuffer[j] != rxbuffer[j])
  117. {
  118. writereadstatus++;
  119. break;
  120. }
  121. }
  122. printf("\r\nthe result to access the nand flash:");
  123. if (writereadstatus == 0)
  124. {
  125. printf("\r\naccess NAND flash successfully!");
  126. gd_eval_led_on(LED2);
  127. }
  128. else
  129. {
  130. printf("\r\naccess NAND flash failure!");
  131. /* failure, light on LED4 */
  132. gd_eval_led_on(LED4);
  133. while(1);
  134. }
  135. printf("\r\nprintf data to be read:\r\n");
  136. for(k = 0; k < BUFFER_SIZE; k ++)
  137. {
  138. printf("0x%02X ",rxbuffer[k]);
  139. }
  140. }
  141. else
  142. {
  143. printf("\n\rread NAND ID failure!\n\r");
  144. /* failure, light on LED4 */
  145. gd_eval_led_on(LED4);
  146. while(1);
  147. }
  148. while (1);
  149. }
  150. /* retarget the C library printf function to the USART */
  151. int fputc(int ch, FILE *f)
  152. {
  153. usart_data_transmit(EVAL_COM0, (uint8_t) ch);
  154. while (RESET == usart_flag_get(EVAL_COM0,USART_FLAG_TBE));
  155. return ch;
  156. }