-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy path0030-RP2040-add-lp-and-us-timer.patch
255 lines (252 loc) · 7.38 KB
/
0030-RP2040-add-lp-and-us-timer.patch
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
From 9da0254589cc629d7bc602c1ec2280f333c45748 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Tue, 23 Feb 2021 16:52:04 +0100
Subject: [PATCH 030/204] RP2040: add lp and us timer
lptimer is deactivated since it's meant to run at 1hz :|
---
.../TARGET_RP2040/lp_ticker.c | 114 ++++++++++++++++++
.../TARGET_RP2040/us_ticker.c | 93 ++++++++++++++
targets/targets.json | 2 +
3 files changed, 209 insertions(+)
create mode 100644 targets/TARGET_RASPBERRYPI/TARGET_RP2040/lp_ticker.c
create mode 100644 targets/TARGET_RASPBERRYPI/TARGET_RP2040/us_ticker.c
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/lp_ticker.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/lp_ticker.c
new file mode 100644
index 0000000000..1e48dcbfd2
--- /dev/null
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/lp_ticker.c
@@ -0,0 +1,114 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2015 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "lp_ticker_api.h"
+#include "hardware/rtc.h"
+#include "hardware/irq.h"
+#include "hardware/resets.h"
+#include "hardware/clocks.h"
+#include "time.h"
+
+#include "platform/mbed_critical.h"
+
+#define RTC_COUNTER_BITS 32u
+#define RTC_FREQ 1u
+
+#if DEVICE_LPTICKER
+
+/* LP ticker counts with 1Hz resolution */
+const ticker_info_t* lp_ticker_get_info()
+{
+ static const ticker_info_t info = {
+ RTC_FREQ,
+ RTC_COUNTER_BITS
+ };
+ return &info;
+}
+
+static datetime_t now = {
+ .year = 0,
+ .month = 1,
+ .day = 1,
+ .dotw = 0,
+ .hour = 0,
+ .min = 0,
+ .sec = 0
+};
+
+static datetime_t* epoch_to_datetime(timestamp_t epoch, datetime_t* date) {
+ return NULL;
+}
+
+static timestamp_t datetime_to_epoch(datetime_t* date) {
+ struct tm t;
+ time_t t_of_day;
+
+ t.tm_year = date->year; // Year
+ t.tm_mon = date->month + 1; // Month, where 0 = jan
+ t.tm_mday = date->day; // Day of the month
+ t.tm_hour = date->hour;
+ t.tm_min = date->min;
+ t.tm_sec = date->sec;
+ t_of_day = mktime(&t);
+
+ return (long)t_of_day;
+}
+
+void lp_ticker_init(void)
+{
+ if (rtc_running()) {
+ // Populates now struct with existing data
+ lp_ticker_read();
+ return;
+ }
+ rtc_init();
+ rtc_set_datetime(&now);
+}
+
+void lp_ticker_free(void)
+{
+}
+
+uint32_t lp_ticker_read()
+{
+ rtc_get_datetime(&now);
+ return 0; //datetime_to_epoch(&now);
+}
+
+void lp_ticker_set_interrupt(timestamp_t timestamp)
+{
+ timestamp_t target_ts = lp_ticker_read() + timestamp;
+ datetime_t target;
+ epoch_to_datetime(target_ts, &target);
+ rtc_set_alarm(&target, NULL);
+}
+
+void lp_ticker_fire_interrupt(void)
+{
+ rtc_get_datetime(&now);
+ rtc_set_alarm(&now, NULL);
+}
+
+void lp_ticker_disable_interrupt(void)
+{
+ rtc_disable_alarm();
+}
+
+void lp_ticker_clear_interrupt(void)
+{
+}
+
+#endif // DEVICE_LPTICKER
diff --git a/targets/TARGET_RASPBERRYPI/TARGET_RP2040/us_ticker.c b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/us_ticker.c
new file mode 100644
index 0000000000..00edf82830
--- /dev/null
+++ b/targets/TARGET_RASPBERRYPI/TARGET_RP2040/us_ticker.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2013 Nordic Semiconductor ASA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list
+ * of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
+ * integrated circuit in a product or a software update for such product, must reproduce
+ * the above copyright notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without specific prior
+ * written permission.
+ *
+ * 4. This software, with or without modification, must only be used with a
+ * Nordic Semiconductor ASA integrated circuit.
+ *
+ * 5. Any software provided in binary or object form under this license must not be reverse
+ * engineered, decompiled, modified and/or disassembled.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "us_ticker_api.h"
+#include "mbed_critical.h"
+#include "hardware/timer.h"
+
+#define US_TICKER_COUNTER_BITS 32u
+#define US_TICKER_FREQ 1000000
+
+/* us ticker is driven by 1MHz clock and counter length is 32 bits */
+const ticker_info_t* us_ticker_get_info()
+{
+ static const ticker_info_t info = {
+ US_TICKER_FREQ,
+ US_TICKER_COUNTER_BITS
+ };
+ return &info;
+}
+
+const uint8_t alarm_num = 0;
+
+void us_ticker_init(void)
+{
+ hardware_alarm_set_callback(alarm_num, us_ticker_irq_handler);
+}
+
+uint32_t us_ticker_read()
+{
+ return time_us_32();
+}
+
+void us_ticker_set_interrupt(timestamp_t timestamp)
+{
+ core_util_critical_section_enter();
+ absolute_time_t target = {timestamp};
+ hardware_alarm_set_target(alarm_num, target);
+ core_util_critical_section_exit();
+}
+
+void us_ticker_fire_interrupt(void)
+{
+ us_ticker_irq_handler();
+}
+
+void us_ticker_disable_interrupt(void)
+{
+ hardware_alarm_cancel(alarm_num);
+}
+
+void us_ticker_clear_interrupt(void)
+{
+ hardware_alarm_cancel(alarm_num);
+}
+
+void us_ticker_free(void)
+{
+}
diff --git a/targets/targets.json b/targets/targets.json
index d0e00a173c..6b16afb1ad 100644
--- a/targets/targets.json
+++ b/targets/targets.json
@@ -9421,12 +9421,14 @@
"device_has": [
"ANALOGIN",
"I2C",
+ "INTERRUPTIN",
"PORT_IN",
"PORT_OUT",
"PWMOUT",
"SERIAL",
"SERIAL_FC",
"SPI",
+ "USTICKER",
"USBDEVICE"
]
},
--
2.39.1