{"action":"created","issue":{"url":"https://api.github.com/repos/arduino/Arduino/issues/7254","repository_url":"https://api.github.com/repos/arduino/Arduino","labels_url":"https://api.github.com/repos/arduino/Arduino/issues/7254/labels{/name}","comments_url":"https://api.github.com/repos/arduino/Arduino/issues/7254/comments","events_url":"https://api.github.com/repos/arduino/Arduino/issues/7254/events","html_url":"https://github.com/arduino/Arduino/issues/7254","id":299279499,"number":7254,"title":"Add flushReceiveBuffer() to HardwareSerial","user":{"login":"RobTillaart","id":462844,"avatar_url":"https://avatars3.githubusercontent.com/u/462844?v=4","gravatar_id":"","url":"https://api.github.com/users/RobTillaart","html_url":"https://github.com/RobTillaart","followers_url":"https://api.github.com/users/RobTillaart/followers","following_url":"https://api.github.com/users/RobTillaart/following{/other_user}","gists_url":"https://api.github.com/users/RobTillaart/gists{/gist_id}","starred_url":"https://api.github.com/users/RobTillaart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RobTillaart/subscriptions","organizations_url":"https://api.github.com/users/RobTillaart/orgs","repos_url":"https://api.github.com/users/RobTillaart/repos","events_url":"https://api.github.com/users/RobTillaart/events{/privacy}","received_events_url":"https://api.github.com/users/RobTillaart/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-02-22T09:33:34Z","updated_at":"2018-02-22T12:22:40Z","closed_at":null,"author_association":"NONE","body":"In Serial communication sometimes one wants to flush all data in the receive buffer at once. E.g. because data has become invalid. - https://forum.arduino.cc/index.php?topic=530650 - \r\n\r\nThe code to do this very fast in HardwareSerial is straightforward,\r\n```\r\nvoid HardwareSerial::flushReceiveBuffer()\r\n{\r\n  _rx_buffer_head = _rx_buffer_tail;\r\n}\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/arduino/Arduino/issues/comments/367664387","html_url":"https://github.com/arduino/Arduino/issues/7254#issuecomment-367664387","issue_url":"https://api.github.com/repos/arduino/Arduino/issues/7254","id":367664387,"user":{"login":"RobTillaart","id":462844,"avatar_url":"https://avatars3.githubusercontent.com/u/462844?v=4","gravatar_id":"","url":"https://api.github.com/users/RobTillaart","html_url":"https://github.com/RobTillaart","followers_url":"https://api.github.com/users/RobTillaart/followers","following_url":"https://api.github.com/users/RobTillaart/following{/other_user}","gists_url":"https://api.github.com/users/RobTillaart/gists{/gist_id}","starred_url":"https://api.github.com/users/RobTillaart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RobTillaart/subscriptions","organizations_url":"https://api.github.com/users/RobTillaart/orgs","repos_url":"https://api.github.com/users/RobTillaart/repos","events_url":"https://api.github.com/users/RobTillaart/events{/privacy}","received_events_url":"https://api.github.com/users/RobTillaart/received_events","type":"User","site_admin":false},"created_at":"2018-02-22T12:22:40Z","updated_at":"2018-02-22T12:22:40Z","author_association":"NONE","body":"Thanks John, (read the discussion)\r\n\r\nif we do not use ATOMIC and use \r\n\r\n_rx_buffer_head = _rx_buffer_tail;\r\n\r\n**Three cases**  - assuming single byte indices\r\n- if the rx_complete_irq comes before the read of tail -> no problem\r\n- if the rx_complete_irq comes after the read of tail and before write of head, -> head will be overwritten and the new char will effectively be \"flushed\" too\r\n- if the rx_complete_irq comes after the write of head, -> there is just a new char after the flush (happens all the time.\r\n\r\nin case of multi byte indices ATOMIC is needed (no doubt about that)\r\n\r\nATOMIC could be added conditionally depending on ```#if  (SERIAL_RX_BUFFER_SIZE>256).``` but as I don't know how much clock cycles are added by the ATOMIC that might be overkill. Also from maintenance point of view.\r\n\r\nSo the state of the proposal is now:\r\n```\r\nvoid HardwareSerial::clearReceiveBuffer()\r\n{\r\n  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {\r\n    _rx_buffer_head = _rx_buffer_tail;\r\n  }\r\n}\r\n```\r\n\r\n"}}
{"actor":{"display_login":"RobTillaart"}}