Hi, I use the flash routines from the file download area "XC16x In-Application On-Chip Flash-Programming". This example is for a 20 Mhz CPU clock. My XC164CM- board runs with 40 Mhz. The flash routines doesnt work at 40 Mhz. At my opinion is it enough to change PLLCON from 0x7884 to 0x7889 for 20 Mhz. (UnlockProtReg is also used). My problem is: The first printf-instruction after switch back to 40 Mhz is wrong. The first one or two charcters are not correct. This is undependent from interrupts. Could this be a stack-problem? But the progam is so small. The second problem is thats a little bit more than 64 bytes are written at the flash memory. Has someone expiriences ? Kindly regards Rainer srom.c :
#include <string.h> #include <intrins.h> #include <srom.h> // SROM Handling definitions #include <XC167.h> #include <stdio.h> SROM_PS (PFLASH) // define SROM program segment from PFLASH.A66 #define FLASH_ADR 0xC1F000L // Address in on-chip flash, that is used to write to extern void far SerComm_Init (void); extern void far UnlockProtecReg(void); // Write 64 Bytes to target_adr in Flash Memory extern int far PFlash_Write (void huge *target_adr, void huge *buffer); // Erase the Flash memory sector specified by sector_adr extern int far PFlash_Erase (void huge *sector_adr); unsigned char buf[64] = "This is a string that is written to on-chip flash 22.09.2007"; void main (void) { unsigned char huge *pFLASH_ROM = FLASH_ADR; unsigned char i,x_ch; unsigned long int a = (SROM_PS_TRG(PFLASH)); unsigned long int b = (SROM_PS_SRC(PFLASH)); unsigned long int c = (SROM_PS_LEN(PFLASH)); unsigned short Revision = 0x0; unsigned int myPLLCON_20 = 0x7889; unsigned int myPLLCON_40 = 0x7884; SerComm_Init(); printf("\n\n0x%.8LX:\n",(unsigned long)pFLASH_ROM); for (i=0;i<64;i++) { printf("%c",*pFLASH_ROM); pFLASH_ROM++; } printf("\n"); _bfld_ (PSW, 0xF000, 0xF000); // disable interrupts UnlockProtecReg(); PLLCON = myPLLCON_20; _bfld_ (PSW, 0xF000, 0x0000); // enable interrupts // copy flash program code to execution address hmemcpy (SROM_PS_TRG(PFLASH), SROM_PS_SRC(PFLASH), SROM_PS_LEN(PFLASH)); _bfld_ (PSW, 0xF000, 0xF000); // disable interrupts PFlash_Erase (FLASH_ADR); // Erase sector _bfld_ (PSW, 0xF000, 0x0000); // enable interrupts _nop_(); _bfld_ (PSW, 0xF000, 0xF000); // disable interrupts PFlash_Write (FLASH_ADR, buf);// program 64 bytes _bfld_ (PSW, 0xF000, 0x0000); // enable interrupts _bfld_ (PSW, 0xF000, 0xF000); // disable interrupts UnlockProtecReg(); PLLCON = myPLLCON_40; _bfld_ (PSW, 0xF000, 0x0000); // enable interrupts Revision = IDCHIP & 0x00FF; // Read Chip Revision printf("Hello, back to 40 Mhz!\n"); printf("Revision: %.4X\n",Revision); printf("\nPress key ............."); x_ch = _getkey(); printf("\n"); pFLASH_ROM = FLASH_ADR; for (i=0;i<64;i++) { printf("0x%.8LX: %c(%u)\n",(unsigned long)pFLASH_ROM,*pFLASH_ROM,*pFLASH_ROM); pFLASH_ROM++; } _nop_(); printf("...ready!\n"); while (1);