A Turkish court has freed US pastor Andrew Brunson two years after he was detained, charged with helping to plot a coup against President Recep Tayyip Erdogan.