mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DynamicPwm.cpp Source File

DynamicPwm.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 /**
00027   * Class definition for DynamicPwm.
00028   *
00029   * This class addresses a few issues found in the underlying libraries.
00030   * This provides the ability for a neat, clean swap between PWM channels.
00031   */
00032 
00033 #include "MicroBitConfig.h"
00034 #include "DynamicPwm.h"
00035 #include "MicroBitPin.h"
00036 #include "ErrorNo.h"
00037 
00038 DynamicPwm* DynamicPwm::pwms[NO_PWMS] = { NULL, NULL, NULL };
00039 
00040 uint8_t DynamicPwm::lastUsed = NO_PWMS+1; //set it to out of range i.e. 4 so we know it hasn't been used yet.
00041 
00042 uint16_t DynamicPwm::sharedPeriod = 0; //set the shared period to an unknown state
00043 
00044 /**
00045   * Reassigns an already operational PWM channel to the given pin.
00046   *
00047   * @param pin The desired pin to begin a PWM wave.
00048   *
00049   * @param oldPin The pin to stop running a PWM wave.
00050   *
00051   * @param channel_number The GPIOTE channel being used to drive this PWM channel
00052   *
00053   * TODO: Merge into mbed, at a later date.
00054   */
00055 void gpiote_reinit(PinName pin, PinName oldPin, uint8_t channel_number)
00056 {
00057     // Connect GPIO input buffers and configure PWM_OUTPUT_PIN_NUMBER as an output.
00058     NRF_GPIO->PIN_CNF[pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
00059                             | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
00060                             | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
00061                             | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
00062                             | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
00063 
00064     NRF_GPIO->OUTCLR = (1 << oldPin);
00065     NRF_GPIO->OUTCLR = (1 << pin);
00066 
00067     /* Finally configure the channel as the caller expects. If OUTINIT works, the channel is configured properly.
00068        If it does not, the channel output inheritance sets the proper level. */
00069 
00070     NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
00071                                          ((uint32_t)pin << GPIOTE_CONFIG_PSEL_Pos) |
00072                                          ((uint32_t)GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos) |
00073                                          ((uint32_t)GPIOTE_CONFIG_OUTINIT_Low << GPIOTE_CONFIG_OUTINIT_Pos); // ((uint32_t)GPIOTE_CONFIG_OUTINIT_High <<
00074                                                                                                              // GPIOTE_CONFIG_OUTINIT_Pos);//
00075 
00076     /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
00077     __NOP();
00078     __NOP();
00079     __NOP();
00080 
00081     NRF_TIMER2->CC[channel_number] = 0;
00082 }
00083 
00084 /**
00085   * An internal constructor used when allocating a new DynamicPwm instance.
00086   *
00087   * @param pin the name of the pin for the pwm to target
00088   *
00089   * @param persistance the level of persistence for this pin PWM_PERSISTENCE_PERSISTENT (can not be replaced until freed, should only be used for system services really.)
00090   *                    or PWM_PERSISTENCE_TRANSIENT (can be replaced at any point if a channel is required.)
00091   */
00092 DynamicPwm::DynamicPwm(PinName pin, PwmPersistence persistence) : PwmOut(pin)
00093 {
00094     this->flags = persistence;
00095 }
00096 
00097 /**
00098   * Redirects the pwm channel to point at a different pin.
00099   *
00100   * @param pin the desired pin to output a PWM wave.
00101   *
00102   * @code
00103   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00104   * pwm->redirect(p0); // pwm is now produced on p0
00105   * @endcode
00106   */
00107 void DynamicPwm::redirect(PinName pin)
00108 {
00109     gpiote_reinit(pin, _pwm.pin, (uint8_t)_pwm.pwm);
00110     this->_pwm.pin = pin;
00111 }
00112 
00113 /**
00114   * Creates a new DynamicPwm instance, or reuses an existing instance that
00115   * has a persistence level of PWM_PERSISTENCE_TRANSIENT.
00116   *
00117   * @param pin the name of the pin for the pwm to target
00118   *
00119   * @param persistance the level of persistence for this pin PWM_PERSISTENCE_PERSISTENT (can not be replaced until freed, should only be used for system services really.)
00120   *                    or PWM_PERSISTENCE_TRANSIENT (can be replaced at any point if a channel is required.)
00121   *
00122   * @return a pointer to the first available free pwm channel - or the first one that can be reallocated. If
00123   *         no channels are available, NULL is returned.
00124   *
00125   * @code
00126   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00127   * @endcode
00128   */
00129 DynamicPwm* DynamicPwm::allocate(PinName pin, PwmPersistence persistence)
00130 {
00131     //try to find a blank spot first
00132     for(int i = 0; i < NO_PWMS; i++)
00133     {
00134         if(pwms[i] == NULL)
00135         {
00136             lastUsed = i;
00137             pwms[i] = new DynamicPwm(pin, persistence);
00138             return pwms[i];
00139         }
00140     }
00141 
00142     //no blank spot.. try to find a transient PWM
00143     int channelIterator = (lastUsed + 1 > NO_PWMS - 1) ? 0 : lastUsed + 1;
00144 
00145     while(channelIterator != lastUsed)
00146     {
00147         if(pwms[channelIterator]->flags & PWM_PERSISTENCE_TRANSIENT)
00148         {
00149             lastUsed = channelIterator;
00150             pwms[channelIterator]->flags = persistence;
00151             pwms[channelIterator]->redirect(pin);
00152             return pwms[channelIterator];
00153         }
00154 
00155         channelIterator = (channelIterator + 1 > NO_PWMS - 1) ? 0 : channelIterator + 1;
00156     }
00157 
00158     //if we haven't found a free one, we must try to allocate the last used...
00159     if(pwms[lastUsed]->flags & PWM_PERSISTENCE_TRANSIENT)
00160     {
00161         pwms[lastUsed]->flags = persistence;
00162         pwms[lastUsed]->redirect(pin);
00163         return pwms[lastUsed];
00164     }
00165 
00166     //well if we have no transient channels - we can't give any away! :( return null
00167     return (DynamicPwm*)NULL;
00168 }
00169 
00170 /**
00171   * Frees this DynamicPwm instance for reuse.
00172   *
00173   * @code
00174   * DynamicPwm* pwm = DynamicPwm::allocate();
00175   * pwm->release();
00176   * @endcode
00177   */
00178 void DynamicPwm::release()
00179 {
00180     //free the pwm instance.
00181     NRF_GPIOTE->CONFIG[(uint8_t) _pwm.pwm] = 0;
00182     pwmout_free(&_pwm);
00183     this->flags = PWM_PERSISTENCE_TRANSIENT;
00184 
00185     //set the pointer to this object to null...
00186     for(int i =0; i < NO_PWMS; i++)
00187         if(pwms[i] == this)
00188         {
00189             delete pwms[i];
00190             pwms[i] = NULL;
00191         }
00192 }
00193 
00194 /**
00195   * A lightweight wrapper around the super class' write in order to capture the value
00196   *
00197   * @param value the duty cycle percentage in floating point format.
00198   *
00199   * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range
00200   *
00201   * @code
00202   * DynamicPwm* pwm = DynamicPwm::allocate();
00203   * pwm->write(0.5);
00204   * @endcode
00205   */
00206 int DynamicPwm::write(float value){
00207 
00208     if(value < 0)
00209         return MICROBIT_INVALID_PARAMETER;
00210 
00211     PwmOut::write(value);
00212     lastValue = value;
00213 
00214     return MICROBIT_OK;
00215 }
00216 
00217 /**
00218   * Retreives the PinName associated with this DynamicPwm instance.
00219   *
00220   * @code
00221   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00222   *
00223   * // returns the PinName n.
00224   * pwm->getPinName();
00225   * @endcode
00226   *
00227   * @note This should be used to check that the DynamicPwm instance has not
00228   *       been reallocated for use in another part of a program.
00229   */
00230 PinName DynamicPwm::getPinName()
00231 {
00232     return _pwm.pin;
00233 }
00234 
00235 /**
00236   * Retreives the last value that has been written to this DynamicPwm instance.
00237   * in the range 0 - 1023 inclusive.
00238   *
00239   * @code
00240   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00241   * pwm->write(0.5);
00242   *
00243   * // will return 512.
00244   * pwm->getValue();
00245   * @endcode
00246   */
00247 int DynamicPwm::getValue()
00248 {
00249     return (float)lastValue * float(MICROBIT_PIN_MAX_OUTPUT);
00250 }
00251 
00252 /**
00253   * Retreives the current period in use by the entire PWM module in microseconds.
00254   *
00255   * Example:
00256   * @code
00257   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00258   * pwm->getPeriod();
00259   * @endcode
00260   */
00261 int DynamicPwm::getPeriodUs()
00262 {
00263     return sharedPeriod;
00264 }
00265 
00266 /**
00267   * Retreives the current period in use by the entire PWM module in milliseconds.
00268   *
00269   * Example:
00270   * @code
00271   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00272   * pwm->setPeriodUs(20000);
00273   *
00274   * // will return 20000
00275   * pwm->getPeriod();
00276   * @endcode
00277   */
00278 int DynamicPwm::getPeriod()
00279 {
00280     return getPeriodUs() / 1000;
00281 }
00282 
00283 /**
00284   * Sets the period used by the WHOLE PWM module.
00285   *
00286   * @param period the desired period in microseconds.
00287   *
00288   * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if period is out of range
00289   *
00290   * Example:
00291   * @code
00292   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00293   *
00294   * // period now is 20ms
00295   * pwm->setPeriodUs(20000);
00296   * @endcode
00297   *
00298   * @note Any changes to the period will AFFECT ALL CHANNELS.
00299   */
00300 int DynamicPwm::setPeriodUs(int period)
00301 {
00302     if(period < 0)
00303         return MICROBIT_INVALID_PARAMETER;
00304 
00305     //#HACK this forces mbed to update the pulse width calculation.
00306     period_us(period);
00307     write(lastValue);
00308     sharedPeriod = period;
00309 
00310     return MICROBIT_OK;
00311 }
00312 
00313 /**
00314   * Sets the period used by the WHOLE PWM module. Any changes to the period will AFFECT ALL CHANNELS.
00315   *
00316   * @param period the desired period in milliseconds.
00317   *
00318   * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if period is out of range
00319   *
00320   * Example:
00321   * @code
00322   * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00323   *
00324   * // period now is 20ms
00325   * pwm->setPeriod(20);
00326   * @endcode
00327   */
00328 int DynamicPwm::setPeriod(int period)
00329 {
00330     return setPeriodUs(period * 1000);
00331 }