mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 *
elessair 0:f269e3021894 16 * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
elessair 0:f269e3021894 17 */
elessair 0:f269e3021894 18 #include "mbed_assert.h"
elessair 0:f269e3021894 19 #include "pwmout_api.h"
elessair 0:f269e3021894 20 #include "cmsis.h"
elessair 0:f269e3021894 21 #include "pinmap.h"
elessair 0:f269e3021894 22 #include "mbed_error.h"
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 // PWM implementation for the LPC43xx using State Configurable Timer (SCT)
elessair 0:f269e3021894 25 // * PWM_0 to PWM_15 on mbed use CTOUT_0 to CTOUT_15 outputs on LPC43xx
elessair 0:f269e3021894 26 // * Event 0 is PWM period, events 1 to PWM_EVENT_MAX are PWM channels
elessair 0:f269e3021894 27 // * Default is unified 32-bit timer, but could be configured to use
elessair 0:f269e3021894 28 // a 16-bit timer so a timer is available for other SCT functions
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 // configuration options
elessair 0:f269e3021894 31 #define PWM_FREQ_BASE 1000000 // Base frequency 1 MHz = 1000000
elessair 0:f269e3021894 32 #define PWM_MODE 1 // 0 = 32-bit, 1 = 16-bit low, 2 = 16-bit high
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 // macros
elessair 0:f269e3021894 35 #define PWM_SETCOUNT(x) (x - 1) // set count value
elessair 0:f269e3021894 36 #define PWM_GETCOUNT(x) (x + 1) // get count value
elessair 0:f269e3021894 37 #if (PWM_MODE == 0) // unified 32-bit counter, events 1 to 15
elessair 0:f269e3021894 38 #define PWM_EVENT_MAX (CONFIG_SCT_nEV - 1) // Max PWM channels
elessair 0:f269e3021894 39 #define PWM_CONFIG SCT_CONFIG_32BIT_COUNTER // default config
elessair 0:f269e3021894 40 #define PWM_CTRL &LPC_SCT->CTRL_U // control register
elessair 0:f269e3021894 41 #define PWM_HALT SCT_CTRL_HALT_L // halt counter
elessair 0:f269e3021894 42 #define PWM_CLEAR SCT_CTRL_CLRCTR_L // clock clear
elessair 0:f269e3021894 43 #define PWM_PRE(x) SCT_CTRL_PRE_L(x) // clock prescale
elessair 0:f269e3021894 44 #define PWM_EVT_MASK (1 << 12) // event control mask
elessair 0:f269e3021894 45 #define PWM_LIMIT &LPC_SCT->LIMIT_L // limit register
elessair 0:f269e3021894 46 #define PWM_MATCH(x) &LPC_SCT->MATCH[x].U // match register
elessair 0:f269e3021894 47 #define PWM_MR(x) &LPC_SCT->MATCHREL[x].U // 32-bit match reload register
elessair 0:f269e3021894 48 #elif (PWM_MODE == 1) // 16-bit low counter, events 1 to 7
elessair 0:f269e3021894 49 #define PWM_EVENT_MAX (CONFIG_SCT_nEV/2 - 1) // Max PWM channels
elessair 0:f269e3021894 50 #define PWM_CONFIG SCT_CONFIG_16BIT_COUNTER // default config
elessair 0:f269e3021894 51 #define PWM_CTRL &LPC_SCT->CTRL_L // control register
elessair 0:f269e3021894 52 #define PWM_HALT SCT_CTRL_HALT_L // halt counter
elessair 0:f269e3021894 53 #define PWM_CLEAR SCT_CTRL_CLRCTR_L // clock clear
elessair 0:f269e3021894 54 #define PWM_PRE(x) SCT_CTRL_PRE_L(x) // clock prescale
elessair 0:f269e3021894 55 #define PWM_EVT_MASK (1 << 12) // event control mask
elessair 0:f269e3021894 56 #define PWM_LIMIT &LPC_SCT->LIMIT_L // limit register
elessair 0:f269e3021894 57 #define PWM_MATCH(x) &LPC_SCT->MATCH[x].L // match register
elessair 0:f269e3021894 58 #define PWM_MR(x) &LPC_SCT->MATCHREL[x].L // 16-bit match reload register
elessair 0:f269e3021894 59 #elif (PWM_MODE == 2) // 16-bit high counter, events 1 to 7
elessair 0:f269e3021894 60 // [TODO] use events 8 to 15 on mode 2
elessair 0:f269e3021894 61 #define PWM_EVENT_MAX (CONFIG_SCT_nEV/2 - 1) // Max PWM channels
elessair 0:f269e3021894 62 #define PWM_CONFIG SCT_CONFIG_16BIT_COUNTER // default config
elessair 0:f269e3021894 63 #define PWM_CTRL &LPC_SCT->CTRL_H // control register
elessair 0:f269e3021894 64 #define PWM_HALT SCT_CTRL_HALT_L // halt counter
elessair 0:f269e3021894 65 #define PWM_CLEAR SCT_CTRL_CLRCTR_L // clock clear
elessair 0:f269e3021894 66 #define PWM_PRE(x) SCT_CTRL_PRE_L(x) // clock prescale
elessair 0:f269e3021894 67 #define PWM_EVT_MASK ((1 << 4) | (1 << 12)) // event control mask
elessair 0:f269e3021894 68 #define PWM_LIMIT &LPC_SCT->LIMIT_H // limit register
elessair 0:f269e3021894 69 #define PWM_MATCH(x) &LPC_SCT->MATCH[x].H // match register
elessair 0:f269e3021894 70 #define PWM_MR(x) &LPC_SCT->MATCHREL[x].H // 16-bit match reload register
elessair 0:f269e3021894 71 #else
elessair 0:f269e3021894 72 #error "PWM mode not implemented"
elessair 0:f269e3021894 73 #endif
elessair 0:f269e3021894 74 #define PWM_MR0 PWM_MR(0) // MR register 0 is for period
elessair 0:f269e3021894 75
elessair 0:f269e3021894 76 static uint8_t event = 0;
elessair 0:f269e3021894 77
elessair 0:f269e3021894 78 // PORT ID, PWM ID, Pin function
elessair 0:f269e3021894 79 static const PinMap PinMap_PWM[] = {
elessair 0:f269e3021894 80 {P1_1, PWM_7, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 81 {P1_2, PWM_6, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 82 {P1_3, PWM_8, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 83 {P1_4, PWM_9, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 84 {P1_5, PWM_10, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 85 {P1_7, PWM_13, (SCU_PINIO_FAST | 2)},
elessair 0:f269e3021894 86 {P1_8, PWM_12, (SCU_PINIO_FAST | 2)},
elessair 0:f269e3021894 87 {P1_9, PWM_11, (SCU_PINIO_FAST | 2)},
elessair 0:f269e3021894 88 {P1_10, PWM_14, (SCU_PINIO_FAST | 2)},
elessair 0:f269e3021894 89 {P1_11, PWM_15, (SCU_PINIO_FAST | 2)},
elessair 0:f269e3021894 90 {P2_7, PWM_1, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 91 {P2_8, PWM_0, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 92 {P2_9, PWM_3, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 93 {P2_10, PWM_2, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 94 {P2_11, PWM_5, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 95 {P2_12, PWM_4, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 96 {P4_1, PWM_1, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 97 {P4_2, PWM_0, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 98 {P4_3, PWM_3, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 99 {P4_4, PWM_2, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 100 {P4_5, PWM_5, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 101 {P4_6, PWM_4, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 102 {P6_5, PWM_6, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 103 {P6_12, PWM_7, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 104 {P7_0, PWM_14, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 105 {P7_1, PWM_15, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 106 {P7_4, PWM_13, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 107 {P7_5, PWM_12, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 108 {P7_6, PWM_11, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 109 {P7_7, PWM_8, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 110 {PA_4, PWM_9, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 111 {PB_0, PWM_10, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 112 {PB_1, PWM_6, (SCU_PINIO_FAST | 5)},
elessair 0:f269e3021894 113 {PB_2, PWM_7, (SCU_PINIO_FAST | 5)},
elessair 0:f269e3021894 114 {PB_3, PWM_8, (SCU_PINIO_FAST | 5)},
elessair 0:f269e3021894 115 {PD_0, PWM_15, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 116 {PD_2, PWM_7, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 117 {PD_3, PWM_6, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 118 {PD_4, PWM_8, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 119 {PD_5, PWM_9, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 120 {PD_6, PWM_10, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 121 {PD_9, PWM_13, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 122 {PD_11, PWM_14, (SCU_PINIO_FAST | 6)},
elessair 0:f269e3021894 123 {PD_12, PWM_10, (SCU_PINIO_FAST | 6)},
elessair 0:f269e3021894 124 {PD_13, PWM_13, (SCU_PINIO_FAST | 6)},
elessair 0:f269e3021894 125 {PD_14, PWM_11, (SCU_PINIO_FAST | 6)},
elessair 0:f269e3021894 126 {PD_15, PWM_8, (SCU_PINIO_FAST | 6)},
elessair 0:f269e3021894 127 {PD_16, PWM_12, (SCU_PINIO_FAST | 6)},
elessair 0:f269e3021894 128 {PE_5, PWM_3, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 129 {PE_6, PWM_2, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 130 {PE_7, PWM_5, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 131 {PE_8, PWM_4, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 132 {PE_11, PWM_12, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 133 {PE_12, PWM_11, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 134 {PE_13, PWM_14, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 135 {PE_15, PWM_0, (SCU_PINIO_FAST | 1)},
elessair 0:f269e3021894 136 {PF_9, PWM_1, (SCU_PINIO_FAST | 2)},
elessair 0:f269e3021894 137 {NC, NC, 0}
elessair 0:f269e3021894 138 };
elessair 0:f269e3021894 139
elessair 0:f269e3021894 140 static unsigned int pwm_clock_mhz;
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 static void _pwmout_dev_init() {
elessair 0:f269e3021894 143 uint32_t i;
elessair 0:f269e3021894 144
elessair 0:f269e3021894 145 // set SCT clock and config
elessair 0:f269e3021894 146 LPC_CCU1->CLKCCU[CLK_MX_SCT].CFG = (1 << 0); // enable SCT clock in CCU1
elessair 0:f269e3021894 147 LPC_SCT->CONFIG |= PWM_CONFIG; // set config options
elessair 0:f269e3021894 148 *PWM_CTRL |= PWM_HALT; // set HALT bit to stop counter
elessair 0:f269e3021894 149 // clear counter and set prescaler for desired freq
elessair 0:f269e3021894 150 *PWM_CTRL |= PWM_CLEAR | PWM_PRE(SystemCoreClock / PWM_FREQ_BASE - 1);
elessair 0:f269e3021894 151 pwm_clock_mhz = PWM_FREQ_BASE / 1000000;
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 // configure SCT events
elessair 0:f269e3021894 154 for (i = 0; i < PWM_EVENT_MAX; i++) {
elessair 0:f269e3021894 155 *PWM_MATCH(i) = 0; // match register
elessair 0:f269e3021894 156 *PWM_MR(i) = 0; // match reload register
elessair 0:f269e3021894 157 LPC_SCT->EVENT[i].STATE = 0xFFFFFFFF; // event happens in all states
elessair 0:f269e3021894 158 LPC_SCT->EVENT[i].CTRL = (i << 0) | PWM_EVT_MASK; // match condition only
elessair 0:f269e3021894 159 }
elessair 0:f269e3021894 160 *PWM_LIMIT = (1 << 0) ; // set event 0 as limit
elessair 0:f269e3021894 161 // initialize period to 20ms: standard for servos, and fine for e.g. brightness control
elessair 0:f269e3021894 162 *PWM_MR0 = PWM_SETCOUNT((uint32_t)(((20 * PWM_FREQ_BASE) / 1000000) * 1000));
elessair 0:f269e3021894 163
elessair 0:f269e3021894 164 // initialize SCT outputs
elessair 0:f269e3021894 165 for (i = 0; i < CONFIG_SCT_nOU; i++) {
elessair 0:f269e3021894 166 LPC_SCT->OUT[i].SET = (1 << 0); // event 0 will set SCTOUT_xx
elessair 0:f269e3021894 167 LPC_SCT->OUT[i].CLR = 0; // set clear event when duty cycle
elessair 0:f269e3021894 168 }
elessair 0:f269e3021894 169 LPC_SCT->OUTPUT = 0; // default outputs to clear
elessair 0:f269e3021894 170
elessair 0:f269e3021894 171 *PWM_CTRL &= ~PWM_HALT; // clear HALT bit to start counter
elessair 0:f269e3021894 172 }
elessair 0:f269e3021894 173
elessair 0:f269e3021894 174 void pwmout_init(pwmout_t* obj, PinName pin) {
elessair 0:f269e3021894 175 // determine the channel
elessair 0:f269e3021894 176 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
elessair 0:f269e3021894 177 MBED_ASSERT((pwm != (PWMName)NC) && (event < PWM_EVENT_MAX));
elessair 0:f269e3021894 178
elessair 0:f269e3021894 179 // init SCT clock and outputs on first PWM init
elessair 0:f269e3021894 180 if (event == 0) {
elessair 0:f269e3021894 181 _pwmout_dev_init();
elessair 0:f269e3021894 182 }
elessair 0:f269e3021894 183 // init PWM object
elessair 0:f269e3021894 184 event++;
elessair 0:f269e3021894 185 obj->pwm = pwm; // pwm output
elessair 0:f269e3021894 186 obj->mr = event; // index of match reload register
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 // initial duty cycle is 0
elessair 0:f269e3021894 189 pwmout_write(obj, 0);
elessair 0:f269e3021894 190
elessair 0:f269e3021894 191 // Wire pinout
elessair 0:f269e3021894 192 pinmap_pinout(pin, PinMap_PWM);
elessair 0:f269e3021894 193 }
elessair 0:f269e3021894 194
elessair 0:f269e3021894 195 void pwmout_free(pwmout_t* obj) {
elessair 0:f269e3021894 196 // [TODO]
elessair 0:f269e3021894 197 }
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 void pwmout_write(pwmout_t* obj, float value) {
elessair 0:f269e3021894 200 if (value < 0.0f) {
elessair 0:f269e3021894 201 value = 0.0;
elessair 0:f269e3021894 202 } else if (value > 1.0f) {
elessair 0:f269e3021894 203 value = 1.0;
elessair 0:f269e3021894 204 }
elessair 0:f269e3021894 205
elessair 0:f269e3021894 206 // set new pulse width
elessair 0:f269e3021894 207 uint32_t us = (uint32_t)((float)PWM_GETCOUNT(*PWM_MR0) * value) * pwm_clock_mhz;
elessair 0:f269e3021894 208 pwmout_pulsewidth_us(obj, us);
elessair 0:f269e3021894 209 }
elessair 0:f269e3021894 210
elessair 0:f269e3021894 211 float pwmout_read(pwmout_t* obj) {
elessair 0:f269e3021894 212 float v = (float)PWM_GETCOUNT(*PWM_MR(obj->mr)) / (float)PWM_GETCOUNT(*PWM_MR0);
elessair 0:f269e3021894 213 return (v > 1.0f) ? (1.0f) : (v);
elessair 0:f269e3021894 214 }
elessair 0:f269e3021894 215
elessair 0:f269e3021894 216 void pwmout_period(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 217 pwmout_period_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 218 }
elessair 0:f269e3021894 219
elessair 0:f269e3021894 220 void pwmout_period_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 221 pwmout_period_us(obj, ms * 1000);
elessair 0:f269e3021894 222 }
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224 // Set the PWM period, keeping the duty cycle the same.
elessair 0:f269e3021894 225 void pwmout_period_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 226 // calculate number of ticks
elessair 0:f269e3021894 227 uint32_t ticks = pwm_clock_mhz * us;
elessair 0:f269e3021894 228 uint32_t old_ticks = PWM_GETCOUNT(*PWM_MR0);
elessair 0:f269e3021894 229 uint32_t i, v;
elessair 0:f269e3021894 230
elessair 0:f269e3021894 231 // set new period
elessair 0:f269e3021894 232 *PWM_MR0 = PWM_SETCOUNT(ticks);
elessair 0:f269e3021894 233
elessair 0:f269e3021894 234 // Scale pulse widths to preserve the duty ratio
elessair 0:f269e3021894 235 for (i = 1; i < PWM_EVENT_MAX; i++) {
elessair 0:f269e3021894 236 v = PWM_GETCOUNT(*PWM_MR(i));
elessair 0:f269e3021894 237 if (v > 1) {
elessair 0:f269e3021894 238 v = (v * ticks) / old_ticks;
elessair 0:f269e3021894 239 *PWM_MR(i) = PWM_SETCOUNT(v);
elessair 0:f269e3021894 240 }
elessair 0:f269e3021894 241 }
elessair 0:f269e3021894 242 }
elessair 0:f269e3021894 243
elessair 0:f269e3021894 244 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
elessair 0:f269e3021894 245 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
elessair 0:f269e3021894 246 }
elessair 0:f269e3021894 247
elessair 0:f269e3021894 248 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
elessair 0:f269e3021894 249 pwmout_pulsewidth_us(obj, ms * 1000);
elessair 0:f269e3021894 250 }
elessair 0:f269e3021894 251
elessair 0:f269e3021894 252 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
elessair 0:f269e3021894 253 // calculate number of ticks
elessair 0:f269e3021894 254 uint32_t v = pwm_clock_mhz * us;
elessair 0:f269e3021894 255 //MBED_ASSERT(PWM_GETCOUNT(*PWM_MR0) >= v);
elessair 0:f269e3021894 256
elessair 0:f269e3021894 257 if (v > 0) {
elessair 0:f269e3021894 258 // set new match register value and enable SCT output
elessair 0:f269e3021894 259 *PWM_MR(obj->mr) = PWM_SETCOUNT(v);
elessair 0:f269e3021894 260 LPC_SCT->OUT[obj->pwm].CLR = (1 << obj->mr); // on event will clear PWM_XX
elessair 0:f269e3021894 261 } else {
elessair 0:f269e3021894 262 // set match to zero and disable SCT output
elessair 0:f269e3021894 263 *PWM_MR(obj->mr) = 0;
elessair 0:f269e3021894 264 LPC_SCT->OUT[obj->pwm].CLR = 0;
elessair 0:f269e3021894 265 }
elessair 0:f269e3021894 266 }