Skip to content

Commit 18513de

Browse files
authored
Merge pull request #3710 from jepler/esp32-i2c-crash
esp32: Two random changes that also fixed the I2C crash for me
2 parents 226715b + 9206925 commit 18513de

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

ports/esp32s2/common-hal/pulseio/PulseIn.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ void pulsein_reset(void) {
7777
for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) {
7878
handles[i] = NULL;
7979
}
80-
supervisor_disable_tick();
80+
if (refcount != 0) {
81+
supervisor_disable_tick();
82+
}
8183
refcount = 0;
8284
}
8385

@@ -122,8 +124,10 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
122124

123125
// start RMT RX, and enable ticks so the core doesn't turn off.
124126
rmt_rx_start(channel, true);
125-
supervisor_enable_tick();
126127
refcount++;
128+
if (refcount == 1) {
129+
supervisor_enable_tick();
130+
}
127131
}
128132

129133
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {

ports/esp32s2/supervisor/port.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ uint32_t port_get_saved_word(void) {
191191
}
192192

193193
uint64_t port_get_raw_ticks(uint8_t* subticks) {
194-
struct timeval tv_now;
195-
gettimeofday(&tv_now, NULL);
196-
// convert usec back to ticks
197-
uint64_t all_subticks = (uint64_t)(tv_now.tv_usec * 2) / 71;
194+
// Convert microseconds to subticks of 1/32768 seconds
195+
// 32768/1000000 = 64/15625 in lowest terms
196+
// this arithmetic overflows after 570 years
197+
int64_t all_subticks = esp_timer_get_time() * 512 / 15625;
198198
if (subticks != NULL) {
199199
*subticks = all_subticks % 32;
200200
}
201-
return (uint64_t)tv_now.tv_sec * 1024L + all_subticks / 32;
201+
return all_subticks / 32;
202202
}
203203

204204
// Enable 1/1024 second tick.

0 commit comments

Comments
 (0)