view fatfs_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

/*
 * Glue between FatFS and the ST SDIO 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 "stm32f10x.h"
#include "stm32_eval_sdio_sd.h"
#include "diskio.h"

DSTATUS
disk_initialize(BYTE pdrv) {
    SD_Error err;
    
    /* Only have 1 disk */
    if (pdrv != 0)
	return STA_NOINIT;
    
    if ((err = SD_Init()) != SD_OK) {
	printf("Failed init: %d\r\n", err);
	return STA_NOINIT;
    }

    return 0;
}

DSTATUS
disk_status(BYTE pdrv) {
    if (pdrv != 0)
	return STA_NOINIT;

    return 0;
}

DRESULT
disk_read(BYTE pdrv, BYTE *buff, DWORD sector, BYTE count) {
    SD_Error err;
    
    if (pdrv != 0)
	return STA_NOINIT;

    if (count == 1)
	err = SD_ReadBlock(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE);
    else
	err = SD_ReadMultiBlocks(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE, count);
    
    if (err != SD_OK) {
	printf("Read failed: %d\r\n", err);
	return STA_NOINIT;
    }
    
    return RES_OK;
}

DRESULT
disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, BYTE count) {
    if (pdrv != 0)
	return STA_NOINIT;

    return RES_WRPRT;
}

DRESULT
disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
    WORD *wd;
    DWORD *dwd;
    SD_CardInfo cardinfo;
    SD_Error err;
    
    wd = buff;
    dwd = buff;
    
    if (pdrv != 0)
	return STA_NOINIT;
    switch (cmd) {
	case CTRL_SYNC:
	    printf("Sync\r\n");
	    break;

	case GET_SECTOR_SIZE:
	    printf("Get sector size\r\n");
	    *wd = SD_BLOCK_SIZE;
	    break;
	    
	case GET_SECTOR_COUNT:
	    printf("Get sector count\r\n");

	    if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) {
		printf("Get card info failed: %d\r\b", err);
		return RES_ERROR;
	    }
	    *dwd = cardinfo.CardCapacity / SD_BLOCK_SIZE;
	    break;
	    
	case GET_BLOCK_SIZE:
	    printf("Get block size\r\n");

	    if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) {
		printf("Get card info failed: %d\r\b", err);
		return RES_ERROR;
	    }
	    
	    /* FatFS wants log2(blocksize) */
	    *dwd = 0;
	    while (cardinfo.CardBlockSize > 0) {
		*dwd++;
		cardinfo.CardBlockSize >>= 1;
	    }
	    break;

	case CTRL_ERASE_SECTOR:
	    return RES_ERROR;
    }
	    
    return RES_OK;
	    
}

/*
  bit 31:25  Year from 1980 (0..127)
  bit 24:21  Month (1..12)
  bit 20:16  Day in month(1..31)
  bit 15:11  Hour (0..23)
  bit 10: 5  Minute (0..59)
  bit  4: 0  Second / 2 (0..29)
*/
DWORD
get_fattime(void) {
    return 0;
}

void *
ff_memalloc(UINT msize) {
    return malloc(msize);
}

void
ff_memfree(void *mblock) {
    free(mblock);
}