Shanks0224 opened a new pull request, #22602:
URL: https://github.com/apache/nuttx/pull/22602

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   `arch/xtensa/src/common/xtensa_oneshot.c` (the CCOUNT/CCOMPARE oneshot
   lower half, `CONFIG_XTENSA_ONESHOT`) has no `current()` method, so
   `oneshot_current()` calls a NULL pointer.  `drivers/timers/arch_alarm.c`
   calls it while setting up the lower half, so this driver can not be used
   as the system timer through `up_alarm_set_lowerhalf()`.  It also does not
   build with `CONFIG_ONESHOT_COUNT`, because its methods still have the
   timespec prototypes.
   
   - Implement `current()`.  CCOUNT is a 32-bit counter, so the wraps are
     counted to keep the time increasing.  A wrap is seen only if
     `current()` runs at least once per wrap period (about 17.9 s at
     240 MHz); with the system tick on this timer it runs on every tick.
   - Support `CONFIG_ONESHOT_COUNT`: implement `current()`, `start()`,
     `start_absolute()`, `cancel()` and `max_delay()` with counts and call
     `oneshot_count_init()`.  The timespec methods are kept for the other
     configurations.  The wrap tracking state is moved into the lower half
     structure, as a function-local static would be shared by all instances.
   - Read CCOUNT and update the wrap tracking state with the lower half lock
     held, as `current()` can be called concurrently and could lose or count
     a wrap twice.
   
   ## Impact
   
   Only `xtensa_oneshot.c`, which is built for all Xtensa chips but is only
   used when a board calls `xtensa_oneshot_initialize()`.  No in-tree board
   calls it, so no in-tree configuration changes behavior.  CCOUNT is a
   per-CPU register, as before.
   
   ## Testing
   
   No in-tree board uses this driver, so for testing only I made the ESP32
   system tick run on it: `up_timer_initialize()` calls
   `xtensa_oneshot_initialize()` for the internal timer 0 interrupt and
   `up_alarm_set_lowerhalf()` (`CONFIG_ALARM_ARCH=y`).  On ESP32 the timer
   interrupt must be registered with `esp_setup_irq()`, so the test patch
   does that and restores the ESP interrupt demux handler, which the
   `irq_attach()` in `xtensa_oneshot_initialize()` replaces on ESP32.  A 
test-only
   Kconfig line selects `CONFIG_ONESHOT_COUNT`, as no Xtensa config selects
   it.  The test patch is not part of this PR.
   
   esp32-devkitc:nsh on the Espressif QEMU, xtensa-esp-elf 14.2.0.  The test
   program polls CCOUNT every 100 ms for 45 s (two CCOUNT wraps) and
   compares the cycles with `CLOCK_MONOTONIC`.
   
   master: crashes at boot (the NULL `current()`):
   ```
   xtensa_user_panic: User Exception: EXCCAUSE=0014 task: Idle_Task
   up_dump_register:    PC: 00000000    PS: 00060b30
   ```
   and with `CONFIG_ONESHOT_COUNT=y` it does not build:
   ```
   common/xtensa_oneshot.c:72:16: error: initialization of 'void (*)(struct 
oneshot_lowerhalf_s *, clkcnt_t)' ... from incompatible pointer type 'int 
(*)(struct oneshot_lowerhalf_s *, const struct timespec *)'
   ```
   
   this PR, `CONFIG_ONESHOT_COUNT=n`:
   ```
   R1 ccount 49494 ms, monotonic 49494 ms, diff 120 us, wraps 2
   TEST DONE
   ```
   ostest: `ostest_main: Exiting with status 0`, no ERROR lines.
   
   this PR, `CONFIG_ONESHOT_COUNT=y`:
   ```
   R1 ccount 49498 ms, monotonic 49498 ms, diff 181 us, wraps 2
   TEST DONE
   ```
   ostest: `ostest_main: Exiting with status 0`, no ERROR lines.
   
   Each commit builds esp32-devkitc:nsh and esp32s3-devkit:nsh with
   `CONFIG_ONESHOT=y CONFIG_XTENSA_ONESHOT=y` without warnings.
   
   Not tested on real hardware.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

  • [PR] arch/xtensa: Add current... Shanks0224 (via GitHub) via commits

Reply via email to