/** * \internal * \brief Test for AC comparison in single shot mode. * * This test checks the single shot comparison of the AC. * 0.5V is applied to the negative input of AC from internal voltage scaler. * 0V and 1V from DAC is applied to the positive input and the results * are verified. * * \param test Current test case. */ static void run_ac_single_shot_test(const struct test_case *test) { volatile uint32_t state = AC_CHAN_STATUS_UNKNOWN; /* Skip test if initialization failed */ test_assert_true(test, ac_init_success, "Skipping test due to failed AC initialization"); /* Test for positive input < negative input */ dac_chan_write(&dac_inst, DAC_CHANNEL_0, DAC_VAL_ZERO_VOLT); delay_ms(1); ac_chan_trigger_single_shot(&ac_inst, AC_CHAN_CHANNEL_0); while (!ac_chan_is_ready(&ac_inst, AC_CHAN_CHANNEL_0)) { } state = ac_chan_get_status(&ac_inst, AC_CHAN_CHANNEL_0); state = state & AC_CHAN_STATUS_NEG_ABOVE_POS; test_assert_true(test, state == AC_CHAN_STATUS_NEG_ABOVE_POS, "AC comparison failed: POS < NEG not detected"); /* Test for negative input < positive input */ state = AC_CHAN_STATUS_UNKNOWN; dac_chan_write(&dac_inst, DAC_CHANNEL_0, DAC_VAL_ONE_VOLT); delay_ms(1); ac_chan_trigger_single_shot(&ac_inst, AC_CHAN_CHANNEL_0); while (!ac_chan_is_ready(&ac_inst, AC_CHAN_CHANNEL_0)) { } state = ac_chan_get_status(&ac_inst, AC_CHAN_CHANNEL_0); state = state & AC_CHAN_STATUS_POS_ABOVE_NEG; test_assert_true(test, state == AC_CHAN_STATUS_POS_ABOVE_NEG, "AC comparison failed: Interrupt not detected"); ac_chan_clear_status(&ac_inst, AC_CHAN_CHANNEL_0); }
/** * \internal * \brief AC window mode test function * * This test checks the window functionality of the AC module. * Inputs are given in each region of the window (below, inside & above) * and corresponding window output states are verified. * * \param test Current test case. */ static void run_ac_window_mode_test(const struct test_case *test) { volatile uint32_t state = AC_WIN_STATUS_UNKNOWN; /* Skip test if initialization failed */ test_assert_true(test, ac_init_success, "Skipping test due to failed AC initialization"); /* Test for region-below detection */ dac_chan_write(&dac_inst, DAC_CHANNEL_0, DAC_VAL_ZERO_VOLT); delay_ms(1); ac_chan_trigger_single_shot(&ac_inst, AC_CHAN_CHANNEL_0); while (!ac_win_is_ready(&ac_inst, AC_WIN_CHANNEL_0)) { } state = ac_win_get_status(&ac_inst, AC_WIN_CHANNEL_0); state = state & AC_WIN_STATUS_BELOW; test_assert_true(test, state == AC_WIN_STATUS_BELOW, "AC window mode: Less than lower limit not detected"); ac_win_clear_status(&ac_inst, AC_WIN_CHANNEL_0); ac_chan_clear_status(&ac_inst, AC_CHAN_CHANNEL_0); ac_chan_clear_status(&ac_inst, AC_CHAN_CHANNEL_1); /* Test for region-inside detection */ state = AC_WIN_STATUS_UNKNOWN; dac_chan_write(&dac_inst, DAC_CHANNEL_0, DAC_VAL_HALF_VOLT); delay_ms(1); ac_chan_trigger_single_shot(&ac_inst, AC_CHAN_CHANNEL_0); while (!ac_win_is_ready(&ac_inst, AC_WIN_CHANNEL_0)) { } state = ac_win_get_status(&ac_inst, AC_WIN_CHANNEL_0); state = state & AC_WIN_STATUS_INSIDE; test_assert_true(test, state == AC_WIN_STATUS_INSIDE, "AC window mode: Within limit not detected"); ac_win_clear_status(&ac_inst, AC_WIN_CHANNEL_0); ac_chan_clear_status(&ac_inst, AC_CHAN_CHANNEL_0); ac_chan_clear_status(&ac_inst, AC_CHAN_CHANNEL_1); /* Test for region-above detection */ state = AC_WIN_STATUS_UNKNOWN; dac_chan_write(&dac_inst, DAC_CHANNEL_0, DAC_VAL_ONE_VOLT); delay_ms(1); ac_chan_trigger_single_shot(&ac_inst, AC_CHAN_CHANNEL_0); while (!ac_win_is_ready(&ac_inst, AC_WIN_CHANNEL_0)) { } state = ac_win_get_status(&ac_inst, AC_WIN_CHANNEL_0); state = state & AC_WIN_STATUS_ABOVE; test_assert_true(test, state == AC_WIN_STATUS_ABOVE, "AC window mode: More than upper limit not detected"); ac_win_clear_status(&ac_inst, AC_WIN_CHANNEL_0); ac_chan_clear_status(&ac_inst, AC_CHAN_CHANNEL_0); ac_chan_clear_status(&ac_inst, AC_CHAN_CHANNEL_1); }
//! [setup] int main(void) { //! [setup_init] system_init(); configure_ac(); configure_ac_channel(); configure_ac_callback(); //! [setup_17] ac_enable(&ac_instance); //! [setup_17] //! [setup_init] //! [main] //! [main_1] ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL); //! [main_1] //! [main_2] uint8_t last_comparison = AC_CHAN_STATUS_UNKNOWN; //! [main_2] port_pin_set_output_level(LED_0_PIN, true); //! [main_3] while (true) { //! [main_3] //! [main_4] if (callback_status == true) { //! [main_4] //! [main_5] do { last_comparison = ac_chan_get_status(&ac_instance, AC_COMPARATOR_CHANNEL); } while (last_comparison & AC_CHAN_STATUS_UNKNOWN); //! [main_5] //! [main_6] port_pin_set_output_level(LED_0_PIN, (last_comparison & AC_CHAN_STATUS_NEG_ABOVE_POS)); //! [main_6] //! [main_7] callback_status = false; //! [main_7] //! [main_8] ac_chan_trigger_single_shot(&ac_instance, AC_COMPARATOR_CHANNEL); //! [main_8] } } //! [main] }