spi_nand_flash_demo.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_spi_nand_flash.h"
  17. #include "ql_log.h"
  18. #include "spi_nand_flash_demo.h"
  19. #include "ql_gpio.h"
  20. #define QL_SPI_NAND_LOG_LEVEL QL_LOG_LEVEL_INFO
  21. #define QL_SPI_NAND_LOG(msg, ...) QL_LOG(QL_SPI_NAND_LOG_LEVEL, "ql_SPI_NAND_DEMO", msg, ##__VA_ARGS__)
  22. #define QL_SPI_NAND_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_SPI_NAND_DEMO", msg, ##__VA_ARGS__)
  23. ql_task_t spi_nand_flash_demo_task = NULL;
  24. #define QL_CUR_SPI_PORT QL_SPI_PORT1
  25. #define QL_CUR_SPI_CS_PIN QL_CUR_SPI1_CS_PIN
  26. #define QL_CUR_SPI_CS_FUNC QL_CUR_SPI1_CS_FUNC
  27. #define QL_CUR_SPI_CLK_PIN QL_CUR_SPI1_CLK_PIN
  28. #define QL_CUR_SPI_CLK_FUNC QL_CUR_SPI1_CLK_FUNC
  29. #define QL_CUR_SPI_DO_PIN QL_CUR_SPI1_DO_PIN
  30. #define QL_CUR_SPI_DO_FUNC QL_CUR_SPI1_DO_FUNC
  31. #define QL_CUR_SPI_DI_PIN QL_CUR_SPI1_DI_PIN
  32. #define QL_CUR_SPI_DI_FUNC QL_CUR_SPI1_DI_FUNC
  33. void ql_spi_nand_flash_data_prifnt(unsigned char *data, int len)
  34. {
  35. int i = 0;
  36. int count_len = 256;
  37. int count = 0;
  38. int exit = 0;
  39. int write_len = 0;
  40. unsigned char *str_data = (unsigned char *)malloc(len*2+64);
  41. if (str_data == NULL)
  42. {
  43. QL_SPI_NAND_LOG("malloc err");
  44. return ;
  45. }
  46. QL_SPI_NAND_LOG("read len=%d", len);
  47. while ((exit == 0))
  48. {
  49. if (len - count > 256)
  50. {
  51. count_len = 256;
  52. }
  53. else
  54. {
  55. count_len = len - count;
  56. exit = 1;
  57. }
  58. memset(str_data, 0, len*2+64);
  59. for (i=count; i<count+count_len; i++)
  60. {
  61. //sprintf((char *)str_data,"%s%02x", str_data, data[i]);
  62. write_len = snprintf((char *)str_data + write_len,(len*2+64 - write_len), "%02x", data[i]);
  63. }
  64. QL_SPI_NAND_LOG("data[%d-%d]=%s", count, count+count_len, str_data);
  65. count += count_len;
  66. }
  67. free(str_data);
  68. }
  69. //extern ql_errcode_gpio ql_pin_set_func(uint8_t pin_num, uint8_t func_sel);
  70. static void ql_spi_nand_flash_demo_task_pthread(void *ctx)
  71. {
  72. QlOSStatus err = 0;
  73. ql_errcode_spi_nand_e ret;
  74. ql_errcode_gpio gpio_ret;
  75. int block = 50; //50的block改写了坏块标志就使用不了了?
  76. int page = 0;
  77. unsigned char *data = NULL;
  78. int data_len = QL_SPI_NAND_PAGE_SPARE_SIZE;
  79. unsigned int page_addr = block << 6 | page;
  80. unsigned short column_addr = 0;
  81. int i = 0;
  82. ql_spi_nand_config_s nand_config = {0};
  83. if (QL_CUR_SPI_CS_PIN == QUEC_PIN_NONE || QL_CUR_SPI_CS_PIN == QUEC_PIN_NONE || \
  84. QL_CUR_SPI_DO_PIN == QUEC_PIN_NONE || QL_CUR_SPI_DI_PIN == QUEC_PIN_NONE)
  85. {
  86. QL_SPI_NAND_LOG("pin err");
  87. goto QL_SPI_TASK_EXIT;
  88. }
  89. gpio_ret = ql_pin_set_func(QL_CUR_SPI_CS_PIN, QL_CUR_SPI_CS_FUNC);
  90. if (gpio_ret != QL_GPIO_SUCCESS)
  91. {
  92. QL_SPI_NAND_LOG("set pin err");
  93. goto QL_SPI_NAND_EXIT;
  94. }
  95. gpio_ret = ql_pin_set_func(QL_CUR_SPI_CLK_PIN, QL_CUR_SPI_CLK_FUNC);
  96. if (gpio_ret != QL_GPIO_SUCCESS)
  97. {
  98. QL_SPI_NAND_LOG("set pin err");
  99. goto QL_SPI_NAND_EXIT;
  100. }
  101. gpio_ret = ql_pin_set_func(QL_CUR_SPI_DO_PIN, QL_CUR_SPI_DO_FUNC);
  102. if (gpio_ret != QL_GPIO_SUCCESS)
  103. {
  104. QL_SPI_NAND_LOG("set pin err");
  105. goto QL_SPI_NAND_EXIT;
  106. }
  107. gpio_ret = ql_pin_set_func(QL_CUR_SPI_DI_PIN, QL_CUR_SPI_DI_FUNC);
  108. if (gpio_ret != QL_GPIO_SUCCESS)
  109. {
  110. QL_SPI_NAND_LOG("set pin err");
  111. goto QL_SPI_NAND_EXIT;
  112. }
  113. data = (unsigned char *)malloc(data_len);
  114. if (data == NULL)
  115. {
  116. QL_SPI_NAND_LOG("malloc err");
  117. goto QL_SPI_NAND_EXIT;
  118. }
  119. //If you use the default parameters, you can initialize it with ql_spi_nand_init
  120. //ret = ql_spi_nand_init(QL_CUR_SPI_PORT, QL_SPI_CLK_25MHZ);
  121. nand_config.port = QL_CUR_SPI_PORT;
  122. nand_config.spiclk = QL_SPI_CLK_25MHZ;
  123. nand_config.input_sel = QL_SPI_DI_1;
  124. nand_config.transmode = QL_SPI_DIRECT_POLLING;
  125. nand_config.cs = QL_SPI_CS0;
  126. ret = ql_spi_nand_init_ext(nand_config);
  127. if (ret != QL_SPI_FLASH_SUCCESS)
  128. {
  129. QL_SPI_NAND_LOG("init err");
  130. goto QL_SPI_NAND_EXIT;
  131. }
  132. unsigned char mid[3] = {0};
  133. ret = ql_spi_nand_read_devid(QL_CUR_SPI_PORT, mid);
  134. if (ret != QL_SPI_FLASH_SUCCESS)
  135. {
  136. QL_SPI_NAND_LOG("read devid err");
  137. goto QL_SPI_NAND_EXIT;
  138. }
  139. #if 1
  140. unsigned char status = 0;
  141. ret = ql_spi_nand_read_status(QL_CUR_SPI_PORT, QL_NAND_FLASH_STATUS_1, &status);
  142. if (ret != QL_SPI_FLASH_SUCCESS)
  143. {
  144. QL_SPI_NAND_LOG("read status err");
  145. goto QL_SPI_NAND_EXIT;
  146. }
  147. QL_SPI_NAND_LOG("sr1 = %x", status);
  148. ret = ql_spi_nand_read_status(QL_CUR_SPI_PORT, QL_NAND_FLASH_STATUS_2, &status);
  149. if (ret != QL_SPI_FLASH_SUCCESS)
  150. {
  151. QL_SPI_NAND_LOG("read status err");
  152. goto QL_SPI_NAND_EXIT;
  153. }
  154. QL_SPI_NAND_LOG("sr2 = %x", status);
  155. ret = ql_spi_nand_read_status(QL_CUR_SPI_PORT, QL_NAND_FLASH_STATUS_3, &status);
  156. if (ret != QL_SPI_FLASH_SUCCESS)
  157. {
  158. QL_SPI_NAND_LOG("read status err");
  159. goto QL_SPI_NAND_EXIT;
  160. }
  161. QL_SPI_NAND_LOG("sr3 = %x", status);
  162. ret = ql_spi_nand_read_status(QL_CUR_SPI_PORT, QL_NAND_FLASH_STATUS_4, &status);
  163. if (ret != QL_SPI_FLASH_SUCCESS)
  164. {
  165. QL_SPI_NAND_LOG("read status err");
  166. goto QL_SPI_NAND_EXIT;
  167. }
  168. QL_SPI_NAND_LOG("sr4 = %x", status);
  169. #endif
  170. ret = ql_spi_nand_erase_block(QL_CUR_SPI_PORT, page_addr);
  171. if (ret != QL_SPI_FLASH_SUCCESS)
  172. {
  173. QL_SPI_NAND_LOG("erase err");
  174. goto QL_SPI_NAND_EXIT;
  175. }
  176. #if 1
  177. memset(data, 0, data_len);
  178. for (i=0; i<data_len; i++)
  179. {
  180. data[i] = 'e';//i % 256;
  181. }
  182. ret = ql_spi_nand_write_page_spare(QL_CUR_SPI_PORT, page_addr, column_addr, data, data_len);
  183. //ret = ql_spi_nand_write_spare(QL_CUR_SPI_PORT, page_addr, data, 12);
  184. if (ret != QL_SPI_FLASH_SUCCESS)
  185. {
  186. QL_SPI_NAND_LOG("read data err");
  187. goto QL_SPI_NAND_EXIT;
  188. }
  189. memset(data, 0, data_len);
  190. ret = ql_spi_nand_read_page_spare(QL_CUR_SPI_PORT, page_addr, column_addr, data, data_len);
  191. if (ret != QL_SPI_FLASH_SUCCESS)
  192. {
  193. QL_SPI_NAND_LOG("read data err");
  194. goto QL_SPI_NAND_EXIT;
  195. }
  196. ql_spi_nand_flash_data_prifnt(data, data_len);
  197. #endif
  198. QL_SPI_NAND_EXIT:
  199. free(data);
  200. ql_spi_release(QL_CUR_SPI_PORT);
  201. QL_SPI_TASK_EXIT:
  202. QL_SPI_NAND_LOG("ql_rtos_task_delete");
  203. err = ql_rtos_task_delete(NULL);
  204. if(err != QL_OSI_SUCCESS)
  205. {
  206. QL_SPI_NAND_LOG("task deleted failed");
  207. }
  208. }
  209. QlOSStatus ql_spi_nand_flash_demo_init(void)
  210. {
  211. QlOSStatus err = QL_OSI_SUCCESS;
  212. //QL_SPI_NAND_LOG("enter ql_spi_nand_flash_demo_init");
  213. err = ql_rtos_task_create(&spi_nand_flash_demo_task, SPI_NAND_FLASH_DEMO_TASK_STACK_SIZE, SPI_NAND_FLASH_DEMO_TASK_PRIO, "ql_spi_nand", ql_spi_nand_flash_demo_task_pthread, NULL, SPI_NAND_FLASH_DEMO_TASK_EVENT_CNT);
  214. if(err != QL_OSI_SUCCESS)
  215. {
  216. QL_SPI_NAND_LOG("demo_task created failed");
  217. return err;
  218. }
  219. return err;
  220. }