forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mphalport.c
176 lines (135 loc) · 5.04 KB
/
mphalport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022-2024 Infineon Technologies AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// std includes
#include "stdbool.h" // because of missing include in shared/timeutils/timeutils.h
#include "stdio.h"
// micropython includes
#include "mpconfigport.h"
#include "mphalport.h"
#include "py/runtime.h"
#include "shared/timeutils/timeutils.h"
// MTB includes
#include "cyhal.h"
#include "cy_retarget_io.h"
// port-specific includes
#include "machine_pin_phy.h"
extern cyhal_rtc_t psoc6_rtc;
extern cyhal_timer_t psoc6_timer;
void mp_hal_delay_ms(mp_uint_t ms) {
#if defined(CY_RTOS_AWARE) || defined(COMPONENT_RTOS_AWARE)
// Check
// file: mtb-hal-cat1/src/cyhal_system.c
// function: cyhal_system_delay_ms()
// lines 65-68
// for an explanation.
// An increment of 1 ms is added to the delay. In principle
// that should be corrected by some internal behaviour or RTOS
// but it does not seem to work like this, as we are always
// getting one more second in our Test 1 of tests/psoc/time.py.
// TODO: Find if there is a more elegant way to avoid the RTOS
// configuration to propagate to this level.
ms -= 1;
#endif
cyhal_system_delay_ms(ms);
}
void mp_hal_delay_us(mp_uint_t us) {
cyhal_system_delay_us(us);
}
// Issues may arise if time is incremented only each second.
// Would require proper ns count from epoch of clock the source (see also "extmod/vfs_lfs.c", function "lfs_get_mtime" and "mphalport.c", function "mp_hal_time_ns")
uint64_t mp_hal_time_ns(void) {
struct tm current_date_time = {0};
cy_rslt_t result = cyhal_rtc_read(&psoc6_rtc, ¤t_date_time);
if (CY_RSLT_SUCCESS != result) {
mp_raise_ValueError(MP_ERROR_TEXT("cyhal_rtc_read failed !"));
}
uint64_t s = timeutils_seconds_since_epoch(current_date_time.tm_year, current_date_time.tm_mon, current_date_time.tm_mday,
current_date_time.tm_hour, current_date_time.tm_min, current_date_time.tm_sec);
// add ticks to make sure time is strictly monotonic
return s * 1000000000ULL + cyhal_timer_read(&psoc6_timer) * 1000ULL;
}
mp_uint_t mp_hal_ticks_ms(void) {
return cyhal_timer_read(&psoc6_timer) / 1000;
}
mp_uint_t mp_hal_ticks_us(void) {
return cyhal_timer_read(&psoc6_timer);
}
mp_uint_t mp_hal_ticks_cpu(void) {
return cyhal_timer_read(&psoc6_timer);
}
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
printf("mp_hal_stdio_poll\n");
mp_raise_NotImplementedError(MP_ERROR_TEXT("mp_hal_stdio_poll not implemented !"));
uintptr_t ret = 0;
return ret;
}
// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
int r = write(STDOUT_FILENO, str, len);
(void)r;
}
int mp_hal_stdin_rx_chr(void) {
for (;;) {
uint8_t c = 0;
cy_rslt_t result;
result = cyhal_uart_getc(&cy_retarget_io_uart_obj, &c, 1);
if (result == CY_RSLT_SUCCESS) {
return c;
}
MICROPY_EVENT_POLL_HOOK
}
}
void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) {
cyhal_gpio_write(pin, false);
}
void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) {
cyhal_gpio_write(pin, true);
}
int mp_hal_pin_read(mp_hal_pin_obj_t pin) {
return cyhal_gpio_read(pin);
}
void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
cyhal_gpio_configure(pin, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW);
}
uint8_t mp_hal_pin_name(mp_hal_pin_obj_t pin) {
return pin;
}
void mp_hal_pin_write(mp_hal_pin_obj_t pin, uint8_t polarity) {
if (polarity == 1) {
cyhal_gpio_write(pin, true);
} else {
cyhal_gpio_write(pin, false);
}
}
void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
cyhal_gpio_configure(pin, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_NONE);
}
void mp_hal_pin_input(mp_hal_pin_obj_t pin) {
cyhal_gpio_configure(pin, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_NONE);
}
mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) {
return pin_addr_by_name(obj);
}