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

Should the NRF52Serial::setBaudrate() use a KeyValueTable to best approximate request? #459

Open
microbit-carlos opened this issue Jan 10, 2025 · 0 comments

Comments

@microbit-carlos
Copy link
Collaborator

microbit-carlos commented Jan 10, 2025

Currently it's just a switch case where the users has to specify the exact baud rate from those available, or the defaul 115200 is applied instead:

int NRF52Serial::setBaudrate(uint32_t baudrate)
{
    nrf_uarte_baudrate_t baud = NRF_UARTE_BAUDRATE_115200;

    switch(baudrate)
    {
        case 9600 : baud = NRF_UARTE_BAUDRATE_9600; break;
        case 31250 : baud = NRF_UARTE_BAUDRATE_31250; break;
        case 38400 : baud = NRF_UARTE_BAUDRATE_38400; break;
        case 57600 : baud = NRF_UARTE_BAUDRATE_57600; break;
        case 115200 : baud = NRF_UARTE_BAUDRATE_115200; break;
        case 230400 : baud = NRF_UARTE_BAUDRATE_230400; break;
        case 921600 : baud = NRF_UARTE_BAUDRATE_921600; break;
        case 1000000 : baud = NRF_UARTE_BAUDRATE_1000000; break;
    }

    nrf_uarte_baudrate_set(p_uarte_, baud);
    return DEVICE_OK;
}

The micro:bit V1 Mbed implementation did something very similar to KeyValueTable, where best approximation below the request is applied instead:

void serial_baud(serial_t *obj, int baudrate)
{
    if (baudrate<=1200) {
        obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200;
        return;
    }

    for (int i = 1; i<17; i++) {
        if (baudrate<acceptedSpeeds[i][0]) {
            obj->uart->BAUDRATE = acceptedSpeeds[i - 1][1];
            return;
        }
    }
    obj->uart->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1M;
}

So this would align better with the V1 behaviour and also provide a baud rate closer to the one requested by the user (like DAL/CODAL do in other places like setting the acc/mag sampling rates, etc).


Somewhat related:

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

1 participant