{"action":"created","comment":{"url":"https://api.github.com/repos/micropython/micropython/pulls/comments/65386978","id":65386978,"diff_hunk":"@@ -81,28 +84,40 @@ STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp\n     return (mp_obj_t)&pyb_rtc_obj;\n }\n \n-STATIC uint64_t pyb_rtc_raw_us(uint64_t cal) {\n-    return (system_get_rtc_time() * cal) >> 12;\n-};\n-\n void pyb_rtc_set_us_since_2000(uint64_t nowus) {\n     uint32_t cal = system_rtc_clock_cali_proc();\n-    int64_t delta = nowus - pyb_rtc_raw_us(cal);\n+    uint32_t rtc_ticks = system_get_rtc_time();\n+    int64_t delta = nowus - (((uint64_t)rtc_ticks * cal) >> 12);\n \n     // As the calibration value jitters quite a bit, to make the\n     // clock at least somewhat practially usable, we need to store it\n     system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal));\n     system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta));\n+    // Save RTC ticks for overflow detection.\n+    system_rtc_mem_write(MEM_OF_CHECK_ADDR, &rtc_ticks, sizeof(rtc_ticks));\n };\n \n uint64_t pyb_rtc_get_us_since_2000() {\n     uint32_t cal;\n     int64_t delta;\n+    uint32_t rtc_ticks, rtc_last;\n \n     system_rtc_mem_read(MEM_CAL_ADDR, &cal, sizeof(cal));\n     system_rtc_mem_read(MEM_DELTA_ADDR, &delta, sizeof(delta));\n \n-    return pyb_rtc_raw_us(cal) + delta;\n+    // ESP-SDK system_get_rtc_time() only returns uint32 and therefore\n+    // overflow about every 7:45h.  Thus, we have to check for\n+    // overflow and handle it.\n+    rtc_ticks = system_get_rtc_time();\n+    system_rtc_mem_read(MEM_OF_CHECK_ADDR, &rtc_last, sizeof(rtc_last));\n+    system_rtc_mem_write(MEM_OF_CHECK_ADDR, &rtc_ticks, sizeof(rtc_ticks));\n+    if (rtc_ticks < rtc_last) {\n+        // Adjust delta because of RTC overflow.\n+        delta += ((uint64_t)1<<20) * cal;","path":"esp8266/modpybrtc.c","position":58,"original_position":58,"commit_id":"0ebaa4945447a17e4c9c6f555073e92f775d5079","original_commit_id":"0ebaa4945447a17e4c9c6f555073e92f775d5079","user":{"login":"dpgeorge","id":6187689,"avatar_url":"https://avatars.githubusercontent.com/u/6187689?v=3","gravatar_id":"","url":"https://api.github.com/users/dpgeorge","html_url":"https://github.com/dpgeorge","followers_url":"https://api.github.com/users/dpgeorge/followers","following_url":"https://api.github.com/users/dpgeorge/following{/other_user}","gists_url":"https://api.github.com/users/dpgeorge/gists{/gist_id}","starred_url":"https://api.github.com/users/dpgeorge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpgeorge/subscriptions","organizations_url":"https://api.github.com/users/dpgeorge/orgs","repos_url":"https://api.github.com/users/dpgeorge/repos","events_url":"https://api.github.com/users/dpgeorge/events{/privacy}","received_events_url":"https://api.github.com/users/dpgeorge/received_events","type":"User","site_admin":false},"body":"This can be written simply as `(uint64_t)cal << 20`.","created_at":"2016-06-01T15:44:01Z","updated_at":"2016-06-01T15:44:01Z","html_url":"https://github.com/micropython/micropython/pull/2107#discussion_r65386978","pull_request_url":"https://api.github.com/repos/micropython/micropython/pulls/2107","_links":{"self":{"href":"https://api.github.com/repos/micropython/micropython/pulls/comments/65386978"},"html":{"href":"https://github.com/micropython/micropython/pull/2107#discussion_r65386978"},"pull_request":{"href":"https://api.github.com/repos/micropython/micropython/pulls/2107"}}},"pull_request":{"url":"https://api.github.com/repos/micropython/micropython/pulls/2107","id":71223504,"html_url":"https://github.com/micropython/micropython/pull/2107","diff_url":"https://github.com/micropython/micropython/pull/2107.diff","patch_url":"https://github.com/micropython/micropython/pull/2107.patch","issue_url":"https://api.github.com/repos/micropython/micropython/issues/2107","number":2107,"state":"open","locked":false,"title":"esp8266: handle rtc overflow","user":{"login":"puuu","id":19514939,"avatar_url":"https://avatars.githubusercontent.com/u/19514939?v=3","gravatar_id":"","url":"https://api.github.com/users/puuu","html_url":"https://github.com/puuu","followers_url":"https://api.github.com/users/puuu/followers","following_url":"https://api.github.com/users/puuu/following{/other_user}","gists_url":"https://api.github.com/users/puuu/gists{/gist_id}","starred_url":"https://api.github.com/users/puuu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/puuu/subscriptions","organizations_url":"https://api.github.com/users/puuu/orgs","repos_url":"https://api.github.com/users/puuu/repos","events_url":"https://api.github.com/users/puuu/events{/privacy}","received_events_url":"https://api.github.com/users/puuu/received_events","type":"User","site_admin":false},"body":"This pull request should address #2097. Tested over 20 hours without problems.\r\n\r\nPlease reviewing. This fix requires, that `pyb_rtc_raw_us()` is called at least once within 7.45h. E.g. by calling `time.localtime()`, `machine.RTC().datetime()` or `machine_deepsleep()`. I believe, this is a typical use case, but maybe it is better to break out an additional function and hook it in some timer.\r\nAny suggestions are welcome.","created_at":"2016-05-24T13:50:49Z","updated_at":"2016-06-01T15:44:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fac51639e0cc89e2d7cf32483c8037231da707bf","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/micropython/micropython/pulls/2107/commits","review_comments_url":"https://api.github.com/repos/micropython/micropython/pulls/2107/comments","review_comment_url":"https://api.github.com/repos/micropython/micropython/pulls/comments{/number}","comments_url":"https://api.github.com/repos/micropython/micropython/issues/2107/comments","statuses_url":"https://api.github.com/repos/micropython/micropython/statuses/0ebaa4945447a17e4c9c6f555073e92f775d5079","head":{"label":"puuu:rtc-overflow","ref":"rtc-overflow","sha":"0ebaa4945447a17e4c9c6f555073e92f775d5079","user":{"login":"puuu","id":19514939,"avatar_url":"https://avatars.githubusercontent.com/u/19514939?v=3","gravatar_id":"","url":"https://api.github.com/users/puuu","html_url":"https://github.com/puuu","followers_url":"https://api.github.com/users/puuu/followers","following_url":"https://api.github.com/users/puuu/following{/other_user}","gists_url":"https://api.github.com/users/puuu/gists{/gist_id}","starred_url":"https://api.github.com/users/puuu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/puuu/subscriptions","organizations_url":"https://api.github.com/users/puuu/orgs","repos_url":"https://api.github.com/users/puuu/repos","events_url":"https://api.github.com/users/puuu/events{/privacy}","received_events_url":"https://api.github.com/users/puuu/received_events","type":"User","site_admin":false},"repo":{"id":59574971,"name":"micropython","full_name":"puuu/micropython","owner":{"login":"puuu","id":19514939,"avatar_url":"https://avatars.githubusercontent.com/u/19514939?v=3","gravatar_id":"","url":"https://api.github.com/users/puuu","html_url":"https://github.com/puuu","followers_url":"https://api.github.com/users/puuu/followers","following_url":"https://api.github.com/users/puuu/following{/other_user}","gists_url":"https://api.github.com/users/puuu/gists{/gist_id}","starred_url":"https://api.github.com/users/puuu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/puuu/subscriptions","organizations_url":"https://api.github.com/users/puuu/orgs","repos_url":"https://api.github.com/users/puuu/repos","events_url":"https://api.github.com/users/puuu/events{/privacy}","received_events_url":"https://api.github.com/users/puuu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/puuu/micropython","description":"MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems","fork":true,"url":"https://api.github.com/repos/puuu/micropython","forks_url":"https://api.github.com/repos/puuu/micropython/forks","keys_url":"https://api.github.com/repos/puuu/micropython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/puuu/micropython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/puuu/micropython/teams","hooks_url":"https://api.github.com/repos/puuu/micropython/hooks","issue_events_url":"https://api.github.com/repos/puuu/micropython/issues/events{/number}","events_url":"https://api.github.com/repos/puuu/micropython/events","assignees_url":"https://api.github.com/repos/puuu/micropython/assignees{/user}","branches_url":"https://api.github.com/repos/puuu/micropython/branches{/branch}","tags_url":"https://api.github.com/repos/puuu/micropython/tags","blobs_url":"https://api.github.com/repos/puuu/micropython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/puuu/micropython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/puuu/micropython/git/refs{/sha}","trees_url":"https://api.github.com/repos/puuu/micropython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/puuu/micropython/statuses/{sha}","languages_url":"https://api.github.com/repos/puuu/micropython/languages","stargazers_url":"https://api.github.com/repos/puuu/micropython/stargazers","contributors_url":"https://api.github.com/repos/puuu/micropython/contributors","subscribers_url":"https://api.github.com/repos/puuu/micropython/subscribers","subscription_url":"https://api.github.com/repos/puuu/micropython/subscription","commits_url":"https://api.github.com/repos/puuu/micropython/commits{/sha}","git_commits_url":"https://api.github.com/repos/puuu/micropython/git/commits{/sha}","comments_url":"https://api.github.com/repos/puuu/micropython/comments{/number}","issue_comment_url":"https://api.github.com/repos/puuu/micropython/issues/comments{/number}","contents_url":"https://api.github.com/repos/puuu/micropython/contents/{+path}","compare_url":"https://api.github.com/repos/puuu/micropython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/puuu/micropython/merges","archive_url":"https://api.github.com/repos/puuu/micropython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/puuu/micropython/downloads","issues_url":"https://api.github.com/repos/puuu/micropython/issues{/number}","pulls_url":"https://api.github.com/repos/puuu/micropython/pulls{/number}","milestones_url":"https://api.github.com/repos/puuu/micropython/milestones{/number}","notifications_url":"https://api.github.com/repos/puuu/micropython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/puuu/micropython/labels{/name}","releases_url":"https://api.github.com/repos/puuu/micropython/releases{/id}","deployments_url":"https://api.github.com/repos/puuu/micropython/deployments","created_at":"2016-05-24T13:21:29Z","updated_at":"2016-05-24T13:21:42Z","pushed_at":"2016-06-01T14:05:28Z","git_url":"git://github.com/puuu/micropython.git","ssh_url":"git@github.com:puuu/micropython.git","clone_url":"https://github.com/puuu/micropython.git","svn_url":"https://github.com/puuu/micropython","homepage":"","size":26051,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"micropython:master","ref":"master","sha":"752e952096815adc2e418967e4b056680924a584","user":{"login":"micropython","id":6298560,"avatar_url":"https://avatars.githubusercontent.com/u/6298560?v=3","gravatar_id":"","url":"https://api.github.com/users/micropython","html_url":"https://github.com/micropython","followers_url":"https://api.github.com/users/micropython/followers","following_url":"https://api.github.com/users/micropython/following{/other_user}","gists_url":"https://api.github.com/users/micropython/gists{/gist_id}","starred_url":"https://api.github.com/users/micropython/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/micropython/subscriptions","organizations_url":"https://api.github.com/users/micropython/orgs","repos_url":"https://api.github.com/users/micropython/repos","events_url":"https://api.github.com/users/micropython/events{/privacy}","received_events_url":"https://api.github.com/users/micropython/received_events","type":"Organization","site_admin":false},"repo":{"id":15337142,"name":"micropython","full_name":"micropython/micropython","owner":{"login":"micropython","id":6298560,"avatar_url":"https://avatars.githubusercontent.com/u/6298560?v=3","gravatar_id":"","url":"https://api.github.com/users/micropython","html_url":"https://github.com/micropython","followers_url":"https://api.github.com/users/micropython/followers","following_url":"https://api.github.com/users/micropython/following{/other_user}","gists_url":"https://api.github.com/users/micropython/gists{/gist_id}","starred_url":"https://api.github.com/users/micropython/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/micropython/subscriptions","organizations_url":"https://api.github.com/users/micropython/orgs","repos_url":"https://api.github.com/users/micropython/repos","events_url":"https://api.github.com/users/micropython/events{/privacy}","received_events_url":"https://api.github.com/users/micropython/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/micropython/micropython","description":"MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems","fork":false,"url":"https://api.github.com/repos/micropython/micropython","forks_url":"https://api.github.com/repos/micropython/micropython/forks","keys_url":"https://api.github.com/repos/micropython/micropython/keys{/key_id}","collaborators_url":"https://api.github.com/repos/micropython/micropython/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/micropython/micropython/teams","hooks_url":"https://api.github.com/repos/micropython/micropython/hooks","issue_events_url":"https://api.github.com/repos/micropython/micropython/issues/events{/number}","events_url":"https://api.github.com/repos/micropython/micropython/events","assignees_url":"https://api.github.com/repos/micropython/micropython/assignees{/user}","branches_url":"https://api.github.com/repos/micropython/micropython/branches{/branch}","tags_url":"https://api.github.com/repos/micropython/micropython/tags","blobs_url":"https://api.github.com/repos/micropython/micropython/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/micropython/micropython/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/micropython/micropython/git/refs{/sha}","trees_url":"https://api.github.com/repos/micropython/micropython/git/trees{/sha}","statuses_url":"https://api.github.com/repos/micropython/micropython/statuses/{sha}","languages_url":"https://api.github.com/repos/micropython/micropython/languages","stargazers_url":"https://api.github.com/repos/micropython/micropython/stargazers","contributors_url":"https://api.github.com/repos/micropython/micropython/contributors","subscribers_url":"https://api.github.com/repos/micropython/micropython/subscribers","subscription_url":"https://api.github.com/repos/micropython/micropython/subscription","commits_url":"https://api.github.com/repos/micropython/micropython/commits{/sha}","git_commits_url":"https://api.github.com/repos/micropython/micropython/git/commits{/sha}","comments_url":"https://api.github.com/repos/micropython/micropython/comments{/number}","issue_comment_url":"https://api.github.com/repos/micropython/micropython/issues/comments{/number}","contents_url":"https://api.github.com/repos/micropython/micropython/contents/{+path}","compare_url":"https://api.github.com/repos/micropython/micropython/compare/{base}...{head}","merges_url":"https://api.github.com/repos/micropython/micropython/merges","archive_url":"https://api.github.com/repos/micropython/micropython/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/micropython/micropython/downloads","issues_url":"https://api.github.com/repos/micropython/micropython/issues{/number}","pulls_url":"https://api.github.com/repos/micropython/micropython/pulls{/number}","milestones_url":"https://api.github.com/repos/micropython/micropython/milestones{/number}","notifications_url":"https://api.github.com/repos/micropython/micropython/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/micropython/micropython/labels{/name}","releases_url":"https://api.github.com/repos/micropython/micropython/releases{/id}","deployments_url":"https://api.github.com/repos/micropython/micropython/deployments","created_at":"2013-12-20T11:47:07Z","updated_at":"2016-06-01T13:54:02Z","pushed_at":"2016-06-01T13:30:00Z","git_url":"git://github.com/micropython/micropython.git","ssh_url":"git@github.com:micropython/micropython.git","clone_url":"https://github.com/micropython/micropython.git","svn_url":"https://github.com/micropython/micropython","homepage":"","size":64164,"stargazers_count":3250,"watchers_count":3250,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":656,"mirror_url":null,"open_issues_count":246,"forks":656,"open_issues":246,"watchers":3250,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/micropython/micropython/pulls/2107"},"html":{"href":"https://github.com/micropython/micropython/pull/2107"},"issue":{"href":"https://api.github.com/repos/micropython/micropython/issues/2107"},"comments":{"href":"https://api.github.com/repos/micropython/micropython/issues/2107/comments"},"review_comments":{"href":"https://api.github.com/repos/micropython/micropython/pulls/2107/comments"},"review_comment":{"href":"https://api.github.com/repos/micropython/micropython/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/micropython/micropython/pulls/2107/commits"},"statuses":{"href":"https://api.github.com/repos/micropython/micropython/statuses/0ebaa4945447a17e4c9c6f555073e92f775d5079"}}}}
{"actor":{"display_login":"dpgeorge"}}