Example #1
0
/**
 * \brief Test physical loop-back with some characters in sunc mode.
 *
 * This function sends a character over USART on loop back to verify that init
 * and sending/receiving works. A jumper is connected on the USART.
 *
 * \param test Current test case.
 */
static void run_loopback_syncmode_test(const struct test_case *test)
{
	uint8_t out_c = 'c';
	uint8_t in_c  = 0;
        port_pin_t sck_pin;
        
        sysclk_enable_module(POWER_RED_REG0, PRUSART0_bm);
        
        usart_set_mode(&CONF_UNIT_USART, USART_CMODE_SYNCHRONOUS_gc);

	sck_pin = IOPORT_CREATE_PIN(PORTE, 2);
	ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
				ioport_pin_to_mask(sck_pin),
				IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH );
        
        usart_spi_set_baudrate(&CONF_UNIT_USART, CONF_UNIT_BAUDRATE,
			sysclk_get_source_clock_hz());
	usart_tx_enable(&CONF_UNIT_USART);
	usart_rx_enable(&CONF_UNIT_USART);

	usart_putchar(&CONF_UNIT_USART, out_c);
	in_c = usart_getchar(&CONF_UNIT_USART);

	test_assert_true(test, in_c == out_c,
	   "Read character through sync mode is not correct: %d != %d", in_c, out_c);			
}
Example #2
0
/**
 * \brief Initialize USART in SPI master mode.
 *
 * This function initializes the USART module in SPI master mode using the
 * usart_spi_options_t configuration structure and CPU frequency.
 *
 * \param usart The USART module.
 * \param opt The RS232 configuration option.
 */
void usart_init_spi(USART_t *usart, const usart_spi_options_t *opt)
{
	usart->UBRR = 0;

	usart_enable_module_clock(usart);
	usart_set_mode(usart, USART_CMODE_MSPI_gc);
	port_pin_t sck_pin;

#ifdef USARTA0
	if ((uintptr_t)usart == (uintptr_t)&UCSR0A) {
		sck_pin = IOPORT_CREATE_PIN(PORTE, 2);
		ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
				ioport_pin_to_mask(sck_pin),
				IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH );
	}
#endif
#ifdef USARTA1
	if ((uintptr_t)usart == (uintptr_t)&UCSR1A) {
		sck_pin = IOPORT_CREATE_PIN(PORTD, 5);
		ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
				ioport_pin_to_mask(sck_pin),
				IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH );
	}
#endif
	if (opt->spimode == 1 || opt->spimode == 3) {
		usart->UCSRnC |= USART_UCPHA_bm;
	} else {
		usart->UCSRnC &= ~USART_UCPHA_bm;
	}
	if (opt->spimode == 2 || opt->spimode == 3) {
		usart->UCSRnC |= USART_UCPOL_bm;
	} else {
		usart->UCSRnC &= ~USART_UCPOL_bm;
	}	
	
	if (opt->data_order) {
		usart->UCSRnC |= USART_DORD_bm;
	} else {
		usart->UCSRnC &= ~USART_DORD_bm;
	}
	
	
	usart_spi_set_baudrate(usart, opt->baudrate,
			sysclk_get_source_clock_hz());
	usart_tx_enable(usart);
	usart_rx_enable(usart);
}
Example #3
0
void ui_wakeup_enable(void)
{
	PORT_t *port;
	// Configure pin change interrupt for asynch. wake-up on button pin.
	ioport_configure_pin(GPIO_PUSH_BUTTON_0,
			IOPORT_DIR_INPUT | IOPORT_PULL_UP);
	port = ioport_pin_to_port(GPIO_PUSH_BUTTON_0);
	port->INT0MASK = 0x01;
	port->INTCTRL = PORT_INT0LVL_LO_gc;
}
Example #4
0
void ui_wakeup_disable(void)
{
	PORT_t *port;
	port = ioport_pin_to_port(GPIO_PUSH_BUTTON_0);
	port->INT0MASK = 0x00;
}
Example #5
0
/**
 * \brief Initialize USART in SPI master mode.
 *
 * This function initializes the USART module in SPI master mode using the
 * usart_spi_options_t configuration structure and CPU frequency.
 *
 * \param usart The USART module.
 * \param opt The RS232 configuration option.
 */
void usart_init_spi(USART_t *usart, const usart_spi_options_t *opt)
{
    usart_enable_module_clock(usart);
    usart_set_mode(usart, USART_CMODE_MSPI_gc);
    port_pin_t sck_pin;

    if (opt->spimode == 1 || opt->spimode == 3) {
        //! \todo Fix when UCPHA_bm is added to header file.
        usart->CTRLC |= 0x02;
    } else {
        //! \todo Fix when UCPHA_bm is added to header file.
        usart->CTRLC &= ~0x02;
    }

    // configure Clock polarity using INVEN bit of the correct SCK I/O port
    if (opt->spimode == 2 || opt->spimode == 3) {
#ifdef USARTC0
        if ((uint16_t)usart == (uint16_t)&USARTC0) {
            sck_pin = IOPORT_CREATE_PIN(PORTC, 1);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
#ifdef USARTC1
        if ((uint16_t)usart == (uint16_t)&USARTC1) {
            sck_pin = IOPORT_CREATE_PIN(PORTC, 5);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
#ifdef USARTD0
        if ((uint16_t)usart == (uint16_t)&USARTD0) {
            sck_pin = IOPORT_CREATE_PIN(PORTD, 1);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
#ifdef USARTD1
        if ((uint16_t)usart == (uint16_t)&USARTD1) {
            sck_pin = IOPORT_CREATE_PIN(PORTD, 5);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
#ifdef USARTE0
        if ((uint16_t)usart == (uint16_t)&USARTE0) {
            sck_pin = IOPORT_CREATE_PIN(PORTE, 1);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
#ifdef USARTE1
        if ((uint16_t)usart == (uint16_t)&USARTE1) {
            sck_pin = IOPORT_CREATE_PIN(PORTE, 5);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
#ifdef USARTF0
        if ((uint16_t)usart == (uint16_t)&USARTF0) {
            sck_pin = IOPORT_CREATE_PIN(PORTF, 1);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
#ifdef USARTF1
        if ((uint16_t)usart == (uint16_t)&USARTF1) {
            sck_pin = IOPORT_CREATE_PIN(PORTF, 5);
            ioport_configure_port_pin(ioport_pin_to_port(sck_pin),
                                      ioport_pin_to_mask(sck_pin),
                                      IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH | IOPORT_INV_ENABLED);
        }
#endif
    }

    usart_spi_set_baudrate(usart, opt->baudrate, sysclk_get_per_hz());
    usart_tx_enable(usart);
    usart_rx_enable(usart);
}
int main(void)
{
	// Set the sleep mode to initially lock.
	enum sleepmgr_mode mode = SLEEPMGR_ACTIVE;
	PORT_t *port;

	board_init();
	sysclk_init();

	// Turn on LED to indicate the device is active.
	ioport_set_pin_low(LED_PIN);

	// Configure pin change interrupt for asynch. wake-up on button pin.
	ioport_configure_pin(BUTTON_PIN, IOPORT_DIR_INPUT | IOPORT_PULL_UP |
			IOPORT_FALLING);

	port = ioport_pin_to_port(BUTTON_PIN);
#if XMEGA_E
	port->INTMASK = PIN2_bm;
	port->INTCTRL = PORT_INTLVL_LO_gc;
#else
	port->INT0MASK = PIN2_bm;
	port->INTCTRL = PORT_INT0LVL_LO_gc;
#endif


	// Enable RTC with ULP as clock source.
	sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_RTC);
	CLK.RTCCTRL = CLK_RTCSRC_ULP_gc | CLK_RTCEN_bm;

	// Configure RTC for wakeup at 1.5 second period (at 256x prescaling).
	RTC.PER = 6;
	RTC.INTCTRL = RTC_OVFINTLVL_LO_gc;

	// Wait until RTC is ready before continuing.
	do { } while (RTC.STATUS & RTC_SYNCBUSY_bm);

	// Initialize the sleep manager, lock initial mode.
	sleepmgr_init();
	sleepmgr_lock_mode(mode);

	// Enable low level interrupts for wakeups to occur.
	PMIC.CTRL = PMIC_LOLVLEN_bm;

	do {
		// Delay for 3 seconds to show the device is awake.
		mdelay(3000);

		// Turn off the LED, restart the RTC and go to sleep.
		ioport_set_pin_high(LED_PIN);
		RTC.CNT = 0;
		RTC.CTRL = RTC_PRESCALER_DIV256_gc;
		do { } while (RTC.STATUS & RTC_SYNCBUSY_bm);
		sleepmgr_enter_sleep();

		// Stop the RTC and turn on the LED.
		RTC.CTRL = RTC_PRESCALER_OFF_gc;
		ioport_set_pin_low(LED_PIN);

		// Unlock current mode, then lock the next one.
		sleepmgr_unlock_mode(mode);
		if (++mode < SLEEPMGR_NR_OF_MODES) {
			sleepmgr_lock_mode(mode);
		} else {
			mode = SLEEPMGR_ACTIVE;
			sleepmgr_lock_mode(mode);
		}
	} while (1);
}
Example #7
0
/*! \brief Install a sensor interrupt handler
 *
 * The Sensors Xplained add-on boards route sensor device I/O pins to GPIO
 * pins for the MCU installed on an Xplained platform board.  Some sensor
 * devices can be configured to generate interrupts on these pins to indicate
 * the availability of new sensor data or the occurrence of configurable
 * events related to sensor data thresholds, for example.
 *
 * This routine will enable interrupts on the GPIO pin specified by the
 * \c gpio_pin parameter and call a user-defined callback \c handler when an
 * interrupt is detected.  The \c arg parameter is used to pass the address
 * of user-defined input and output storage for the callback handler.  Calling
 * the routine with the \c handler parameter set to 0 (the NULL pointer) will
 * fail with \c false returned to the caller.
 *
 * \param   gpio_pin    Board-specific GPIO pin interface to the MCU.
 * \param   handler     The address of a driver-defined interrupt handler.
 * \param   arg         An optional address passed to the interrupt handler.
 *
 * \return  bool        true if the call succeeds, else false.
 */
bool sensor_board_irq_connect(uint32_t gpio_pin,
		SENSOR_IRQ_HANDLER handler, void *arg)
{
	bool status = false;

#if XMEGA
	PORT_t *sensor_port;
#endif

	/* Ensure that the caller has specified a function address. */

	if (handler == NULL) {
		return status;
	}

	/* Save the interrupt flag state and disable MCU interrupts. */

	irqflags_t const irq_flags = cpu_irq_save();

	cpu_irq_disable();

	/* Initialize an interrupt for a specified I/O pin. */

	if (SENSOR_BOARD_PIN3 == gpio_pin) {
		sensor_pin3_handler = handler;
		sensor_pin3_arg     = arg;

#if UC3
#  if defined(SENSOR_PIN3_EIC_LINE)
		eic_irq_connect(SENSOR_PIN3_EIC_LINE, SENSOR_PIN3_EIC_PIN,
				SENSOR_PIN3_EIC_FUNC, SENSOR_PIN3_EIC_IRQ,
				eic_pin3_handler);
#  else
		gpio_irq_connect(gpio_pin, SENSOR_PIN3_IRQ);
#  endif
#elif XMEGA
		sensor_port = ioport_pin_to_port(SENSOR_BOARD_PIN3);
		sensor_port->INTCTRL   = PORT_INT0LVL_LO_gc;
		sensor_port->INT0MASK |= ioport_pin_to_mask(SENSOR_BOARD_PIN3);
		/* Some Xplained kits have limited asynchronous sensing on most
		 * pins, which requires them to be sensing on both edges.
		 */
		ioport_set_pin_sense_mode(SENSOR_BOARD_PIN3,
				IOPORT_SENSE_BOTHEDGES);
#endif
		status = true;
	} else if (SENSOR_BOARD_PIN4 == gpio_pin) {
		sensor_pin4_handler = handler;
		sensor_pin4_arg     = arg;

#if UC3
#  if defined(SENSOR_PIN4_EIC_LINE)
		eic_irq_connect(SENSOR_PIN4_EIC_LINE, SENSOR_PIN4_EIC_PIN,
				SENSOR_PIN4_EIC_FUNC, SENSOR_PIN4_EIC_IRQ,
				eic_pin4_handler);
#  else
		gpio_irq_connect(gpio_pin, SENSOR_PIN4_IRQ);
#  endif
#elif XMEGA
		sensor_port = ioport_pin_to_port(SENSOR_BOARD_PIN4);
		sensor_port->INTCTRL   = PORT_INT0LVL_LO_gc;
		sensor_port->INT0MASK |= ioport_pin_to_mask(SENSOR_BOARD_PIN4);
		/* Some Xplained kits have limited asynchronous sensing on most
		 * pins, which requires them to be sensing on both edges.
		 */
		ioport_set_pin_sense_mode(SENSOR_BOARD_PIN4,
				IOPORT_SENSE_BOTHEDGES);
#endif
		status = true;
	} else if (SENSOR_BOARD_PIN5 == gpio_pin) {
		sensor_pin5_handler = handler;
		sensor_pin5_arg     = arg;

#if UC3
#  if defined(SENSOR_PIN5_EIC_LINE)
		eic_irq_connect(SENSOR_PIN5_EIC_LINE, SENSOR_PIN5_EIC_PIN,
				SENSOR_PIN5_EIC_FUNC, SENSOR_PIN5_EIC_IRQ,
				eic_pin5_handler);
#  else
		gpio_irq_connect(gpio_pin, SENSOR_PIN5_IRQ);
#  endif
#elif XMEGA
		sensor_port = ioport_pin_to_port(SENSOR_BOARD_PIN5);
		sensor_port->INTCTRL   = PORT_INT0LVL_LO_gc;
		sensor_port->INT0MASK |= ioport_pin_to_mask(SENSOR_BOARD_PIN5);
		/* Some Xplained kits have limited asynchronous sensing on most
		 * pins, which requires them to be sensing on both edges.
		 */
		ioport_set_pin_sense_mode(SENSOR_BOARD_PIN5,
				IOPORT_SENSE_BOTHEDGES);
#endif
		status = true;
	}

	/* Restore the MCU interrupt flag state. */

	cpu_irq_restore(irq_flags);

	return status;
}