title: How to implement "auto baud rate detection" feature on Cortex-M3
body: <p>in my project I'm trying to implement a Cortex-M0-like UART auto baud rate detection feature on a Cortex-M3, which unfortunately does not have this handy feature on-board. </p>
  
  <p>The idea is that the master uses two sync bits (1-0 sequence) at the beginning of each sent frame which allow the slave to synchronize itself to the (unknown) baud rate by measuring the time between the two falling edges (of the start bit and the high bit). </p>
  
  <p>Basically I got this working with a timer input-capture which is connected to the UART RX pin, but only if my initial baud-rate is close to the actual baud-rate used by the master (+/- 15%). </p>
  
  <p>However, problems arise if I use a slave default baud-rate which is much lower or higher than the actual baud-rate used. I can still measure the duration of the first two (sync) bits, adjust my baud-rate, but still I can't synchronize with the end of the frame/stop bit and thus I'm losing data. </p>
  
  <p>For example, if I set my slave's UART to 9600 B/s per default and my master is actually sending with a much higher baud-rate, let's say 230,400 B/s:</p>
  
  <ul>
  <li>UART RX will detect the start bit and starts to sample with 9,600 B/s</li>
  <li>At the same time, I will measure the time between the falling edge of the start bit and of the high-bit to detect the actual baud-rate</li>
  <li>When the measurement is done correctly, I will adjust UART baud-rate to 230,400 B/s</li>
  <li>BUT, at that time, UART RX will still be waiting for the first sample to take, since it expects that 1st data bit much later (as it would occur with 9,600 B/s). So after adjusting my baud-rate, there will still be 7 or 8 samples taken although in reality, the first two data bits are already over.</li>
  </ul>
  
  <p>I've tested this with STM32F0's auto baud rate feature and there I can select any default baud-rate value without losing any data. So I guess, since M0-UART "knows" that two sync-bits have already passed it will only sample 6 more data bits after baud-rate measurement to keep synchronized with the stop-bit. But how can I achieve this behaviour manually? </p>
  
  <p>I hope you can get my point, though it's somewhat hard to explain for me, and I appreciate your ideas!</p>