本例程跑了下SD/MMC栈用FatFS。这里将建立一个文件系统写一个文件到卡上。
本例程得用I/01Xplained Pro扩展板到EXT1上边。
在这里串口初始化是用了另一种格式。结构体
const usart_serial_options_t usart_serial_options = {
.baudrate = CONF_TEST_BAUDRATE,
.charlength = CONF_TEST_CHARLENGTH,
.paritytype = CONF_TEST_PARITY,
.stopbits = CONF_TEST_STOPBITS,
};
而USART管脚定义在:
sam4n_xplained _pro.h中
#define CONSOLE_UART UART0
#define CONSOLE_UART_ID ID_UART0
/** UART0 pins (UTXD0 and URXD0) definitions, PA9,10. */
#define PINS_UART0 (PIO_PA9A_URXD0 | PIO_PA10A_UTXD0)
#define PINS_UART0_FLAGS (IOPORT_MODE_MUX_A)
#define PINS_UART0_PORT IOPORT_PIOA
#define PINS_UART0_MASK (PIO_PA9A_URXD0 | PIO_PA10A_UTXD0)
#define PINS_UART0_PIO PIOA
#define PINS_UART0_ID ID_PIOA
#define PINS_UART0_TYPE PIO_PERIPH_A
#define PINS_UART0_ATTR PIO_DEFAULT
而初始化MMC在这里:
void sd_mmc_init(void)
{
//! Enable the PMC clock for the card detect pins
#if (defined SD_MMC_0_CD_GPIO) && (!defined SAM4L)
# include "pmc.h"
# define SD_MMC_ENABLE_CD_PIN(slot, unused) \
pmc_enable_periph_clk(SD_MMC_##slot##_CD_PIO_ID);
MREPEAT(SD_MMC_MEM_CNT, SD_MMC_ENABLE_CD_PIN, ~)
# undef SD_MMC_ENABLE_CD_PIN
#endif
//! Enable the PMC clock for the card write protection pins
#if (defined SD_MMC_0_WP_GPIO) && (!defined SAM4L)
# include "pmc.h"
# define SD_MMC_ENABLE_WP_PIN(slot, unused) \
pmc_enable_periph_clk(SD_MMC_##slot##_WP_PIO_ID);
MREPEAT(SD_MMC_MEM_CNT, SD_MMC_ENABLE_WP_PIN, ~)
# undef SD_MMC_ENABLE_WP_PIN
#endif
for (uint8_t slot = 0; slot < SD_MMC_MEM_CNT; slot++) {
sd_mmc_cards[slot].state = SD_MMC_CARD_STATE_NO_CARD;
}
sd_mmc_slot_sel = 0xFF; // No slot configurated
driver_init();
}
最后在主程序完成一系列的动作:
printf("Please plug an SD, MMC or SDIO card in slot.\n\r");
/* Wait card present and ready */
do {
status = sd_mmc_test_unit_ready(0);
if (CTRL_FAIL == status) {
printf("Card install FAIL\n\r");
printf("Please unplug and re-plug the card.\n\r");
while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
}
}
} while (CTRL_GOOD != status);
printf("Mount disk (f_mount)...\r\n");
memset(&fs, 0, sizeof(FATFS));
res = f_mount(LUN_ID_SD_MMC_0_MEM, &fs);
if (FR_INVALID_DRIVE == res) {
printf("[FAIL] res %d\r\n", res);
goto main_end_of_test;
}
printf("[OK]\r\n");
printf("Create a file (f_open)...\r\n");
test_file_name[0] = LUN_ID_SD_MMC_0_MEM + '0';
res = f_open(&file_object,
(char const *)test_file_name,
FA_CREATE_ALWAYS | FA_WRITE);
if (res != FR_OK) {
printf("[FAIL] res %d\r\n", res);
goto main_end_of_test;
}
printf("[OK]\r\n");
printf("Write to test file (f_puts)...\r\n");
if (0 == f_puts("Test SD/MMC stack\n", &file_object)) {
f_close(&file_object);
printf("[FAIL]\r\n");
goto main_end_of_test;
}
printf("[OK]\r\n");
f_close(&file_object);
printf("Test is successful.\n\r");
main_end_of_test:
printf("Please unplug the card.\n\r");
while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
}
最后的运行结果:
|