Forked.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Aug 31 17:27:04 2017 +0100
Revision:
172:7d866c31b3c5
This updates the lib to the mbed lib v 150

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 172:7d866c31b3c5 1 /* mbed Microcontroller Library
AnnaBridge 172:7d866c31b3c5 2 * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved
AnnaBridge 172:7d866c31b3c5 3 *
AnnaBridge 172:7d866c31b3c5 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 172:7d866c31b3c5 5 * you may not use this file except in compliance with the License.
AnnaBridge 172:7d866c31b3c5 6 * You may obtain a copy of the License at
AnnaBridge 172:7d866c31b3c5 7 *
AnnaBridge 172:7d866c31b3c5 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 172:7d866c31b3c5 9 *
AnnaBridge 172:7d866c31b3c5 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 172:7d866c31b3c5 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 172:7d866c31b3c5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 172:7d866c31b3c5 13 * See the License for the specific language governing permissions and
AnnaBridge 172:7d866c31b3c5 14 * limitations under the License.
AnnaBridge 172:7d866c31b3c5 15 */
AnnaBridge 172:7d866c31b3c5 16 #include "pwmout_api.h"
AnnaBridge 172:7d866c31b3c5 17 #include "PeripheralNames.h"
AnnaBridge 172:7d866c31b3c5 18 #include "pinmap.h"
AnnaBridge 172:7d866c31b3c5 19 #include "tmpm066_tmrb.h"
AnnaBridge 172:7d866c31b3c5 20
AnnaBridge 172:7d866c31b3c5 21 #define CLOCK_FREQUENCY SystemCoreClock // input source clock
AnnaBridge 172:7d866c31b3c5 22 #define MAX_COUNTER_16B 0xFFFF
AnnaBridge 172:7d866c31b3c5 23 #define DEFAULT_CLOCK_DIVISION 32
AnnaBridge 172:7d866c31b3c5 24 #define DEFAULT_PERIOD 0.02f // 20ms
AnnaBridge 172:7d866c31b3c5 25
AnnaBridge 172:7d866c31b3c5 26 static const PinMap PinMap_PWM[] = {
AnnaBridge 172:7d866c31b3c5 27 {PD1, PWM_0, PIN_DATA(1, 1)},
AnnaBridge 172:7d866c31b3c5 28 {PD2, PWM_1, PIN_DATA(1, 1)},
AnnaBridge 172:7d866c31b3c5 29 {PD3, PWM_2, PIN_DATA(1, 1)},
AnnaBridge 172:7d866c31b3c5 30 {PF4, PWM_3, PIN_DATA(1, 1)},
AnnaBridge 172:7d866c31b3c5 31 {PF5, PWM_4, PIN_DATA(1, 1)},
AnnaBridge 172:7d866c31b3c5 32 {PJ0, PWM_5, PIN_DATA(1, 1)},
AnnaBridge 172:7d866c31b3c5 33 {PJ1, PWM_6, PIN_DATA(1, 1)},
AnnaBridge 172:7d866c31b3c5 34 {NC, NC, 0}
AnnaBridge 172:7d866c31b3c5 35 };
AnnaBridge 172:7d866c31b3c5 36
AnnaBridge 172:7d866c31b3c5 37 static const uint32_t prescale_tbl[] = {
AnnaBridge 172:7d866c31b3c5 38 2, 8, 32, 64, 128, 256, 512
AnnaBridge 172:7d866c31b3c5 39 };
AnnaBridge 172:7d866c31b3c5 40
AnnaBridge 172:7d866c31b3c5 41 void pwmout_init(pwmout_t *obj, PinName pin)
AnnaBridge 172:7d866c31b3c5 42 {
AnnaBridge 172:7d866c31b3c5 43 uint16_t counter = 0;
AnnaBridge 172:7d866c31b3c5 44 TMRB_FFOutputTypeDef FFStruct;
AnnaBridge 172:7d866c31b3c5 45 TMRB_InitTypeDef m_tmrb;
AnnaBridge 172:7d866c31b3c5 46
AnnaBridge 172:7d866c31b3c5 47 // Determine the pwm channel
AnnaBridge 172:7d866c31b3c5 48 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
AnnaBridge 172:7d866c31b3c5 49 //Assert input is valid
AnnaBridge 172:7d866c31b3c5 50 MBED_ASSERT(pwm != (PWMName)NC);
AnnaBridge 172:7d866c31b3c5 51
AnnaBridge 172:7d866c31b3c5 52 // Enable clock supply to TB0
AnnaBridge 172:7d866c31b3c5 53 CG_SetFcPeriphA(CG_FC_PERIPH_TMRB0_3, ENABLE);
AnnaBridge 172:7d866c31b3c5 54 CG_SetFcPeriphA(CG_FC_PERIPH_TMRB4_6, ENABLE);
AnnaBridge 172:7d866c31b3c5 55
AnnaBridge 172:7d866c31b3c5 56 switch (pwm) {
AnnaBridge 172:7d866c31b3c5 57 case PWM_0:
AnnaBridge 172:7d866c31b3c5 58 obj->channel = TSB_TB0;
AnnaBridge 172:7d866c31b3c5 59 break;
AnnaBridge 172:7d866c31b3c5 60 case PWM_1:
AnnaBridge 172:7d866c31b3c5 61 obj->channel = TSB_TB1;
AnnaBridge 172:7d866c31b3c5 62 break;
AnnaBridge 172:7d866c31b3c5 63 case PWM_2:
AnnaBridge 172:7d866c31b3c5 64 obj->channel = TSB_TB2;
AnnaBridge 172:7d866c31b3c5 65 break;
AnnaBridge 172:7d866c31b3c5 66 case PWM_3:
AnnaBridge 172:7d866c31b3c5 67 obj->channel = TSB_TB3;
AnnaBridge 172:7d866c31b3c5 68 break;
AnnaBridge 172:7d866c31b3c5 69 case PWM_4:
AnnaBridge 172:7d866c31b3c5 70 obj->channel = TSB_TB4;
AnnaBridge 172:7d866c31b3c5 71 break;
AnnaBridge 172:7d866c31b3c5 72 case PWM_5:
AnnaBridge 172:7d866c31b3c5 73 obj->channel = TSB_TB5;
AnnaBridge 172:7d866c31b3c5 74 CG_SetFcPeriphA(CG_FC_PERIPH_PORTJ, ENABLE);
AnnaBridge 172:7d866c31b3c5 75 break;
AnnaBridge 172:7d866c31b3c5 76 case PWM_6:
AnnaBridge 172:7d866c31b3c5 77 obj->channel = TSB_TB6;
AnnaBridge 172:7d866c31b3c5 78 CG_SetFcPeriphA(CG_FC_PERIPH_PORTJ, ENABLE);
AnnaBridge 172:7d866c31b3c5 79 break;
AnnaBridge 172:7d866c31b3c5 80 default:
AnnaBridge 172:7d866c31b3c5 81 obj->channel = NULL;
AnnaBridge 172:7d866c31b3c5 82 return;
AnnaBridge 172:7d866c31b3c5 83 }
AnnaBridge 172:7d866c31b3c5 84 // Set pin function as PWM
AnnaBridge 172:7d866c31b3c5 85 pinmap_pinout(pin, PinMap_PWM);
AnnaBridge 172:7d866c31b3c5 86 obj->pin = pin;
AnnaBridge 172:7d866c31b3c5 87 obj->period = DEFAULT_PERIOD;
AnnaBridge 172:7d866c31b3c5 88 // Enable channel
AnnaBridge 172:7d866c31b3c5 89 TMRB_Enable(obj->channel);
AnnaBridge 172:7d866c31b3c5 90 // Stops and clear count operation
AnnaBridge 172:7d866c31b3c5 91 TMRB_SetRunState(obj->channel, TMRB_STOP);
AnnaBridge 172:7d866c31b3c5 92 // Disables double buffering
AnnaBridge 172:7d866c31b3c5 93 TMRB_SetDoubleBuf(obj->channel, DISABLE);
AnnaBridge 172:7d866c31b3c5 94 // Set default period = 20ms, duty cycle = 0
AnnaBridge 172:7d866c31b3c5 95 obj->divisor = DEFAULT_CLOCK_DIVISION;
AnnaBridge 172:7d866c31b3c5 96 counter = (uint16_t)((DEFAULT_PERIOD * CLOCK_FREQUENCY) / obj->divisor);
AnnaBridge 172:7d866c31b3c5 97
AnnaBridge 172:7d866c31b3c5 98 // Init timer variable for using PPG mode
AnnaBridge 172:7d866c31b3c5 99 m_tmrb.Mode = TMRB_INTERVAL_TIMER;
AnnaBridge 172:7d866c31b3c5 100 m_tmrb.ClkDiv = TMRB_CLK_DIV_32;
AnnaBridge 172:7d866c31b3c5 101 m_tmrb.UpCntCtrl = TMRB_AUTO_CLEAR; // clear UC when matching value
AnnaBridge 172:7d866c31b3c5 102 m_tmrb.TrailingTiming = counter; // period = 20ms
AnnaBridge 172:7d866c31b3c5 103 m_tmrb.LeadingTiming = counter; // duty cycle = 0%
AnnaBridge 172:7d866c31b3c5 104
AnnaBridge 172:7d866c31b3c5 105 // Init timer function
AnnaBridge 172:7d866c31b3c5 106 TMRB_Init(obj->channel, &m_tmrb);
AnnaBridge 172:7d866c31b3c5 107 obj->trailing_timing = counter;
AnnaBridge 172:7d866c31b3c5 108 obj->leading_timing = counter;
AnnaBridge 172:7d866c31b3c5 109
AnnaBridge 172:7d866c31b3c5 110 // Enable double buffering
AnnaBridge 172:7d866c31b3c5 111 TMRB_SetDoubleBuf(obj->channel, ENABLE);
AnnaBridge 172:7d866c31b3c5 112
AnnaBridge 172:7d866c31b3c5 113 // Setting to TBxFF0 reverse trigger
AnnaBridge 172:7d866c31b3c5 114 FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_CLEAR;
AnnaBridge 172:7d866c31b3c5 115 FFStruct.FlipflopReverseTrg = TMRB_FLIPFLOP_MATCH_TRAILINGTIMING | TMRB_FLIPFLOP_MATCH_LEADINGTIMING;
AnnaBridge 172:7d866c31b3c5 116 TMRB_SetFlipFlop(obj->channel, &FFStruct);
AnnaBridge 172:7d866c31b3c5 117
AnnaBridge 172:7d866c31b3c5 118 // Start count operation
AnnaBridge 172:7d866c31b3c5 119 TMRB_SetRunState(obj->channel, TMRB_RUN);
AnnaBridge 172:7d866c31b3c5 120 }
AnnaBridge 172:7d866c31b3c5 121
AnnaBridge 172:7d866c31b3c5 122 void pwmout_free(pwmout_t *obj)
AnnaBridge 172:7d866c31b3c5 123 {
AnnaBridge 172:7d866c31b3c5 124 // Stops and clear count operation
AnnaBridge 172:7d866c31b3c5 125 TMRB_SetRunState(obj->channel, TMRB_STOP);
AnnaBridge 172:7d866c31b3c5 126 pwmout_write(obj,0);
AnnaBridge 172:7d866c31b3c5 127 obj->pin = NC;
AnnaBridge 172:7d866c31b3c5 128 obj->channel = NULL;
AnnaBridge 172:7d866c31b3c5 129 obj->trailing_timing = 0;
AnnaBridge 172:7d866c31b3c5 130 obj->leading_timing = 0;
AnnaBridge 172:7d866c31b3c5 131 obj->divisor = 0;
AnnaBridge 172:7d866c31b3c5 132 }
AnnaBridge 172:7d866c31b3c5 133
AnnaBridge 172:7d866c31b3c5 134 void pwmout_write(pwmout_t *obj, float value)
AnnaBridge 172:7d866c31b3c5 135 {
AnnaBridge 172:7d866c31b3c5 136 TMRB_FFOutputTypeDef FFStruct;
AnnaBridge 172:7d866c31b3c5 137
AnnaBridge 172:7d866c31b3c5 138 // Stop timer for setting clock again
AnnaBridge 172:7d866c31b3c5 139 TMRB_SetRunState(obj->channel, TMRB_STOP);
AnnaBridge 172:7d866c31b3c5 140 // values outside this range will be saturated to 0.0f or 1.0f
AnnaBridge 172:7d866c31b3c5 141 // Disable flip-flop reverse trigger when leading_timing and trailing_timing are duplicated
AnnaBridge 172:7d866c31b3c5 142 if (value <= 0.0f) {
AnnaBridge 172:7d866c31b3c5 143 value = 0;
AnnaBridge 172:7d866c31b3c5 144 FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_CLEAR;
AnnaBridge 172:7d866c31b3c5 145 FFStruct.FlipflopReverseTrg = TMRB_DISABLE_FLIPFLOP;
AnnaBridge 172:7d866c31b3c5 146 TMRB_SetFlipFlop(obj->channel, &FFStruct);
AnnaBridge 172:7d866c31b3c5 147 } else if (value >= 1.0f) {
AnnaBridge 172:7d866c31b3c5 148 value = 1;
AnnaBridge 172:7d866c31b3c5 149 FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_SET;
AnnaBridge 172:7d866c31b3c5 150 FFStruct.FlipflopReverseTrg = TMRB_DISABLE_FLIPFLOP;
AnnaBridge 172:7d866c31b3c5 151 TMRB_SetFlipFlop(obj->channel, &FFStruct);
AnnaBridge 172:7d866c31b3c5 152 } else {
AnnaBridge 172:7d866c31b3c5 153 FFStruct.FlipflopCtrl = TMRB_FLIPFLOP_CLEAR;
AnnaBridge 172:7d866c31b3c5 154 FFStruct.FlipflopReverseTrg = TMRB_FLIPFLOP_MATCH_TRAILINGTIMING | TMRB_FLIPFLOP_MATCH_LEADINGTIMING;
AnnaBridge 172:7d866c31b3c5 155 TMRB_SetFlipFlop(obj->channel, &FFStruct);
AnnaBridge 172:7d866c31b3c5 156 }
AnnaBridge 172:7d866c31b3c5 157
AnnaBridge 172:7d866c31b3c5 158 // Store the new leading_timing value
AnnaBridge 172:7d866c31b3c5 159 obj->leading_timing = obj->trailing_timing - (uint16_t)(obj->trailing_timing * value);
AnnaBridge 172:7d866c31b3c5 160
AnnaBridge 172:7d866c31b3c5 161 // Setting TBxRG0 register
AnnaBridge 172:7d866c31b3c5 162 TMRB_ChangeLeadingTiming(obj->channel, obj->leading_timing);
AnnaBridge 172:7d866c31b3c5 163 TMRB_SetRunState(obj->channel, TMRB_RUN);
AnnaBridge 172:7d866c31b3c5 164 }
AnnaBridge 172:7d866c31b3c5 165
AnnaBridge 172:7d866c31b3c5 166 float pwmout_read(pwmout_t *obj)
AnnaBridge 172:7d866c31b3c5 167 {
AnnaBridge 172:7d866c31b3c5 168 float duty_cycle = (float)(obj->trailing_timing - obj->leading_timing) / obj->trailing_timing;
AnnaBridge 172:7d866c31b3c5 169 return duty_cycle;
AnnaBridge 172:7d866c31b3c5 170 }
AnnaBridge 172:7d866c31b3c5 171
AnnaBridge 172:7d866c31b3c5 172 void pwmout_period(pwmout_t *obj, float seconds)
AnnaBridge 172:7d866c31b3c5 173 {
AnnaBridge 172:7d866c31b3c5 174 pwmout_period_us(obj, (int)(seconds * 1000000.0f));
AnnaBridge 172:7d866c31b3c5 175 }
AnnaBridge 172:7d866c31b3c5 176
AnnaBridge 172:7d866c31b3c5 177 void pwmout_period_ms(pwmout_t *obj, int ms)
AnnaBridge 172:7d866c31b3c5 178 {
AnnaBridge 172:7d866c31b3c5 179 pwmout_period_us(obj, ms * 1000);
AnnaBridge 172:7d866c31b3c5 180 }
AnnaBridge 172:7d866c31b3c5 181
AnnaBridge 172:7d866c31b3c5 182 // Set the PWM period, keeping the duty cycle the same.
AnnaBridge 172:7d866c31b3c5 183 void pwmout_period_us(pwmout_t *obj, int us)
AnnaBridge 172:7d866c31b3c5 184 {
AnnaBridge 172:7d866c31b3c5 185 float seconds = 0;
AnnaBridge 172:7d866c31b3c5 186 int cycles = 0;
AnnaBridge 172:7d866c31b3c5 187 uint32_t clkdiv = 0;
AnnaBridge 172:7d866c31b3c5 188 float duty_cycle = 0;
AnnaBridge 172:7d866c31b3c5 189 TMRB_InitTypeDef m_tmrb;
AnnaBridge 172:7d866c31b3c5 190 seconds = (float)((us) / 1000000.0f);
AnnaBridge 172:7d866c31b3c5 191 obj->period = seconds;
AnnaBridge 172:7d866c31b3c5 192
AnnaBridge 172:7d866c31b3c5 193 MBED_ASSERT(obj->channel != NULL);
AnnaBridge 172:7d866c31b3c5 194
AnnaBridge 172:7d866c31b3c5 195 // Select highest timer resolution
AnnaBridge 172:7d866c31b3c5 196 for (int i = 0; i < 7; ++i) {
AnnaBridge 172:7d866c31b3c5 197 cycles = (int)((CLOCK_FREQUENCY / prescale_tbl[i]) * seconds);
AnnaBridge 172:7d866c31b3c5 198 if (cycles <= MAX_COUNTER_16B) {
AnnaBridge 172:7d866c31b3c5 199 clkdiv = i + 1; // range 1:7
AnnaBridge 172:7d866c31b3c5 200 break;
AnnaBridge 172:7d866c31b3c5 201 }
AnnaBridge 172:7d866c31b3c5 202 }
AnnaBridge 172:7d866c31b3c5 203 // Stop timer for setting clock again
AnnaBridge 172:7d866c31b3c5 204 TMRB_SetRunState(obj->channel, TMRB_STOP);
AnnaBridge 172:7d866c31b3c5 205 // Restore the duty-cycle
AnnaBridge 172:7d866c31b3c5 206 duty_cycle = (float)(obj->trailing_timing - obj->leading_timing) / obj->trailing_timing;
AnnaBridge 172:7d866c31b3c5 207 obj->trailing_timing = cycles;
AnnaBridge 172:7d866c31b3c5 208 obj->leading_timing = (cycles - (uint16_t)(cycles * duty_cycle));
AnnaBridge 172:7d866c31b3c5 209
AnnaBridge 172:7d866c31b3c5 210 // Change the source clock division and period
AnnaBridge 172:7d866c31b3c5 211 m_tmrb.Mode = TMRB_INTERVAL_TIMER;
AnnaBridge 172:7d866c31b3c5 212 m_tmrb.ClkDiv = clkdiv;
AnnaBridge 172:7d866c31b3c5 213 m_tmrb.UpCntCtrl = TMRB_AUTO_CLEAR;
AnnaBridge 172:7d866c31b3c5 214 m_tmrb.TrailingTiming = obj->trailing_timing;
AnnaBridge 172:7d866c31b3c5 215 m_tmrb.LeadingTiming = obj->leading_timing;
AnnaBridge 172:7d866c31b3c5 216
AnnaBridge 172:7d866c31b3c5 217 //Init timer function
AnnaBridge 172:7d866c31b3c5 218 TMRB_Init(obj->channel, &m_tmrb);
AnnaBridge 172:7d866c31b3c5 219 //Start timer function
AnnaBridge 172:7d866c31b3c5 220 TMRB_SetRunState(obj->channel, TMRB_RUN);
AnnaBridge 172:7d866c31b3c5 221 }
AnnaBridge 172:7d866c31b3c5 222
AnnaBridge 172:7d866c31b3c5 223 void pwmout_pulsewidth(pwmout_t *obj, float seconds)
AnnaBridge 172:7d866c31b3c5 224 {
AnnaBridge 172:7d866c31b3c5 225 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
AnnaBridge 172:7d866c31b3c5 226 }
AnnaBridge 172:7d866c31b3c5 227
AnnaBridge 172:7d866c31b3c5 228 void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
AnnaBridge 172:7d866c31b3c5 229 {
AnnaBridge 172:7d866c31b3c5 230 pwmout_pulsewidth_us(obj, ms * 1000);
AnnaBridge 172:7d866c31b3c5 231 }
AnnaBridge 172:7d866c31b3c5 232
AnnaBridge 172:7d866c31b3c5 233 void pwmout_pulsewidth_us(pwmout_t *obj, int us)
AnnaBridge 172:7d866c31b3c5 234 {
AnnaBridge 172:7d866c31b3c5 235 float seconds = 0;
AnnaBridge 172:7d866c31b3c5 236 float value = 0;
AnnaBridge 172:7d866c31b3c5 237
AnnaBridge 172:7d866c31b3c5 238 MBED_ASSERT(obj->channel != NULL);
AnnaBridge 172:7d866c31b3c5 239
AnnaBridge 172:7d866c31b3c5 240 seconds = (float)(us / 1000000.0f);
AnnaBridge 172:7d866c31b3c5 241 value = (((seconds / obj->period) * 100.0f) / 100.0f);
AnnaBridge 172:7d866c31b3c5 242 pwmout_write(obj, value);
AnnaBridge 172:7d866c31b3c5 243 }