Community
cancel
Showing results for 
Search instead for 
Did you mean: 
CWong12
New Contributor I
1,257 Views

PWM on eclipse with MRAA lib

Jump to solution

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

# 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 Solution
ABake3
Novice
98 Views

hey mate,

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

https://github.com/intel-iot-devkit/mraa/blob/master/examples/cycle-pwm3.c mraa/cycle-pwm3.c at master · intel-iot-devkit/mraa · GitHub

The C example is:

# include

# 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 http://iotdk.intel.com/docs/master/mraa/edison.html 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.

View solution in original post

6 Replies
Pedro_M_Intel
Employee
98 Views

Hello cheyne,

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

# include

# 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 https://github.com/intel-iot-devkit/mraa/blob/master/docs/edison.md 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.

CWong12
New Contributor I
98 Views

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?

ABake3
Novice
99 Views

hey mate,

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

https://github.com/intel-iot-devkit/mraa/blob/master/examples/cycle-pwm3.c mraa/cycle-pwm3.c at master · intel-iot-devkit/mraa · GitHub

The C example is:

# include

# 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 http://iotdk.intel.com/docs/master/mraa/edison.html 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.

View solution in original post

CWong12
New Contributor I
98 Views

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?

Brendan_L_Intel
Employee
98 Views

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 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 .

CWong12
New Contributor I
98 Views

Thanks for the clear explanation.

Reply