// max timeout is 27 seconds at 160MHz clock and 54 seconds at 80MHz clock unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) { const uint32_t max_timeout_us = clockCyclesToMicroseconds(UINT_MAX); if (timeout > max_timeout_us) { timeout = max_timeout_us; } const uint32_t timeout_cycles = microsecondsToClockCycles(timeout); const uint32_t start_cycle_count = xthal_get_ccount(); WAIT_FOR_PIN_STATE(!state); WAIT_FOR_PIN_STATE(state); const uint32_t pulse_start_cycle_count = xthal_get_ccount(); WAIT_FOR_PIN_STATE(!state); return clockCyclesToMicroseconds(xthal_get_ccount() - pulse_start_cycle_count); }
static void testthread(void *arg) { intr_handle_t handle; in_int_context=0; int_handled=0; TEST_ASSERT(!xPortInIsrContext()); xthal_set_ccompare(1, xthal_get_ccount()+8000000); esp_err_t err = esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, 0, &testint, NULL, &handle); TEST_ASSERT_EQUAL_HEX32(ESP_OK, err); vTaskDelay(100 / portTICK_PERIOD_MS); TEST_ASSERT(int_handled); TEST_ASSERT(in_int_context); TEST_ASSERT_EQUAL_HEX32( ESP_OK, esp_intr_free(handle) ); vTaskDelete(NULL); }
void local_timer_test() { intr_handle_t ih; esp_err_t r; r=esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, 0, int_timer_handler, NULL, &ih); TEST_ASSERT(r==ESP_OK); printf("Int timer 1 intno %d\n", esp_intr_get_intno(ih)); xthal_set_ccompare(1, xthal_get_ccount()+8000000); int_timer_ctr=0; vTaskDelay(1000 / portTICK_PERIOD_MS); printf("Timer val after 1 sec: %d\n", int_timer_ctr); TEST_ASSERT(int_timer_ctr!=0); printf("Disabling int\n"); esp_intr_disable(ih); int_timer_ctr=0; vTaskDelay(1000 / portTICK_PERIOD_MS); printf("Timer val after 1 sec: %d\n", int_timer_ctr); TEST_ASSERT(int_timer_ctr==0); printf("Re-enabling\n"); esp_intr_enable(ih); vTaskDelay(1000 / portTICK_PERIOD_MS); printf("Timer val after 1 sec: %d\n", int_timer_ctr); TEST_ASSERT(int_timer_ctr!=0); printf("Free int, re-alloc disabled\n"); r=esp_intr_free(ih); TEST_ASSERT(r==ESP_OK); r=esp_intr_alloc(ETS_INTERNAL_TIMER1_INTR_SOURCE, ESP_INTR_FLAG_INTRDISABLED, int_timer_handler, NULL, &ih); TEST_ASSERT(r==ESP_OK); int_timer_ctr=0; vTaskDelay(1000 / portTICK_PERIOD_MS); printf("Timer val after 1 sec: %d\n", int_timer_ctr); TEST_ASSERT(int_timer_ctr==0); printf("Re-enabling\n"); esp_intr_enable(ih); vTaskDelay(1000 / portTICK_PERIOD_MS); printf("Timer val after 1 sec: %d\n", int_timer_ctr); TEST_ASSERT(int_timer_ctr!=0); r=esp_intr_free(ih); TEST_ASSERT(r==ESP_OK); printf("Done.\n"); }
void sdk_os_delay_us(uint16_t us) { uint32_t start_ccount = xthal_get_ccount(); uint32_t delay_ccount = cpu_freq * us; while (xthal_get_ccount() - start_ccount < delay_ccount) {} }
void int_timer_handler(void *arg) { xthal_set_ccompare(1, xthal_get_ccount()+8000000); int_timer_ctr++; }
static void testint(void *arg) { xthal_set_ccompare(1, xthal_get_ccount()+8000000000); ets_printf("INT!\n"); if (xPortInIsrContext()) in_int_context++; int_handled++; }
/* To avoid accidentally losing the fix for the TCP port randomization * during an LWIP upgrade, we've implemented most it outside the LWIP * source itself. This enables us to test for the presence of the fix * /at link time/ and error out if it's been lost. * The fix itself consists of putting the function-static 'port' variable * into its own section, and get the linker to provide an alias for it. * From there we can then manually randomize it at boot. */ static inline void tcp_random_port_init (void) { extern uint16_t _tcp_new_port_port; // provided by the linker script _tcp_new_port_port += xthal_get_ccount () % 4096; }