Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: DtPWM.cpp
- Revision:
- 4:a490a36f1cb1
- Parent:
- 3:d7b9697768d8
--- a/DtPWM.cpp Tue Apr 04 22:20:38 2017 +0000 +++ b/DtPWM.cpp Fri Apr 07 08:43:16 2017 +0000 @@ -1,16 +1,13 @@ #include "DtPWM.h" DtPWM::DtPWM(bool enabled){ + + LEDs = false; //LEDs are off on default + LPC_SC->PCONP|=(1<<6); //enable power on PWM1 LPC_SC->PCLKSEL0|=(1<<12); //run PWM1 clock at prescaler 1 (96MHz) - /* enable LED output as well - LPC_PINCON->PINSEL3|=(2<<14) | (2<<8); - LPC_PINCON->PINMODE3|=(2<<14) | (2<<8); - */ - - LPC_PWM1->MCR|=(1<<1); //no interrupt, no reset, no stop at TC compare match @@ -49,42 +46,139 @@ } +void DtPWM::setGPIO(char channel, bool high) { + + // set GPIO on these pins to low + + uint8_t mask = 0; + if( channel == 'h' || channel == 'b' || channel == 'a' ) + mask |= (1 << 1); // P2.1 + if( channel == 'l' || channel == 'b' || channel == 'a' ) + mask |= (1 << 3); // P2.3 + if( channel == 't' || channel == 'a') + mask |= (1 << 4); // P2.4 + + LPC_GPIO2->FIODIR0 |= mask; // set pins as outputs + LPC_GPIO2->FIOMASK0 &= ~mask; // clear the mask bits + + if( high ) { + LPC_GPIO2->FIOSET0 |= mask; // set pins to high + } else { + LPC_GPIO2->FIOCLR0 |= mask; // set pins to low + } + +} + +void DtPWM::setPINS(char channel, uint32_t function) { + uint32_t mask = 0; + if( channel == 'h' || channel == 'b' || channel == 'a' ) + mask |= (0x3 << 2); // P2.1 + if( channel == 'l' || channel == 'b' || channel == 'a' ) + mask |= (0x3 << 6); // P2.3 + if( channel == 't' || channel == 'a') + mask |= (0x3 << 8); // P2.4 + + LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~mask) | (function & mask); + + // set pins to 10, no pull up / pull down + LPC_PINCON->PINMODE4 = (LPC_PINCON->PINMODE4 & ~mask) | (FUNC_10 & mask); +} + void DtPWM::enable(char channel) { - LPC_PINCON->PINSEL4 |= (1<<6) | (1<<2); //PWM on channel 2 and 4 (GPIO P2.1, P2.3) - LPC_PINCON->PINMODE4 |= (2<<6) | (2<<2); //no pull ups/pull downs + // set pins to PWM function (01) + setPINS(channel, FUNC_01); - if( channel == 'h' ) { - LPC_PWM1->PCR &= ~(1<<12); // disable PWM output 4 + // turn high side channel on (PWM1.2) + if( channel == 'h' || channel == 'b' || channel == 'a' ) { LPC_PWM1->PCR |= (1<<10); // enable PWM output 2 + } + // turn low side channel on (PWM1.4) + if( channel == 'l' || channel == 'b' || channel == 'a' ) { + LPC_PWM1->PCR |= (1<<12); // enable PWM output 2 } - else if( channel == 'l' ) { - LPC_PWM1->PCR &= ~(1<<10); // disable PWM output 2 - LPC_PWM1->PCR |= (1<<12); // enable PWM output 4 + // turn trigger channel on (PWM1.5); + if( channel == 't' || channel == 'a') { + LPC_PWM1->PCR |= (1<<13); // enable PWM output 5 } - else if( channel == 'a' ) { - LPC_PWM1->PCR |= (1<<12) | (1<<10); // enable PWM output 2 & 4; - } - - // set PGIO on these pins to low for fast PWM turn off - LPC_GPIO2->FIODIR0 |= (1 << 1) | (1 << 3); // set P2.1 & P2.3 as outputs - LPC_GPIO2->FIOCLR0 |= (1 << 1) | (1 << 3); // set P2.1 & P2.3 to 0 - + LPC_PWM1->TCR|=(1<<0); //enable counter } -void DtPWM::disable() { +void DtPWM::setOutput(char channel, bool high) { + setPINS( channel, FUNC_00 ); + setGPIO( channel, high ); +} +void DtPWM::setLEDs( bool on ) { + + uint32_t mask = (0x3<<14) | (0x3<<8); + + if( on && !LEDs ) { - LPC_PINCON->PINSEL4|=(0<<6) | (0<<2); //GPIO on channel 2 and 4 (GPIO P2.1, P2.3) - LPC_PINCON->PINMODE4|=(3<<6) | (3<<2); //pull them down + LEDs_pinsel = LPC_PINCON->PINSEL3; + + /* set ports P1.20 to PWM1.2 and P1.23 to PWM1.2 (LED2 & 4) */ + LPC_PINCON->PINSEL3 &= ~mask; // reset bits + LPC_PINCON->PINSEL3 |= (0x2<<14) | (0x2<<8); // set functions 10 (PWM) + + LEDs_pinmode = LPC_PINCON->PINMODE3; + LPC_PINCON->PINMODE3 &= ~mask; // reset bits + LPC_PINCON->PINMODE3 |= (2<<14) | (2<<8); // no pull ups/pull downs + + LEDs = true; + } + if ( !on && LEDs ) { + /* reset pinsel and pinmode registers */ + LPC_PINCON->PINSEL3 &= ~mask; // reset bits + LPC_PINCON->PINSEL3 |= LEDs_pinsel & mask; + + LPC_PINCON->PINMODE3 &= ~mask; // reset bits + LPC_PINCON->PINMODE3 |= LEDs_pinmode & mask; + + LEDs = false; + } +} + +int DtPWM::isEnabled(char channel) { - LPC_GPIO2->FIOCLR0 |= (1 << 1) | (1 << 3); // set P2.1 & P2.3 to 0 (which they should be already) - LPC_GPIO2->FIODIR0 |= (1 << 1) | (1 << 3); // set P2.1 & P2.3 as outputs (which they should be already) + if( channel == 'h' ) { + return LPC_PWM1->PCR & (1<<10); // check PWM output 2 + } + else if( channel == 'l' ) { + return LPC_PWM1->PCR & (1<<12); // check PWM output 4 + } + else if( channel == 'b' ) { + return LPC_PWM1->PCR & ( (1<<12) | (1<<10) ); // check PWM output 2 and 4 + } + else if( channel == 't' ) { + return LPC_PWM1->PCR & (1<<13); // check PWM output 5 + } + else if( channel == 'a' ) { + return LPC_PWM1->PCR & (0x3F<<9); // return all enable bits + } - LPC_PWM1->PCR&= ~((1<<12) | (1<<10)); // disable PWM output 2 & 4; + return -1; +} + +void DtPWM::disable(char channel) { + - LPC_PWM1->TCR&= ~(1<<0); //disable counter - + // turn high side channel off (PWM1.2) + if( channel == 'h' || channel == 'b' || channel == 'a' ) { + LPC_PWM1->PCR &= ~(1<<10); // disable PWM output 2 + } + // turn low side channel off (PWM1.4) + if( channel == 'l' || channel == 'b' || channel == 'a' ) { + LPC_PWM1->PCR &= ~(1<<12); // disable PWM output 2 + } + // turn trigger channel off (PWM1.5); + if( channel == 't' || channel == 'a') { + LPC_PWM1->PCR &= ~(1<<13); // disable PWM output 5 + } + + setGPIO( channel, 0 ); // set the gpio to low + setPINS( channel, FUNC_00 ); + } void DtPWM::setInterrupts(uint32_t flags, int prio){