Преглед изворни кода

fmrx: use prescaler if baud rate is low

For low baud rates, the counter can get uncomfortably close to
overflow, causing a late flank to be misdetected. Enable the prescaler
in such case.
H. Peter Anvin пре 3 година
родитељ
комит
77bb7cc50f
1 измењених фајлова са 14 додато и 4 уклоњено
  1. 14 4
      USBCAS/fmrx.c

+ 14 - 4
USBCAS/fmrx.c

@@ -37,14 +37,16 @@ void fmrx_init(void)
 	/* Enable interrupts on input capture */
 	TIMSK1 = (1 << ICIE1);
 
+	/* Clear interrupt flag if set */
+	TIFR1 = (1 << ICF1);
+
 	/* Clear timer counter */
 	TCNT1 = 0;
 
 	/*
-	 * Input capture noise canceler on, no prescaler, normal mode;
-	 * start timer
+	 * Input capture noise canceler on, normal mode; not counting yet
 	 */
-	tccr1b = (1 << ICNC1) + (1 << CS10);
+	tccr1b = (1 << ICNC1) + (0 << CS10);
 	if (!(PIND & (1 << 4)))
 		tccr1b |= 1 << ICES1;
 	TCCR1B = tccr1b;
@@ -54,12 +56,20 @@ static uint16_t bytes;
 
 void fmrx_set_speed(uint32_t baudrate)
 {
-	char msg[256];
+	uint8_t prescale = 1;	/* No prescaling */
+
+	/* Enable prescaler for low baud rates to reduce wraparound */
+	if (baudrate < 976) {
+		baudrate <<= 3;	/* ... for calculation purposes ... */
+		prescale++;	/* Prescale by 8 */
+	}
 
 	one_bit_lo = F_CPU / (4 * baudrate);
 	one_bit_hi = 3 * one_bit_lo;
 
 	bytes = 0;
+
+	TCCR1B = (TCCR1B & ~7) | prescale;
 }
 
 /* We "hunt" for a block until we find this bit pattern; same as ABC800 */