{"action":"created","comment":{"url":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/comments/33999562","id":33999562,"diff_hunk":"@@ -34,17 +42,37 @@ void rng_setup(void)\n \n u32 random_int(void)\n {\n+  chMtxLock(&rng_mutex);\n+\n   static u32 last_value;\n-  static u32 new_value;\n-  u32 error_bits = 0;\n-  error_bits = RNG_SR_SEIS | RNG_SR_CEIS;\n-  while (new_value == last_value) {\n-    /* Check for error flags and if data is ready. */\n-    if (((RNG_SR & error_bits) == 0) &&\n-        ((RNG_SR & RNG_SR_DRDY) == 1)) {\n-      new_value = RNG_DR;\n+  u32 new_value = last_value;\n+  int tries = 0;\n+  do {\n+    u32 sr;\n+    /* Check for error flags. */\n+    if ((sr = RNG_SR)) {\n+      if (sr & RNG_SR_SEIS) {\n+        /* Seed error, we must reinitialise */\n+        log_warn(\"random: Seed error, restarting RNG 0x%\"PRIx32\"\\n\", sr);\n+        RNG_CR &= ~RNG_CR_RNGEN;\n+        RNG_CR |= RNG_CR_RNGEN;\n+      }\n+      /* Write back as zero to clear */\n+      RNG_SR = ~(sr & (RNG_SR_SEIS | RNG_SR_CEIS));\n     }\n+    /* Check if data is ready. */\n+    if ((RNG_SR & RNG_SR_DRDY) == 0)\n+      continue;\n+\n+    new_value = RNG_DR;\n+  } while ((new_value == last_value) && (tries++ < RANDOM_MAX_TRIES));","path":"src/peripherals/random.c","position":53,"original_position":53,"commit_id":"74bb43fce7c5319b5991727fcac9e8320d3bf04c","original_commit_id":"74bb43fce7c5319b5991727fcac9e8320d3bf04c","user":{"login":"gsmcmullin","id":466364,"avatar_url":"https://avatars.githubusercontent.com/u/466364?v=3","gravatar_id":"","url":"https://api.github.com/users/gsmcmullin","html_url":"https://github.com/gsmcmullin","followers_url":"https://api.github.com/users/gsmcmullin/followers","following_url":"https://api.github.com/users/gsmcmullin/following{/other_user}","gists_url":"https://api.github.com/users/gsmcmullin/gists{/gist_id}","starred_url":"https://api.github.com/users/gsmcmullin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsmcmullin/subscriptions","organizations_url":"https://api.github.com/users/gsmcmullin/orgs","repos_url":"https://api.github.com/users/gsmcmullin/repos","events_url":"https://api.github.com/users/gsmcmullin/events{/privacy}","received_events_url":"https://api.github.com/users/gsmcmullin/received_events","type":"User","site_admin":false},"body":"It's supposed to be `RNG_SR_DRDY`, but the datasheet says explicitly that you should check each value is different from the previous value read.","created_at":"2015-07-07T00:56:15Z","updated_at":"2015-07-07T00:56:15Z","html_url":"https://github.com/swift-nav/piksi_firmware/pull/483#discussion_r33999562","pull_request_url":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483","_links":{"self":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/comments/33999562"},"html":{"href":"https://github.com/swift-nav/piksi_firmware/pull/483#discussion_r33999562"},"pull_request":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483"}}},"pull_request":{"url":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483","id":39324934,"html_url":"https://github.com/swift-nav/piksi_firmware/pull/483","diff_url":"https://github.com/swift-nav/piksi_firmware/pull/483.diff","patch_url":"https://github.com/swift-nav/piksi_firmware/pull/483.patch","issue_url":"https://api.github.com/repos/swift-nav/piksi_firmware/issues/483","number":483,"state":"open","locked":false,"title":"random: better checks and thread safety on RNG.","user":{"login":"gsmcmullin","id":466364,"avatar_url":"https://avatars.githubusercontent.com/u/466364?v=3","gravatar_id":"","url":"https://api.github.com/users/gsmcmullin","html_url":"https://github.com/gsmcmullin","followers_url":"https://api.github.com/users/gsmcmullin/followers","following_url":"https://api.github.com/users/gsmcmullin/following{/other_user}","gists_url":"https://api.github.com/users/gsmcmullin/gists{/gist_id}","starred_url":"https://api.github.com/users/gsmcmullin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsmcmullin/subscriptions","organizations_url":"https://api.github.com/users/gsmcmullin/orgs","repos_url":"https://api.github.com/users/gsmcmullin/repos","events_url":"https://api.github.com/users/gsmcmullin/events{/privacy}","received_events_url":"https://api.github.com/users/gsmcmullin/received_events","type":"User","site_admin":false},"body":"Check and clear error flags from RNG, restarting RNG in the case of seed error.\r\nReport seed error and too many iterations.\r\nProtect RNG access with a mutex.","created_at":"2015-07-07T00:32:39Z","updated_at":"2015-07-07T00:56:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"95ce1b7a6fd8713240435751587b341adec41c0c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483/commits","review_comments_url":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483/comments","review_comment_url":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/comments{/number}","comments_url":"https://api.github.com/repos/swift-nav/piksi_firmware/issues/483/comments","statuses_url":"https://api.github.com/repos/swift-nav/piksi_firmware/statuses/74bb43fce7c5319b5991727fcac9e8320d3bf04c","head":{"label":"gsmcmullin:rng_fixes","ref":"rng_fixes","sha":"74bb43fce7c5319b5991727fcac9e8320d3bf04c","user":{"login":"gsmcmullin","id":466364,"avatar_url":"https://avatars.githubusercontent.com/u/466364?v=3","gravatar_id":"","url":"https://api.github.com/users/gsmcmullin","html_url":"https://github.com/gsmcmullin","followers_url":"https://api.github.com/users/gsmcmullin/followers","following_url":"https://api.github.com/users/gsmcmullin/following{/other_user}","gists_url":"https://api.github.com/users/gsmcmullin/gists{/gist_id}","starred_url":"https://api.github.com/users/gsmcmullin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsmcmullin/subscriptions","organizations_url":"https://api.github.com/users/gsmcmullin/orgs","repos_url":"https://api.github.com/users/gsmcmullin/repos","events_url":"https://api.github.com/users/gsmcmullin/events{/privacy}","received_events_url":"https://api.github.com/users/gsmcmullin/received_events","type":"User","site_admin":false},"repo":{"id":16241768,"name":"piksi_firmware","full_name":"gsmcmullin/piksi_firmware","owner":{"login":"gsmcmullin","id":466364,"avatar_url":"https://avatars.githubusercontent.com/u/466364?v=3","gravatar_id":"","url":"https://api.github.com/users/gsmcmullin","html_url":"https://github.com/gsmcmullin","followers_url":"https://api.github.com/users/gsmcmullin/followers","following_url":"https://api.github.com/users/gsmcmullin/following{/other_user}","gists_url":"https://api.github.com/users/gsmcmullin/gists{/gist_id}","starred_url":"https://api.github.com/users/gsmcmullin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsmcmullin/subscriptions","organizations_url":"https://api.github.com/users/gsmcmullin/orgs","repos_url":"https://api.github.com/users/gsmcmullin/repos","events_url":"https://api.github.com/users/gsmcmullin/events{/privacy}","received_events_url":"https://api.github.com/users/gsmcmullin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/gsmcmullin/piksi_firmware","description":"Firmware for the Piksi GNSS receiver.","fork":true,"url":"https://api.github.com/repos/gsmcmullin/piksi_firmware","forks_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/forks","keys_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/teams","hooks_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/hooks","issue_events_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/issues/events{/number}","events_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/events","assignees_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/assignees{/user}","branches_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/branches{/branch}","tags_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/tags","blobs_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/git/refs{/sha}","trees_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/statuses/{sha}","languages_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/languages","stargazers_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/stargazers","contributors_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/contributors","subscribers_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/subscribers","subscription_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/subscription","commits_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/commits{/sha}","git_commits_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/git/commits{/sha}","comments_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/comments{/number}","issue_comment_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/issues/comments{/number}","contents_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/contents/{+path}","compare_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/merges","archive_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/downloads","issues_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/issues{/number}","pulls_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/pulls{/number}","milestones_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/milestones{/number}","notifications_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/labels{/name}","releases_url":"https://api.github.com/repos/gsmcmullin/piksi_firmware/releases{/id}","created_at":"2014-01-25T22:41:13Z","updated_at":"2014-06-19T20:04:55Z","pushed_at":"2015-07-07T00:28:21Z","git_url":"git://github.com/gsmcmullin/piksi_firmware.git","ssh_url":"git@github.com:gsmcmullin/piksi_firmware.git","clone_url":"https://github.com/gsmcmullin/piksi_firmware.git","svn_url":"https://github.com/gsmcmullin/piksi_firmware","homepage":"swift-nav.com","size":3756,"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":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"swift-nav:master","ref":"master","sha":"19dcd24cd7736830d2e32cb12be7d74a9de9d47b","user":{"login":"swift-nav","id":1069835,"avatar_url":"https://avatars.githubusercontent.com/u/1069835?v=3","gravatar_id":"","url":"https://api.github.com/users/swift-nav","html_url":"https://github.com/swift-nav","followers_url":"https://api.github.com/users/swift-nav/followers","following_url":"https://api.github.com/users/swift-nav/following{/other_user}","gists_url":"https://api.github.com/users/swift-nav/gists{/gist_id}","starred_url":"https://api.github.com/users/swift-nav/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swift-nav/subscriptions","organizations_url":"https://api.github.com/users/swift-nav/orgs","repos_url":"https://api.github.com/users/swift-nav/repos","events_url":"https://api.github.com/users/swift-nav/events{/privacy}","received_events_url":"https://api.github.com/users/swift-nav/received_events","type":"Organization","site_admin":false},"repo":{"id":2464156,"name":"piksi_firmware","full_name":"swift-nav/piksi_firmware","owner":{"login":"swift-nav","id":1069835,"avatar_url":"https://avatars.githubusercontent.com/u/1069835?v=3","gravatar_id":"","url":"https://api.github.com/users/swift-nav","html_url":"https://github.com/swift-nav","followers_url":"https://api.github.com/users/swift-nav/followers","following_url":"https://api.github.com/users/swift-nav/following{/other_user}","gists_url":"https://api.github.com/users/swift-nav/gists{/gist_id}","starred_url":"https://api.github.com/users/swift-nav/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swift-nav/subscriptions","organizations_url":"https://api.github.com/users/swift-nav/orgs","repos_url":"https://api.github.com/users/swift-nav/repos","events_url":"https://api.github.com/users/swift-nav/events{/privacy}","received_events_url":"https://api.github.com/users/swift-nav/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/swift-nav/piksi_firmware","description":"Firmware for the Piksi GNSS receiver.","fork":false,"url":"https://api.github.com/repos/swift-nav/piksi_firmware","forks_url":"https://api.github.com/repos/swift-nav/piksi_firmware/forks","keys_url":"https://api.github.com/repos/swift-nav/piksi_firmware/keys{/key_id}","collaborators_url":"https://api.github.com/repos/swift-nav/piksi_firmware/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/swift-nav/piksi_firmware/teams","hooks_url":"https://api.github.com/repos/swift-nav/piksi_firmware/hooks","issue_events_url":"https://api.github.com/repos/swift-nav/piksi_firmware/issues/events{/number}","events_url":"https://api.github.com/repos/swift-nav/piksi_firmware/events","assignees_url":"https://api.github.com/repos/swift-nav/piksi_firmware/assignees{/user}","branches_url":"https://api.github.com/repos/swift-nav/piksi_firmware/branches{/branch}","tags_url":"https://api.github.com/repos/swift-nav/piksi_firmware/tags","blobs_url":"https://api.github.com/repos/swift-nav/piksi_firmware/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/swift-nav/piksi_firmware/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/swift-nav/piksi_firmware/git/refs{/sha}","trees_url":"https://api.github.com/repos/swift-nav/piksi_firmware/git/trees{/sha}","statuses_url":"https://api.github.com/repos/swift-nav/piksi_firmware/statuses/{sha}","languages_url":"https://api.github.com/repos/swift-nav/piksi_firmware/languages","stargazers_url":"https://api.github.com/repos/swift-nav/piksi_firmware/stargazers","contributors_url":"https://api.github.com/repos/swift-nav/piksi_firmware/contributors","subscribers_url":"https://api.github.com/repos/swift-nav/piksi_firmware/subscribers","subscription_url":"https://api.github.com/repos/swift-nav/piksi_firmware/subscription","commits_url":"https://api.github.com/repos/swift-nav/piksi_firmware/commits{/sha}","git_commits_url":"https://api.github.com/repos/swift-nav/piksi_firmware/git/commits{/sha}","comments_url":"https://api.github.com/repos/swift-nav/piksi_firmware/comments{/number}","issue_comment_url":"https://api.github.com/repos/swift-nav/piksi_firmware/issues/comments{/number}","contents_url":"https://api.github.com/repos/swift-nav/piksi_firmware/contents/{+path}","compare_url":"https://api.github.com/repos/swift-nav/piksi_firmware/compare/{base}...{head}","merges_url":"https://api.github.com/repos/swift-nav/piksi_firmware/merges","archive_url":"https://api.github.com/repos/swift-nav/piksi_firmware/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/swift-nav/piksi_firmware/downloads","issues_url":"https://api.github.com/repos/swift-nav/piksi_firmware/issues{/number}","pulls_url":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls{/number}","milestones_url":"https://api.github.com/repos/swift-nav/piksi_firmware/milestones{/number}","notifications_url":"https://api.github.com/repos/swift-nav/piksi_firmware/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/swift-nav/piksi_firmware/labels{/name}","releases_url":"https://api.github.com/repos/swift-nav/piksi_firmware/releases{/id}","created_at":"2011-09-26T23:35:50Z","updated_at":"2015-06-17T13:25:27Z","pushed_at":"2015-07-07T00:32:40Z","git_url":"git://github.com/swift-nav/piksi_firmware.git","ssh_url":"git@github.com:swift-nav/piksi_firmware.git","clone_url":"https://github.com/swift-nav/piksi_firmware.git","svn_url":"https://github.com/swift-nav/piksi_firmware","homepage":"swift-nav.com","size":6395,"stargazers_count":60,"watchers_count":60,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":55,"mirror_url":null,"open_issues_count":91,"forks":55,"open_issues":91,"watchers":60,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483"},"html":{"href":"https://github.com/swift-nav/piksi_firmware/pull/483"},"issue":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/issues/483"},"comments":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/issues/483/comments"},"review_comments":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483/comments"},"review_comment":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/pulls/483/commits"},"statuses":{"href":"https://api.github.com/repos/swift-nav/piksi_firmware/statuses/74bb43fce7c5319b5991727fcac9e8320d3bf04c"}}}}