All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: Arnaud Minier <arnaud.minier@telecom-paris.fr>
Cc: qemu-devel@nongnu.org, "Laurent Vivier" <lvivier@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	qemu-arm@nongnu.org, "Paolo Bonzini" <pbonzini@redhat.com>,
	"Alistair Francis" <alistair@alistair23.me>,
	"Thomas Huth" <thuth@redhat.com>,
	"Inès Varhol" <ines.varhol@telecom-paris.fr>,
	"Samuel Tardieu" <samuel.tardieu@telecom-paris.fr>
Subject: Re: [PATCH v2 4/6] hw/char/stm32l4x5_usart: Add options for serial parameters setting
Date: Thu, 28 Mar 2024 16:03:22 +0000	[thread overview]
Message-ID: <CAFEAcA9n8g6USCpYzBQSOns9HOyXx_QNFAfwj1YY1+J1tEDYFw@mail.gmail.com> (raw)
In-Reply-To: <20240324165545.201908-5-arnaud.minier@telecom-paris.fr>

On Sun, 24 Mar 2024 at 16:57, Arnaud Minier
<arnaud.minier@telecom-paris.fr> wrote:
>
> Add a function to change the settings of the
> serial connection.
>
> Signed-off-by: Arnaud Minier <arnaud.minier@telecom-paris.fr>
> Signed-off-by: Inès Varhol <ines.varhol@telecom-paris.fr>
> ---
>  hw/char/stm32l4x5_usart.c | 98 +++++++++++++++++++++++++++++++++++++++
>  hw/char/trace-events      |  1 +
>  2 files changed, 99 insertions(+)
>
> diff --git a/hw/char/stm32l4x5_usart.c b/hw/char/stm32l4x5_usart.c
> index ec8c2f6e63..b4d11dd826 100644
> --- a/hw/char/stm32l4x5_usart.c
> +++ b/hw/char/stm32l4x5_usart.c
> @@ -267,6 +267,92 @@ static void usart_cancel_transmit(Stm32l4x5UsartBaseState *s)
>      }
>  }
>
> +static void stm32l4x5_update_params(Stm32l4x5UsartBaseState *s)
> +{
> +    int speed, parity, data_bits, stop_bits;
> +    uint32_t value, usart_div;
> +    QEMUSerialSetParams ssp;
> +
> +    /* Select the parity type */
> +    if (s->cr1 & R_CR1_PCE_MASK) {
> +        if (s->cr1 & R_CR1_PS_MASK) {
> +            parity = 'O';
> +        } else {
> +            parity = 'E';
> +        }
> +    } else {
> +        parity = 'N';
> +    }
> +
> +    /* Select the number of stop bits */
> +    switch (FIELD_EX32(s->cr2, CR2, STOP)) {
> +    case 0:
> +        stop_bits = 1;
> +        break;
> +    case 2:
> +        stop_bits = 2;
> +        break;
> +    default:
> +        qemu_log_mask(LOG_UNIMP,
> +            "UNIMPLEMENTED: fractionnal stop bits; CR2[13:12] = %x",

%x without a leading 0x is a bit odd. In this case since
the possible values are 0-3 it doesn't make a difference,
but maybe better to use %u ?

> +            FIELD_EX32(s->cr2, CR2, STOP));
> +        return;
> +    }
> +
> +    /* Select the length of the word */
> +    switch ((FIELD_EX32(s->cr1, CR1, M1) << 1) | FIELD_EX32(s->cr1, CR1, M0)) {
> +    case 0:
> +        data_bits = 8;
> +        break;
> +    case 1:
> +        data_bits = 9;
> +        break;
> +    case 2:
> +        data_bits = 7;
> +        break;
> +    default:
> +        qemu_log_mask(LOG_GUEST_ERROR,
> +            "UNDEFINED: invalid word length, CR1.M = 0b11");
> +        return;
> +    }
> +
> +    /* Select the baud rate */
> +    value = FIELD_EX32(s->brr, BRR, BRR);
> +    if (value < 16) {
> +        qemu_log_mask(LOG_GUEST_ERROR,
> +            "UNDEFINED: BRR lesser than 16: %u", value);

"less than"

> +        return;
> +    }
> +
> +    if (FIELD_EX32(s->cr1, CR1, OVER8) == 0) {
> +        /*
> +         * Oversampling by 16
> +         * BRR = USARTDIV
> +         */
> +        usart_div = value;
> +    } else {
> +        /*
> +         * Oversampling by 8
> +         * - BRR[2:0] = USARTDIV[3:0] shifted 1 bit to the right.
> +         * - BRR[3] must be kept cleared.
> +         * - BRR[15:4] = USARTDIV[15:4]
> +         * - The frequency is multiplied by 2
> +         */
> +        usart_div = ((value & 0xFFF0) | ((value & 0x0007) << 1)) / 2;
> +    }
> +
> +    speed = clock_get_hz(s->clk) / usart_div;
> +
> +    ssp.speed     = speed;
> +    ssp.parity    = parity;
> +    ssp.data_bits = data_bits;
> +    ssp.stop_bits = stop_bits;
> +
> +    qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
> +
> +    trace_stm32l4x5_usart_update_params(speed, parity, data_bits, stop_bits);
> +}

Otherwise
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM


  reply	other threads:[~2024-03-28 16:04 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-24 16:55 [PATCH v2 0/6] hw/char: Implement the STM32L4x5 USART, UART and LPUART Arnaud Minier
2024-03-24 16:55 ` [PATCH v2 1/6] hw/misc/stm32l4x5_rcc: Propagate period when enabling a clock Arnaud Minier
2024-03-24 16:55 ` [PATCH v2 2/6] hw/char: Implement STM32L4x5 USART skeleton Arnaud Minier
2024-03-28 15:55   ` Peter Maydell
2024-03-24 16:55 ` [PATCH v2 3/6] hw/char/stm32l4x5_usart: Enable serial read and write Arnaud Minier
2024-03-28 15:59   ` Peter Maydell
2024-03-24 16:55 ` [PATCH v2 4/6] hw/char/stm32l4x5_usart: Add options for serial parameters setting Arnaud Minier
2024-03-28 16:03   ` Peter Maydell [this message]
2024-03-24 16:55 ` [PATCH v2 5/6] hw/arm: Add the USART to the stm32l4x5 SoC Arnaud Minier
2024-03-28 16:06   ` Peter Maydell
2024-03-24 16:55 ` [PATCH v2 6/6] tests/qtest: Add tests for the STM32L4x5 USART Arnaud Minier
2024-03-25  6:19   ` Thomas Huth
2024-03-28 16:14     ` Peter Maydell
2024-03-28 16:10 ` [PATCH v2 0/6] hw/char: Implement the STM32L4x5 USART, UART and LPUART Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFEAcA9n8g6USCpYzBQSOns9HOyXx_QNFAfwj1YY1+J1tEDYFw@mail.gmail.com \
    --to=peter.maydell@linaro.org \
    --cc=alistair@alistair23.me \
    --cc=arnaud.minier@telecom-paris.fr \
    --cc=ines.varhol@telecom-paris.fr \
    --cc=lvivier@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=samuel.tardieu@telecom-paris.fr \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.