annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 /*
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2 * Glue between FatFS and the ST SDIO code
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 *
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 * Copyright (c) 2013
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 *
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 * are met:
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 *
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 * SUCH DAMAGE.
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 */
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 #include <assert.h>
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 #include <inttypes.h>
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 #include <stdio.h>
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 #include <stdint.h>
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 #include <stdlib.h>
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 #include <string.h>
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
35 #include <time.h>
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
37 #include "delay.h"
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 #include "stm32f10x.h"
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 #include "stm32_eval_sdio_sd.h"
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 #include "diskio.h"
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
42 #if 0
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
43 #define DEBUG(fmt, ...) printf("%s: " fmt, __FUNCTION__, ## __VA_ARGS__)
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
44 #else
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
45 #define DEBUG(...)
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
46 #endif
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
47
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
48 char *sderr2str(SD_Error err);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
49 void dump_buf(uint8_t *buf, uint32_t size);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
51 static SD_CardInfo cardinfo;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
52
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 DSTATUS
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 disk_initialize(BYTE pdrv) {
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 SD_Error err;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
57 DEBUG("disk_initialize(%d)\n", pdrv);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
58
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 /* Only have 1 disk */
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 if (pdrv != 0)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 return STA_NOINIT;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 if ((err = SD_Init()) != SD_OK) {
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
64 printf("Failed init: %s\n", sderr2str(err));
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
65 return STA_NOINIT;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
66 }
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
67 if ((err = SD_GetCardInfo(&cardinfo)) != SD_OK) {
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
68 printf("Get card info failed: %s\n", sderr2str(err));
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 return STA_NOINIT;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 return 0;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 DSTATUS
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 disk_status(BYTE pdrv) {
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
77 DEBUG("disk_status(%d)\n", pdrv);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
78
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 if (pdrv != 0)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 return STA_NOINIT;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 return 0;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 DRESULT
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 disk_read(BYTE pdrv, BYTE *buff, DWORD sector, BYTE count) {
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 SD_Error err;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
89 DEBUG("disk_read(%d, 0x%08x, %d, %d)\n", pdrv, buff, sector, count);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
90
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 if (pdrv != 0)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 return STA_NOINIT;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 if (count == 1)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 err = SD_ReadBlock(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE);
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 else
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 err = SD_ReadMultiBlocks(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE, count);
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
99 #ifdef SD_DMA_MODE
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
100 if ((err = SD_WaitReadOperation()) != SD_OK) {
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
101 printf("Wait returned %s\n", sderr2str(err));
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
102 goto read_exit;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
103 }
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
104 #endif
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 if (err != SD_OK) {
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
106 printf("Read failed: %s\n", sderr2str(err));
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 return STA_NOINIT;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
110 while(SD_GetStatus() != SD_TRANSFER_OK)
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
111 ;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
112
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 return RES_OK;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 DRESULT
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, BYTE count) {
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
118 SD_Error err;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
119
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
120 DEBUG("disk_write(%d, 0x%08x, %d, %d)\n", pdrv, buff, sector, count);
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 if (pdrv != 0)
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
122 return RES_NOTRDY;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
123
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
124 if (count == 1)
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
125 err = SD_WriteBlock(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
126 else
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
127 err = SD_WriteMultiBlocks(buff, sector * SD_BLOCK_SIZE, SD_BLOCK_SIZE, count);
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
129 #ifdef SD_DMA_MODE
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
130 if ((err = SD_WaitReadOperation()) != SD_OK) {
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
131 printf("Wait returned %s\n", sderr2str(err));
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
132 goto read_exit;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
133 }
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
134 #endif
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
135 if (err != SD_OK) {
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
136 printf("Write failed: %s\n", sderr2str(err));
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
137 return RES_ERROR;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
138 }
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
139
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
140 while(SD_GetStatus() != SD_TRANSFER_OK)
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
141 ;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
142
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
143 return RES_OK;
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 DRESULT
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 WORD *wd;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 DWORD *dwd;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 SD_Error err;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
152 DEBUG("disk_ioctl(%d, %d, ...)\n", pdrv, cmd);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
153
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 wd = buff;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 dwd = buff;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 if (pdrv != 0)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 return STA_NOINIT;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 switch (cmd) {
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 case CTRL_SYNC:
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
161 DEBUG("Sync\n");
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 break;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 case GET_SECTOR_SIZE:
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 *wd = SD_BLOCK_SIZE;
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
166 DEBUG("Get sector size (%d)\n", *wd);
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 break;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 case GET_SECTOR_COUNT:
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 *dwd = cardinfo.CardCapacity / SD_BLOCK_SIZE;
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
171 DEBUG("Get sector count (%d)\n", *dwd);
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 break;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 case GET_BLOCK_SIZE:
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 /* FatFS wants log2(blocksize) */
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 *dwd = 0;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 while (cardinfo.CardBlockSize > 0) {
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
178 *dwd = *dwd + 1;
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 cardinfo.CardBlockSize >>= 1;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 }
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
181 DEBUG("Get block size (%d)\n", *dwd);
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 break;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 case CTRL_ERASE_SECTOR:
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 return RES_ERROR;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 return RES_OK;
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 /*
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 bit 31:25 Year from 1980 (0..127)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 bit 24:21 Month (1..12)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 bit 20:16 Day in month(1..31)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196 bit 15:11 Hour (0..23)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 bit 10: 5 Minute (0..59)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 bit 4: 0 Second / 2 (0..29)
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 */
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 DWORD
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 get_fattime(void) {
50
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
202 struct tm now;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
203 time_t t;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
204 DWORD res;
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
205
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
206 t = time(NULL);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
207 localtime_r(&t, &now);
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
208 res = ((now.tm_year - 80) << 25 |
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
209 ((now.tm_mon + 1) << 21) |
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
210 (now.tm_mday << 16) |
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
211 (now.tm_hour << 11) |
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
212 (now.tm_min << 5) |
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
213 (now.tm_sec >> 1));
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
214
d7207a9d3c3b Add write support. LFN still broken though.
Daniel O'Connor <darius@dons.net.au>
parents: 49
diff changeset
215 return res;
49
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 void *
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 ff_memalloc(UINT msize) {
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 return malloc(msize);
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 }
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 void
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 ff_memfree(void *mblock) {
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 free(mblock);
ace431a0d0f5 Add SDIO code poached from STM. Use FatFS to read from SD card.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 }