{"action":"created","issue":{"url":"https://api.github.com/repos/spark/firmware/issues/762","labels_url":"https://api.github.com/repos/spark/firmware/issues/762/labels{/name}","comments_url":"https://api.github.com/repos/spark/firmware/issues/762/comments","events_url":"https://api.github.com/repos/spark/firmware/issues/762/events","html_url":"https://github.com/spark/firmware/pull/762","id":121584215,"number":762,"title":"Improve EEPROM stability and persistence","user":{"login":"monkbroc","id":1919681,"avatar_url":"https://avatars.githubusercontent.com/u/1919681?v=3","gravatar_id":"","url":"https://api.github.com/users/monkbroc","html_url":"https://github.com/monkbroc","followers_url":"https://api.github.com/users/monkbroc/followers","following_url":"https://api.github.com/users/monkbroc/following{/other_user}","gists_url":"https://api.github.com/users/monkbroc/gists{/gist_id}","starred_url":"https://api.github.com/users/monkbroc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/monkbroc/subscriptions","organizations_url":"https://api.github.com/users/monkbroc/orgs","repos_url":"https://api.github.com/users/monkbroc/repos","events_url":"https://api.github.com/users/monkbroc/events{/privacy}","received_events_url":"https://api.github.com/users/monkbroc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-12-10T21:45:03Z","updated_at":"2015-12-14T18:53:32Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/spark/firmware/pulls/762","html_url":"https://github.com/spark/firmware/pull/762","diff_url":"https://github.com/spark/firmware/pull/762.diff","patch_url":"https://github.com/spark/firmware/pull/762.patch"},"body":"### Changes\r\n* [Don't erase a page of flash ON EVERY BOOT](https://github.com/monkbroc/firmware/blob/18eda04106ea7f54d1a4a794840aa2403dad28bd/hal/src/stm32f2xx/eeprom_hal.c#L539) to increase Flash life and avoid erasing Flash in every low power situations\r\n* Don't format the EEPROM in case both pages are valid, just erase one\r\n* Lock the Flash after initialization or variable write\r\n\r\n### Background\r\n\r\nBad chargers and depleted batteries cause the EEPROM contents to be lost on the Photon and Electron.\r\nhttp://community.particle.io/t/eeprom-persistence-issue/16514\r\nhttps://community.particle.io/t/photon-ran-low-on-battery-now-stuck-in-sos/16710\r\n#704\r\n\r\nThe root cause is still not clear but one factor making it worse is the fact that the STM EEPROM emulation code (AN3390) erases one page of Flash on every boot. This is unnecessary and aweful for Flash life. Also what happens exactly if the microcontroller looses power during this erase operation? Experience shows that the EEPROM contents will be lost.\r\n\r\nThis is not a complete fix since an EEPROM erase could still happen during a low battery situation, but it will decrease the occurence of these issues.\r\n\r\n### Steps to reproduce EEPROM persistence issue\r\n\r\n1. Flash this application code\r\n\r\n```\r\n#include \"application.h\"\r\n\r\n// Write something to the EEPROM on demand\r\nint eepromWrite(String value) {\r\n  EEPROM.write(0, 0xA5);\r\n  return 0;\r\n}\r\n\r\nvoid setup()\r\n{\r\n  RGB.control(true);\r\n  if(EEPROM.read(0) == 0xFF) {\r\n    // Red means the EEPROM was wiped\r\n    RGB.color(255, 0, 0);\r\n  } else {\r\n    // Yellow means the EEPROM was preserved\r\n    RGB.color(255, 255, 0);\r\n  }\r\n  Particle.function(\"eepromWrite\", eepromWrite);\r\n}\r\n```\r\n\r\n2. Call `particle call device eepromWrite`\r\n3. Reboot the Photon and notice the yellow LED meaning EEPROM contents are saved\r\n4. Unplug the USB\r\n5. Replug it partially, fiddle with it, making sure the white LED turn on and off multiple times and even turns partially on\r\n6. Plug USB firmly\r\n7. Notice the red LED meaning the EEPROM contents were lost. Try again if it doesn't fail on the first try\r\n\r\n[Video of failure before the fix](https://youtu.be/YSzFfkvTlE8)\r\n[Video of correct behavior after the fix](https://youtu.be/iKj_9vaid5A)"},"comment":{"url":"https://api.github.com/repos/spark/firmware/issues/comments/164524678","html_url":"https://github.com/spark/firmware/pull/762#issuecomment-164524678","issue_url":"https://api.github.com/repos/spark/firmware/issues/762","id":164524678,"user":{"login":"andyw-lala","id":3930944,"avatar_url":"https://avatars.githubusercontent.com/u/3930944?v=3","gravatar_id":"","url":"https://api.github.com/users/andyw-lala","html_url":"https://github.com/andyw-lala","followers_url":"https://api.github.com/users/andyw-lala/followers","following_url":"https://api.github.com/users/andyw-lala/following{/other_user}","gists_url":"https://api.github.com/users/andyw-lala/gists{/gist_id}","starred_url":"https://api.github.com/users/andyw-lala/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andyw-lala/subscriptions","organizations_url":"https://api.github.com/users/andyw-lala/orgs","repos_url":"https://api.github.com/users/andyw-lala/repos","events_url":"https://api.github.com/users/andyw-lala/events{/privacy}","received_events_url":"https://api.github.com/users/andyw-lala/received_events","type":"User","site_admin":false},"created_at":"2015-12-14T18:53:31Z","updated_at":"2015-12-14T18:53:31Z","body":"Meaning that a fix that fixes the biggest problems but may not address\nevery single less common failure case, is to be preferred vastly over\nholding out for the perfect fix to all problems (not that I believe anyone\nwas pushing for that in this case.)\n\nIt wasn't a loaded or sarcastic remark, if that's the way it came across.\n\n\nOn Mon, Dec 14, 2015 at 12:43 PM, Matthew McGowan <notifications@github.com>\nwrote:\n\n> \"but never let perfection get in the way of progress.\" What do you mean\n> there @andyw-lala <https://github.com/andyw-lala>?\n>\n> —\n> Reply to this email directly or view it on GitHub\n> <https://github.com/spark/firmware/pull/762#issuecomment-164522055>.\n>\n\n\n\n-- \nAndy\n"}}