PaulStoffregen / AltSoftSerial

Software emulated serial using hardware timers for improved compatibility
http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
328 stars 131 forks source link

Improved prescale #26

Open DrDiettrich opened 8 years ago

DrDiettrich commented 8 years ago

Thanks for your fine code, both easily understandable and efficient :-) Here is a suggestion for improved handling of the prescaler, better readable and easier to adopt to various prescaling factors. It also allows to report/flag invalid baudrates. prescale.zip

PaulStoffregen commented 8 years ago

From the zip file:

bool prescale(uint32_t cycles_per_bit)
{
    ticks_per_bit = cycles_per_bit;
    if (ticks_per_bit < MAX_COUNTS_PER_BIT) {
            //Serial.printf("cycles_per_bit = %d\n", cycles_per_bit);
            CONFIG_TIMER_NOPRESCALE();
            return true;
    }
    ticks_per_bit = cycles_per_bit / 8;
    if (ticks_per_bit < MAX_COUNTS_PER_BIT) {
            //Serial.printf("cycles_per_bit/8 = %d\n", cycles_per_bit);
            CONFIG_TIMER_PRESCALE_8();
            return true;
    }
#if defined(CONFIG_TIMER_PRESCALE_128)
    ticks_per_bit = cycles_per_bit / 128;
    if (ticks_per_bit < MAX_COUNTS_PER_BIT) {
            //Serial.printf("cycles_per_bit/64 = %d\n", cycles_per_bit);
            CONFIG_TIMER_PRESCALE_128();
            return true;
    }
#endif
#if defined(CONFIG_TIMER_PRESCALE_256)
    ticks_per_bit = cycles_per_bit / 256;
    if (ticks_per_bit < MAX_COUNTS_PER_BIT) {
            //Serial.printf("cycles_per_bit/256 = %d\n", cycles_per_bit);
            CONFIG_TIMER_PRESCALE_256();
            return true;
    }
#endif
#if defined(CONFIG_TIMER_PRESCALE_1024)
    ticks_per_bit = cycles_per_bit / 1024;
    if (ticks_per_bit < MAX_COUNTS_PER_BIT) {
            //Serial.printf("cycles_per_bit/1024 = %d\n", cycles_per_bit);
            CONFIG_TIMER_PRESCALE_1024();
            return true;
    }
#endif
    AltSoftSerial::timing_error = true;
    //Serial.println("baudrate too low");
    return false; //baudrate too low
}
DrDiettrich commented 8 years ago

I've just created a fork and want to add complete and test the modifications there. But perhaps you already see what I want to change, and how you can incorporate it into your library- your comment is not very enlightening.