/** * \internal * \brief Common function to test frequency computation from QDEC * * \param test Current test case * \param res QDec resolution for this test * \param delay Delay between each quadrature steps * \param freq_unit QDec frequency unit used for this test * \param freq Expected frequency for this test */ static void run_qdec_freq_mode(const struct test_case *test, uint16_t res, uint8_t delay, uint16_t freq_unit, uint16_t freq) { qdec_config_t config; uint32_t freq_computed; /* Set to 0 the pins */ generator_qenc_reset(); /* QDec configuration */ qdec_get_config_defaults(&config); qdec_config_phase_pins(&config, CONF_QDEC_PORT, CONF_QDEC_PINS_BASE, false, 1); qdec_config_tc(&config, CONF_QDEC_TC); qdec_config_enable_freq(&config, freq_unit); qdec_config_freq_tc(&config, CONF_QDEC_FREQ_TC); qdec_config_revolution(&config, res); /* QDec enable */ qdec_enabled(&config); /* Reenable dir out on pins */ generator_qenc_reset(); /* Launch test */ generator_qenc(QENC_STEP_1); main_delay_ms(delay); generator_qenc(QENC_STEP_2); main_delay_ms(delay); generator_qenc(QENC_STEP_3); main_delay_ms(delay); generator_qenc(QENC_STEP_4); main_delay_ms(delay); generator_qenc(QENC_STEP_1); main_delay_ms(delay); generator_qenc(QENC_STEP_2); main_delay_ms(delay); generator_qenc(QENC_STEP_3); freq_computed = qdec_get_frequency(&config); /* 5% tolerance on Frequency */ if (freq_unit == 1) { test_assert_true(test, (freq_computed >= (freq - ((freq * 5) / 100))) && (freq_computed <= (freq + ((freq * 5) / 100))), "QDec frequency failure: expected %umHz, catched %umHz\r\n", freq, freq_computed); } else { test_assert_true(test, (freq_computed >= (freq - ((freq * 5) / 100))) && (freq_computed <= (freq + ((freq * 5) / 100))), "QDec frequency failure: expected %uHz, catched %uHz\r\n", freq, freq_computed); } }
/** * \brief Main application routine * - Configure system clock * - Call QDec driver API to configure it and enable it * - Call generator of quadrature encoder * - Get position, direction and frequency and display its */ int main( void ) { const usart_serial_options_t usart_serial_options = { .baudrate = CONF_TEST_BAUDRATE, .charlength = CONF_TEST_CHARLENGTH, .paritytype = CONF_TEST_PARITY, .stopbits = CONF_TEST_STOPBITS, }; qdec_config_t config; uint16_t qdec_position, qdec_position_prev = 0; uint32_t generator_qenc_freq = 2000 / QUADRATURE_RESOLUTION; bool generator_qenc_dir = true; /* Usual initializations */ board_init(); sysclk_init(); sleepmgr_init(); irq_initialize_vectors(); cpu_irq_enable(); stdio_serial_init(CONF_TEST_USART, &usart_serial_options); printf("\x0C\n\r-- QDec Example --\n\r"); printf("-- Compiled: %s %s --\n\r\n\r", __DATE__, __TIME__); /* QDec configuration * Here, we use the default hardware ressources * proposed by default configuration. */ qdec_get_config_defaults(&config); qdec_config_phase_pins(&config, CONF_QDEC_PORT, CONF_QDEC_PINS_BASE, false, 1); qdec_config_tc(&config, CONF_QDEC_TC); qdec_config_enable_freq(&config, 1); qdec_config_freq_tc(&config, CONF_QDEC_FREQ_TC); qdec_config_revolution(&config, QUADRATURE_RESOLUTION); /* QDec enable */ qdec_enabled(&config); /* Generate quadrature encoder signals */ generator_qenc_enable(CONF_QDEC_PORT, CONF_QDEC_PINS_BASE, CONF_GENERATOR_QENC_TC, QUADRATURE_RESOLUTION, generator_qenc_freq, generator_qenc_dir); while (1) { /* read position */ qdec_position = qdec_get_position(&config); if (qdec_position_prev != qdec_position) { /* New position then display it */ qdec_position_prev = qdec_position; printf("%02u", qdec_position); /* display direction */ if (qdec_get_direction(&config)) { printf(" ++"); } else { printf(" --"); } /* Display frequency */ printf(" %5umHz\r\n", qdec_get_frequency(&config)); } /* Manage Quadrature encoder generator through UART */ if (usart_rx_is_complete(CONF_TEST_USART)) { char key = getchar(); if (key == 'd') { generator_qenc_dir = !generator_qenc_dir; generator_qenc_set_direction(generator_qenc_dir); } if (key == '-') { if (generator_qenc_freq > QUADRATURE_GENERATOR_FREQ_STEP) { generator_qenc_freq -= QUADRATURE_GENERATOR_FREQ_STEP; generator_qenc_set_freq(generator_qenc_freq); } } if (key == '+') { generator_qenc_freq += QUADRATURE_GENERATOR_FREQ_STEP; generator_qenc_set_freq(generator_qenc_freq); } } } }