← index #5545Issue #1327
Related · high · value 3.398
QUERY · ISSUE

machine.RTC.init throws exception on Pyboard 1.0

openby peterhinchopened 2020-01-16updated 2021-02-07
docsport-stm32

The code example in the docs fails with a surprising traceback.

rtc = machine.RTC()
rtc.init((2014, 5, 1, 4, 13, 0, 0, 0))

Outcome:

MicroPython v1.12-61-g0f16eeab2 on 2020-01-16; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> 
>>> from machine import RTC
>>> rtc = RTC()
>>> rtc.init((2014, 5, 1, 4, 13, 0, 0, 0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes 1 positional arguments but 2 were given
CANDIDATE · ISSUE

stmhal: rtc.datetime() throws an exception if called without arguments

closedby owens-billopened 2015-06-14updated 2015-06-15

The documentation for pyb.RTC on the pyboard says that without any arguments the method will return the current time as an 8-tuple; with one argument (an 8-tuple) the RTC will be set. The comments in the code say the same:

/// With no arguments, this method returns an 8-tuple with the current
/// date and time.  With 1 argument (being an 8-tuple) it sets the date
/// and time.

However, the code disagrees:

mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
    if (n_args == 1) {
        // get date and time
        // note: need to call get time then get date to correctly access the registers

and:

MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime);

The behavior is consistent with the code (naturally):

Micro Python v1.4.3-159-g3ce212e on 2015-06-14; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> import pyb
>>> rtc = pyb.RTC
>>> dir(rtc)
['info', 'datetime', 'wakeup', 'calibration']
>>> rtc.datetime()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function missing 1 required positional arguments
>>> rtc.datetime('foo')
(2014, 1, 1, 3, 0, 37, 49, 203)
>>> rtc.datetime((2015, 6, 14, 7, 0, 0, 0, 0))
(2014, 1, 1, 3, 0, 39, 11, 86)
>>> rtc.datetime('foo',(2015, 6, 14, 7, 0, 0, 0, 0))
>>> rtc.datetime('foo')
(2015, 6, 14, 7, 0, 0, 5, 101)
>>> rtc.datetime()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function missing 1 required positional arguments

Am I reading this wrong? I made a simple change (three lines) to rtc.c and produced the behavior as documented, but I don't understand why it would have been different in the first place...

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied