//search for an empty block starting from the block passed. it returns next block free or EMPTY_VALUE+ERROR if no more blocks avaiable... int8 eepromfs_findEmptyBlock(int8 fromBlock) { int8 aux82; int16 aux161,aux162,aux163; //get INDEX BLOCK START ADDRESS aux161=eepromfs_getAddress(INDEX_BLOCK_START_ADDR); //get INDEX BLOCK END ADDRESS aux162=eepromfs_getAddress(INDEX_BLOCK_END_ADDR); //Get file max quantity(same as block count...) aux162-=aux161; aux162++; //get FREE BLOCK START ADDRESS aux161=eepromfs_getAddress(FREE_BLOCK_START_ADDR); aux161+=fromBlock/8; //initialize block counter... aux163=fromBlock; //find if theres a free block in actual block octet aux82=read_ext_eeprom(aux161); while(aux163%8){ if(bit_test(aux82,aux163%8)) aux163++; else break; } //if theres one, return low byte with block number... if(aux163%8 && aux163<aux162) return aux163; //search for an empty block(8 blocks at a time) while(read_ext_eeprom(aux161)==0xFF && aux163<aux162){ aux163+=8; aux161++; } //get the one who have an empty block aux82=read_ext_eeprom(aux161); //search especific block in the octet... while(bit_test(aux82,0) && aux163<aux162){ aux163++; aux82>>=1; } //no empty blocks avaiable? if(aux163>=aux162){ eepromfs_flag_error|=ERR_OUT_OF_SPACE; return EEPROM_EMPTY_VALUE; } //return low byte of var witch contains block number... return aux163; }
unsigned int16 eepromfs_getAddress(unsigned int16 varPos) { int8 aux81,aux82; aux82=read_ext_eeprom(varPos); //CCS buggy! aux81=read_ext_eeprom(varPos+1); //CCS buggy! aux82=read_ext_eeprom(varPos); //reals...:( aux81=read_ext_eeprom(varPos+1); //reals...:( return make16(aux82,aux81); }
int8 master_macro_aws(int16 step){ int16 addr; int8 macro_cmd[2]; int16 macro_arg[2]; int8 macro_end; --step; addr = macro_address[0] + (step*6); init_ext_eeprom(); // port macro_cmd[0] = read_ext_eeprom(addr); ++addr; macro_arg[0] = read16_ext_eeprom(addr); ++addr; ++addr; // macro macro_cmd[1] = read_ext_eeprom(addr); ++addr; macro_arg[1] = read16_ext_eeprom(addr); ++addr; ++addr; macro_end = read_ext_eeprom(addr); // check basic structure if (macro_cmd[0] == 'P' && macro_cmd[1] == 'M') { RTC_read(); RTC_display(); fprintf(COM_A, "%c%Lu,%c%Lu\r\n", macro_cmd[0], macro_arg[0], // port macro_cmd[1], macro_arg[1]); // macro if (macro_arg[1] > 0 && macro_arg[1] < 17) { play_macro(macro_arg[1], macro_arg[0]); // [1] casts to int8 / [0] = port } } else { cmd_err(); macro_end = 59; } return (macro_end); }
short eepromfs_setBlockIdentifiers(int8 blockNmr, struct blockIdentifiers * identIn) { int8 aux81,aux82; int16 aux161,aux162,aux163; aux82=blockNmr; //get FREE BLOCK START ADDRESS aux161=eepromfs_getAddress(FREE_BLOCK_START_ADDR); while(aux82>7) { aux161++; aux82-=8; } //get 8 block info... aux81=read_ext_eeprom(aux161); if((*identIn).state) bit_set(aux81,aux82); else bit_clear(aux81,aux82); //write result... write_ext_eeprom(aux161,aux81); //get BLOCK SIZE aux162=eepromfs_getAddress(BLOCK_SIZE_ADDR); //calculate selected empty block address... aux163=eepromfs_getBlockAddress(blockNmr); //data block start address + (block size * empty block finded) //add real block size... //aux163+=aux162-BLOCK_IDENTIFIER_SIZE; //jump to identifiers zone write_page_ext_eeprom(aux163+aux162-BLOCK_IDENTIFIER_SIZE,&(*identIn).control,BLOCK_IDENTIFIER_SIZE); return TRUE; }
int16 read16_ext_eeprom(int16 addr) { int i; int16 data; for (i = 0; i < 2; i++) *((int8*)&data + i) = read_ext_eeprom(i + addr); return(data); }
void eeprom_test() { int8 data; int8 wrote; int32 addr; int16 errors = 0; init_ext_eeprom(); // Fill eeprom with random data. printf("\n\r"); printf("writing"); // comment out for PIC16 srand(0x55); for(addr = 0; addr < EEPROM_SIZE; addr++) { write_ext_eeprom(addr, (int8)rand()); //comment out above and use line below for PIC16 //write_ext_eeprom(addr, 0x88); if((int8)addr == 0) putc('.'); } // Read the eeprom and check for errors. printf("\n\r"); printf("reading"); // comment out for PIC16 srand(0x55); for(addr = 0; addr < EEPROM_SIZE; addr++) { data = read_ext_eeprom(addr); wrote = (int8)rand(); //comment out above and use line below for PIC16 //wrote = 0x88; if(data != wrote) { printf("%lx: read %x, should be %x\n\r", addr, data, wrote); errors++; if(errors >= 10) break; } if((int8)addr == 0) putc('.'); } output_low(EEP_WP); printf("\n\r"); printf("done\n\r"); }
char eepromfs_fileCount(void) { char count; int16 aux161,aux162; aux161=eepromfs_getAddress(INDEX_BLOCK_START_ADDR); aux162=eepromfs_getAddress(INDEX_BLOCK_END_ADDR); count=0; while(aux161<=aux162){ if(read_ext_eeprom(aux161)!=EEPROM_EMPTY_VALUE) count++; aux161++; } return count; }
///////////////////////////////////////////////// //AUXILIAR FUNCTIONS... ///////////////////////////////////////////////// int16 eepromfs_fileCalculatePos(int8 fileNmr, int16 position, int8 * parentBlockNmr) { int8 aux83; int16 aux161,blockSize; struct blockIdentifiers blkIdent; aux161=eepromfs_getAddress(INDEX_BLOCK_START_ADDR); aux161+=fileNmr; //read first block number of file... aux83=read_ext_eeprom(aux161); blockSize=eepromfs_getAddress(BLOCK_SIZE_ADDR); //get real block size... blockSize-=BLOCK_IDENTIFIER_SIZE; //get file first block info... eepromfs_getBlockIdentifiers(aux83,&blkIdent); //if file continue in other block, continue calculating position... while(bit_test(blkIdent.control,7) && position>blockSize) { //get next block info... eepromfs_getBlockIdentifiers(aux83,&blkIdent); //if next block, point to next block... if(bit_test(blkIdent.control,7)){ aux83=blkIdent.control2; //substract an entire block :) position-=blockSize; } } //fill parent block with block Number... *parentBlockNmr=aux83; //we're reaching to the end of the search...lets calculate final position... //first, go to last data block address aux161=eepromfs_getBlockAddress(aux83); //and add remaining bytes... aux161+=position; //return with address return aux161; }
int main() { char arry[255]={0}; mJTAGPortEnable(0); lcd_config(); lcd_ini(); chip_select(); //select the slavehhjhjkhk unsigned short address=0x0000; unsigned char da_ta=0x20; unsigned char loc=0x80; for(i=0x0000,n=0;n<255;i++,n++) { lcd_ini(); arry[n]=read_ext_eeprom(i); } }
int16 eepromfs_fileSize(int8 fileNmr) { int8 aux83; int16 aux161,aux162,aux163; struct blockIdentifiers blkIdent; eepromfs_flag_error=ERR_NO_ERRORS; if(!eepromfs_fileExists(fileNmr)) return 0; //get block size info... aux162=eepromfs_getAddress(BLOCK_SIZE_ADDR); //write real block size... aux162-=BLOCK_IDENTIFIER_SIZE; //we will first, go to file block info... aux161=eepromfs_getAddress(INDEX_BLOCK_START_ADDR); aux161+=fileNmr; //read first block number of file... aux83=read_ext_eeprom(aux161); //init counter... aux163=0; do{ //get block info... eepromfs_getBlockIdentifiers(aux83,&blkIdent); //point to next block... aux83=blkIdent.control2; //if file continue in other block, continue blocks... //add block bytes to size if(bit_test(blkIdent.control,7)) aux163+=aux162; else aux163+=blkIdent.control2; }while(bit_test(blkIdent.control,7)); //return with file size return aux163; }
short eepromfs_fileExists(int8 fileNmr) { int16 aux161; eepromfs_flag_error=ERR_NO_ERRORS; if(!eepromfs_fileInBounds(fileNmr)) return FALSE; //get INDEX BLOCK START ADDRESS aux161=eepromfs_getAddress(INDEX_BLOCK_START_ADDR); //point to eeprom position were file index should be aux161+=fileNmr; //if already exists, return FALSE... if(read_ext_eeprom(aux161)!=EEPROM_EMPTY_VALUE){ eepromfs_flag_error|=ERR_FILE_ALREADY_EXISTS; return TRUE; }else{ eepromfs_flag_error|=ERR_FILE_DOESNT_EXISTS; return FALSE; } }
int16 eepromfs_freeSpace(void){ int16 aux161,aux162,aux163; char frees,aux,cont; //get INDEX BLOCK START ADDRESS aux162=eepromfs_getAddress(INDEX_BLOCK_START_ADDR); //get INDEX BLOCK END ADDRESS aux161=eepromfs_getAddress(INDEX_BLOCK_END_ADDR); //Get file max quantity(same as block count...) aux161-=aux162; aux161++; //get block size... aux162=eepromfs_getAddress(BLOCK_SIZE_ADDR); aux162-=BLOCK_IDENTIFIER_SIZE; //get FREE BLOCK START ADDRESS aux163=eepromfs_getAddress(FREE_BLOCK_START_ADDR); frees=0; while(1){ aux=read_ext_eeprom(aux163++); for(cont=0;cont<8;cont++){ //if no more blocks, exit while... if(!aux161) break; if(!bit_test(aux,0)) frees++; //rotate 1 bit to the right aux/=2; aux161--; } if(!aux161) break; } return (int16)frees*aux162; }
short eepromfs_getBlockIdentifiers(int8 blockNmr, struct blockIdentifiers * identOut) { int8 aux81,aux82; int16 aux161,aux162; //if block number is empty value, return with fail... if(blockNmr==EEPROM_EMPTY_VALUE) return FALSE; //get FREE BLOCK START ADDRESS aux161=eepromfs_getAddress(FREE_BLOCK_START_ADDR); aux82=blockNmr; while(aux82>7) { aux161++; aux82-=8; } //get 8 block info... aux81=read_ext_eeprom(aux161); //and set pointer according to free block value if(bit_test(aux81,aux82)) (*identOut).state=TRUE; else (*identOut).state=FALSE; //now, lets read block control info... aux162=eepromfs_getAddress(BLOCK_SIZE_ADDR); //go to block data init... aux161=eepromfs_getBlockAddress(blockNmr); //add bytes to get control address aux161+=(aux162-BLOCK_IDENTIFIER_SIZE); //read control & control2 info... read_page_ext_eeprom(aux161,&(*identOut).control,BLOCK_IDENTIFIER_SIZE); return TRUE; }
//Deletes a file. If files is out of bounds or doesnt exists returns with flag errors short eepromfs_fileRemove(int8 fileNmr) { struct blockIdentifiers blkIdentSet,blkIdentGet; int8 aux83; int16 aux161; //Check that file number is valid & that it exists... if(!eepromfs_fileExists(fileNmr)) return FALSE; //we will first, go to file block info... aux161=eepromfs_getAddress(INDEX_BLOCK_START_ADDR); aux161+=fileNmr; //read first block number of file... aux83=read_ext_eeprom(aux161); //delete this file INDEX BLOCK info... write_ext_eeprom(aux161,EEPROM_EMPTY_VALUE); do{ //get block info... eepromfs_getBlockIdentifiers(aux83,&blkIdentGet); //free this block!!! blkIdentSet.state=0; blkIdentSet.control=0; blkIdentSet.control2=0; eepromfs_setBlockIdentifiers(aux83,&blkIdentSet); //point to next block to erase... aux83=blkIdentGet.control2; //if file continue in other block, continue deleting blocks... }while(bit_test(blkIdentGet.control,7)); //Success! return TRUE; }