mbed-os
Fork of mbed-os by
targets/TARGET_NXP/TARGET_LPC176X/pwmout_api.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New 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 | #include "mbed_assert.h" |
elessair | 0:f269e3021894 | 17 | #include "pwmout_api.h" |
elessair | 0:f269e3021894 | 18 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 19 | #include "pinmap.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #define TCR_CNT_EN 0x00000001 |
elessair | 0:f269e3021894 | 22 | #define TCR_RESET 0x00000002 |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | // PORT ID, PWM ID, Pin function |
elessair | 0:f269e3021894 | 25 | static const PinMap PinMap_PWM[] = { |
elessair | 0:f269e3021894 | 26 | {P1_18, PWM_1, 2}, |
elessair | 0:f269e3021894 | 27 | {P1_20, PWM_2, 2}, |
elessair | 0:f269e3021894 | 28 | {P1_21, PWM_3, 2}, |
elessair | 0:f269e3021894 | 29 | {P1_23, PWM_4, 2}, |
elessair | 0:f269e3021894 | 30 | {P1_24, PWM_5, 2}, |
elessair | 0:f269e3021894 | 31 | {P1_26, PWM_6, 2}, |
elessair | 0:f269e3021894 | 32 | {P2_0 , PWM_1, 1}, |
elessair | 0:f269e3021894 | 33 | {P2_1 , PWM_2, 1}, |
elessair | 0:f269e3021894 | 34 | {P2_2 , PWM_3, 1}, |
elessair | 0:f269e3021894 | 35 | {P2_3 , PWM_4, 1}, |
elessair | 0:f269e3021894 | 36 | {P2_4 , PWM_5, 1}, |
elessair | 0:f269e3021894 | 37 | {P2_5 , PWM_6, 1}, |
elessair | 0:f269e3021894 | 38 | {P3_25, PWM_2, 3}, |
elessair | 0:f269e3021894 | 39 | {P3_26, PWM_3, 3}, |
elessair | 0:f269e3021894 | 40 | {NC, NC, 0} |
elessair | 0:f269e3021894 | 41 | }; |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | __IO uint32_t *PWM_MATCH[] = { |
elessair | 0:f269e3021894 | 44 | &(LPC_PWM1->MR0), |
elessair | 0:f269e3021894 | 45 | &(LPC_PWM1->MR1), |
elessair | 0:f269e3021894 | 46 | &(LPC_PWM1->MR2), |
elessair | 0:f269e3021894 | 47 | &(LPC_PWM1->MR3), |
elessair | 0:f269e3021894 | 48 | &(LPC_PWM1->MR4), |
elessair | 0:f269e3021894 | 49 | &(LPC_PWM1->MR5), |
elessair | 0:f269e3021894 | 50 | &(LPC_PWM1->MR6) |
elessair | 0:f269e3021894 | 51 | }; |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | #define TCR_PWM_EN 0x00000008 |
elessair | 0:f269e3021894 | 54 | |
elessair | 0:f269e3021894 | 55 | static unsigned int pwm_clock_mhz; |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | void pwmout_init(pwmout_t* obj, PinName pin) { |
elessair | 0:f269e3021894 | 58 | // determine the channel |
elessair | 0:f269e3021894 | 59 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
elessair | 0:f269e3021894 | 60 | MBED_ASSERT(pwm != (PWMName)NC); |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | obj->pwm = pwm; |
elessair | 0:f269e3021894 | 63 | obj->MR = PWM_MATCH[pwm]; |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | // ensure the power is on |
elessair | 0:f269e3021894 | 66 | LPC_SC->PCONP |= 1 << 6; |
elessair | 0:f269e3021894 | 67 | |
elessair | 0:f269e3021894 | 68 | // ensure clock to /4 |
elessair | 0:f269e3021894 | 69 | LPC_SC->PCLKSEL0 &= ~(0x3 << 12); // pclk = /4 |
elessair | 0:f269e3021894 | 70 | LPC_PWM1->PR = 0; // no pre-scale |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | // ensure single PWM mode |
elessair | 0:f269e3021894 | 73 | LPC_PWM1->MCR = 1 << 1; // reset TC on match 0 |
elessair | 0:f269e3021894 | 74 | |
elessair | 0:f269e3021894 | 75 | // enable the specific PWM output |
elessair | 0:f269e3021894 | 76 | LPC_PWM1->PCR |= 1 << (8 + pwm); |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | pwm_clock_mhz = SystemCoreClock / 4000000; |
elessair | 0:f269e3021894 | 79 | |
elessair | 0:f269e3021894 | 80 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
elessair | 0:f269e3021894 | 81 | pwmout_period_ms(obj, 20); |
elessair | 0:f269e3021894 | 82 | pwmout_write (obj, 0); |
elessair | 0:f269e3021894 | 83 | |
elessair | 0:f269e3021894 | 84 | // Wire pinout |
elessair | 0:f269e3021894 | 85 | pinmap_pinout(pin, PinMap_PWM); |
elessair | 0:f269e3021894 | 86 | } |
elessair | 0:f269e3021894 | 87 | |
elessair | 0:f269e3021894 | 88 | void pwmout_free(pwmout_t* obj) { |
elessair | 0:f269e3021894 | 89 | // [TODO] |
elessair | 0:f269e3021894 | 90 | } |
elessair | 0:f269e3021894 | 91 | |
elessair | 0:f269e3021894 | 92 | void pwmout_write(pwmout_t* obj, float value) { |
elessair | 0:f269e3021894 | 93 | if (value < 0.0f) { |
elessair | 0:f269e3021894 | 94 | value = 0.0; |
elessair | 0:f269e3021894 | 95 | } else if (value > 1.0f) { |
elessair | 0:f269e3021894 | 96 | value = 1.0; |
elessair | 0:f269e3021894 | 97 | } |
elessair | 0:f269e3021894 | 98 | |
elessair | 0:f269e3021894 | 99 | // set channel match to percentage |
elessair | 0:f269e3021894 | 100 | uint32_t v = (uint32_t)((float)(LPC_PWM1->MR0) * value); |
elessair | 0:f269e3021894 | 101 | |
elessair | 0:f269e3021894 | 102 | // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout |
elessair | 0:f269e3021894 | 103 | if (v == LPC_PWM1->MR0) { |
elessair | 0:f269e3021894 | 104 | v++; |
elessair | 0:f269e3021894 | 105 | } |
elessair | 0:f269e3021894 | 106 | |
elessair | 0:f269e3021894 | 107 | *obj->MR = v; |
elessair | 0:f269e3021894 | 108 | |
elessair | 0:f269e3021894 | 109 | // accept on next period start |
elessair | 0:f269e3021894 | 110 | LPC_PWM1->LER |= 1 << obj->pwm; |
elessair | 0:f269e3021894 | 111 | } |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | float pwmout_read(pwmout_t* obj) { |
elessair | 0:f269e3021894 | 114 | float v = (float)(*obj->MR) / (float)(LPC_PWM1->MR0); |
elessair | 0:f269e3021894 | 115 | return (v > 1.0f) ? (1.0f) : (v); |
elessair | 0:f269e3021894 | 116 | } |
elessair | 0:f269e3021894 | 117 | |
elessair | 0:f269e3021894 | 118 | void pwmout_period(pwmout_t* obj, float seconds) { |
elessair | 0:f269e3021894 | 119 | pwmout_period_us(obj, seconds * 1000000.0f); |
elessair | 0:f269e3021894 | 120 | } |
elessair | 0:f269e3021894 | 121 | |
elessair | 0:f269e3021894 | 122 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
elessair | 0:f269e3021894 | 123 | pwmout_period_us(obj, ms * 1000); |
elessair | 0:f269e3021894 | 124 | } |
elessair | 0:f269e3021894 | 125 | |
elessair | 0:f269e3021894 | 126 | // Set the PWM period, keeping the duty cycle the same. |
elessair | 0:f269e3021894 | 127 | void pwmout_period_us(pwmout_t* obj, int us) { |
elessair | 0:f269e3021894 | 128 | // calculate number of ticks |
elessair | 0:f269e3021894 | 129 | uint32_t ticks = pwm_clock_mhz * us; |
elessair | 0:f269e3021894 | 130 | |
elessair | 0:f269e3021894 | 131 | // set reset |
elessair | 0:f269e3021894 | 132 | LPC_PWM1->TCR = TCR_RESET; |
elessair | 0:f269e3021894 | 133 | |
elessair | 0:f269e3021894 | 134 | // set the global match register |
elessair | 0:f269e3021894 | 135 | LPC_PWM1->MR0 = ticks; |
elessair | 0:f269e3021894 | 136 | |
elessair | 0:f269e3021894 | 137 | // Scale the pulse width to preserve the duty ratio |
elessair | 0:f269e3021894 | 138 | if (LPC_PWM1->MR0 > 0) { |
elessair | 0:f269e3021894 | 139 | *obj->MR = (*obj->MR * ticks) / LPC_PWM1->MR0; |
elessair | 0:f269e3021894 | 140 | } |
elessair | 0:f269e3021894 | 141 | |
elessair | 0:f269e3021894 | 142 | // set the channel latch to update value at next period start |
elessair | 0:f269e3021894 | 143 | LPC_PWM1->LER |= 1 << 0; |
elessair | 0:f269e3021894 | 144 | |
elessair | 0:f269e3021894 | 145 | // enable counter and pwm, clear reset |
elessair | 0:f269e3021894 | 146 | LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN; |
elessair | 0:f269e3021894 | 147 | } |
elessair | 0:f269e3021894 | 148 | |
elessair | 0:f269e3021894 | 149 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
elessair | 0:f269e3021894 | 150 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
elessair | 0:f269e3021894 | 151 | } |
elessair | 0:f269e3021894 | 152 | |
elessair | 0:f269e3021894 | 153 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
elessair | 0:f269e3021894 | 154 | pwmout_pulsewidth_us(obj, ms * 1000); |
elessair | 0:f269e3021894 | 155 | } |
elessair | 0:f269e3021894 | 156 | |
elessair | 0:f269e3021894 | 157 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
elessair | 0:f269e3021894 | 158 | // calculate number of ticks |
elessair | 0:f269e3021894 | 159 | uint32_t v = pwm_clock_mhz * us; |
elessair | 0:f269e3021894 | 160 | |
elessair | 0:f269e3021894 | 161 | // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout |
elessair | 0:f269e3021894 | 162 | if (v == LPC_PWM1->MR0) { |
elessair | 0:f269e3021894 | 163 | v++; |
elessair | 0:f269e3021894 | 164 | } |
elessair | 0:f269e3021894 | 165 | |
elessair | 0:f269e3021894 | 166 | // set the match register value |
elessair | 0:f269e3021894 | 167 | *obj->MR = v; |
elessair | 0:f269e3021894 | 168 | |
elessair | 0:f269e3021894 | 169 | // set the channel latch to update value at next period start |
elessair | 0:f269e3021894 | 170 | LPC_PWM1->LER |= 1 << obj->pwm; |
elessair | 0:f269e3021894 | 171 | } |