comparison 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
comparison
equal deleted inserted replaced
48:2f336d212c74 49:ace431a0d0f5
1 /*
2 * Glue between FatFS and the ST SDIO code
3 *
4 * Copyright (c) 2013
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <assert.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "stm32f10x.h"
37 #include "stm32_eval_sdio_sd.h"
38 #include "diskio.h"
39
40 DSTATUS
41 disk_initialize(BYTE pdrv) {
42 SD_Error err;
43
44 /* Only have 1 disk */
45 if (pdrv != 0)
46 return STA_NOINIT;
47
48 if ((err = SD_Init()) != SD_OK) {
49 printf("Failed init: %d\r\n", err);
50 return STA_NOINIT;
51 }
52
53 return 0;
54 }
55
56 DSTATUS
57 disk_status(BYTE pdrv) {
58 if (pdrv != 0)
59 return STA_NOINIT;
60
61 return 0;
62 }
63
64 DRESULT
65 disk_read(BYTE pdrv, BYTE *buff, DWORD sector, BYTE count) {
66 SD_Error err;
67
68 if (pdrv != 0)
69 return STA_NOINIT;
70
71 if (count == 1)
72 err = SD_ReadBlock(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE);
73 else
74 err = SD_ReadMultiBlocks(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE, count);
75
76 if (err != SD_OK) {
77 printf("Read failed: %d\r\n", err);
78 return STA_NOINIT;
79 }
80
81 return RES_OK;
82 }
83
84 DRESULT
85 disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, BYTE count) {
86 if (pdrv != 0)
87 return STA_NOINIT;
88
89 return RES_WRPRT;
90 }
91
92 DRESULT
93 disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
94 WORD *wd;
95 DWORD *dwd;
96 SD_CardInfo cardinfo;
97 SD_Error err;
98
99 wd = buff;
100 dwd = buff;
101
102 if (pdrv != 0)
103 return STA_NOINIT;
104 switch (cmd) {
105 case CTRL_SYNC:
106 printf("Sync\r\n");
107 break;
108
109 case GET_SECTOR_SIZE:
110 printf("Get sector size\r\n");
111 *wd = SD_BLOCK_SIZE;
112 break;
113
114 case GET_SECTOR_COUNT:
115 printf("Get sector count\r\n");
116
117 if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) {
118 printf("Get card info failed: %d\r\b", err);
119 return RES_ERROR;
120 }
121 *dwd = cardinfo.CardCapacity / SD_BLOCK_SIZE;
122 break;
123
124 case GET_BLOCK_SIZE:
125 printf("Get block size\r\n");
126
127 if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) {
128 printf("Get card info failed: %d\r\b", err);
129 return RES_ERROR;
130 }
131
132 /* FatFS wants log2(blocksize) */
133 *dwd = 0;
134 while (cardinfo.CardBlockSize > 0) {
135 *dwd++;
136 cardinfo.CardBlockSize >>= 1;
137 }
138 break;
139
140 case CTRL_ERASE_SECTOR:
141 return RES_ERROR;
142 }
143
144 return RES_OK;
145
146 }
147
148 /*
149 bit 31:25 Year from 1980 (0..127)
150 bit 24:21 Month (1..12)
151 bit 20:16 Day in month(1..31)
152 bit 15:11 Hour (0..23)
153 bit 10: 5 Minute (0..59)
154 bit 4: 0 Second / 2 (0..29)
155 */
156 DWORD
157 get_fattime(void) {
158 return 0;
159 }
160
161 void *
162 ff_memalloc(UINT msize) {
163 return malloc(msize);
164 }
165
166 void
167 ff_memfree(void *mblock) {
168 free(mblock);
169 }