view sd.c @ 49:ace431a0d0f5

Add SDIO code poached from STM. Use FatFS to read from SD card. LFN doesn't work reliably so it's disabled for now.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 03 Apr 2013 23:34:20 +1030
parents
children d7207a9d3c3b
line wrap: on
line source

/*
 * SD card code
 *
 * Copyright (c) 2013
 *      Daniel O'Connor <darius@dons.net.au>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "ff.h"
#include "stm32f10x.h"
#include "sd.h"
#include "stm32_eval_sdio_sd.h"

#define FAT_YEAR(d)	((((d) & 0xfe00) >> 9) + 1980)
#define FAT_MONTH(d)	(((d) & 0x01e0) >> 5)
#define FAT_DAY(d)	(((d) & 0x001f) >> 0)
#define FAT_HOUR(t)	(((t) & 0xf800) >> 11)
#define FAT_MIN(t)	(((t) & 0x07e0) >> 5)
#define FAT_SEC(t)	(((t) & 0x001f) << 1)

static FATFS fsh;

static void sd_domount(void);

void
sd_init(void) {
    sd_domount();
}

void
sd_domount(void) {
    SD_Error err;
    FRESULT fserr;

    if ((err = SD_Init()) != SD_OK) {
	printf("Failed init: %d\n", err);
	return;
    }
    
    if ((fserr = f_mount(0, &fsh)) != FR_OK) {
	printf("Unable to mount FS\n");
	return;
    }
}

void
sd_cmd(int argc, char **argv) {
    SD_Error err;

    if (argc < 1) {
	printf("No command specified\n");
	return;
    }
    
    if (!strcmp(argv[0], "info")) {
	SD_CardInfo cardinfo;
	if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) {
	    printf("Get card info failed: %d\r\b", err);
	} else {
	    printf("Mfg ID %" PRIu8 ", OEM %" PRIu16 ", Prod1 %" PRIu32 ", Prod2 %" PRIu8 ", Rev %" PRIu8 ", SN %" PRIu32 ", Mfg %" PRIu16 "\n",
		   cardinfo.SD_cid.ManufacturerID, cardinfo.SD_cid.OEM_AppliID,
		   cardinfo.SD_cid.ProdName1, cardinfo.SD_cid.ProdName2,
		   cardinfo.SD_cid.ProdRev, cardinfo.SD_cid.ProdSN,
		   cardinfo.SD_cid.ManufactDate);
	    printf("Capacity %" PRIu32 ", blocksize %" PRIu32 "\n", cardinfo.CardCapacity, cardinfo.CardBlockSize);
	}
    } else if (!strcmp(argv[0], "mount")) {
	sd_domount();
    } else if (!strcmp(argv[0], "read")) {
	uint32_t addr;
	uint8_t *buf;
	uint32_t i;
	
	if (argc != 2) {
	    printf("Block to read not specified\n");
	    return;
	}
	addr = atoi(argv[1]);

	if ((buf = malloc(SD_BLOCK_SIZE)) == NULL) {
	    printf("Unable to allocate buffer\n");
	    return;
	}
	
	/* If passed values not modulo 512 it hangs */
	if ((err = SD_ReadBlock(buf, addr * SD_BLOCK_SIZE, SD_BLOCK_SIZE)) != SD_OK) {
	    printf("Read block returned %d\n", err);
	    goto read_exit;
	}
	
#ifdef SD_DMA_MODE
	if ((err = SD_WaitReadOperation()) != SD_OK) {
	    printf("Wait returned %d\n", err);
	    goto read_exit;
	}
#endif
	while(SD_GetStatus() != SD_TRANSFER_OK)
	    ;

	for (i = 0; i < SD_BLOCK_SIZE; i += 16) {
	    printf("0x%04lx: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i,
		   buf[i + 0], buf[i + 1], buf[i + 2], buf[i + 3], buf[i + 4], buf[i + 5], buf[i + 6], buf[i + 7],
		   buf[i + 8], buf[i + 9], buf[i + 10], buf[i + 11], buf[i + 12], buf[i + 13], buf[i + 14], buf[i + 15]);
	}
	
      read_exit:
	free(buf);
    } else if (!strcmp(argv[0], "erase")) {
	uint32_t addr;
	
	if (argc != 2) {
	    printf("Block to erase not specified\n");
	    return;
	}
	addr = atoi(argv[1]);
	
	if ((err = SD_Erase(addr * SD_BLOCK_SIZE, (addr + 1) * SD_BLOCK_SIZE)) != SD_OK) {
	    printf("Erase block returned %d\n", err);
	    goto read_exit;
	}
	
#ifdef SD_DMA_MODE
	if ((err = SD_WaitReadOperation()) != SD_OK) {
	    printf("Wait returned %d\n", err);
	    goto read_exit;
	}
#endif
	while(SD_GetStatus() != SD_TRANSFER_OK)
	    ;
	printf("Erased OK\n");
    } else if (!strcmp(argv[0], "ls")) {
	FRESULT fserr;
	DIR d;
	FILINFO fno;
	char *fn;
	char label[12];
	DWORD sn;
	
	if ((fserr = f_getlabel("", label, &sn)) != FR_OK) {
	    printf("Unable to read label\n");
	} else {
	    printf("Label: %s Serial: %d\n", label, sn);
	}
	
	if ((fserr = f_opendir(&d, argc == 1 ? "\\" : argv[1])) != FR_OK) {
	    printf("Unable to opendir: %d\n", fserr);
	    return;
	}
	
	for (;;) {
	    fserr = f_readdir(&d, &fno);
	    if (fserr != FR_OK) {
		printf("Readdir failed: %d\n", fserr);
		break;
	    }
	    if (fno.fname[0] == '\0')
		break;
#if _USE_LFN
            fn = *fno.lfname ? fno.lfname : fno.fname;
#else
            fn = fno.fname;
#endif
	    printf("%-12s %5u %d/%d/%d %02d:%02d:%02d %s%s%s%s%s\n", fn, fno.fsize,
		   FAT_YEAR(fno.fdate), FAT_MONTH(fno.fdate), FAT_DAY(fno.fdate),
		   FAT_HOUR(fno.ftime), FAT_MIN(fno.ftime), FAT_SEC(fno.ftime),
		   fno.fattrib & AM_DIR ? "D" : "", fno.fattrib & AM_RDO ? "R" : "",
		   fno.fattrib & AM_HID ? "H" : "", fno.fattrib & AM_SYS ? "S" : "", 
		   fno.fattrib & AM_ARC ? "A" : "");
	}
    } else if (!strcmp(argv[0], "cat")) {
	FRESULT fserr;
	FIL f;
	char buf[128], *tmp;
	
	if (argc != 2) {
	    printf("No file given\n");
	    return;
	}
	
	if ((fserr = f_open(&f, argv[1], FA_READ)) != FR_OK) {
	    printf("Failed to open file: %d\n", fserr);
	    return;
	}
	
	while ((tmp = f_gets(buf, sizeof(buf), &f)) != NULL) {
	    puts(tmp);
	}
    } else {
	printf("Unknown command\n");
    }
}

void
SD_LowLevel_DeInit(void) {
    GPIO_InitTypeDef  GPIO_InitStructure;
  
    /*!< Disable SDIO Clock */
    SDIO_ClockCmd(DISABLE);
  
    /*!< Set Power State to OFF */
    SDIO_SetPowerState(SDIO_PowerState_OFF);

    /*!< DeInitializes the SDIO peripheral */
    SDIO_DeInit();
  
    /*!< Disable the SDIO AHB Clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, DISABLE);

    /*!< Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /*!< Configure PD.02 CMD line */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void
SD_LowLevel_Init(void) {
    GPIO_InitTypeDef  GPIO_InitStructure;

    /*!< GPIOC and GPIOD Periph clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

    /*!< Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /*!< Configure PD.02 CMD line */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

#ifdef SD_HAVE_DETECT
    RCC_APB2PeriphClockCmd(SD_DETECT_GPIO_CLK, ENABLE);

    /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */
    GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure);
#endif
  
    /*!< Enable the SDIO AHB Clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, ENABLE);

#ifdef SD_DMA_MODE
    /*!< Enable the DMA2 Clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
#endif
}

void
SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize) {
    printf("SD_LowLevel_DMA_RxConfig unimplemented\n");
    abort();
}

void
SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize) {
    printf("SD_LowLevel_DMA_TxConfig unimplemented\n");
    abort();
}

uint32_t SD_DMAEndOfTransferStatus(void) {
  return (uint32_t)DMA_GetFlagStatus(DMA2_FLAG_TC4);
}