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

feat: add uart_with_dynamic_baudrate to SoCCore #2122

Conversation

jwise
Copy link
Contributor

@jwise jwise commented Nov 9, 2024

In our application, we would like our default UART to have a configurable baud rate based on other system parameters. Sample code to drive this:

static uint32_t current_uart_baudrate = 921600;

static struct tuning {
    uint32_t baudrate;
    uint32_t tuning;
} predefined_tunings[] = {
    { 1500000, 85899346 },
    { 921600, 52776558 },
    { 115200, 6597070 }
};

static void update_uart_baudrate(uint32_t desired_baudrate) {
    uint32_t new_tuning = 0;
    for (int i = 0; i < sizeof(predefined_tunings) / sizeof(predefined_tunings[0]); i++) {
        if (predefined_tunings[i].baudrate == desired_baudrate) {
            new_tuning = predefined_tunings[i].tuning;
        }
    }

    if (new_tuning == 0) {
        uint64_t new_baudrate = (uint64_t)desired_baudrate << 32;
        new_tuning = new_baudrate / 75000000l;
    }
    if (new_tuning) {
        uartbone_phy_tuning_word_write(new_tuning);
        current_uart_baudrate = desired_baudrate;
    }
}

static void baud_cmd(char *arg)
{
    int baud = atoi(arg);
    update_uart_baudrate(baud);
    printf("baud rate is now %d\n", baud);
}
DEFINE_COMMAND(baud_cmd, "baud", "set UART baud rate");

cc: @jameyhicks

Copy link
Owner

@enjoy-digital enjoy-digital left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jwise, this looks good.

@enjoy-digital enjoy-digital merged commit 8399c91 into enjoy-digital:master Nov 15, 2024
1 check passed
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

Successfully merging this pull request may close these issues.

3 participants