Hello,
Today I ate quite a lot of ATMEL SAM3X datasheet and indeed, after focusing on this PWM subject for a couple of hours, we can do nice programming of PWMs on the Due !!
I diretly configured SAM3X registers, it may be "brutal" and sometimes unappropriate, anyway I put here some draft code, it could help someone :
// Let's say we want to configure PWM channel 0, PWML0 which is on pin34 Due board, and corresponds to the port C.2
// First the peripheral must be activated instead off the general GPIO
REG_PIOC_PDR = 4;
// Moreover it's peripheral B (not A), which means we have to change PIO_ABSR[2]
REG_PIOC_ABSR = REG_PIOC_ABSR | 4;
// Now concerning PWM settings...
// activation of the clock dedicated to PWM peripheral (id36), which is fifth bit of PMC_PCSR1
REG_PMC_PCER1 = REG_PMC_PCER1 | 16;
// activation of PWM_ENA register with channel 0
REG_PWM_ENA = REG_PWM_SR | 1;
// setting the period (which can take any 16 bit value...)
REG_PWM_CPRD0 = 5000;
// note : with this 5000 setting, we have thus 1/5000 resol, with a frequency of 16khz, if we use the defaut main clock : that's good
// Finally, setting the duty cycle to the value you want (between 0 and the period, of course)
REG_PWM_CDTY0 = 1000;
// And here we can check at the oscilloscope that we get a nice PWM on pin 34, with a period of 60usec... nice!
Now I have one question :
Why do the PWM pin are indicated on pin number 2 to 13 on arduino boards !! ?
It seems that the 8 true PWM channels are not these ones... for example here I used the channel 0, which can be found on pin 34, 35 and other pins...
And one other interesting remark :
We can actually reach very nice resolution, especially if we use low (or high) pwm commands. For example, the hardware I use with this pwm is highly non linear, and I need to be precise with low duty cycle.
As I don't really care about precise frequency (let's say anything between 8khz and 20khz is fine), I can modify the period as well as the duty cycle.
So, from the duty cycle 1/5000, I can go to 2/5000, but also I can use 1/4990, 1/4900, etc.
And it works well.
Hope this will be usefull for someone.