Esempio n. 1
0
/**
 * Configure a specific adc.
 */
void adc_config(uint32_t adc, const uint8_t const *channel_array)
{
	adc_enable_scan_mode(adc);
	adc_set_continuous_conversion_mode(adc);
	adc_set_right_aligned(adc);
	adc_enable_external_trigger_regular(adc, ADC_CR2_EXTSEL_SWSTART);
	adc_set_sample_time_on_all_channels(adc, ADC_SAMPLE_TIME);
	adc_enable_dma(adc);

	adc_power_on(adc);

	{
		int i;
		/* Wait a bit for the adc to power on. */
		for (i = 0; i < 800000; i++) {
			__asm("nop");
		}
	}

	adc_reset_calibration(adc);
	adc_calibration(adc);

	adc_set_regular_sequence(adc, ADC_RAW_SAMPLE_COUNT/2,
				 (uint8_t *)channel_array);
}
Esempio n. 2
0
/**
 * NOTE this is a state machine, but it expects to run often enough for millis()
 * @param machine
 * @param res
 */
void jack_run_task(volatile struct jacks_machine_t *machine, struct jacks_result_t *res)
{
	res->ready = false;
	if (!jack_connected(machine->jack)) {
		return;
	}
	switch (machine->step) {
	case jack_machine_step_off:
		// is it time to do a reading yet?
		if (millis() - 3000 > machine->last_read_millis) {
			printf("switching power on: channel %u\n", (unsigned int) machine->jack->val_channel);
			gpio_set(machine->jack->power_port, machine->jack->power_pin);
			machine->step = jack_machine_step_powered;
			machine->step_entry_millis = millis();
		}
		break;

	case jack_machine_step_powered:
		// have we been powered up long enough yet?
		if (millis() - machine->jack->power_on_time_millis > machine->step_entry_millis) {
			printf("power stable!\n");
			machine->step = jack_machine_step_ready;
			// not really necessary... machine->step_entry_millis = millis();
		} else {
			printf(".");
		}
		break;

	case jack_machine_step_ready:
		// TODO - this should actually start a dma sequence and go to a next step 
		// that decimates/averages and finally returns.
		// ok! do a few readings and call it good
		adc_disable_scan_mode(ADC1);
		adc_set_single_conversion_mode(ADC1);
		adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
		//adc_set_single_channel(ADC1, machine->jack->val_channel);
		adc_set_regular_sequence(ADC1, 1, (u8*)&(machine->jack->val_channel));

		adc_enable_external_trigger_regular(ADC1, ADC_CR2_EXTSEL_SWSTART);
		adc_start_conversion_regular(ADC1);
		printf("ok, doing reading on channel!\n");
		while(!adc_eoc(ADC1)) {
			;
		}
		res->ready = true;
		res->value = adc_read_regular(ADC1);
		machine->last_value = res->value;
		machine->last_read_millis = millis();
		gpio_clear(machine->jack->power_port, machine->jack->power_pin);
		machine->step = jack_machine_step_off;
		break;
	}
	return;
}