File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed
Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change 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
1415static 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
2122unsigned 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 ))
You can’t perform that action at this time.
0 commit comments