mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC408X/pwmout_api.c@15:4892fe388435, 2013-08-07 (annotated)
- Committer:
- bogdanm
- Date:
- Wed Aug 07 16:43:59 2013 +0300
- Revision:
- 15:4892fe388435
- Child:
- 29:6ac4027eff2b
Added LPC4088 target and interrupt chaining code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 15:4892fe388435 | 1 | /* mbed Microcontroller Library |
bogdanm | 15:4892fe388435 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 15:4892fe388435 | 3 | * |
bogdanm | 15:4892fe388435 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 15:4892fe388435 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 15:4892fe388435 | 6 | * You may obtain a copy of the License at |
bogdanm | 15:4892fe388435 | 7 | * |
bogdanm | 15:4892fe388435 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 15:4892fe388435 | 9 | * |
bogdanm | 15:4892fe388435 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 15:4892fe388435 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 15:4892fe388435 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 15:4892fe388435 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 15:4892fe388435 | 14 | * limitations under the License. |
bogdanm | 15:4892fe388435 | 15 | */ |
bogdanm | 15:4892fe388435 | 16 | #include "pwmout_api.h" |
bogdanm | 15:4892fe388435 | 17 | #include "cmsis.h" |
bogdanm | 15:4892fe388435 | 18 | #include "pinmap.h" |
bogdanm | 15:4892fe388435 | 19 | #include "error.h" |
bogdanm | 15:4892fe388435 | 20 | |
bogdanm | 15:4892fe388435 | 21 | #define TCR_CNT_EN 0x00000001 |
bogdanm | 15:4892fe388435 | 22 | #define TCR_RESET 0x00000002 |
bogdanm | 15:4892fe388435 | 23 | |
bogdanm | 15:4892fe388435 | 24 | // PORT ID, PWM ID, Pin function |
bogdanm | 15:4892fe388435 | 25 | static const PinMap PinMap_PWM[] = { |
bogdanm | 15:4892fe388435 | 26 | {P1_2, PWM0_1, 3}, |
bogdanm | 15:4892fe388435 | 27 | {P1_3, PWM0_2, 3}, |
bogdanm | 15:4892fe388435 | 28 | {P1_5, PWM0_3, 3}, |
bogdanm | 15:4892fe388435 | 29 | {P1_6, PWM0_4, 3}, |
bogdanm | 15:4892fe388435 | 30 | {P1_7, PWM0_5, 3}, |
bogdanm | 15:4892fe388435 | 31 | {P1_11, PWM0_6, 3}, |
bogdanm | 15:4892fe388435 | 32 | {P1_18, PWM1_1, 2}, |
bogdanm | 15:4892fe388435 | 33 | {P1_20, PWM1_2, 2}, |
bogdanm | 15:4892fe388435 | 34 | {P1_21, PWM1_3, 2}, |
bogdanm | 15:4892fe388435 | 35 | {P1_23, PWM1_4, 2}, |
bogdanm | 15:4892fe388435 | 36 | {P1_24, PWM1_5, 2}, |
bogdanm | 15:4892fe388435 | 37 | {P1_26, PWM1_6, 2}, |
bogdanm | 15:4892fe388435 | 38 | {P2_0, PWM1_1, 1}, |
bogdanm | 15:4892fe388435 | 39 | {P2_1, PWM1_2, 1}, |
bogdanm | 15:4892fe388435 | 40 | {P2_2, PWM1_3, 1}, |
bogdanm | 15:4892fe388435 | 41 | {P2_3, PWM1_4, 1}, |
bogdanm | 15:4892fe388435 | 42 | {P2_4, PWM1_5, 1}, |
bogdanm | 15:4892fe388435 | 43 | {P2_5, PWM1_6, 1}, |
bogdanm | 15:4892fe388435 | 44 | {P3_16, PWM0_1, 2}, |
bogdanm | 15:4892fe388435 | 45 | {P3_17, PWM0_2, 2}, |
bogdanm | 15:4892fe388435 | 46 | {P3_18, PWM0_3, 2}, |
bogdanm | 15:4892fe388435 | 47 | {P3_19, PWM0_4, 2}, |
bogdanm | 15:4892fe388435 | 48 | {P3_20, PWM0_5, 2}, |
bogdanm | 15:4892fe388435 | 49 | {P3_21, PWM0_6, 2}, |
bogdanm | 15:4892fe388435 | 50 | {P3_24, PWM1_1, 2}, |
bogdanm | 15:4892fe388435 | 51 | {P3_25, PWM1_2, 2}, |
bogdanm | 15:4892fe388435 | 52 | {P3_26, PWM1_3, 2}, |
bogdanm | 15:4892fe388435 | 53 | {P3_27, PWM1_4, 2}, |
bogdanm | 15:4892fe388435 | 54 | {P3_28, PWM1_5, 2}, |
bogdanm | 15:4892fe388435 | 55 | {P3_29, PWM1_6, 2}, |
bogdanm | 15:4892fe388435 | 56 | {NC, NC, 0} |
bogdanm | 15:4892fe388435 | 57 | }; |
bogdanm | 15:4892fe388435 | 58 | |
bogdanm | 15:4892fe388435 | 59 | static const uint32_t PWM_mr_offset[7] = { |
bogdanm | 15:4892fe388435 | 60 | 0x18, 0x1C, 0x20, 0x24, 0x40, 0x44, 0x48 |
bogdanm | 15:4892fe388435 | 61 | }; |
bogdanm | 15:4892fe388435 | 62 | |
bogdanm | 15:4892fe388435 | 63 | #define TCR_PWM_EN 0x00000008 |
bogdanm | 15:4892fe388435 | 64 | static unsigned int pwm_clock_mhz; |
bogdanm | 15:4892fe388435 | 65 | |
bogdanm | 15:4892fe388435 | 66 | void pwmout_init(pwmout_t* obj, PinName pin) { |
bogdanm | 15:4892fe388435 | 67 | // determine the channel |
bogdanm | 15:4892fe388435 | 68 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
bogdanm | 15:4892fe388435 | 69 | if (pwm == (uint32_t)NC) |
bogdanm | 15:4892fe388435 | 70 | error("PwmOut pin mapping failed"); |
bogdanm | 15:4892fe388435 | 71 | |
bogdanm | 15:4892fe388435 | 72 | obj->channel = pwm; |
bogdanm | 15:4892fe388435 | 73 | obj->pwm = LPC_PWM0; |
bogdanm | 15:4892fe388435 | 74 | |
bogdanm | 15:4892fe388435 | 75 | if (obj->channel > 6) { // PWM1 is used if pwm > 6 |
bogdanm | 15:4892fe388435 | 76 | obj->channel -= 6; |
bogdanm | 15:4892fe388435 | 77 | obj->pwm = LPC_PWM1; |
bogdanm | 15:4892fe388435 | 78 | } |
bogdanm | 15:4892fe388435 | 79 | |
bogdanm | 15:4892fe388435 | 80 | obj->MR = (__IO uint32_t *)((uint32_t)obj->pwm + PWM_mr_offset[obj->channel]); |
bogdanm | 15:4892fe388435 | 81 | |
bogdanm | 15:4892fe388435 | 82 | // ensure the power is on |
bogdanm | 15:4892fe388435 | 83 | if (obj->pwm == LPC_PWM0) { |
bogdanm | 15:4892fe388435 | 84 | LPC_SC->PCONP |= 1 << 5; |
bogdanm | 15:4892fe388435 | 85 | } else { |
bogdanm | 15:4892fe388435 | 86 | LPC_SC->PCONP |= 1 << 6; |
bogdanm | 15:4892fe388435 | 87 | } |
bogdanm | 15:4892fe388435 | 88 | |
bogdanm | 15:4892fe388435 | 89 | obj->pwm->PR = 0; // no pre-scale |
bogdanm | 15:4892fe388435 | 90 | |
bogdanm | 15:4892fe388435 | 91 | // ensure single PWM mode |
bogdanm | 15:4892fe388435 | 92 | obj->pwm->MCR = 1 << 1; // reset TC on match 0 |
bogdanm | 15:4892fe388435 | 93 | |
bogdanm | 15:4892fe388435 | 94 | // enable the specific PWM output |
bogdanm | 15:4892fe388435 | 95 | obj->pwm->PCR |= 1 << (8 + obj->channel); |
bogdanm | 15:4892fe388435 | 96 | |
bogdanm | 15:4892fe388435 | 97 | pwm_clock_mhz = PeripheralClock / 1000000; |
bogdanm | 15:4892fe388435 | 98 | |
bogdanm | 15:4892fe388435 | 99 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
bogdanm | 15:4892fe388435 | 100 | pwmout_period_ms(obj, 20); |
bogdanm | 15:4892fe388435 | 101 | pwmout_write (obj, 0); |
bogdanm | 15:4892fe388435 | 102 | |
bogdanm | 15:4892fe388435 | 103 | // Wire pinout |
bogdanm | 15:4892fe388435 | 104 | pinmap_pinout(pin, PinMap_PWM); |
bogdanm | 15:4892fe388435 | 105 | } |
bogdanm | 15:4892fe388435 | 106 | |
bogdanm | 15:4892fe388435 | 107 | void pwmout_free(pwmout_t* obj) { |
bogdanm | 15:4892fe388435 | 108 | // [TODO] |
bogdanm | 15:4892fe388435 | 109 | } |
bogdanm | 15:4892fe388435 | 110 | |
bogdanm | 15:4892fe388435 | 111 | void pwmout_write(pwmout_t* obj, float value) { |
bogdanm | 15:4892fe388435 | 112 | if (value < 0.0f) { |
bogdanm | 15:4892fe388435 | 113 | value = 0.0; |
bogdanm | 15:4892fe388435 | 114 | } else if (value > 1.0f) { |
bogdanm | 15:4892fe388435 | 115 | value = 1.0; |
bogdanm | 15:4892fe388435 | 116 | } |
bogdanm | 15:4892fe388435 | 117 | |
bogdanm | 15:4892fe388435 | 118 | // set channel match to percentage |
bogdanm | 15:4892fe388435 | 119 | uint32_t v = (uint32_t)((float)(obj->pwm->MR0) * value); |
bogdanm | 15:4892fe388435 | 120 | |
bogdanm | 15:4892fe388435 | 121 | // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout |
bogdanm | 15:4892fe388435 | 122 | if (v == obj->pwm->MR0) { |
bogdanm | 15:4892fe388435 | 123 | v++; |
bogdanm | 15:4892fe388435 | 124 | } |
bogdanm | 15:4892fe388435 | 125 | |
bogdanm | 15:4892fe388435 | 126 | *obj->MR = v; |
bogdanm | 15:4892fe388435 | 127 | |
bogdanm | 15:4892fe388435 | 128 | // accept on next period start |
bogdanm | 15:4892fe388435 | 129 | obj->pwm->LER |= 1 << obj->channel; |
bogdanm | 15:4892fe388435 | 130 | } |
bogdanm | 15:4892fe388435 | 131 | |
bogdanm | 15:4892fe388435 | 132 | float pwmout_read(pwmout_t* obj) { |
bogdanm | 15:4892fe388435 | 133 | float v = (float)(*obj->MR) / (float)(obj->pwm->MR0); |
bogdanm | 15:4892fe388435 | 134 | return (v > 1.0f) ? (1.0f) : (v); |
bogdanm | 15:4892fe388435 | 135 | } |
bogdanm | 15:4892fe388435 | 136 | |
bogdanm | 15:4892fe388435 | 137 | void pwmout_period(pwmout_t* obj, float seconds) { |
bogdanm | 15:4892fe388435 | 138 | pwmout_period_us(obj, seconds * 1000000.0f); |
bogdanm | 15:4892fe388435 | 139 | } |
bogdanm | 15:4892fe388435 | 140 | |
bogdanm | 15:4892fe388435 | 141 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
bogdanm | 15:4892fe388435 | 142 | pwmout_period_us(obj, ms * 1000); |
bogdanm | 15:4892fe388435 | 143 | } |
bogdanm | 15:4892fe388435 | 144 | |
bogdanm | 15:4892fe388435 | 145 | // Set the PWM period, keeping the duty cycle the same. |
bogdanm | 15:4892fe388435 | 146 | void pwmout_period_us(pwmout_t* obj, int us) { |
bogdanm | 15:4892fe388435 | 147 | // calculate number of ticks |
bogdanm | 15:4892fe388435 | 148 | uint32_t ticks = pwm_clock_mhz * us; |
bogdanm | 15:4892fe388435 | 149 | |
bogdanm | 15:4892fe388435 | 150 | // set reset |
bogdanm | 15:4892fe388435 | 151 | obj->pwm->TCR = TCR_RESET; |
bogdanm | 15:4892fe388435 | 152 | |
bogdanm | 15:4892fe388435 | 153 | // set the global match register |
bogdanm | 15:4892fe388435 | 154 | obj->pwm->MR0 = ticks; |
bogdanm | 15:4892fe388435 | 155 | |
bogdanm | 15:4892fe388435 | 156 | // Scale the pulse width to preserve the duty ratio |
bogdanm | 15:4892fe388435 | 157 | if (obj->pwm->MR0 > 0) { |
bogdanm | 15:4892fe388435 | 158 | *obj->MR = (*obj->MR * ticks) / obj->pwm->MR0; |
bogdanm | 15:4892fe388435 | 159 | } |
bogdanm | 15:4892fe388435 | 160 | |
bogdanm | 15:4892fe388435 | 161 | // set the channel latch to update value at next period start |
bogdanm | 15:4892fe388435 | 162 | obj->pwm->LER |= 1 << 0; |
bogdanm | 15:4892fe388435 | 163 | |
bogdanm | 15:4892fe388435 | 164 | // enable counter and pwm, clear reset |
bogdanm | 15:4892fe388435 | 165 | obj->pwm->TCR = TCR_CNT_EN | TCR_PWM_EN; |
bogdanm | 15:4892fe388435 | 166 | } |
bogdanm | 15:4892fe388435 | 167 | |
bogdanm | 15:4892fe388435 | 168 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
bogdanm | 15:4892fe388435 | 169 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
bogdanm | 15:4892fe388435 | 170 | } |
bogdanm | 15:4892fe388435 | 171 | |
bogdanm | 15:4892fe388435 | 172 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
bogdanm | 15:4892fe388435 | 173 | pwmout_pulsewidth_us(obj, ms * 1000); |
bogdanm | 15:4892fe388435 | 174 | } |
bogdanm | 15:4892fe388435 | 175 | |
bogdanm | 15:4892fe388435 | 176 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
bogdanm | 15:4892fe388435 | 177 | // calculate number of ticks |
bogdanm | 15:4892fe388435 | 178 | uint32_t v = pwm_clock_mhz * us; |
bogdanm | 15:4892fe388435 | 179 | |
bogdanm | 15:4892fe388435 | 180 | // workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout |
bogdanm | 15:4892fe388435 | 181 | if (v == obj->pwm->MR0) { |
bogdanm | 15:4892fe388435 | 182 | v++; |
bogdanm | 15:4892fe388435 | 183 | } |
bogdanm | 15:4892fe388435 | 184 | |
bogdanm | 15:4892fe388435 | 185 | // set the match register value |
bogdanm | 15:4892fe388435 | 186 | *obj->MR = v; |
bogdanm | 15:4892fe388435 | 187 | |
bogdanm | 15:4892fe388435 | 188 | // set the channel latch to update value at next period start |
bogdanm | 15:4892fe388435 | 189 | obj->pwm->LER |= 1 << obj->channel; |
bogdanm | 15:4892fe388435 | 190 | } |