//*****************************************************************************
//
// This function is called from the application main loop to keep the
// acquisition running.  It checks to see if there is any new ADC data, and
// if so it processes the new ADC data.
//
// The function returns non-zero if data was acquired, 0 if no data was
// acquired.
//
//*****************************************************************************
int32_t
AcquireRun(void)
{
    tLogRecord *psRecord = &g_sRecordBuf.sRecord;

    //
    // Make sure we are properly configured to run
    //
    if(!g_psConfigState)
    {
        return(0);
    }

    //
    // Check to see if new ADC data is available
    //
    if(g_ui32ADCCount != g_ui32LastADCCount)
    {
        g_ui32LastADCCount = g_ui32ADCCount;

        //
        // Process the ADC data and store it in the record buffer.
        //
        ProcessDataItems(psRecord);

        //
        // Add the newly processed data to the strip chart.  Do not add to
        // strip start if sleep-logging.
        //
        if((g_psConfigState->ui8Storage != CONFIG_STORAGE_VIEWER) &&
           !g_psConfigState->ui32SleepLogging)
        {
            StripChartMgrAddItems(psRecord->pi16Items);
        }

        //
        // If USB stick is used, write the record to the USB stick
        //
        if(g_psConfigState->ui8Storage == CONFIG_STORAGE_USB)
        {
            USBStickWriteRecord(psRecord);
        }

        //
        // If host PC is used, write data to USB serial port
        //
        if(g_psConfigState->ui8Storage == CONFIG_STORAGE_HOSTPC)
        {
            USBSerialWriteRecord(psRecord);
        }

        //
        // If flash storage is used, write data to the flash
        //
        if(g_psConfigState->ui8Storage == CONFIG_STORAGE_FLASH)
        {
            FlashStoreWriteRecord(psRecord);

            //
            // If we are sleep logging, then save the storage address for
            // use in the next cycle.
            //
            if(g_psConfigState->ui32SleepLogging)
            {
                g_psConfigState->ui32FlashStore = FlashStoreGetAddr();
            }
        }
        else if(g_psConfigState->ui8Storage == CONFIG_STORAGE_VIEWER)
        {
            //
            // If in viewer mode, then update the viewer text strings.
            //
            UpdateViewerData(psRecord);
        }

        //
        // Return indication to caller that data was processed.
        //
        return(1);
    }
    else if((g_psConfigState->ui8Storage == CONFIG_STORAGE_HOSTPC) &&
            (g_bNeedKeepAlive == true))
    {
        //
        // Else there is no new data to process, but check to see if we are
        // logging to PC and a keep alive packet is needed.
        //
        // Clear keep-alive needed flag
        //
        g_bNeedKeepAlive = false;

        //
        // Make a keep alive packet by creating a record with timestamp of 0.
        //
        psRecord->ui32Seconds = 0;
        psRecord->ui16Subseconds = 0;
        psRecord->ui16ItemMask = 0;

        //
        // Transmit the dummy record to host PC.
        //
        USBSerialWriteRecord(psRecord);
    }

    //
    // Return indication that data was not processed.
    //
    return(0);
}
Exemplo n.º 2
0
//*****************************************************************************
//
// Saves data records that are stored in the flash to an externally connected
// USB memory storage device (USB stick).
// The flash memory is scanned for the presence of store data records.  When
// records are found they are written in CSV format to the USB stick.  This
// function assumes a non-corrupted storage area, and that any records, once
// found, are contiguous with all stored records.  It will find the oldest
// record and start with that when storing.
//
//*****************************************************************************
int
FlashStoreSave(void)
{
    unsigned long ulAddr;
    unsigned long ulOldestRecord = FLASH_STORE_START_ADDR;
    unsigned long ulOldestSeconds = 0xFFFFFFFF;
    tLogRecord *pRecord;

    //
    // Show a message to the user.
    //
    SetStatusText("SAVE", "SCANNING", "FLASH", 0);

    //
    // Start at beginning of flash storage area
    //
    ulAddr = FLASH_STORE_START_ADDR;

    //
    // Search all of flash area checking every stored record.
    //
    while(ulAddr < FLASH_STORE_END_ADDR)
    {
        //
        // If a record signature is found, check for oldest record, then
        // increment to the next record
        //
        if((HWREG(ulAddr) & 0xFFFFFF00) == 0x53554100)
        {
            //
            // Get a pointer to the data record (account for flash header word)
            //
            pRecord = (tLogRecord *)(ulAddr + 4);

            //
            // If the seconds in this record are older than any found so far
            // then save the seconds value, and the address of this record
            //
            if(pRecord->ulSeconds < ulOldestSeconds)
            {
                ulOldestSeconds = pRecord->ulSeconds;
                ulOldestRecord = ulAddr;
            }

            //
            // Advance the address to the next record.
            //
            ulAddr += HWREG(ulAddr) & 0xFF;
        }

        //
        // Otherwise a record was not found so just advance to the next
        // location in flash
        //
        else
        {
            ulAddr += 4;
        }
    }

    //
    // If no "oldest" seconds was found, then there is no valid data stored
    //
    if(ulOldestSeconds == 0xFFFFFFFF)
    {
        SetStatusText("SAVE", "NO RECORDS", "FOUND", "PRESS <");
        return(1);
    }

    //
    // Open the output file on the USB stick.  It will return NULL if there
    // was any problem.
    //
    if(!USBStickOpenLogFile(0))
    {
        SetStatusText("SAVE", 0, "USB ERROR", "PRESS <");
        return(1);
    }

    //
    // Notify user we are saving data to USB
    //
    SetStatusText("SAVE", "SAVING", "TO USB", 0);

    //
    // Start reading records from flash, start at the address of the oldest
    // record, as found above.  We scan through records, assuming the flash
    // store is not corrupted.  Continue scanning until a blank space is
    // found which should indicate the end of recorded data, or until we
    // have read all the records.
    //
    ulAddr = ulOldestRecord;
    while(HWREG(ulAddr) != 0xFFFFFFFF)
    {
        unsigned long ulCount;
        unsigned long ulPartialCount;

        //
        // If a record signature is found (which it should be), extract the
        // record data and send it to USB stick.
        //
        if((HWREG(ulAddr) & 0xFFFFFF00) == 0x53554100)
        {
            //
            // Get byte count for this record
            //
            ulCount = HWREG(ulAddr) & 0xFF;

            //
            // Adjust the count and the address to remove the flash header
            //
            ulCount -= 4;
            ulAddr += 4;

            //
            // Adjust for memory wrap
            //
            if(ulAddr >= FLASH_STORE_END_ADDR)
            {
                ulAddr = FLASH_STORE_START_ADDR;
            }

            //
            // If the contents of this record go past the end of the memory
            // storage area, then perform a partial copy first.
            //
            ulPartialCount = 0;
            if((ulAddr + ulCount) >= FLASH_STORE_END_ADDR)
            {
                //
                // Find how many bytes are left on this page
                //
                ulPartialCount = FLASH_STORE_END_ADDR - ulAddr;

                //
                // Copy the portion until the end of memory store, adjust
                // remaining count and address
                //
                memcpy(g_ulRecordBuf, (void *)ulAddr, ulPartialCount);
                ulCount -= ulPartialCount;
                ulAddr = FLASH_STORE_START_ADDR;
            }

            //
            // Copy entire record (or remaining part of record if memory wrap)
            // into record buffer
            //
            memcpy(&g_ulRecordBuf[ulPartialCount / 4], (void *)ulAddr,
                   ulCount);

            //
            // Update address pointer to next record
            //
            ulAddr += ulCount;

            //
            // Now we have an entire data logger record copied from flash
            // storage into a local (contiguous) memory buffer.  Pass it
            // to the USB file writing function to write the record to the
            // USB stick.
            //
            USBStickWriteRecord((tLogRecord *)g_ulRecordBuf);
        }

        //
        // This should not happen, but it means we ended up in a non-blank
        // location that is not the start of a record.  In this case just
        // advance through memory until either a blank location or another
        // record is found.
        //
        else
        {
            //
            // Increment to next word in flash, adjust for memory wrap.
            //
            ulAddr += 4;
            if(ulAddr >= FLASH_STORE_END_ADDR)
            {
                ulAddr = FLASH_STORE_START_ADDR;
            }
        }
    }

    //
    // Close the USB stick file so that any buffers will be flushed.
    //
    USBStickCloseFile();

    //
    // Inform user that save is complete.
    //
    SetStatusText("SAVE", "USB SAVE", "COMPLETE", "PRESS <");

    //
    // Return success
    //
    return(0);
}