The IAP is driving me crazy. When I single step my code using ulink it works, but when I just let it run the micro reboots itself and the IAP hasn't worked. I've read the manual, AN10256, searched the forums and looked at the sample code.
Here's my code:
ErrorCode_t writeBufToOneFlashSector(uint8_t secNum) { BOOL good = TRUE; ErrorCode_t retCode = ERR_NO_ERROR; unsigned int enables; BOOL pllStat; // Disable interrupts during erase and write operations // Keep a copy of what was enabled enables = VICIntEnable; command[0] = IAP_PrepareSectors; command[1] = (uint32_t)secNum; command[2] = (uint32_t)secNum; waitTXComplete(); pauseTX(TRUE); pllStat = usePLL(FALSE); iap_entry(command, result); good = (BOOL)(IAP_CMD_SUCCESS == result[0]); if(!good) { usePLL(pllStat); retCode = ERR_IAP_ERASE_PREPARE | ERR_NUM_USED; errVal = (ParamType)result[0]; } else { command[0] = IAP_EraseSectors; command[1] = (uint32_t)secNum; command[2] = (uint32_t)secNum; command[3] = XTAL_OSC_KHZ; // Disable interrupts during erase VICIntEnable = 0; iap_entry(command, result); VICIntEnable = enables; good = (BOOL)(IAP_CMD_SUCCESS == result[0]); if(!good) { usePLL(pllStat); retCode = ERR_IAP_ERASE | ERR_NUM_USED; errVal = (ParamType)result[0]; } } // See about doing the write if(good) { command[0] = IAP_PrepareSectors; command[1] = (uint32_t)secNum; command[2] = (uint32_t)secNum; iap_entry(command, result); good = (BOOL)(IAP_CMD_SUCCESS == result[0]); if(!good) { usePLL(pllStat); retCode = ERR_IAP_WRITE_PREPARE | ERR_NUM_USED; errVal = (ParamType)result[0]; } } // Ready to write at last if(good) { command[0] = IAP_CopyRAMtoFlash; command[1] = SectorStartAddr[secNum]; command[2] = (unsigned int)nvBuf; command[3] = FLASH_NV_PAGE_SIZE8; command[4] = XTAL_OSC_KHZ; // Disable interrupts during erase VICIntEnable = 0; iap_entry(command, result); usePLL(pllStat); VICIntEnable = enables; good = (BOOL)(IAP_CMD_SUCCESS == result[0]); if(!good) { retCode = ERR_IAP_WRITE | ERR_NUM_USED; errVal = (ParamType)result[0]; } } pauseTX(FALSE); return retCode; }