Example #1
0
//------------  --------------------
//-----湿度读取子程序 ------------
//----------------------  ----------
//----以下变量均为全局变量--------
//----温度高8位== U8T_data_H------
//----温度低8位== U8T_data_L------
//----湿度高8位== U8RH_data_H-----
//----湿度低8位== U8RH_data_L-----
//----校验 8位 == U8checkdata-----
//----调用相关子程序如下----------
//---- delay_us();, delay_ms();COM();
//---------------------    -----------
void RH(void)
{
	//主机拉低18ms
	port_pin_set_config(DATA, &pinc);
	port_pin_set_output_level(DATA, false);
	delay_ms(18);
	port_pin_set_output_level(DATA, true);
	//总线由上拉电阻拉高 主机延时20us
	delay_us(20);
	//主机设为输入 判断从机响应信号
	port_pin_set_output_level(DATA, true);
	port_pin_set_config(DATA, &pini);
	//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行
	
	if(!port_pin_get_input_level(DATA)) //T !
	{
		U8FLAG=2;
		//判断从机是否发出 80us 的低电平响应信号是否结束
		
		while((!port_pin_get_input_level(DATA))&&U8FLAG++);
		U8FLAG=2;
		
		//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
		while((port_pin_get_input_level(DATA))&&U8FLAG++);
		//数据接收状态
		Run_COM();
		
		U8RH_data_H_temp=U8comdata;
		Run_COM();
		U8RH_data_L_temp=U8comdata;
		Run_COM();
		U8T_data_H_temp=U8comdata;
		Run_COM();
		U8T_data_L_temp=U8comdata;
		Run_COM();
		U8checkdata_temp=U8comdata;
		port_pin_set_config(DATA, &pinc);
		port_pin_set_output_level(DATA, true);
		//数据校验
		
		U8Temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
		
		if(U8Temp==U8checkdata_temp)
		{
			
			U8RH_data_H=U8RH_data_H_temp;
			U8RH_data_L=U8RH_data_L_temp;
			U8T_data_H=U8T_data_H_temp;
			U8T_data_L=U8T_data_L_temp;
			U8checkdata=U8checkdata_temp;
			
		}//fi
	}//fi
}
Example #2
0
void ui_process(uint16_t framenumber)
{
	bool b_btn_state;
	static bool btn0_last_state = false;
	static uint8_t cpt_sof = 0;

	// Blink LED
	if ((framenumber % 1000) == 0) {
		LED_On(LED_0_PIN);
	}
	if ((framenumber % 1000) == 500) {
		LED_Off(LED_0_PIN);
	}

	// Scan process running each 40ms
	cpt_sof++;
	if (cpt_sof < 40) {
		return;
	}
	cpt_sof = 0;
	// Scan buttons

	b_btn_state = !port_pin_get_input_level(BUTTON_0_PIN);
	if (b_btn_state != btn0_last_state) {
		ui_multitouch_down();
		ui_step++;
		btn0_last_state = b_btn_state;
	}
}
Example #3
0
void ui_process(uint16_t framenumber)
{
	bool b_btn_state;
	static bool btn0_last_state = false;
	static uint8_t cpt_sof = 0;

	// Blink LED
	if (ui_b_led_blink) {
		if ((framenumber % 1000) == 0) {
			LED_On(LED_0_PIN);
		}
		if ((framenumber % 1000) == 500) {
			LED_Off(LED_0_PIN);
		}
	}

	// Scan process running each 40ms
	cpt_sof++;
	if (cpt_sof < 40) {
		return;
	}
	cpt_sof = 0;

	// Scan buttons
	b_btn_state = !port_pin_get_input_level(BUTTON_0_PIN);
	if (b_btn_state != btn0_last_state) {
		ui_hid_report[0] = b_btn_state;
		udi_hid_generic_send_report_in(ui_hid_report);
		btn0_last_state = b_btn_state;
	}
}
int main(void)
{
    system_init();

    //! [setup_init]
    configure_extint_channel();
    //! [setup_init]

    //! [main]
    while (true) {
        //! [main_1]
        if (extint_chan_is_detected(BUTTON_0_EIC_LINE)) {
            //! [main_1]

            //! [main_2]
            // Do something in response to EXTINT edge detection
            bool button_pin_state = port_pin_get_input_level(BUTTON_0_PIN);
            port_pin_set_output_level(LED_0_PIN, button_pin_state);
            //! [main_2]

            //! [main_3]
            extint_chan_clear_detected(BUTTON_0_EIC_LINE);
            //! [main_3]
        }
    }
    //! [main]
}
Example #5
0
File: ui.c Project: Dewb/mod
void ui_process(uint16_t framenumber)
{
	static uint8_t cpt_sof = 0;
	bool b_btn_state;
	static bool btn0_last_state = false;

	if (!associated) {
		if ((framenumber % 1000) == 0) {
			LED_On();
		}
		if ((framenumber % 1000) == 500) {
			LED_Off();
		}
	} else {
		LED_On();
	}

	/* Scan process running each 20ms */
	cpt_sof++;
	if (20 > cpt_sof) {
		return;
	}

	cpt_sof = 0;

	/* Use buttons to send measures */
	b_btn_state = !port_pin_get_input_level(BUTTON_0_PIN);
	if (b_btn_state != btn0_last_state) {
		btn0_last_state = b_btn_state;
		if (b_btn_state) {
			ieee11073_skeleton_send_measure_1();
		}
	}
}
Example #6
0
int main(void)
{
//! [main_setup]
//! [system_init]
	system_init();
//! [system_init]
//! [run_config]
	configure_spi_master();
//! [run_config]
//! [main_setup]

//! [main_use_case]
//! [inf_loop]
	while (true) {
		/* Infinite loop */
		if(!port_pin_get_input_level(BUTTON_0_PIN)) {
			//! [select_slave]
			spi_select_slave(&spi_master_instance, &slave, true);
			//! [select_slave]
			//! [write]
			spi_write_buffer_wait(&spi_master_instance, buffer, BUF_LENGTH);
			//! [write]
			//! [deselect_slave]
			spi_select_slave(&spi_master_instance, &slave, false);
			//! [deselect_slave]
			//! [light_up]
			port_pin_set_output_level(LED_0_PIN, LED0_ACTIVE);
			//! [light_up]
		}
	}
//! [inf_loop]
//! [main_use_case]
}
/*
 * Determine if button is pressed
 *
 * \return true if button is pressed, else false
 */
bool button_pressed(void)
{
#if SAMD || SAMR21
	if (port_pin_get_input_level(SW0_PIN)) {
		return false;
	} else {
		return true;
	}
#endif

#ifdef SENSOR_TERMINAL_BOARD
	if (stb_button_read()) {
		return true;
	} else {
		return false;
	}

#elif defined GPIO_PUSH_BUTTON_0   /*Read the current state of the button*/
	if (ioport_get_pin_level(GPIO_PUSH_BUTTON_0)) {
		return false;
	} else {
		return true;
	}
#endif

	return false;
}
Example #8
0
//! [callback_eic]
static void eic_callback_to_clear_halt(void)
{
	if (port_pin_get_input_level(CONF_FAULT_EIC_PIN)) {
		tcc_clear_status(&tcc_instance,
				TCC_STATUS_RECOVERABLE_FAULT_PRESENT(CONF_PWM_CHANNEL) |
				TCC_STATUS_RECOVERABLE_FAULT_OCCUR(CONF_PWM_CHANNEL));
	}
}
Example #9
0
/* Updates the board LED to the current button state. */
static void update_led_state(void)
{
	bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
	if (pin_state) {
		port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
	} else {
		port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
	}
}
Example #10
0
void Run_COM(void)
{
	unsigned char  i;
	for(i=0;i<8;i++)
	{
		U8FLAG=2;
		
		while((!port_pin_get_input_level(DATA))&&U8FLAG++);
		delay_us(15);
		U8Temp=0;
		if(port_pin_get_input_level(DATA))U8Temp=1;
		U8FLAG=2;
		while((port_pin_get_input_level(DATA))&&U8FLAG++);
		//超时则跳出for循环
		if(U8FLAG==1)break;
		//判断数据位是0还是1
		// 如果高电平高过预定0高电平值则数据位为 1
		U8comdata<<=1;
		U8comdata|=U8Temp; //0
	}//rof
}
Example #11
0
/*---------------------------------------------------------------------------*/
static void
extint_detection_callback(void)
{
  bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
  pin_state = pin_state;

  if(!timer_expired(&debouncetimer)) {
    return;
  }

  sensors_changed(&button_sensor);
  timer_set(&debouncetimer, DEBOUNCE_TIME);
}
Example #12
0
void ui_usb_sof_event(void)
{
	bool b_btn_state;
	static bool btn_suspend = false;
	static uint16_t counter_sof = 0;

	if (ui_enum_status == UHC_ENUM_SUCCESS) {
		/* Display device enumerated and in active mode */
		if (++counter_sof > ui_device_speed_blink) {
			counter_sof = 0;
			if (ui_hid_mouse_plug || ui_msc_plug) {
				LED_Toggle(LED_0_PIN);
			} else {
				LED_On(LED_0_PIN);
			}
			if (ui_test_done) {
				if (ui_test_result) {
					/* Test successful */
					LED_On(LED_0_PIN);
				} else {
					/* Test fail */
					LED_Off(LED_0_PIN);
				}
			}
		}
		/* Scan button to enter in suspend mode and remote wakeup */
		b_btn_state = !port_pin_get_input_level(BUTTON_0_PIN);
		if (b_btn_state != btn_suspend) {
			/* Button have changed */
			btn_suspend = b_btn_state;
			if (b_btn_state) {
				/* Button has been pressed */
				LED_Off(LED_0_PIN);
				ui_enable_asynchronous_interrupt();
				uhc_suspend(true);
				return;
			}
		}

		/* Power on a LED when the mouse button down */
		if (ui_nb_down) {
			LED_On(LED_0_PIN);
		}
	}
}
Example #13
0
int main (void)
{
	system_init();

	// Insert application code here, after the board has been initialized.

	// This skeleton code simply sets the LED to the state of the button.
	while (1) {
		// Is button pressed?
		if (port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_ACTIVE) {
			// Yes, so turn LED on.
			port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
		} else {
			// No, so turn LED off.
			port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE);
		}
	}
}
Example #14
0
int main(void)
{
	system_init();

	//! [setup_init]
	configure_port_pins();
	//! [setup_init]

	//! [main]
	while (true) {
		//! [main_1]
		bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
		//! [main_1]

		//! [main_2]
		port_pin_set_output_level(LED_0_PIN, !pin_state);
		//! [main_2]
	}
	//! [main]
}
int main(void)
{
//! [main_start]
	/* Initialize system */
//! [system_init]
	system_init();
//! [system_init]

//! [run_config]
	configure_spi_master();
//! [run_config]
//! [run_callback_config]
	configure_spi_master_callbacks();
//! [run_callback_config]
//! [main_start]

//! [main_use_case]
//! [inf_loop]
	while (true) {
		/* Infinite loop */
		if (!port_pin_get_input_level(BUTTON_0_PIN)) {
			//! [select_slave]
			spi_select_slave(&spi_master_instance, &slave, true);
			//! [select_slave]
			//! [write and read]
			spi_transceive_buffer_job(&spi_master_instance, wr_buffer,rd_buffer,BUF_LENGTH);
			//! [write and read]
			//! [wait]
			while (!transrev_complete_spi_master) {
				/////* Wait for write and read complete */
			}
			//! [wait]
			//! [deselect_slave]
			spi_select_slave(&spi_master_instance, &slave, false);
			//! [deselect_slave]
		}
	}
//! [inf_loop]
//! [main_use_case]
}
Example #16
0
void ui_process(uint16_t framenumber)
{
	static uint8_t cpt_sof = 0;

	if ((framenumber % 1000) == 0) {
		LED_On(LED_0_PIN);
	}
	if ((framenumber % 1000) == 500) {
		LED_Off(LED_0_PIN);
	}
	/* Scan process running each 5ms */
	cpt_sof++;
	if (cpt_sof < 5) {
		return;
	}
	cpt_sof = 0;

	/* Uses buttons to move mouse */
	if (!port_pin_get_input_level(BUTTON_0_PIN)) {
		udi_hid_mouse_moveY(-MOUSE_MOVE_RANGE);
	}
}
/*============================================================================
*  Name    :   BitBangSPI_Send_Byte
*  ------------------------------------------------------------------------------
*  Purpose :   Send and Read one byte using BitBangSPI Interface
*  Input   :   Data to send to slave
*  Output  :   Data read from slave
*  Notes   :	Called from BitBangSPI_Send_Message in this file
*  ============================================================================*/
uint8_t BitBangSPI_Send_Byte(uint8_t c)
{
	uint32_t bit;
	bool i;
	for (bit = 0u; bit < 8u; bit++) {
		/* write MOSI on trailing edge of previous clock */
		if (c & 0x80u) {
			port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_MOSI,
					MOSI_BB), 1u);
		} else {
			port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_MOSI,
					MOSI_BB), 0u);
		}

		c <<= 1u;

		/* half a clock cycle before leading/rising edge */
		cpu_delay_us(1u); /* SHOULD BE ~3uS. */
		port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_SCK, SCK_BB), 1u);

		/* half a clock cycle before trailing/falling edge */
		cpu_delay_us(1u); /* SHOULD BE ~3uS. */

		/* read MISO on trailing edge */
		i = (port_pin_get_input_level(CONCAT(PIN_P, SPI_BB_MISO, MISO_BB)));
		if (i == true) {
			c |= (0x01u);
		} else {
		}

		port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_SCK, SCK_BB), 0u);
	}

	port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_MOSI, MOSI_BB), 0u);

	cpu_delay_us(50u); /* SHOULD BE ~18uS. */

	return c;
}
Example #18
0
File: ui.c Project: scarecrowli/asf
void ui_host_sof_event(void)
{
	bool b_btn_state;
	static bool btn_suspend_and_remotewakeup = false;
	static uint16_t counter_sof = 0;

	if (ui_enum_status == UHC_ENUM_SUCCESS) {
		/* Display device enumerated and in active mode */
		if (++counter_sof > ui_device_speed_blink) {
			counter_sof = 0;
			LED_Toggle();
		}

		/* Scan button to enter in suspend mode and remote wakeup */
		b_btn_state = !port_pin_get_input_level(BUTTON_0_PIN);
		if (b_btn_state != btn_suspend_and_remotewakeup) {
			/* Button have changed */
			btn_suspend_and_remotewakeup = b_btn_state;
			if (b_btn_state) {
				/* Button has been pressed */
				ui_enable_asynchronous_interrupt();
				LED_Off();
				uhc_suspend(true);
				return;
			}
		}

		/* Power on a LED when the mouse button down */
		if (ui_nb_down) {
			LED_On();
		}
		/* Power on a LED when the mouse moves */
		if (ui_move) {
			ui_move = false;
			LED_On();
		}
	}
}
Example #19
0
int main(void)
{
	system_init();

	//! [setup_init]
	configure_wdt();
	//! [setup_init]

	//! [main]
	//! [main_1]
	enum system_reset_cause reset_cause = system_get_reset_cause();
	//! [main_1]

	//! [main_2]
	if (reset_cause == SYSTEM_RESET_CAUSE_WDT) {
		port_pin_set_output_level(LED_0_PIN, LED_0_INACTIVE);
	}
	else {
		port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);
	}
	//! [main_2]

	//! [main_3]
	while (true) {
	//! [main_3]
		//! [main_4]
		if (port_pin_get_input_level(BUTTON_0_PIN) == false) {
		//! [main_4]
		//! [main_5]
			port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);

			wdt_reset_count();
		//! [main_5]
		}
	}
	//! [main]
}
Example #20
0
/**
 * \brief Gets button push
 */
static enum button get_button(void)
{
	if (port_pin_get_input_level(WING_BUTTON_1) == false) {
		/* Wait for button to be released */
		while(!port_pin_get_input_level(WING_BUTTON_1)) {
		}
		return BUTTON_1;
	} else if (port_pin_get_input_level(WING_BUTTON_2) == false) {
		/* Wait for button to be released */
		while(!port_pin_get_input_level(WING_BUTTON_2)) {
		}
		return BUTTON_2;
	} else if (port_pin_get_input_level(WING_BUTTON_3) == false) {
		/* Wait for button to be released */
		while(!port_pin_get_input_level(WING_BUTTON_3)) {
		}
		return BUTTON_3;
	} else {
		/* No button pushed */
		return BUTTON_NONE;
	}
}
Example #21
0
void ui_usb_enum_event(uhc_device_t *dev, uhc_enum_status_t status)
{
	ui_enum_status = status;
	if (ui_enum_status == UHC_ENUM_SUCCESS) {
		switch (dev->speed) {
		case UHD_SPEED_HIGH:
			ui_device_speed_blink = 250;
			break;

		case UHD_SPEED_FULL:
			ui_device_speed_blink = 500;
			break;

		case UHD_SPEED_LOW:
		default:
			ui_device_speed_blink = 1000;
			break;
		}
		/* USB Device CDC connected
		   Open and configure UART and USB CDC ports */
		usb_cdc_line_coding_t cfg = {
			.dwDTERate   = CPU_TO_LE32(19200),
			.bCharFormat = CDC_STOP_BITS_1,
			.bParityType = CDC_PAR_NONE,
			.bDataBits   = 8,
		};
		uart_open();
		uart_config(&cfg);
		uhi_cdc_open(0, &cfg);
	}
}

void ui_usb_wakeup_event(void)
{
#ifdef USB_HOST_LPM_SUPPORT
	ui_disable_asynchronous_interrupt();
#endif
}

void ui_usb_sof_event(void)
{
#ifdef USB_HOST_LPM_SUPPORT
	bool b_btn_state;
	static bool btn_suspend_and_remotewakeup = false;
#endif

	static uint16_t counter_sof = 0;

	if (ui_enum_status == UHC_ENUM_SUCCESS) {
		/* Display device enumerated and in active mode */
		if (++counter_sof > ui_device_speed_blink) {
			counter_sof = 0;
			if (!ui_data_transfer) {
				LED_Toggle(LED_0_PIN);
			}
		}
#ifdef USB_HOST_LPM_SUPPORT
		/* Scan button to enter in suspend mode and remote wakeup */
		b_btn_state = !port_pin_get_input_level(BUTTON_0_PIN);
		if (b_btn_state != btn_suspend_and_remotewakeup) {
			/* Button have changed */
			btn_suspend_and_remotewakeup = b_btn_state;
			if (b_btn_state) {
				/* Button has been pressed */
				ui_enable_asynchronous_interrupt();
				LED_Off(LED_0_PIN);
                uhc_suspend_lpm(true, HIRD_800_US);
				return;
			}
		}
#endif // #ifdef USB_HOST_LPM_SUPPORT
	}
}
Example #22
0
bool cph_deca_isr_is_detected(void) {
	return port_pin_get_input_level(DW_IRQ_PIN);
	//return extint_chan_is_detected(DW_IRQ_LINE);
}
Example #23
0
File: ui.c Project: scarecrowli/asf
void ui_device_sof_action(void)
{
	uint16_t framenumber;
	static uint8_t cpt_sof = 0;
	static int32_t move_count = MOUSE_MOVE_COUNT;
	static uint8_t move_dir = MOVE_UP;

	if (!ui_device_b_mouse_enable) {
		return;
	}

	framenumber = udd_get_frame_number();
	if ((framenumber % 1000) == 0) {
		LED_On();
	}

	if ((framenumber % 1000) == 500) {
		LED_Off();
	}

	/* Scan process running each 2ms */
	cpt_sof++;
	if (2 > cpt_sof) {
		return;
	}

	cpt_sof = 0;

	/* Uses buttons to move mouse */
	if (!port_pin_get_input_level(BUTTON_0_PIN)) {
		move_count--;
		switch (move_dir) {
		case MOVE_UP:
			udi_hid_mouse_moveY(-MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_RIGHT;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;

		case MOVE_RIGHT:
			udi_hid_mouse_moveX(+MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_DOWN;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;

		case MOVE_DOWN:
			udi_hid_mouse_moveY(+MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_LEFT;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;

		case MOVE_LEFT:
			udi_hid_mouse_moveX(-MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_UP;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;
		}
	}
}
Example #24
0
/** Initializes the XOSC32K crystal failure detector, and starts it.
 *
 *  \param[in]  ok_callback    Callback function to run upon XOSC32K operational
 *  \param[in]  fail_callback  Callback function to run upon XOSC32K failure
 */
static void init_xosc32k_fail_detector(
		const tc_callback_t ok_callback,
		const tc_callback_t fail_callback)
{
	/* TC pairs share the same clock, ensure reference and crystal timers use
	 * different clocks */
	Assert(Abs(_tc_get_inst_index(CONF_TC_OSC32K) -
			_tc_get_inst_index(CONF_TC_XOSC32K)) >= 2);

	/* The crystal detection cycle count must be less than the reference cycle
	 * count, so that the reference timer is periodically reset before expiry */
	Assert(CRYSTAL_RESET_CYCLES < CRYSTAL_FAIL_CYCLES);

	/* Must use different clock generators for the crystal and reference, must
	 * not be CPU generator 0 */
	Assert(GCLK_GENERATOR_XOSC32K != GCLK_GENERATOR_OSC32K);
	Assert(GCLK_GENERATOR_XOSC32K != GCLK_GENERATOR_0);
	Assert(GCLK_GENERATOR_OSC32K  != GCLK_GENERATOR_0);

	/* Configure and enable the XOSC32K GCLK generator */
	struct system_gclk_gen_config xosc32k_gen_conf;
	system_gclk_gen_get_config_defaults(&xosc32k_gen_conf);
	xosc32k_gen_conf.source_clock = SYSTEM_CLOCK_SOURCE_XOSC32K;
	system_gclk_gen_set_config(GCLK_GENERATOR_XOSC32K, &xosc32k_gen_conf);
	system_gclk_gen_enable(GCLK_GENERATOR_XOSC32K);

	/* Configure and enable the reference clock GCLK generator */
	struct system_gclk_gen_config ref_gen_conf;
	system_gclk_gen_get_config_defaults(&ref_gen_conf);
	ref_gen_conf.source_clock = SYSTEM_CLOCK_SOURCE_OSC32K;
	system_gclk_gen_set_config(GCLK_GENERATOR_OSC32K, &ref_gen_conf);
	system_gclk_gen_enable(GCLK_GENERATOR_OSC32K);

	/* Set up crystal counter - when target count elapses, trigger event */
	struct tc_config tc_xosc32k_conf;
	tc_get_config_defaults(&tc_xosc32k_conf);
	tc_xosc32k_conf.clock_source = GCLK_GENERATOR_XOSC32K;
	tc_xosc32k_conf.counter_16_bit.compare_capture_channel[0] =
			CRYSTAL_RESET_CYCLES;
	tc_xosc32k_conf.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ;
	tc_init(&tc_xosc32k, CONF_TC_XOSC32K, &tc_xosc32k_conf);

	/* Set up reference counter - when event received, restart */
	struct tc_config tc_osc32k_conf;
	tc_get_config_defaults(&tc_osc32k_conf);
	tc_osc32k_conf.clock_source = GCLK_GENERATOR_OSC32K;
	tc_osc32k_conf.counter_16_bit.compare_capture_channel[0] =
			CRYSTAL_FAIL_CYCLES;
	tc_osc32k_conf.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ;
	tc_init(&tc_osc32k, CONF_TC_OSC32K, &tc_osc32k_conf);

	/* Configure event channel and link it to the xosc32k counter */
	struct events_config config;
	struct events_resource event;
	events_get_config_defaults(&config);
	config.edge_detect  = EVENTS_EDGE_DETECT_NONE;
	config.generator    = CONF_EVENT_GENERATOR_ID;
	config.path         = EVENTS_PATH_ASYNCHRONOUS;
	events_allocate(&event, &config);
	/* Configure event user and link it to the osc32k counter */
	events_attach_user(&event, CONF_EVENT_USED_ID);

	/* Enable event generation for crystal counter */
	struct tc_events tc_xosc32k_events = { .generate_event_on_overflow = true };
	tc_enable_events(&tc_xosc32k, &tc_xosc32k_events);

	/* Enable event reception for reference counter */
	struct tc_events tc_osc32k_events = { .on_event_perform_action = true };
	tc_osc32k_events.event_action = TC_EVENT_ACTION_RETRIGGER;
	tc_enable_events(&tc_osc32k, &tc_osc32k_events);

	/* Enable overflow callback for the crystal counter - if crystal count
	 * has been reached, crystal is operational */
	tc_register_callback(&tc_xosc32k, ok_callback, TC_CALLBACK_CC_CHANNEL0);
	tc_enable_callback(&tc_xosc32k, TC_CALLBACK_CC_CHANNEL0);

	/* Enable compare callback for the reference counter - if reference count
	 * has been reached, crystal has failed */
	tc_register_callback(&tc_osc32k, fail_callback, TC_CALLBACK_CC_CHANNEL0);
	tc_enable_callback(&tc_osc32k, TC_CALLBACK_CC_CHANNEL0);

	/* Start both crystal and reference counters */
	tc_enable(&tc_xosc32k);
	tc_enable(&tc_osc32k);
}

/** Main application entry point. */
int main(void)
{
	system_init();

	system_flash_set_waitstates(2);

	init_osc32k();
	init_xosc32k();
	init_xosc32k_fail_detector(
			xosc32k_ok_callback, xosc32k_fail_callback);

#if ENABLE_CPU_CLOCK_OUT == true
	/* Configure a GPIO pin as the CPU clock output */
	struct system_pinmux_config clk_out_pin;
	system_pinmux_get_config_defaults(&clk_out_pin);
	clk_out_pin.direction    = SYSTEM_PINMUX_PIN_DIR_OUTPUT;
	clk_out_pin.mux_position = CONF_CLOCK_PIN_MUX;
	system_pinmux_pin_set_config(CONF_CLOCK_PIN_OUT, &clk_out_pin);
#endif

	for (;;) {
		static bool old_run_osc = true;
		bool new_run_osc =
				(port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_INACTIVE);

		/* Check if the XOSC32K needs to be started or stopped when the board
		 * button is pressed or released */
		if (new_run_osc != old_run_osc) {
			if (new_run_osc) {
				system_clock_source_enable(SYSTEM_CLOCK_SOURCE_XOSC32K);
				while(!system_clock_source_is_ready(
						SYSTEM_CLOCK_SOURCE_XOSC32K));
			}
			else {
				system_clock_source_disable(SYSTEM_CLOCK_SOURCE_XOSC32K);
			}

			old_run_osc = new_run_osc;
		}
	}
}
Example #25
0
File: ui.c Project: InSoonPark/asf
void ui_process(uint16_t framenumber)
{
	static uint8_t cpt_sof = 0;
	static bool btn_last_state = false;
	static bool sequence_running = false;
	static uint8_t sequence_pos = 0;

	bool b_btn_state, success;
	uint8_t value;

	if ((framenumber % 1000) == 0) {
		LED_On(LED_0_PIN);
	}
	if ((framenumber % 1000) == 500) {
		LED_Off(LED_0_PIN);
	}
	/* Scan process running each 5ms */
	cpt_sof++;
	
	if(button_function) {
		if (cpt_sof < 5) {
			return;
		}
		cpt_sof = 0;
		/* Uses buttons to move mouse */
		if (!port_pin_get_input_level(BUTTON_0_PIN)) {
			udi_hid_mouse_moveY(-MOUSE_MOVE_RANGE);
		}
	} else {
		if ((cpt_sof % 2) == 0) {
			return;
		}
		// Scan buttons on switch 0 to send keys sequence
		b_btn_state = (!port_pin_get_input_level(BUTTON_0_PIN));
		if (b_btn_state != btn_last_state) {
			btn_last_state = b_btn_state;
			if (btn_wakeup) {
				if (!b_btn_state) {
					btn_wakeup = false;
				}
			} else {
				sequence_running = true;
			}
		}
		// Sequence process running each period
		if (SEQUENCE_PERIOD > cpt_sof) {
			return;
		}
		cpt_sof = 0;

		if (sequence_running) {
			// Send next key
			value = ui_sequence[sequence_pos].value;
			if (value!=0) {
				if (ui_sequence[sequence_pos].b_modifier) {
					if (ui_sequence[sequence_pos].b_down) {
						success = udi_hid_kbd_modifier_down(value);
					} else {
						success = udi_hid_kbd_modifier_up(value);
					}
				} else {
					if (ui_sequence[sequence_pos].b_down) {
						success = udi_hid_kbd_down(value);
					} else {
						success = udi_hid_kbd_up(value);
					}
				}	
				if (!success) {
					return; // Retry it on next schedule
				}
			}
			// Valid sequence position
			sequence_pos++;
			if (sequence_pos >=
				sizeof(ui_sequence) / sizeof(ui_sequence[0])) {
				sequence_pos = 0;
				sequence_running = false;
				button_function  = 1;
			}
		}
	}
}
//! [setup_7]
void extint_detection_callback(void)
{
	bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
	port_pin_set_output_level(LED_0_PIN, pin_state);
}
Example #27
0
/**
 * \brief Configures the DAC in event triggered mode.
 *
 * Configures the DAC to use the module's default configuration, with output
 * channel mode configured for event triggered conversions.
 *
 * \param dev_inst  Pointer to the DAC module software instance to initialize
 */
static void configure_dac(struct dac_module *dac_module)
{
    struct dac_config config;
    struct dac_chan_config channel_config;

    /* Get the DAC default configuration */
    dac_get_config_defaults(&config);

    /* Switch to GCLK generator 0 */
    config.clock_source = GCLK_GENERATOR_0;

    dac_init(dac_module, DAC, &config);

    /* Get the default DAC channel config */
    dac_chan_get_config_defaults(&channel_config);

    /* Set the channel configuration, and enable it */
    dac_chan_set_config(dac_module, DAC_CHANNEL_0, &channel_config);
    dac_chan_enable(dac_module, DAC_CHANNEL_0);

    /* Enable event triggered conversions */
    struct dac_events events = { .on_event_start_conversion = true };
    dac_enable_events(dac_module, &events);

    dac_enable(dac_module);
}

/**
 * \brief Configures the TC to generate output events at the sample frequency.
 *
 * Configures the TC in Frequency Generation mode, with an event output once
 * each time the audio sample frequency period expires.
 *
 * \param dev_inst  Pointer to the TC module software instance to initialize
 */
static void configure_tc(struct tc_module *tc_module)
{
    struct tc_config config;
    tc_get_config_defaults(&config);

    config.clock_source    = GCLK_GENERATOR_0;
    config.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ;

    tc_init(tc_module, TC3, &config);

    /* Enable periodic event output generation */
    struct tc_events events = { .generate_event_on_overflow = true };
    tc_enable_events(tc_module, &events);

    /* Set the timer top value to alter the overflow frequency */
    tc_set_top_value(tc_module,
                     system_gclk_gen_get_hz(GCLK_GENERATOR_0) / sample_rate);


    tc_enable(tc_module);
}

/**
 * \brief Configures the event system to link the sample timer to the DAC.
 *
 * Configures the event system, linking the TC module used for the audio sample
 * rate timing to the DAC, so that a new conversion is triggered each time the
 * DAC receives an event from the timer.
 */
static void configure_events(struct events_resource *event)
{
    struct events_config config;

    events_get_config_defaults(&config);

    config.generator    = EVSYS_ID_GEN_TC3_OVF;
    config.path         = EVENTS_PATH_ASYNCHRONOUS;

    events_allocate(event, &config);
    events_attach_user(event, EVSYS_ID_USER_DAC_START);
}

/**
 * \brief Main application routine
 */
int main(void)
{
    struct dac_module dac_module;
    struct tc_module tc_module;
    struct events_resource event;

    /* Initialize all the system clocks, pm, gclk... */
    system_init();

    /* Enable the internal bandgap to use as reference to the DAC */
    system_voltage_reference_enable(SYSTEM_VOLTAGE_REFERENCE_BANDGAP);

    /* Module configuration */
    configure_tc(&tc_module);
    configure_dac(&dac_module);
    configure_events(&event);

    /* Start the sample trigger timer */
    tc_start_counter(&tc_module);

    while (true) {
        while (port_pin_get_input_level(SW0_PIN) == SW0_INACTIVE) {
            /* Wait for the button to be pressed */
        }

        port_pin_toggle_output_level(LED0_PIN);

        for (uint32_t i = 0; i < number_of_samples; i++) {
            dac_chan_write(&dac_module, DAC_CHANNEL_0, wav_samples[i]);

            while (!(DAC->INTFLAG.reg & DAC_INTFLAG_EMPTY)) {
                /* Wait for data buffer to be empty */
            }

        }

        while (port_pin_get_input_level(SW0_PIN) == SW0_ACTIVE) {
            /* Wait for the button to be depressed */
        }
    }
}
Example #28
0
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
  int level = port_pin_get_input_level(BUTTON_0_PIN);
  return !level; /* Pressed returns 1, released returns 0 */
}
Example #29
0
/*---------------------------------------------------------------------------*/
static int
button_sensor_value(int type)
{
  bool PinLvl = port_pin_get_input_level(BUTTON_0_PIN);
  return PinLvl;
}
Example #30
0
/** Updates the board LED to the current button state. */
static void update_led_state(void)
{
	bool pin_state = port_pin_get_input_level(BUTTON_1_PIN);
	port_pin_set_output_level(LED_1_PIN, pin_state);
}