Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison 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 * SD card 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 "ff.h" | |
37 #include "stm32f10x.h" | |
38 #include "sd.h" | |
39 #include "stm32_eval_sdio_sd.h" | |
40 | |
41 #define FAT_YEAR(d) ((((d) & 0xfe00) >> 9) + 1980) | |
42 #define FAT_MONTH(d) (((d) & 0x01e0) >> 5) | |
43 #define FAT_DAY(d) (((d) & 0x001f) >> 0) | |
44 #define FAT_HOUR(t) (((t) & 0xf800) >> 11) | |
45 #define FAT_MIN(t) (((t) & 0x07e0) >> 5) | |
46 #define FAT_SEC(t) (((t) & 0x001f) << 1) | |
47 | |
48 static FATFS fsh; | |
49 | |
50 static void sd_domount(void); | |
51 | |
52 void | |
53 sd_init(void) { | |
54 sd_domount(); | |
55 } | |
56 | |
57 void | |
58 sd_domount(void) { | |
59 SD_Error err; | |
60 FRESULT fserr; | |
61 | |
62 if ((err = SD_Init()) != SD_OK) { | |
63 printf("Failed init: %d\n", err); | |
64 return; | |
65 } | |
66 | |
67 if ((fserr = f_mount(0, &fsh)) != FR_OK) { | |
68 printf("Unable to mount FS\n"); | |
69 return; | |
70 } | |
71 } | |
72 | |
73 void | |
74 sd_cmd(int argc, char **argv) { | |
75 SD_Error err; | |
76 | |
77 if (argc < 1) { | |
78 printf("No command specified\n"); | |
79 return; | |
80 } | |
81 | |
82 if (!strcmp(argv[0], "info")) { | |
83 SD_CardInfo cardinfo; | |
84 if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) { | |
85 printf("Get card info failed: %d\r\b", err); | |
86 } else { | |
87 printf("Mfg ID %" PRIu8 ", OEM %" PRIu16 ", Prod1 %" PRIu32 ", Prod2 %" PRIu8 ", Rev %" PRIu8 ", SN %" PRIu32 ", Mfg %" PRIu16 "\n", | |
88 cardinfo.SD_cid.ManufacturerID, cardinfo.SD_cid.OEM_AppliID, | |
89 cardinfo.SD_cid.ProdName1, cardinfo.SD_cid.ProdName2, | |
90 cardinfo.SD_cid.ProdRev, cardinfo.SD_cid.ProdSN, | |
91 cardinfo.SD_cid.ManufactDate); | |
92 printf("Capacity %" PRIu32 ", blocksize %" PRIu32 "\n", cardinfo.CardCapacity, cardinfo.CardBlockSize); | |
93 } | |
94 } else if (!strcmp(argv[0], "mount")) { | |
95 sd_domount(); | |
96 } else if (!strcmp(argv[0], "read")) { | |
97 uint32_t addr; | |
98 uint8_t *buf; | |
99 uint32_t i; | |
100 | |
101 if (argc != 2) { | |
102 printf("Block to read not specified\n"); | |
103 return; | |
104 } | |
105 addr = atoi(argv[1]); | |
106 | |
107 if ((buf = malloc(SD_BLOCK_SIZE)) == NULL) { | |
108 printf("Unable to allocate buffer\n"); | |
109 return; | |
110 } | |
111 | |
112 /* If passed values not modulo 512 it hangs */ | |
113 if ((err = SD_ReadBlock(buf, addr * SD_BLOCK_SIZE, SD_BLOCK_SIZE)) != SD_OK) { | |
114 printf("Read block returned %d\n", err); | |
115 goto read_exit; | |
116 } | |
117 | |
118 #ifdef SD_DMA_MODE | |
119 if ((err = SD_WaitReadOperation()) != SD_OK) { | |
120 printf("Wait returned %d\n", err); | |
121 goto read_exit; | |
122 } | |
123 #endif | |
124 while(SD_GetStatus() != SD_TRANSFER_OK) | |
125 ; | |
126 | |
127 for (i = 0; i < SD_BLOCK_SIZE; i += 16) { | |
128 printf("0x%04lx: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, | |
129 buf[i + 0], buf[i + 1], buf[i + 2], buf[i + 3], buf[i + 4], buf[i + 5], buf[i + 6], buf[i + 7], | |
130 buf[i + 8], buf[i + 9], buf[i + 10], buf[i + 11], buf[i + 12], buf[i + 13], buf[i + 14], buf[i + 15]); | |
131 } | |
132 | |
133 read_exit: | |
134 free(buf); | |
135 } else if (!strcmp(argv[0], "erase")) { | |
136 uint32_t addr; | |
137 | |
138 if (argc != 2) { | |
139 printf("Block to erase not specified\n"); | |
140 return; | |
141 } | |
142 addr = atoi(argv[1]); | |
143 | |
144 if ((err = SD_Erase(addr * SD_BLOCK_SIZE, (addr + 1) * SD_BLOCK_SIZE)) != SD_OK) { | |
145 printf("Erase block returned %d\n", err); | |
146 goto read_exit; | |
147 } | |
148 | |
149 #ifdef SD_DMA_MODE | |
150 if ((err = SD_WaitReadOperation()) != SD_OK) { | |
151 printf("Wait returned %d\n", err); | |
152 goto read_exit; | |
153 } | |
154 #endif | |
155 while(SD_GetStatus() != SD_TRANSFER_OK) | |
156 ; | |
157 printf("Erased OK\n"); | |
158 } else if (!strcmp(argv[0], "ls")) { | |
159 FRESULT fserr; | |
160 DIR d; | |
161 FILINFO fno; | |
162 char *fn; | |
163 char label[12]; | |
164 DWORD sn; | |
165 | |
166 if ((fserr = f_getlabel("", label, &sn)) != FR_OK) { | |
167 printf("Unable to read label\n"); | |
168 } else { | |
169 printf("Label: %s Serial: %d\n", label, sn); | |
170 } | |
171 | |
172 if ((fserr = f_opendir(&d, argc == 1 ? "\\" : argv[1])) != FR_OK) { | |
173 printf("Unable to opendir: %d\n", fserr); | |
174 return; | |
175 } | |
176 | |
177 for (;;) { | |
178 fserr = f_readdir(&d, &fno); | |
179 if (fserr != FR_OK) { | |
180 printf("Readdir failed: %d\n", fserr); | |
181 break; | |
182 } | |
183 if (fno.fname[0] == '\0') | |
184 break; | |
185 #if _USE_LFN | |
186 fn = *fno.lfname ? fno.lfname : fno.fname; | |
187 #else | |
188 fn = fno.fname; | |
189 #endif | |
190 printf("%-12s %5u %d/%d/%d %02d:%02d:%02d %s%s%s%s%s\n", fn, fno.fsize, | |
191 FAT_YEAR(fno.fdate), FAT_MONTH(fno.fdate), FAT_DAY(fno.fdate), | |
192 FAT_HOUR(fno.ftime), FAT_MIN(fno.ftime), FAT_SEC(fno.ftime), | |
193 fno.fattrib & AM_DIR ? "D" : "", fno.fattrib & AM_RDO ? "R" : "", | |
194 fno.fattrib & AM_HID ? "H" : "", fno.fattrib & AM_SYS ? "S" : "", | |
195 fno.fattrib & AM_ARC ? "A" : ""); | |
196 } | |
197 } else if (!strcmp(argv[0], "cat")) { | |
198 FRESULT fserr; | |
199 FIL f; | |
200 char buf[128], *tmp; | |
201 | |
202 if (argc != 2) { | |
203 printf("No file given\n"); | |
204 return; | |
205 } | |
206 | |
207 if ((fserr = f_open(&f, argv[1], FA_READ)) != FR_OK) { | |
208 printf("Failed to open file: %d\n", fserr); | |
209 return; | |
210 } | |
211 | |
212 while ((tmp = f_gets(buf, sizeof(buf), &f)) != NULL) { | |
213 puts(tmp); | |
214 } | |
215 } else { | |
216 printf("Unknown command\n"); | |
217 } | |
218 } | |
219 | |
220 void | |
221 SD_LowLevel_DeInit(void) { | |
222 GPIO_InitTypeDef GPIO_InitStructure; | |
223 | |
224 /*!< Disable SDIO Clock */ | |
225 SDIO_ClockCmd(DISABLE); | |
226 | |
227 /*!< Set Power State to OFF */ | |
228 SDIO_SetPowerState(SDIO_PowerState_OFF); | |
229 | |
230 /*!< DeInitializes the SDIO peripheral */ | |
231 SDIO_DeInit(); | |
232 | |
233 /*!< Disable the SDIO AHB Clock */ | |
234 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, DISABLE); | |
235 | |
236 /*!< Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin */ | |
237 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12; | |
238 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; | |
239 GPIO_Init(GPIOC, &GPIO_InitStructure); | |
240 | |
241 /*!< Configure PD.02 CMD line */ | |
242 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; | |
243 GPIO_Init(GPIOD, &GPIO_InitStructure); | |
244 } | |
245 | |
246 void | |
247 SD_LowLevel_Init(void) { | |
248 GPIO_InitTypeDef GPIO_InitStructure; | |
249 | |
250 /*!< GPIOC and GPIOD Periph clock enable */ | |
251 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE); | |
252 | |
253 /*!< Configure PC.08, PC.09, PC.10, PC.11, PC.12 pin: D0, D1, D2, D3, CLK pin */ | |
254 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12; | |
255 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | |
256 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; | |
257 GPIO_Init(GPIOC, &GPIO_InitStructure); | |
258 | |
259 /*!< Configure PD.02 CMD line */ | |
260 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; | |
261 GPIO_Init(GPIOD, &GPIO_InitStructure); | |
262 | |
263 #ifdef SD_HAVE_DETECT | |
264 RCC_APB2PeriphClockCmd(SD_DETECT_GPIO_CLK, ENABLE); | |
265 | |
266 /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */ | |
267 GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN; | |
268 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; | |
269 GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure); | |
270 #endif | |
271 | |
272 /*!< Enable the SDIO AHB Clock */ | |
273 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, ENABLE); | |
274 | |
275 #ifdef SD_DMA_MODE | |
276 /*!< Enable the DMA2 Clock */ | |
277 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); | |
278 #endif | |
279 } | |
280 | |
281 void | |
282 SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize) { | |
283 printf("SD_LowLevel_DMA_RxConfig unimplemented\n"); | |
284 abort(); | |
285 } | |
286 | |
287 void | |
288 SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize) { | |
289 printf("SD_LowLevel_DMA_TxConfig unimplemented\n"); | |
290 abort(); | |
291 } | |
292 | |
293 uint32_t SD_DMAEndOfTransferStatus(void) { | |
294 return (uint32_t)DMA_GetFlagStatus(DMA2_FLAG_TC4); | |
295 } | |
296 | |
297 | |
298 |