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