-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcard.c
159 lines (129 loc) · 4.24 KB
/
card.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************
Analog Devices, Inc. All Rights Reserved.
Description: This file tests the SD interface on the EZ-KIT.
*******************************************************************/
#include <stdio.h>
#include <time.h>
#include "card.h"
#include "rsi.h"
#include "fatfs.h"
/* Private functions ---------------------------------------------------------*/
/*---------------------------------------------------------*/
/* User Provided Timer Function for FatFs module */
/*---------------------------------------------------------*/
section("L1_code")
DWORD get_fattime(void)
{
TIME_DATE t = {
.sec = 0,
.min = 30,
.hour = 14,
.week = 0, /* äåíü íåäåëè...íå èñïîëüçóåöà */
.day = 26,
.month = 7, /* 1 - 12 */
.year = 2013
};
return ((DWORD) (t.year - 1980) << 25) /* Ãîä */
|((DWORD) t.mon << 21) /* Ìåñÿö: 1...12 */
|((DWORD) t.day << 16) /* äåíü: 1...30 */
|((DWORD) t.hour << 11) /* ÷àñû: 0...23 */
|((DWORD) t.min << 5) /* ìèíóòû: 0...59 */
|((DWORD) (t.sec / 2) >> 1); /* ñåêóíäû * 2: 0...29 */
}
/**
* Îïðàøèâàòü ñîñòîÿíèå äèñêà, è åñëè åå âûíóëè-ïîêàçàòü íåãîòîâíîñòü
*/
section("L1_code")
DSTATUS disk_status(BYTE drv)
{
if (!(*pPORTGIO & PG1))
return STA_NOINIT;
if (!(*pPORTGIO & PG0))
return STA_PROTECT;
return 0;
}
/**
* Èíèöèàëèçèðîâàòü SD èíòåðôåéñ-âûçîâ ôóíêöèè ïåðåíåñ èç äðóãèõ ôàéëîâ ff.c
*/
section("L1_code")
DSTATUS disk_init(BYTE drv)
{
if (rsi_setup()) /* Íå âñòàâëåíà êàðòà èëè êàêèå-òî ïðîáëåìû */
return STA_NOINIT;
return 0;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
section("L1_code")
DRESULT disk_read(BYTE drv, /* Physical drive nmuber (0) */
BYTE * buff, /* Pointer to the data buffer to store read data */
DWORD sector, /* Start sector number (LBA) */
BYTE count /* Sector count (1..128) */
)
{
DSTATUS s;
s = disk_status(drv);
if (s & STA_NOINIT)
return RES_NOTRDY;
if (!count)
return RES_PARERR;
/* Ïðî÷èòàòü áëîê / áëîêè */
rsi_read_blocks_from_sd_card(sector, buff, count);
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
section("L1_code")
DRESULT disk_write(BYTE drv, /* Physical drive nmuber (0) */
const BYTE * buff, /* Pointer to the data to be written */
DWORD sector, /* Start sector number (LBA) */
BYTE count /* Sector count (1..128) */
)
{
DSTATUS s;
s = disk_status(drv);
if (s & STA_NOINIT)
return RES_NOTRDY;
if (s & STA_PROTECT)
return RES_WRPRT; /* ïîêà íå ïðîâåðÿåì ýòî */
if (!count)
return RES_PARERR;
/* Çàïèñàòü áëîê / áëîêè */
rsi_write_blocks_to_sd_card(sector, (void *) buff, count);
return RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
section("L1_code")
DRESULT disk_ioctl(BYTE drv, /* Physical drive nmuber (0) */
BYTE ctrl, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
BYTE n, csd[16];
WORD cs;
if (disk_status(drv) & STA_NOINIT) /* Check if card is in the socket */
return RES_NOTRDY;
res = RES_ERROR;
switch (ctrl) {
case CTRL_SYNC: /* Make sure that no pending write process */
res = RES_OK;
break;
case GET_SECTOR_COUNT: /* Get number of sectors on the disk (DWORD) */
cs = rsi_get_sec_count();
*(DWORD *) buff = (DWORD) cs << 10;
res = RES_OK;
break;
case GET_BLOCK_SIZE: /* Get erase block size in unit of sector (DWORD) */
*(DWORD *) buff = 128;
res = RES_OK;
break;
default:
res = RES_PARERR;
}
return res;
}