Mouse code for the MacroRat
mbed-dev/targets/TARGET_Atmel/TARGET_SAM_CortexM0P/pwmout_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2015 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "pwmout_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "pinmap_function.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | |
sahilmgandhi | 18:6a4db94011d3 | 23 | /* Prescaler values for TCC Module */ |
sahilmgandhi | 18:6a4db94011d3 | 24 | const uint32_t tcc_prescaler[] = { |
sahilmgandhi | 18:6a4db94011d3 | 25 | TCC_CLOCK_PRESCALER_DIV1, |
sahilmgandhi | 18:6a4db94011d3 | 26 | TCC_CLOCK_PRESCALER_DIV2, |
sahilmgandhi | 18:6a4db94011d3 | 27 | TCC_CLOCK_PRESCALER_DIV4, |
sahilmgandhi | 18:6a4db94011d3 | 28 | TCC_CLOCK_PRESCALER_DIV8, |
sahilmgandhi | 18:6a4db94011d3 | 29 | TCC_CLOCK_PRESCALER_DIV16, |
sahilmgandhi | 18:6a4db94011d3 | 30 | TCC_CLOCK_PRESCALER_DIV64, |
sahilmgandhi | 18:6a4db94011d3 | 31 | TCC_CLOCK_PRESCALER_DIV256, |
sahilmgandhi | 18:6a4db94011d3 | 32 | TCC_CLOCK_PRESCALER_DIV1024 |
sahilmgandhi | 18:6a4db94011d3 | 33 | }; |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | /* Max count limits of TCC Modules */ |
sahilmgandhi | 18:6a4db94011d3 | 36 | extern const uint32_t _tcc_maxs[TCC_INST_NUM]; |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | /** Set the period of PWM object (will not update the waveform) |
sahilmgandhi | 18:6a4db94011d3 | 39 | * |
sahilmgandhi | 18:6a4db94011d3 | 40 | * @param[in] obj The PWM object whose period is to be updated |
sahilmgandhi | 18:6a4db94011d3 | 41 | * @param[in] period_us Period in microseconds |
sahilmgandhi | 18:6a4db94011d3 | 42 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 43 | */ |
sahilmgandhi | 18:6a4db94011d3 | 44 | static void pwmout_set_period(pwmout_t* obj, int period_us) |
sahilmgandhi | 18:6a4db94011d3 | 45 | { |
sahilmgandhi | 18:6a4db94011d3 | 46 | uint8_t i; |
sahilmgandhi | 18:6a4db94011d3 | 47 | uint32_t freq_hz; |
sahilmgandhi | 18:6a4db94011d3 | 48 | uint32_t div_freq; |
sahilmgandhi | 18:6a4db94011d3 | 49 | double us_per_cycle; |
sahilmgandhi | 18:6a4db94011d3 | 50 | uint64_t max_period = 0; |
sahilmgandhi | 18:6a4db94011d3 | 51 | uint32_t us_period = period_us; |
sahilmgandhi | 18:6a4db94011d3 | 52 | |
sahilmgandhi | 18:6a4db94011d3 | 53 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 54 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 55 | |
sahilmgandhi | 18:6a4db94011d3 | 56 | /* TCC instance index */ |
sahilmgandhi | 18:6a4db94011d3 | 57 | uint8_t module_index = _tcc_get_inst_index(obj->tcc.hw); |
sahilmgandhi | 18:6a4db94011d3 | 58 | |
sahilmgandhi | 18:6a4db94011d3 | 59 | uint32_t count_max = _tcc_maxs[module_index]; |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | freq_hz = system_gclk_gen_get_hz(obj->clock_source); |
sahilmgandhi | 18:6a4db94011d3 | 62 | |
sahilmgandhi | 18:6a4db94011d3 | 63 | for (i=0; i<sizeof(tcc_prescaler); i++) { |
sahilmgandhi | 18:6a4db94011d3 | 64 | div_freq = freq_hz >> tcc_prescaler[i]; |
sahilmgandhi | 18:6a4db94011d3 | 65 | if (!div_freq) break; |
sahilmgandhi | 18:6a4db94011d3 | 66 | us_per_cycle = 1000000.00 / div_freq; |
sahilmgandhi | 18:6a4db94011d3 | 67 | max_period = us_per_cycle * count_max; |
sahilmgandhi | 18:6a4db94011d3 | 68 | if (max_period >= us_period) { |
sahilmgandhi | 18:6a4db94011d3 | 69 | obj->clock_prescaler = (enum tc_clock_prescaler)tcc_prescaler[i]; |
sahilmgandhi | 18:6a4db94011d3 | 70 | obj->period = us_period / us_per_cycle; |
sahilmgandhi | 18:6a4db94011d3 | 71 | obj->us_per_cycle = us_per_cycle; |
sahilmgandhi | 18:6a4db94011d3 | 72 | break; |
sahilmgandhi | 18:6a4db94011d3 | 73 | } |
sahilmgandhi | 18:6a4db94011d3 | 74 | } |
sahilmgandhi | 18:6a4db94011d3 | 75 | } |
sahilmgandhi | 18:6a4db94011d3 | 76 | |
sahilmgandhi | 18:6a4db94011d3 | 77 | /** Initialize PWM Module with updated values |
sahilmgandhi | 18:6a4db94011d3 | 78 | * |
sahilmgandhi | 18:6a4db94011d3 | 79 | * @param[in][out] obj The PWM object to initialize |
sahilmgandhi | 18:6a4db94011d3 | 80 | * @return non-zero if success |
sahilmgandhi | 18:6a4db94011d3 | 81 | */ |
sahilmgandhi | 18:6a4db94011d3 | 82 | bool pwmout_init_hw(pwmout_t* obj) |
sahilmgandhi | 18:6a4db94011d3 | 83 | { |
sahilmgandhi | 18:6a4db94011d3 | 84 | uint32_t mux_func = (uint32_t)NC; |
sahilmgandhi | 18:6a4db94011d3 | 85 | uint32_t pwm = (uint32_t)NC; |
sahilmgandhi | 18:6a4db94011d3 | 86 | PinName pin; |
sahilmgandhi | 18:6a4db94011d3 | 87 | uint32_t ch_index = (uint32_t)NC; |
sahilmgandhi | 18:6a4db94011d3 | 88 | struct tcc_config config_tcc; |
sahilmgandhi | 18:6a4db94011d3 | 89 | uint32_t tcc_channel = (uint32_t)NC; |
sahilmgandhi | 18:6a4db94011d3 | 90 | |
sahilmgandhi | 18:6a4db94011d3 | 91 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 92 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 93 | |
sahilmgandhi | 18:6a4db94011d3 | 94 | pin = obj->pin; |
sahilmgandhi | 18:6a4db94011d3 | 95 | pwm = pinmap_peripheral(pin, PinMap_PWM); |
sahilmgandhi | 18:6a4db94011d3 | 96 | if (pwm == (uint32_t)NC) return 0; /* Pin not supported */ |
sahilmgandhi | 18:6a4db94011d3 | 97 | |
sahilmgandhi | 18:6a4db94011d3 | 98 | mux_func = pinmap_function(pin, PinMap_PWM); |
sahilmgandhi | 18:6a4db94011d3 | 99 | ch_index = pinmap_channel_pwm(pin, (PWMName) pwm); |
sahilmgandhi | 18:6a4db94011d3 | 100 | if ((mux_func == (uint32_t)NC) || (ch_index == (uint32_t)NC)) { |
sahilmgandhi | 18:6a4db94011d3 | 101 | /* Pin not supported */ |
sahilmgandhi | 18:6a4db94011d3 | 102 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 103 | } |
sahilmgandhi | 18:6a4db94011d3 | 104 | if ((ch_index == 0) || (ch_index == 4)) { |
sahilmgandhi | 18:6a4db94011d3 | 105 | tcc_channel = 0; |
sahilmgandhi | 18:6a4db94011d3 | 106 | } else if ((ch_index == 1) || (ch_index == 5)) { |
sahilmgandhi | 18:6a4db94011d3 | 107 | tcc_channel = 1; |
sahilmgandhi | 18:6a4db94011d3 | 108 | } else if ((ch_index == 2) || (ch_index == 6)) { |
sahilmgandhi | 18:6a4db94011d3 | 109 | tcc_channel = 2; |
sahilmgandhi | 18:6a4db94011d3 | 110 | } else if ((ch_index == 3) || (ch_index == 7)) { |
sahilmgandhi | 18:6a4db94011d3 | 111 | tcc_channel = 3; |
sahilmgandhi | 18:6a4db94011d3 | 112 | } |
sahilmgandhi | 18:6a4db94011d3 | 113 | |
sahilmgandhi | 18:6a4db94011d3 | 114 | tcc_get_config_defaults(&config_tcc, (Tcc*)pwm); |
sahilmgandhi | 18:6a4db94011d3 | 115 | |
sahilmgandhi | 18:6a4db94011d3 | 116 | config_tcc.counter.clock_source = obj->clock_source; |
sahilmgandhi | 18:6a4db94011d3 | 117 | config_tcc.counter.clock_prescaler = (enum tcc_clock_prescaler)obj->clock_prescaler; |
sahilmgandhi | 18:6a4db94011d3 | 118 | |
sahilmgandhi | 18:6a4db94011d3 | 119 | config_tcc.counter.period = obj->period; |
sahilmgandhi | 18:6a4db94011d3 | 120 | config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; |
sahilmgandhi | 18:6a4db94011d3 | 121 | config_tcc.compare.match[tcc_channel] = obj->period * obj->duty_cycle; |
sahilmgandhi | 18:6a4db94011d3 | 122 | |
sahilmgandhi | 18:6a4db94011d3 | 123 | config_tcc.pins.enable_wave_out_pin[ch_index] = true; |
sahilmgandhi | 18:6a4db94011d3 | 124 | config_tcc.pins.wave_out_pin[ch_index] = pin; |
sahilmgandhi | 18:6a4db94011d3 | 125 | config_tcc.pins.wave_out_pin_mux[ch_index] = mux_func; |
sahilmgandhi | 18:6a4db94011d3 | 126 | |
sahilmgandhi | 18:6a4db94011d3 | 127 | return (STATUS_OK == tcc_init(&obj->tcc, (Tcc*)pwm, &config_tcc)); |
sahilmgandhi | 18:6a4db94011d3 | 128 | |
sahilmgandhi | 18:6a4db94011d3 | 129 | } |
sahilmgandhi | 18:6a4db94011d3 | 130 | |
sahilmgandhi | 18:6a4db94011d3 | 131 | /** Initialize PWM Module |
sahilmgandhi | 18:6a4db94011d3 | 132 | * |
sahilmgandhi | 18:6a4db94011d3 | 133 | * @param[in][out] obj The PWM object to initialize |
sahilmgandhi | 18:6a4db94011d3 | 134 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 135 | */ |
sahilmgandhi | 18:6a4db94011d3 | 136 | void pwmout_init(pwmout_t* obj, PinName pin) |
sahilmgandhi | 18:6a4db94011d3 | 137 | { |
sahilmgandhi | 18:6a4db94011d3 | 138 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 139 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 140 | |
sahilmgandhi | 18:6a4db94011d3 | 141 | if ((uint32_t)NC == pinmap_peripheral(pin, PinMap_PWM)) { |
sahilmgandhi | 18:6a4db94011d3 | 142 | /* Pin not supported */ |
sahilmgandhi | 18:6a4db94011d3 | 143 | return; |
sahilmgandhi | 18:6a4db94011d3 | 144 | } |
sahilmgandhi | 18:6a4db94011d3 | 145 | |
sahilmgandhi | 18:6a4db94011d3 | 146 | obj->pin = pin; |
sahilmgandhi | 18:6a4db94011d3 | 147 | obj->period = 0xFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 148 | obj->duty_cycle = 1; |
sahilmgandhi | 18:6a4db94011d3 | 149 | obj->clock_source = GCLK_GENERATOR_0; /* 8Mhz input clock */ |
sahilmgandhi | 18:6a4db94011d3 | 150 | obj->clock_prescaler = (enum tc_clock_prescaler)TCC_CLOCK_PRESCALER_DIV8; /* Default to 1MHz for 8Mhz input clock */ |
sahilmgandhi | 18:6a4db94011d3 | 151 | |
sahilmgandhi | 18:6a4db94011d3 | 152 | /* Update the changes */ |
sahilmgandhi | 18:6a4db94011d3 | 153 | if (pwmout_init_hw(obj)) { |
sahilmgandhi | 18:6a4db94011d3 | 154 | /* Enable PWM Module */ |
sahilmgandhi | 18:6a4db94011d3 | 155 | tcc_enable(&obj->tcc); |
sahilmgandhi | 18:6a4db94011d3 | 156 | } |
sahilmgandhi | 18:6a4db94011d3 | 157 | |
sahilmgandhi | 18:6a4db94011d3 | 158 | } |
sahilmgandhi | 18:6a4db94011d3 | 159 | |
sahilmgandhi | 18:6a4db94011d3 | 160 | /** Free the PWM Module |
sahilmgandhi | 18:6a4db94011d3 | 161 | * |
sahilmgandhi | 18:6a4db94011d3 | 162 | * @param[in] obj The PWM object to free |
sahilmgandhi | 18:6a4db94011d3 | 163 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 164 | */ |
sahilmgandhi | 18:6a4db94011d3 | 165 | void pwmout_free(pwmout_t* obj) |
sahilmgandhi | 18:6a4db94011d3 | 166 | { |
sahilmgandhi | 18:6a4db94011d3 | 167 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 168 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 169 | |
sahilmgandhi | 18:6a4db94011d3 | 170 | tcc_disable(&obj->tcc); |
sahilmgandhi | 18:6a4db94011d3 | 171 | } |
sahilmgandhi | 18:6a4db94011d3 | 172 | |
sahilmgandhi | 18:6a4db94011d3 | 173 | /** Set the duty cycle of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 174 | * |
sahilmgandhi | 18:6a4db94011d3 | 175 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 176 | * @param[in] value New duty cycle to be set |
sahilmgandhi | 18:6a4db94011d3 | 177 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 178 | */ |
sahilmgandhi | 18:6a4db94011d3 | 179 | void pwmout_write(pwmout_t* obj, float value) |
sahilmgandhi | 18:6a4db94011d3 | 180 | { |
sahilmgandhi | 18:6a4db94011d3 | 181 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 182 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 183 | |
sahilmgandhi | 18:6a4db94011d3 | 184 | if (value < 0.0f) { |
sahilmgandhi | 18:6a4db94011d3 | 185 | value = 0; |
sahilmgandhi | 18:6a4db94011d3 | 186 | } else if (value > 1.0f) { |
sahilmgandhi | 18:6a4db94011d3 | 187 | value = 1; |
sahilmgandhi | 18:6a4db94011d3 | 188 | } |
sahilmgandhi | 18:6a4db94011d3 | 189 | |
sahilmgandhi | 18:6a4db94011d3 | 190 | /* Modify the pulse width keeping period same */ |
sahilmgandhi | 18:6a4db94011d3 | 191 | obj->duty_cycle = value; |
sahilmgandhi | 18:6a4db94011d3 | 192 | |
sahilmgandhi | 18:6a4db94011d3 | 193 | /* Disable PWM Module */ |
sahilmgandhi | 18:6a4db94011d3 | 194 | tcc_disable(&obj->tcc); |
sahilmgandhi | 18:6a4db94011d3 | 195 | |
sahilmgandhi | 18:6a4db94011d3 | 196 | /* Update the changes */ |
sahilmgandhi | 18:6a4db94011d3 | 197 | if (pwmout_init_hw(obj)) { |
sahilmgandhi | 18:6a4db94011d3 | 198 | /* Enable PWM Module */ |
sahilmgandhi | 18:6a4db94011d3 | 199 | tcc_enable(&obj->tcc); |
sahilmgandhi | 18:6a4db94011d3 | 200 | } |
sahilmgandhi | 18:6a4db94011d3 | 201 | } |
sahilmgandhi | 18:6a4db94011d3 | 202 | |
sahilmgandhi | 18:6a4db94011d3 | 203 | /** Get the duty cycle of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 204 | * |
sahilmgandhi | 18:6a4db94011d3 | 205 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 206 | * @return Current duty cycle |
sahilmgandhi | 18:6a4db94011d3 | 207 | */ |
sahilmgandhi | 18:6a4db94011d3 | 208 | float pwmout_read(pwmout_t* obj) |
sahilmgandhi | 18:6a4db94011d3 | 209 | { |
sahilmgandhi | 18:6a4db94011d3 | 210 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 211 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 212 | |
sahilmgandhi | 18:6a4db94011d3 | 213 | return obj->duty_cycle; |
sahilmgandhi | 18:6a4db94011d3 | 214 | } |
sahilmgandhi | 18:6a4db94011d3 | 215 | |
sahilmgandhi | 18:6a4db94011d3 | 216 | /** Set the period of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 217 | * |
sahilmgandhi | 18:6a4db94011d3 | 218 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 219 | * @param[in] seconds New period in seconds |
sahilmgandhi | 18:6a4db94011d3 | 220 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 221 | */ |
sahilmgandhi | 18:6a4db94011d3 | 222 | void pwmout_period(pwmout_t* obj, float seconds) |
sahilmgandhi | 18:6a4db94011d3 | 223 | { |
sahilmgandhi | 18:6a4db94011d3 | 224 | pwmout_period_us(obj, seconds * 1000000.0f); |
sahilmgandhi | 18:6a4db94011d3 | 225 | } |
sahilmgandhi | 18:6a4db94011d3 | 226 | |
sahilmgandhi | 18:6a4db94011d3 | 227 | /** Set the period of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 228 | * |
sahilmgandhi | 18:6a4db94011d3 | 229 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 230 | * @param[in] value New period in milliseconds |
sahilmgandhi | 18:6a4db94011d3 | 231 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 232 | */ |
sahilmgandhi | 18:6a4db94011d3 | 233 | void pwmout_period_ms(pwmout_t* obj, int ms) |
sahilmgandhi | 18:6a4db94011d3 | 234 | { |
sahilmgandhi | 18:6a4db94011d3 | 235 | pwmout_period_us(obj, ms * 1000); |
sahilmgandhi | 18:6a4db94011d3 | 236 | } |
sahilmgandhi | 18:6a4db94011d3 | 237 | |
sahilmgandhi | 18:6a4db94011d3 | 238 | /** Set the period of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 239 | * |
sahilmgandhi | 18:6a4db94011d3 | 240 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 241 | * @param[in] us New period in microseconds |
sahilmgandhi | 18:6a4db94011d3 | 242 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 243 | */ |
sahilmgandhi | 18:6a4db94011d3 | 244 | void pwmout_period_us(pwmout_t* obj, int us) |
sahilmgandhi | 18:6a4db94011d3 | 245 | { |
sahilmgandhi | 18:6a4db94011d3 | 246 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 247 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 248 | |
sahilmgandhi | 18:6a4db94011d3 | 249 | /* Disable PWM Module */ |
sahilmgandhi | 18:6a4db94011d3 | 250 | tcc_disable(&obj->tcc); |
sahilmgandhi | 18:6a4db94011d3 | 251 | |
sahilmgandhi | 18:6a4db94011d3 | 252 | /* TODO: Find and set the period */ |
sahilmgandhi | 18:6a4db94011d3 | 253 | pwmout_set_period(obj, us); |
sahilmgandhi | 18:6a4db94011d3 | 254 | |
sahilmgandhi | 18:6a4db94011d3 | 255 | /* Update the changes */ |
sahilmgandhi | 18:6a4db94011d3 | 256 | if (pwmout_init_hw(obj)) { |
sahilmgandhi | 18:6a4db94011d3 | 257 | /* Enable PWM Module */ |
sahilmgandhi | 18:6a4db94011d3 | 258 | tcc_enable(&obj->tcc); |
sahilmgandhi | 18:6a4db94011d3 | 259 | } |
sahilmgandhi | 18:6a4db94011d3 | 260 | } |
sahilmgandhi | 18:6a4db94011d3 | 261 | |
sahilmgandhi | 18:6a4db94011d3 | 262 | /** Set the pulse width of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 263 | * |
sahilmgandhi | 18:6a4db94011d3 | 264 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 265 | * @param[in] seconds New pulse width in seconds |
sahilmgandhi | 18:6a4db94011d3 | 266 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 267 | */ |
sahilmgandhi | 18:6a4db94011d3 | 268 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) |
sahilmgandhi | 18:6a4db94011d3 | 269 | { |
sahilmgandhi | 18:6a4db94011d3 | 270 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
sahilmgandhi | 18:6a4db94011d3 | 271 | } |
sahilmgandhi | 18:6a4db94011d3 | 272 | |
sahilmgandhi | 18:6a4db94011d3 | 273 | /** Set the pulse width of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 274 | * |
sahilmgandhi | 18:6a4db94011d3 | 275 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 276 | * @param[in] ms New pulse width in milliseconds |
sahilmgandhi | 18:6a4db94011d3 | 277 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 278 | */ |
sahilmgandhi | 18:6a4db94011d3 | 279 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) |
sahilmgandhi | 18:6a4db94011d3 | 280 | { |
sahilmgandhi | 18:6a4db94011d3 | 281 | pwmout_pulsewidth_us(obj, ms * 1000); |
sahilmgandhi | 18:6a4db94011d3 | 282 | } |
sahilmgandhi | 18:6a4db94011d3 | 283 | |
sahilmgandhi | 18:6a4db94011d3 | 284 | /** Set the pulse width of PWM Waveform |
sahilmgandhi | 18:6a4db94011d3 | 285 | * |
sahilmgandhi | 18:6a4db94011d3 | 286 | * @param[in] obj The PWM object |
sahilmgandhi | 18:6a4db94011d3 | 287 | * @param[in] us New pulse width in microseconds |
sahilmgandhi | 18:6a4db94011d3 | 288 | * @return void |
sahilmgandhi | 18:6a4db94011d3 | 289 | */ |
sahilmgandhi | 18:6a4db94011d3 | 290 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) |
sahilmgandhi | 18:6a4db94011d3 | 291 | { |
sahilmgandhi | 18:6a4db94011d3 | 292 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 293 | MBED_ASSERT(obj); |
sahilmgandhi | 18:6a4db94011d3 | 294 | |
sahilmgandhi | 18:6a4db94011d3 | 295 | uint32_t us_pulse = us; |
sahilmgandhi | 18:6a4db94011d3 | 296 | |
sahilmgandhi | 18:6a4db94011d3 | 297 | /* Find the new duty cycle */ |
sahilmgandhi | 18:6a4db94011d3 | 298 | double duty_cycle = us_pulse / ((double)obj->period * obj->us_per_cycle); |
sahilmgandhi | 18:6a4db94011d3 | 299 | |
sahilmgandhi | 18:6a4db94011d3 | 300 | /* This call updates pulse width as well as period */ |
sahilmgandhi | 18:6a4db94011d3 | 301 | pwmout_write(obj, duty_cycle); |
sahilmgandhi | 18:6a4db94011d3 | 302 | } |