Fast PWM Frequency Due

I have an IC I need to supply 3 Square waves to that have frequencies of ~1 MHz, ~62500 kHz, and ~31250 kHz.

I'm unsure exactly how to adjust the frequency of the Due and the one library I found won't accurately get any where near what you want after 40 kHz. Setting it to 61 kHz is giving me like 330kHz on my oscilloscope.

Can the due do ~1 MHz square wave? I'd imagine a divider for 84 MHz should be possible but not finding anything helpful with my weak google-fu.

OK I can get up to ~2MHz with pretty good quality then either my oscilloscope is ringing terribly or the quality of the signal gets pretty terrible.

I don't really understand how it works and if someone could give me some insight it would be much appreciated.

#include "Arduino.h"

const int freq1 = (1 << 23);
const int freq2 = 31250;
const int pin = 6;

void setup() 
{
  // put your setup code here, to run once:
  pinMode(6,OUTPUT);
  analogWriteResolution(12);
  pmc_enable_periph_clk(PWM_INTERFACE_ID);
  //Configure Clocks
  PWMC_ConfigureClocks(freq1,freq2,VARIANT_MCK/8);
  //Configure Pin
  PIO_Configure(g_APinDescription[pin].pPort,
  g_APinDescription[pin].ulPinType,
  g_APinDescription[pin].ulPin,
  g_APinDescription[pin].ulPinConfiguration);
  //Set Pin to count off CLKA set at freq1
  int chan = g_APinDescription[pin].ulPWMChannel;
  PWMC_ConfigureChannel(PWM_INTERFACE,chan,PWM_CMR_CPRE_CLKA,0,0);
  PWMC_SetPeriod(PWM_INTERFACE,chan,80);
  PWMC_SetDutyCycle(PWM_INTERFACE,chan,40);
  PWMC_EnableChannel(PWM_INTERFACE,chan);
  //analogWrite(6,2048);
}

void loop() 
{
  // put your main code here, to run repeatedly:

}

If you adjust the PWMC_SetPeriod value and PWMC_SetDutyCycle Function you will get the desired affect. However, the PWMC_ConfigureClocks function is required also. Increasing the VARIANT_MCK value slows it down and decreasing it increases the value. If someone can figure out how those values relate to each other I'll write a library for due but the 30 min. I've spent figuring this out isn't enough yet and have to go do a bunch of stuff will check back in when I have more time.

I just don't get what freq1 & freq2 actually do, the rest makes sense to me.

Ok, I've figured it, out for myself I can get any value between 641 Hz - 21000000 MHz but anything above about 1 MHz looks fairly questionable and above about 2 MHz is pretty bad. Anything in the kHz range looks pretty great even 500 kHz or so. Someone with a better oscilloscope / setup can test if it is the chip or my setup.

I know this thing can also do below 1 Hz : / just have to figure out how and I'll make a library that will get you to whatever Hz that is requested.

#include "Arduino.h"

const int freq1 = 21000000;
const int freq2 = 21000000;
const int pin = 6;

const int minfreq1 = 641;

void SetPinFrequency(int pin,int frequency,int clock)
{
    //Configure Pin
  PIO_Configure(g_APinDescription[pin].pPort,
  g_APinDescription[pin].ulPinType,
  g_APinDescription[pin].ulPin,
  g_APinDescription[pin].ulPinConfiguration);
  //Set Pin to count off CLKA set at freq1
  int chan = g_APinDescription[pin].ulPWMChannel;
  int clk = clock == 1 ? PWM_CMR_CPRE_CLKA : PWM_CMR_CPRE_CLKB;
  PWMC_ConfigureChannel(PWM_INTERFACE,chan,clk,0,0);
  int divider = 42000000/frequency;
  PWMC_SetPeriod(PWM_INTERFACE,chan,divider);
  PWMC_SetDutyCycle(PWM_INTERFACE,chan,divider/2);
  PWMC_EnableChannel(PWM_INTERFACE,chan);
}

void setup() 
{
  // put your setup code here, to run once:
  pinMode(6,OUTPUT);
  analogWriteResolution(12);
  pmc_enable_periph_clk(PWM_INTERFACE_ID);
  //Configure Clocks
  PWMC_ConfigureClocks(freq1,freq2,2*freq1);

  SetPinFrequency(6,31250,1);
  SetPinFrequency(7,62500,2);
}

void loop() 
{
  // put your main code here, to run repeatedly:

}