|
| 1 | +/* HW Timer test */ |
| 2 | +#include <unity.h> |
| 3 | + |
| 4 | +#define TIMER_FREQUENCY 4000000 |
| 5 | +#define TIMER_FREQUENCY_XTAL_CLK 1000 |
| 6 | + |
| 7 | +/* |
| 8 | + * ESP32 - APB clk only (1kHz not possible) |
| 9 | + * C3 - APB + XTAL clk |
| 10 | + * S2 - APB + XTAL clk |
| 11 | + * S3 - APB + XTAL clk |
| 12 | + */ |
| 13 | + |
| 14 | +static hw_timer_t *timer = NULL; |
| 15 | +static volatile bool alarm_flag; |
| 16 | + |
| 17 | +/* setUp / tearDown functions are intended to be called before / after each test. */ |
| 18 | +void setUp(void) { |
| 19 | + timer = timerBegin(TIMER_FREQUENCY); |
| 20 | + if (timer == NULL) { |
| 21 | + TEST_FAIL_MESSAGE("Timer init failed in setUp()"); |
| 22 | + } |
| 23 | + timerStop(timer); |
| 24 | + timerRestart(timer); |
| 25 | +} |
| 26 | + |
| 27 | +void tearDown(void) { |
| 28 | + timerEnd(timer); |
| 29 | +} |
| 30 | + |
| 31 | +void ARDUINO_ISR_ATTR onTimer() { |
| 32 | + alarm_flag = true; |
| 33 | +} |
| 34 | + |
| 35 | +void timer_interrupt_test(void) { |
| 36 | + |
| 37 | + alarm_flag = false; |
| 38 | + timerAttachInterrupt(timer, &onTimer); |
| 39 | + timerAlarm(timer, (1.2 * TIMER_FREQUENCY), true, 0); |
| 40 | + timerStart(timer); |
| 41 | + |
| 42 | + delay(2000); |
| 43 | + |
| 44 | + TEST_ASSERT_EQUAL(true, alarm_flag); |
| 45 | + |
| 46 | + timerStop(timer); |
| 47 | + timerRestart(timer); |
| 48 | + alarm_flag = false; |
| 49 | + timerDetachInterrupt(timer); |
| 50 | + timerStart(timer); |
| 51 | + |
| 52 | + delay(2000); |
| 53 | + TEST_ASSERT_EQUAL(false, alarm_flag); |
| 54 | +} |
| 55 | + |
| 56 | +void timer_divider_test(void) { |
| 57 | + |
| 58 | + uint64_t time_val; |
| 59 | + uint64_t comp_time_val; |
| 60 | + |
| 61 | + timerStart(timer); |
| 62 | + |
| 63 | + delay(1000); |
| 64 | + time_val = timerRead(timer); |
| 65 | + |
| 66 | + // compare divider 16 and 8, value should be double |
| 67 | + timerEnd(timer); |
| 68 | + |
| 69 | + timer = timerBegin(2 * TIMER_FREQUENCY); |
| 70 | + if (timer == NULL) { |
| 71 | + TEST_FAIL_MESSAGE("Timer init failed!"); |
| 72 | + } |
| 73 | + timerRestart(timer); |
| 74 | + delay(1000); |
| 75 | + comp_time_val = timerRead(timer); |
| 76 | + |
| 77 | + TEST_ASSERT_INT_WITHIN(4000, 4000000, time_val); |
| 78 | + TEST_ASSERT_INT_WITHIN(8000, 8000000, comp_time_val); |
| 79 | + |
| 80 | + // divider is 256, value should be 2^4 |
| 81 | + timerEnd(timer); |
| 82 | + |
| 83 | + timer = timerBegin(TIMER_FREQUENCY / 16); |
| 84 | + if (timer == NULL) { |
| 85 | + TEST_FAIL_MESSAGE("Timer init failed!"); |
| 86 | + } |
| 87 | + timerRestart(timer); |
| 88 | + delay(1000); |
| 89 | + comp_time_val = timerRead(timer); |
| 90 | + |
| 91 | + TEST_ASSERT_INT_WITHIN(4000, 4000000, time_val); |
| 92 | + TEST_ASSERT_INT_WITHIN(2500, 250000, comp_time_val); |
| 93 | +} |
| 94 | + |
| 95 | +void timer_read_test(void) { |
| 96 | + |
| 97 | + uint64_t set_timer_val = 0xFF; |
| 98 | + uint64_t get_timer_val = 0; |
| 99 | + |
| 100 | + timerWrite(timer, set_timer_val); |
| 101 | + get_timer_val = timerRead(timer); |
| 102 | + |
| 103 | + TEST_ASSERT_EQUAL(set_timer_val, get_timer_val); |
| 104 | +} |
| 105 | + |
| 106 | +void timer_clock_select_test(void) { |
| 107 | + // Set timer frequency that can be achieved using XTAL clock source (autoselected) |
| 108 | + timer = timerBegin(TIMER_FREQUENCY_XTAL_CLK); |
| 109 | + |
| 110 | + uint32_t resolution = timerGetFrequency(timer); |
| 111 | + TEST_ASSERT_EQUAL(TIMER_FREQUENCY_XTAL_CLK, resolution); |
| 112 | +} |
| 113 | + |
| 114 | +void setup() { |
| 115 | + |
| 116 | + // Open serial communications and wait for port to open: |
| 117 | + Serial.begin(115200); |
| 118 | + while (!Serial) { |
| 119 | + ; |
| 120 | + } |
| 121 | + |
| 122 | + UNITY_BEGIN(); |
| 123 | + RUN_TEST(timer_read_test); |
| 124 | + RUN_TEST(timer_interrupt_test); |
| 125 | + RUN_TEST(timer_divider_test); |
| 126 | +#if !CONFIG_IDF_TARGET_ESP32 |
| 127 | + RUN_TEST(timer_clock_select_test); |
| 128 | +#endif |
| 129 | + UNITY_END(); |
| 130 | +} |
| 131 | + |
| 132 | +void loop() {} |
0 commit comments