Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved prescale #26

Open
DrDiettrich opened this issue May 21, 2016 · 2 comments
Open

Improved prescale #26

DrDiettrich opened this issue May 21, 2016 · 2 comments

Comments

@DrDiettrich
Copy link

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
Copy link
Owner

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants