Ejemplo n.º 1
0
/**
 * \brief Clears a module status flag.
 *
 * Clears the given status flag of the module.
 *
 * \param[in] module_inst   Pointer to the TCC software instance struct
 * \param[in] status_flags  Bitmask of \c TCC_STATUS_* flags to clear
 */
void tcc_clear_status(
		struct tcc_module *const module_inst,
		const uint32_t status_flags)
{
	/* Sanity check arguments */
	Assert(module_inst);
	Assert(module_inst->hw);

	uint32_t int_clr = 0;
	uint32_t status_clr = 0;
	int i;

	/* Channels */
	for (i = 0; i < TCC_NUM_CHANNELS; i++) {
		if (status_flags & TCC_STATUS_CHANNEL_MATCH_CAPTURE(i)) {
			int_clr |= TCC_INTFLAG_MC(i);
		}
	}
	/* Faults */
	if (status_flags & TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(1)) {
		int_clr |= TCC_INTFLAG_FAULT1;
		status_clr |= TCC_STATUS_FAULT1;
	}
	if (status_flags & TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(0)) {
		int_clr |= TCC_INTFLAG_FAULT0;
		status_clr |= TCC_STATUS_FAULT0;
	}
	if (status_flags & TCC_STATUS_RECOVERABLE_FAULT_OCCUR(1)) {
		int_clr |= TCC_INTFLAG_FAULTB;
		status_clr |= TCC_STATUS_FAULTB;
	}
	if (status_flags & TCC_STATUS_RECOVERABLE_FAULT_OCCUR(0)) {
		int_clr |= TCC_INTFLAG_FAULTA;
		status_clr |= TCC_STATUS_FAULTA;
	}
	/* Check for TCC capture overflow */
	if (status_flags & TCC_STATUS_CAPTURE_OVERFLOW) {
		int_clr |= TCC_INTFLAG_ERR;
	}
	/* Check for TCC count counter */
	if (status_flags & TCC_STATUS_COUNTER_EVENT) {
		int_clr |= TCC_INTFLAG_CNT;
	}
	/* Check for TCC count retrigger */
	if (status_flags & TCC_STATUS_COUNTER_RETRIGGERED) {
		int_clr = TCC_INTFLAG_TRG;
	}
	/* Check for TCC count overflow */
	if (status_flags & TCC_STATUS_COUNT_OVERFLOW) {
		int_clr |= TCC_INTFLAG_OVF;
	}
	/* Clear status flag */
	module_inst->hw->STATUS.reg = status_clr;
	/* Clear interrupt flag */
	module_inst->hw->INTFLAG.reg = int_clr;
}
Ejemplo n.º 2
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));
	}
}
Ejemplo n.º 3
0
/**
 * \brief Retrieves the current module status.
 *
 * Retrieves the status of the module, giving overall state information.
 *
 * \param[in] module_inst  Pointer to the TCC software instance struct
 *
 * \return Bitmask of \c TCC_STATUS_* flags.
 *
 * \retval TCC_STATUS_CHANNEL_MATCH_CAPTURE(n)         Channel n match/capture has occured
 * \retval TCC_STATUS_CHANNEL_OUTPUT(n)                Channel n match/capture output state
 * \retval TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(x)   Non-recoverable fault x has occured
 * \retval TCC_STATUS_RECOVERABLE_FAULT_OCCUR(n)       Recoverable fault n has occured
 * \retval TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(x) Non-recoverable fault x input present
 * \retval TCC_STATUS_RECOVERABLE_FAULT_PRESENT(n)     Recoverable fault n input present
 * \retval TCC_STATUS_SYNC_READY          None of register is syncing
 * \retval TCC_STATUS_CAPTURE_OVERFLOW    Timer capture data has overflowed
 * \retval TCC_STATUS_COUNTER_EVENT       Timer counter event has occurred
 * \retval TCC_STATUS_COUNT_OVERFLOW      Timer count value has overflowed
 * \retval TCC_STATUS_COUNTER_RETRIGGERED Timer counter has been retriggered
 * \retval TCC_STATUS_STOP                Timer counter has been stopped
 * \retval TCC_STATUS_RAMP_CYCLE_INDEX    Wave ramp index for cycle
 */
uint32_t tcc_get_status(
		struct tcc_module *const module_inst)
{
	/* Sanity check arguments */
	Assert(module_inst);
	Assert(module_inst->hw);

	uint32_t int_flags = module_inst->hw->INTFLAG.reg;
	uint32_t status_flags = module_inst->hw->STATUS.reg;
	uint32_t status = 0;
	int i;

	/* SYNC */
	if (module_inst->hw->SYNCBUSY.reg == 0) {
		status |= TCC_STATUS_SYNC_READY;
	}

	/* Channels */
	for (i = 0; i < TCC_NUM_CHANNELS; i++) {
		if (int_flags & TCC_INTFLAG_MC(i)) {
			status |= TCC_STATUS_CHANNEL_MATCH_CAPTURE(i);
		}
		if (status_flags & TCC_STATUS_CMP(i)) {
			status |= TCC_STATUS_CHANNEL_OUTPUT(i);
		}
	}
	/* Non-recoverable fault state */
	if ((int_flags & TCC_INTFLAG_FAULT1) ||
		(status_flags & TCC_STATUS_FAULT1)) {
		status |= TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(1);
	}
	if ((int_flags & TCC_INTFLAG_FAULT0) ||
		(status_flags & TCC_STATUS_FAULT0)) {
		status |= TCC_STATUS_NON_RECOVERABLE_FAULT_OCCUR(0);
	}
	/* Non-recoverable fault inputs */
	if (status_flags & TCC_STATUS_FAULT0IN) {
		status |= TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(0);
	}
	if (status_flags & TCC_STATUS_FAULT1IN) {
		status |= TCC_STATUS_NON_RECOVERABLE_FAULT_PRESENT(1);
	}
	/* Recoverable fault state */
	if ((int_flags & TCC_INTFLAG_FAULTB) ||
		(status_flags & TCC_STATUS_FAULTB)) {
		status |= TCC_STATUS_RECOVERABLE_FAULT_OCCUR(1);
	}
	if ((int_flags & TCC_INTFLAG_FAULTA) ||
		(status_flags & TCC_STATUS_FAULTA)) {
		status |= TCC_STATUS_RECOVERABLE_FAULT_OCCUR(0);
	}
	/* Recoverable fault inputs */
	if (status_flags & TCC_STATUS_FAULTAIN) {
		status |= TCC_STATUS_RECOVERABLE_FAULT_PRESENT(0);
	}
	if (status_flags & TCC_STATUS_FAULTBIN) {
		status |= TCC_STATUS_RECOVERABLE_FAULT_PRESENT(1);
	}

	/* Check for TCC capture overflow */
	if (int_flags & TCC_INTFLAG_ERR) {
		status |= TCC_STATUS_CAPTURE_OVERFLOW;
	}
	/* Check for TCC count counter */
	if (int_flags & TCC_INTFLAG_CNT) {
		status |= TCC_STATUS_COUNTER_EVENT;
	}
	/* Check for TCC count retrigger */
	if (int_flags & TCC_INTFLAG_TRG) {
		status |= TCC_STATUS_COUNTER_RETRIGGERED;
	}
	/* Check for TCC count overflow */
	if (int_flags & TCC_INTFLAG_OVF) {
		status |= TCC_STATUS_COUNT_OVERFLOW;
	}
	/* Check for TCC count stop */
	if (status_flags & TCC_STATUS_STOP) {
		status |= TCC_STATUS_STOPPED;
	}
	return status;
}
Ejemplo n.º 4
0
/**
 * \internal
 * \brief Test capture and compare
 *
 * This test uses TCC0 as a PWM generator (compare function). TCC1 will be set
 * to capture the signal from TCC0 to test the capture functionality.
 *
 * \param test Current test case.
 */
static void run_capture_and_compare_test(const struct test_case *test)
{
	test_assert_true(test,
			tcc_init_success == true,
			"TCC initialization failed, skipping test");

	test_assert_true(test,
			callback_function_entered == 1,
			"The callback test has failed, skipping test");

	/* Configure TCC module for PWM generation */
	tcc_reset(&tcc_test0_module);
	tcc_get_config_defaults(&tcc_test0_config, CONF_TEST_TCC0);
	tcc_test0_config.counter.period = 0x03FF;
	tcc_test0_config.compare.wave_generation  =
			TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
	tcc_test0_config.compare.match[TCC_MATCH_CAPTURE_CHANNEL_0]  = 0x01FF;

	/* Calculate the theoretical PWM frequency & duty */
	uint32_t frequency_output, duty_output;
	frequency_output = system_clock_source_get_hz(SYSTEM_CLOCK_SOURCE_OSC8M) / (0x03FF+1);

	/* This value is depend on the WaveGeneration Mode */
	duty_output = (uint32_t)(tcc_test0_config.compare.match[TCC_MATCH_CAPTURE_CHANNEL_0] * 100) /
			tcc_test0_config.counter.period;

	tcc_test0_config.pins.enable_wave_out_pin[TCC_MATCH_CAPTURE_CHANNEL_0] = true;
	tcc_test0_config.pins.wave_out_pin[TCC_MATCH_CAPTURE_CHANNEL_0]        = CONF_TEST_PIN_OUT;
	tcc_test0_config.pins.wave_out_pin_mux[TCC_MATCH_CAPTURE_CHANNEL_0]    = CONF_TEST_PIN_MUX;
	tcc_init(&tcc_test0_module, CONF_TEST_TCC0, &tcc_test0_config);

	tcc_register_callback(&tcc_test0_module, tcc_callback_function, TCC_CALLBACK_CHANNEL_0);
	tcc_enable_callback(&tcc_test0_module, TCC_CALLBACK_CHANNEL_0);

	/* Configure TCC module for capture */
	tcc_reset(&tcc_test1_module);
	tcc_get_config_defaults(&tcc_test1_config, CONF_TEST_TCC1);
	tcc_test1_config.counter.period          = 0xFFFF;
	tcc_test1_config.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1;
	tcc_test1_config.capture.channel_function[CONF_CAPTURE_CHAN_0] = TCC_CHANNEL_FUNCTION_CAPTURE;
	tcc_test1_config.capture.channel_function[CONF_CAPTURE_CHAN_1] = TCC_CHANNEL_FUNCTION_CAPTURE;

	tcc_init(&tcc_test1_module, CONF_TEST_TCC1, &tcc_test1_config);

	struct tcc_events tcc_events = {
		.on_input_event_perform_action[1] = true,
		.input_config[1].modify_action = true,
		.input_config[1].action = TCC_EVENT_ACTION_PERIOD_PULSE_WIDTH_CAPTURE
	};

	tcc_enable_events(&tcc_test1_module, &tcc_events);

	/* Configure external interrupt controller */
	struct extint_chan_conf extint_chan_config;
	extint_chan_config.gpio_pin            = CONF_EIC_PIN;
	extint_chan_config.gpio_pin_mux        = CONF_EIC_MUX;
	extint_chan_config.gpio_pin_pull       = EXTINT_PULL_UP;
	extint_chan_config.wake_if_sleeping    = false;
	extint_chan_config.filter_input_signal = false;
	extint_chan_config.detection_criteria  = EXTINT_DETECT_HIGH;
	extint_chan_set_config(CONF_EIC_CHAN, &extint_chan_config);

	/* Configure external interrupt module to be event generator */
	struct extint_events extint_event_conf;
	extint_event_conf.generate_event_on_detect[CONF_EIC_CHAN] = true;
	extint_enable_events(&extint_event_conf);

	/* Configure event system */
	struct events_resource event_res;

	/* Configure channel */
	struct events_config config;
	events_get_config_defaults(&config);
	config.generator      = CONF_EVENT_GENERATOR_ID;
	config.edge_detect    = EVENTS_EDGE_DETECT_NONE;
	config.path           = EVENTS_PATH_ASYNCHRONOUS;
	events_allocate(&event_res, &config);

	/* Configure user */
	events_attach_user(&event_res, CONF_EVENT_USED_ID);

	/* Enable TCC modules */
	tcc_enable(&tcc_test1_module);
	tcc_enable(&tcc_test0_module);

	uint16_t period_after_capture = 0;
	uint16_t pulse_width_after_capture = 0;
	uint32_t capture_frequency = 0;
	uint32_t capture_duty = 0;

	while (callback_function_entered < 4) {
		period_after_capture = tcc_get_capture_value(&tcc_test1_module,
				TCC_MATCH_CAPTURE_CHANNEL_0);
		pulse_width_after_capture = tcc_get_capture_value(&tcc_test1_module,
				TCC_MATCH_CAPTURE_CHANNEL_1);
	}

	if(period_after_capture != 0) {
		capture_frequency =
				system_clock_source_get_hz(SYSTEM_CLOCK_SOURCE_OSC8M) /
				period_after_capture;
		capture_duty = (uint32_t)(pulse_width_after_capture) * 100 /
				period_after_capture;
	}

	test_assert_true(test,
			(capture_frequency <= (frequency_output * (100 + CONF_TEST_TOLERANCE) / 100)) && \
			(capture_frequency >= (frequency_output * (100 - CONF_TEST_TOLERANCE) / 100)) && \
			(capture_duty <= (duty_output * (100 + CONF_TEST_TOLERANCE) / 100)) && \
			(capture_duty >= (duty_output * (100 - CONF_TEST_TOLERANCE) / 100)) \
			,"The result of Capture is wrong, captured frequency: %ldHz, captured duty: %ld%%",
			capture_frequency,
			capture_duty
			);
}

/**
 * \internal
 * \brief Test Recoverable Fault (FAULTn)
 *
 * This test uses TCC0 as a PWM generator (compare function).
 * EXTINT will be used to route fault input to TCC0.
 *
 * \param test Current test case.
 */
static void run_faultn_test(const struct test_case *test)
{
	test_assert_true(test,
			tcc_init_success == true,
			"TCC initialization failed, skipping test");

	/* Configure TCC module for PWM generation with fault */
	tcc_reset(&tcc_test0_module);
	tcc_get_config_defaults(&tcc_test0_config, CONF_TEST_TCC0);
	tcc_test0_config.counter.period = 0x03FF;
	tcc_test0_config.compare.wave_generation  =
			TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
	tcc_test0_config.compare.match[TCC_MATCH_CAPTURE_CHANNEL_0]  = 0x01FF;
	tcc_test0_config.wave_ext.recoverable_fault[TCC_MATCH_CAPTURE_CHANNEL_0].source = TCC_FAULT_SOURCE_ENABLE;
	tcc_test0_config.wave_ext.recoverable_fault[TCC_MATCH_CAPTURE_CHANNEL_0].halt_action = TCC_FAULT_HALT_ACTION_SW_HALT;
	tcc_init(&tcc_test0_module, CONF_TEST_TCC0, &tcc_test0_config);
	/* Configure TCC events for recoverable fault input */
	struct tcc_events tcc_test_events;
	memset(&tcc_test_events, 0, sizeof(struct tcc_events));
	tcc_test_events.on_event_perform_channel_action[TCC_MATCH_CAPTURE_CHANNEL_0] = true;
	tcc_enable_events(&tcc_test0_module, &tcc_test_events);
	tcc_enable(&tcc_test0_module);

	/* Configure IO pin to generate fault */
	struct port_config config_pin;
	port_get_config_defaults(&config_pin);
	config_pin.direction = PORT_PIN_DIR_OUTPUT;
	port_pin_set_config(CONF_TEST_PIN_OUT, &config_pin);
	port_pin_set_output_level(CONF_TEST_PIN_OUT, true);

	/* Configure EIC to capture fault input */
	struct extint_chan_conf config;
	extint_chan_get_config_defaults(&config);
	config.filter_input_signal = true;
	config.detection_criteria  = EXTINT_DETECT_BOTH;
	config.gpio_pin     = CONF_EIC_PIN;
	config.gpio_pin_mux = CONF_EIC_MUX;
	extint_chan_set_config(CONF_EIC_CHAN, &config);

	struct extint_events extint_test_events;
	memset(&extint_test_events, 0, sizeof(struct extint_events));
	extint_test_events.generate_event_on_detect[CONF_EIC_CHAN] = true;
	extint_enable_events(&extint_test_events);

	/* Configure EVENTs to route fault input */
	struct events_resource event_resource;
	struct events_config event_config;
	events_get_config_defaults(&event_config);
	event_config.generator = CONF_EVENT_GENERATOR_ID;
	event_config.path      = EVENTS_PATH_ASYNCHRONOUS;
	events_allocate(&event_resource, &event_config);
	events_attach_user(&event_resource, CONF_EVENT_USER_ID_FAULTn);

	/* Clear halt status */
	tcc_clear_status(&tcc_test0_module,
			TCC_STATUS_RECOVERABLE_FAULT_PRESENT(0) |
			TCC_STATUS_RECOVERABLE_FAULT_OCCUR(0));

	uint32_t test_val1 = tcc_get_count_value(&tcc_test0_module);
	uint32_t test_val2 = tcc_get_count_value(&tcc_test0_module);

	/* Check TCC is running */
	test_assert_true(test,
			test_val1 != test_val2,
			"The counter failed to stop on recoverable fault");

	/* Set fault */
	port_pin_set_output_level(CONF_TEST_PIN_OUT, false);

	/* Check fault state */
	test_assert_true(test,
			TCC_STATUS_RECOVERABLE_FAULT_OCCUR(0) &
					tcc_get_status(&tcc_test0_module),
			"The counter failed to detect recoverable fault");

	/* Check TCC is running */
	test_val1 = tcc_get_count_value(&tcc_test0_module);
	test_val2 = tcc_get_count_value(&tcc_test0_module);
	test_assert_true(test,
			test_val1 == test_val2,
			"The counter failed to stop on recoverable fault");
}