Skip to content

Commit 05b26e9

Browse files
committed
Repair RTC for preload
1 parent b8a2e6a commit 05b26e9

4 files changed

Lines changed: 41 additions & 16 deletions

File tree

src/time/engine_rtc.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
#if defined(__arm__)
77
#include "../lib/bm8563/bm8563.h"
8+
#include "pico/stdlib.h"
89
#include "hardware/gpio.h"
910
#include "hardware/i2c.h"
11+
#include "pico/time.h"
1012

1113
bm8563_t bm;
1214

@@ -15,13 +17,18 @@
1517

1618
// Custom I2C reader/writer functions for bm8563 library to call
1719
int32_t custom_i2c_read(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size){
18-
if(i2c_write_blocking(i2c0, address, &reg, 1, true) == PICO_ERROR_GENERIC){
20+
// Write register we want to read from
21+
int ret = i2c_write_timeout_us(i2c0, address, &reg, 1, true, 500000);
22+
if(ret == PICO_ERROR_GENERIC || ret == PICO_ERROR_TIMEOUT){
1923
return BM8563_ERROR_NOTTY;
2024
}
2125

22-
if(i2c_read_blocking(i2c0, address, buffer, size, false) == PICO_ERROR_GENERIC){
26+
// Read what was returned by the write
27+
ret = i2c_read_timeout_us(i2c0, address, buffer, size, false, 500000);
28+
if(ret == PICO_ERROR_GENERIC || ret == PICO_ERROR_TIMEOUT){
2329
return BM8563_ERROR_NOTTY;
2430
}
31+
2532
return BM8563_OK;
2633
}
2734

@@ -30,9 +37,12 @@
3037
data[0] = reg;
3138
memcpy(data+1, buffer, size);
3239

33-
if(i2c_write_blocking(i2c0, address, data, size+1, false) == PICO_ERROR_GENERIC){
40+
// Write the data
41+
int ret = i2c_write_timeout_us(i2c0, address, data, size+1, false, 500000);
42+
if(ret == PICO_ERROR_GENERIC || ret == PICO_ERROR_TIMEOUT){
3443
return BM8563_ERROR_NOTTY;
3544
}
45+
3646
return BM8563_OK;
3747
}
3848
#endif
@@ -41,10 +51,10 @@
4151
void engine_rtc_init(){
4252
#if defined(__arm__)
4353
// Setup I2C instance and pins: https://github.com/micropython/micropython/blob/ba98533454eef5ab5783039f9929351c8f54d005/ports/rp2/machine_i2c.c#L136-L146
44-
// https://www.lcsc.com/datasheet/lcsc_datasheet_2308181040_GATEMODE-BM8563EMA_C269878.pdf <- actual part
54+
// https://www.lcsc.com/datasheet/lcsc_datasheet_2308181040_GATEMODE-BM8563EMA_C269878.pdf <- actual part
4555
// https://static.chipdip.ru/lib/031/DOC043031000.pdf
4656
// https://www.nxp.com/docs/en/data-sheet/PCF8563.pdf
47-
i2c_init(i2c0, 400000);
57+
i2c_init(i2c0, 100000); // Reduced from 400kHz to 100kHz because of I2C errors and freezing due to pull up resistor values
4858
gpio_set_function(RTC_I2C_SCL_GPIO, GPIO_FUNC_I2C);
4959
gpio_set_function(RTC_I2C_SDA_GPIO, GPIO_FUNC_I2C);
5060
gpio_set_pulls(RTC_I2C_SCL_GPIO, true, 0);
@@ -59,10 +69,18 @@ void engine_rtc_init(){
5969
}
6070

6171

62-
bool engine_rtc_check_compromised(){
72+
int engine_rtc_check_compromised(){
6373
#if defined(__arm__)
6474
struct tm rtc;
65-
if(bm8563_read(&bm, &rtc) == BM8563_ERR_LOW_VOLTAGE) return true;
75+
76+
bm8563_err_t status = bm8563_read(&bm, &rtc);
77+
78+
if(status == BM8563_ERR_LOW_VOLTAGE){
79+
return true;
80+
}else if(status == BM8563_ERROR_NOTTY){
81+
return ENGINE_RTC_I2C_ERROR;
82+
}
83+
6684
#endif
6785

6886
return false;
@@ -101,4 +119,4 @@ int engine_rtc_set_datetime(struct tm *rtc){
101119
#endif
102120

103121
return ENGINE_RTC_OK;
104-
}
122+
}

src/time/engine_rtc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ void engine_rtc_init();
1414

1515
// Checks if the RTC may have gone below voltage and lost time.
1616
// Returns true of compromised
17-
bool engine_rtc_check_compromised();
17+
int engine_rtc_check_compromised();
1818

1919
// Get the broken down time
2020
int engine_rtc_get_datetime(struct tm *rtc);
2121

2222
// Set the broken down time
2323
int engine_rtc_set_datetime(struct tm *rtc);
2424

25-
#endif // ENGINE_RTC_H
25+
#endif // ENGINE_RTC_H

src/time/engine_time_module.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ int engine_time_set(mp_obj_t input_tuple){
6767
/* --- doc ---
6868
NAME: datetime
6969
ID: datetime
70-
DESC: Returns a datetime tuple representing the current date down to the second (based on the RTC clock that may lose time if it/Thumby Color fully loses power)
70+
DESC: Returns a datetime tuple representing the current date down to the second (based on the RTC clock that may lose time if it/Thumby Color fully loses power)
7171
PARAM: [type=tuple] [name=time] [value=tuple of ints in format (year, month, mday, hour, min, sec, wday{optional & unused}, yday{optional & unused}) (optional)]
7272
RETURN: RTC_OK, RTC_I2C_ERROR or tuple (year, month, mday, hour, min, sec, wday, yday)
7373
*/
74-
static mp_obj_t engine_time_datetime(mp_uint_t n_args, const mp_obj_t *args){
74+
static mp_obj_t engine_time_datetime(size_t n_args, const mp_obj_t *args){
7575
int status = 0;
7676

7777
if(n_args == 0){
@@ -96,10 +96,17 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(engine_time_datetime_obj, 0, 1, engine_time_
9696
NAME: is_compromised
9797
ID: is_compromised
9898
DESC: Returns True if the RTC clock detected a low voltage that meant it lost power or was very close
99-
RETURN: True or False
99+
RETURN: True, False, or RTC_I2C_ERROR
100100
*/
101-
static mp_obj_t engine_time_is_compromised(mp_uint_t n_args, const mp_obj_t *args){
102-
return mp_obj_new_bool(engine_rtc_check_compromised());
101+
static mp_obj_t engine_time_is_compromised(size_t n_args, const mp_obj_t *args){
102+
int status = engine_rtc_check_compromised();
103+
104+
if(status == true || status == false){
105+
mp_obj_new_bool(status);
106+
}
107+
108+
// RTC_I2C_ERROR at this point
109+
return mp_obj_new_int(status);
103110
}
104111
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(engine_time_is_compromised_obj, 0, 1, engine_time_is_compromised);
105112

src/time/engine_time_module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44

55

6-
#endif // ENGINE_TIME_MODULE_H
6+
#endif // ENGINE_TIME_MODULE_H

0 commit comments

Comments
 (0)