/** @brief Program Command * * This will write 1 word to an empty(0xFFFF) flash address. If you try to * write to an address containing data(not 0xFFFF),an error will register at * FSTAT. The embedded algorithm works like this, just write to the desired * flash address as you would any other writable address. Then register the * program command(0x20) at FCDM, the rest is handled by StackBurner(); * * @author Sean Keys * * @warning Be sure your destination address is not protected or you will flag an error in FSTAT * * @param flashDestination where you want to write your data * @param data the data you are going to write * * @return an error code. Zero means success, anything else is a failure. */ unsigned short writeWord(unsigned short* flashDestination, unsigned short data){ if((unsigned short)flashDestination & 0x0001){ return addressNotWordAligned; } FSTAT=(ACCERR | PVIOL); *flashDestination = data; FCMD = WORD_PROGRAM; //Load Flash Command Register With Word_Program mask StackBurner(); return 0; }
/** @brief Program Command * * This will write 1 word to an empty(0xFFFF) flash address. If you try to * write to an address containing data(not 0xFFFF),an error will register at * FSTAT. The embedded algorithm works like this, just write to the desired * flash address as you would any other writable address. Then register the * program command(0x20) at FCDM, the rest is handled by StackBurner(); * * @author Sean Keys * * @warning Be sure your destination address is not protected or you will flag an error in FSTAT * * @param flashDestination where you want to write your data * @param data the data you are going to write * * @return an error code. Zero means success, anything else is a failure. */ unsigned short writeWord(unsigned short* flashDestination, unsigned short data){ if((unsigned short)flashDestination & 0x0001){ return ADDRESS_NOT_WORD_ALIGNED; } FSTAT=(ACCERR | PVIOL); *flashDestination = data; FCMD = WORD_PROGRAM; //Load Flash Command Register With Word_Program mask StackBurner(); // @todo TODO verify the write? necessary?? return 0; }
/** @brief Erases a sector of flash memory * * This will erase a 1k sector in flash. Write 0xFFFF to the starting sector * to be erased, 0xFFFF will be written regardless. Register the flash sector * erase command(0x40) and call StackBurner();. If you try to erase a protected * sector you will get PVIOL in the FSTAT register. * * @author Sean Keys * * @warning This will erase an entire 1k block starting at flashAddr * * @param PPage the flash page the sector is in * @param flashAddr the start address of the sector * * @return An error code. Zero means success, anything else is a failure. */ unsigned short eraseSector(unsigned char PPage, unsigned short *flashAddr){ if (((unsigned short)flashAddr % flashSectorSize) != 0){ return addressNotSectorAligned; } unsigned char currentPage = PPAGE; PPAGE = PPage; FSTAT = (PVIOL|ACCERR); /* clear any errors */ (*flashAddr) = 0xFFFF; /* Dummy data to first word of page to be erased it will write FFFF regardless with the erase command*/ PPAGE = currentPage; FCMD = SECTOR_ERASE; /* set the flash command register mode to ERASE */ StackBurner(); //PPAGE loaded into Register B, PPAGE is set with Reg B in StackBurn asm file //TODO add return for accerr and pviol error bits return 0; }
/** @brief Erases a sector of flash memory * * This will erase a 1k sector in flash. Write 0xFFFF to the starting sector * to be erased, 0xFFFF will be written regardless. Register the flash sector * erase command(0x40) and call StackBurner();. If you try to erase a protected * sector you will get PVIOL in the FSTAT register. * * @author Sean Keys * * @warning This will erase an entire 1k block starting at flashAddr * * @param PPage the flash page the sector is in * @param flashAddr the start address of the sector * * @return An error code. Zero means success, anything else is a failure. */ unsigned short eraseSector(unsigned char PPage, unsigned short *flashAddr){ if(((unsigned short)flashAddr % FLASHSECTORSIZE) != 0){ return ADDRESS_NOT_SECTOR_ALIGNED; } unsigned char currentPage = PPAGE; PPAGE = PPage; FSTAT = (PVIOL|ACCERR); /* clear any errors */ (*flashAddr) = 0xFFFF; /* Dummy data to first word of page to be erased it will write FFFF regardless with the erase command*/ PPAGE = currentPage; FCMD = SECTOR_ERASE; /* set the flash command register mode to ERASE */ StackBurner(); //PPAGE loaded into Register B, PPAGE is set with Reg B in StackBurn asm file //TODO add return for accerr and pviol error bits // @todo TODO verify the erase, is this necessary or is it taken care of by the hardware?? return 0; }