flash读 写 擦除

00049 #ifndef HAL_FLASH_H__
00050 #define HAL_FLASH_H__
00051 
00052 #include 
00053 #include "hal_flash_hw.h"
00054 
00058 void hal_flash_page_erase(uint8_t pn);
00059 
00064 void hal_flash_byte_write(uint16_t a, uint8_t b);
00065 
00071 void hal_flash_bytes_write(uint16_t a, uint8_t *p, uint16_t n);
00072 
00077 uint8_t hal_flash_byte_read(uint16_t a);
00078 
00084 void hal_flash_bytes_read(uint16_t a, uint8_t *p, uint16_t n);
00085 
00086 #endif // HAL_FLASH_H__
00019 #include "hal_flash.h"
00020 
00021 void hal_flash_page_erase(uint8_t pn)
00022 { 
00023   // Save interrupt enable state and disable interrupts:
00024   F0 = EA;
00025   EA = 0;
00026 
00027   // Enable flash write operation:
00028   WEN = 1;
00029  
00030   // Write the page address to FCR to start the page erase operation. This
00031   // operation is "self timed" when executing from the flash; the CPU will
00032   // halt until the operation is finished:
00033   FCR = pn;
00034 
00035   // When running from XDATA RAM we need to wait for the operation to finish:
00036   while(RDYN == 1)
00037     ;
00038     
00039   WEN = 0;
00040   
00041   EA = F0; // Restore interrupt enable state  
00042 }
00043 
00044 void hal_flash_byte_write(uint16_t a, uint8_t b)
00045 {
00046   uint8_t xdata *data pb;
00047     
00048   // Save interrupt enable state and disable interrupts:
00049   F0 = EA;
00050   EA = 0;
00051   
00052   // Enable flash write operation:
00053   WEN = 1;
00054   
00055   // Write the byte directly to the flash. This operation is "self timed" when
00056   // executing from the flash; the CPU will halt until the operation is
00057   // finished:
00058   pb = (uint8_t xdata *)a;
00059   *pb = b;
00060 
00061   // When running from XDATA RAM we need to wait for the operation to finish:
00062   while(RDYN == 1)
00063     ;
00064 
00065   WEN = 0;
00066 
00067   EA = F0; // Restore interrupt enable state
00068 }
00069 
00070 void hal_flash_bytes_write(uint16_t a, uint8_t *p, uint16_t n)
00071 {
00072   uint8_t xdata *data pb;
00073 
00074   // Save interrupt enable state and disable interrupts:
00075   F0 = EA;
00076   EA = 0;
00077 
00078   // Enable flash write operation:
00079   WEN = 1;
00080 
00081   // Write the bytes directly to the flash. This operation is
00082   // "self timed"; the CPU will halt until the operation is
00083   // finished:
00084   pb = (uint8_t xdata *)a;
00085   while(n--)
00086   {
00087     *pb++ = *p++;
00088     //
00089     // When running from XDATA RAM we need to wait for the operation to
00090     // finish:
00091     while(RDYN == 1)
00092       ;
00093   }
00094 
00095   WEN = 0;
00096 
00097   EA = F0; // Restore interrupt enable state
00098 }
00099 
00100 uint8_t hal_flash_byte_read(uint16_t a)
00101 {
00102   uint8_t xdata *pb = (uint8_t xdata *)a;
00103   return *pb;
00104 }
00105 
00106 void hal_flash_bytes_read(uint16_t a, uint8_t *p, uint16_t n)
00107 {  
00108   uint8_t xdata *pb = (uint8_t xdata *)a;
00109   while(n--)
00110   {
00111     *p = *pb;
00112     pb++;
00113     p++;
00114   }
00115 }

你可能感兴趣的:(flash读,写,擦除)