[Atmel] 跑一下SAM4N例程(二十):Fatfs

[复制链接]
1398|0
 楼主| ddllxxrr 发表于 2015-4-7 21:41 | 显示全部楼层 |阅读模式
本例程跑了下SD/MMC栈用FatFS。这里将建立一个文件系统写一个文件到卡上。

本例程得用I/01Xplained Pro扩展板到EXT1上边。

在这里串口初始化是用了另一种格式。结构体


  1.         const usart_serial_options_t usart_serial_options = {
  2.                 .baudrate   = CONF_TEST_BAUDRATE,
  3.                 .charlength = CONF_TEST_CHARLENGTH,
  4.                 .paritytype = CONF_TEST_PARITY,
  5.                 .stopbits   = CONF_TEST_STOPBITS,
  6.         };


而USART管脚定义在:

sam4n_xplained _pro.h中


  1. #define CONSOLE_UART              UART0
  2. #define CONSOLE_UART_ID        ID_UART0

  3. /** UART0 pins (UTXD0 and URXD0) definitions, PA9,10. */
  4. #define PINS_UART0        (PIO_PA9A_URXD0 | PIO_PA10A_UTXD0)
  5. #define PINS_UART0_FLAGS  (IOPORT_MODE_MUX_A)

  6. #define PINS_UART0_PORT   IOPORT_PIOA
  7. #define PINS_UART0_MASK   (PIO_PA9A_URXD0 | PIO_PA10A_UTXD0)
  8. #define PINS_UART0_PIO    PIOA
  9. #define PINS_UART0_ID     ID_PIOA
  10. #define PINS_UART0_TYPE   PIO_PERIPH_A
  11. #define PINS_UART0_ATTR   PIO_DEFAULT
而初始化MMC在这里:

  1. void sd_mmc_init(void)
  2. {
  3.         //! Enable the PMC clock for the card detect pins
  4. #if (defined SD_MMC_0_CD_GPIO) && (!defined SAM4L)
  5. # include "pmc.h"
  6. # define SD_MMC_ENABLE_CD_PIN(slot, unused) \
  7.         pmc_enable_periph_clk(SD_MMC_##slot##_CD_PIO_ID);
  8.         MREPEAT(SD_MMC_MEM_CNT, SD_MMC_ENABLE_CD_PIN, ~)
  9. # undef SD_MMC_ENABLE_CD_PIN
  10. #endif
  11.         //! Enable the PMC clock for the card write protection pins
  12. #if (defined SD_MMC_0_WP_GPIO) && (!defined SAM4L)
  13. # include "pmc.h"
  14. # define SD_MMC_ENABLE_WP_PIN(slot, unused) \
  15.         pmc_enable_periph_clk(SD_MMC_##slot##_WP_PIO_ID);
  16.         MREPEAT(SD_MMC_MEM_CNT, SD_MMC_ENABLE_WP_PIN, ~)
  17. # undef SD_MMC_ENABLE_WP_PIN
  18. #endif
  19.         for (uint8_t slot = 0; slot < SD_MMC_MEM_CNT; slot++) {
  20.                 sd_mmc_cards[slot].state = SD_MMC_CARD_STATE_NO_CARD;
  21.         }
  22.         sd_mmc_slot_sel = 0xFF; // No slot configurated
  23.         driver_init();
  24. }



最后在主程序完成一系列的动作:


  1.                 printf("Please plug an SD, MMC or SDIO card in slot.\n\r");

  2.                 /* Wait card present and ready */
  3.                 do {
  4.                         status = sd_mmc_test_unit_ready(0);
  5.                         if (CTRL_FAIL == status) {
  6.                                 printf("Card install FAIL\n\r");
  7.                                 printf("Please unplug and re-plug the card.\n\r");
  8.                                 while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
  9.                                 }
  10.                         }
  11.                 } while (CTRL_GOOD != status);

  12.                 printf("Mount disk (f_mount)...\r\n");
  13.                 memset(&fs, 0, sizeof(FATFS));
  14.                 res = f_mount(LUN_ID_SD_MMC_0_MEM, &fs);
  15.                 if (FR_INVALID_DRIVE == res) {
  16.                         printf("[FAIL] res %d\r\n", res);
  17.                         goto main_end_of_test;
  18.                 }
  19.                 printf("[OK]\r\n");

  20.                 printf("Create a file (f_open)...\r\n");
  21.                 test_file_name[0] = LUN_ID_SD_MMC_0_MEM + '0';
  22.                 res = f_open(&file_object,
  23.                                 (char const *)test_file_name,
  24.                                 FA_CREATE_ALWAYS | FA_WRITE);
  25.                 if (res != FR_OK) {
  26.                         printf("[FAIL] res %d\r\n", res);
  27.                         goto main_end_of_test;
  28.                 }
  29.                 printf("[OK]\r\n");

  30.                 printf("Write to test file (f_puts)...\r\n");
  31.                 if (0 == f_puts("Test SD/MMC stack\n", &file_object)) {
  32.                         f_close(&file_object);
  33.                         printf("[FAIL]\r\n");
  34.                         goto main_end_of_test;
  35.                 }
  36.                 printf("[OK]\r\n");
  37.                 f_close(&file_object);
  38.                 printf("Test is successful.\n\r");

  39. main_end_of_test:
  40.                 printf("Please unplug the card.\n\r");
  41.                 while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
  42.                 }



最后的运行结果:


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部