|
23 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | 24 | * THE SOFTWARE. |
25 | 25 | */ |
26 | | - |
27 | | -#include <stdbool.h> |
28 | | -#include <sys/time.h> |
29 | | -#include <cxd56_clock.h> |
30 | | -#include <sys/boardctl.h> |
31 | | - |
32 | | -#include "py/mpstate.h" |
33 | | - |
34 | | -#include "supervisor/shared/tick.h" |
35 | | - |
36 | | -#define DELAY_CORRECTION (700) |
37 | | -#define DELAY_INTERVAL (50) |
38 | | - |
39 | | -void mp_hal_init(void) { |
40 | | - boardctl(BOARDIOC_INIT, 0); |
41 | | -} |
42 | | - |
43 | | -mp_uint_t mp_hal_ticks_ms(void) { |
44 | | - struct timeval tv; |
45 | | - gettimeofday(&tv, NULL); |
46 | | - return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
47 | | -} |
48 | | - |
49 | | -mp_uint_t mp_hal_ticks_us(void) { |
50 | | - struct timeval tv; |
51 | | - gettimeofday(&tv, NULL); |
52 | | - return tv.tv_sec * 1000000 + tv.tv_usec; |
53 | | -} |
54 | | - |
55 | | -mp_uint_t mp_hal_ticks_cpu(void) { |
56 | | - return cxd56_get_cpu_baseclk(); |
57 | | -} |
58 | | - |
59 | | -void mp_hal_delay_ms(mp_uint_t delay) { |
60 | | - uint64_t start_tick = supervisor_ticks_ms64(); |
61 | | - uint64_t duration = 0; |
62 | | - while (duration < delay) { |
63 | | - #ifdef MICROPY_VM_HOOK_LOOP |
64 | | - MICROPY_VM_HOOK_LOOP |
65 | | - #endif |
66 | | - // Check to see if we've been CTRL-Ced by autoreload or the user. |
67 | | - if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)) || |
68 | | - MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { |
69 | | - break; |
70 | | - } |
71 | | - duration = (supervisor_ticks_ms64() - start_tick); |
72 | | - // TODO(tannewt): Go to sleep for a little while while we wait. |
73 | | - } |
74 | | -} |
75 | | - |
76 | | -void mp_hal_delay_us(uint32_t us) { |
77 | | - if (us) { |
78 | | - unsigned long long ticks = mp_hal_ticks_cpu() / 1000000L * us; |
79 | | - if (ticks < DELAY_CORRECTION) return; // delay time already used in calculation |
80 | | - |
81 | | - ticks -= DELAY_CORRECTION; |
82 | | - ticks /= 6; |
83 | | - // following loop takes 6 cycles |
84 | | - do { |
85 | | - __asm__ __volatile__("nop"); |
86 | | - } while(--ticks); |
87 | | - } |
88 | | -} |
0 commit comments