main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*!
  2. \file main.c
  3. \brief SD card read and write 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.1, 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 "gd32f303e_eval.h"
  34. #include "sdcard.h"
  35. #include <stdio.h>
  36. //#define DATA_PRINT /* uncomment the macro to print out the data */
  37. sd_card_info_struct sd_cardinfo; /* information of SD card */
  38. uint32_t buf_write[512]; /* store the data written to the card */
  39. uint32_t buf_read[512]; /* store the data read from the card */
  40. void nvic_config(void);
  41. sd_error_enum sd_io_init(void);
  42. void card_info_get(void);
  43. /*!
  44. \brief main function
  45. \param[in] none
  46. \param[out] none
  47. \retval none
  48. */
  49. int main(void)
  50. {
  51. sd_error_enum sd_error;
  52. uint16_t i = 5;
  53. #ifdef DATA_PRINT
  54. uint8_t *pdata;
  55. #endif /* DATA_PRINT */
  56. /* configure the NVIC, USART and LED */
  57. nvic_config();
  58. gd_eval_com_init(EVAL_COM1);
  59. gd_eval_led_init(LED2);
  60. gd_eval_led_init(LED3);
  61. gd_eval_led_init(LED4);
  62. gd_eval_led_init(LED5);
  63. /* turn off all the LEDs */
  64. gd_eval_led_off(LED2);
  65. gd_eval_led_off(LED3);
  66. gd_eval_led_off(LED4);
  67. gd_eval_led_off(LED5);
  68. /* initialize the card */
  69. do{
  70. sd_error = sd_io_init();
  71. }while((SD_OK != sd_error) && (--i));
  72. if(i){
  73. printf("\r\n Card init success!\r\n");
  74. }else{
  75. printf("\r\n Card init failed!\r\n");
  76. /* turn on LED2, LED4 and turn off LED3, LED5 */
  77. gd_eval_led_on(LED2);
  78. gd_eval_led_on(LED4);
  79. gd_eval_led_off(LED3);
  80. gd_eval_led_off(LED5);
  81. while (1){
  82. }
  83. }
  84. /* get the information of the card and print it out by USART */
  85. card_info_get();
  86. /* init the write buffer */
  87. for(i=0; i<512; i++){
  88. buf_write[i] = i;
  89. }
  90. printf("\r\n\r\n Card test:");
  91. /* single block operation test */
  92. sd_error = sd_block_write(buf_write, 100*512, 512);
  93. if(SD_OK != sd_error){
  94. printf("\r\n Block write fail!");
  95. /* turn on LED2, LED4 and turn off LED3, LED5 */
  96. gd_eval_led_on(LED2);
  97. gd_eval_led_on(LED4);
  98. gd_eval_led_off(LED3);
  99. gd_eval_led_off(LED5);
  100. while (1){
  101. }
  102. }else{
  103. printf("\r\n Block write success!");
  104. }
  105. sd_error = sd_block_read(buf_read, 100*512, 512);
  106. if(SD_OK != sd_error){
  107. printf("\r\n Block read fail!");
  108. /* turn on LED2, LED4 and turn off LED3, LED5 */
  109. gd_eval_led_on(LED2);
  110. gd_eval_led_on(LED4);
  111. gd_eval_led_off(LED3);
  112. gd_eval_led_off(LED5);
  113. while (1){
  114. }
  115. }else{
  116. printf("\r\n Block read success!");
  117. #ifdef DATA_PRINT
  118. pdata = (uint8_t *)buf_read;
  119. /* print data by USART */
  120. printf("\r\n");
  121. for(i = 0; i < 128; i++){
  122. printf(" %3d %3d %3d %3d ", *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
  123. pdata += 4;
  124. if(0 == (i + 1) % 4){
  125. printf("\r\n");
  126. }
  127. }
  128. #endif /* DATA_PRINT */
  129. }
  130. /* lock and unlock operation test */
  131. if(SD_CCC_LOCK_CARD & sd_cardinfo.card_csd.ccc){
  132. /* lock the card */
  133. sd_error = sd_lock_unlock(SD_LOCK);
  134. if(SD_OK != sd_error){
  135. printf("\r\n Lock failed!");
  136. /* turn on LED2, LED4 and turn off LED3, LED5 */
  137. gd_eval_led_on(LED2);
  138. gd_eval_led_on(LED4);
  139. gd_eval_led_off(LED3);
  140. gd_eval_led_off(LED5);
  141. while (1){
  142. }
  143. }else{
  144. printf("\r\n The card is locked!");
  145. }
  146. sd_error = sd_erase(100*512, 101*512);
  147. if(SD_OK != sd_error){
  148. printf("\r\n Erase failed!");
  149. }else{
  150. printf("\r\n Erase success!");
  151. }
  152. /* unlock the card */
  153. sd_error = sd_lock_unlock(SD_UNLOCK);
  154. if(SD_OK != sd_error){
  155. printf("\r\n Unlock failed!");
  156. /* turn on LED2, LED4 and turn off LED3, LED5 */
  157. gd_eval_led_on(LED2);
  158. gd_eval_led_on(LED4);
  159. gd_eval_led_off(LED3);
  160. gd_eval_led_off(LED5);
  161. while (1){
  162. }
  163. }else{
  164. printf("\r\n The card is unlocked!");
  165. }
  166. sd_error = sd_erase(100*512, 101*512);
  167. if(SD_OK != sd_error){
  168. printf("\r\n Erase failed!");
  169. }else{
  170. printf("\r\n Erase success!");
  171. }
  172. sd_error = sd_block_read(buf_read, 100*512, 512);
  173. if(SD_OK != sd_error){
  174. printf("\r\n Block read fail!");
  175. /* turn on LED2, LED4 and turn off LED3, LED5 */
  176. gd_eval_led_on(LED2);
  177. gd_eval_led_on(LED4);
  178. gd_eval_led_off(LED3);
  179. gd_eval_led_off(LED5);
  180. while (1){
  181. }
  182. }else{
  183. printf("\r\n Block read success!");
  184. #ifdef DATA_PRINT
  185. pdata = (uint8_t *)buf_read;
  186. /* print data by USART */
  187. printf("\r\n");
  188. for(i = 0; i < 128; i++){
  189. printf(" %3d %3d %3d %3d ", *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
  190. pdata += 4;
  191. if(0 == (i + 1) % 4){
  192. printf("\r\n");
  193. }
  194. }
  195. #endif /* DATA_PRINT */
  196. }
  197. }
  198. /* multiple blocks operation test */
  199. sd_error = sd_multiblocks_write(buf_write, 200*512, 512, 3);
  200. if(SD_OK != sd_error){
  201. printf("\r\n Multiple block write fail!");
  202. /* turn on LED2, LED4 and turn off LED3, LED5 */
  203. gd_eval_led_on(LED2);
  204. gd_eval_led_on(LED4);
  205. gd_eval_led_off(LED3);
  206. gd_eval_led_off(LED5);
  207. while (1){
  208. }
  209. }else{
  210. printf("\r\n Multiple block write success!");
  211. }
  212. sd_error = sd_multiblocks_read(buf_read, 200*512, 512, 3);
  213. if(SD_OK != sd_error){
  214. printf("\r\n Multiple block read fail!");
  215. /* turn on LED2, LED4 and turn off LED3, LED5 */
  216. gd_eval_led_on(LED2);
  217. gd_eval_led_on(LED4);
  218. gd_eval_led_off(LED3);
  219. gd_eval_led_off(LED5);
  220. while (1){
  221. }
  222. }else{
  223. printf("\r\n Multiple block read success!");
  224. #ifdef DATA_PRINT
  225. pdata = (uint8_t *)buf_read;
  226. /* print data by USART */
  227. printf("\r\n");
  228. for(i = 0; i < 512; i++){
  229. printf(" %3d %3d %3d %3d ", *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
  230. pdata += 4;
  231. if(0 == (i + 1) % 4){
  232. printf("\r\n");
  233. }
  234. }
  235. #endif /* DATA_PRINT */
  236. }
  237. /* turn on all the LEDs */
  238. gd_eval_led_on(LED2);
  239. gd_eval_led_on(LED3);
  240. gd_eval_led_on(LED4);
  241. gd_eval_led_on(LED5);
  242. while (1){
  243. }
  244. }
  245. /*!
  246. \brief configure the NVIC
  247. \param[in] none
  248. \param[out] none
  249. \retval none
  250. */
  251. void nvic_config(void)
  252. {
  253. nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  254. nvic_irq_enable(SDIO_IRQn, 0, 0);
  255. }
  256. /*!
  257. \brief initialize the card, get the card information, set the bus mode and transfer mode
  258. \param[in] none
  259. \param[out] none
  260. \retval sd_error_enum
  261. */
  262. sd_error_enum sd_io_init(void)
  263. {
  264. sd_error_enum status = SD_OK;
  265. uint32_t cardstate = 0;
  266. status = sd_init();
  267. if(SD_OK == status){
  268. status = sd_card_information_get(&sd_cardinfo);
  269. }
  270. if(SD_OK == status){
  271. status = sd_card_select_deselect(sd_cardinfo.card_rca);
  272. }
  273. status = sd_cardstatus_get(&cardstate);
  274. if(cardstate & 0x02000000){
  275. printf("\r\n the card is locked!");
  276. while (1){
  277. }
  278. }
  279. if ((SD_OK == status) && (!(cardstate & 0x02000000)))
  280. {
  281. /* set bus mode */
  282. status = sd_bus_mode_config(SDIO_BUSMODE_4BIT);
  283. // status = sd_bus_mode_config( SDIO_BUSMODE_1BIT );
  284. }
  285. if (SD_OK == status)
  286. {
  287. /* set data transfer mode */
  288. // status = sd_transfer_mode_config( SD_DMA_MODE );
  289. status = sd_transfer_mode_config( SD_POLLING_MODE );
  290. }
  291. return status;
  292. }
  293. /*!
  294. \brief get the card information and print it out by USRAT
  295. \param[in] none
  296. \param[out] none
  297. \retval none
  298. */
  299. void card_info_get(void)
  300. {
  301. uint8_t sd_spec, sd_spec3, sd_spec4, sd_security;
  302. uint32_t block_count, block_size;
  303. uint16_t temp_ccc;
  304. printf("\r\n Card information:");
  305. sd_spec = (sd_scr[1] & 0x0F000000) >> 24;
  306. sd_spec3 = (sd_scr[1] & 0x00008000) >> 15;
  307. sd_spec4 = (sd_scr[1] & 0x00000400) >> 10;
  308. if(2 == sd_spec)
  309. {
  310. if(1 == sd_spec3)
  311. {
  312. if(1 == sd_spec4)
  313. {
  314. printf("\r\n## Card version 4.xx ##");
  315. }
  316. else
  317. {
  318. printf("\r\n## Card version 3.0x ##");
  319. }
  320. }
  321. else
  322. {
  323. printf("\r\n## Card version 2.00 ##");
  324. }
  325. }
  326. else if(1 == sd_spec)
  327. {
  328. printf("\r\n## Card version 1.10 ##");
  329. }
  330. else if(0 == sd_spec)
  331. {
  332. printf("\r\n## Card version 1.0x ##");
  333. }
  334. sd_security = (sd_scr[1] & 0x00700000) >> 20;
  335. if(2 == sd_security)
  336. {
  337. printf("\r\n## SDSC card ##");
  338. }
  339. else if(3 == sd_security)
  340. {
  341. printf("\r\n## SDHC card ##");
  342. }
  343. else if(4 == sd_security)
  344. {
  345. printf("\r\n## SDXC card ##");
  346. }
  347. block_count = (sd_cardinfo.card_csd.c_size + 1)*1024;
  348. block_size = 512;
  349. printf("\r\n## Device size is %dKB ##", sd_card_capacity_get());
  350. printf("\r\n## Block size is %dB ##", block_size);
  351. printf("\r\n## Block count is %d ##", block_count);
  352. if(sd_cardinfo.card_csd.read_bl_partial){
  353. printf("\r\n## Partial blocks for read allowed ##" );
  354. }
  355. if(sd_cardinfo.card_csd.write_bl_partial){
  356. printf("\r\n## Partial blocks for write allowed ##" );
  357. }
  358. temp_ccc = sd_cardinfo.card_csd.ccc;
  359. printf("\r\n## CardCommandClasses is: %x ##", temp_ccc);
  360. if((SD_CCC_BLOCK_READ & temp_ccc) && (SD_CCC_BLOCK_WRITE & temp_ccc)){
  361. printf("\r\n## Block operation supported ##");
  362. }
  363. if(SD_CCC_ERASE & temp_ccc){
  364. printf("\r\n## Erase supported ##");
  365. }
  366. if(SD_CCC_WRITE_PROTECTION & temp_ccc){
  367. printf("\r\n## Write protection supported ##");
  368. }
  369. if(SD_CCC_LOCK_CARD & temp_ccc){
  370. printf("\r\n## Lock unlock supported ##");
  371. }
  372. if(SD_CCC_APPLICATION_SPECIFIC & temp_ccc){
  373. printf("\r\n## Application specific supported ##");
  374. }
  375. if(SD_CCC_IO_MODE & temp_ccc){
  376. printf("\r\n## I/O mode supported ##");
  377. }
  378. if(SD_CCC_SWITCH & temp_ccc){
  379. printf("\r\n## Switch function supported ##");
  380. }
  381. }
  382. /* retarget the C library printf function to the USART */
  383. int fputc(int ch, FILE *f)
  384. {
  385. usart_data_transmit(EVAL_COM1, (uint8_t)ch);
  386. while (RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  387. return ch;
  388. }