Exemplo n.º 1
0
/*
 * This function synchronously waits any pending block transfers
 * are finished. If your program needs to ensure a block has finished
 * transferring, call this function.
 *
 * Note that sd_read_block() and sd_write_block() already call this
 * function internally before attempting a new transfer, so there are
 * only two times when a user would need to use this function.
 *
 * 1) When the processor will be shutting down. All pending
 *    writes should be finished first.
 * 2) When the user needs the result of an sd_read_block() call
 *    right away.
 */
void sd_wait_notbusy(sd_context_t *sdc)
{
	/* Just twiddle our thumbs until the transfer's done */
	while ((DMA0CTL & DMAEN) != 0);

	/* Reset the DMA controller */
	DMACTL0 = 0;

	/* Ignore the checksum */
	sd_delay(4);

	/* Check for the busy flag (set on write block) */
	if (sdc->busyflag == 1) {
		while (spi_recv_byte() != 0xFF);
		sdc->busyflag = 0;
	}

	/* Deassert CS */
	spi_cs_deassert();

	/*
	 * Send some extra clocks so the card can resynchronize
	 * on next transfer
	 */
	sd_delay(2);
}
Exemplo n.º 2
0
static void bus_spi_stop(void)
{
    spi_cs_deassert();

    console_puts("CS DISABLED");
    console_newline();
}
Exemplo n.º 3
0
void sd_wait_notbusy (sd_context_t *sdc){
while ((DMA0CTL & DMAEN) != 0){;}			//Just twiddle our thumbs until the transfer’s done.
DMACTL0 = 0;								//Reset the DMA controller.
sd_delay(4);								// Ignore the checksum.
if (sdc->busyflag == 1){					//Check for the busy flag (set on a write block).
	while (spi_rcv_byte() != 0xFF);
	sdc->busyflag = 0;
}
spi_cs_deassert();		//Deassert CS.
//-------------------------Send some extra clocks so the card can resynchronize on the next transfer-------------------------
sd_delay(2);
}
Exemplo n.º 4
0
int sd_initialize()
{
	char i, j;
	sdc->busyflag = 0;
	for (i=0; i<4; i++)
		argument[i] = 0;
	/* Delay for at least 74 clock cycles. This means to actually
	 * *clock* out at least 74 clock cycles with no data present on
	 * the clock. In SPI mode, send at least 10 idle bytes (0xFF). */
	spi_cs_assert();
	sd_delay(100);
	spi_cs_deassert();
	sd_delay(2);
	/* Put the card in the idle state */
	if (sd_send_command(sdc, CMD0, CMD0_R, response, argument) == 0)
		return 0;
	/* Now wait until the card goes idle. Retry at most SD_IDLE_WAIT_MAX times */
	j = 0;
	do
	{
		j++;
		/* Flag the next command as an application-specific command */
		if (sd_send_command(sdc, CMD55, CMD55_R, response, argument) == 1)
		{
			/* Tell the card to send its OCR */
			sd_send_command(sdc, ACMD41, ACMD41_R, response, argument);
		}
		else
		{
			/* No response, bail early */
			j = SD_IDLE_WAIT_MAX;
		}
	}
	while ((response[0] & MSK_IDLE) == MSK_IDLE && j < SD_IDLE_WAIT_MAX);
	/* As long as we didn't hit the timeout, assume we're OK. */
	if (j >= SD_IDLE_WAIT_MAX)
		return 0;
	if (sd_send_command(sdc, CMD58, CMD58_R, response, argument) == 0)
		return 0;
	/* At a very minimum, we must allow 3.3V. */
	if ((response[2] & MSK_OCR_33) != MSK_OCR_33)
		return 0;
	/* Set the block length */
	if (sd_set_blocklen (sdc, SD_BLOCKSIZE) != 1)
		return 0;
	/* If we got this far, initialization was OK. */
	return 1;
}
Exemplo n.º 5
0
							//initialization was successful, 0 otherwise.
							//sd_context_t *sdc -- pointer to a data structure containing
							//information about the card. For now, the
							//timeouts MUST be specified in advance. This
							//function does not yet calculate them from the
							//card data.
int sd_initialize(sd_context_t *sdc){
	char i, j;						//SPI SD initialization sequence: CMD0->CMD55->ACMD41->CMD58.
	j = 0;							//Note there is no CMD2 or CMD3 in SPI mode. These instructions are devoted to addressing on the SD bus.
	sdc->busyflag = 0;				//SD memory card SD initialization sequence: CMD0->CMD55->ACMD41->CMD2->CMD3.
	
	for (i=0; i<4; i++){
	argument[i] = 0;
	}								//Delay for at least 74 clock cycles. This means to actually
	spi_cs_assert();				//*clock* out at least 74 clock cycles with no data present on
	sd_delay(100);					//the clock. In SPI mode, send at least 10 idle bytes (0xFF).
	spi_cs_deassert();
	sd_delay(2);
	if (sd_send_command(sdc, CMD0, CMD0_R, response, argument) == 0){	 //Put the card in the idle state
	return 0;
	}
//-------------------------------------Now wait until the card goes idle. Retry at most SD_IDLE_WAIT_MAX times.-------------------------------------- 
	do{
	j++;
									//Flag the next command as an application-specific command.
	if (sd_send_command(sdc, CMD55, CMD55_R, response, argument) == 1){
		sd_send_command(sdc, ACMD41, ACMD41_R, response, argument);		//Tell the card to send its OCR
	}
	else{							// No response, bail early.
		j = SD_IDLE_WAIT_MAX;
	}
	}
	while ((response[0] & MSK_IDLE) == MSK_IDLE && j < SD_IDLE_WAIT_MAX);	//As long as we didn’t hit the timeout, assume we’re OK.
	if (j >= SD_IDLE_WAIT_MAX){
	return 0;
	}
	if (sd_send_command(sdc, CMD58, CMD58_R, response, argument) == 0){
	return 0;
	}
	if ((response[2] & MSK_OCR_33) != MSK_OCR_33){		//At a very minimum, we must allow 3.3V.
	return 0;
	}
	if (sd_set_blocklen (sdc, SD_BLOCKSIZE) != 1){		//Set the block length
	return 0;
	}
	return 1;	//If we got this far, initialization was OK.
}
Exemplo n.º 6
0
int sd_send_command(sd_context_t *sdc,
		unsigned char cmd, unsigned char response_type,
		unsigned char *response, unsigned char *argument)
{
	int i;
	char response_length;
	unsigned char tmp;
	spi_cs_assert();
	/* All data is sent MSB first, and MSb first */
	/* Send the header/command */
	/* Format:
	cmd[7:6] : 01
	cmd[5:0] : command */
	spi_send_byte((cmd & 0x3F) | 0x40);
	for (i=3; i>=0; i--)
	{
		spi_send_byte(argument[i]);
	}
	/* This is the CRC. It only matters what we put here for the first
	command. Otherwise, the CRC is ignored for SPI mode unless we
	enable CRC checking. */
	spi_send_byte(0x95);
	response_length = 0;
	switch (response_type)
	{
	case R1:
	case R1B:
		response_length = 1;
		break;
	case R2:
		response_length = 2;
		break;
	case R3:
		response_length = 5;
		break;
	default:
		break;
	}
	/* Wait for a response. A response can be recognized by the
		start bit (a zero) */
	i=0;
	do
	{
		tmp = spi_rcv_byte();
		i++;
	}
	while (((tmp & 0x80) != 0) && i < SD_CMD_TIMEOUT);
	/* Just bail if we never got a response */
	if (i >= SD_CMD_TIMEOUT)
	{
		spi_cs_deassert();
		return 0;
	}
	for (i=response_length-1; i>=0; i--)
	{
		response[i] = tmp;
		/* This handles the trailing-byte requirement. */
		tmp = spi_rcv_byte();
	}
	/* If the response is a "busy" type (R1B), then there's some
	 * special handling that needs to be done. The card will
	 * output a continuous stream of zeros, so the end of the BUSY
	 * state is signaled by any nonzero response. The bus idles
	 * high.
	 */
	i=0;
	if (response_type == R1B)
	{
		do
		{
			i++;
			tmp = spi_rcv_byte();
		}
		/* This should never time out, unless SDI is grounded.
		 * Don't bother forcing a timeout condition here. */
		while (tmp != 0xFF);
		spi_send_byte(0xFF);
	}
	spi_cs_deassert();
	return 1;
}
Exemplo n.º 7
0
/*
 * This function initializes the SD card. It returns 1 if
 * initialization was successful, 0 otherwise.
 *
 * sd_context_t *sdc -- Pointer to a data structure containing
 *                      information about the card. For now, the
 *                      timeouts must be specified in advance. It
 *                      is not yet calculated from the card data.
 */
int sd_initialize(sd_context_t *sdc)
{
	u16 i;

	/*
	 * SPI SDv2 initialization sequence:
	 *   CMD0
	 *   CMD8
	 *   CMD55+ACMD41
	 *   CMD58
	 *     (There is no CMD2 or CMD3 in SPI mode. These
	 *      instructions are devoted to addressing on the SD bus)
	 */

	sdc->busyflag = 0;

	/*
	 * Delay for at least 74 clock cycles. This means to actually
	 * *clock* out at least 74 clock cycles with no data present on
	 * the clock. In SPI mode, send at least 10 idle bytes (0xFF)
	 */
	spi_cs_deassert();
	sd_delay(100);
	spi_cs_assert();
	sd_delay(2);

	/* Put the card in thee idle state */
	if (sd_send_command(sdc, CMD0, CMD0_R, response, NULL) == 0)
		return 0;

	sd_packarg(argument, 0x000001AA);
	if (sd_send_command(sdc, CMD8, CMD8_R, response, argument) == 0) {
		return 0;
	}

	if (response[0] & MSK_ILL_CMD) {
		/* Illegal command, try SDv1 init */
		sd_packarg(argument, 0x00000000);
	} else {
		/* Check if the voltage range is appropiate */
		if (response[3] == 0x01 && response[4] == 0xAA) {
			sd_packarg(argument, 0x40000000);
		} else {
			return 0;
		}
	}

	/* Now wait until the card goes idle. Retry at most SD_IDLE_WAIT_MAX */
	i = 0;
	do {
		i++;
		/* Flag the next command as an application-specific command */
		if (sd_send_command(sdc, CMD55, CMD55_R, response, NULL) == 1) {
			/* Tell the card to send its OCR */
			sd_send_command(sdc, ACMD41, ACMD41_R, response, argument);
		} else {
			/* No response, bail early */
			i = SD_IDLE_WAIT_MAX;
		}
	} while ((response[0] & MSK_IDLE) == MSK_IDLE && i < SD_IDLE_WAIT_MAX);

	/* As long as we didn't hit the timeout, assume we're OK */
	if (i >= SD_IDLE_WAIT_MAX)
		return 0;

	if (sd_send_command(sdc, CMD58, CMD58_R, response, NULL) == 0)
		return 0;

	/* At the very minimum, we must allow 3.3V */
	if ((response[2] & MSK_OCR_33) != MSK_OCR_33)
		return 0;

	/* Set the block length */
	if (sd_set_blocklen(sdc, SD_BLOCKSIZE) != 1)
		return 0;

	/* If we got this far, initialization was OK */
	return 1;
}