mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
targets/TARGET_NXP/TARGET_LPC11UXX/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 | #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | #define TCR_CNT_EN 0x00000001 |
elessair | 0:f269e3021894 | 23 | #define TCR_RESET 0x00000002 |
elessair | 0:f269e3021894 | 24 | |
elessair | 0:f269e3021894 | 25 | typedef struct { |
elessair | 0:f269e3021894 | 26 | uint8_t timer; |
elessair | 0:f269e3021894 | 27 | uint8_t mr; |
elessair | 0:f269e3021894 | 28 | } timer_mr; |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | static timer_mr pwm_timer_map[11] = { |
elessair | 0:f269e3021894 | 31 | {0, 0}, {0, 1}, {0, 2}, |
elessair | 0:f269e3021894 | 32 | {1, 0}, {1, 1}, |
elessair | 0:f269e3021894 | 33 | {2, 0}, {2, 1}, {2, 2}, |
elessair | 0:f269e3021894 | 34 | {3, 0}, {3, 1}, {3, 2}, |
elessair | 0:f269e3021894 | 35 | }; |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | static LPC_CTxxBx_Type *Timers[4] = { |
elessair | 0:f269e3021894 | 38 | LPC_CT16B0, LPC_CT16B1, |
elessair | 0:f269e3021894 | 39 | LPC_CT32B0, LPC_CT32B1 |
elessair | 0:f269e3021894 | 40 | }; |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | void pwmout_init(pwmout_t* obj, PinName pin) { |
elessair | 0:f269e3021894 | 43 | // determine the channel |
elessair | 0:f269e3021894 | 44 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
elessair | 0:f269e3021894 | 45 | MBED_ASSERT(pwm != (PWMName)NC); |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | obj->pwm = pwm; |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | // Timer registers |
elessair | 0:f269e3021894 | 50 | timer_mr tid = pwm_timer_map[pwm]; |
elessair | 0:f269e3021894 | 51 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | // Disable timer |
elessair | 0:f269e3021894 | 54 | timer->TCR = 0; |
elessair | 0:f269e3021894 | 55 | |
elessair | 0:f269e3021894 | 56 | // Power the correspondent timer |
elessair | 0:f269e3021894 | 57 | LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7); |
elessair | 0:f269e3021894 | 58 | |
elessair | 0:f269e3021894 | 59 | /* Enable PWM function */ |
elessair | 0:f269e3021894 | 60 | timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0); |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | /* Reset Functionality on MR3 controlling the PWM period */ |
elessair | 0:f269e3021894 | 63 | timer->MCR = 1 << 10; |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
elessair | 0:f269e3021894 | 66 | pwmout_period_ms(obj, 20); |
elessair | 0:f269e3021894 | 67 | pwmout_write (obj, 0); |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | // Wire pinout |
elessair | 0:f269e3021894 | 70 | pinmap_pinout(pin, PinMap_PWM); |
elessair | 0:f269e3021894 | 71 | } |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | void pwmout_free(pwmout_t* obj) { |
elessair | 0:f269e3021894 | 74 | // [TODO] |
elessair | 0:f269e3021894 | 75 | } |
elessair | 0:f269e3021894 | 76 | |
elessair | 0:f269e3021894 | 77 | void pwmout_write(pwmout_t* obj, float value) { |
elessair | 0:f269e3021894 | 78 | if (value < 0.0f) { |
elessair | 0:f269e3021894 | 79 | value = 0.0; |
elessair | 0:f269e3021894 | 80 | } else if (value > 1.0f) { |
elessair | 0:f269e3021894 | 81 | value = 1.0; |
elessair | 0:f269e3021894 | 82 | } |
elessair | 0:f269e3021894 | 83 | |
elessair | 0:f269e3021894 | 84 | timer_mr tid = pwm_timer_map[obj->pwm]; |
elessair | 0:f269e3021894 | 85 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
elessair | 0:f269e3021894 | 86 | uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value); |
elessair | 0:f269e3021894 | 87 | |
elessair | 0:f269e3021894 | 88 | timer->MR[tid.mr] = t_off; |
elessair | 0:f269e3021894 | 89 | } |
elessair | 0:f269e3021894 | 90 | |
elessair | 0:f269e3021894 | 91 | float pwmout_read(pwmout_t* obj) { |
elessair | 0:f269e3021894 | 92 | timer_mr tid = pwm_timer_map[obj->pwm]; |
elessair | 0:f269e3021894 | 93 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
elessair | 0:f269e3021894 | 94 | |
elessair | 0:f269e3021894 | 95 | float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3); |
elessair | 0:f269e3021894 | 96 | return (v > 1.0f) ? (1.0f) : (v); |
elessair | 0:f269e3021894 | 97 | } |
elessair | 0:f269e3021894 | 98 | |
elessair | 0:f269e3021894 | 99 | void pwmout_period(pwmout_t* obj, float seconds) { |
elessair | 0:f269e3021894 | 100 | pwmout_period_us(obj, seconds * 1000000.0f); |
elessair | 0:f269e3021894 | 101 | } |
elessair | 0:f269e3021894 | 102 | |
elessair | 0:f269e3021894 | 103 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
elessair | 0:f269e3021894 | 104 | pwmout_period_us(obj, ms * 1000); |
elessair | 0:f269e3021894 | 105 | } |
elessair | 0:f269e3021894 | 106 | |
elessair | 0:f269e3021894 | 107 | // Set the PWM period, keeping the duty cycle the same. |
elessair | 0:f269e3021894 | 108 | void pwmout_period_us(pwmout_t* obj, int us) { |
elessair | 0:f269e3021894 | 109 | int i = 0; |
elessair | 0:f269e3021894 | 110 | uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000); |
elessair | 0:f269e3021894 | 111 | |
elessair | 0:f269e3021894 | 112 | timer_mr tid = pwm_timer_map[obj->pwm]; |
elessair | 0:f269e3021894 | 113 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
elessair | 0:f269e3021894 | 114 | uint32_t old_period_ticks = timer->MR3; |
elessair | 0:f269e3021894 | 115 | |
elessair | 0:f269e3021894 | 116 | // for 16bit timer, set prescaler to avoid overflow |
elessair | 0:f269e3021894 | 117 | if (timer == LPC_CT16B0 || timer == LPC_CT16B1) { |
elessair | 0:f269e3021894 | 118 | uint16_t high_period_ticks = period_ticks >> 16; |
elessair | 0:f269e3021894 | 119 | timer->PR = high_period_ticks; |
elessair | 0:f269e3021894 | 120 | period_ticks /= (high_period_ticks + 1); |
elessair | 0:f269e3021894 | 121 | } |
elessair | 0:f269e3021894 | 122 | |
elessair | 0:f269e3021894 | 123 | timer->TCR = TCR_RESET; |
elessair | 0:f269e3021894 | 124 | timer->MR3 = period_ticks; |
elessair | 0:f269e3021894 | 125 | |
elessair | 0:f269e3021894 | 126 | // Scale the pulse width to preserve the duty ratio |
elessair | 0:f269e3021894 | 127 | if (old_period_ticks > 0) { |
elessair | 0:f269e3021894 | 128 | for (i=0; i<3; i++) { |
elessair | 0:f269e3021894 | 129 | uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks); |
elessair | 0:f269e3021894 | 130 | timer->MR[i] = t_off; |
elessair | 0:f269e3021894 | 131 | } |
elessair | 0:f269e3021894 | 132 | } |
elessair | 0:f269e3021894 | 133 | timer->TCR = TCR_CNT_EN; |
elessair | 0:f269e3021894 | 134 | } |
elessair | 0:f269e3021894 | 135 | |
elessair | 0:f269e3021894 | 136 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
elessair | 0:f269e3021894 | 137 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
elessair | 0:f269e3021894 | 138 | } |
elessair | 0:f269e3021894 | 139 | |
elessair | 0:f269e3021894 | 140 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
elessair | 0:f269e3021894 | 141 | pwmout_pulsewidth_us(obj, ms * 1000); |
elessair | 0:f269e3021894 | 142 | } |
elessair | 0:f269e3021894 | 143 | |
elessair | 0:f269e3021894 | 144 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
elessair | 0:f269e3021894 | 145 | timer_mr tid = pwm_timer_map[obj->pwm]; |
elessair | 0:f269e3021894 | 146 | LPC_CTxxBx_Type *timer = Timers[tid.timer]; |
elessair | 0:f269e3021894 | 147 | uint32_t t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1)); |
elessair | 0:f269e3021894 | 148 | |
elessair | 0:f269e3021894 | 149 | timer->TCR = TCR_RESET; |
elessair | 0:f269e3021894 | 150 | if (t_on > timer->MR3) { |
elessair | 0:f269e3021894 | 151 | pwmout_period_us(obj, us); |
elessair | 0:f269e3021894 | 152 | t_on = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000 / (timer->PR + 1)); |
elessair | 0:f269e3021894 | 153 | } |
elessair | 0:f269e3021894 | 154 | uint32_t t_off = timer->MR3 - t_on; |
elessair | 0:f269e3021894 | 155 | timer->MR[tid.mr] = t_off; |
elessair | 0:f269e3021894 | 156 | timer->TCR = TCR_CNT_EN; |
elessair | 0:f269e3021894 | 157 | } |