Skip to content

Commit 63904f3

Browse files
AlessandroLuokartben
authored andcommitted
drivers: rtc: add rtc support for apollo3&3p
Add RTC support for Apollo3 and Apollo3 Plus Soc Signed-off-by: Hao Luo <[email protected]>
1 parent 888bf13 commit 63904f3

File tree

6 files changed

+82
-21
lines changed

6 files changed

+82
-21
lines changed

boards/ambiq/apollo3_evb/apollo3_evb.dts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
sw1 = &button1;
3131
bootloader-led0 = &led0;
3232
mcuboot-led0 = &led0;
33+
rtc = &rtc0;
3334
};
3435

3536
leds {
@@ -189,6 +190,11 @@
189190
status = "okay";
190191
};
191192

193+
&rtc0 {
194+
status = "okay";
195+
clock = "XTAL";
196+
};
197+
192198
&adc0 {
193199
compatible = "ambiq,adc";
194200
pinctrl-0 = <&adc0_default>;

boards/ambiq/apollo3p_evb/apollo3p_evb.dts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <ambiq/ambiq_apollo3p_blue.dtsi>
33

44
#include "apollo3p_evb-pinctrl.dtsi"
5+
#include <zephyr/dt-bindings/input/input-event-codes.h>
56

67
/ {
78
model = "Ambiq Apollo3 Blue Plus evaluation board";
@@ -25,6 +26,7 @@
2526
led2 = &led2;
2627
sw0 = &button0;
2728
sw1 = &button1;
29+
rtc = &rtc0;
2830
};
2931

3032
leds {
@@ -56,14 +58,17 @@
5658
button0: button_0 {
5759
gpios = <&gpio0_31 16 GPIO_ACTIVE_LOW>;
5860
label = "BTN0";
61+
zephyr,code = <INPUT_KEY_0>;
5962
};
6063
button1: button_1 {
6164
gpios = <&gpio0_31 18 GPIO_ACTIVE_LOW>;
6265
label = "BTN1";
66+
zephyr,code = <INPUT_KEY_1>;
6367
};
6468
button2: button_2 {
6569
gpios = <&gpio0_31 19 GPIO_ACTIVE_LOW>;
6670
label = "BTN2";
71+
zephyr,code = <INPUT_KEY_2>;
6772
};
6873
};
6974
};
@@ -163,6 +168,11 @@
163168
status = "okay";
164169
};
165170

171+
&rtc0 {
172+
status = "okay";
173+
clock = "XTAL";
174+
};
175+
166176
&adc0 {
167177
compatible = "ambiq,adc";
168178
pinctrl-0 = <&adc0_default>;

drivers/rtc/rtc_ambiq.c

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ struct ambiq_rtc_data {
4242

4343
static void rtc_time_to_ambiq_time_set(const struct rtc_time *tm, am_hal_rtc_time_t *atm)
4444
{
45-
atm->ui32CenturyBit = ((tm->tm_year <= 99) || (tm->tm_year >= 200));
45+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
46+
atm->ui32Century = ((tm->tm_year <= 99) || (tm->tm_year >= 200));
47+
#else
48+
atm->ui32CenturyBit = ((tm->tm_year > 99) && (tm->tm_year < 200));
49+
#endif
4650
atm->ui32Year = tm->tm_year;
4751
if (tm->tm_year > 99) {
4852
atm->ui32Year = tm->tm_year % 100;
@@ -68,11 +72,19 @@ static void rtc_time_to_ambiq_time_set(const struct rtc_time *tm, am_hal_rtc_tim
6872
static void ambiq_time_to_rtc_time_set(const am_hal_rtc_time_t *atm, struct rtc_time *tm)
6973
{
7074
tm->tm_year = atm->ui32Year;
71-
if (atm->ui32CenturyBit == 0) {
75+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
76+
if (atm->ui32Century == 0) {
7277
tm->tm_year += 100;
7378
} else {
7479
tm->tm_year += 200;
7580
}
81+
#else
82+
if (atm->ui32CenturyBit == 0) {
83+
tm->tm_year += 200;
84+
} else {
85+
tm->tm_year += 100;
86+
}
87+
#endif
7688
tm->tm_wday = atm->ui32Weekday;
7789
tm->tm_mon = atm->ui32Month - 1;
7890
tm->tm_mday = atm->ui32DayOfMonth;
@@ -180,8 +192,6 @@ static int ambiq_rtc_alarm_get_supported_fields(const struct device *dev,
180192
static int ambiq_rtc_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
181193
struct rtc_time *timeptr)
182194
{
183-
184-
int err = 0;
185195
am_hal_rtc_time_t ambiq_time = {0};
186196
struct ambiq_rtc_data *data = dev->data;
187197

@@ -192,11 +202,11 @@ static int ambiq_rtc_alarm_get_time(const struct device *dev, uint16_t id, uint1
192202

193203
k_spinlock_key_t key = k_spin_lock(&data->lock);
194204

195-
err = am_hal_rtc_alarm_get(&ambiq_time, NULL);
196-
if (err != 0) {
197-
LOG_DBG("Invalid Input Value");
198-
return -EINVAL;
199-
}
205+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
206+
am_hal_rtc_alarm_get(&ambiq_time);
207+
#else
208+
am_hal_rtc_alarm_get(&ambiq_time, NULL);
209+
#endif
200210

201211
ambiq_time_to_rtc_time_set(&ambiq_time, timeptr);
202212

@@ -208,13 +218,12 @@ static int ambiq_rtc_alarm_get_time(const struct device *dev, uint16_t id, uint1
208218

209219
k_spin_unlock(&data->lock, key);
210220

211-
return err;
221+
return 0;
212222
}
213223

214224
static int ambiq_rtc_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
215225
const struct rtc_time *timeptr)
216226
{
217-
int err = 0;
218227
struct ambiq_rtc_data *data = dev->data;
219228
am_hal_rtc_time_t ambiq_time = {0};
220229
uint16_t mask_available;
@@ -240,8 +249,13 @@ static int ambiq_rtc_alarm_set_time(const struct device *dev, uint16_t id, uint1
240249
k_spinlock_key_t key = k_spin_lock(&data->lock);
241250

242251
/* Disable and clear the alarm */
252+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
253+
am_hal_rtc_int_disable(AM_HAL_RTC_INT_ALM);
254+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
255+
#else
243256
am_hal_rtc_interrupt_disable(AM_HAL_RTC_INT_ALM);
244257
am_hal_rtc_interrupt_clear(AM_HAL_RTC_INT_ALM);
258+
#endif
245259

246260
/* When mask is 0 */
247261
if (mask == 0) {
@@ -258,18 +272,18 @@ static int ambiq_rtc_alarm_set_time(const struct device *dev, uint16_t id, uint1
258272
rtc_time_to_ambiq_time_set(timeptr, &ambiq_time);
259273

260274
/* Set RTC ALARM, Ambiq must have interval != AM_HAL_RTC_ALM_RPT_DIS */
261-
if (0 != am_hal_rtc_alarm_set(&ambiq_time, AM_HAL_RTC_ALM_RPT_YR)) {
262-
LOG_DBG("Invalid Input Value");
263-
err = -EINVAL;
264-
goto unlock;
265-
}
275+
am_hal_rtc_alarm_set(&ambiq_time, AM_HAL_RTC_ALM_RPT_YR);
266276

277+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
278+
am_hal_rtc_int_enable(AM_HAL_RTC_INT_ALM);
279+
#else
267280
am_hal_rtc_interrupt_enable(AM_HAL_RTC_INT_ALM);
281+
#endif
268282

269283
unlock:
270284
k_spin_unlock(&data->lock, key);
271285

272-
return err;
286+
return 0;
273287
}
274288

275289
static int ambiq_rtc_alarm_is_pending(const struct device *dev, uint16_t id)
@@ -292,7 +306,11 @@ static int ambiq_rtc_alarm_is_pending(const struct device *dev, uint16_t id)
292306
static void ambiq_rtc_isr(const struct device *dev)
293307
{
294308
/* Clear the RTC alarm interrupt. 8*/
309+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
310+
am_hal_rtc_int_clear(AM_HAL_RTC_INT_ALM);
311+
#else
295312
am_hal_rtc_interrupt_clear(AM_HAL_RTC_INT_ALM);
313+
#endif
296314

297315
#if defined(CONFIG_RTC_ALARM)
298316
struct ambiq_rtc_data *data = dev->data;
@@ -321,7 +339,11 @@ static int ambiq_rtc_alarm_set_callback(const struct device *dev, uint16_t id,
321339
data->alarm_user_callback = callback;
322340
data->alarm_user_data = user_data;
323341
if ((callback == NULL) && (user_data == NULL)) {
342+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
343+
am_hal_rtc_int_disable(AM_HAL_RTC_INT_ALM);
344+
#else
324345
am_hal_rtc_interrupt_disable(AM_HAL_RTC_INT_ALM);
346+
#endif
325347
}
326348
}
327349

@@ -332,14 +354,16 @@ static int ambiq_rtc_alarm_set_callback(const struct device *dev, uint16_t id,
332354
static int ambiq_rtc_init(const struct device *dev)
333355
{
334356
const struct ambiq_rtc_config *config = dev->config;
335-
#
357+
336358
#ifdef CONFIG_RTC_ALARM
337359
struct ambiq_rtc_data *data = dev->data;
338360
#endif
339361

340-
/* Enable the clock for RTC. */
341-
am_hal_clkgen_control(config->clk_src, NULL);
342-
362+
/* Enable the clock for RTC. */
363+
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
364+
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_XTAL_START + config->clk_src, NULL);
365+
#endif
366+
am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_RTC_SEL_XTAL + config->clk_src, NULL);
343367
/* Enable the RTC. */
344368
am_hal_rtc_osc_enable();
345369

dts/arm/ambiq/ambiq_apollo3_blue.dtsi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,14 @@
349349
ambiq,pwrcfg = <&pwrcfg 0x8 0x800>;
350350
};
351351

352+
rtc0: rtc@40004240 {
353+
compatible = "ambiq,rtc";
354+
reg = <0x40004240 0xD0>;
355+
interrupts = <2 0>;
356+
alarms-count = <1>;
357+
status = "disabled";
358+
};
359+
352360
bleif: spi@5000c000 {
353361
compatible = "ambiq,spi-bleif";
354362
reg = <0x5000c000 0x414>;

dts/arm/ambiq/ambiq_apollo3p_blue.dtsi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@
390390
ambiq,pwrcfg = <&pwrcfg 0x8 0x2000>;
391391
};
392392

393+
rtc0: rtc@40004240 {
394+
compatible = "ambiq,rtc";
395+
reg = <0x40004240 0xD0>;
396+
interrupts = <2 0>;
397+
alarms-count = <1>;
398+
status = "disabled";
399+
};
400+
393401
bleif: spi@5000c000 {
394402
compatible = "ambiq,spi-bleif";
395403
reg = <0x5000c000 0x414>;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2024, Ambiq Micro Inc. <www.ambiq.com>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_RTC_ALARM=y
5+
CONFIG_TEST_RTC_ALARM_TIME_MASK=79

0 commit comments

Comments
 (0)