int main() { char ascii_buffer[17]; char fbyte, inchar; int i, j, k, pagenum, value, start, end; char linebuf[80], *p, *buf, ch; brdInit(); sfspi_init(); if (sf_init()) { printf("Flash init failed\n"); exit(-1); } else { printf("Flash init OK\n"); printf("# of blocks: %d\n", sf_blocks); printf("size of block: %d\n\n", sf_blocksize); } print_command(); while(1) { inchar = getchar(); if(inchar == 'c') { printf("page number to clear?"); pagenum = input_number(); if(pagenum >= 0 && pagenum < sf_blocks) { printf("\nClearing page %d\n", pagenum); memset (flash_buf, 0, sf_blocksize); sf_writeRAM(flash_buf, 0, sf_blocksize); sf_RAMToPage(pagenum); } else { printf("ERROR: invalid page number\n"); } } //end if(inchar =='c') else if((inchar == 'p') || (inchar == 'r')) { if (inchar == 'p') { // Use start for page to print printf("Page number to print out?"); start = input_number(); // Check that it is a valid page if(start < 0 || start >= sf_blocks) { printf("Invalid page number.\n\n"); continue; } // Set single page range for 'p' command end = start; } else { printf("Starting page number to print out?"); start = input_number(); // Check that it is a valid page if(start < 0 || start >= sf_blocks) { printf("Invalid page number.\n\n"); continue; } printf("\nEnding page number to print out?"); end = input_number(); if(end < start || end >= sf_blocks) { printf("Invalid page number.\n\n"); continue; } } // Loop through range of pages (range of 1 page for 'p' command) for (pagenum = start; pagenum <= end; pagenum++) { printf("\nPage %d", pagenum); sf_pageToRAM(pagenum); sf_readRAM(flash_buf, 0, sf_blocksize); // Test if entire buffer filled with a single value buf = flash_buf; for (j = k = 0, ch = *buf; j < 512; j++) { if(ch != *buf++) { k = 1; break; } } // See if page is all the same value if (k) { printf("\n"); // No, drop through to print data } else { // Yes, print out message instead printf(" ALL = 0x%02x\n", ch); continue; } k = (sf_blocksize & 0xFFF0) + ((sf_blocksize & 0x000F) ? 16 : 0); ascii_buffer[16] = 0; for (j = 0, buf = flash_buf; j < k; j++) { if (j % 16 == 0) { p = linebuf; p += sprintf (p, "%04x: ", j); } fbyte = *buf++; if (j >= sf_blocksize) { p += sprintf (p, " "); ascii_buffer[j % 16] = ' '; } else { p += sprintf (p, "%02x ", fbyte); ascii_buffer[j % 16] = isprint (fbyte) ? fbyte : '.'; } if (j % 16 == 15) { printf ("%s %s\n", linebuf, ascii_buffer); } } } } // end if((inchar =='p') || (inchar == 'r')) else if(inchar == 'f') { printf("page number to fill with specified value? "); pagenum = input_number(); if(pagenum >= 0 && pagenum < sf_blocks) { printf("\nPage %d\n", pagenum); printf("enter fill value " ); value = input_number(); printf("\nValue is %d dec is %02x hex", value,value); printf("\nFilling page %d with value %02x hex\n", pagenum,value); memset (flash_buf, value, sf_blocksize); sf_writeRAM(flash_buf, 0, sf_blocksize); sf_RAMToPage(pagenum); } else { printf("ERROR: invalid page number\n"); } } // end of if(inchar == 'f') else if(inchar == 't') { printf("page number in which to write text? "); pagenum = input_number(); if(pagenum >= 0 && pagenum < sf_blocks) { printf("\nPage %d\n", pagenum); printf("enter character string followed by RETURN \n" ); gets (flash_buf); printf("Storing the following text ==> %s \n",flash_buf); sf_writeRAM(flash_buf, 0, sf_blocksize); sf_RAMToPage(pagenum); } else { printf("ERROR: invalid page number\n"); } } // end of if(inchar == 't') print_command(); } // end of while }
int init_log(void) { auto int err, i, j; auto int found_start, found_end; auto log_block_header header; sfspi_init(); if (err = sf_initDevice(&my_dev, SF_SPI_CSPORT, &SF_SPI_CSSHADOW, SF_SPI_CSPIN)) { printf("\nERROR - Serial Flash init failed (%d)\n", err); return err; } printf("\nSerial Flash Initialized\n"); printf("# of blocks: %d\n", my_dev.pages); printf("size of blocks: %d\n", my_dev.pagesize); //calculate space for data in a block log_block_datasize = my_dev.pagesize - sizeof (log_block_header); //find beginning of log (if it exists) found_start = 0; for (i = 0; i < my_dev.pages; ++i) { sf_readPage(&my_dev, 1, i); sf_readDeviceRAM(&my_dev, paddr(&header), 0, sizeof header, SF_RAMBANK1); if (!strncmp(header.marker_string, "SLOG", 4)) { log_start_block = i; printf("Found start at block %d\n", i); found_start = 1; break; } } if (found_start) { //look for end found_end = 0; for (i = 0; i < my_dev.pages; ++i) { j = (i + log_start_block) % (int) my_dev.pages; sf_readPage(&my_dev, 1, j); sf_readDeviceRAM(&my_dev, paddr(&header), 0, sizeof header, SF_RAMBANK1); if (!strncmp(header.marker_string, "ELOG", 4)) { log_end_block = j; printf("Found end at block %d\n", j); found_end = 1; break; } } if (!found_end) { log_end_block = log_start_block; //no end found, must be first block } } else { //no start found, begin new log at block 0 memcpy(header.marker_string, "SLOG", 4); header.datalen = 0; sf_writeDeviceRAM(&my_dev, paddr(&header), 0, sizeof header, SF_RAMBANK1); sf_writePage(&my_dev, 1, 0); log_start_block = 0; log_end_block = 0; } return 0; //init OK }
int init_log() { int err, i, j; int found_start, found_end; log_block_header header; sfspi_init(); if(err = sf_init()) { printf("ERROR - Serial Flash init failed\n"); return err; } printf("Serial Flash Initialized\n"); printf("# of blocks: %d\n", sf_blocks); printf("size of blocks: %d\n", sf_blocksize); //calculate space for data in a block log_block_datasize = sf_blocksize - sizeof(log_block_header); //find beginning of log(if it exists) found_start = 0; for(i = 0;i < sf_blocks;i++) { sf_pageToRAM(i); sf_readRAM((char*)&header, 0, sizeof(header)); if(!strncmp(header.marker_string, "SLOG", 4)) { log_start_block = i; printf("Found start at block %d\n", i); found_start = 1; break; } } if(found_start) { //look for end found_end = 0; for(i = 0;i < sf_blocks;i++) { j = (i + log_start_block) % (int)sf_blocks; sf_pageToRAM(j); sf_readRAM((char *)&header, 0, sizeof(header)); if(!strncmp(header.marker_string, "ELOG", 4)) { log_end_block = j; printf("Found end at block %d\n", j); found_end = 1; break; } } if(!found_end) { log_end_block = log_start_block; //no end found, must be first block } } else { //no start found, begin new log at block 0 memcpy(header.marker_string, "SLOG", 4); header.datalen = 0; sf_writeRAM((char *)&header, 0, sizeof(header)); sf_RAMToPage(0); log_start_block = 0; log_end_block = 0; } return 0; //init OK }