You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.1 KiB
C

/**
* \file
*
* \brief SD/MMC card example with FatFs
*
* Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
*
* \asf_license_start
*
* \page License
*
* Subject to your compliance with these terms, you may use Microchip
* software and any derivatives exclusively with Microchip products.
* It is your responsibility to comply with third party license terms applicable
* to your use of third party software (including open source software) that
* may accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
* INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
* AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
* LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
* LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
* SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
* POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
* ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
* RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*
* \asf_license_stop
*
*/
/**
* \mainpage SD/MMC Card with FatFs Example
*
* \section Purpose
*
* This example shows how to implement the SD/MMC stack with the FatFS.
* It will mount the file system and write a file in the card.
*
* The example outputs the information through the standard output (stdio).
* To know the output used on the board, look in the conf_example.h file
* and connect a terminal to the correct port.
*
* While using Xplained Pro evaluation kits, please attach I/O1 Xplained Pro
* extension board to EXT1.
*
* \section Usage
*
* -# Build the program and download it into the board.
* -# On the computer, open and configure a terminal application.
* Refert to conf_example.h file.
* -# Start the application.
* -# In the terminal window, the following text should appear:
* \code
* -- SD/MMC Card Example on FatFs --
* -- Compiled: xxx xx xxxx xx:xx:xx --
* Please plug an SD, MMC card in slot.
* \endcode
*/
/*
* Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
*/
#include <asf.h>
#include "conf_example.h"
#include <string.h>
//! Structure for UART module connected to EDBG (used for unit test output)
struct usart_module cdc_uart_module;
/**
* \brief Initialize the USART for output
*
* Initializes the SERCOM USART used for sending the output to the
* computer via the EDBG CDC gateway.
*/
static void cdc_uart_init(void)
{
struct usart_config usart_conf;
/* Configure USART for unit test output */
usart_get_config_defaults(&usart_conf);
usart_conf.mux_setting = CONF_STDIO_MUX_SETTING;
usart_conf.pinmux_pad0 = CONF_STDIO_PINMUX_PAD0;
usart_conf.pinmux_pad1 = CONF_STDIO_PINMUX_PAD1;
usart_conf.pinmux_pad2 = CONF_STDIO_PINMUX_PAD2;
usart_conf.pinmux_pad3 = CONF_STDIO_PINMUX_PAD3;
usart_conf.baudrate = CONF_STDIO_BAUDRATE;
stdio_serial_init(&cdc_uart_module, CONF_STDIO_USART, &usart_conf);
usart_enable(&cdc_uart_module);
}
/**
* \brief Application entry point.
*
* \return Unused (ANSI-C compatibility).
*/
int main(void)
{
char test_file_name[] = "0:sd_mmc_test.txt";
Ctrl_status status;
FRESULT res;
FATFS fs;
FIL file_object;
system_init();
delay_init();
cdc_uart_init();
irq_initialize_vectors();
cpu_irq_enable();
/* Initialize SD MMC stack */
sd_mmc_init();
printf("\x0C\n\r-- SD/MMC Card Example on FatFs --\n\r");
printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
while (1) {
printf("Please plug an SD/MMC 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)) {
}
}
}