linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Csókás Bence" <csokas.bence@prolan.hu>
To: William Breathitt Gray <wbg@kernel.org>
Cc: <linux-iio@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<timestamp@lists.linux.dev>, Jonathan Cameron <jic23@kernel.org>,
	"Lars-Peter Clausen" <lars@metafoo.de>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	Dipen Patel <dipenp@nvidia.com>, <dlechner@baylibre.com>
Subject: Re: [Q] Frequency & duty cycle measurement?
Date: Thu, 30 Jan 2025 14:59:20 +0100	[thread overview]
Message-ID: <dffc0f18-799a-4fc7-a6b5-2fa270e1fc58@prolan.hu> (raw)
In-Reply-To: <Z5efcokgHix-k3lW@ishi>

Hi William,

On 2025. 01. 27. 16:00, William Breathitt Gray wrote:
> In the userspace application, you would setup a Counter "watch" to
> collect each desired timer value on the respective Counter events; I
> assume RA and RB are Count 0 and Count 1 respectively, but if they
> represent something else please let me know:
> 
>      static struct counter_watch watches[2] = {
>              {
>                      /* Component data: Count 0 count */
>                      .component.type = COUNTER_COMPONENT_COUNT,
>                      .component.scope = COUNTER_SCOPE_COUNT,
>                      .component.parent = 0,
>                      /* Event type: Capture */
>                      .event = COUNTER_EVENT_CAPTURE,
>                      /* Device event channel 0 */
>                      .channel = 0,
>              },
>              {
>                      /* Component data: Count 1 count */
>                      .component.type = COUNTER_COMPONENT_COUNT,
>                      .component.scope = COUNTER_SCOPE_COUNT,
>                      .component.parent = 1,
>                      /* Event type: Capture */
>                      .event = COUNTER_EVENT_CAPTURE,
>                      /* Device event channel 0 */
>                      .channel = 0,
>              },
>      };
>      ...
>      int main(void)
>      {
>              int fd;
>              int i;
> 	    unsigned long long delta_ts, delta_ra, delta_rb;
> 	    double ra_frequency, rb_frequency, rb_ra;
>              struct counter_event first_capture[2], second_capture[2];
>              
>              /* Open Counter chrdev */
>              fd = open("/dev/counter0", O_RDWR);
>              
>              for (i = 0; i < 2; i++) {
> 	            /* Register all Counter watches */
>                      ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches + i);
>              }
> 	    /* Start collecting Counter events */
>              ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
>              
>              for (;;) {
> 	        /* Read first Counter event capture */
>              	read(fd, first_capture, sizeof(first_capture));
> 	        /* Read second Counter event capture */
>              	read(fd, second_capture, sizeof(second_capture));
>                  
> 		/* Within each capture, timestamp is the same so only
> 		 * first element of each capture needs to be compared */
> 		delta_ts = second_capture[0].timestamp - first_capture[0].timestamp;
> 		/* Compute deltas of timer register pair RA and RB.
> 		delta_ra = second_capture[0].value - first_capture[0].value;
> 		delta_rb = second_capture[1].value - first_capture[1].value;
>                  
> 		ra_frequency = (double)delta_ra / delta_ts;
> 		rb_frequency = (double)delta_rb / delta_ts;
> 		rb_ra = (double)delta_rb / delta_ra;
>                  
>              	printf("RA frequency: %ld\n"
> 		       "RB frequency: %ld\n"
> 		       "RB per RA: %ld\n"
>              	       ra_frequency, rb_frequency, rb_ra);
>              }
>              
>              return 0;
>      }
> 
> If RA and RB are provided as a memory buffer on your device, you can
> instead expose them via DEFINE_COUNTER_ARRAY_CAPTURE() such as the
> ti-ecap-capture driver does, then perform your userspace computations
> by utilizing those respective "capture" array attribute values (via
> chrdev like the example above or alternatively via sysfs).

Thanks for your extensive explanation! With 
DEFINE_COUNTER_ARRAY_CAPTURE() I was able to expose RA and RB as 
`/sys/bus/counter/devices/counter0/count0/capture{0,1}`, and could 
verify that by replacing `devmem` calls with read()-reopen(), our PoC 
code still works. Now I want to use the chardev interface, but I 
couldn't find how to set up the watches appropriately. So far I have:

	{
		.component.type = COUNTER_COMPONENT_EXTENSION,
		// also tried COUNTER_COMPONENT_COUNT
		.component.scope = COUNTER_SCOPE_COUNT,
		.component.parent = 0,
		.component.id = X, // also tried this instead:
		// .channel = X,
		.event = COUNTER_EVENT_CAPTURE,
	},

However, with this, the first read() never comes back.

Bence


  reply	other threads:[~2025-01-30 13:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-21 15:19 [Q] Frequency & duty cycle measurement? Csókás Bence
2025-01-21 17:45 ` David Lechner
2025-01-27 15:00 ` William Breathitt Gray
2025-01-30 13:59   ` Csókás Bence [this message]
2025-02-04 23:37     ` William Breathitt Gray
2025-02-05  9:30       ` Csókás Bence
2025-02-05  9:58         ` Csókás Bence
2025-02-19 22:32 ` Dipen Patel
2025-02-26 13:56   ` Csókás Bence

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=dffc0f18-799a-4fc7-a6b5-2fa270e1fc58@prolan.hu \
    --to=csokas.bence@prolan.hu \
    --cc=daniel.lezcano@linaro.org \
    --cc=dipenp@nvidia.com \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=timestamp@lists.linux.dev \
    --cc=wbg@kernel.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).