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