6 Replies Latest reply on Oct 5, 2015 9:24 PM by cheyne

    PWM on eclipse with MRAA lib

    cheyne

      Hi,

       

      I am trying to generate a PWM signal on PWM0, GP12 using Edison mini-breakout board. Below is the code i have tested but no output is generated at J18 pin7. Please advise.

       

      #include <unistd.h>

      #include "mraa.h"

       

      int main()

      {

           mraa_init();

           mraa_pwm_context pwm;

       

           pwm = mraa_pwm_init_raw(0,0);

           mraa_pwm_period_us(pwm, 100);

           mraa_pwm_pulsewidth_us(pwm, 10);

           mraa_pwm_enable(pwm, 1);

           while(1);

      }

        • 1. Re: PWM on eclipse with MRAA lib
          Intel_Peter

          Hello cheyne,

           

          I've been trying to do this, I used a very similar script but with a slight difference:

           

          #include <unistd.h>
          #include "mraa.h"
          int main ()
          {
              mraa_init();
              mraa_pwm_context pwm;
              pwm = mraa_pwm_init_raw(0,0);
              if (pwm == NULL)
              {
                  return 1;
              }
              mraa_pwm_period(pwm, 0.04f);
              mraa_pwm_enable(pwm, 1);
              while (1)
              {
                  mraa_pwm_write(pwm, 0.5);
              }
          }
          

           

          I tried with several pins as specified in mraa/edison.md at master · intel-iot-devkit/mraa · GitHub, but when I tried, let's say, pin 14 (PWM1), 'pwm' was detected as 'NULL' and the script closed. When I tried pin 0 (PWM2) I had the same output as you. I was not able to see anything on J17-1.

          arfoll, do you have any thoughts?

           

          Peter.

          • 2. Re: PWM on eclipse with MRAA lib
            cheyne

            So far nobody encounter this problem before? Could it be the PWM's power not on? Or should I configure the GPIO pin to output before configure to funtion as PWM?

            • 3. Re: PWM on eclipse with MRAA lib
              digibake

              hey mate,

               

              There are some examples for different languages in the intel-iot-devkit/mraa library:

              mraa/cycle-pwm3.c at master · intel-iot-devkit/mraa · GitHub

               

              The C example is:

               

              #include <unistd.h>
              
              #include "mraa.h"
              
              int
              main()
              {
                mraa_init();
                //! [Interesting]
                mraa_pwm_context pwm;
                pwm = mraa_pwm_init(20);
                if (pwm == NULL) {
                return 1;
                }
                mraa_pwm_period_us(pwm, 200);
                mraa_pwm_enable(pwm, 1);
              
                float value = 0.0f;
              
                while (1) {
                value = value + 0.01f;
                mraa_pwm_write(pwm, value);
                usleep(50000);
                if (value >= 1.0f) {
                value = 0.0f;
                }
                float output = mraa_pwm_read(pwm);
                }
                //! [Interesting]
                return 0;
              }
              

               

              According to this table: PWM0 is MRAA number 20 so...

              pwm = mraa_pwm_init(20);

               

              Slight disclaimer: I haven't tried the code above, sorry, but hopefully it works or it can help in some way.

              • 4. Re: PWM on eclipse with MRAA lib
                cheyne

                Hi digibake,

                 

                It works after using pwm = mraa_pwm_init(20); However, what's wrong with this line pwm = mraa_pwm_init_raw(0,0), since I am using pwmchip0 and PWM0?

                • 5. Re: PWM on eclipse with MRAA lib
                  arfoll

                  Mraa's 'raw' mode is indeed used in that way, however edison's IO is heavily muxed so in order to use a pwm pin you need to set the muxes up correctly. This is what mraa does in the platform configuration when you request pwm pin '20'. For example the following function is done on init of a pwm pin which enables the output pin as well as setting generic muxes in mraa_pwm_init:

                  https://github.com/intel-iot-devkit/mraa/blob/master/src/x86/intel_edison_fab_c.c#L353.

                   

                  Testing this is easy just do the following:

                   

                  pwm_enable = mraa_pwm_init(20);
                  pwm_handle = mraa_pwm_init_raw(0, 0);
                  
                  

                   

                  Now using pwm_handle or pwm_enable will work. Obviously doing so is a bit pointless .

                  • 6. Re: PWM on eclipse with MRAA lib
                    cheyne

                    Thanks for the clear explanation.