Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | Microchip Memory Disk Drive File System |
||
4 | |||
5 | ***************************************************************************** |
||
6 | FileName: FS Phys Interface Template.c |
||
7 | Dependencies: TEMPLATEFILE.h |
||
8 | string.h |
||
9 | FSIO.h |
||
10 | FSDefs.h |
||
11 | Processor: None |
||
12 | Compiler: None |
||
13 | Company: Microchip Technology, Inc. |
||
14 | |||
15 | Software License Agreement |
||
16 | |||
17 | The software supplied herewith by Microchip Technology Incorporated |
||
18 | (the Company) for its PICmicro® Microcontroller is intended and |
||
19 | supplied to you, the Companys customer, for use solely and |
||
20 | exclusively on Microchip PICmicro Microcontroller products. The |
||
21 | software is owned by the Company and/or its supplier, and is |
||
22 | protected under applicable copyright laws. All rights are reserved. |
||
23 | Any use in violation of the foregoing restrictions may subject the |
||
24 | user to criminal sanctions under applicable laws, as well as to |
||
25 | civil liability for the breach of the terms and conditions of this |
||
26 | license. |
||
27 | |||
28 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
29 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
30 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
31 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
32 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
33 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
34 | |||
35 | *****************************************************************************/ |
||
36 | //DOM-IGNORE-BEGIN |
||
37 | /******************************************************************** |
||
38 | Change History: |
||
39 | Rev Description |
||
40 | ----- ----------- |
||
41 | 1.2.5 Fixed bug where sector address was calculated incorrectly |
||
42 | 1.2.6 No change |
||
43 | ********************************************************************/ |
||
44 | //DOM-IGNORE-END |
||
45 | |||
46 | #include "Compiler.h" |
||
47 | #include "MDD File System\FSIO.h" |
||
48 | #include "MDD File System\FSDefs.h" |
||
49 | #include "string.h" |
||
50 | #include "MDD File System\Internal Flash.h" |
||
51 | #include "HardwareProfile.h" |
||
52 | #include "FSConfig.h" |
||
53 | |||
54 | /*************************************************************************/ |
||
55 | /* Note: This file is included as a template of a C file for */ |
||
56 | /* a new physical layer. It is designed to go with */ |
||
57 | /* "TEMPLATEFILE.h" */ |
||
58 | /*************************************************************************/ |
||
59 | |||
60 | /****************************************************************************** |
||
61 | * Global Variables |
||
62 | *****************************************************************************/ |
||
63 | |||
64 | #ifdef USE_PIC18 |
||
65 | #pragma udata |
||
66 | #pragma code |
||
67 | #endif |
||
68 | |||
69 | static MEDIA_INFORMATION mediaInformation; |
||
70 | |||
71 | /****************************************************************************** |
||
72 | * Prototypes |
||
73 | *****************************************************************************/ |
||
74 | void EraseBlock(ROM BYTE* dest); |
||
75 | void WriteRow(void); |
||
76 | void WriteByte(unsigned char); |
||
77 | BYTE DISKmount( DISK *dsk); |
||
78 | BYTE LoadMBR(DISK *dsk); |
||
79 | BYTE LoadBootSector(DISK *dsk); |
||
80 | extern void Delayms(BYTE milliseconds); |
||
81 | MEDIA_INFORMATION * MediaInitialize(void); |
||
82 | |||
83 | /****************************************************************************** |
||
84 | * Function: BYTE MediaDetect(void) |
||
85 | * |
||
86 | * PreCondition: InitIO() function has been executed. |
||
87 | * |
||
88 | * Input: void |
||
89 | * |
||
90 | * Output: TRUE - Card detected |
||
91 | * FALSE - No card detected |
||
92 | * |
||
93 | * Side Effects: None |
||
94 | * |
||
95 | * Overview: None |
||
96 | * |
||
97 | * Note: None |
||
98 | *****************************************************************************/ |
||
99 | BYTE MDD_IntFlash_MediaDetect() |
||
100 | { |
||
101 | return TRUE; |
||
102 | }//end MediaDetect |
||
103 | |||
104 | /****************************************************************************** |
||
105 | * Function: WORD ReadSectorSize(void) |
||
106 | * |
||
107 | * PreCondition: MediaInitialize() is complete |
||
108 | * |
||
109 | * Input: void |
||
110 | * |
||
111 | * Output: WORD - size of the sectors for this physical media. |
||
112 | * |
||
113 | * Side Effects: None |
||
114 | * |
||
115 | * Overview: None |
||
116 | * |
||
117 | * Note: None |
||
118 | *****************************************************************************/ |
||
119 | WORD MDD_IntFlash_ReadSectorSize(void) |
||
120 | { |
||
121 | return MEDIA_SECTOR_SIZE; |
||
122 | } |
||
123 | |||
124 | /****************************************************************************** |
||
125 | * Function: DWORD ReadCapacity(void) |
||
126 | * |
||
127 | * PreCondition: MediaInitialize() is complete |
||
128 | * |
||
129 | * Input: void |
||
130 | * |
||
131 | * Output: DWORD - size of the "disk" |
||
132 | * |
||
133 | * Side Effects: None |
||
134 | * |
||
135 | * Overview: None |
||
136 | * |
||
137 | * Note: None |
||
138 | *****************************************************************************/ |
||
139 | DWORD MDD_IntFlash_ReadCapacity(void) |
||
140 | { |
||
141 | return MDD_INTERNAL_FLASH_TOTAL_DISK_SIZE; |
||
142 | } |
||
143 | |||
144 | /****************************************************************************** |
||
145 | * Function: BYTE InitIO(void) |
||
146 | * |
||
147 | * PreCondition: None |
||
148 | * |
||
149 | * Input: void |
||
150 | * |
||
151 | * Output: TRUE - Card initialized |
||
152 | * FALSE - Card not initialized |
||
153 | * |
||
154 | * Side Effects: None |
||
155 | * |
||
156 | * Overview: None |
||
157 | * |
||
158 | * Note: None |
||
159 | *****************************************************************************/ |
||
160 | BYTE MDD_IntFlash_InitIO (void) |
||
161 | { |
||
162 | return TRUE; |
||
163 | } |
||
164 | |||
165 | /****************************************************************************** |
||
166 | * Function: BYTE MediaInitialize(void) |
||
167 | * |
||
168 | * PreCondition: None |
||
169 | * |
||
170 | * Input: None |
||
171 | * |
||
172 | * Output: Returns a pointer to a MEDIA_INFORMATION structure |
||
173 | * |
||
174 | * Overview: MediaInitialize initializes the media card and supporting variables. |
||
175 | * |
||
176 | * Note: None |
||
177 | *****************************************************************************/ |
||
178 | MEDIA_INFORMATION * MDD_IntFlash_MediaInitialize(void) |
||
179 | { |
||
180 | mediaInformation.errorCode = MEDIA_NO_ERROR; |
||
181 | return &mediaInformation; |
||
182 | }//end MediaInitialize |
||
183 | |||
184 | |||
185 | /****************************************************************************** |
||
186 | * Function: BYTE SectorRead(DWORD sector_addr, BYTE *buffer) |
||
187 | * |
||
188 | * PreCondition: None |
||
189 | * |
||
190 | * Input: sector_addr - Sector address, each sector contains 512-byte |
||
191 | * buffer - Buffer where data will be stored, see |
||
192 | * 'ram_acs.h' for 'block' definition. |
||
193 | * 'Block' is dependent on whether internal or |
||
194 | * external memory is used |
||
195 | * |
||
196 | * Output: Returns TRUE if read successful, false otherwise |
||
197 | * |
||
198 | * Side Effects: None |
||
199 | * |
||
200 | * Overview: SectorRead reads 512 bytes of data from the card starting |
||
201 | * at the sector address specified by sector_addr and stores |
||
202 | * them in the location pointed to by 'buffer'. |
||
203 | * |
||
204 | * Note: The device expects the address field in the command packet |
||
205 | * to be byte address. Therefore the sector_addr must first |
||
206 | * be converted to byte address. This is accomplished by |
||
207 | * shifting the address left 9 times. |
||
208 | *****************************************************************************/ |
||
209 | BYTE MDD_IntFlash_SectorRead(DWORD sector_addr, BYTE* buffer) |
||
210 | { |
||
211 | #if defined(__C30__) |
||
212 | #if defined(__PIC24FJ256DA210__) \ |
||
213 | || defined(__PIC24FJ128DA210__) \ |
||
214 | || defined(__PIC24FJ256DA206__) \ |
||
215 | || defined(__PIC24FJ128DA206__) \ |
||
216 | || defined(__PIC24FJ256DA110__) \ |
||
217 | || defined(__PIC24FJ128DA110__) \ |
||
218 | || defined(__PIC24FJ256DA106__) \ |
||
219 | || defined(__PIC24FJ128DA106__) \ |
||
220 | || defined(__PIC24FJ256GB210__) |
||
221 | WORD DSRPageSave; |
||
222 | DSRPageSave = DSRPAG; |
||
223 | |||
224 | DSRPAG = (FILES_ADDRESS + sector_addr * MEDIA_SECTOR_SIZE)/0x8000 + 0x0200; |
||
225 | |||
226 | #else |
||
227 | WORD PSVPageSave; |
||
228 | |||
229 | PSVPageSave = PSVPAG; |
||
230 | |||
231 | PSVPAG = (FILES_ADDRESS + sector_addr * MEDIA_SECTOR_SIZE)/0x8000; |
||
232 | #endif |
||
233 | #endif |
||
234 | |||
235 | memcpypgm2ram |
||
236 | ( |
||
237 | (void*)buffer, |
||
238 | (ROM void*)(MASTER_BOOT_RECORD_ADDRESS + (sector_addr * MEDIA_SECTOR_SIZE)), |
||
239 | MEDIA_SECTOR_SIZE |
||
240 | ); |
||
241 | |||
242 | #if defined(__C30__) |
||
243 | #if defined(__PIC24FJ256DA210__) \ |
||
244 | || defined(__PIC24FJ128DA210__) \ |
||
245 | || defined(__PIC24FJ256DA206__) \ |
||
246 | || defined(__PIC24FJ128DA206__) \ |
||
247 | || defined(__PIC24FJ256DA110__) \ |
||
248 | || defined(__PIC24FJ128DA110__) \ |
||
249 | || defined(__PIC24FJ256DA106__) \ |
||
250 | || defined(__PIC24FJ128DA106__) \ |
||
251 | || defined(__PIC24FJ256GB210__) |
||
252 | DSRPAG = DSRPageSave; |
||
253 | #else |
||
254 | PSVPAG = PSVPageSave; |
||
255 | #endif |
||
256 | #endif |
||
257 | |||
258 | return TRUE; |
||
259 | }//end SectorRead |
||
260 | |||
261 | /****************************************************************************** |
||
262 | * Function: BYTE SectorWrite(DWORD sector_addr, BYTE *buffer, BYTE allowWriteToZero) |
||
263 | * |
||
264 | * PreCondition: None |
||
265 | * |
||
266 | * Input: sector_addr - Sector address, each sector contains 512-byte |
||
267 | * buffer - Buffer where data will be read |
||
268 | * allowWriteToZero - If true, writes to the MBR will be valid |
||
269 | * |
||
270 | * Output: Returns TRUE if write successful, FALSE otherwise |
||
271 | * |
||
272 | * Side Effects: None |
||
273 | * |
||
274 | * Overview: SectorWrite sends 512 bytes of data from the location |
||
275 | * pointed to by 'buffer' to the card starting |
||
276 | * at the sector address specified by sector_addr. |
||
277 | * |
||
278 | * Note: The sample device expects the address field in the command packet |
||
279 | * to be byte address. Therefore the sector_addr must first |
||
280 | * be converted to byte address. This is accomplished by |
||
281 | * shifting the address left 9 times. |
||
282 | *****************************************************************************/ |
||
283 | #if defined(__18CXX) |
||
284 | #pragma udata myFileBuffer |
||
285 | #endif |
||
286 | volatile unsigned char file_buffer[ERASE_BLOCK_SIZE] __attribute__((far)); |
||
287 | #if defined(__18CXX) |
||
288 | #pragma udata |
||
289 | #endif |
||
290 | |||
291 | #define INTERNAL_FLASH_PROGRAM_WORD 0x4003 |
||
292 | #define INTERNAL_FLASH_ERASE 0x4042 |
||
293 | #define INTERNAL_FLASH_PROGRAM_PAGE 0x4001 |
||
294 | |||
295 | |||
296 | #if defined(__C32__) |
||
297 | #define PTR_SIZE DWORD |
||
298 | #else |
||
299 | #define PTR_SIZE WORD |
||
300 | #endif |
||
301 | ROM BYTE *FileAddress = 0; |
||
302 | |||
303 | |||
304 | BYTE MDD_IntFlash_SectorWrite(DWORD sector_addr, BYTE* buffer, BYTE allowWriteToZero) |
||
305 | { |
||
306 | #if !defined(INTERNAL_FLASH_WRITE_PROTECT) |
||
307 | ROM BYTE* dest; |
||
308 | BOOL foundDifference; |
||
309 | WORD blockCounter; |
||
310 | WORD sectorCounter; |
||
311 | |||
312 | #if defined(__18CXX) |
||
313 | BYTE *p; |
||
314 | #endif |
||
315 | |||
316 | #if defined(__C30__) |
||
317 | #if defined(__PIC24FJ256DA210__) \ |
||
318 | || defined(__PIC24FJ128DA210__) \ |
||
319 | || defined(__PIC24FJ256DA206__) \ |
||
320 | || defined(__PIC24FJ128DA206__) \ |
||
321 | || defined(__PIC24FJ256DA110__) \ |
||
322 | || defined(__PIC24FJ128DA110__) \ |
||
323 | || defined(__PIC24FJ256DA106__) \ |
||
324 | || defined(__PIC24FJ128DA106__) \ |
||
325 | || defined(__PIC24FJ256GB210__) |
||
326 | WORD DSRPageSave; |
||
327 | DSRPageSave = DSRPAG; |
||
328 | |||
329 | DSRPAG = (FILES_ADDRESS + sector_addr * MEDIA_SECTOR_SIZE)/0x8000 + 0x0200; |
||
330 | |||
331 | #else |
||
332 | WORD PSVPageSave; |
||
333 | |||
334 | PSVPageSave = PSVPAG; |
||
335 | |||
336 | PSVPAG = (FILES_ADDRESS + sector_addr * MEDIA_SECTOR_SIZE)/0x8000; |
||
337 | #endif |
||
338 | #endif |
||
339 | |||
340 | dest = (ROM BYTE*)(MASTER_BOOT_RECORD_ADDRESS + (sector_addr * MEDIA_SECTOR_SIZE)); |
||
341 | |||
342 | sectorCounter = 0; |
||
343 | |||
344 | while(sectorCounter < MEDIA_SECTOR_SIZE) |
||
345 | { |
||
346 | foundDifference = FALSE; |
||
347 | for(blockCounter = 0; blockCounter < ERASE_BLOCK_SIZE; blockCounter++) |
||
348 | { |
||
349 | if(dest[sectorCounter] != buffer[sectorCounter]) |
||
350 | { |
||
351 | foundDifference = TRUE; |
||
352 | sectorCounter -= blockCounter; |
||
353 | break; |
||
354 | } |
||
355 | sectorCounter++; |
||
356 | } |
||
357 | if(foundDifference == TRUE) |
||
358 | { |
||
359 | BYTE i,j; |
||
360 | PTR_SIZE address; |
||
361 | |||
362 | #if (ERASE_BLOCK_SIZE >= MEDIA_SECTOR_SIZE) |
||
363 | address = ((PTR_SIZE)(dest + sectorCounter) & ~(ERASE_BLOCK_SIZE - 1)); |
||
364 | |||
365 | memcpypgm2ram |
||
366 | ( |
||
367 | (void*)file_buffer, |
||
368 | (ROM void*)address, |
||
369 | ERASE_BLOCK_SIZE |
||
370 | ); |
||
371 | |||
372 | EraseBlock((ROM BYTE*)address); |
||
373 | |||
374 | address = ((PTR_SIZE)(dest + sectorCounter) & (ERASE_BLOCK_SIZE - 1)); |
||
375 | |||
376 | memcpy |
||
377 | ( |
||
378 | (void*)(&file_buffer[address]), |
||
379 | (void*)buffer, |
||
380 | MEDIA_SECTOR_SIZE |
||
381 | ); |
||
382 | |||
383 | #else |
||
384 | |||
385 | address = ((WORD)(&dest[sectorCounter]) & ~(ERASE_BLOCK_SIZE - 1)); |
||
386 | |||
387 | EraseBlock((ROM BYTE*)address); |
||
388 | |||
389 | sectorCounter = sectorCounter & ~(ERASE_BLOCK_SIZE - 1); |
||
390 | |||
391 | memcpy |
||
392 | ( |
||
393 | (void*)file_buffer, |
||
394 | (void*)buffer+sectorCounter, |
||
395 | ERASE_BLOCK_SIZE |
||
396 | ); |
||
397 | #endif |
||
398 | |||
399 | //sectorCounter = sectorCounter & ~(ERASE_BLOCK_SIZE - 1); |
||
400 | |||
401 | i=ERASE_BLOCK_SIZE/WRITE_BLOCK_SIZE; |
||
402 | j=0; |
||
403 | |||
404 | #if defined(__18CXX) |
||
405 | p = file_buffer; |
||
406 | #endif |
||
407 | |||
408 | while(i-->0) |
||
409 | { |
||
410 | //Write the new data |
||
411 | for(blockCounter = 0; blockCounter < WRITE_BLOCK_SIZE; blockCounter++) |
||
412 | { |
||
413 | //Write the data |
||
414 | #if defined(__18CXX) |
||
415 | TABLAT = *p++; |
||
416 | _asm tblwtpostinc _endasm |
||
417 | sectorCounter++; |
||
418 | #endif |
||
419 | |||
420 | #if defined(__C30__) |
||
421 | __builtin_tblwtl((int)FileAddress, *((WORD*)&file_buffer[sectorCounter])); |
||
422 | __builtin_tblwth((int)FileAddress, 0); |
||
423 | FileAddress += 2; |
||
424 | sectorCounter += 2; |
||
425 | blockCounter++; |
||
426 | #endif |
||
427 | |||
428 | #if defined(__C32__) |
||
429 | NVMWriteWord((DWORD*)KVA_TO_PA(FileAddress), *((DWORD*)&file_buffer[sectorCounter])); |
||
430 | FileAddress += 4; |
||
431 | sectorCounter += 4; |
||
432 | #endif |
||
433 | } |
||
434 | |||
435 | j++; |
||
436 | |||
437 | //write the row |
||
438 | #if defined(__18CXX) |
||
439 | // Start the write process: reposition tblptr back into memory block that we want to write to. |
||
440 | _asm tblrdpostdec _endasm |
||
441 | |||
442 | // Write flash memory, enable write control. |
||
443 | EECON1 = 0x84; |
||
444 | |||
445 | EECON2 = 0x55; |
||
446 | EECON2 = 0xaa; |
||
447 | EECON1bits.WR = 1; |
||
448 | Nop(); |
||
449 | EECON1bits.WREN = 0; |
||
450 | |||
451 | TBLPTR++; |
||
452 | #endif |
||
453 | #if defined(__C30__) |
||
454 | NVMCON = INTERNAL_FLASH_PROGRAM_PAGE; |
||
455 | asm("DISI #16"); //Disable interrupts for next few instructions for unlock sequence |
||
456 | __builtin_write_NVM(); |
||
457 | #endif |
||
458 | |||
459 | Nop(); |
||
460 | Nop(); |
||
461 | } |
||
462 | } |
||
463 | } |
||
464 | |||
465 | #if defined(__C30__) |
||
466 | #if defined(__PIC24FJ256DA210__) \ |
||
467 | || defined(__PIC24FJ128DA210__) \ |
||
468 | || defined(__PIC24FJ256DA206__) \ |
||
469 | || defined(__PIC24FJ128DA206__) \ |
||
470 | || defined(__PIC24FJ256DA110__) \ |
||
471 | || defined(__PIC24FJ128DA110__) \ |
||
472 | || defined(__PIC24FJ256DA106__) \ |
||
473 | || defined(__PIC24FJ128DA106__) \ |
||
474 | || defined(__PIC24FJ256GB210__) |
||
475 | DSRPAG = DSRPageSave; |
||
476 | #else |
||
477 | PSVPAG = PSVPageSave; |
||
478 | #endif |
||
479 | #endif |
||
480 | |||
481 | return TRUE; |
||
482 | #else |
||
483 | return TRUE; |
||
484 | #endif |
||
485 | } //end SectorWrite |
||
486 | |||
487 | #if !defined(INTERNAL_FLASH_WRITE_PROTECT) |
||
488 | void EraseBlock(ROM BYTE* dest) |
||
489 | { |
||
490 | #if defined(__18CXX) |
||
491 | TBLPTR = (unsigned short long)dest; |
||
492 | |||
493 | //Erase the current block |
||
494 | EECON1 = 0x94; |
||
495 | |||
496 | EECON2 = 0x55; |
||
497 | EECON2 = 0xaa; |
||
498 | EECON1bits.WR = 1; |
||
499 | |||
500 | EECON1bits.WREN = 0; |
||
501 | #endif |
||
502 | |||
503 | #if defined(__C30__) |
||
504 | NVMCON = INTERNAL_FLASH_ERASE; //Erase page on next WR |
||
505 | __builtin_tblwtl((int)dest, 0xFFFF); |
||
506 | asm("DISI #16"); //Disable interrupts for next few instructions for unlock sequence |
||
507 | __builtin_write_NVM(); |
||
508 | FileAddress = dest; |
||
509 | NVMCON = INTERNAL_FLASH_PROGRAM_PAGE; |
||
510 | #endif |
||
511 | |||
512 | #if defined(__C32__) |
||
513 | FileAddress = dest; |
||
514 | NVMErasePage((BYTE *)KVA_TO_PA(dest)); |
||
515 | #endif |
||
516 | } |
||
517 | #endif |
||
518 | |||
519 | /****************************************************************************** |
||
520 | * Function: BYTE WriteProtectState(void) |
||
521 | * |
||
522 | * PreCondition: None |
||
523 | * |
||
524 | * Input: None |
||
525 | * |
||
526 | * Output: BYTE - Returns the status of the "write enabled" pin |
||
527 | * |
||
528 | * Side Effects: None |
||
529 | * |
||
530 | * Overview: Determines if the card is write-protected |
||
531 | * |
||
532 | * Note: None |
||
533 | *****************************************************************************/ |
||
534 | |||
535 | BYTE MDD_IntFlash_WriteProtectState(void) |
||
536 | { |
||
537 | #if defined(INTERNAL_FLASH_WRITE_PROTECT) |
||
538 | return TRUE; |
||
539 | #else |
||
540 | return FALSE; |
||
541 | #endif |
||
542 | } |
||
543 |
Powered by WebSVN v2.8.3