joan2937 / pigpio

pigpio is a C library for the Raspberry which allows control of the General Purpose Input Outputs (GPIO).
The Unlicense
1.43k stars 403 forks source link

more detailed documentation required about gpioWaveCreatePad #575

Closed audi0615 closed 1 year ago

audi0615 commented 1 year ago

Hi.

I understand that gpioWaveCreatePad has been newly added for continuous filling and transmitting of waves by padding out waves to have the same size for the re-use of resources.

However, I believe that a more detailed explanation is needed for the following:

Q1. Can wave chunks for filling and transmitting have different delays (frequency of pulses) and numbers of pulses?

Q2. How do you determine pctCB and pctBOOL?

pctCB: 0-100, the percent of all DMA control blocks to consume.
pctBOOL: 0-100, percent On-Off-Level (OOL) buffer to consume for wave output.
pctTOOL: 0-100, the percent of OOL buffer to consume for wave input (flags).

In the following example, let's assume that nextWaveChunk has 10,000 pulses. Would setting pctCB and pctBOOL to 50% be sufficient? How can I determine the appropriate values for these parameters?

 // get firstWaveChunk, somehow
  gpioWaveAddGeneric(firstWaveChunk);
  wid = gpioWaveCreatePad(50, 50, 0);
  gpioWaveTxSend(wid, PI_WAVE_MODE_ONE_SHOT);
  // get nextWaveChunk

  while (nextWaveChunk) {
     gpioWaveAddGeneric(nextWaveChunk);
     nextWid = gpioWaveCreatePad(50, 50, 0);
     gpioWaveTxSend(nextWid, PI_WAVE_MODE_ONE_SHOT_SYNC);
     while(gpioWaveTxAt() == wid) time_sleep(0.1);
     gpioWaveDelete(wid);
     wid = nextWid;
     // get nextWaveChunk
  }
guymcswain commented 1 year ago

Q1: Different delays? Yes, that was the original intention, as I recall. You could change the number of pulses as long as the chunk fit within the allocated resources.

Q2: In general, 50% is the logical choice - it splits all available resources so that the next chunk can be computed while the current chunk is transmitting. If your chunk of 10000 pulses does not fit within 50%, you will need to adjust your chunk size.

Why this parameter? There was some concern about exhausting one of the “OOL”s when doing bit-banged SPI operation simultaneously (if I recall correctly). That situation was never tested as it seemed to be a fairly unusual use case.

audi0615 commented 1 year ago

Thank you guymcswain for your reply. regarding Q2, how do i know a certain number of pulses are fit within 50% of the available resources? Is this related to the hard limit of number of pulses(12,000)? so 6,000 pulses would be ok with 50%?

guymcswain commented 1 year ago

It’s a bit more complicated than hard number limits so you need to use the API’s that give you the available CB, and other statistics. After you create the first chunk you can verify you are within 50% of the total. I suppose a more complete example including this would have improved the doc.

A good strategy is to pick a chunk size that provides enough total delay to assure you have time to compute the next before the transmission of the previous chunk expires. In the use cases I originally considered, I was never anywhere close to needing 50% of the available resources. But, one can never imagine all applications.

guymcswain commented 1 year ago

I just remembered that a wave in pigpio is really a container for multiple “wavelets” - or waves on different gpio. Each additional wavelet will consume more resource.

audi0615 commented 1 year ago

Thank you so much guymcswain. Now everything is very clear. I would start with 3,000 pulses for each wave chunk to control 3 stepper motors simultaneously.

Thanks again.