00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00013 00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00015 * 00016 * Redistribution and use in source and binary forms, with or without 00017 * modification, are permitted provided that the following conditions are met: 00018 * 00019 * 1. Redistributions of source code must retain the above copyright notice, 00020 * this list of conditions and the following disclaimer. 00021 * 00022 * 2. Redistributions in binary form must reproduce the above copyright notice, 00023 * this list of conditions and the following disclaimer in the documentation 00024 * and/or other materials provided with the distribution. 00025 * 00026 * 3. The name of Atmel may not be used to endorse or promote products derived 00027 * from this software without specific prior written permission. 00028 * 00029 * 4. This software may only be redistributed and used in connection with an Atmel 00030 * AVR product. 00031 * 00032 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00033 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00034 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND 00035 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, 00036 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00037 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00038 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00039 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00040 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00041 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00042 */ 00043 00044 //_____ I N C L U D E S ____________________________________________________ 00045 00046 #include "config.h" // system configuration 00047 #include "lib_mcu/usb/usb_drv.h" // usb driver definition 00048 #include "lib_mcu/spi/spi_drv.h" // spi driver definition 00049 #include "df.h" // dataflash definition 00050 00051 00052 //_____ D E F I N I T I O N ________________________________________________ 00053 00054 static U32 gl_ptr_mem; // memory data pointer 00055 static U8 df_mem_busy; // memories in busy state 00056 static U8 df_select; // current memory selected 00057 00058 00059 //_____ D E C L A R A T I O N _____________________________________________ 00060 00061 static void df_wait_busy(void); 00062 static void df_chipselect_current(void); 00063 00064 00067 void df_init (void) 00068 { 00069 // Init the SPI communication link between the DF and the DF driver. 00070 Df_init_spi(); 00071 Spi_select_master(); 00072 Spi_set_mode(SPI_MODE_3); 00073 Spi_init_bus(); 00074 Spi_set_rate(SPI_RATE_0); // SCK freq == fosc/2. 00075 Spi_disable_ss(); 00076 Spi_enable(); 00077 00078 // Data Flash Controller init. 00079 df_mem_busy = 0; // all memories ready 00080 df_select = 0; 00081 } 00082 00083 00091 static void df_chipselect_memzone(U8 memzone) 00092 { 00093 #if (DF_NB_MEM == 1) 00094 df_select = 0; 00095 #else 00096 #if (DF_NB_MEM == 2) 00097 #if (DF_PAGE_SIZE == 512) 00098 df_select = (memzone>>0)&0x01; 00099 #else 00100 df_select = (memzone>>1)&0x01; 00101 #endif 00102 #else 00103 #if (DF_PAGE_SIZE == 512) 00104 df_select = (memzone>>0)&0x03; 00105 #else 00106 df_select = (memzone>>1)&0x03; 00107 #endif 00108 #endif 00109 #endif 00110 } 00111 00112 00115 static void df_chipselect_current(void) 00116 { 00117 Df_desel_all(); 00118 switch (df_select) 00119 { 00120 case 0: 00121 Df_select_0(); 00122 break; 00123 #if (DF_NB_MEM > 1) 00124 case 1: 00125 Df_select_1(); 00126 break; 00127 #endif 00128 #if (DF_NB_MEM > 2) 00129 case 2: 00130 Df_select_2(); 00131 break; 00132 case 3: 00133 Df_select_3(); 00134 break; 00135 #endif 00136 } 00137 } 00138 00139 00168 static U32 df_translate_addr(Uint32 log_sect_addr) 00169 { 00170 #if (DF_NB_MEM == 1) 00171 return (log_sect_addr << 9); // this case if only one memory... 00172 #else 00173 #if (DF_NB_MEM == 2) 00174 #if (DF_PAGE_SIZE == 512) 00175 return ((log_sect_addr&0xFFFFFFFE) << 8); 00176 #else 00177 return (((log_sect_addr&0xFFFFFFFC) << 8) | ((log_sect_addr&0x00000001) << 9)); 00178 #endif 00179 #else 00180 #if (DF_PAGE_SIZE == 512) 00181 return ((log_sect_addr&0xFFFFFFFC) << 7); 00182 #else 00183 return (((log_sect_addr&0xFFFFFFF8) << 7) | ((log_sect_addr&0x00000001) << 9)); 00184 #endif 00185 #endif 00186 #endif 00187 } 00188 00189 00197 Bool df_mem_check (void) 00198 { 00199 // DF memory check. 00200 for(df_select=0; df_select<DF_NB_MEM; df_select++) 00201 { 00202 df_chipselect_current(); 00203 Spi_write_data(DF_RD_STATUS); // Send the read status register cmd 00204 // + 1st step to clear the SPIF bit 00205 Spi_write_data(0); // dummy write that: 00206 // - finalize the clear of the SPIF bit (access to SPDR) 00207 // - get status register 00208 // - does the 1st step to clear the SPIF bit 00209 // Following Spi_read_data() finalize the clear of SPIF by accessing SPDR. 00210 // Check the DF density. 00211 if ((Spi_read_data() & DF_MSK_DENSITY) != DF_DENSITY) 00212 { 00213 // Unexpected value. 00214 Df_desel_all(); 00215 return (KO); 00216 } 00217 Df_desel_all(); 00218 } 00219 return OK; 00220 } 00221 00222 00225 static void df_wait_busy (void) 00226 { 00227 // Read the status register until the DataFlash is not busy. 00228 df_chipselect_current(); 00229 Spi_write_data(DF_RD_STATUS); // Send the read status register cmd 00230 // + 1st step to clear the SPIF bit 00231 Spi_write_data(0); // dummy write that: 00232 // - finalize the clear of the SPIF bit (access to SPDR) 00233 // - get status register 00234 // - does the 1st step to clear the SPIF bit 00235 // Following Spi_read_data() finalize the clear of SPIF by accessing SPDR 00236 while ((Spi_read_data() & DF_MSK_BIT_BUSY) == DF_MEM_BUSY) 00237 { 00238 Spi_write_data(0); // dummy write to get new status 00239 // + 1st step to clear the SPIF bit 00240 } 00241 Df_desel_all(); // unselect memory to leave STATUS request mode 00242 Spi_ack_write(); // Final step to clear the SPIF bit. 00243 } 00244 00245 00255 Bool df_read_open (Uint32 pos) 00256 { 00257 // Set the global memory ptr at a Byte address. 00258 gl_ptr_mem = df_translate_addr(pos); 00259 00260 // Select the DF that memory "pos" points to (the "df_select" variable will be updated) 00261 df_chipselect_memzone(LSB0(pos)); 00262 00263 // If the DF memory is busy, wait until it's not. 00264 if (is_df_busy(df_select)) 00265 { 00266 df_release_busy(df_select); 00267 df_wait_busy(); // wait end of programming 00268 } 00269 00270 // Physically assert the selected dataflash 00271 df_chipselect_current(); 00272 00273 //# 00274 //# Initiate a page read at a given sector address. 00275 //# 00276 // Send read main command, + first step to clear the SPIF bit 00277 Spi_write_data(DF_RD_MAIN); 00278 // Final step to clear the SPIF bit will be done on the next write 00279 00280 // Send the three address Bytes made of: 00281 // - the page-address(first xbits), 00282 // - the Byte-address within the page(last ybits). 00283 // (x and y depending on the DF type). 00284 // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local 00285 // DF addressing. They are used for DF discrimination when there are several 00286 // DFs. 00287 Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2)); 00288 Spi_write_data(((MSB2(gl_ptr_mem) & ~DF_PAGE_MASK) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) & DF_PAGE_MASK)); 00289 Spi_write_data(MSB3(gl_ptr_mem)); 00290 // Final step to clear the SPIF bit will be done on the next write 00291 00292 // 4 dummy writes for reading delay 00293 Spi_write_data(0xFF); 00294 Spi_write_data(0xFF); 00295 Spi_write_data(0xFF); 00296 Spi_write_data(0xFF); // Tx 0xFF, first step to clear the SPIF bit 00297 Spi_ack_write(); // Final step to clear the SPIF bit. 00298 00299 return OK; 00300 } 00301 00302 00305 void df_read_close (void) 00306 { 00307 Df_desel_all(); // Unselect memory 00308 } 00309 00310 00326 Bool df_read_sector (Uint16 nb_sector) 00327 { 00328 U8 i; 00329 #ifdef DF_CODE_SIZE_OPTIMIZATION 00330 U8 j; 00331 #endif 00332 do 00333 { 00334 for (i = 8; i != 0; i--) 00335 { 00336 Disable_interrupt(); // Global disable. 00337 00338 // Principle: send any Byte to get a Byte. 00339 // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit. 00340 // Spi_read_data(): get the Byte + final step to clear the SPIF bit. 00341 #ifdef DF_CODE_SIZE_OPTIMIZATION 00342 for(j=0;j<64;j++) 00343 { 00344 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00345 } 00346 #else 00347 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00348 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00349 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00350 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00351 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00352 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00353 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00354 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00355 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00356 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00357 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00358 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00359 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00360 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00361 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00362 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00363 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00364 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00365 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00366 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00367 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00368 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00369 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00370 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00371 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00372 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00373 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00374 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00375 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00376 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00377 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00378 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00379 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00380 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00381 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00382 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00383 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00384 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00385 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00386 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00387 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00388 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00389 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00390 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00391 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00392 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00393 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00394 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00395 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00396 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00397 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00398 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00399 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00400 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00401 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00402 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00403 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00404 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00405 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00406 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00407 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00408 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00409 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00410 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00411 #endif 00412 Usb_send_in(); // Send the FIFO IN content to the USB Host. 00413 00414 Enable_interrupt(); // Global interrupt re-enable. 00415 00416 // Wait until the tx is done so that we may write to the FIFO IN again. 00417 while(Is_usb_write_enabled()==FALSE) 00418 { 00419 if(!Is_usb_endpoint_enabled()) 00420 return KO; // USB Reset 00421 } 00422 } 00423 gl_ptr_mem += 512; // increment global address pointer 00424 nb_sector--; // 1 more sector read 00425 #if (DF_NB_MEM == 1) // end of page ? 00426 #if (DF_PAGE_SIZE == 512) 00427 Df_desel_all(); 00428 if (nb_sector != 0) 00429 df_read_open(gl_ptr_mem>>9); 00430 #else 00431 if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00) 00432 { 00433 Df_desel_all(); 00434 if (nb_sector != 0) 00435 df_read_open(gl_ptr_mem>>9); 00436 } 00437 #endif 00438 #endif 00439 } 00440 while (nb_sector != 0); 00441 00442 return OK; 00443 } 00444 00445 00455 Bool df_write_open (Uint32 pos) 00456 { 00457 // Set the global memory ptr at a Byte address. 00458 gl_ptr_mem = df_translate_addr(pos); 00459 00460 // Select the DF that memory "pos" points to 00461 df_chipselect_memzone(LSB0(pos)); 00462 00463 // If the DF memory is busy, wait until it's not. 00464 if (is_df_busy(df_select)) 00465 { 00466 df_release_busy(df_select); 00467 df_wait_busy(); // wait end of programming 00468 } 00469 00470 #if DF_PAGE_SIZE > 512 00471 // Physically assert the selected dataflash 00472 df_chipselect_current(); 00473 00474 //# 00475 //# Transfer the current page content in buffer1. 00476 //# 00477 // Send Main Mem page to Buffer1 command, + first step to clear the SPIF bit 00478 Spi_write_data(DF_TF_BUF_1); 00479 // Final step to clear the SPIF bit will be done on the next write 00480 00481 // Send the three address Bytes made of: 00482 // - the page-address(first xbits), 00483 // - remaining don't care bits(last ybits). 00484 // (x and y depending on the DF type). 00485 // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local 00486 // DF addressing. They are used for DF discrimination when there are several 00487 // DFs. 00488 Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2)); 00489 Spi_write_data(MSB2(gl_ptr_mem) << DF_SHFT_B1); 00490 Spi_write_data(0); // Remaining don't care bits. 00491 Spi_ack_write(); // Final step to clear the SPIF bit. 00492 00493 Df_desel_all(); // Unselect memory to validate the command 00494 00495 df_wait_busy(); // Wait end of page transfer 00496 #endif 00497 00498 // Physically assert the selected dataflash 00499 df_chipselect_current(); 00500 00501 //# 00502 //# Initiate a page write at a given sector address. 00503 //# 00504 // Send Main Memory Page Program Through Buffer1 command, 00505 // + first step to clear the SPIF bit 00506 Spi_write_data(DF_PG_BUF_1); 00507 // Final step to clear the SPIF bit will be done on the next write 00508 00509 // Send the three address Bytes made of: 00510 // (.) the page-address(first xbits), 00511 // (.) the Byte-address within the page(last ybits). 00512 // (x and y depending on the DF type). 00513 // NOTE: the bits of gl_ptr_mem above the 24bits are not useful for the local 00514 // DF addressing. They are used for DF discrimination when there are several 00515 // DFs. 00516 Spi_write_data((MSB1(gl_ptr_mem) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) >> DF_SHFT_B2)); 00517 Spi_write_data(((MSB2(gl_ptr_mem) & ~DF_PAGE_MASK) << DF_SHFT_B1) | (MSB2(gl_ptr_mem) & DF_PAGE_MASK)); 00518 Spi_write_data(MSB3(gl_ptr_mem)); 00519 Spi_ack_write(); // Final step to clear the SPIF bit. 00520 00521 return OK; 00522 } 00523 00524 00527 void df_write_close (void) 00528 { 00529 //# 00530 //# While end of logical sector (512B) not reached, zero-fill the remaining 00531 //# memory Bytes. 00532 //# 00533 while ((MSB3(gl_ptr_mem) != 0x00) || ((MSB2(gl_ptr_mem) & 0x01) != 0x00)) 00534 { 00535 Spi_write_data(0x00); // - Final step to clear the SPIF bit, 00536 // - Write 0x00 00537 // - first step to clear the SPIF bit. 00538 gl_ptr_mem++; 00539 } 00540 Spi_ack_write(); // Final step to clear the SPIF bit. 00541 00542 Df_desel_all(); // Launch page programming (or simply unselect memory 00543 // if the while loop was not performed). 00544 df_set_busy(df_select); // Current memory is busy 00545 } 00546 00547 00562 Bool df_write_sector (Uint16 nb_sector) 00563 { 00564 U8 i; 00565 #ifdef DF_CODE_SIZE_OPTIMIZATION 00566 U8 j; 00567 #endif 00568 do 00569 { 00570 //# Write 8x64b = 512b from the USB FIFO OUT. 00571 for (i = 8; i != 0; i--) 00572 { 00573 // Wait end of rx in USB EPOUT. 00574 while(!Is_usb_read_enabled()) 00575 { 00576 if(!Is_usb_endpoint_enabled()) 00577 return KO; // USB Reset 00578 } 00579 00580 Disable_interrupt(); // Global disable. 00581 00582 // SPI write principle: send a Byte then clear the SPIF flag. 00583 // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit, 00584 // (.) send a Byte read from USB, 00585 // (.) 1st step to clear the SPIF bit. 00586 #ifdef DF_CODE_SIZE_OPTIMIZATION 00587 for(j=0;j<64;j++) 00588 { 00589 Spi_write_data(Usb_read_byte()); 00590 } 00591 #else 00592 Spi_write_data(Usb_read_byte()); 00593 Spi_write_data(Usb_read_byte()); 00594 Spi_write_data(Usb_read_byte()); 00595 Spi_write_data(Usb_read_byte()); 00596 Spi_write_data(Usb_read_byte()); 00597 Spi_write_data(Usb_read_byte()); 00598 Spi_write_data(Usb_read_byte()); 00599 Spi_write_data(Usb_read_byte()); 00600 Spi_write_data(Usb_read_byte()); 00601 Spi_write_data(Usb_read_byte()); 00602 Spi_write_data(Usb_read_byte()); 00603 Spi_write_data(Usb_read_byte()); 00604 Spi_write_data(Usb_read_byte()); 00605 Spi_write_data(Usb_read_byte()); 00606 Spi_write_data(Usb_read_byte()); 00607 Spi_write_data(Usb_read_byte()); 00608 Spi_write_data(Usb_read_byte()); 00609 Spi_write_data(Usb_read_byte()); 00610 Spi_write_data(Usb_read_byte()); 00611 Spi_write_data(Usb_read_byte()); 00612 Spi_write_data(Usb_read_byte()); 00613 Spi_write_data(Usb_read_byte()); 00614 Spi_write_data(Usb_read_byte()); 00615 Spi_write_data(Usb_read_byte()); 00616 Spi_write_data(Usb_read_byte()); 00617 Spi_write_data(Usb_read_byte()); 00618 Spi_write_data(Usb_read_byte()); 00619 Spi_write_data(Usb_read_byte()); 00620 Spi_write_data(Usb_read_byte()); 00621 Spi_write_data(Usb_read_byte()); 00622 Spi_write_data(Usb_read_byte()); 00623 Spi_write_data(Usb_read_byte()); 00624 Spi_write_data(Usb_read_byte()); 00625 Spi_write_data(Usb_read_byte()); 00626 Spi_write_data(Usb_read_byte()); 00627 Spi_write_data(Usb_read_byte()); 00628 Spi_write_data(Usb_read_byte()); 00629 Spi_write_data(Usb_read_byte()); 00630 Spi_write_data(Usb_read_byte()); 00631 Spi_write_data(Usb_read_byte()); 00632 Spi_write_data(Usb_read_byte()); 00633 Spi_write_data(Usb_read_byte()); 00634 Spi_write_data(Usb_read_byte()); 00635 Spi_write_data(Usb_read_byte()); 00636 Spi_write_data(Usb_read_byte()); 00637 Spi_write_data(Usb_read_byte()); 00638 Spi_write_data(Usb_read_byte()); 00639 Spi_write_data(Usb_read_byte()); 00640 Spi_write_data(Usb_read_byte()); 00641 Spi_write_data(Usb_read_byte()); 00642 Spi_write_data(Usb_read_byte()); 00643 Spi_write_data(Usb_read_byte()); 00644 Spi_write_data(Usb_read_byte()); 00645 Spi_write_data(Usb_read_byte()); 00646 Spi_write_data(Usb_read_byte()); 00647 Spi_write_data(Usb_read_byte()); 00648 Spi_write_data(Usb_read_byte()); 00649 Spi_write_data(Usb_read_byte()); 00650 Spi_write_data(Usb_read_byte()); 00651 Spi_write_data(Usb_read_byte()); 00652 Spi_write_data(Usb_read_byte()); 00653 Spi_write_data(Usb_read_byte()); 00654 Spi_write_data(Usb_read_byte()); 00655 Spi_write_data(Usb_read_byte()); 00656 #endif 00657 Spi_ack_write(); // Final step to clear the SPIF bit. 00658 00659 Usb_ack_receive_out(); // USB EPOUT read acknowledgement. 00660 00661 Enable_interrupt(); // Global re-enable. 00662 } // for (i = 8; i != 0; i--) 00663 00664 gl_ptr_mem += 512; // Update the memory pointer. 00665 nb_sector--; // 1 more sector written 00666 00667 //# Launch page programming if end of page. 00668 //# 00669 #if DF_PAGE_SIZE > 512 00670 // Check if end of 1024b page. 00671 if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00) 00672 { 00673 Df_desel_all(); // Launch page programming 00674 df_set_busy(df_select); // memory is busy 00675 #if (DF_NB_MEM == 1) 00676 if (nb_sector != 0) 00677 df_write_open(gl_ptr_mem>>9); 00678 #endif 00679 } 00680 #else 00681 // Always end of page. 00682 Df_desel_all(); // Launch page programming 00683 df_set_busy(df_select); // memory is busy 00684 #if (DF_NB_MEM == 1) 00685 if (nb_sector != 0) 00686 df_write_open(gl_ptr_mem>>9); 00687 #endif 00688 #endif 00689 } 00690 while (nb_sector != 0); 00691 00692 return OK; // Write done 00693 } 00694 00695 00696 #if( USB_HOST_FEATURE==ENABLE ) 00720 bit df_host_write_sector (Uint16 nb_sector) 00721 { 00722 Byte i; 00723 00724 do 00725 { 00726 //# Write 8x64b = 512b from the USB FIFO OUT. 00727 for (i = 8; i != 0; i--) 00728 { 00729 // Wait end of rx in USB PIPE IN. 00730 Host_unfreeze_pipe(); 00731 while(Is_host_read_enabled()==FALSE); 00732 00733 Disable_interrupt(); // Global disable. 00734 00735 // SPI write principle: send a Byte then clear the SPIF flag. 00736 // Spi_write_data(Usb_read_byte()): (.) Final step to clear the SPIF bit, 00737 // (.) send a Byte read from USB, 00738 // (.) 1st step to clear the SPIF bit. 00739 Spi_write_data(Usb_read_byte()); 00740 Spi_write_data(Usb_read_byte()); 00741 Spi_write_data(Usb_read_byte()); 00742 Spi_write_data(Usb_read_byte()); 00743 Spi_write_data(Usb_read_byte()); 00744 Spi_write_data(Usb_read_byte()); 00745 Spi_write_data(Usb_read_byte()); 00746 Spi_write_data(Usb_read_byte()); 00747 Spi_write_data(Usb_read_byte()); 00748 Spi_write_data(Usb_read_byte()); 00749 Spi_write_data(Usb_read_byte()); 00750 Spi_write_data(Usb_read_byte()); 00751 Spi_write_data(Usb_read_byte()); 00752 Spi_write_data(Usb_read_byte()); 00753 Spi_write_data(Usb_read_byte()); 00754 Spi_write_data(Usb_read_byte()); 00755 Spi_write_data(Usb_read_byte()); 00756 Spi_write_data(Usb_read_byte()); 00757 Spi_write_data(Usb_read_byte()); 00758 Spi_write_data(Usb_read_byte()); 00759 Spi_write_data(Usb_read_byte()); 00760 Spi_write_data(Usb_read_byte()); 00761 Spi_write_data(Usb_read_byte()); 00762 Spi_write_data(Usb_read_byte()); 00763 Spi_write_data(Usb_read_byte()); 00764 Spi_write_data(Usb_read_byte()); 00765 Spi_write_data(Usb_read_byte()); 00766 Spi_write_data(Usb_read_byte()); 00767 Spi_write_data(Usb_read_byte()); 00768 Spi_write_data(Usb_read_byte()); 00769 Spi_write_data(Usb_read_byte()); 00770 Spi_write_data(Usb_read_byte()); 00771 Spi_write_data(Usb_read_byte()); 00772 Spi_write_data(Usb_read_byte()); 00773 Spi_write_data(Usb_read_byte()); 00774 Spi_write_data(Usb_read_byte()); 00775 Spi_write_data(Usb_read_byte()); 00776 Spi_write_data(Usb_read_byte()); 00777 Spi_write_data(Usb_read_byte()); 00778 Spi_write_data(Usb_read_byte()); 00779 Spi_write_data(Usb_read_byte()); 00780 Spi_write_data(Usb_read_byte()); 00781 Spi_write_data(Usb_read_byte()); 00782 Spi_write_data(Usb_read_byte()); 00783 Spi_write_data(Usb_read_byte()); 00784 Spi_write_data(Usb_read_byte()); 00785 Spi_write_data(Usb_read_byte()); 00786 Spi_write_data(Usb_read_byte()); 00787 Spi_write_data(Usb_read_byte()); 00788 Spi_write_data(Usb_read_byte()); 00789 Spi_write_data(Usb_read_byte()); 00790 Spi_write_data(Usb_read_byte()); 00791 Spi_write_data(Usb_read_byte()); 00792 Spi_write_data(Usb_read_byte()); 00793 Spi_write_data(Usb_read_byte()); 00794 Spi_write_data(Usb_read_byte()); 00795 Spi_write_data(Usb_read_byte()); 00796 Spi_write_data(Usb_read_byte()); 00797 Spi_write_data(Usb_read_byte()); 00798 Spi_write_data(Usb_read_byte()); 00799 Spi_write_data(Usb_read_byte()); 00800 Spi_write_data(Usb_read_byte()); 00801 Spi_write_data(Usb_read_byte()); 00802 Spi_write_data(Usb_read_byte()); 00803 Spi_ack_write(); // Final step to clear the SPIF bit. 00804 Host_ack_in_received(); // USB PIPE IN read acknowledgement. 00805 00806 Enable_interrupt(); // Global re-enable. 00807 } // for (i = 8; i != 0; i--) 00808 00809 gl_ptr_mem += 512; // Update the memory pointer. 00810 nb_sector--; // 1 more sector written 00811 00812 //# Launch page programming if end of page. 00813 //# 00814 #if DF_PAGE_SIZE > 512 00815 // Check if end of 1024b page. 00816 if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00) 00817 { 00818 Df_desel_all(); // Launch page programming 00819 df_set_busy(df_select); // memory is busy 00820 #if (DF_NB_MEM == 1) 00821 if (nb_sector != 0) 00822 df_write_open(gl_ptr_mem>>9); 00823 #endif 00824 } 00825 #else 00826 // Always end of page. 00827 Df_desel_all(); // Launch page programming 00828 df_set_busy(df_select); // memory is busy 00829 #if (DF_NB_MEM == 1) 00830 if (nb_sector != 0) 00831 df_write_open(gl_ptr_mem>>9); 00832 #endif 00833 #endif 00834 } 00835 while (nb_sector != 0); 00836 00837 return OK; // Write done 00838 } 00839 00864 bit df_host_read_sector (Uint16 nb_sector) 00865 { 00866 U8 i; 00867 do 00868 { 00869 for (i = 8; i != 0; i--) 00870 { 00871 Disable_interrupt(); // Global disable. 00872 00873 // Principle: send any Byte to get a Byte. 00874 // Spi_write_data(0): send any Byte + 1st step to clear the SPIF bit. 00875 // Spi_read_data(): get the Byte + final step to clear the SPIF bit. 00876 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00877 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00878 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00879 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00880 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00881 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00882 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00883 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00884 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00885 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00886 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00887 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00888 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00889 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00890 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00891 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00892 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00893 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00894 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00895 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00896 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00897 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00898 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00899 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00900 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00901 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00902 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00903 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00904 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00905 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00906 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00907 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00908 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00909 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00910 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00911 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00912 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00913 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00914 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00915 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00916 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00917 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00918 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00919 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00920 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00921 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00922 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00923 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00924 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00925 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00926 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00927 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00928 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00929 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00930 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00931 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00932 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00933 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00934 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00935 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00936 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00937 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00938 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00939 Spi_write_data(0); Usb_write_byte(Spi_read_data()); 00940 00941 Enable_interrupt(); // Global re-enable. 00942 00943 //# 00944 //# Send the USB FIFO IN content to the USB Host. 00945 //# 00946 Host_send_out(); // Send the FIFO from the USB Host. 00947 Host_unfreeze_pipe(); 00948 // Wait until the tx is done so that we may write to the FIFO IN again. 00949 while(Is_host_out_sent()==FALSE); 00950 Host_ack_out_sent(); 00951 } 00952 gl_ptr_mem += 512; // increment global address pointer 00953 nb_sector--; // 1 more sector read 00954 #if (DF_NB_MEM == 1) // end of page ? 00955 #if (DF_PAGE_SIZE == 512) 00956 Df_desel_all(); 00957 if (nb_sector != 0) 00958 df_read_open(gl_ptr_mem>>9); 00959 #else 00960 if ((MSB2(gl_ptr_mem) & DF_PAGE_MASK) == 0x00) 00961 { 00962 Df_desel_all(); 00963 if (nb_sector != 0) 00964 df_read_open(gl_ptr_mem>>9); 00965 } 00966 #endif 00967 #endif 00968 } 00969 while (nb_sector != 0); 00970 00971 return OK; // Read done. 00972 } 00973 #endif // USB_HOST_FEATURE==ENABLE 00974 00984 Bool df_read_sector_2_ram(U8 *ram) 00985 { 00986 U16 i; 00987 for(i=0;i<512;i++) 00988 { 00989 Spi_write_data(0); 00990 *ram=Spi_read_data(); 00991 ram++; 00992 } 00993 gl_ptr_mem += 512; // Update the memory pointer. 00994 return OK; 00995 } 00996 00997 01007 Bool df_write_sector_from_ram(U8 *ram) 01008 { 01009 U16 i; 01010 for(i=0;i<512;i++) 01011 { 01012 Spi_write_data(*ram); 01013 ram++; 01014 } 01015 Spi_ack_write(); // Final step to clear the SPIF bit. 01016 gl_ptr_mem += 512; // Update the memory pointer. 01017 return OK; 01018 } 01019