Ejemplo n.º 1
0
static void adc_interval_read(int ain_id, int interval_ms)
{
	adc_configure(ain_id);

	/* EXTEN=01 -> hardware trigger detection on rising edge */
	STM32_ADC_CFGR1 = (STM32_ADC_CFGR1 & ~0xc00) | (1 << 10);

	/* EXTSEL=TRG3 -> Trigger on TIM3_TRGO */
	STM32_ADC_CFGR1 = (STM32_ADC_CFGR1 & ~0x1c0) | (3 << 6);

	__hw_timer_enable_clock(TIM_ADC, 1);

	/* Upcounter, counter disabled, update event only on underflow */
	STM32_TIM_CR1(TIM_ADC) = 0x0004;

	/* TRGO on update event */
	STM32_TIM_CR2(TIM_ADC) = 0x0020;
	STM32_TIM_SMCR(TIM_ADC) = 0x0000;

	/* Auto-reload value */
	STM32_TIM_ARR(TIM_ADC) = interval_ms & 0xffff;

	/* Set prescaler to tick per millisecond */
	STM32_TIM_PSC(TIM_ADC) = (clock_get_freq() / MSEC) - 1;

	/* Start counting */
	STM32_TIM_CR1(TIM_ADC) |= 1;

	/* Start ADC conversion */
	STM32_ADC_CR |= 1 << 2; /* ADSTART */
}
Ejemplo n.º 2
0
/**@brief Function for application main entry.
 */
int main(void)
{
    bool erase_bonds;

    // Initialize.
    app_trace_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    adc_configure();
    device_manager_init(erase_bonds);
    gap_params_init();
    advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);
    services_init();
    conn_params_init();

    // Start execution.
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        power_manage();
    }
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: tkadom/TWBLE
/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    // Initialize.
    app_trace_init();
    timers_init();
    gpiote_init();
    err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS,
                        APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),
                        button_event_handler);
    APP_ERROR_CHECK(err_code);
    ble_stack_init();
    adc_configure();
    device_manager_init();
    gap_params_init();
    advertising_init(BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE);
    services_init();
    conn_params_init();

    // Start execution.
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        power_manage();
    }
}
Ejemplo n.º 4
0
int adc_read_channel(enum adc_channel ch)
{
	const struct adc_t *adc = adc_channels + ch;
	int value;
	int restore_watchdog = 0;

	mutex_lock(&adc_lock);
	if (adc_watchdog_enabled()) {
		restore_watchdog = 1;
		adc_disable_watchdog_no_lock();
	}

	adc_configure(adc->channel);

	/* Clear flags */
	STM32_ADC_ISR = 0xe;

	/* Start conversion */
	STM32_ADC_CR |= 1 << 2; /* ADSTART */

	/* Wait for end of conversion */
	while (!(STM32_ADC_ISR & (1 << 2)))
		;
	/* read converted value */
	value = STM32_ADC_DR;

	if (restore_watchdog)
		adc_enable_watchdog_no_lock();
	mutex_unlock(&adc_lock);

	return value * adc->factor_mul / adc->factor_div + adc->shift;
}
Ejemplo n.º 5
0
void adc_open(int fifo_sz) {
    adc1_fifo = fifo_create(fifo_sz);
    assert(adc1_fifo != NULL);

    tim2_config();
    adc_configure();
}
Ejemplo n.º 6
0
void adc_open(void) {
    DMA2_Stream0_fifo = fifo_create(FIFO_SZ);
    assert(DMA2_Stream0_fifo != NULL);

    Timer1Config();
    adc_configure();
    ADC_SoftwareStartConv(ADC1);
}
Ejemplo n.º 7
0
static void adc_continuous_read(int ain_id)
{
	adc_configure(ain_id);

	/* CONT=1 -> continuous mode on */
	STM32_ADC_CFGR1 |= 1 << 13;

	/* Start continuous conversion */
	STM32_ADC_CR |= 1 << 2; /* ADSTART */
}
Ejemplo n.º 8
0
static void adc_init(void)
{
	int i;

	/* Configure GPIOs */
	configure_gpio();

	/*
	 * Temporarily enable the PLL when turning on the clock to the ADC
	 * module, to work around chip errata (10.4).  No need to notify
	 * other modules; the PLL isn't enabled long enough to matter.
	 */
	clock_enable_pll(1, 0);

	/* Enable ADC0 module in run and sleep modes. */
	clock_enable_peripheral(CGC_OFFSET_ADC, 0x1,
			CGC_MODE_RUN | CGC_MODE_SLEEP);

	/*
	 * Use external voltage references (VREFA+, VREFA-) instead of
	 * VDDA and GNDA.
	 */
	LM4_ADC_ADCCTL = 0x01;

	/* Use internal oscillator */
	LM4_ADC_ADCCC = 0x1;

	/* Disable the PLL now that the ADC is using the internal oscillator */
	clock_enable_pll(0, 0);

	/* No tasks waiting yet */
	for (i = 0; i < LM4_ADC_SEQ_COUNT; i++)
		task_waiting_on_ss[i] = TASK_ID_INVALID;

	/* Enable IRQs */
	task_enable_irq(LM4_IRQ_ADC0_SS0);
	task_enable_irq(LM4_IRQ_ADC0_SS1);
	task_enable_irq(LM4_IRQ_ADC0_SS2);
	task_enable_irq(LM4_IRQ_ADC0_SS3);

	/* 2**6 = 64x oversampling */
	LM4_ADC_ADCSAC = 6;

	/* Initialize ADC sequencer */
	for (i = 0; i < ADC_CH_COUNT; ++i)
		adc_configure(adc_channels + i);

	/* Disable ADC0 module until it is needed to conserve power. */
	clock_disable_peripheral(CGC_OFFSET_ADC, 0x1,
			CGC_MODE_RUN | CGC_MODE_SLEEP);
}
Ejemplo n.º 9
0
void acc_init(void)
{
  volatile avr32_adc_t *adc = &AVR32_ADC;
  static const gpio_map_t ADC_GPIO_MAP =
  {
    {AVR32_ADC_AD_3_PIN,  AVR32_ADC_AD_3_FUNCTION}, // ADC channel 3
    {AVR32_ADC_AD_1_PIN,  AVR32_ADC_AD_1_FUNCTION}, // ADC channel 1
    {AVR32_ADC_AD_2_PIN,  AVR32_ADC_AD_2_FUNCTION}  // ADC channel 2
  };

  // enable GPIO pins for ADC
  gpio_enable_module(ADC_GPIO_MAP,
                     sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0]));

  // configure ADC
  adc_configure(adc);
}
Ejemplo n.º 10
0
void battery_callback()
{
	if (!charging_allowed)
		return;

	adc_configure(&batt_adc_config);
    BATTERY_VOLTAGE_TRIS = 1;
	BATTERY_VOLTAGE_DIGITAL = 0;

    adc_set_channel(1);
    last_battery_voltage = adc_convert_one();

    //Disable charging if battery voltage is greater than max charge level
    if (last_battery_voltage >= kBatteryChargedLevel)
    	disable_charging();
    else if (last_battery_voltage < kBatteryHysteresisLevel) //Reenable charging once battery level falls below hysteresis
    	enable_charging();
}
Ejemplo n.º 11
0
int adc_read_channel(enum adc_channel ch)
{
	const struct adc_t *adc = adc_channels + ch;
	int value;
	int restore_watchdog = 0;
	timestamp_t deadline;

	if (!adc_powered())
		return EC_ERROR_UNKNOWN;

	mutex_lock(&adc_lock);

	if (adc_watchdog_enabled()) {
		restore_watchdog = 1;
		adc_disable_watchdog_no_lock();
	}

	adc_configure(adc->channel);

	/* Clear EOC bit */
	STM32_ADC_SR &= ~(1 << 1);

	/* Start conversion */
	STM32_ADC_CR2 |= (1 << 0); /* ADON */

	/* Wait for EOC bit set */
	deadline.val = get_time().val + ADC_SINGLE_READ_TIMEOUT;
	value = ADC_READ_ERROR;
	do {
		if (adc_conversion_ended()) {
			value = STM32_ADC_DR & ADC_READ_MAX;
			break;
		}
	} while (!timestamp_expired(deadline, NULL));

	if (restore_watchdog)
		adc_enable_watchdog_no_lock();

	mutex_unlock(&adc_lock);
	return (value == ADC_READ_ERROR) ? ADC_READ_ERROR :
	       value * adc->factor_mul / adc->factor_div + adc->shift;
}
Ejemplo n.º 12
0
static int adc_enable_watchdog_no_lock(void)
{
	/* Select channel */
	STM32_ADC_CFGR1 = (STM32_ADC_CFGR1 & ~0x7c000000) |
			  (watchdog_ain_id << 26);
	adc_configure(watchdog_ain_id);

	/* Clear AWD interupt flag */
	STM32_ADC_ISR = 0x80;
	/* Set Watchdog enable bit on a single channel */
	STM32_ADC_CFGR1 |= (1 << 23) | (1 << 22);
	/* Enable interrupt */
	STM32_ADC_IER |= 1 << 7;

	if (watchdog_delay_ms)
		adc_interval_read(watchdog_ain_id, watchdog_delay_ms);
	else
		adc_continuous_read(watchdog_ain_id);

	return EC_SUCCESS;
}
Ejemplo n.º 13
0
/** \brief Prepare ADC for touch measuring.
 *
 * Register the interrupt handler, set sample, hold and startup time.
 */
static void inline rtouch_prepare_adc(void)
{
	Disable_global_interrupt();
	INTC_register_interrupt(&rtouch_adc_int_handler, RTOUCH_ADC_IRQ,
			RTOUCH_ADC_INT_LEVEL);
	Enable_global_interrupt();

#ifdef AVR32_ADCIFA
	volatile avr32_adcifa_t *adcifa = &RTOUCH_ADC;

	/* configure ADCIFA */
	adcifa_configure(adcifa, &adcifa_opt, FOSC0);
#else
	volatile avr32_adc_t *adc = &RTOUCH_ADC;

	adc_configure(adc);

	/* we need to lower the adc clock under 5MHz */
	/* adc_clock = adc_input_clock /((prescaler + 1)*2) */
	adc->mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
#endif
}
Ejemplo n.º 14
0
static void ui_adc_init(void)
{
	/* GPIO pin/adc-function map */
	static const gpio_map_t ADC_GPIO_MAP = {
#if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL)
		{EXAMPLE_ADC_TEMPERATURE_PIN, EXAMPLE_ADC_TEMPERATURE_FUNCTION},
#endif
#if defined(EXAMPLE_ADC_LIGHT_CHANNEL)
		{EXAMPLE_ADC_LIGHT_PIN, EXAMPLE_ADC_LIGHT_FUNCTION},
#endif
#if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL)
		{EXAMPLE_ADC_POTENTIOMETER_PIN,
		 EXAMPLE_ADC_POTENTIOMETER_FUNCTION}
#endif
	};

	/* Assign and enable GPIO pins to the ADC function */
	gpio_enable_module(ADC_GPIO_MAP,
			sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0]));

	/* configure ADC
	 * Lower the ADC clock to match the ADC characteristics (because we
	 * configured the CPU clock to 12MHz, and the ADC clock characteristics
	 * are usually lower;
	 * cf. the ADC Characteristic section in the datasheet) */
	AVR32_ADC.mr |= AVR32_ADC_MR_PRESCAL_MASK;
	adc_configure(&AVR32_ADC);

	/* Enable the ADC channels */
#if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL)
	adc_enable(&AVR32_ADC, EXAMPLE_ADC_TEMPERATURE_CHANNEL);
#endif
#if defined(EXAMPLE_ADC_LIGHT_CHANNEL)
	adc_enable(&AVR32_ADC, EXAMPLE_ADC_LIGHT_CHANNEL);
#endif
#if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL)
	adc_enable(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL);
#endif
}
void init_LCD(void){
	 static const gpio_map_t DIP204_SPI_GPIO_MAP =
	 {
		 {DIP204_SPI_SCK_PIN,  DIP204_SPI_SCK_FUNCTION },  // SPI Clock.
		 {DIP204_SPI_MISO_PIN, DIP204_SPI_MISO_FUNCTION},  // MISO.
		 {DIP204_SPI_MOSI_PIN, DIP204_SPI_MOSI_FUNCTION},  // MOSI.
		 {DIP204_SPI_NPCS_PIN, DIP204_SPI_NPCS_FUNCTION}   // Chip Select NPCS.
	 };
	 spi_options_t spiOptions =
	 {
		 .reg          = DIP204_SPI_NPCS,
		 .baudrate     = 1000000,
		 .bits         = 8,
		 .spck_delay   = 0,
		 .trans_delay  = 0,
		 .stay_act     = 1,
		 .spi_mode     = 0,
		 .modfdis      = 1
	 };
	 gpio_enable_module(DIP204_SPI_GPIO_MAP,
	 sizeof(DIP204_SPI_GPIO_MAP) / sizeof(DIP204_SPI_GPIO_MAP[0]));
	  spi_initMaster(DIP204_SPI, &spiOptions);
	  spi_selectionMode(DIP204_SPI, 0, 0, 0);
	  spi_enable(DIP204_SPI);
	  spi_setupChipReg(DIP204_SPI, &spiOptions, FOSC0);
	  dip204_init(backlight_PWM, true);
	  clear_Display();
	  dip204_hide_cursor();
}
void init_Potentiometer(void){
	const gpio_map_t ADC_GPIO_MAP = {
		{EXAMPLE_ADC_POTENTIOMETER_PIN, EXAMPLE_ADC_POTENTIOMETER_FUNCTION}
	};
	gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) /
	sizeof(ADC_GPIO_MAP[0]));
	AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
	adc_configure(&AVR32_ADC);
	adc_enable(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL);
	adc_start(&AVR32_ADC);
}
void init_CurrentSensor(void){
	#define AVR32_ADC_AD_1_PIN 22
	const gpio_map_t ADC_GPIO_MAP = {
		{AVR32_ADC_AD_3_PIN, AVR32_ADC_AD_3_FUNCTION}
	};
	gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) /
	sizeof(ADC_GPIO_MAP[0]));
	AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
	adc_configure(&AVR32_ADC);
	adc_enable(&AVR32_ADC, 3);
	adc_start(&AVR32_ADC);
}
void clear_Line(int line){
	for(int i = 0; i<21;i++){
		dip204_set_cursor_position(i,line);
		dip204_write_string(" ");
	}
}
void set_Direccion(int direccion){
	if(direccion == 1){
		clear_Line(2);
		dip204_set_cursor_position(1,2);
		dip204_write_string("Direccion:");
		dip204_set_cursor_position(12,2);
		dip204_write_string("Forward");
	}
	if(direccion == 0){
		clear_Line(2);
		dip204_set_cursor_position(1,2);
		dip204_write_string("Direccion:");
		dip204_set_cursor_position(12,2);
		dip204_write_string("Reverse");
	}
}
void set_Velocidad(int velocidad){
	clear_Line(3);
	dip204_set_cursor_position(1,3);
	dip204_write_string("Velocidad:");
	dip204_set_cursor_position(12,3);
	switch (velocidad){
		case 1:
			dip204_write_string("1");
			break;
		case 2:
			dip204_write_string("2");
			break;
		case 3:
			dip204_write_string("3");
			break;
		case 4:
			dip204_write_string("4");
			break;
		case 5:
			dip204_write_string("5");
			break;
		case 6:
			dip204_write_string("6");
			break;
		case 7:
			dip204_write_string("7");
			break;
		case 8:
			dip204_write_string("8");
			break;
		case 9:
			dip204_write_string("9");
			break;
		case 10:
		dip204_write_string("10");
		break;
	}//SWITCH
}
Ejemplo n.º 16
0
/** \brief Main application entry point - init and loop to display ADC values */
int main(void)
{
#if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL)
	signed short adc_value_temp  = -1;
#endif
#if defined(EXAMPLE_ADC_LIGHT_CHANNEL)
	signed short adc_value_light = -1;
#endif
#if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL)
	signed short adc_value_pot   = -1;
#endif

	/* Init system clocks */
	sysclk_init();

	/* init debug serial line */
	init_dbg_rs232(sysclk_get_cpu_hz());

	/* Assign and enable GPIO pins to the ADC function. */
	gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) /
			sizeof(ADC_GPIO_MAP[0]));

	/* Configure the ADC peripheral module.
	 * Lower the ADC clock to match the ADC characteristics (because we
	 * configured the CPU clock to 12MHz, and the ADC clock characteristics are
	 *  usually lower; cf. the ADC Characteristic section in the datasheet). */
	AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
	adc_configure(&AVR32_ADC);

	/* Enable the ADC channels. */
#if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL)
	adc_enable(&AVR32_ADC, EXAMPLE_ADC_TEMPERATURE_CHANNEL);
#endif
#if defined(EXAMPLE_ADC_LIGHT_CHANNEL)
	adc_enable(&AVR32_ADC, EXAMPLE_ADC_LIGHT_CHANNEL);
#endif
#if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL)
	adc_enable(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL);
#endif

	/* Display a header to user */
	print_dbg("\x1B[2J\x1B[H\r\nADC Example\r\n");

	while (true) {
		/* Start conversions on all enabled channels */
		adc_start(&AVR32_ADC);

#if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL)
		/* Get value for the temperature adc channel */
		adc_value_temp = adc_get_value(&AVR32_ADC,
				EXAMPLE_ADC_TEMPERATURE_CHANNEL);

		/* Display value to user */
		print_dbg("HEX Value for Channel temperature : 0x");
		print_dbg_hex(adc_value_temp);
		print_dbg("\r\n");
#endif

#if defined(EXAMPLE_ADC_LIGHT_CHANNEL)
		/* Get value for the light adc channel */
		adc_value_light = adc_get_value(&AVR32_ADC,
				EXAMPLE_ADC_LIGHT_CHANNEL);
		
		/* Display value to user */
		print_dbg("HEX Value for Channel light : 0x");
		print_dbg_hex(adc_value_light);
		print_dbg("\r\n");
#endif

#if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL)
		/* Get value for the potentiometer adc channel */
		adc_value_pot = adc_get_value(&AVR32_ADC,
				EXAMPLE_ADC_POTENTIOMETER_CHANNEL);
				
		/* Display value to user */
		print_dbg("HEX Value for Channel pot : 0x");
		print_dbg_hex(adc_value_pot);
		print_dbg("\r\n");
#endif

		/* Slow down the display of converted values */
		delay_ms(500);
	}

	return 0;
}