Skip to content

Commit fb02b1f

Browse files
committed
2 parents 760008c + 1a61d0b commit fb02b1f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

cores/arduino/wiring_pulse.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "gd32vf103.h"
22
#include "pins_arduino.h"
3+
#include "riscv_encoding.h"
34
#include <stdint.h>
45

56
#define _ClockCyclesPerMicrosecond() (SystemCoreClock / 1000000)
@@ -13,15 +14,20 @@ union _mcycle_full {
1314

1415
static inline uint64_t get_mcycle(void) {
1516
union _mcycle_full ret;
16-
__asm("rdcycle %[l]" : [ l ] "=r"(ret.v_part[0]) :);
17-
__asm("rdcycleh %[h]" : [ h ] "=r"(ret.v_part[1]) :);
17+
__asm volatile("rdcycle %[l]" : [ l ] "=r"(ret.v_part[0]) :);
18+
__asm volatile("rdcycleh %[h]" : [ h ] "=r"(ret.v_part[1]) :);
1819
return ret.v;
1920
}
2021

2122
unsigned long pulseIn(pin_size_t pin, uint8_t state, unsigned long timeout) {
2223
if (pin >= VARIANT_GPIO_NUM) {
2324
return 0;
2425
}
26+
27+
// enable cycle counter by resetting bit 0 of mcountinhibit register
28+
uint32_t mcountinhibit = read_csr(0x320);
29+
write_csr(0x320, mcountinhibit & 0xFFFFFFFE);
30+
2531
uint64_t timeoutInCycles = timeout * _ClockCyclesPerMicrosecond();
2632
uint64_t start = get_mcycle();
2733
while (gpio_input_bit_get(digitalPinToPort(pin), digitalPinToBitMask(pin))

0 commit comments

Comments
 (0)