0

I combine C examples of pigpio for gpioSetAlertFunc and gpioWaveTxSend working with the same pin. My PWM has several pulses, so I expect alert function to print out a message for me several times. Sometimes it happens, but sometimes I run the program, but alert function is not called.

      ```
      int res=gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT);
      ```

...and res gets correct value (no error).

So what might be a reason, that alert function is not called?

I add code snippets to clarify:

      //includes

      static int counter_edge=0;
      void countEdge(int gpio, int level, uint32_t tick)
      {
         counter_edge++;
         printf("GPIO %d became %d at %u\n", gpio, level, tick);
      }

      //the function follows example from pigpio
      int send_pwm(uint32_t pin)
      {
         counter_edge = 0;

         gpioCfgClock(1,0,-1);

         if (gpioInitialise() < 0)
         {
            fprintf(stderr, "pigpio initialisation failed\n");
            return 1;
         }

         //!!! alert function assigned to the pin
         //!!! call countEdge whenever GPIO changes state
         gpioSetAlertFunc(pin, countEdge);

         // Construct and send a 30 microsecond square wave.
         gpioSetMode(pin, PI_OUTPUT);

         const int pulse_size=16;
         gpioPulse_t pulse[pulse_size];
         uint32_t try_send_cnt=0,crc_pwm;

         uint32_t i=0;
         uint32_t interval=10;  //us microseconds
         uint32_t gpio_pin = (1<<pin);
         crc_pwm=0;

         for(i=0;i<pulse_size;i++){
           if(i%2)
           {
             pulse[i].gpioOn = gpio_pin;
             pulse[i].gpioOff = 0;
             pulse[i].usDelay = interval;
           }
           else
           {
             pulse[i].gpioOn = 0;
             pulse[i].gpioOff = gpio_pin;
             pulse[i].usDelay = interval;
           }
         }

         crc_pwm=0;
         for(i=1;i<pulse_size;i++)
           if(pulse[i].gpioOn!=pulse[i-1].gpioOn)
              crc_pwm++;
         // alert function should be triggered crc_pwm times 

         if (wave_id >= 0)
         {
           for(try_send_cnt=0;try_send_cnt<20;try_send_cnt++)
           {
              counter_edge = 0;
              res_pwm=gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT);

              if(counter_edge==crc_pwm)
                  break;
            }

            if(res_pwm==PI_BAD_WAVE_ID||res_pwm==PI_BAD_WAVE_MODE)
              printf("ERROR: failure in PWM\n");

            if(counter_edge!=crc_pwm)
               printf("ERROR: Pwm failed after several attemps (%d           %d)\n",counter_edge,crc_pwm);
            else
               printf("DONE %d %d\n",counter_edge,crc_pwm);
         }
         gpioTerminate();
      }

      //tests in main
      int main(){
         send_pwm(17);
         send_pwm(18);

         return 0;
      }

as a result I observe a message like "GPIO 17 became 1" only sometimes.

  • 1
    Hi Alexander, and welcome to Stack Exchange. To better help you out with your problem, we may need to see more of your code. How are you configuring the alert? Whenever your code does work, do you know if you get an alert each time your GPIO pin changes state? – David Apr 2 at 22:31
  • Hi David, I added my code snippets – Alexander Starostin Apr 3 at 21:04
0

From the pigpio website:

Level changes shorter than the sample rate may be missed.

The thread which calls the alert functions is triggered nominally 1000 times per second. The active alert functions will be called once per level change since the last time the thread was activated. i.e. The active alert functions will get all level changes but there will be a latency.

Perhaps you're running your PWM signal at a frequency that is too high. My suggestion is that you start by reducing your PWM frequency with gpioSetPWMfrequency.

  • Thank you for the remark. Looks, like it brings me to the limit of pwm frequency for pin. I cannot achieve frequency for pin as in my sample rate. I set my sample rate to 1us, which correponds to 1MHz. gpioCfgClock(1,0,-1); but when I am trying to set 1MHz for the pin, it cannot do it. int specFreq = 1000000; gpioSetPWMfrequency(pin, specFreq); printf("freq: %d asked %d\n",gpioGetPWMfrequency(pin), specFreq); This code gives me "freq: 1000000 asked 40000" According to the documentation 40000Hz is really the highest value – Alexander Starostin Apr 4 at 8:20
  • Probably I need to post another question. How could I check gpioWaveTxSend, at the high frequency without an additional hardware? – Alexander Starostin Apr 4 at 8:21
  • @AlexanderStarostin, I do not understand what is the problem you are describing now. Please clarify your last two comments. Furthermore, why are you trying to increase the frequency here? I suggested you lower it. – David Apr 5 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.