void sysRead(uint8_t *dest, uint16_t addr, uint16_t count) { uint8_t oldRFIE = RFIE; uint16_t PageCounter = 0; RFIE = 0; do { EES_nCS = 0; MacroNop(); EESPIPut(SPI_RD_STATUS); PageCounter = EESPIGet(); EES_nCS = 1; MacroNop(); } while(PageCounter & 0x01 ); EES_nCS = 0; EESPIPut(SPI_READ); EESPIPut(0x01); // allow system settings for second bank EESPIPut(addr >> 8); EESPIPut(addr); while( count > 0 ) { *dest++ = EESPIGet(); count--; } EES_nCS = 1; RFIE = oldRFIE; }
void sysErase() { uint32_t addr = 0; uint32_t count = 0x20000; uint16_t PageCounter = 0; uint8_t oldRFIE = RFIE; RFIE = 0; EEPROM_NEXT_PAGE: do { EES_nCS = 0; MacroNop(); EESPIPut(SPI_RD_STATUS); PageCounter = EESPIGet(); EES_nCS = 1; MacroNop(); } while(PageCounter & 0x01 ); EES_nCS = 0; MacroNop(); EESPIPut(SPI_EN_WRT); EES_nCS = 1; MacroNop(); EES_nCS = 0; MacroNop(); EESPIPut(SPI_WRITE); EESPIPut(addr >> 16); // allow system settings for second bank EESPIPut(addr >> 8); EESPIPut(addr); PageCounter = 0; while( count > 0 ) { EESPIPut(0xFF); count--; PageCounter++; if( ((addr + PageCounter) & (NVM_PAGE_SIZE-1)) == 0 ) { EES_nCS = 1; addr += PageCounter; goto EEPROM_NEXT_PAGE; } } EES_nCS = 1; RFIE = oldRFIE; }
void NVMWrite(BYTE *src, ROM BYTE* dest, WORD count) { ROM char *pEraseBlock; static BYTE memBlock[ERASE_BLOCK_SIZE]; BYTE *pMemBlock; BYTE writeIndex; BYTE writeStart; BYTE writeCount; BYTE oldGIEH; DWORD oldTBLPTR; #if defined(VERIFY_WRITE) while( memcmppgm2ram( src, (MEM_MODEL ROM void *)dest, count)) #elif defined(CHECK_BEFORE_WRITE) if (memcmppgm2ram( src, (MEM_MODEL ROM void *)dest, count )) #endif { // First of all get nearest "left" erase block boundary pEraseBlock = (ROM char*)((long)dest & (long)(~(ERASE_BLOCK_SIZE-1))); writeStart = (BYTE)((BYTE)dest & (BYTE)(ERASE_BLOCK_SIZE-1)); while( count ) { // Now read the entire erase block size into RAM. NVMRead(memBlock, (far ROM void*)pEraseBlock, ERASE_BLOCK_SIZE); // Erase the block. // Erase flash memory, enable write control. EECON1 = 0x94; oldGIEH = INTCONbits.GIEH; INTCONbits.GIEH = 0; #if defined(__18CXX) TBLPTR = (unsigned short long)pEraseBlock; #endif EECON2 = 0x55; EECON2 = 0xaa; EECON1bits.WR = 1; MacroNop(); EECON1bits.WREN = 0; oldTBLPTR = TBLPTR; INTCONbits.GIEH = oldGIEH; // Modify 64-byte block of RAM buffer as per what is required. pMemBlock = &memBlock[writeStart]; while( writeStart < ERASE_BLOCK_SIZE && count ) { *pMemBlock++ = *src++; count--; writeStart++; } // After first block write, next start would start from 0. writeStart = 0; // Now write entire 64 byte block in one write block at a time. writeIndex = ERASE_BLOCK_SIZE / WRITE_BLOCK_SIZE; pMemBlock = memBlock; while( writeIndex ) { oldGIEH = INTCONbits.GIEH; INTCONbits.GIEH = 0; TBLPTR = oldTBLPTR; // Load individual block writeCount = WRITE_BLOCK_SIZE; while( writeCount-- ) { TABLAT = *pMemBlock++; //TBLWTPOSTINC(); _asm tblwtpostinc _endasm } // Start the write process: reposition tblptr back into memory block that we want to write to. #if defined(__18CXX) _asm tblrdpostdec _endasm #endif // Write flash memory, enable write control. EECON1 = 0x84; EECON2 = 0x55; EECON2 = 0xaa; EECON1bits.WR = 1; MacroNop(); EECON1bits.WREN = 0; // One less block to write writeIndex--; TBLPTR++; oldTBLPTR = TBLPTR; INTCONbits.GIEH = oldGIEH; } // Go back and do it all over again until we write all // data bytes - this time the next block. #if !defined(WIN32) pEraseBlock += ERASE_BLOCK_SIZE; #endif } }
void NVMWrite(BYTE *source, WORD addr, WORD count) { BYTE PageCounter = 0; //BYTE i; #if defined(__18CXX) BYTE oldGIEH = INTCONbits.GIEH; INTCONbits.GIEH = 0; #else BYTE oldRFIE = RFIE; RFIE = 0; #endif EEPROM_NEXT_PAGE: do { EE_nCS = 0; EESPIPut(SPI_RD_STATUS); PageCounter = EESPIGet(); EE_nCS = 1; MacroNop(); } while(PageCounter & 0x01 ); EE_nCS = 0; EESPIPut(SPI_EN_WRT); EE_nCS = 1; MacroNop(); EE_nCS = 0; #if MCHP_EEPROM < MCHP_4KBIT EESPIPut(SPI_WRITE); EESPIPut(addr); #elif MCHP_EEPROM == MCHP_4KBIT if( addr > 0xFF ) { EESPIPut(SPI_WRITE | 0x08); } else { EESPIPut(SPI_WRITE); } EESPIPut(addr); #elif MCHP_EEPROM < MCHP_1MBIT EESPIPut(SPI_WRITE); EESPIPut(addr>>8); EESPIPut(addr); #endif PageCounter = 0; while( count > 0 ) { EESPIPut(*source++); count--; PageCounter++; if( ((addr + PageCounter) & (NVM_PAGE_SIZE-1)) == 0 ) { EE_nCS = 1; addr += PageCounter; goto EEPROM_NEXT_PAGE; } } EE_nCS = 1; #if defined(__18CXX) INTCONbits.GIEH = oldGIEH; #else RFIE = oldRFIE; #endif }