Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /*
MrBedfordVan 0:b9164b348919 2 The MIT License (MIT)
MrBedfordVan 0:b9164b348919 3
MrBedfordVan 0:b9164b348919 4 Copyright (c) 2016 British Broadcasting Corporation.
MrBedfordVan 0:b9164b348919 5 This software is provided by Lancaster University by arrangement with the BBC.
MrBedfordVan 0:b9164b348919 6
MrBedfordVan 0:b9164b348919 7 Permission is hereby granted, free of charge, to any person obtaining a
MrBedfordVan 0:b9164b348919 8 copy of this software and associated documentation files (the "Software"),
MrBedfordVan 0:b9164b348919 9 to deal in the Software without restriction, including without limitation
MrBedfordVan 0:b9164b348919 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
MrBedfordVan 0:b9164b348919 11 and/or sell copies of the Software, and to permit persons to whom the
MrBedfordVan 0:b9164b348919 12 Software is furnished to do so, subject to the following conditions:
MrBedfordVan 0:b9164b348919 13
MrBedfordVan 0:b9164b348919 14 The above copyright notice and this permission notice shall be included in
MrBedfordVan 0:b9164b348919 15 all copies or substantial portions of the Software.
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MrBedfordVan 0:b9164b348919 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MrBedfordVan 0:b9164b348919 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MrBedfordVan 0:b9164b348919 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MrBedfordVan 0:b9164b348919 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
MrBedfordVan 0:b9164b348919 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
MrBedfordVan 0:b9164b348919 23 DEALINGS IN THE SOFTWARE.
MrBedfordVan 0:b9164b348919 24 */
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 /**
MrBedfordVan 0:b9164b348919 27 * Class definition for DynamicPwm.
MrBedfordVan 0:b9164b348919 28 *
MrBedfordVan 0:b9164b348919 29 * This class addresses a few issues found in the underlying libraries.
MrBedfordVan 0:b9164b348919 30 * This provides the ability for a neat, clean swap between PWM channels.
MrBedfordVan 0:b9164b348919 31 */
MrBedfordVan 0:b9164b348919 32
MrBedfordVan 0:b9164b348919 33 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 34 #include "DynamicPwm.h"
MrBedfordVan 0:b9164b348919 35 #include "MicroBitPin.h"
MrBedfordVan 0:b9164b348919 36 #include "ErrorNo.h"
MrBedfordVan 0:b9164b348919 37
MrBedfordVan 0:b9164b348919 38 DynamicPwm* DynamicPwm::pwms[NO_PWMS] = { NULL, NULL, NULL };
MrBedfordVan 0:b9164b348919 39
MrBedfordVan 0:b9164b348919 40 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.
MrBedfordVan 0:b9164b348919 41
MrBedfordVan 0:b9164b348919 42 uint16_t DynamicPwm::sharedPeriod = 0; //set the shared period to an unknown state
MrBedfordVan 0:b9164b348919 43
MrBedfordVan 0:b9164b348919 44 /**
MrBedfordVan 0:b9164b348919 45 * Reassigns an already operational PWM channel to the given pin.
MrBedfordVan 0:b9164b348919 46 *
MrBedfordVan 0:b9164b348919 47 * @param pin The desired pin to begin a PWM wave.
MrBedfordVan 0:b9164b348919 48 *
MrBedfordVan 0:b9164b348919 49 * @param oldPin The pin to stop running a PWM wave.
MrBedfordVan 0:b9164b348919 50 *
MrBedfordVan 0:b9164b348919 51 * @param channel_number The GPIOTE channel being used to drive this PWM channel
MrBedfordVan 0:b9164b348919 52 *
MrBedfordVan 0:b9164b348919 53 * TODO: Merge into mbed, at a later date.
MrBedfordVan 0:b9164b348919 54 */
MrBedfordVan 0:b9164b348919 55 void gpiote_reinit(PinName pin, PinName oldPin, uint8_t channel_number)
MrBedfordVan 0:b9164b348919 56 {
MrBedfordVan 0:b9164b348919 57 // Connect GPIO input buffers and configure PWM_OUTPUT_PIN_NUMBER as an output.
MrBedfordVan 0:b9164b348919 58 NRF_GPIO->PIN_CNF[pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
MrBedfordVan 0:b9164b348919 59 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
MrBedfordVan 0:b9164b348919 60 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
MrBedfordVan 0:b9164b348919 61 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
MrBedfordVan 0:b9164b348919 62 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
MrBedfordVan 0:b9164b348919 63
MrBedfordVan 0:b9164b348919 64 NRF_GPIO->OUTCLR = (1 << oldPin);
MrBedfordVan 0:b9164b348919 65 NRF_GPIO->OUTCLR = (1 << pin);
MrBedfordVan 0:b9164b348919 66
MrBedfordVan 0:b9164b348919 67 /* Finally configure the channel as the caller expects. If OUTINIT works, the channel is configured properly.
MrBedfordVan 0:b9164b348919 68 If it does not, the channel output inheritance sets the proper level. */
MrBedfordVan 0:b9164b348919 69
MrBedfordVan 0:b9164b348919 70 NRF_GPIOTE->CONFIG[channel_number] = (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) |
MrBedfordVan 0:b9164b348919 71 ((uint32_t)pin << GPIOTE_CONFIG_PSEL_Pos) |
MrBedfordVan 0:b9164b348919 72 ((uint32_t)GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos) |
MrBedfordVan 0:b9164b348919 73 ((uint32_t)GPIOTE_CONFIG_OUTINIT_Low << GPIOTE_CONFIG_OUTINIT_Pos); // ((uint32_t)GPIOTE_CONFIG_OUTINIT_High <<
MrBedfordVan 0:b9164b348919 74 // GPIOTE_CONFIG_OUTINIT_Pos);//
MrBedfordVan 0:b9164b348919 75
MrBedfordVan 0:b9164b348919 76 /* Three NOPs are required to make sure configuration is written before setting tasks or getting events */
MrBedfordVan 0:b9164b348919 77 __NOP();
MrBedfordVan 0:b9164b348919 78 __NOP();
MrBedfordVan 0:b9164b348919 79 __NOP();
MrBedfordVan 0:b9164b348919 80
MrBedfordVan 0:b9164b348919 81 NRF_TIMER2->CC[channel_number] = 0;
MrBedfordVan 0:b9164b348919 82 }
MrBedfordVan 0:b9164b348919 83
MrBedfordVan 0:b9164b348919 84 /**
MrBedfordVan 0:b9164b348919 85 * An internal constructor used when allocating a new DynamicPwm instance.
MrBedfordVan 0:b9164b348919 86 *
MrBedfordVan 0:b9164b348919 87 * @param pin the name of the pin for the pwm to target
MrBedfordVan 0:b9164b348919 88 *
MrBedfordVan 0:b9164b348919 89 * @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.)
MrBedfordVan 0:b9164b348919 90 * or PWM_PERSISTENCE_TRANSIENT (can be replaced at any point if a channel is required.)
MrBedfordVan 0:b9164b348919 91 */
MrBedfordVan 0:b9164b348919 92 DynamicPwm::DynamicPwm(PinName pin, PwmPersistence persistence) : PwmOut(pin)
MrBedfordVan 0:b9164b348919 93 {
MrBedfordVan 0:b9164b348919 94 this->flags = persistence;
MrBedfordVan 0:b9164b348919 95 }
MrBedfordVan 0:b9164b348919 96
MrBedfordVan 0:b9164b348919 97 /**
MrBedfordVan 0:b9164b348919 98 * Redirects the pwm channel to point at a different pin.
MrBedfordVan 0:b9164b348919 99 *
MrBedfordVan 0:b9164b348919 100 * @param pin the desired pin to output a PWM wave.
MrBedfordVan 0:b9164b348919 101 *
MrBedfordVan 0:b9164b348919 102 * @code
MrBedfordVan 0:b9164b348919 103 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 104 * pwm->redirect(p0); // pwm is now produced on p0
MrBedfordVan 0:b9164b348919 105 * @endcode
MrBedfordVan 0:b9164b348919 106 */
MrBedfordVan 0:b9164b348919 107 void DynamicPwm::redirect(PinName pin)
MrBedfordVan 0:b9164b348919 108 {
MrBedfordVan 0:b9164b348919 109 gpiote_reinit(pin, _pwm.pin, (uint8_t)_pwm.pwm);
MrBedfordVan 0:b9164b348919 110 this->_pwm.pin = pin;
MrBedfordVan 0:b9164b348919 111 }
MrBedfordVan 0:b9164b348919 112
MrBedfordVan 0:b9164b348919 113 /**
MrBedfordVan 0:b9164b348919 114 * Creates a new DynamicPwm instance, or reuses an existing instance that
MrBedfordVan 0:b9164b348919 115 * has a persistence level of PWM_PERSISTENCE_TRANSIENT.
MrBedfordVan 0:b9164b348919 116 *
MrBedfordVan 0:b9164b348919 117 * @param pin the name of the pin for the pwm to target
MrBedfordVan 0:b9164b348919 118 *
MrBedfordVan 0:b9164b348919 119 * @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.)
MrBedfordVan 0:b9164b348919 120 * or PWM_PERSISTENCE_TRANSIENT (can be replaced at any point if a channel is required.)
MrBedfordVan 0:b9164b348919 121 *
MrBedfordVan 0:b9164b348919 122 * @return a pointer to the first available free pwm channel - or the first one that can be reallocated. If
MrBedfordVan 0:b9164b348919 123 * no channels are available, NULL is returned.
MrBedfordVan 0:b9164b348919 124 *
MrBedfordVan 0:b9164b348919 125 * @code
MrBedfordVan 0:b9164b348919 126 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 127 * @endcode
MrBedfordVan 0:b9164b348919 128 */
MrBedfordVan 0:b9164b348919 129 DynamicPwm* DynamicPwm::allocate(PinName pin, PwmPersistence persistence)
MrBedfordVan 0:b9164b348919 130 {
MrBedfordVan 0:b9164b348919 131 //try to find a blank spot first
MrBedfordVan 0:b9164b348919 132 for(int i = 0; i < NO_PWMS; i++)
MrBedfordVan 0:b9164b348919 133 {
MrBedfordVan 0:b9164b348919 134 if(pwms[i] == NULL)
MrBedfordVan 0:b9164b348919 135 {
MrBedfordVan 0:b9164b348919 136 lastUsed = i;
MrBedfordVan 0:b9164b348919 137 pwms[i] = new DynamicPwm(pin, persistence);
MrBedfordVan 0:b9164b348919 138 return pwms[i];
MrBedfordVan 0:b9164b348919 139 }
MrBedfordVan 0:b9164b348919 140 }
MrBedfordVan 0:b9164b348919 141
MrBedfordVan 0:b9164b348919 142 //no blank spot.. try to find a transient PWM
MrBedfordVan 0:b9164b348919 143 int channelIterator = (lastUsed + 1 > NO_PWMS - 1) ? 0 : lastUsed + 1;
MrBedfordVan 0:b9164b348919 144
MrBedfordVan 0:b9164b348919 145 while(channelIterator != lastUsed)
MrBedfordVan 0:b9164b348919 146 {
MrBedfordVan 0:b9164b348919 147 if(pwms[channelIterator]->flags & PWM_PERSISTENCE_TRANSIENT)
MrBedfordVan 0:b9164b348919 148 {
MrBedfordVan 0:b9164b348919 149 lastUsed = channelIterator;
MrBedfordVan 0:b9164b348919 150 pwms[channelIterator]->flags = persistence;
MrBedfordVan 0:b9164b348919 151 pwms[channelIterator]->redirect(pin);
MrBedfordVan 0:b9164b348919 152 return pwms[channelIterator];
MrBedfordVan 0:b9164b348919 153 }
MrBedfordVan 0:b9164b348919 154
MrBedfordVan 0:b9164b348919 155 channelIterator = (channelIterator + 1 > NO_PWMS - 1) ? 0 : channelIterator + 1;
MrBedfordVan 0:b9164b348919 156 }
MrBedfordVan 0:b9164b348919 157
MrBedfordVan 0:b9164b348919 158 //if we haven't found a free one, we must try to allocate the last used...
MrBedfordVan 0:b9164b348919 159 if(pwms[lastUsed]->flags & PWM_PERSISTENCE_TRANSIENT)
MrBedfordVan 0:b9164b348919 160 {
MrBedfordVan 0:b9164b348919 161 pwms[lastUsed]->flags = persistence;
MrBedfordVan 0:b9164b348919 162 pwms[lastUsed]->redirect(pin);
MrBedfordVan 0:b9164b348919 163 return pwms[lastUsed];
MrBedfordVan 0:b9164b348919 164 }
MrBedfordVan 0:b9164b348919 165
MrBedfordVan 0:b9164b348919 166 //well if we have no transient channels - we can't give any away! :( return null
MrBedfordVan 0:b9164b348919 167 return (DynamicPwm*)NULL;
MrBedfordVan 0:b9164b348919 168 }
MrBedfordVan 0:b9164b348919 169
MrBedfordVan 0:b9164b348919 170 /**
MrBedfordVan 0:b9164b348919 171 * Frees this DynamicPwm instance for reuse.
MrBedfordVan 0:b9164b348919 172 *
MrBedfordVan 0:b9164b348919 173 * @code
MrBedfordVan 0:b9164b348919 174 * DynamicPwm* pwm = DynamicPwm::allocate();
MrBedfordVan 0:b9164b348919 175 * pwm->release();
MrBedfordVan 0:b9164b348919 176 * @endcode
MrBedfordVan 0:b9164b348919 177 */
MrBedfordVan 0:b9164b348919 178 void DynamicPwm::release()
MrBedfordVan 0:b9164b348919 179 {
MrBedfordVan 0:b9164b348919 180 //free the pwm instance.
MrBedfordVan 0:b9164b348919 181 NRF_GPIOTE->CONFIG[(uint8_t) _pwm.pwm] = 0;
MrBedfordVan 0:b9164b348919 182 pwmout_free(&_pwm);
MrBedfordVan 0:b9164b348919 183 this->flags = PWM_PERSISTENCE_TRANSIENT;
MrBedfordVan 0:b9164b348919 184
MrBedfordVan 0:b9164b348919 185 //set the pointer to this object to null...
MrBedfordVan 0:b9164b348919 186 for(int i =0; i < NO_PWMS; i++)
MrBedfordVan 0:b9164b348919 187 if(pwms[i] == this)
MrBedfordVan 0:b9164b348919 188 {
MrBedfordVan 0:b9164b348919 189 delete pwms[i];
MrBedfordVan 0:b9164b348919 190 pwms[i] = NULL;
MrBedfordVan 0:b9164b348919 191 }
MrBedfordVan 0:b9164b348919 192 }
MrBedfordVan 0:b9164b348919 193
MrBedfordVan 0:b9164b348919 194 /**
MrBedfordVan 0:b9164b348919 195 * A lightweight wrapper around the super class' write in order to capture the value
MrBedfordVan 0:b9164b348919 196 *
MrBedfordVan 0:b9164b348919 197 * @param value the duty cycle percentage in floating point format.
MrBedfordVan 0:b9164b348919 198 *
MrBedfordVan 0:b9164b348919 199 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range
MrBedfordVan 0:b9164b348919 200 *
MrBedfordVan 0:b9164b348919 201 * @code
MrBedfordVan 0:b9164b348919 202 * DynamicPwm* pwm = DynamicPwm::allocate();
MrBedfordVan 0:b9164b348919 203 * pwm->write(0.5);
MrBedfordVan 0:b9164b348919 204 * @endcode
MrBedfordVan 0:b9164b348919 205 */
MrBedfordVan 0:b9164b348919 206 int DynamicPwm::write(float value){
MrBedfordVan 0:b9164b348919 207
MrBedfordVan 0:b9164b348919 208 if(value < 0)
MrBedfordVan 0:b9164b348919 209 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 210
MrBedfordVan 0:b9164b348919 211 PwmOut::write(value);
MrBedfordVan 0:b9164b348919 212 lastValue = value;
MrBedfordVan 0:b9164b348919 213
MrBedfordVan 0:b9164b348919 214 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 215 }
MrBedfordVan 0:b9164b348919 216
MrBedfordVan 0:b9164b348919 217 /**
MrBedfordVan 0:b9164b348919 218 * Retreives the PinName associated with this DynamicPwm instance.
MrBedfordVan 0:b9164b348919 219 *
MrBedfordVan 0:b9164b348919 220 * @code
MrBedfordVan 0:b9164b348919 221 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 222 *
MrBedfordVan 0:b9164b348919 223 * // returns the PinName n.
MrBedfordVan 0:b9164b348919 224 * pwm->getPinName();
MrBedfordVan 0:b9164b348919 225 * @endcode
MrBedfordVan 0:b9164b348919 226 *
MrBedfordVan 0:b9164b348919 227 * @note This should be used to check that the DynamicPwm instance has not
MrBedfordVan 0:b9164b348919 228 * been reallocated for use in another part of a program.
MrBedfordVan 0:b9164b348919 229 */
MrBedfordVan 0:b9164b348919 230 PinName DynamicPwm::getPinName()
MrBedfordVan 0:b9164b348919 231 {
MrBedfordVan 0:b9164b348919 232 return _pwm.pin;
MrBedfordVan 0:b9164b348919 233 }
MrBedfordVan 0:b9164b348919 234
MrBedfordVan 0:b9164b348919 235 /**
MrBedfordVan 0:b9164b348919 236 * Retreives the last value that has been written to this DynamicPwm instance.
MrBedfordVan 0:b9164b348919 237 * in the range 0 - 1023 inclusive.
MrBedfordVan 0:b9164b348919 238 *
MrBedfordVan 0:b9164b348919 239 * @code
MrBedfordVan 0:b9164b348919 240 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 241 * pwm->write(0.5);
MrBedfordVan 0:b9164b348919 242 *
MrBedfordVan 0:b9164b348919 243 * // will return 512.
MrBedfordVan 0:b9164b348919 244 * pwm->getValue();
MrBedfordVan 0:b9164b348919 245 * @endcode
MrBedfordVan 0:b9164b348919 246 */
MrBedfordVan 0:b9164b348919 247 int DynamicPwm::getValue()
MrBedfordVan 0:b9164b348919 248 {
MrBedfordVan 0:b9164b348919 249 return (float)lastValue * float(MICROBIT_PIN_MAX_OUTPUT);
MrBedfordVan 0:b9164b348919 250 }
MrBedfordVan 0:b9164b348919 251
MrBedfordVan 0:b9164b348919 252 /**
MrBedfordVan 0:b9164b348919 253 * Retreives the current period in use by the entire PWM module in microseconds.
MrBedfordVan 0:b9164b348919 254 *
MrBedfordVan 0:b9164b348919 255 * Example:
MrBedfordVan 0:b9164b348919 256 * @code
MrBedfordVan 0:b9164b348919 257 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 258 * pwm->getPeriod();
MrBedfordVan 0:b9164b348919 259 * @endcode
MrBedfordVan 0:b9164b348919 260 */
MrBedfordVan 0:b9164b348919 261 int DynamicPwm::getPeriodUs()
MrBedfordVan 0:b9164b348919 262 {
MrBedfordVan 0:b9164b348919 263 return sharedPeriod;
MrBedfordVan 0:b9164b348919 264 }
MrBedfordVan 0:b9164b348919 265
MrBedfordVan 0:b9164b348919 266 /**
MrBedfordVan 0:b9164b348919 267 * Retreives the current period in use by the entire PWM module in milliseconds.
MrBedfordVan 0:b9164b348919 268 *
MrBedfordVan 0:b9164b348919 269 * Example:
MrBedfordVan 0:b9164b348919 270 * @code
MrBedfordVan 0:b9164b348919 271 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 272 * pwm->setPeriodUs(20000);
MrBedfordVan 0:b9164b348919 273 *
MrBedfordVan 0:b9164b348919 274 * // will return 20000
MrBedfordVan 0:b9164b348919 275 * pwm->getPeriod();
MrBedfordVan 0:b9164b348919 276 * @endcode
MrBedfordVan 0:b9164b348919 277 */
MrBedfordVan 0:b9164b348919 278 int DynamicPwm::getPeriod()
MrBedfordVan 0:b9164b348919 279 {
MrBedfordVan 0:b9164b348919 280 return getPeriodUs() / 1000;
MrBedfordVan 0:b9164b348919 281 }
MrBedfordVan 0:b9164b348919 282
MrBedfordVan 0:b9164b348919 283 /**
MrBedfordVan 0:b9164b348919 284 * Sets the period used by the WHOLE PWM module.
MrBedfordVan 0:b9164b348919 285 *
MrBedfordVan 0:b9164b348919 286 * @param period the desired period in microseconds.
MrBedfordVan 0:b9164b348919 287 *
MrBedfordVan 0:b9164b348919 288 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if period is out of range
MrBedfordVan 0:b9164b348919 289 *
MrBedfordVan 0:b9164b348919 290 * Example:
MrBedfordVan 0:b9164b348919 291 * @code
MrBedfordVan 0:b9164b348919 292 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 293 *
MrBedfordVan 0:b9164b348919 294 * // period now is 20ms
MrBedfordVan 0:b9164b348919 295 * pwm->setPeriodUs(20000);
MrBedfordVan 0:b9164b348919 296 * @endcode
MrBedfordVan 0:b9164b348919 297 *
MrBedfordVan 0:b9164b348919 298 * @note Any changes to the period will AFFECT ALL CHANNELS.
MrBedfordVan 0:b9164b348919 299 */
MrBedfordVan 0:b9164b348919 300 int DynamicPwm::setPeriodUs(int period)
MrBedfordVan 0:b9164b348919 301 {
MrBedfordVan 0:b9164b348919 302 if(period < 0)
MrBedfordVan 0:b9164b348919 303 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 304
MrBedfordVan 0:b9164b348919 305 //#HACK this forces mbed to update the pulse width calculation.
MrBedfordVan 0:b9164b348919 306 period_us(period);
MrBedfordVan 0:b9164b348919 307 write(lastValue);
MrBedfordVan 0:b9164b348919 308 sharedPeriod = period;
MrBedfordVan 0:b9164b348919 309
MrBedfordVan 0:b9164b348919 310 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 311 }
MrBedfordVan 0:b9164b348919 312
MrBedfordVan 0:b9164b348919 313 /**
MrBedfordVan 0:b9164b348919 314 * Sets the period used by the WHOLE PWM module. Any changes to the period will AFFECT ALL CHANNELS.
MrBedfordVan 0:b9164b348919 315 *
MrBedfordVan 0:b9164b348919 316 * @param period the desired period in milliseconds.
MrBedfordVan 0:b9164b348919 317 *
MrBedfordVan 0:b9164b348919 318 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if period is out of range
MrBedfordVan 0:b9164b348919 319 *
MrBedfordVan 0:b9164b348919 320 * Example:
MrBedfordVan 0:b9164b348919 321 * @code
MrBedfordVan 0:b9164b348919 322 * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
MrBedfordVan 0:b9164b348919 323 *
MrBedfordVan 0:b9164b348919 324 * // period now is 20ms
MrBedfordVan 0:b9164b348919 325 * pwm->setPeriod(20);
MrBedfordVan 0:b9164b348919 326 * @endcode
MrBedfordVan 0:b9164b348919 327 */
MrBedfordVan 0:b9164b348919 328 int DynamicPwm::setPeriod(int period)
MrBedfordVan 0:b9164b348919 329 {
MrBedfordVan 0:b9164b348919 330 return setPeriodUs(period * 1000);
MrBedfordVan 0:b9164b348919 331 }