main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. * @defgroup fatfs_example_main main.c
  42. * @{
  43. * @ingroup fatfs_example
  44. * @brief FATFS Example Application main file.
  45. *
  46. * This file contains the source code for a sample application using FAT filesystem and SD card library.
  47. *
  48. */
  49. #include "nrf.h"
  50. #include "bsp.h"
  51. #include "ff.h"
  52. #include "diskio_blkdev.h"
  53. #include "nrf_block_dev_sdc.h"
  54. #include "nrf_log.h"
  55. #include "nrf_log_ctrl.h"
  56. #include "nrf_log_default_backends.h"
  57. #define FILE_NAME "NORDIC.TXT"
  58. #define TEST_STRING "SD card example."
  59. #define SDC_SCK_PIN ARDUINO_13_PIN ///< SDC serial clock (SCK) pin.
  60. #define SDC_MOSI_PIN ARDUINO_11_PIN ///< SDC serial data in (DI) pin.
  61. #define SDC_MISO_PIN ARDUINO_12_PIN ///< SDC serial data out (DO) pin.
  62. #define SDC_CS_PIN ARDUINO_10_PIN ///< SDC chip select (CS) pin.
  63. /**
  64. * @brief SDC block device definition
  65. * */
  66. NRF_BLOCK_DEV_SDC_DEFINE(
  67. m_block_dev_sdc,
  68. NRF_BLOCK_DEV_SDC_CONFIG(
  69. SDC_SECTOR_SIZE,
  70. APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)
  71. ),
  72. NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
  73. );
  74. /**
  75. * @brief Function for demonstrating FAFTS usage.
  76. */
  77. static void fatfs_example()
  78. {
  79. static FATFS fs;
  80. static DIR dir;
  81. static FILINFO fno;
  82. static FIL file;
  83. uint32_t bytes_written;
  84. FRESULT ff_result;
  85. DSTATUS disk_state = STA_NOINIT;
  86. // Initialize FATFS disk I/O interface by providing the block device.
  87. static diskio_blkdev_t drives[] =
  88. {
  89. DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
  90. };
  91. diskio_blockdev_register(drives, ARRAY_SIZE(drives));
  92. NRF_LOG_INFO("Initializing disk 0 (SDC)...");
  93. for (uint32_t retries = 3; retries && disk_state; --retries)
  94. {
  95. disk_state = disk_initialize(0);
  96. }
  97. if (disk_state)
  98. {
  99. NRF_LOG_INFO("Disk initialization failed.");
  100. return;
  101. }
  102. uint32_t blocks_per_mb = (1024uL * 1024uL) / m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_size;
  103. uint32_t capacity = m_block_dev_sdc.block_dev.p_ops->geometry(&m_block_dev_sdc.block_dev)->blk_count / blocks_per_mb;
  104. NRF_LOG_INFO("Capacity: %d MB", capacity);
  105. NRF_LOG_INFO("Mounting volume...");
  106. ff_result = f_mount(&fs, "", 1);
  107. if (ff_result)
  108. {
  109. NRF_LOG_INFO("Mount failed.");
  110. return;
  111. }
  112. NRF_LOG_INFO("\r\n Listing directory: /");
  113. ff_result = f_opendir(&dir, "/");
  114. if (ff_result)
  115. {
  116. NRF_LOG_INFO("Directory listing failed!");
  117. return;
  118. }
  119. do
  120. {
  121. ff_result = f_readdir(&dir, &fno);
  122. if (ff_result != FR_OK)
  123. {
  124. NRF_LOG_INFO("Directory read failed.");
  125. return;
  126. }
  127. if (fno.fname[0])
  128. {
  129. if (fno.fattrib & AM_DIR)
  130. {
  131. NRF_LOG_RAW_INFO(" <DIR> %s",(uint32_t)fno.fname);
  132. }
  133. else
  134. {
  135. NRF_LOG_RAW_INFO("%9lu %s", fno.fsize, (uint32_t)fno.fname);
  136. }
  137. }
  138. }
  139. while (fno.fname[0]);
  140. NRF_LOG_RAW_INFO("");
  141. NRF_LOG_INFO("Writing to file " FILE_NAME "...");
  142. ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND);
  143. if (ff_result != FR_OK)
  144. {
  145. NRF_LOG_INFO("Unable to open or create file: " FILE_NAME ".");
  146. return;
  147. }
  148. ff_result = f_write(&file, TEST_STRING, sizeof(TEST_STRING) - 1, (UINT *) &bytes_written);
  149. if (ff_result != FR_OK)
  150. {
  151. NRF_LOG_INFO("Write failed\r\n.");
  152. }
  153. else
  154. {
  155. NRF_LOG_INFO("%d bytes written.", bytes_written);
  156. }
  157. (void) f_close(&file);
  158. return;
  159. }
  160. /**
  161. * @brief Function for main application entry.
  162. */
  163. int main(void)
  164. {
  165. bsp_board_init(BSP_INIT_LEDS);
  166. APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  167. NRF_LOG_DEFAULT_BACKENDS_INIT();
  168. NRF_LOG_INFO("FATFS example started.");
  169. fatfs_example();
  170. while (true)
  171. {
  172. __WFE();
  173. }
  174. }
  175. /** @} */