Exemplo n.º 1
0
static void adc_setup(void)
{
	int i;

	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);

	/* Make sure the ADC doesn't run during config. */
	adc_off(ADC1);

	/* We configure everything for one single conversion. */
	adc_disable_scan_mode(ADC1);
	adc_set_single_conversion_mode(ADC1);
	adc_disable_external_trigger_regular(ADC1);
	adc_set_right_aligned(ADC1);
	/* We want to read the temperature sensor, so we have to enable it. */
	adc_enable_temperature_sensor(ADC1);
	adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

	adc_power_on(ADC1);

	/* Wait for ADC starting up. */
	for (i = 0; i < 800000; i++)    /* Wait a bit. */
		__asm__("nop");

	adc_reset_calibration(ADC1);
	adc_calibration(ADC1);
}
static void adc_setup(void)
{
	int i;

	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);

	/* Make sure the ADC doesn't run during config. */
	adc_off(ADC1);

	/* We configure everything for one single timer triggered injected conversion. */
	adc_disable_scan_mode(ADC1);
	adc_set_single_conversion_mode(ADC1);
	/* We can only use discontinuous mode on either the regular OR injected channels, not both */
	adc_disable_discontinuous_mode_regular(ADC1);
	adc_enable_discontinuous_mode_injected(ADC1);
	/* We want to start the injected conversion with the TIM2 TRGO */
	adc_enable_external_trigger_injected(ADC1,ADC_CR2_JEXTSEL_TIM2_TRGO);
	adc_set_right_aligned(ADC1);
	/* We want to read the temperature sensor, so we have to enable it. */
	adc_enable_temperature_sensor(ADC1);
	adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

	adc_power_on(ADC1);

	/* Wait for ADC starting up. */
	for (i = 0; i < 800000; i++)    /* Wait a bit. */
		__asm__("nop");

	adc_reset_calibration(ADC1);
	while ((ADC_CR2(ADC1) & ADC_CR2_RSTCAL) != 0);
	adc_calibration(ADC1);
	while ((ADC_CR2(ADC1) & ADC_CR2_CAL) != 0);
}
static void adc_setup(void)
{
    gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0);
    gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO1);

    /* Make sure the ADC doesn't run during config. */
    adc_off(ADC1);

    /* We configure everything for one single conversion. */
    adc_disable_scan_mode(ADC1);
    adc_set_single_conversion_mode(ADC1);
    adc_disable_external_trigger_regular(ADC1);
    adc_set_right_aligned(ADC1);
    adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

    adc_power_on(ADC1);

    /* Wait for ADC starting up. */
    int i;
    for (i = 0; i < 800000; i++) /* Wait a bit. */
        __asm__("nop");

    adc_reset_calibration(ADC1);
    adc_calibration(ADC1);
}
Exemplo n.º 4
0
void adc_init (void)
{
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
  rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
  rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN);



  gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1);	//PA1   joint_1
  gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO2);	//PA2   joint_2
  gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO3);	//PA3   joint_3
  gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1);	//PC1   joint_4
  gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO2);	//PC2   joint_5
  gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO5);	//PC5   joint_6

  adc_set_clk_prescale(ADC_CCR_ADCPRE_BY2);
  adc_disable_scan_mode(ADC1);
  adc_set_single_conversion_mode(ADC1);

  adc_set_sample_time(ADC1, ADC_CHANNEL1, ADC_SMPR_SMP_3CYC);   //joint_1
  adc_set_sample_time(ADC1, ADC_CHANNEL2, ADC_SMPR_SMP_3CYC);   //joint_2
  adc_set_sample_time(ADC1, ADC_CHANNEL3, ADC_SMPR_SMP_3CYC);   //joint_3
  adc_set_sample_time(ADC1, ADC_CHANNEL11, ADC_SMPR_SMP_3CYC);  //joint_4
  adc_set_sample_time(ADC1, ADC_CHANNEL12, ADC_SMPR_SMP_3CYC);  //joint_5
  adc_set_sample_time(ADC1, ADC_CHANNEL15, ADC_SMPR_SMP_3CYC);  //joint_6

  adc_set_multi_mode(ADC_CCR_MULTI_INDEPENDENT);
  adc_power_on(ADC1);

  //nvic_enable_irq(NVIC_ADC_IRQ);
  //adc_enable_eoc_interrupt(ADC1);
  //adc_disable_eoc_interrupt(ADC1);
}
Exemplo n.º 5
0
void adc_setup()
{
    rcc_periph_clock_enable(RCC_ADC1);

    /* Make sure the ADC doesn't run during config. */
    adc_off(ADC1);

    /* We configure everything for one single conversion. */
    adc_disable_scan_mode(ADC1);
    adc_set_single_conversion_mode(ADC1);
    adc_disable_external_trigger_regular(ADC1);
    adc_set_right_aligned(ADC1);
    adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

    adc_power_on(ADC1);

    /* Wait for ADC starting up. */
    int i;
    for (i = 0; i < 800000; i++) /* Wait a bit. */
        __asm__("nop");

    /* Select the channel we want to convert. 16=temperature_sensor. */
    uint8_t channel_array[16];
    channel_array[0] = 0;
    adc_set_regular_sequence(ADC1, 1, channel_array);

    /* Start the conversion directly (not trigger mode). */
    adc_start_conversion_direct(ADC1);
}
Exemplo n.º 6
0
void adc_setup(void)
{
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
		      GPIO_CNF_INPUT_FLOAT, GPIO6);
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, GPIO5 | GPIO1);

	/* Make sure the ADC doesn't run during config. */
	adc_off(ADC1);

	/* We configure everything for one single conversion. */
	adc_disable_scan_mode(ADC1);
	adc_set_single_conversion_mode(ADC1);
	adc_disable_external_trigger_regular(ADC1);
	adc_set_right_aligned(ADC1);
	
	//adc_enable_temperature_sensor(ADC1);
	//adc_set_injected_offset(ADC1, 0x2, 0x00);
	adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

	adc_power_on(ADC1);

	/* Wait for ADC starting up. */
	delay_ms(170);

	adc_reset_calibration(ADC1);
	adc_calibration(ADC1);
}
Exemplo n.º 7
0
static uint16_t ADC_Measure(uint16_t ch)
{
	uint16_t val;
	uint8_t channel_array[16];

	/* Make sure the ADC doesn't run during config. */
//	adc_off(TS_ADC);
	/* We configure everything for one single conversion. */
	adc_disable_scan_mode(TS_ADC);
	adc_set_single_conversion_mode(TS_ADC);
	adc_disable_external_trigger_regular(TS_ADC);
	adc_set_right_aligned(TS_ADC);
	
	/* ADC regular channel14 configuration */ 
//    adc_set_sample_time(TS_ADC, ch, ADC_SMPR_SMP_55DOT5CYC);
	adc_set_sample_time_on_all_channels(TS_ADC, ADC_SMPR_SMP_55DOT5CYC);
//	adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
//	ADC_RegularChannelConfig(TS_ADC, ch, 1, ADC_SampleTime_55Cycles5);
	
	/* Enable ADC */
//	ADC_Cmd(TS_ADC, ENABLE);
    adc_power_on(TS_ADC);
	delay(100);
	
#if 1
	/* Enable ADC reset calibaration register */   
    adc_reset_calibration(TS_ADC);
//	ADC_ResetCalibration(TS_ADC);
	/* Check the end of ADC reset calibration register */
//	while(ADC_GetResetCalibrationStatus(TS_ADC));
	
	/* Start ADC calibaration */
//	ADC_StartCalibration(TS_ADC);
//    adc_calibration(TS_ADC);
    adc_calibrate_async(TS_ADC);
	/* Check the end of ADC calibration */
//	while(ADC_GetCalibrationStatus(TS_ADC));
#endif     

    /* Select the channel we want to convert. */
	channel_array[0] = ch;
	adc_set_regular_sequence(TS_ADC, 1, channel_array);

	/* Start ADC Software Conversion */ 
//    adc_start_conversion_regular(TS_ADC);
	adc_start_conversion_direct(TS_ADC);
	
	while(!adc_eoc(TS_ADC));

//    val = adc_read_regular(TS_ADC);
    val = ADC_DR(TS_ADC);
	
//    adc_off(TS_ADC);

	return val;
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
0
static void adc_setup(void)
{
	gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0);
	gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1);

	adc_power_off(ADC1);
	adc_disable_scan_mode(ADC1);
	adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_3CYC);

	adc_power_on(ADC1);

}
Exemplo n.º 10
0
void battery_setup() {
	gpio_mode_setup(BAT_STAT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, BAT_STAT1_PORT | BAT_STAT2_PORT | BAT_PG_PORT);

	gpio_mode_setup(BAT_SENSE_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, BAT_SENSE_PIN);

	adc_off(BAT_SENSE_ADC);
	adc_disable_scan_mode(BAT_SENSE_ADC);
	adc_set_single_conversion_mode(ADC1);
	adc_set_sample_time(ADC1, ADC_CHANNEL10, ADC_SMPR_SMP_15CYC);
	uint8_t channels[] = {ADC_CHANNEL10};
	adc_set_regular_sequence(BAT_SENSE_ADC, 1, channels);

	adc_power_on(BAT_SENSE_ADC);
}
Exemplo n.º 11
0
static void setup_stm32f1_peripherals(void)
{
	rcc_peripheral_enable_clock(&RCC_APB1ENR, 
			RCC_APB1ENR_I2C1EN);

	rcc_peripheral_enable_clock(&RCC_APB2ENR, 
			RCC_APB2ENR_ADC1EN);

	/* GPIO pin for I2C1 SCL, SDA */

	/* VESNA v1.0
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6);
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO7);
	*/

	/* VESNA v1.1 */
	AFIO_MAPR |= AFIO_MAPR_I2C1_REMAP;
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, TDA_PIN_SCL);
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, TDA_PIN_SDA);

	/* GPIO pin for TDA18219 IRQ */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
			GPIO_CNF_INPUT_FLOAT, TDA_PIN_IRQ);

	/* GPIO pin for TDA18219 IF AGC */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, TDA_PIN_IF_AGC);
	/* Set to lowest gain for now */
	gpio_clear(GPIOA, TDA_PIN_IF_AGC);

	/* GPIO pin for AD8307 ENB */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, TDA_PIN_ENB);

	/* ADC pin for AD8307 output */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
			GPIO_CNF_INPUT_ANALOG, TDA_PIN_OUT);

	/* Setup I2C */
	i2c_peripheral_disable(I2C1);

	/* 400 kHz - I2C Fast Mode */
	i2c_set_clock_frequency(I2C1, I2C_CR2_FREQ_24MHZ);
	i2c_set_fast_mode(I2C1);
	/* 400 kHz */
	i2c_set_ccr(I2C1, 0x14);
	/* 300 ns rise time */
	i2c_set_trise(I2C1, 0x08);

	i2c_peripheral_enable(I2C1);


	/* Make sure the ADC doesn't run during config. */
	adc_off(ADC1);

	/* We configure everything for one single conversion. */
	adc_disable_scan_mode(ADC1);
	adc_set_single_conversion_mode(ADC1);
	adc_enable_discontinous_mode_regular(ADC1);
	adc_disable_external_trigger_regular(ADC1);
	adc_set_right_aligned(ADC1);
	adc_set_conversion_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);

	adc_on(ADC1);

	/* Wait for ADC starting up. */
	int i;
	for (i = 0; i < 800000; i++)    /* Wait a bit. */
		__asm__("nop");

	adc_reset_calibration(ADC1);
	adc_calibration(ADC1);

	uint8_t channel_array[16];
	/* Select the channel we want to convert. */
	if(TDA_PIN_OUT == GPIO0) {
		channel_array[0] = 0;
	} else if(TDA_PIN_OUT == GPIO2) {
		channel_array[0] = 2;
	}
	adc_set_regular_sequence(ADC1, 1, channel_array);
}