//============================================================================= // routine to restore setup data // //============================================================================= int restore_setup( void ) { int size = sizeof(pid); int *dptr = (int *)&pid; int res = 0; int offset = 0; // this routine attempts to read the entire calibration structure // into the ram on board. // read 16 words of structure at a time while (size > 0) { res = ReadEE(__builtin_tblpage(&pidEE), __builtin_tbloffset(&pidEE)+offset, dptr, ROW); if (res) printf("%d read from eeprom failed at offset %d\r\n", res,offset); offset += ROW*2; // bump offset to destination 32 bytes up dptr += ROW; // bump source ptr up 16 words size -= ROW*2; // 16 words or 32 bytes/write } return res; }
WORD read_freq(void) { WORD offset; TBLPAG = __builtin_tblpage(&dat); offset = __builtin_tbloffset(&dat); return(__builtin_tblrdl(offset)); }
void write_freq(WORD newData) { WORD offset; NVMCON = 0x4058; TBLPAG = __builtin_tblpage(&dat); offset = __builtin_tbloffset(&dat); __builtin_tblwtl(offset,0); asm volatile("disi #5"); __builtin_write_NVM(); while(NVMCONbits.WR == 1); NVMCON = 0x4004; TBLPAG = __builtin_tblpage(&dat); offset = __builtin_tbloffset(&dat); __builtin_tblwtl(offset,newData); asm volatile("disi #5"); __builtin_write_NVM(); while(NVMCONbits.WR == 1); }
//============================================================================= // Routine to save setup structure into eeprom // //============================================================================= int save_setup( void ) { int size = sizeof(pid); int *sptr = (int *)&pid; int res; int offset = 0; // compute correct checksum for upper part of array // and place it in the checsum variable pid.cksum = -calc_cksum((sizeof(pid)-sizeof(int))/sizeof(int), (int*)&pid); // this routine attempts to write the entire ram setup structure // into the eeprom on board. // write 16 words of structure at a time while (size > 0) { // Erase 16 words (1 row in dsPIC30F DataEEPROM) in Data EEPROM // from calEE structure res = EraseEE(__builtin_tblpage(&pidEE), __builtin_tbloffset(&pidEE)+offset, ROW); if (res) printf("clr of eeprom failed at %d\r\n",offset); res = WriteEE(sptr, __builtin_tblpage(&pidEE), __builtin_tbloffset(&pidEE)+offset, ROW); if (res) printf("write to eeprom failed at offset %d\r\n",offset); offset += ROW*2; // bump offset to destination 32 bytes up sptr += ROW; // bump source ptr up 16 words size -= ROW*2; // 16 words or 32 bytes/write } return res; }