docs: def raw_on(serial):
      """
      Puts the device into raw mode.
      """
  
      def flush_to_msg(serial, msg):
          """Read the rx serial data until we reach an expected message."""
          data = serial.read_until(msg)
          if not data.endswith(msg):
              if COMMAND_LINE_FLAG:
                  print(data)
              raise IOError('Could not enter raw REPL.')
  
      def flush(serial):
          """Flush all rx input without relying on serial.flushInput()."""
          n = serial.inWaiting()
          while n > 0:
              serial.read(n)
              n = serial.inWaiting()
  
      raw_repl_msg = b'raw REPL; CTRL-B to exit\r\n>'
      # Send CTRL-B to end raw mode if required.
      serial.write(b'\x02')
      # Send CTRL-C three times between pauses to break out of loop.
      for i in range(3):
          serial.write(b'\r\x03')
          time.sleep(0.01)
      flush(serial)
      # Go into raw mode with CTRL-A.
      serial.write(b'\r\x01')
      flush_to_msg(serial, raw_repl_msg)
      # Soft Reset with CTRL-D
      serial.write(b'\x04')
      flush_to_msg(serial, b'soft reboot\r\n')
      # Some MicroPython versions/ports/forks provide a different message after
      # a Soft Reset, check if we are in raw REPL, if not send a CTRL-A again
      data = serial.read_until(raw_repl_msg)
      if not data.endswith(raw_repl_msg):
          serial.write(b'\r\x01')
          flush_to_msg(serial, raw_repl_msg)
      flush(serial)