/* * Lower-level routines */ int ad1848_commit_settings(void *addr) { struct ad1848_softc *sc = addr; int timeout; u_char fs; int s; if (!sc->need_commit) return 0; s = splaudio(); ad1848_mute_monitor(sc, 1); /* Enables changes to the format select reg */ ad_set_MCE(sc, 1); fs = sc->speed_bits | sc->format_bits; if (sc->channels == 2) fs |= FMT_STEREO; ad_write(sc, SP_CLOCK_DATA_FORMAT, fs); /* * If mode == 2 (CS4231), set I28 also. It's the capture format * register. */ if (sc->mode == 2) { /* Gravis Ultrasound MAX SDK sources says something about * errata sheets, with the implication that these inb()s * are necessary. */ (void)ADREAD(sc, AD1848_IDATA); (void)ADREAD(sc, AD1848_IDATA); /* * Write to I8 starts resynchronization. Wait until it * completes. */ timeout = AD1848_TIMO; while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT) timeout--; ad_write(sc, CS_REC_FORMAT, fs); /* Gravis Ultrasound MAX SDK sources says something about * errata sheets, with the implication that these inb()s * are necessary. */ (void)ADREAD(sc, AD1848_IDATA); (void)ADREAD(sc, AD1848_IDATA); /* Now wait for resync for capture side of the house */ } /* * Write to I8 starts resynchronization. Wait until it completes. */ timeout = AD1848_TIMO; while (timeout > 0 && ADREAD(sc, AD1848_IADDR) == SP_IN_INIT) timeout--; if (ADREAD(sc, AD1848_IADDR) == SP_IN_INIT) printf("ad1848_commit: Auto calibration timed out\n"); /* * Starts the calibration process and enters playback mode after it. */ ad_set_MCE(sc, 0); wait_for_calibration(sc); ad1848_mute_monitor(sc, 0); splx(s); sc->need_commit = 0; return 0; }
/** * Our working loop while when running. */ void infinite_deep_sleep(void) { uint8_t has_logged = 0; uint32_t left_em_acc = 0, right_em_acc = 0; //uint16_t left_envelope = 0, right_envelope = 0; uint32_t acc_counter = 0; /* Configure all the calibration stuff first */ configure_calibration(); /* Start the first calibration running */ start_calibration(); /* Configure all the registers for deep sleep */ configure_deep_sleep(); /* Wait for the first calibration to finish */ wait_for_calibration(); while (1) { /* Sleep for 500 milliseconds */ do_deep_sleep(1); increment_us(500*1000); if (is_time_valid()) { /* Fire up the ADC */ prepare_sampling(); /* If we've taken at least one reading before */ if (has_logged > 1) { /** * Update the envelope values. NOTE: This must be done before * the fft as the fft is in-place. */ // left_envelope = get_envelope_32(left_envelope, samples_left+4); // right_envelope = get_envelope_32(right_envelope, samples_right+4); /** * Add our samples to the accumulators. We skip the first 4 points of each sample. * TODO 48MHz clock? */ left_em_acc += fft_32(samples_left+2, get_left_tuned_bin()) >> 7; right_em_acc += fft_32(samples_right+2, get_right_tuned_bin()) >> 7; if (++acc_counter >= 128) { /* If we're ready to write to memory */ /* Write em to memory */ /* Middle of average is 32 seconds ago */ write_sample_to_mem(get_em_record_flags(), left_em_acc, right_em_acc, 32); /* Clear accumulators */ acc_counter = left_em_acc = right_em_acc = 0; /* Wait for the write to finish */ wait_for_write_complete(); /* Write envelope to memory */ // write_sample_to_mem(get_envelope_record_flags(), // left_envelope, right_envelope, 32); /* Clear envelope */ // left_envelope = right_envelope = 0; /* Wait for the write to finish */ // wait_for_write_complete(); } } else { has_logged++; LED_OFF(); } /* Take a reading */ do_sampling(); /* Shutdown the ADC */ shutdown_sampling(); /* Take battery readings */ do_battery(); } else { /* Invalid time */