I have a Mega 2560 clone that seems to work fine with the ChibiOS demo. But when I run the PWM hal test, the light just blinks quickly. It looks like maybe the PWM frequency is set too low. Changing the values in pwmEnableChannel makes it blink faster or slower, but it's not the fade I would expect with PWM.
Is there some difference in the 2560 timers vs the 1280 timers that would account for this behavior?
Thanks,
Mike
PWM frequency
-
- Posts: 359
- Joined: Sat Jan 07, 2012 6:22 pm
- Location: Brazil
- Has thanked: 1 time
- Been thanked: 20 times
- Contact:
Re: PWM frequency
Hi Mike,
Changing frequency is not supported in the driver AFAIK. I guess the AVR timers are quite limited in what you can really achieve because you need to configure a prescaler that support only a handfull of options. And I remember checking the width in enableChannel with an oscilloscope and it seems to be correct.
Cheers,
Fabio Utzig
Changing frequency is not supported in the driver AFAIK. I guess the AVR timers are quite limited in what you can really achieve because you need to configure a prescaler that support only a handfull of options. And I remember checking the width in enableChannel with an oscilloscope and it seems to be correct.
Cheers,
Fabio Utzig
-
- Posts: 28
- Joined: Tue Mar 10, 2015 4:49 pm
- Been thanked: 1 time
Re: PWM frequency
Hi Fabio,
I went into pwm_lld.c and changed the frequency scalar to 256 instead of 1024. It seems to be working now.
When I set it to 8 the LED seemed to be at 100% no matter what the duty cycle was set to, thought it's possible I messed up and wasn't actually sending 010 to the register.
Thanks,
Mike
I went into pwm_lld.c and changed the frequency scalar to 256 instead of 1024. It seems to be working now.
When I set it to 8 the LED seemed to be at 100% no matter what the duty cycle was set to, thought it's possible I messed up and wasn't actually sending 010 to the register.
Thanks,
Mike
-
- Posts: 28
- Joined: Tue Mar 10, 2015 4:49 pm
- Been thanked: 1 time
Re: PWM frequency
Saying PWM worked may have been a bit premature.
After noticing that I couldn't adjust the brightness of the LED.
What I've found is that calling: seems to cause constant resets.
I pulled the PWM code from testhal into the RT-MEGA demo and replaced pwmTogglePad with a pwmEnableChannel and a simple counter, but it never makes it there.
With pwmStart commented out the test thread works and the LED is at full brightness whether I call pwmEnableChannel or not. If I uncomment pwmStart the LED stays at its initial brightness setting and I see:
*** Kernel: 3.0.0
*** ChibiOS/RT test suite
***
repeated endlessly in the serial window.
After noticing that I couldn't adjust the brightness of the LED.
What I've found is that calling:
Code: Select all
pwmStart(&PWMD1, &pwm1cfg);
I pulled the PWM code from testhal into the RT-MEGA demo and replaced pwmTogglePad with a pwmEnableChannel and a simple counter, but it never makes it there.
With pwmStart commented out the test thread works and the LED is at full brightness whether I call pwmEnableChannel or not. If I uncomment pwmStart the LED stays at its initial brightness setting and I see:
*** Kernel: 3.0.0
*** ChibiOS/RT test suite
***
repeated endlessly in the serial window.
-
- Posts: 359
- Joined: Sat Jan 07, 2012 6:22 pm
- Location: Brazil
- Has thanked: 1 time
- Been thanked: 20 times
- Contact:
Re: PWM frequency
The freerunning demo is probably using the same timer that the PWM driver (TIM1).
You can either change the PWM timer defines in mcuconf to:
and use PWMD3, or more easily, enable periodic timer for the system timer (which uses TIM0) changing these three values in chconf.h:
Cheers,
Fabio Utzig
You can either change the PWM timer defines in mcuconf to:
Code: Select all
#define AVR_PWM_USE_TIM1 FALSE
#define AVR_PWM_USE_TIM2 FALSE
#define AVR_PWM_USE_TIM3 TRUE
#define AVR_PWM_USE_TIM4 FALSE
#define AVR_PWM_USE_TIM5 FALSE
and use PWMD3, or more easily, enable periodic timer for the system timer (which uses TIM0) changing these three values in chconf.h:
Code: Select all
#define CH_CFG_ST_FREQUENCY 1000
#define CH_CFG_ST_TIMEDELTA 0
#define CH_CFG_TIME_QUANTUM 20
Cheers,
Fabio Utzig
-
- Posts: 28
- Joined: Tue Mar 10, 2015 4:49 pm
- Been thanked: 1 time
Re: PWM frequency
Hi Fabio,
I'm still having trouble.
I start with a clean copy of the PWM program from testhal and change:
to TRUE in mcuconf.h
Then I added: just above the while(1){}.
If I comment out pwmStart, I see "Hello world" printed one time as expected, but with pwmStart active, I see "Hello world repeated endlessly.
That is the only change I have made to the program.
Thanks,
Mike
I'm still having trouble.
I start with a clean copy of the PWM program from testhal and change:
Code: Select all
AVR_SERIAL_USE_USART0
Then I added:
Code: Select all
chnWrite(&SD1, (const uint8_t *) "Hello world\r\n", 13);
If I comment out pwmStart, I see "Hello world" printed one time as expected, but with pwmStart active, I see "Hello world repeated endlessly.
That is the only change I have made to the program.
Thanks,
Mike
-
- Posts: 28
- Joined: Tue Mar 10, 2015 4:49 pm
- Been thanked: 1 time
Re: PWM frequency
Thanks for the help Fabio.
I zipped my project folder and included the slightly modified board.h from os/hal/boards/ARDUINO_MEGA/
I have it set up for tickless mode right now so I can use the led that's on the board rather than dealing with breadboards etc.
Thanks,
Mike
I zipped my project folder and included the slightly modified board.h from os/hal/boards/ARDUINO_MEGA/
I have it set up for tickless mode right now so I can use the led that's on the board rather than dealing with breadboards etc.
Thanks,
Mike
- Attachments
-
- Mega.zip
- Single thread with PWM
- (14.69 KiB) Downloaded 221 times
-
- Posts: 359
- Joined: Sat Jan 07, 2012 6:22 pm
- Location: Brazil
- Has thanked: 1 time
- Been thanked: 20 times
- Contact:
Re: PWM frequency
Hi Mike,
That was really a bug in the PWM driver. What happened was that timer interrupts where enabled even without a callback. I did a simple check but a more thorough review should be done, maybe add checks to all interrupt handlers too. Anyway it should be working if you update to the last revision.
Also some small suggestions, set AVR_PWM_USE_TIM2 to FALSE in mcuconf.h (because you don't need it) and comment out the line:
in your Makefile because for that will drop your flash usage quite a lot.
Cheers,
Fabio Utzig
That was really a bug in the PWM driver. What happened was that timer interrupts where enabled even without a callback. I did a simple check but a more thorough review should be done, maybe add checks to all interrupt handlers too. Anyway it should be working if you update to the last revision.
Also some small suggestions, set AVR_PWM_USE_TIM2 to FALSE in mcuconf.h (because you don't need it) and comment out the line:
Code: Select all
include $(CHIBIOS)/test/rt/test.mk
in your Makefile because for that will drop your flash usage quite a lot.
Cheers,
Fabio Utzig
-
- Posts: 28
- Joined: Tue Mar 10, 2015 4:49 pm
- Been thanked: 1 time
Re: PWM frequency
Hi Fabio,
Thanks for the fix and the tips.
I'll test out your changes as soon as they hit github.
Mike
Thanks for the fix and the tips.
I'll test out your changes as soon as they hit github.
Mike
Who is online
Users browsing this forum: No registered users and 15 guests