Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | Microchip Memory Disk Drive File System |
||
4 | |||
5 | ***************************************************************************** |
||
6 | FileName: CF-PMP.c |
||
7 | Dependencies: GenericTypeDefs.h |
||
8 | FSIO.h |
||
9 | FSDefs.h |
||
10 | CF-PMP.h |
||
11 | string.h |
||
12 | Processor: PIC24/dsPIC30/dsPIC33 |
||
13 | Compiler: C30 |
||
14 | Company: Microchip Technology, Inc. |
||
15 | |||
16 | Software License Agreement |
||
17 | |||
18 | The software supplied herewith by Microchip Technology Incorporated |
||
19 | (the Company) for its PICmicro® Microcontroller is intended and |
||
20 | supplied to you, the Companys customer, for use solely and |
||
21 | exclusively on Microchip PICmicro Microcontroller products. The |
||
22 | software is owned by the Company and/or its supplier, and is |
||
23 | protected under applicable copyright laws. All rights are reserved. |
||
24 | Any use in violation of the foregoing restrictions may subject the |
||
25 | user to criminal sanctions under applicable laws, as well as to |
||
26 | civil liability for the breach of the terms and conditions of this |
||
27 | license. |
||
28 | |||
29 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
30 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
31 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
32 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
33 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
34 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
35 | |||
36 | Change History: |
||
37 | Rev Description |
||
38 | ------- -------------------------------- |
||
39 | No change since 1.2.4 |
||
40 | |||
41 | *****************************************************************************/ |
||
42 | |||
43 | |||
44 | #include "CF-PMP.h" // compact flash interface |
||
45 | #include "GenericTypeDefs.h" |
||
46 | #include "FSIO.h" |
||
47 | #include "FSDefs.h" |
||
48 | #include "string.h" |
||
49 | |||
50 | |||
51 | |||
52 | /***************************************************************************/ |
||
53 | /* Defines */ |
||
54 | /***************************************************************************/ |
||
55 | |||
56 | |||
57 | /***************************************************************************/ |
||
58 | /* Global Variables */ |
||
59 | /***************************************************************************/ |
||
60 | |||
61 | static MEDIA_INFORMATION mediaInformation; |
||
62 | |||
63 | /************************************************************************/ |
||
64 | /* Prototypes */ |
||
65 | /************************************************************************/ |
||
66 | |||
67 | |||
68 | |||
69 | /****************************************************************************** |
||
70 | * Function: BYTE MDD_CFPMP_InitIO(void) |
||
71 | * |
||
72 | * PreCondition: None |
||
73 | * |
||
74 | * Input: void |
||
75 | * |
||
76 | * Output: TRUE - Card initialized |
||
77 | * FALSE - Card not initialized |
||
78 | * |
||
79 | * Side Effects: None |
||
80 | * |
||
81 | * Overview: None |
||
82 | * |
||
83 | * Note: None |
||
84 | *****************************************************************************/ |
||
85 | |||
86 | void MDD_CFPMP_InitIO( void) |
||
87 | { |
||
88 | PMADDR = 0x0000; |
||
89 | PMMODE = 0x1AFF; |
||
90 | PMAEN = 0xC00F; |
||
91 | PMCON = 0x83A0; |
||
92 | |||
93 | LATDbits.LATD4 = 1; |
||
94 | LATDbits.LATD5 = 1; |
||
95 | TRISDbits.TRISD4 = OUTPUT; |
||
96 | TRISDbits.TRISD5 = OUTPUT; |
||
97 | CF_PMP_RDY = 0; |
||
98 | CF_PMP_READYDIR = INPUT; |
||
99 | CF_PMP_CD1 = 0; |
||
100 | CF_PMP_CD1DIR = INPUT; |
||
101 | CF_PMP_RST = 0; |
||
102 | CF_PMP_RESETDIR = OUTPUT; |
||
103 | |||
104 | } // CFinit |
||
105 | |||
106 | |||
107 | /****************************************************************************** |
||
108 | * Function: BYTE MDD_CFPMP_MediaDetect(void) |
||
109 | * |
||
110 | * PreCondition: None |
||
111 | * |
||
112 | * Input: void |
||
113 | * |
||
114 | * Output: TRUE - Card present |
||
115 | * FALSE - Card absent |
||
116 | * |
||
117 | * Side Effects: None |
||
118 | * |
||
119 | * Overview: Determines if a card is inserted |
||
120 | * |
||
121 | * Note: None |
||
122 | *****************************************************************************/ |
||
123 | |||
124 | BYTE MDD_CFPMP_MediaDetect( void) |
||
125 | { |
||
126 | return (CF_PMP_CD1 == 0) ? TRUE : FALSE ; // test for CD1 pull down |
||
127 | } //CFDetect |
||
128 | |||
129 | |||
130 | /****************************************************************************** |
||
131 | * Function: BYTE MDD_CFPMP_WriteProtectState(void) |
||
132 | * |
||
133 | * PreCondition: None |
||
134 | * |
||
135 | * Input: void |
||
136 | * |
||
137 | * Output: 0 |
||
138 | * |
||
139 | * Side Effects: None |
||
140 | * |
||
141 | * Overview: Added for compatibility- no write protect feature |
||
142 | * |
||
143 | * Note: None |
||
144 | *****************************************************************************/ |
||
145 | |||
146 | BYTE MDD_CFPMP_WriteProtectState (void) |
||
147 | { |
||
148 | return 0; |
||
149 | } |
||
150 | |||
151 | |||
152 | /****************************************************************************** |
||
153 | * Function: BYTE MDD_CFPMP_MediaInitialize(void) |
||
154 | * |
||
155 | * PreCondition: None |
||
156 | * |
||
157 | * Input: void |
||
158 | * |
||
159 | * Output: MEDIA_NO_ERROR - The media initialized successfully |
||
160 | * |
||
161 | * Side Effects: None |
||
162 | * |
||
163 | * Overview: Return a MEDIA_INFORMATION structure to FSIO.c |
||
164 | * |
||
165 | * Note: None |
||
166 | *****************************************************************************/ |
||
167 | |||
168 | MEDIA_INFORMATION * MDD_CFPMP_MediaInitialize (void) |
||
169 | { |
||
170 | mediaInformation.errorCode = MEDIA_NO_ERROR; |
||
171 | mediaInformation.validityFlags.value = 0; |
||
172 | return &mediaInformation; |
||
173 | } |
||
174 | |||
175 | /****************************************************************************** |
||
176 | * Function: void MDD_CFPMP_CFwait(void) |
||
177 | * |
||
178 | * PreCondition: None |
||
179 | * |
||
180 | * Input: None |
||
181 | * |
||
182 | * Output: None |
||
183 | * |
||
184 | * Side Effects: None |
||
185 | * |
||
186 | * Overview: Wait until the card and PMP are ready |
||
187 | * |
||
188 | * Note: None |
||
189 | *****************************************************************************/ |
||
190 | |||
191 | void MDD_CFPMP_CFwait(void) |
||
192 | { |
||
193 | while(CF_PMP_RDY == 0); |
||
194 | while(PMMODEbits.BUSY); |
||
195 | } |
||
196 | |||
197 | |||
198 | /****************************************************************************** |
||
199 | * Function: BYTE MDD_CFPMP_CFread(BYTE add) |
||
200 | * |
||
201 | * PreCondition: None |
||
202 | * |
||
203 | * Input: BYTE add - address to read from |
||
204 | * |
||
205 | * Output: BYTE - the byte read |
||
206 | * |
||
207 | * Side Effects: None |
||
208 | * |
||
209 | * Overview: Reads a byte from the CF card |
||
210 | * |
||
211 | * Note: None |
||
212 | *****************************************************************************/ |
||
213 | |||
214 | BYTE MDD_CFPMP_CFread( BYTE add) |
||
215 | // add : register address |
||
216 | { |
||
217 | char d; |
||
218 | |||
219 | MDD_CFPMP_CFwait(); |
||
220 | |||
221 | MDD_CFPMP_DATABinput; |
||
222 | |||
223 | PMADDR = (WORD)add | (PMADDR & 0xC000); |
||
224 | |||
225 | d = PMDIN1; // get the data |
||
226 | |||
227 | return d; |
||
228 | } // CFread |
||
229 | |||
230 | |||
231 | /****************************************************************************** |
||
232 | * Function: BYTE MDD_CFPMP_CFwrite(BYTE add, BYTE d) |
||
233 | * |
||
234 | * PreCondition: None |
||
235 | * |
||
236 | * Input: BYTE add - the address to write to |
||
237 | * BYTE d - the byte to write |
||
238 | * |
||
239 | * Output: None |
||
240 | * |
||
241 | * Side Effects: None |
||
242 | * |
||
243 | * Overview: Writes a byte to the CF card |
||
244 | * |
||
245 | * Note: None |
||
246 | *****************************************************************************/ |
||
247 | |||
248 | void MDD_CFPMP_CFwrite( BYTE add, BYTE d) |
||
249 | // add : CF register |
||
250 | // d : data |
||
251 | { |
||
252 | MDD_CFPMP_CFwait(); |
||
253 | |||
254 | MDD_CFPMP_DATABoutput; |
||
255 | |||
256 | PMADDR = (WORD)add | (PMADDR & 0xC000); |
||
257 | |||
258 | PMDIN1 = d; // publish the data |
||
259 | |||
260 | } // CFwrite |
||
261 | |||
262 | |||
263 | |||
264 | |||
265 | /****************************************************************************** |
||
266 | * Function: BYTE MDD_CFPMP_SectorRead(DWORD sector_addr, BYTE *buffer) |
||
267 | * |
||
268 | * PreCondition: None |
||
269 | * |
||
270 | * Input: sector_addr - Sector address, each sector contains 512-byte |
||
271 | * buffer - Buffer where data will be stored |
||
272 | * |
||
273 | * Output: TRUE - Sector read |
||
274 | * FALSE - Sector could not be read |
||
275 | * |
||
276 | * Side Effects: None |
||
277 | * |
||
278 | * Overview: SectorRead reads 512 bytes of data from the card starting |
||
279 | * at the sector address specified by sector_addr and stores |
||
280 | * them in the location pointed to by 'buffer'. |
||
281 | * |
||
282 | * Note: None |
||
283 | *****************************************************************************/ |
||
284 | |||
285 | |||
286 | BYTE MDD_CFPMP_SectorRead(DWORD sector_addr, BYTE * buffer) |
||
287 | { |
||
288 | BYTE test; |
||
289 | WORD i = 0; |
||
290 | |||
291 | PMADDRbits.CS1 = 1; |
||
292 | |||
293 | MDD_CFPMP_CFwrite( R_COUNT, 1); |
||
294 | MDD_CFPMP_CFwrite( R_SECT, sector_addr); |
||
295 | MDD_CFPMP_CFwrite( R_CYLO, sector_addr>>8); |
||
296 | MDD_CFPMP_CFwrite( R_CYHI, sector_addr>>16); |
||
297 | MDD_CFPMP_CFwrite( R_DRIVE, ((BYTE)(sector_addr>>24) & 0xf)|0xe0); // always select card #0 |
||
298 | MDD_CFPMP_CFwrite( R_CMD, C_SECTOR_READ); |
||
299 | |||
300 | |||
301 | // Dummy read to get the first byte |
||
302 | MDD_CFPMP_CFread (0); |
||
303 | |||
304 | while ((test = MDD_CFPMP_CFread( R_STATUS)) != S_READY) |
||
305 | { |
||
306 | // If error flag is set . . . |
||
307 | if (MDD_CFPMP_CFread (R_STATUS) & 0x01) |
||
308 | { |
||
309 | return FALSE; |
||
310 | } |
||
311 | } |
||
312 | |||
313 | PMADDR = R_DATA | (PMADDR & 0xC000); |
||
314 | |||
315 | MDD_CFPMP_CFread (0); |
||
316 | |||
317 | while (i < 512) |
||
318 | { |
||
319 | MDD_CFPMP_CFwait(); |
||
320 | buffer[i++] = PMDIN1; |
||
321 | } |
||
322 | |||
323 | PMADDRbits.CS1 = 0; // CF deselected when done |
||
324 | |||
325 | return TRUE; |
||
326 | } // read_sector |
||
327 | |||
328 | |||
329 | /****************************************************************************** |
||
330 | * Function: BYTE MDD_CFPMP_SectorWrite(DWORD sector_addr, BYTE *buffer, BYTE allowWriteToZero) |
||
331 | * |
||
332 | * PreCondition: None |
||
333 | * |
||
334 | * Input: sector_addr - Sector address, each sector contains 512 bytes |
||
335 | * buffer - Buffer where data will be read from |
||
336 | * allowWriteToZero - allows write to the MBR sector |
||
337 | * |
||
338 | * Output: TRUE - Sector written |
||
339 | * FALSE - Sector could not be written |
||
340 | * |
||
341 | * Side Effects: None |
||
342 | * |
||
343 | * Overview: SectorWrite sends 512 bytes of data from the location |
||
344 | * pointed to by 'buffer' to the card starting |
||
345 | * at the sector address specified by sector_addr. |
||
346 | * |
||
347 | * Note: None |
||
348 | *****************************************************************************/ |
||
349 | |||
350 | BYTE MDD_CFPMP_SectorWrite( DWORD sector_addr, BYTE * buffer, BYTE allowWriteToZero) |
||
351 | { |
||
352 | WORD i; |
||
353 | |||
354 | if (sector_addr == 0 && allowWriteToZero == FALSE) |
||
355 | return FALSE; |
||
356 | |||
357 | PMADDRbits.CS1 = 1; |
||
358 | |||
359 | MDD_CFPMP_CFwrite( R_COUNT, 1); |
||
360 | MDD_CFPMP_CFwrite( R_SECT, sector_addr); |
||
361 | MDD_CFPMP_CFwrite( R_CYLO, sector_addr>>8); |
||
362 | MDD_CFPMP_CFwrite( R_CYHI, sector_addr>>16); |
||
363 | MDD_CFPMP_CFwrite( R_DRIVE, ((BYTE)(sector_addr>>24) & 0xf)|0xe0); // always select card #0 |
||
364 | |||
365 | MDD_CFPMP_CFwrite( R_CMD, C_SECTOR_WRITE); |
||
366 | |||
367 | MDD_CFPMP_CFread (0); |
||
368 | |||
369 | while (MDD_CFPMP_CFread( R_STATUS) != S_READY) |
||
370 | { |
||
371 | if (MDD_CFPMP_CFread( R_STATUS) == S_ERROR) |
||
372 | { |
||
373 | return FALSE; |
||
374 | } |
||
375 | } |
||
376 | |||
377 | PMADDR = R_DATA | (PMADDR & 0xC000); |
||
378 | |||
379 | for ( i=0; i<512; i++) |
||
380 | { |
||
381 | MDD_CFPMP_CFwait(); |
||
382 | PMDIN1 = RAMread( buffer, 0); // write data |
||
383 | buffer++; |
||
384 | } |
||
385 | PMADDRbits.CS1 = 0; // CF deselected when done |
||
386 | |||
387 | return TRUE; |
||
388 | } // write_sector |
||
389 | |||
390 | |||
391 | |||
392 | /****************************************************************************** |
||
393 | * Function: BYTE MDD_CFPMP_ShutdownMedia(void) |
||
394 | * |
||
395 | * PreCondition: None |
||
396 | * |
||
397 | * Input: None |
||
398 | * |
||
399 | * Output: None |
||
400 | * |
||
401 | * Side Effects: None |
||
402 | * |
||
403 | * Overview: Unmounts the card |
||
404 | * |
||
405 | * Note: None |
||
406 | *****************************************************************************/ |
||
407 | |||
408 | void MDD_CFPMP_ShutdownMedia (DISK * dsk) |
||
409 | { |
||
410 | dsk->mount = FALSE; |
||
411 | return; |
||
412 | } |
||
413 | |||
414 | |||
415 | |||
416 |
Powered by WebSVN v2.8.3