void PrNetRomManager::printPage(int page) {
  Serial.println(page);
  data *p = (data*)ADDRESS_OF_PAGE(page);
  for(int i = 0; i < MAX_ROWS; i++) {
    printf("%d\n", p->data[i]);
  }
}
Beispiel #2
0
bool storer_test()
{    
    storerMode = STORER_INIT;
    debug_log("Erasing first page...\r\n");
    erasePageOfFlash(FIRST_PAGE);
    while (flashWorking);
    
    debug_log("Writing a value to flash...\r\n");
    uint32_t* addr = ADDRESS_OF_PAGE(FIRST_PAGE);
    uint32_t value = 0x42;
    writeBlockToFlash(addr, &value, 1);  // write one word to FLASH
    while (flashWorking);
    
    uint32_t readVal1 = *addr;
    uint32_t readVal2 = *(addr+1);
    
    if (readVal1 != value)  {
        debug_log("***Read wrong value?  Wrote 0x%X, read 0x%X.\r\n",(unsigned int)value,(unsigned int)readVal1);
        return false;
    }
    if (readVal2 != 0xffffffffUL)  {
        debug_log("***Not erased?  Read 0x%X from erased memory.\r\n",(unsigned int)readVal2);
        return false;
    }
    return true;
}
struct prnet_config PrNetRomManager::loadConfig() {
  prnet_config *p = (prnet_config*)ADDRESS_OF_PAGE(SETTINGS_FLASH_PAGE);
  struct prnet_config a;
  a._curflashpage = p->_curflashpage;
  a._deviceID = p->_deviceID;
  a._pagecounter = p->_pagecounter;
  return a;
}
int PrNetRomManager::writePartialPage(int page, struct data values) {
  data *p = (data*) ADDRESS_OF_PAGE(page);
  int rc = flashWriteBlock(p, &values, sizeof(values));
  if( rc == 0) {
    return 0;
  } else {
    return rc;
  }
}
int PrNetRomManager::updateConfig(struct prnet_config c) {
  data *p = (data*) ADDRESS_OF_PAGE(SETTINGS_FLASH_PAGE);
  return flashWriteBlock(p, &c, sizeof(c));
}
void PrNetRomManager::loadPage(int page) {
  data *p = (data*)ADDRESS_OF_PAGE(page);
  for(int i = 0; i < MAX_ROWS; i++) {
    table.data[i] = p->data[i];
  }
}
Beispiel #7
0
void storer_init()
{
    
    storerMode = STORER_INIT;
    store.from = 0;
    store.to = 0;
    
    debug_log("-storer initialization-\r\n");
    
    unsigned long lastStoredTime = MODERN_TIME;
    
    for (int c = 0; c <= LAST_FLASH_CHUNK; c++)  {
        mic_chunk_t* chunkPtr = (mic_chunk_t*)ADDRESS_OF_CHUNK(c);

        unsigned long timestamp = chunkPtr->timestamp;
        unsigned long check = chunkPtr->check;
        
        //is the timestamp possibly valid?
        if (timestamp < FUTURE_TIME && timestamp > MODERN_TIME)  { 
            //is it a completely stored chunk?
            if (timestamp == check || check == CHECK_TRUNC)  { 
                //is it later than the latest stored one found so far?
                if (timestamp > lastStoredTime)  { 
                    store.to = c; //keep track of latest stored chunk
                    lastStoredTime = timestamp;
                }
            }
        }
        
    }
    
    if (lastStoredTime == MODERN_TIME)  {  // no valid chunk found
        debug_log("  No stored chunks found. Will start from chunk 0.\r\n");
        store.to = LAST_FLASH_CHUNK;    // will advance to chunk 0 below.
    }
    else  {
        debug_log("  Last stored chunk: %d at time: 0x%lX\r\n", store.to, lastStoredTime);
        //printStorerChunk(store.to);
    }
    
    
    debug_log("  Initializing FLASH for storage...\r\n");
    
    int newChunk = (store.to < LAST_FLASH_CHUNK) ? store.to+1 : 0;  // next chunk in FLASH (first unused chunk)
    
    unsigned char oldPage = PAGE_OF_CHUNK(store.to);
    unsigned char newPage = PAGE_OF_CHUNK(newChunk);
    
    // If new chunk is on a new page, we can just erase the whole page.
    if (oldPage != newPage)  {
        debug_log("    erase pg%d.\r\n",(int)newPage);
        nrf_delay_ms(20);
        erasePageOfFlash(newPage);
        while(flashWorking);
    }
    // If new chunk is on same page as old data, we need to erase while preserving the old data
    else  {
        uint32_t* oldChunkAddr = ADDRESS_OF_CHUNK(store.to);   // address of latest stored data
        uint32_t* pageAddr = ADDRESS_OF_PAGE(newPage); // address of beginning of current page
        
        int bytesToErase = (int)(oldChunkAddr) - (int)(pageAddr); // how many bytes of current page we need to erase
        int chunksToSave = (BYTES_PER_PAGE - bytesToErase)/CHUNK_SIZE;  // how many old chunks are in this page
        UNUSED_VARIABLE(chunksToSave);
        
        debug_log("    pg%d (%d chunks)-->RAM...", newPage, chunksToSave);
        uint32_t temp[WORDS_PER_PAGE];                            // temporary buffer for page
        memcpy(temp, pageAddr, BYTES_PER_PAGE);                   // copy old data to buffer
        debug_log("clear %dbytes...", bytesToErase);
        memset(temp, 0xff, bytesToErase);                         // clear unused portion of page in buffer
        
        debug_log("erase pg%d...",(int)newPage);
        nrf_delay_ms(20);
        erasePageOfFlash(newPage);                              // erase page
        while(flashWorking);
        
        debug_log("restore data...\r\n");
        nrf_delay_ms(10);
        writeBlockToFlash(pageAddr, temp, WORDS_PER_PAGE);      // replace data from buffer
        while(flashWorking);
    }
    
    store.to = newChunk;
    debug_log("  Ready to store to chunk %d.\r\n",newChunk); 
    
    store.extFrom = 0;
    
    // We need to find the most recent stored chunk, to start storing after it
    
    scan_header_t header;
    scan_tail_t tail;
    
    ext_eeprom_wait();

    store.extTo = EXT_FIRST_DATA_CHUNK;
    unsigned long lastStoredTimestamp = 0;
    for (int i = EXT_FIRST_DATA_CHUNK; i <= EXT_LAST_CHUNK; i++)  {
        unsigned int addr = EXT_ADDRESS_OF_CHUNK(i);
        ext_eeprom_read(addr,header.buf,sizeof(header.buf));               // Beginning of the chunk
        ext_eeprom_wait();
        ext_eeprom_read(addr+EXT_CHUNK_SIZE-4,tail.buf,sizeof(tail.buf));  // End of the chunk
        ext_eeprom_wait();
        
        // Print all written chunks
        /*if(header.timestamp != 0xFFFFFFFFUL)
        {
            debug_log("CH: %d,TS: %X\r\n",i,(int)header.timestamp);
            nrf_delay_ms(2);
        } */
        
        // is it a completely stored scan result chunk?
        if (header.timestamp > MODERN_TIME && header.timestamp < FUTURE_TIME)  {
            if (tail.check == header.timestamp || tail.check == CHECK_TRUNC || tail.check == CHECK_CONTINUE)  {
                // is it the most recent one found yet?
                if (header.timestamp >= lastStoredTimestamp)  {
                    lastStoredTimestamp = header.timestamp;
                    store.extTo = i;
                }
            }
        }
    }
    
    if (lastStoredTimestamp == 0)  {
        debug_log("  No stored scans found.\r\n");
    }
    else  {
        debug_log("  Last stored scan in chunk %d, timestamp %X.\r\n",(int)store.extTo,(int)lastStoredTimestamp);
    }
    
    store.extTo = (store.extTo < EXT_LAST_CHUNK) ? store.extTo+1 : EXT_FIRST_DATA_CHUNK;
    
    
    //store.extFrom = 0;
    //store.extTo = EXT_FIRST_DATA_CHUNK;
    ext_eeprom_global_unprotect();
    ext_eeprom_wait();
    
    
    
    storerMode = STORER_IDLE;
    
}