qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Richard Henderson <richard.henderson@linaro.org>, qemu-devel@nongnu.org
Cc: S.E.Harris@kent.ac.uk, me@xcancerberox.com.ar,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	dovgaluk@ispras.ru, imammedo@redhat.com, mrolnik@gmail.com,
	aleksandar.m.mail@gmail.com
Subject: Re: [PATCH rc1 12/24] hw/timer: Add limited support for Atmel 16 bit timer peripheral
Date: Thu, 23 Jan 2020 05:49:28 +0100	[thread overview]
Message-ID: <13770068-790a-5317-a0fa-5a672bae7253@redhat.com> (raw)
In-Reply-To: <20200123000307.11541-13-richard.henderson@linaro.org>

On 23/01/2020 01.02, Richard Henderson wrote:
> From: Michael Rolnik <mrolnik@gmail.com>
> 
> These were designed to facilitate testing but should provide enough
> function to be useful in other contexts.  Only a subset of the functions
> of each peripheral is implemented, mainly due to the lack of a standard
> way to handle electrical connections (like GPIO pins).
> 
> Signed-off-by: Sarah Harris <S.E.Harris@kent.ac.uk>
> Message-Id: <20200118191416.19934-13-mrolnik@gmail.com>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> [rth: Squash info mtree fixes and a file rename from f4bug, which was:]
> Suggested-by: Aleksandar Markovic <aleksandar.m.mail@gmail.com>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
[...]
> +static void avr_timer16_clksrc_update(AVRTimer16State *t16)
> +{
> +    uint16_t divider = 0;
> +    switch (CLKSRC(t16)) {
> +    case T16_CLKSRC_EXT_FALLING:
> +    case T16_CLKSRC_EXT_RISING:
> +        ERROR("external clock source unsupported");

Maybe this should rather be qemu_log_mask(LOG_UNIMP, ...) instead?

> +        goto end;
> +    case T16_CLKSRC_STOPPED:
> +        goto end;
> +    case T16_CLKSRC_DIV1:
> +        divider = 1;
> +        break;
> +    case T16_CLKSRC_DIV8:
> +        divider = 8;
> +        break;
> +    case T16_CLKSRC_DIV64:
> +        divider = 64;
> +        break;
> +    case T16_CLKSRC_DIV256:
> +        divider = 256;
> +        break;
> +    case T16_CLKSRC_DIV1024:
> +        divider = 1024;
> +        break;
> +    default:
> +        goto end;
> +    }
> +    t16->freq_hz = t16->cpu_freq_hz / divider;
> +    t16->period_ns = NANOSECONDS_PER_SECOND / t16->freq_hz;
> +    DB_PRINT("Timer frequency %" PRIu64 " hz, period %" PRIu64 " ns (%f s)",
> +             t16->freq_hz, t16->period_ns, 1 / (double)t16->freq_hz);
> +end:
> +    return;
> +}

Nit: You could use an early "return" instead of "goto end" and the
superfluous return statement at the end of the function.

> +static void avr_timer16_set_alarm(AVRTimer16State *t16)
> +{
> +    if (CLKSRC(t16) == T16_CLKSRC_EXT_FALLING ||
> +        CLKSRC(t16) == T16_CLKSRC_EXT_RISING ||
> +        CLKSRC(t16) == T16_CLKSRC_STOPPED) {
> +        /* Timer is disabled or set to external clock source (unsupported) */
> +        goto end;
> +    }
> +
> +    uint64_t alarm_offset = 0xffff;
> +    enum NextInterrupt next_interrupt = OVERFLOW;
> +
> +    switch (MODE(t16)) {
> +    case T16_MODE_NORMAL:
> +        /* Normal mode */
> +        if (OCRA(t16) < alarm_offset && OCRA(t16) > CNT(t16) &&
> +            (t16->imsk & T16_INT_OCA)) {
> +            alarm_offset = OCRA(t16);
> +            next_interrupt = COMPA;
> +        }
> +        break;
> +    case T16_MODE_CTC_OCRA:
> +        /* CTC mode, top = ocra */
> +        if (OCRA(t16) < alarm_offset && OCRA(t16) > CNT(t16)) {
> +            alarm_offset = OCRA(t16);
> +            next_interrupt = COMPA;
> +        }
> +       break;
> +    case T16_MODE_CTC_ICR:
> +        /* CTC mode, top = icr */
> +        if (ICR(t16) < alarm_offset && ICR(t16) > CNT(t16)) {
> +            alarm_offset = ICR(t16);
> +            next_interrupt = CAPT;
> +        }
> +        if (OCRA(t16) < alarm_offset && OCRA(t16) > CNT(t16) &&
> +            (t16->imsk & T16_INT_OCA)) {
> +            alarm_offset = OCRA(t16);
> +            next_interrupt = COMPA;
> +        }
> +        break;
> +    default:
> +        ERROR("pwm modes are unsupported");

qemu_log_mask(LOG_UNIMP, ...) ?

> +        goto end;
> +    }
> +    if (OCRB(t16) < alarm_offset && OCRB(t16) > CNT(t16) &&
> +        (t16->imsk & T16_INT_OCB)) {
> +        alarm_offset = OCRB(t16);
> +        next_interrupt = COMPB;
> +    }
> +    if (OCRC(t16) < alarm_offset && OCRB(t16) > CNT(t16) &&
> +        (t16->imsk & T16_INT_OCC)) {
> +        alarm_offset = OCRB(t16);
> +        next_interrupt = COMPC;
> +    }
> +    alarm_offset -= CNT(t16);
> +
> +    t16->next_interrupt = next_interrupt;
> +    uint64_t alarm_ns =
> +        t16->reset_time_ns + ((CNT(t16) + alarm_offset) * t16->period_ns);
> +    timer_mod(t16->timer, alarm_ns);
> +
> +    DB_PRINT("next alarm %" PRIu64 " ns from now",
> +        alarm_offset * t16->period_ns);
> +
> +end:
> +    return;
> +}

This function could also use early returns instead of "goto end".

(no need to respin just because of these nits ... but maybe something to
consider if you respin anyway)

 Thomas



  reply	other threads:[~2020-01-23  4:50 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-23  0:02 [PATCH rc1 00/24] target/avr merger Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 01/24] target/avr: Add outward facing interfaces and core CPU logic Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 02/24] target/avr: Add instruction helpers Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 03/24] target/avr: Add instruction translation - Registers definition Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 04/24] target/avr: Add instruction translation - Arithmetic and Logic Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 05/24] target/avr: Add instruction translation - Branch Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 06/24] target/avr: Add instruction translation - Data Transfer Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 07/24] target/avr: Add instruction translation - Bit and Bit-test Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 08/24] target/avr: Add instruction translation - MCU Control Instructions Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 09/24] target/avr: Add instruction translation - CPU main translation function Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 10/24] target/avr: Add instruction disassembly function Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 11/24] hw/char: Add limited support for Atmel USART peripheral Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 12/24] hw/timer: Add limited support for Atmel 16 bit timer peripheral Richard Henderson
2020-01-23  4:49   ` Thomas Huth [this message]
2020-01-23 17:29     ` Philippe Mathieu-Daudé
2020-01-23  0:02 ` [PATCH rc1 13/24] hw/misc: Add Atmel power device Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 14/24] target/avr: Add section about AVR into QEMU documentation Richard Henderson
2020-01-23  4:56   ` Thomas Huth
2020-01-24  0:33     ` Philippe Mathieu-Daudé
2020-01-23  0:02 ` [PATCH rc1 15/24] target/avr: Register AVR support with the rest of QEMU Richard Henderson
2020-01-23  0:02 ` [PATCH rc1 16/24] target/avr: Add machine none test Richard Henderson
2020-01-23  5:02   ` Thomas Huth
2020-01-23  0:03 ` [PATCH rc1 17/24] target/avr: Update MAINTAINERS file Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 18/24] hw/avr: Introduce ATMEL_ATMEGA_MCU config Richard Henderson
2020-01-23  5:04   ` Thomas Huth
2020-01-23 17:27     ` Philippe Mathieu-Daudé
2020-01-23  0:03 ` [PATCH rc1 19/24] hw/avr: Add some ATmega microcontrollers Richard Henderson
2020-01-23 23:40   ` Philippe Mathieu-Daudé
2020-01-23  0:03 ` [PATCH rc1 20/24] hw/avr: Add some Arduino boards Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 21/24] target/avr: Update build system Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 22/24] tests/boot-serial-test: Test some Arduino boards (AVR based) Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 23/24] tests/acceptance: Test the Arduino MEGA2560 board Richard Henderson
2020-01-23  0:03 ` [PATCH rc1 24/24] .travis.yml: Run the AVR acceptance tests Richard Henderson
2020-01-23  5:12 ` [PATCH rc1 00/24] target/avr merger Thomas Huth
2020-01-23 11:56   ` Michael Rolnik
2020-01-23 17:36     ` Thomas Huth

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=13770068-790a-5317-a0fa-5a672bae7253@redhat.com \
    --to=thuth@redhat.com \
    --cc=S.E.Harris@kent.ac.uk \
    --cc=aleksandar.m.mail@gmail.com \
    --cc=dovgaluk@ispras.ru \
    --cc=f4bug@amsat.org \
    --cc=imammedo@redhat.com \
    --cc=me@xcancerberox.com.ar \
    --cc=mrolnik@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).