view fatfs_sd.c @ 50:d7207a9d3c3b

Add write support. LFN still broken though. Make sure we wait for the card to be done after a read or write (didn't seem to break reading but hosed writes).
author Daniel O'Connor <darius@dons.net.au>
date Fri, 05 Apr 2013 00:08:31 +1030
parents ace431a0d0f5
children 590886732da9
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 <time.h>

#include "delay.h"
#include "stm32f10x.h"
#include "stm32_eval_sdio_sd.h"
#include "diskio.h"

#if 0
#define DEBUG(fmt, ...) printf("%s: " fmt, __FUNCTION__, ## __VA_ARGS__)
#else
#define DEBUG(...)
#endif

char *sderr2str(SD_Error err);
void dump_buf(uint8_t *buf, uint32_t size);

static SD_CardInfo cardinfo;

DSTATUS
disk_initialize(BYTE pdrv) {
    SD_Error err;
    
    DEBUG("disk_initialize(%d)\n", pdrv);
    
    /* Only have 1 disk */
    if (pdrv != 0)
	return STA_NOINIT;
    
    if ((err = SD_Init()) != SD_OK) {
	printf("Failed init: %s\n", sderr2str(err));
	return STA_NOINIT;
    }
    if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) {
	printf("Get card info failed: %s\n", sderr2str(err));
	return STA_NOINIT;
    }

    return 0;
}

DSTATUS
disk_status(BYTE pdrv) {
    DEBUG("disk_status(%d)\n", pdrv);
    
    if (pdrv != 0)
	return STA_NOINIT;

    return 0;
}

DRESULT
disk_read(BYTE pdrv, BYTE *buff, DWORD sector, BYTE count) {
    SD_Error err;
    
    DEBUG("disk_read(%d, 0x%08x, %d, %d)\n", pdrv, buff, sector, count);

    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);
    
#ifdef SD_DMA_MODE
    if ((err = SD_WaitReadOperation()) != SD_OK) {
	printf("Wait returned %s\n", sderr2str(err));
	goto read_exit;
    }
#endif
    if (err != SD_OK) {
	printf("Read failed: %s\n", sderr2str(err));
	return STA_NOINIT;
    }
    
    while(SD_GetStatus() != SD_TRANSFER_OK)
	;

    return RES_OK;
}

DRESULT
disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, BYTE count) {
    SD_Error err;
    
    DEBUG("disk_write(%d, 0x%08x, %d, %d)\n", pdrv, buff, sector, count);
    if (pdrv != 0)
	return RES_NOTRDY;

    if (count == 1)
	err = SD_WriteBlock(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE);
    else
	err = SD_WriteMultiBlocks(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE, count);

#ifdef SD_DMA_MODE
    if ((err = SD_WaitReadOperation()) != SD_OK) {
	printf("Wait returned %s\n", sderr2str(err));
	goto read_exit;
    }
#endif
    if (err != SD_OK) {
	printf("Write failed: %s\n", sderr2str(err));
	return RES_ERROR;
    }
    
    while(SD_GetStatus() != SD_TRANSFER_OK)
	;
    
    return RES_OK;
}

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

	case GET_SECTOR_SIZE:
	    *wd = SD_BLOCK_SIZE;
	    DEBUG("Get sector size (%d)\n", *wd);
	    break;
	    
	case GET_SECTOR_COUNT:
	    *dwd = cardinfo.CardCapacity / SD_BLOCK_SIZE;
	    DEBUG("Get sector count (%d)\n", *dwd);
	    break;
	    
	case GET_BLOCK_SIZE:
	    /* FatFS wants log2(blocksize) */
	    *dwd = 0;
	    while (cardinfo.CardBlockSize > 0) {
		*dwd = *dwd + 1;
		cardinfo.CardBlockSize >>= 1;
	    }
	    DEBUG("Get block size (%d)\n", *dwd);
	    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) {
    struct tm now;
    time_t t;
    DWORD res;

    t = time(NULL);
    localtime_r(&t, &now);
    res = ((now.tm_year - 80) << 25 |
	   ((now.tm_mon + 1) << 21) |
	   (now.tm_mday << 16) |
	   (now.tm_hour << 11) |
	   (now.tm_min << 5) |
	   (now.tm_sec >> 1));

    return res;
}

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

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