PWM Write Protect

Hello

Having abit of trouble setting up the pwm controller, can't seem to turn the write protection off. I've tried all sorts of ways to set the PWM_WPCR registry but not luck, any ideas?

int period = 64000;

void setup() {
REG_PIOC_PDR = 0x3FC;
REG_PIOC_ABSR = REG_PIOC_ABSR | 0x3FC;

REG_PMC_PCER1 = REG_PMC_PCER1 | 16;

REG_PWM_WPCR = 0x50574D3C;   ///////////////////////////////////////////////

REG_PWM_CMR0 = PWM_CMR_CPRE_MCK;
REG_PWM_CMR1 = PWM_CMR_CPRE_MCK_DIV_8;
REG_PWM_CMR2 = PWM_CMR_CPRE_MCK_DIV_64;
REG_PWM_CMR3 = PWM_CMR_CPRE_MCK_DIV_1024;
REG_PWM_CMR0 = PWM_CMR_CALG;
REG_PWM_CMR1 = PWM_CMR_CALG;
REG_PWM_CMR2 = PWM_CMR_CPOL;
REG_PWM_CMR3 = PWM_CMR_CPOL;

REG_PWM_CPRD0 = period;
REG_PWM_CPRD1 = period;                   
REG_PWM_CPRD2 = period;
REG_PWM_CPRD3 = period;
REG_PWM_CDTY0 = period/2;
REG_PWM_CDTY1 = period/2;
REG_PWM_CDTY2 = period/2;
REG_PWM_CDTY3 = period/2;

REG_PWM_ENA = REG_PWM_SR | B1111;

}

void loop() {
}

Thanks Rufe0

This doesn’t work either

REG_PWM_WPCR = (0<<0)|(0<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(0<<9)|(1<<10)|(1<<11)|(0<<12)|(0<<13)|(1<<14)|(0<<15)|(1<<16)|(1<<17)|(1<<18)|(0<<19)|(1<<20)|(0<<21)|(1<<22)|(0<<23)|(0<<24)|(0<<25)|(0<<26)|(0<<27)|(1<<28)|(0<<29)|(1<<30)|(0<<31);

What make you think, that it's locked?
I have programmed PWM using this code:
http://www.jaxcoder.com/Article/SinglePost?postID=1976469335
There is no writing to WP register.
Have you set a pin as digital output? Or access correct pin?

Magician:
What make you think, that it's locked?
I have programmed PWM using this code:
http://www.jaxcoder.com/Article/SinglePost?postID=1976469335
There is no writing to WP register.
Have you set a pin as digital output? Or access correct pin?

I'm using pins 35 - 45, I can get some output however it's fixed at the standard settings.
I can set the period and duty cycle and get the correct result on the pins. However when I try to set CPRE_MCK prescaler, CMR_CALG or CMR_CPOL options there is no effect on the output. Either i'm not setting these settings correctly somehow or the write protection is enabled. In the datasheet it says you must disable the write protection to change these settings.

I was not using those features CALG & CPOL, but have no issue to set PWM clock. Probably, you are overwriting settings, try:

REG_PWM_CMR0 = PWM_CMR_CPRE_MCK;
REG_PWM_CMR1 = PWM_CMR_CPRE_MCK_DIV_8;
REG_PWM_CMR2 = PWM_CMR_CPRE_MCK_DIV_64;
REG_PWM_CMR3 = PWM_CMR_CPRE_MCK_DIV_1024;
REG_PWM_CMR0 |= PWM_CMR_CALG;
REG_PWM_CMR1 |= PWM_CMR_CALG;
REG_PWM_CMR2 |= PWM_CMR_CPOL;
REG_PWM_CMR3 |= PWM_CMR_CPOL;

Yeah that was it, can't believe it all this time it was something so small. Sure I had tried just one of the things at a time so that couldn't be it but obviously not...

Thanks a lot!

You are welcome.
I had this error a few times myself, later on I take as a rule to print out register content over serial, so I have a full perspective what is going on, bits were not set or were overwritten by some arduino initialization function. Most registers support read-write. As an example, my "battle" with timer synchronization:

    in_Byte = Serial.read();
    if (in_Byte == 'd') { //debug         
       TC0->TC_CHANNEL[0].TC_CCR |= TC_CCR_SWTRG;
       TC0->TC_CHANNEL[1].TC_CCR |= TC_CCR_SWTRG;
//       delayMicroseconds(10);    
       reg_val1 = TC0->TC_CHANNEL[0].TC_CV;
       reg_val2 = TC0->TC_CHANNEL[1].TC_CV;
       Serial.print("\n\tposle SWTRG i BEZ zaderzhki (0) t->TC_CV = ");
       Serial.println(reg_val1, DEC);  
       Serial.print("\n\tposle SWTRG i BEZ zaderzhki (1) t->TC_CV = ");
       Serial.println(reg_val2, DEC);  

       reg = 0;
       TC0->TC_CHANNEL[0].TC_CV = reg;
       TC0->TC_CHANNEL[1].TC_CV = reg;

       reg_val1 = TC_BCR_SYNC;     
       TC0->TC_BCR = reg_val1; 


       REG_TC0_CCR0 |= TC_CCR_SWTRG;
       REG_TC0_CCR1 |= TC_CCR_SWTRG;
       delayMicroseconds(10);    
       reg_val1 = REG_TC0_CV0;
       reg_val2 = REG_TC0_CV1;
       Serial.print("\n\tposle SWTRG i 10us zaderzhki (0) t->TC_CV = ");
       Serial.println(reg_val1, DEC);  
       Serial.print("\n\tposle SWTRG i 10us zaderzhki (1) t->TC_CV = ");
       Serial.println(reg_val2, DEC);  


       reg_val1 = TC_BCR_SYNC;     
       REG_TC0_BCR = reg_val1; 
       delayMicroseconds(10);    
       reg_val1 = REG_TC0_CV0;
       reg_val2 = REG_TC0_CV1;
       Serial.print("\n\tposle BMR_SYNC i 10us zaderzhki (0) t->TC_CV = ");
       Serial.println(reg_val1, DEC);  
       Serial.print("\n\tposle BMR_SYNC i 10us zaderzhki (1) t->TC_CV = ");
       Serial.println(reg_val2, DEC);  
      }