| 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 | Change History: |
||
| 36 | Rev Description |
||
| 37 | ------- -------------------------------- |
||
| 38 | No change since 1.2.4 |
||
| 39 | |||
| 40 | *****************************************************************************/ |
||
| 41 | |||
| 42 | #include "FSIO.h" |
||
| 43 | #include "FSDefs.h" |
||
| 44 | #include "string.h" |
||
| 45 | #include "TEMPLATEFILE.h" |
||
| 46 | |||
| 47 | /*************************************************************************/ |
||
| 48 | /* Note: This file is included as a template of a C file for */ |
||
| 49 | /* a new physical layer. It is designed to go with */ |
||
| 50 | /* "TEMPLATEFILE.h" */ |
||
| 51 | /*************************************************************************/ |
||
| 52 | |||
| 53 | |||
| 54 | /****************************************************************************** |
||
| 55 | * Global Variables |
||
| 56 | *****************************************************************************/ |
||
| 57 | |||
| 58 | static MEDIA_INFORMATION mediaInformation; |
||
| 59 | |||
| 60 | /****************************************************************************** |
||
| 61 | * Prototypes |
||
| 62 | *****************************************************************************/ |
||
| 63 | |||
| 64 | extern void Delayms(BYTE milliseconds); |
||
| 65 | BYTE MDD_TEMPLATE_MediaInitialize(void); |
||
| 66 | |||
| 67 | |||
| 68 | /****************************************************************************** |
||
| 69 | * Function: BYTE MDD_TEMPLATE_MediaDetect(void) |
||
| 70 | * |
||
| 71 | * PreCondition: InitIO() function has been executed. |
||
| 72 | * |
||
| 73 | * Input: void |
||
| 74 | * |
||
| 75 | * Output: TRUE - Card detected |
||
| 76 | * FALSE - No card detected |
||
| 77 | * |
||
| 78 | * Side Effects: None |
||
| 79 | * |
||
| 80 | * Overview: None |
||
| 81 | * |
||
| 82 | * Note: None |
||
| 83 | *****************************************************************************/ |
||
| 84 | BYTE MDD_TEMPLATE_MediaDetect() |
||
| 85 | { |
||
| 86 | |||
| 87 | /****************************************************************/ |
||
| 88 | /* YOUR CODE HERE */ |
||
| 89 | /* If there's a way to detect your device is attached, */ |
||
| 90 | /* do it here */ |
||
| 91 | /* */ |
||
| 92 | /* You return: TRUE if device is detected, FALSE otherwise */ |
||
| 93 | /****************************************************************/ |
||
| 94 | |||
| 95 | // Sample code |
||
| 96 | |||
| 97 | if (DEVICE_DETECT_PIN) |
||
| 98 | return(TRUE); |
||
| 99 | else |
||
| 100 | return FALSE; |
||
| 101 | |||
| 102 | |||
| 103 | /****************************************************************/ |
||
| 104 | /* END OF YOUR CODE */ |
||
| 105 | /****************************************************************/ |
||
| 106 | |||
| 107 | |||
| 108 | }//end MediaDetect |
||
| 109 | |||
| 110 | |||
| 111 | /****************************************************************************** |
||
| 112 | * Function: void MDD_TEMPLATE_InitIO(void) |
||
| 113 | * |
||
| 114 | * PreCondition: None |
||
| 115 | * |
||
| 116 | * Input: void |
||
| 117 | * |
||
| 118 | * Output: void |
||
| 119 | * |
||
| 120 | * Side Effects: None |
||
| 121 | * |
||
| 122 | * Overview: None |
||
| 123 | * |
||
| 124 | * Note: None |
||
| 125 | *****************************************************************************/ |
||
| 126 | |||
| 127 | void MDD_TEMPLATE_InitIO (void) |
||
| 128 | { |
||
| 129 | /****************************************************************/ |
||
| 130 | /* YOUR CODE HERE */ |
||
| 131 | /* Initialize your TRIS bits */ |
||
| 132 | /****************************************************************/ |
||
| 133 | |||
| 134 | // Example initialization |
||
| 135 | WRITESTROBE_TRIS_BITS = OUTPUT; |
||
| 136 | READSTROBE_TRIS_BITS = INPUT; |
||
| 137 | address_bus_TRIS_BITS = 0x00; |
||
| 138 | WRITEPROTECTPIN_TRIS = INPUT; |
||
| 139 | DEVICE_DETECT_TRIS = INPUT; |
||
| 140 | |||
| 141 | /****************************************************************/ |
||
| 142 | /* END OF YOUR CODE */ |
||
| 143 | /****************************************************************/ |
||
| 144 | |||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | /****************************************************************************** |
||
| 149 | * Function: MEDIA_INFORMATION * MDD_TEMPLATE_MediaInitialize(void) |
||
| 150 | * |
||
| 151 | * PreCondition: None |
||
| 152 | * |
||
| 153 | * Input: None |
||
| 154 | * |
||
| 155 | * Output: Returns TRUE if media is initialized, FALSE otherwise |
||
| 156 | * |
||
| 157 | * Overview: MediaInitialize initializes the media card and supporting variables. |
||
| 158 | * |
||
| 159 | * Note: None |
||
| 160 | *****************************************************************************/ |
||
| 161 | MEDIA_INFORMATION * MDD_TEMPLATE_MediaInitialize(void) |
||
| 162 | { |
||
| 163 | /****************************************************************/ |
||
| 164 | /* YOUR CODE HERE */ |
||
| 165 | /* */ |
||
| 166 | /* You return: TRUE if the device was initialized */ |
||
| 167 | /* FALSE otherwise */ |
||
| 168 | /****************************************************************/ |
||
| 169 | // Example initialization |
||
| 170 | BYTE result; |
||
| 171 | |||
| 172 | data_bus_TRIS_BITS = 0x00; |
||
| 173 | |||
| 174 | address_bus = 0; |
||
| 175 | data_bus = INITIALIZATION_VALUE; |
||
| 176 | WRITESTROBE = 0; |
||
| 177 | WRITESTROBE = 1; |
||
| 178 | |||
| 179 | READSTROBE = 0; |
||
| 180 | result = data_bus; |
||
| 181 | READSTROBE = 1; |
||
| 182 | |||
| 183 | // You can define error codes depending on how your device works |
||
| 184 | // 0x00 (MEDIA_NO_ERRORS) indicates success |
||
| 185 | if (result == FAILURE) |
||
| 186 | mediaInformation.errorCode = 0x00; |
||
| 187 | else |
||
| 188 | mediaInformation.errorCode = 0x01; |
||
| 189 | |||
| 190 | mediaInformation.validityFlags.value = 0; |
||
| 191 | |||
| 192 | /****************************************************************/ |
||
| 193 | /* END OF YOUR CODE */ |
||
| 194 | /****************************************************************/ |
||
| 195 | |||
| 196 | return &mediaInformation; |
||
| 197 | |||
| 198 | }//end MediaInitialize |
||
| 199 | |||
| 200 | |||
| 201 | /****************************************************************************** |
||
| 202 | * Function: BYTE MDD_TEMPLATE_SectorRead(DWORD sector_addr, BYTE *buffer) |
||
| 203 | * |
||
| 204 | * PreCondition: None |
||
| 205 | * |
||
| 206 | * Input: sector_addr - Sector address, each sector contains 512-byte |
||
| 207 | * buffer - Buffer where data will be stored, see |
||
| 208 | * 'ram_acs.h' for 'block' definition. |
||
| 209 | * 'Block' is dependent on whether internal or |
||
| 210 | * external memory is used |
||
| 211 | * |
||
| 212 | * Output: Returns TRUE if read successful, false otherwise |
||
| 213 | * |
||
| 214 | * Side Effects: None |
||
| 215 | * |
||
| 216 | * Overview: SectorRead reads 512 bytes of data from the card starting |
||
| 217 | * at the sector address specified by sector_addr and stores |
||
| 218 | * them in the location pointed to by 'buffer'. |
||
| 219 | * |
||
| 220 | * Note: The device expects the address field in the command packet |
||
| 221 | * to be byte address. Therefore the sector_addr must first |
||
| 222 | * be converted to byte address. This is accomplished by |
||
| 223 | * shifting the address left 9 times. |
||
| 224 | *****************************************************************************/ |
||
| 225 | BYTE MDD_TEMPLATE_SectorRead(DWORD sector_addr, BYTE* buffer) |
||
| 226 | { |
||
| 227 | /****************************************************************/ |
||
| 228 | /* YOUR CODE HERE */ |
||
| 229 | /* Parameters passed to you: */ |
||
| 230 | /* sector_addr: The sector of the card to read from */ |
||
| 231 | /* buffer: The buffer the info will be read to */ |
||
| 232 | /* */ |
||
| 233 | /* You return: TRUE if the data is successfully read */ |
||
| 234 | /* FALSE otherwise */ |
||
| 235 | /****************************************************************/ |
||
| 236 | |||
| 237 | // Example code |
||
| 238 | WORD index; |
||
| 239 | BYTE status = TRUE; |
||
| 240 | BYTE dummyVariable; |
||
| 241 | |||
| 242 | data_bus_TRIS_BITS = 0xFF; |
||
| 243 | |||
| 244 | // Set the address to read from |
||
| 245 | // sector_addr is given in # of sectors |
||
| 246 | // Multiplying by 512 (the sector size in this example) will |
||
| 247 | // convert it to bytes |
||
| 248 | address_bus = sector_addr << 9; |
||
| 249 | |||
| 250 | for(index = 0; index < MEDIA_SECTOR_SIZE; index++) //Reads in 512-byte of data |
||
| 251 | { |
||
| 252 | // Tell our imaginary device we want to read from it |
||
| 253 | READSTROBE = 0; |
||
| 254 | // Read the byte, unless the buffer pointer is NULL |
||
| 255 | // If it in NULL, just do a dummy read |
||
| 256 | // NULL is passed in to provide compatibility with Microchip's USB mass storage code |
||
| 257 | if (buffer != NULL) |
||
| 258 | buffer[index] = data_bus; |
||
| 259 | else |
||
| 260 | dummyVariable = data_bus; |
||
| 261 | // Reading is done |
||
| 262 | READSTROBE = 1; |
||
| 263 | // Read the next address |
||
| 264 | address_bus++; |
||
| 265 | } |
||
| 266 | |||
| 267 | |||
| 268 | return(status); |
||
| 269 | |||
| 270 | /****************************************************************/ |
||
| 271 | /* END OF YOUR CODE */ |
||
| 272 | /****************************************************************/ |
||
| 273 | |||
| 274 | }//end SectorRead |
||
| 275 | |||
| 276 | /****************************************************************************** |
||
| 277 | * Function: BYTE MDD_TEMPLATE_SectorWrite(DWORD sector_addr, BYTE *buffer, BYTE allowWriteToZero) |
||
| 278 | * |
||
| 279 | * PreCondition: None |
||
| 280 | * |
||
| 281 | * Input: sector_addr - Sector address, each sector contains 512-byte |
||
| 282 | * buffer - Buffer where data will be read |
||
| 283 | * allowWriteToZero - If true, writes to the MBR will be valid |
||
| 284 | * |
||
| 285 | * Output: Returns TRUE if write successful, FALSE otherwise |
||
| 286 | * |
||
| 287 | * Side Effects: None |
||
| 288 | * |
||
| 289 | * Overview: SectorWrite sends 512 bytes of data from the location |
||
| 290 | * pointed to by 'buffer' to the card starting |
||
| 291 | * at the sector address specified by sector_addr. |
||
| 292 | * |
||
| 293 | * Note: The sample device expects the address field in the command packet |
||
| 294 | * to be byte address. Therefore the sector_addr must first |
||
| 295 | * be converted to byte address. This is accomplished by |
||
| 296 | * shifting the address left 9 times. |
||
| 297 | *****************************************************************************/ |
||
| 298 | BYTE MDD_TEMPLATE_SectorWrite(DWORD sector_addr, BYTE* buffer, BYTE allowWriteToZero) |
||
| 299 | { |
||
| 300 | /****************************************************************/ |
||
| 301 | /* YOUR CODE HERE */ |
||
| 302 | /* Parameters passed to you: */ |
||
| 303 | /* sector_addr: The sector of the card to write to */ |
||
| 304 | /* buffer: The buffer containing info to write */ |
||
| 305 | /* allowWriteToZero: Allows overwriting of the MBR */ |
||
| 306 | /* */ |
||
| 307 | /* You return: TRUE if the data is successfully written */ |
||
| 308 | /* FALSE otherwise */ |
||
| 309 | /****************************************************************/ |
||
| 310 | |||
| 311 | // Example code |
||
| 312 | WORD index; |
||
| 313 | BYTE status = TRUE; |
||
| 314 | |||
| 315 | if (sector_addr == 0 && allowWriteToZero == FALSE) |
||
| 316 | status = FALSE; |
||
| 317 | else |
||
| 318 | { |
||
| 319 | data_bus_TRIS_BITS = 0x00; |
||
| 320 | |||
| 321 | // Set the address to write to on your device |
||
| 322 | address_bus = sector_addr << 9; |
||
| 323 | |||
| 324 | for(index = 0; index < MEDIA_SECTOR_SIZE; index++) //Send 512 bytes of data |
||
| 325 | { |
||
| 326 | // Write the data to your device |
||
| 327 | data_bus = buffer[index]; |
||
| 328 | WRITESTROBE = 0; |
||
| 329 | WRITESTROBE = 1; |
||
| 330 | |||
| 331 | address_bus++; |
||
| 332 | } |
||
| 333 | } // Not writing to 0 sector |
||
| 334 | |||
| 335 | return(status); |
||
| 336 | |||
| 337 | |||
| 338 | /****************************************************************/ |
||
| 339 | /* END OF YOUR CODE */ |
||
| 340 | /****************************************************************/ |
||
| 341 | |||
| 342 | } //end SectorWrite |
||
| 343 | |||
| 344 | |||
| 345 | /****************************************************************************** |
||
| 346 | * Function: BYTE MDD_TEMPLATE_WriteProtectState(void) |
||
| 347 | * |
||
| 348 | * PreCondition: None |
||
| 349 | * |
||
| 350 | * Input: None |
||
| 351 | * |
||
| 352 | * Output: BYTE - Returns the status of the "write enabled" pin |
||
| 353 | * |
||
| 354 | * Side Effects: None |
||
| 355 | * |
||
| 356 | * Overview: Determines if the card is write-protected |
||
| 357 | * |
||
| 358 | * Note: None |
||
| 359 | *****************************************************************************/ |
||
| 360 | |||
| 361 | BYTE MDD_TEMPLATE_WriteProtectState(void) |
||
| 362 | { |
||
| 363 | /****************************************************************/ |
||
| 364 | /* YOUR CODE HERE */ |
||
| 365 | /* You return: TRUE if the device is write protected */ |
||
| 366 | /* FALSE otherwise */ |
||
| 367 | /****************************************************************/ |
||
| 368 | |||
| 369 | |||
| 370 | return(WRITEPROTECTPIN); |
||
| 371 | |||
| 372 | |||
| 373 | /****************************************************************/ |
||
| 374 | /* END OF YOUR CODE */ |
||
| 375 | /****************************************************************/ |
||
| 376 | |||
| 377 | } |
||
| 378 | |||
| 379 | |||
| 380 | /****************************************************************************** |
||
| 381 | * Function: BYTE Delayms(void) |
||
| 382 | * |
||
| 383 | * PreCondition: None |
||
| 384 | * |
||
| 385 | * Input: BYTE millisecons - Number of ms to delay |
||
| 386 | * |
||
| 387 | * Output: None |
||
| 388 | * |
||
| 389 | * Side Effects: None |
||
| 390 | * |
||
| 391 | * Overview: Delays the code- not necessairly accurate |
||
| 392 | * |
||
| 393 | * Note: None. |
||
| 394 | *****************************************************************************/ |
||
| 395 | |||
| 396 | |||
| 397 | void Delayms(BYTE milliseconds) |
||
| 398 | { |
||
| 399 | BYTE ms; |
||
| 400 | DWORD count; |
||
| 401 | |||
| 402 | ms = milliseconds; |
||
| 403 | while (ms--) |
||
| 404 | { |
||
| 405 | count = MILLISECDELAY; |
||
| 406 | |||
| 407 | while (count--) |
||
| 408 | { |
||
| 409 | ; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | Nop(); |
||
| 413 | return; |
||
| 414 | } |
||
| 415 |
Powered by WebSVN v2.8.3