1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2019 Damien P. George
7  * Copyright (c) 2020 Jim Mussared
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 #ifndef MICROPY_INCLUDED_MIMXRT_MPHALPORT_H
28 #define MICROPY_INCLUDED_MIMXRT_MPHALPORT_H
29 
30 #include <stdint.h>
31 #include "ticks.h"
32 #include "pin.h"
33 
34 #define MP_HAL_PIN_FMT                  "%q"
35 
36 #define mp_hal_pin_obj_t const machine_pin_obj_t *
37 #define mp_hal_get_pin_obj(o)   pin_find(o)
38 #define mp_hal_pin_name(p)      ((p)->name)
39 #define mp_hal_pin_input(p) machine_pin_set_mode(p, PIN_MODE_IN);
40 #define mp_hal_pin_output(p) machine_pin_set_mode(p, PIN_MODE_OUT);
41 #define mp_hal_pin_open_drain(p) machine_pin_set_mode(p, PIN_MODE_OPEN_DRAIN);
42 #define mp_hal_pin_high(p) (GPIO_PinWrite(p->gpio, p->pin, 1U))
43 #define mp_hal_pin_low(p) (GPIO_PinWrite(p->gpio, p->pin, 0U))
44 #define mp_hal_pin_write(p, value) (GPIO_PinWrite(p->gpio, p->pin, value))
45 #define mp_hal_pin_toggle(p) (GPIO_PortToggle(p->gpio, (1 << p->pin)))
46 #define mp_hal_pin_read(p) (GPIO_PinReadPadStatus(p->gpio, p->pin))
47 
48 #define mp_hal_pin_od_low(p)    mp_hal_pin_low(p)
49 #define mp_hal_pin_od_high(p)   mp_hal_pin_high(p)
50 
51 #define mp_hal_quiet_timing_enter() MICROPY_BEGIN_ATOMIC_SECTION()
52 #define mp_hal_quiet_timing_exit(irq_state) MICROPY_END_ATOMIC_SECTION(irq_state)
53 
54 void mp_hal_set_interrupt_char(int c);
55 
mp_hal_ticks_ms(void)56 static inline mp_uint_t mp_hal_ticks_ms(void) {
57     return ticks_ms32();
58 }
59 
mp_hal_ticks_us(void)60 static inline mp_uint_t mp_hal_ticks_us(void) {
61     return ticks_us32();
62 }
63 
mp_hal_delay_ms(mp_uint_t ms)64 static inline void mp_hal_delay_ms(mp_uint_t ms) {
65     uint64_t us = (uint64_t)ms * 1000;
66     ticks_delay_us64(us);
67 }
68 
mp_hal_delay_us(mp_uint_t us)69 static inline void mp_hal_delay_us(mp_uint_t us) {
70     ticks_delay_us64(us);
71 }
72 
73 #define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
74 
mp_hal_ticks_cpu(void)75 static inline mp_uint_t mp_hal_ticks_cpu(void) {
76     return 0;
77 }
78 
79 
80 #endif // MICROPY_INCLUDED_MIMXRT_MPHALPORT_H
81