void demo_init_pwma(void)
{
  // Enable the pins driving the LEDs with the PWMA function (functionality E).
  gpio_enable_module_pin(LED0_GPIO, LED0_PWM_FUNCTION);
  gpio_enable_module_pin(LED1_GPIO, LED1_PWM_FUNCTION);
  gpio_enable_module_pin(LED2_GPIO, LED2_PWM_FUNCTION);
  gpio_enable_module_pin(LED3_GPIO, LED3_PWM_FUNCTION);

  //#
  //# Setup and enable the "PWMA and GCLK3 pin" generic clock to 120MHz
  //#
  // Start the RC120M clock
  scif_start_rc120M();
  // Setup the generic clock at 120MHz
  scif_gc_setup(  AVR32_PM_GCLK_PWMA, // The generic clock to setup
                  SCIF_GCCTRL_RC120M, // The input clock source to use for the generic clock
                  false,              // Disable the generic clock divisor
                  0);                 // divfactor = DFLL0freq/gclkfreq
  // Enable the generic clock
  scif_gc_enable(AVR32_PM_GCLK_PWMA);

  //#
  //# Enable all the PWMAs to output to all LEDs. LEDs are default turned on by
  //# setting PWM duty cycles to 0.
  //#
  pwma_config_enable(&AVR32_PWMA,PWMA_OUTPUT_FREQUENCY,PWMA_GCLK_FREQUENCY,0);
  pwma_set_channels_value(&AVR32_PWMA,LED_PWMA_CHANNELS_MASK,PWMA_DUTY_CYCLE_INIT_VAL);
}
Beispiel #2
0
/**
 * \brief Initialize ADC driver to read the board temperature sensor.
 *
 * Initializes the board's ADC driver module and configures the ADC channel
 * connected to the onboard NTC temperature sensor ready for conversions.
 */
static void init_adc(void)
{
	// Assign and enable GPIO pin to the ADC function.
	gpio_enable_module_pin(ADC_TEMPERATURE_PIN, ADC_TEMPERATURE_FUNCTION);

	const adcifb_opt_t adcifb_opt = {
		.resolution             = AVR32_ADCIFB_ACR_RES_12BIT,
		.shtim                  = 15,
		.ratio_clkadcifb_clkadc = 2,
		.startup                = 3,
		.sleep_mode_enable      = false
	};

	// Enable and configure the ADCIFB module
	sysclk_enable_peripheral_clock(&AVR32_ADCIFB);
	adcifb_configure(&AVR32_ADCIFB, &adcifb_opt);

	// Configure the trigger (No trigger, only software trigger)
	adcifb_configure_trigger(&AVR32_ADCIFB, AVR32_ADCIFB_TRGMOD_NT, 0);

	// Enable the ADCIFB channel to NTC temperature sensor
	adcifb_channels_enable(&AVR32_ADCIFB, ADC_TEMPERATURE_CHANNEL);
}

/**
 * \brief Initializes the USART.
 *
 * Initializes the board USART ready for serial data to be transmitted and
 * received.
 */
static void init_usart(void)
{
	const usart_options_t usart_options = {
		.baudrate     = 57600,
		.charlength   = 8,
		.paritytype   = USART_NO_PARITY,
		.stopbits     = USART_1_STOPBIT,
		.channelmode  = USART_NORMAL_CHMODE
	};

	// Initialize USART in RS232 mode with the requested settings.
	sysclk_enable_peripheral_clock(USART);
	usart_init_rs232(USART, &usart_options, sysclk_get_pba_hz());
}

/**
 * \brief Initializes the PWM subsystem ready to generate the RGB LED PWM
 * waves.
 *
 * Initializes the on-chip PWM module and configures the RGB LED PWM outputs so
 * the the brightness of the three individual channels can be adjusted.
 */
static void init_pwm(void)
{
	// GPIO pin/function map for the RGB LEDs.
	gpio_enable_module_pin(LED_RED_PWMA,   LED_PWMA_FUNCTION);
	gpio_enable_module_pin(LED_GREEN_PWMA, LED_PWMA_FUNCTION);
	gpio_enable_module_pin(LED_BLUE_PWMA,  LED_PWMA_FUNCTION);

	const scif_gclk_opt_t genclk3_opt = {
		.clock_source = SCIF_GCCTRL_CPUCLOCK,
		.divider      = 8,
		.diven        = true,
	};

	// Start generic clock 3 for the PWM outputs.
	scif_start_gclk(AVR32_PM_GCLK_GCLK3, &genclk3_opt);

	// Enable RGB LED PWM.
	sysclk_enable_peripheral_clock(&AVR32_PWMA);
	pwma_config_enable(&AVR32_PWMA,EXAMPLE_PWMA_FREQUENCY,EXAMPLE_PWMA_GCLK_FREQUENCY,0); 
	pwma_set_channels_value(&AVR32_PWMA,PWM_CHANNEL_RED | PWM_CHANNEL_BLUE| PWM_CHANNEL_GREEN,255);

}

/**
 * \brief Application main loop.
 */
int main(void)
{
	board_init();
	sysclk_init();

	sysclk_enable_peripheral_clock(USART);

	// Initialize touch, ADC, USART and PWM
	init_adc();
	init_usart();
	init_pwm();
	init_touch();

	while (true) {
		uint32_t adc_data;

		// Read slider and button and update RGB led
		touch_handler();

		// Wait until the ADC is ready to perform a conversion.
		do { } while (!adcifb_is_ready(&AVR32_ADCIFB));

		// Start an ADCIFB conversion sequence.
		adcifb_start_conversion_sequence(&AVR32_ADCIFB);

		// Wait until the converted data is available.
		do { } while (!adcifb_is_drdy(&AVR32_ADCIFB));

		// Get the last converted data.
		adc_data = (adcifb_get_last_data(&AVR32_ADCIFB) & 0x3FF);

		// Write temperature data to USART
		do { } while (!usart_tx_empty(USART));
		usart_write_char(USART, (adc_data >> 8));
		do { } while (!usart_tx_empty(USART));
		usart_write_char(USART, (adc_data & 0xFF));
	}
}
Beispiel #3
0
/**
 * \brief Main Application Routine
 *  -Initialize the system clocks                               \n
 *  -Configure and Enable the PWMA Generic clock                \n
 *  -Configure and Enable the PWMA Module                       \n
 *  -Load Duty cycle value using Interlinked multi-value mode   \n
 *  -Register and Enable the Interrupt                          \n
 *  -Enter into sleep mode                                      \n
 */
int main (void)
{
	uint32_t div;
	bool config_status = FAIL;
	bool set_value_status = FAIL;

	/*
	 * Duty cycle value to be loaded. As Two channels are used 
	 * in this example, two values has been assigned. Initialize the unused
	 * channel duty cycle value to 0, as it may take some garbage values 
	 * and will return FAIL while updating duty cycle as the value might 
	 * exceed the limit NOTE : Maximum four values can be loaded at a time, 
	 * as the channel limitation for Interlinked multi-value mode is four.
	 */ 
	uint16_t duty_cycle[] = {11,5,0,0};

	/* PWMA Pin and Function Map */
	static const gpio_map_t PWMA_GPIO_MAP = {
		{EXAMPLE_PWMA_PIN1, EXAMPLE_PWMA_FUNCTION1},
		{EXAMPLE_PWMA_PIN2, EXAMPLE_PWMA_FUNCTION2},
	};

	/* Enable the PWMA Pins */
	gpio_enable_module(PWMA_GPIO_MAP,
			sizeof(PWMA_GPIO_MAP) / sizeof(PWMA_GPIO_MAP[0]));

	/*
	 * Calculate the division factor value to be loaded and
	 * Enable the Generic clock
	 */
	div = div_ceil((sysclk_get_pba_hz()) , EXAMPLE_PWMA_GCLK_FREQUENCY);
	genclk_enable_config(EXAMPLE_PWMA_GCLK_ID,EXAMPLE_PWMA_GCLK_SOURCE,div);

	/*
	 * Initialize the System clock 
	 * Note: Clock should be configured in conf_clock.h
	 */
	sysclk_init();
	
	/* Initialize the delay routines */
	delay_init(sysclk_get_cpu_hz());

	/*
	 * Configure and Enable the PWMA Module. The LED will turned if 
	 * configuration fails because of invalid argument.
	 */
	config_status = pwma_config_enable(pwma, EXAMPLE_PWMA_OUTPUT_FREQUENCY,
								EXAMPLE_PWMA_GCLK_FREQUENCY, PWMA_SPREAD);

	/* Error in configuring the PWMA module */
	if (config_status == FAIL){
		while (1) {
			delay_ms(10);
			gpio_tgl_gpio_pin(ERROR_LED);
		}
	}

	/* Load the duty cycle values using Interlinked multi-value mode */
	set_value_status = pwma_set_multiple_values(pwma, 
			((EXAMPLE_PWMA_CHANNEL_ID1<<0) | (EXAMPLE_PWMA_CHANNEL_ID2<<8)),
			(uint16_t*)&duty_cycle);

	/* Error in loading the duty cycle value */
	if (set_value_status == FAIL){
		while (1) {
			delay_ms(10);
			gpio_tgl_gpio_pin(ERROR_LED);
		}
	}

	/* Disable global interrupts */
	cpu_irq_disable();

	/*
	 * Initialize the interrupt vectors
	 * Note: This function adds nothing for IAR as the interrupts are
	 * handled by the IAR compiler itself. It provides an abstraction
	 * between GCC & IAR compiler to use interrupts.
	 * Refer function implementation in interrupt_avr32.h
	 */
	irq_initialize_vectors();

	/*
	 * Register the ACIFB interrupt handler
	 * Note: This function adds nothing for IAR as the interrupts are
	 * handled by the IAR compiler itself. It provides an abstraction
	 * between GCC & IAR compiler to use interrupts.
	 * Refer function implementation in interrupt_avr32.h
	 */
	irq_register_handler(&tofl_irq, AVR32_PWMA_IRQ, PWMA_INTERRUPT_PRIORITY);

	/* Enable the Timebase overflow interrupt */
	pwma->ier = AVR32_PWMA_IER_TOFL_MASK;

	/* Enable global interrupt */
	cpu_irq_enable();

	/* Go to sleep mode */
	while(1){
		/* Enter into sleep mode */
		pm_sleep(AVR32_PM_SMODE_FROZEN);
	}
}  /* End of main() */