Skip to content

Commit 35f13db

Browse files
committed
Fix RTC and SPI issue
- Update objrtc.c to solve issue of not able to set the time user have key in - Update objspi.c to solve issue of not able to read and write the correct data
1 parent 992fe42 commit 35f13db

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

ports/ameba/mp_helper/mods/machine/objrtc.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "objrtc.h"
28+
#include "rtl8721d_rtc.h"
2829

2930
// singleton RTC object
3031
STATIC rtc_obj_t rtc_obj = {
@@ -49,13 +50,27 @@ STATIC mp_obj_t rtc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uin
4950
}
5051

5152
STATIC mp_obj_t rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
53+
static uint32_t seconds_ymd_backup = 0;
54+
RTC_TimeTypeDef RTC_TimeStruct;
55+
5256
if (n_args == 1) {
53-
// Get time
54-
clock_t secs = rtc_read();
55-
// (Since RTL8195A use 1970 for it's start yaer, so I need to add the seconds of 30 years)
56-
secs -= SECS_IN_30YEARS;
57+
// RTL872x RTC can only count up to 512 days
58+
// Get updated time and reset RTC day counter to 0
59+
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
60+
int days = RTC_TimeStruct.RTC_Days;
61+
RTC_TimeStruct.RTC_Days = 0;
62+
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);
63+
int hours = RTC_TimeStruct.RTC_Hours;
64+
int mins = RTC_TimeStruct.RTC_Minutes;
65+
int secs = RTC_TimeStruct.RTC_Seconds;
66+
67+
clock_t seconds_elapsed = days * NUM_SECS_IN_DAY;
68+
clock_t total_seconds = seconds_elapsed + seconds_ymd_backup + hours * 3600 + mins * 60 + secs;
5769
timeutils_struct_time_t tm;
58-
timeutils_seconds_since_2000_to_struct_time(secs, &tm);
70+
timeutils_seconds_since_2000_to_struct_time(total_seconds, &tm);
71+
72+
// Update backup copy of year month date in seconds
73+
seconds_ymd_backup += seconds_elapsed;
5974

6075
mp_obj_t tuple[8] = {
6176
mp_obj_new_int(tm.tm_year),
@@ -65,7 +80,7 @@ STATIC mp_obj_t rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
6580
mp_obj_new_int(tm.tm_hour),
6681
mp_obj_new_int(tm.tm_min),
6782
mp_obj_new_int(tm.tm_sec),
68-
mp_obj_new_int(secs)
83+
mp_obj_new_int(total_seconds)
6984
};
7085

7186
return mp_obj_new_tuple(8, tuple);
@@ -74,19 +89,27 @@ STATIC mp_obj_t rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
7489
mp_obj_t *items;
7590
mp_obj_get_array_fixed_n(args[1], 8, &items);
7691

77-
rtc_write(
78-
((clock_t)timeutils_seconds_since_2000(
79-
mp_obj_get_int(items[0]),
80-
mp_obj_get_int(items[1]),
81-
mp_obj_get_int(items[2]),
82-
mp_obj_get_int(items[4]),
83-
mp_obj_get_int(items[5]),
84-
mp_obj_get_int(items[6]) + SECS_IN_30YEARS
85-
)));
92+
uint32_t year = mp_obj_get_int(items[0]);
93+
uint32_t month = mp_obj_get_int(items[1]);
94+
uint32_t day = mp_obj_get_int(items[2]);
95+
uint32_t hour = mp_obj_get_int(items[4]);
96+
uint32_t mins = mp_obj_get_int(items[5]);
97+
uint32_t secs = mp_obj_get_int(items[6]);
98+
99+
seconds_ymd_backup = ((clock_t)timeutils_seconds_since_2000(year, month, day, 0, 0, 0));
100+
101+
// RTL872x RTC can only count up to 512 days, so use it to only count day offsets
102+
RTC_TimeStruct.RTC_H12_PMAM = RTC_H12_AM;
103+
RTC_TimeStruct.RTC_Days = 0;
104+
RTC_TimeStruct.RTC_Hours = hour;
105+
RTC_TimeStruct.RTC_Minutes = mins;
106+
RTC_TimeStruct.RTC_Seconds = secs;
107+
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);
86108

87109
return mp_const_none;
88110
}
89111
}
112+
90113
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rtc_datetime_obj, 1, 2, rtc_datetime);
91114

92115
STATIC const mp_map_elem_t rtc_locals_dict_table[] = {

ports/ameba/mp_helper/mods/machine/objrtc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
extern const mp_obj_type_t rtc_type;
3535

3636
#define SECS_IN_30YEARS (365*30+7)*24*60*60
37+
#define NUM_SECS_IN_DAY 86400
3738

3839
typedef struct {
3940
mp_obj_base_t base;

ports/ameba/mp_helper/mods/machine/objspi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
197197
spi_obj_t *self = (spi_obj_t *)self_in;
198198

199199
// Check if there is no destination buffer given
200-
bool write_only = dest == NULL;
200+
bool write_only = (dest == NULL);
201201

202202
if (write_only) {
203203
if (self->mode == SPI_MASTER) {
@@ -207,6 +207,7 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
207207
}
208208
} else {
209209
// reading and writing large trunk of data at the same time
210+
spi_flush_rx_fifo(&mp_spi_obj[id]);
210211
spi_master_write_read_stream(&mp_spi_obj[id], src, dest, len);
211212
}
212213
}

0 commit comments

Comments
 (0)