Example #1
0
Ctrl_status sd_mmc_test_unit_ready_0(void)
{
	ui_start_test();
	Ctrl_status status;
	while ((status = sd_mmc_test_unit_ready(0)) == CTRL_BUSY || status == CTRL_NO_PRESENT) ;
	ui_stop_test();
	return sd_mmc_test_unit_ready(0);
}
Example #2
0
/**
 * \brief Initialize SD/MMC storage.
 */
static void init_storage(void)
{
	FRESULT res;
	Ctrl_status status;

	/* Initialize SD/MMC stack. */
	sd_mmc_init();
	while (true) {
		printf("init_storage: Please plug an SD/MMC card in slot.\r\n");

		/* Wait card present and ready. */
		do {
			status = sd_mmc_test_unit_ready(0);
			if (CTRL_FAIL == status) {
				printf("init_storage: SD Card install Failed.\r\n");
				printf("init_storage: Please unplug and re-plug the card.\r\n");
				while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
				}
			}
		} while (CTRL_GOOD != status);

		printf("init_storage: Mount SD card...\r\n");
		memset(&fatfs, 0, sizeof(FATFS));
		res = f_mount(LUN_ID_SD_MMC_0_MEM, &fatfs);
		if (FR_INVALID_DRIVE == res) {
			printf("init_storage: SD card Mount failed. res %d\r\n", res);
			return;
		}

		printf("init_storage: SD card Mount OK.\r\n");
		add_state(STORAGE_READY);
		return;
	}
}
Example #3
0
//TODO set timeout
int fs_mount(void)
{
	Ctrl_status status;
	FRESULT res;
	/* 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");
			//TODO set timeout here
			while (CTRL_NO_PRESENT != sd_mmc_check(0));
		}
		//TODO set timeout here
	} while (CTRL_GOOD != status);
	
	/* Mount the file sytem */
	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);
		return res;
	}
	#define FS_MOUNTED
	printf("mounting [OK]\r\n");
	return res;
}
Example #4
0
/** @brief Initialize a disk.

    @param bVolNum  The volume number of the volume whose block device is being
                    initialized.
    @param mode     The open mode, indicating the type of access required.

    @return A negated ::REDSTATUS code indicating the operation result.

    @retval 0           Operation was successful.
    @retval -RED_EIO    A disk I/O error occurred.
    @retval -RED_EROFS  The device is read-only media and write access was
                        requested.
*/
static REDSTATUS DiskOpen(
    uint8_t         bVolNum,
    BDEVOPENMODE    mode)
{
    REDSTATUS       ret = 0;
    uint32_t        ulTries;
    Ctrl_status     cs;

    /*  Note: Assuming the volume number is the same as the SD card slot.  The
        ASF SD/MMC driver supports two SD slots.  This implementation will need
        to be modified if multiple volumes share a single SD card.
    */

    /*  The first time the disk is opened, the SD card can take a while to get
        ready, in which time sd_mmc_test_unit_ready() returns either CTRL_BUSY
        or CTRL_NO_PRESENT.  Try numerous times, waiting half a second after
        each failure.  Empirically, this has been observed to succeed on the
        second try, so trying 10x more than that provides a margin of error.
    */
    for(ulTries = 0U; ulTries < 20U; ulTries++) {
        cs = sd_mmc_test_unit_ready(bVolNum);
        if((cs != CTRL_NO_PRESENT) && (cs != CTRL_BUSY)) {
            break;
        }

        vTaskDelay(500U / portTICK_PERIOD_MS);
    }

    if(cs == CTRL_GOOD) {
#if REDCONF_READ_ONLY == 0
        if(mode != BDEV_O_RDONLY) {
            if(sd_mmc_wr_protect(bVolNum)) {
                ret = -RED_EROFS;
            }
        }

        if(ret == 0)
#endif
        {
            uint32_t ulSectorLast;

            (void)sd_mmc_read_capacity(bVolNum, &ulSectorLast);

            /*  The ASF SD/MMC driver only supports 512-byte sectors.
            */
            if(    (gaRedVolConf[bVolNum].ulSectorSize != 512U)
                    || (((uint64_t)ulSectorLast + 1U) < gaRedVolConf[bVolNum].ullSectorCount)) {
                ret = -RED_EINVAL;
            }
        }
    } else {
        ret = -RED_EIO;
    }

    return ret;
}
Example #5
0
/**
 * Try to connect to SD/MMC card
 */
bool media_sd_connect(void)
{
	Ctrl_status status;
	int i;
	for (i = 0; i < 5; i ++) {
		status = sd_mmc_test_unit_ready(0);
		if (CTRL_GOOD == status) {
			return true;
		}
		if (CTRL_NO_PRESENT == sd_mmc_check(0)) {
			return false;
		}
	}
	return false;
}
/**
 * \brief waits till sd card is ready
 *
 * This function will wait, indefinitely till SD card is ready for access.
 *  
 */
static void sd_mmc_ready() 
{
	Ctrl_status status = CTRL_FAIL;
	do {
		status = sd_mmc_test_unit_ready(0);
		if (CTRL_FAIL == status) {
			#ifdef __DEBUG_PRINT__
			printf("[SD Card install FAILED]\n\r");
			printf("Please unplug and re-plug the card!!!\n\r");
			#endif
			while (CTRL_NO_PRESENT != sd_mmc_check(0)) {}
		}
	} while (CTRL_GOOD != status);
	/* Turn on LED if SD card is ready  */
	port_pin_set_output_level(BOOT_LED, false);
	#ifdef __DEBUG_PRINT__
	printf("SD Card Detection successful...\r\n");
	#endif
}
Example #7
0
/** @brief Flush any caches beneath the file system.

    @param bVolNum  The volume number of the volume whose block device is being
                    flushed.

    @return A negated ::REDSTATUS code indicating the operation result.

    @retval 0   Operation was successful.
*/
static REDSTATUS DiskFlush(
    uint8_t     bVolNum)
{
    REDSTATUS   ret;
    Ctrl_status cs;

    /*  The ASF SD/MMC driver appears to write sectors synchronously, so it
        should be fine to do nothing and return success.  However, Atmel's
        implementation of the FatFs diskio.c file does the equivalent of the
        below when the disk is flushed.  Just in case this is important for some
        non-obvious reason, do the same.
    */
    cs = sd_mmc_test_unit_ready(bVolNum);
    if(cs == CTRL_GOOD) {
        ret = 0;
    } else {
        ret = -RED_EIO;
    }

    return ret;
}
Ctrl_status sd_mmc_test_unit_ready_1(void)
{
	return sd_mmc_test_unit_ready(1);
}
Ctrl_status sd_mmc_test_unit_ready_0(void)
{
	return sd_mmc_test_unit_ready(0);
}
Ctrl_status sd_mmc_read_capacity(uint8_t slot, uint32_t *nb_sector)
{
	// Return last sector address (-1)
	*nb_sector = (sd_mmc_get_capacity(slot) * 2) - 1;
	return sd_mmc_test_unit_ready(slot);
}
/**
 * \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;

	const usart_serial_options_t usart_serial_options = {
		.baudrate   = CONF_TEST_BAUDRATE,
		.charlength = CONF_TEST_CHARLENGTH,
		.paritytype = CONF_TEST_PARITY,
		.stopbits   = CONF_TEST_STOPBITS,
	};

	irq_initialize_vectors();
	cpu_irq_enable();

	sysclk_init();
	board_init();
	stdio_serial_init(CONF_TEST_USART, &usart_serial_options);

	/* Initialize SD MMC stack */
	sd_mmc_init();

	printf("\x0C\n\r-- SD/MMC/SDIO Card Example on FatFs --\n\r");
	printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
	while (1) {
		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 successfull.\n\r");

main_end_of_test:
		printf("Please unplug the card.\n\r");
		while (CTRL_NO_PRESENT != sd_mmc_check(0)) {
		}
	}
}
Example #12
0
void ZP_Init(void)
{
		#ifdef DEBUG
		printf("Setting up system clock...\n");
		#endif
	/* Setup SysTick Timer for 1 msec interrupts */
	if (SysTick_Config(sysclk_get_cpu_hz() / 1000))
while (1) {}
	
	#ifdef DEBUG
		printf("ZP Init:\n");
		printf("Setting up pins...\n");
	#endif
	
	
			
	#ifdef DEBUG
		printf("Setting up SD stack...\n");
	#endif
	
	/* Initialize SD MMC stack */
	sd_mmc_init();	

	uint8_t sd_check = 0;
	/* Wait card present and ready */
	do {
		#ifdef DEBUG
		if (sd_check == 1)
		{
			printf("Please plug an SD card in slot.\n");
			sd_check = 2;
		}		
		#endif
		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)) {
			}
		}
		if (sd_check == 0)
			sd_check = 1;
	} while (CTRL_GOOD != status);
	
	#ifdef DEBUG
		printf("SD Card Found\n");
		printf("Mounting disk...");
	#endif
	
	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);
	}
	
	#ifdef DEBUG
		printf("Disk mounted\n");
	#endif
	
	TCHAR root_directory[3] = "0:";
	root_directory[0] = '0' + LUN_ID_SD_MMC_0_MEM;
	
	
	file_name[0] = '0' + LUN_ID_SD_MMC_0_MEM;
	DIR dirs;
	res = f_opendir(&dirs, root_directory);
	
	if(f_opendir(&dir, "0:Scripts") == FR_NO_PATH)
	{
		res = f_mkdir("0:Scripts");
		printf("Made Scripts folder\n");
	}
	
	printf("res: %d\n", res);
	
	
	printf("Open Script\n");
	//f_open(&f_script, "0:script.bin", FA_OPEN_EXISTING | FA_READ);
	
}