Atmel Start ASF4 SD card middleware -DISKIO:disk_initialize() error

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Prerequisites: I have successfully tested my SD card using SD / MMC Access routines and can read and write data, proving that there are no problems with hardware and other operations.

 

Then,I used Atmel Start to select the Middleware ASF4 routine provided by it-DISKIO to drive my SD card. When I call disk_initialize (0) to initialize my SD card, it does not work and returns incorrect data.

So I went into the function and tried to fix it, then I found the error in the for loop,

 

dstatus_t disk_initialize(uint8_t drv)
{
	int           i;
	ctrl_status_t mem_status;

	/* Check LUN ready (USB disk report CTRL_BUSY then CTRL_GOOD) */
	for (i = 0; i < 2; i++) {
		mem_status = sd_mmc_test_unit_ready(drv);
		if (CTRL_BUSY != mem_status) {
			break;
		}
	}
	if (mem_status != CTRL_GOOD) {
		return STA_NOINIT;
	}

	/* Check Write Protection Status */
	if (sd_mmc_is_write_protected(drv)) {
		return STA_PROTECT;
	}

	/* The memory should already be initialized */
	return 0;
}

All I know is that the "sd_mmc_test_unit_ready ()" function will call the  "sd_mmc_check (slot)"  function,

static ctrl_status_t sd_mmc_test_unit_ready(uint8_t slot)
{
	switch (sd_mmc_check(slot)) {
	case SD_MMC_OK:
		if (sd_mmc_get_type(slot) & (CARD_TYPE_SD | CARD_TYPE_MMC)) {
			return CTRL_GOOD;
		} else // It is not a memory card
			return CTRL_NO_PRESENT;
	case SD_MMC_INIT_ONGOING:
		return CTRL_BUSY;

	case SD_MMC_ERR_NO_CARD:
		return CTRL_NO_PRESENT;
	default:
		return CTRL_FAIL;
	}
}

This function-"sd_mmc_check (slot)" is used to actually initialize the SD card,

but it should takes multiple times (> 2 times) to properly initialize the SD card, so we need to call it multiple times to complete the SD initialization,

so before finally completing the SD card initialization It will return incorrect data, which will also cause "sd_mmc_test_unit_ready ()" to return incorrect data and return to the for loop.

Here, “break” will let him jump out of the for loop and end.

In the end, "sd_mmc_check (slot)" was executed only once.

 

MY solution:

  1. increase the number of for loops-to 3 times,
  2. comment out the break statement.

 

Like this:

 

dstatus_t disk_initialize(uint8_t drv)
{
	int           i;
	ctrl_status_t mem_status;

	/* Check LUN ready (USB disk report CTRL_BUSY then CTRL_GOOD) */
	for (i = 0; i < 3; i++) {
		mem_status = sd_mmc_test_unit_ready(drv);
		if (CTRL_BUSY != mem_status) {
			//break;
		}
	}
	if (mem_status != CTRL_GOOD) {
		return STA_NOINIT;
	}

	/* Check Write Protection Status */
	if (sd_mmc_is_write_protected(drv)) {
		return STA_PROTECT;
	}

	/* The memory should already be initialized */
	return 0;
}

 

Then, the "disk_initialize ()" function works normally, and subsequent SD card read and write operations are normal.

 

This is my Atmel Start configuration

 

DISKIO set

 

 

Here is the entire program of my main function

 

#include <atmel_start.h>
#include "diskio.h"
#include "sd_mmc.h"

static uint8_t sd_mmc_block[512];

int main(void)
{
	/* Initializes MCU, drivers and middleware */
	atmel_start_init();

	dstatus_t status;
	dresult_t res;
	status = disk_initialize(0);

	if(0 == status){
		printf("SD Ready! \r\n");
		uint8_t w_buffer[11]="Sample Data";
		status = disk_status(0);
		switch(status){
			case 0:
				res = disk_write(0 /*drv*/, w_buffer, 1/*Sector*/, 1/*count*/);
				if(RES_OK == res){
					res = disk_read(0 /*drv*/, sd_mmc_block, 1 /*Sector*/, 1/*count*/);
					printf("%s\r\n",sd_mmc_block );

					if(RES_OK == res)  return 0;
				}
				break;
			case STA_NODISK:
			case STA_NOINIT:
			case STA_PROTECT:
				printf("disk error\r\n");
				break;
		}
	}

	/* Replace with your application code */
	while (1) {
	}
}

This is the data returned by the serial port(Modified procedure)

 

data returned by the serial port

 

So, where is the problem:

  • There is a problem with the official Atmel Start ASF4 routine
  • Or my question

 

Thank you in advance for your answers.

I am a Chinese. The text above is translated using google. It may be difficult to understand, sorry.

Just code

Last Edited: Tue. Mar 3, 2020 - 03:18 AM