-/**
- * @defgroup ADC Analog To Digital Converter
- *
- * Control and manage the on board PIC A/D converter.
- *
- * @{
- */
-
-/// Filtered voltages using a single pole, low pass filter.
-uint16_t adcMainBusVolt;
-
-/// PIC ADC Channel number of the reference voltage.
-#define ADC_REF 0
-
-/// PIC ADC Channel number of the main bus voltage.
-#define ADC_MAINBUS 1
-
-/// Input diode drop in units of 0.01 volts.
-#define MAIN_BUS_VOLT_OFFSET 20
-
-/**
- * Intialize the ADC subsystem.
- */
-void adcInit()
-{
- // Setup the ADC.
- setup_adc_ports(AN0_TO_AN1);
- setup_adc( ADC_CLOCK_DIV_32 );
-
- // Zero the ADC filters.
- adcMainBusVolt = 0;
-}
-
-/**
- * Filtered main bus voltage in 10mV resolution.
- *
- * @return voltage in 10mV steps
- */
-uint16_t adcGetMainBusVolt()
-{
- uint32_t volts;
-
- volts = (uint32_t) (adcMainBusVolt >> 3);
-
- volts = (volts * 330l) / 1023l;
-
- return (uint16_t) volts + MAIN_BUS_VOLT_OFFSET;
-}
-
-/**
- * Get the current ADC value for the main bus voltage.
- *
- * @return ADC value in the range 0 to 1023
- */
-uint16_t adcRawBusVolt()
-{
- set_adc_channel(ADC_MAINBUS);
- delay_us(50);
- return read_adc();
-}
-
-/**
- * Get the current ADC value for the reference source voltage.
- *
- * @return ADC value in the range 0 to 1023
- */
-uint16_t adcRawRefVolt()
-{
- set_adc_channel(ADC_REF);
- delay_us(50);
- return read_adc();
-}
-
-/**
- * Read and filter the ADC channels for bus voltages.
- */
-void adcUpdate(void)
-{
- // Filter the bus voltage using a single pole low pass filter.
- set_adc_channel(ADC_MAINBUS);
- delay_us(50);
- adcMainBusVolt = read_adc() + adcMainBusVolt - (adcMainBusVolt >> 3);
-}
-
-/** @} */
-
-
-/**
- * @defgroup diag Diagnostics and Control
- *
- * Functions for diagnostics and control of the hardware and flight data recorder.
- *
- * @{
- */
-
-/// Number of bytes per line to display when reading flight data recorder.
-#define DIAG_BYTES_PER_LINE 32
-
-/**
- * Process the command to erase the data logger flash.
- */
-void diagEraseFlash()
-{
- // Confirm we want to erase the flash with the key sequence 'yes' .
- fprintf (PC_HOST, "Are you sure (yes)? ");
-
- if (fgetc(PC_HOST) != 'y')
- return;
-
- if (fgetc(PC_HOST) != 'e')
- return;
-
- if (fgetc(PC_HOST) != 's')
- return;
-
- if (fgetc(PC_HOST) != 13)
- return;
-
- // User feedback and erase the part.
- fprintf (PC_HOST, "Erasing flash...");
-
- flashErase();
-
- fprintf (PC_HOST, "done.\n\r");
-}
-
-/**
- * Display the engineering mode menu.
- */
-void diagMenu()
-{
- // User interface.
- fprintf (PC_HOST, "Options: (e)rase Flash, (r)ead Flash\n\r");
- fprintf (PC_HOST, " Toggle (L)ED\n\r");
- fprintf (PC_HOST, " (P)TT - Push To Transmit\n\r");
- fprintf (PC_HOST, " (f)requencey down, (F)requency up - 1KHz step\n\r");
- fprintf (PC_HOST, " (c)hannel down, (C)hannel up - 25KHz step\n\r");
- fprintf (PC_HOST, " (a)mplitude down, (A)mplitude up - 0.5 dB steps\n\r");
- fprintf (PC_HOST, " e(x)it engineering mode\n\r");
-}
-
-/**
- * Process the command to dump the contents of the data logger flash.
- */
-void diagReadFlash()
-{
- bool_t dataFoundFlag, userStopFlag;
- uint8_t i, buffer[DIAG_BYTES_PER_LINE];
- uint32_t address;
-
- // Set the initial conditions to read the flash.
- address = 0x0000;
- userStopFlag = false;
-
- do
- {
- // Read each block from the flash device.
- flashReadBlock (address, buffer, DIAG_BYTES_PER_LINE);
-
- // This flag will get set if any data byte is not equal to 0xff (erase flash state)
- dataFoundFlag = false;
-
- // Display the address.
- fprintf (PC_HOST, "%08lx ", address);
-
- // Display each byte in the line.
- for (i = 0; i < DIAG_BYTES_PER_LINE; ++i)
- {
- fprintf (PC_HOST, "%02x", buffer[i]);
-
- // Set this flag if the cell is not erased.
- if (buffer[i] != 0xff)
- dataFoundFlag = true;
-
- // Any key will abort the transfer.
- if (kbhit(PC_HOST))
- userStopFlag = true;
- } // END for
-
- // at the end of each line.
- fprintf (PC_HOST, "\n\r");
-
- // Advance to the next block of memory.
- address += DIAG_BYTES_PER_LINE;
- } while (dataFoundFlag && !userStopFlag);
-
- // Feedback to let the user know why the transfer stopped.
- if (userStopFlag)
- fprintf (PC_HOST, "User aborted download!\n\r");
-}
-
-void diag1PPS()
-{
- uint16_t timeStamp, lastTimeStamp;
-
- lastTimeStamp = 0x0000;
-
- gpsPowerOn();
-
- for (;;)
- {
- timeStamp = CCP_2;
-
- if (timeStamp != lastTimeStamp)
- {
- delay_ms (10);
-
- timeStamp = CCP_2;
-
- fprintf (PC_HOST, "%lu %lu\n\r", timeStamp, (timeStamp - lastTimeStamp));
-
- lastTimeStamp = timeStamp;
- }
- }
-}
-
-/**
- * Process diagnostic commands through the debug RS-232 port.
- */
-void diagPort()
-{
- bool_t diagDoneFlag, ledFlag, paFlag, showSettingsFlag;
- uint8_t command, amplitude;
- uint32_t freqHz;
-
- // If the input is low, we aren't connected to the RS-232 device so continue to boot.
- if (!input(PIN_B6))
- return;
-
- fprintf (PC_HOST, "Engineering Mode\n\r");
- fprintf (PC_HOST, "Application Built %s %s\n\r", __DATE__, __TIME__);
-
- // Current state of the status LED.
- ledFlag = false;
- output_bit (IO_LED, ledFlag);
-
- // This flag indicates we are ready to leave the diagnostics mode.
- diagDoneFlag = false;
-
- // Current state of the PA.
- paFlag = false;
-
- // Flag that indicate we should show the current carrier frequency.
- showSettingsFlag = false;
-
- // Set the initial carrier frequency and amplitude.
- freqHz = 445950000;
- amplitude = 0;
-
- // Wait for the exit command.
- while (!diagDoneFlag)
- {
- // Wait for the user command.
- command = fgetc(PC_HOST);
-
- // Decode and process the key stroke.
- switch (command)
- {
- case 'e':
- diagEraseFlash();
- logInit();
- break;
-
- case 'l':
- case 'L':
- ledFlag = (ledFlag ? false : true);
- output_bit (IO_LED, ledFlag);
- break;
-
- case 'h':
- case 'H':
- case '?':
- diagMenu();
- break;
-
- case 'r':
- diagReadFlash();
- break;
-
- case 't':
- tncHighRate (true);
- fprintf (PC_HOST, "Set high rate TNC.\n\r");
- break;
-
- case 'f':
- freqHz -= 1000;
- ddsSetFreq (freqHz);
-
- // Display the new frequency.
- showSettingsFlag = true;
- break;
-
- case 'F':
- freqHz += 1000;
- ddsSetFreq (freqHz);
-
- // Display the new frequency.
- showSettingsFlag = true;
- break;
-
- case 'c':
- freqHz -= 25000;
- ddsSetFreq (freqHz);
-
- // Display the new frequency.
- showSettingsFlag = true;
- break;
-
- case 'C':
- freqHz += 25000;
- ddsSetFreq (freqHz);
-
- // Display the new frequency.
- showSettingsFlag = true;
- break;
-
- case 'p':
- case 'P':
- ddsSetFreq (freqHz);
-
- paFlag = (paFlag ? false : true);
- output_bit (IO_PTT, paFlag);
- output_bit (IO_OSK, paFlag);
-
- if (paFlag)
- {
- ddsSetMode (DDS_MODE_AFSK);
- ddsSetAmplitude (amplitude);
- } else
- ddsSetMode (DDS_MODE_POWERDOWN);
-
- break;
-
- case 'a':
- if (amplitude != 200)
- {
- amplitude += 5;
- ddsSetAmplitude (amplitude);
-
- // Display the new amplitude.
- showSettingsFlag = true;
- }
- break;
-
- case 'A':
- if (amplitude != 0)
- {
- amplitude -= 5;
- ddsSetAmplitude (amplitude);
-
- // Display the new amplitude.
- showSettingsFlag = true;
- }
- break;
-
- case 'g':
- diag1PPS();
- break;
-
- case 'x':
- diagDoneFlag = true;
- break;
-
- default:
- fprintf (PC_HOST, "Invalid command. (H)elp for menu.\n\r");
- break;
- } // END switch
-
- // Display the results of any user requests or commands.
- if (showSettingsFlag)
- {
- showSettingsFlag = false;
-
- fprintf (PC_HOST, "%03ld.%03ld MHz ", freqHz / 1000000, (freqHz / 1000) % 1000);
- fprintf (PC_HOST, "%d.%01ddBc\n\r", amplitude / 10, amplitude % 10);
-
- } // END if
-
- } // END while
-
- // Let the user know we are done with this mode.
- fprintf (PC_HOST, "Exit diagnostic mode.\n\r");
-
- return;
-}
-