Mouse code for the MacroRat
mbed-dev/targets/TARGET_NXP/TARGET_LPC11XX_11CXX/pwmout_api.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
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 | /* To have a PWM where we can change both the period and the duty cycle, |
sahilmgandhi | 18:6a4db94011d3 | 25 | * we need an entire timer. With the following conventions: |
sahilmgandhi | 18:6a4db94011d3 | 26 | * * MR3 is used for the PWM period |
sahilmgandhi | 18:6a4db94011d3 | 27 | * * MR0, MR1, MR2 are used for the duty cycle |
sahilmgandhi | 18:6a4db94011d3 | 28 | */ |
sahilmgandhi | 18:6a4db94011d3 | 29 | static const PinMap PinMap_PWM[] = { |
sahilmgandhi | 18:6a4db94011d3 | 30 | /* CT16B0 */ |
sahilmgandhi | 18:6a4db94011d3 | 31 | {P0_8 , PWM_1, 0x02}, /* MR0 */ |
sahilmgandhi | 18:6a4db94011d3 | 32 | {P0_9 , PWM_2, 0x02}, /* MR1 */ |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | /* CT16B1 */ |
sahilmgandhi | 18:6a4db94011d3 | 35 | {P1_9 , PWM_3, 0x01}, /* MR0 */ |
sahilmgandhi | 18:6a4db94011d3 | 36 | {P1_10, PWM_4, 0x02}, /* MR1 */ |
sahilmgandhi | 18:6a4db94011d3 | 37 | |
sahilmgandhi | 18:6a4db94011d3 | 38 | /* CT32B0 */ |
sahilmgandhi | 18:6a4db94011d3 | 39 | {P0_1 , PWM_5, 0x02}, /* MR2 */ |
sahilmgandhi | 18:6a4db94011d3 | 40 | |
sahilmgandhi | 18:6a4db94011d3 | 41 | {NC , NC ,0x00} |
sahilmgandhi | 18:6a4db94011d3 | 42 | }; |
sahilmgandhi | 18:6a4db94011d3 | 43 | |
sahilmgandhi | 18:6a4db94011d3 | 44 | typedef struct { |
sahilmgandhi | 18:6a4db94011d3 | 45 | uint8_t timer; |
sahilmgandhi | 18:6a4db94011d3 | 46 | uint8_t mr; |
sahilmgandhi | 18:6a4db94011d3 | 47 | } timer_mr; |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | static timer_mr pwm_timer_map[5] = { |
sahilmgandhi | 18:6a4db94011d3 | 50 | {0, 0}, /* CT16B0, MR0 */ |
sahilmgandhi | 18:6a4db94011d3 | 51 | {0, 1}, /* CT16B0, MR1 */ |
sahilmgandhi | 18:6a4db94011d3 | 52 | |
sahilmgandhi | 18:6a4db94011d3 | 53 | {1, 0}, /* CT16B1, MR0 */ |
sahilmgandhi | 18:6a4db94011d3 | 54 | {1, 1}, /* CT16B1, MR1 */ |
sahilmgandhi | 18:6a4db94011d3 | 55 | |
sahilmgandhi | 18:6a4db94011d3 | 56 | {2, 2}, /* CT32B0, MR2 */ |
sahilmgandhi | 18:6a4db94011d3 | 57 | }; |
sahilmgandhi | 18:6a4db94011d3 | 58 | |
sahilmgandhi | 18:6a4db94011d3 | 59 | static LPC_TMR_TypeDef *Timers[3] = { |
sahilmgandhi | 18:6a4db94011d3 | 60 | LPC_TMR16B0, LPC_TMR16B1, |
sahilmgandhi | 18:6a4db94011d3 | 61 | LPC_TMR32B0 |
sahilmgandhi | 18:6a4db94011d3 | 62 | }; |
sahilmgandhi | 18:6a4db94011d3 | 63 | |
sahilmgandhi | 18:6a4db94011d3 | 64 | void pwmout_init(pwmout_t* obj, PinName pin) { |
sahilmgandhi | 18:6a4db94011d3 | 65 | // determine the channel |
sahilmgandhi | 18:6a4db94011d3 | 66 | PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM); |
sahilmgandhi | 18:6a4db94011d3 | 67 | MBED_ASSERT(pwm != (PWMName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 68 | |
sahilmgandhi | 18:6a4db94011d3 | 69 | obj->pwm = pwm; |
sahilmgandhi | 18:6a4db94011d3 | 70 | |
sahilmgandhi | 18:6a4db94011d3 | 71 | // Timer registers |
sahilmgandhi | 18:6a4db94011d3 | 72 | timer_mr tid = pwm_timer_map[pwm]; |
sahilmgandhi | 18:6a4db94011d3 | 73 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
sahilmgandhi | 18:6a4db94011d3 | 74 | |
sahilmgandhi | 18:6a4db94011d3 | 75 | // Disable timer |
sahilmgandhi | 18:6a4db94011d3 | 76 | timer->TCR = 0; |
sahilmgandhi | 18:6a4db94011d3 | 77 | |
sahilmgandhi | 18:6a4db94011d3 | 78 | // Power the correspondent timer |
sahilmgandhi | 18:6a4db94011d3 | 79 | LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7); |
sahilmgandhi | 18:6a4db94011d3 | 80 | |
sahilmgandhi | 18:6a4db94011d3 | 81 | /* Enable PWM function */ |
sahilmgandhi | 18:6a4db94011d3 | 82 | timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0); |
sahilmgandhi | 18:6a4db94011d3 | 83 | |
sahilmgandhi | 18:6a4db94011d3 | 84 | /* Reset Functionality on MR3 controlling the PWM period */ |
sahilmgandhi | 18:6a4db94011d3 | 85 | timer->MCR = 1 << 10; |
sahilmgandhi | 18:6a4db94011d3 | 86 | |
sahilmgandhi | 18:6a4db94011d3 | 87 | if (timer == LPC_TMR16B0 || timer == LPC_TMR16B1) { |
sahilmgandhi | 18:6a4db94011d3 | 88 | /* Set 16-bit timer prescaler to avoid timer expire for default 20ms */ |
sahilmgandhi | 18:6a4db94011d3 | 89 | /* This can be also modified by user application, but the prescaler value */ |
sahilmgandhi | 18:6a4db94011d3 | 90 | /* might be trade-off to timer accuracy */ |
sahilmgandhi | 18:6a4db94011d3 | 91 | timer->PR = 30; |
sahilmgandhi | 18:6a4db94011d3 | 92 | } |
sahilmgandhi | 18:6a4db94011d3 | 93 | |
sahilmgandhi | 18:6a4db94011d3 | 94 | // default to 20ms: standard for servos, and fine for e.g. brightness control |
sahilmgandhi | 18:6a4db94011d3 | 95 | pwmout_period_ms(obj, 20); |
sahilmgandhi | 18:6a4db94011d3 | 96 | pwmout_write (obj, 0); |
sahilmgandhi | 18:6a4db94011d3 | 97 | |
sahilmgandhi | 18:6a4db94011d3 | 98 | // Wire pinout |
sahilmgandhi | 18:6a4db94011d3 | 99 | pinmap_pinout(pin, PinMap_PWM); |
sahilmgandhi | 18:6a4db94011d3 | 100 | } |
sahilmgandhi | 18:6a4db94011d3 | 101 | |
sahilmgandhi | 18:6a4db94011d3 | 102 | void pwmout_free(pwmout_t* obj) { |
sahilmgandhi | 18:6a4db94011d3 | 103 | // [TODO] |
sahilmgandhi | 18:6a4db94011d3 | 104 | } |
sahilmgandhi | 18:6a4db94011d3 | 105 | |
sahilmgandhi | 18:6a4db94011d3 | 106 | void pwmout_write(pwmout_t* obj, float value) { |
sahilmgandhi | 18:6a4db94011d3 | 107 | if (value < 0.0f) { |
sahilmgandhi | 18:6a4db94011d3 | 108 | value = 0.0; |
sahilmgandhi | 18:6a4db94011d3 | 109 | } else if (value > 1.0f) { |
sahilmgandhi | 18:6a4db94011d3 | 110 | value = 1.0; |
sahilmgandhi | 18:6a4db94011d3 | 111 | } |
sahilmgandhi | 18:6a4db94011d3 | 112 | |
sahilmgandhi | 18:6a4db94011d3 | 113 | timer_mr tid = pwm_timer_map[obj->pwm]; |
sahilmgandhi | 18:6a4db94011d3 | 114 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
sahilmgandhi | 18:6a4db94011d3 | 115 | uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value); |
sahilmgandhi | 18:6a4db94011d3 | 116 | // to avoid spike pulse when duty is 0% |
sahilmgandhi | 18:6a4db94011d3 | 117 | if (value == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 118 | t_off++; |
sahilmgandhi | 18:6a4db94011d3 | 119 | } |
sahilmgandhi | 18:6a4db94011d3 | 120 | |
sahilmgandhi | 18:6a4db94011d3 | 121 | timer->TCR = TCR_RESET; |
sahilmgandhi | 18:6a4db94011d3 | 122 | timer->MR[tid.mr] = t_off; |
sahilmgandhi | 18:6a4db94011d3 | 123 | timer->TCR = TCR_CNT_EN; |
sahilmgandhi | 18:6a4db94011d3 | 124 | } |
sahilmgandhi | 18:6a4db94011d3 | 125 | |
sahilmgandhi | 18:6a4db94011d3 | 126 | float pwmout_read(pwmout_t* obj) { |
sahilmgandhi | 18:6a4db94011d3 | 127 | timer_mr tid = pwm_timer_map[obj->pwm]; |
sahilmgandhi | 18:6a4db94011d3 | 128 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
sahilmgandhi | 18:6a4db94011d3 | 129 | |
sahilmgandhi | 18:6a4db94011d3 | 130 | float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3); |
sahilmgandhi | 18:6a4db94011d3 | 131 | if (timer->MR[tid.mr] > timer->MR3) { |
sahilmgandhi | 18:6a4db94011d3 | 132 | v = 0.0f; |
sahilmgandhi | 18:6a4db94011d3 | 133 | } |
sahilmgandhi | 18:6a4db94011d3 | 134 | return (v > 1.0f) ? (1.0f) : (v); |
sahilmgandhi | 18:6a4db94011d3 | 135 | } |
sahilmgandhi | 18:6a4db94011d3 | 136 | |
sahilmgandhi | 18:6a4db94011d3 | 137 | void pwmout_period(pwmout_t* obj, float seconds) { |
sahilmgandhi | 18:6a4db94011d3 | 138 | pwmout_period_us(obj, seconds * 1000000.0f); |
sahilmgandhi | 18:6a4db94011d3 | 139 | } |
sahilmgandhi | 18:6a4db94011d3 | 140 | |
sahilmgandhi | 18:6a4db94011d3 | 141 | void pwmout_period_ms(pwmout_t* obj, int ms) { |
sahilmgandhi | 18:6a4db94011d3 | 142 | pwmout_period_us(obj, ms * 1000); |
sahilmgandhi | 18:6a4db94011d3 | 143 | } |
sahilmgandhi | 18:6a4db94011d3 | 144 | |
sahilmgandhi | 18:6a4db94011d3 | 145 | // Set the PWM period, keeping the duty cycle the same. |
sahilmgandhi | 18:6a4db94011d3 | 146 | void pwmout_period_us(pwmout_t* obj, int us) { |
sahilmgandhi | 18:6a4db94011d3 | 147 | int i = 0; |
sahilmgandhi | 18:6a4db94011d3 | 148 | uint32_t period_ticks; |
sahilmgandhi | 18:6a4db94011d3 | 149 | |
sahilmgandhi | 18:6a4db94011d3 | 150 | timer_mr tid = pwm_timer_map[obj->pwm]; |
sahilmgandhi | 18:6a4db94011d3 | 151 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
sahilmgandhi | 18:6a4db94011d3 | 152 | uint32_t old_period_ticks = timer->MR3; |
sahilmgandhi | 18:6a4db94011d3 | 153 | period_ticks = (SystemCoreClock / 1000000 * us) / (timer->PR + 1); |
sahilmgandhi | 18:6a4db94011d3 | 154 | |
sahilmgandhi | 18:6a4db94011d3 | 155 | timer->TCR = TCR_RESET; |
sahilmgandhi | 18:6a4db94011d3 | 156 | timer->MR3 = period_ticks; |
sahilmgandhi | 18:6a4db94011d3 | 157 | |
sahilmgandhi | 18:6a4db94011d3 | 158 | // Scale the pulse width to preserve the duty ratio |
sahilmgandhi | 18:6a4db94011d3 | 159 | if (old_period_ticks > 0) { |
sahilmgandhi | 18:6a4db94011d3 | 160 | for (i=0; i<3; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 161 | uint32_t t_off = period_ticks - (uint32_t)(((uint64_t)timer->MR[i] * (uint64_t)period_ticks) / (uint64_t)old_period_ticks); |
sahilmgandhi | 18:6a4db94011d3 | 162 | timer->MR[i] = t_off; |
sahilmgandhi | 18:6a4db94011d3 | 163 | } |
sahilmgandhi | 18:6a4db94011d3 | 164 | } |
sahilmgandhi | 18:6a4db94011d3 | 165 | timer->TCR = TCR_CNT_EN; |
sahilmgandhi | 18:6a4db94011d3 | 166 | } |
sahilmgandhi | 18:6a4db94011d3 | 167 | |
sahilmgandhi | 18:6a4db94011d3 | 168 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) { |
sahilmgandhi | 18:6a4db94011d3 | 169 | pwmout_pulsewidth_us(obj, seconds * 1000000.0f); |
sahilmgandhi | 18:6a4db94011d3 | 170 | } |
sahilmgandhi | 18:6a4db94011d3 | 171 | |
sahilmgandhi | 18:6a4db94011d3 | 172 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) { |
sahilmgandhi | 18:6a4db94011d3 | 173 | pwmout_pulsewidth_us(obj, ms * 1000); |
sahilmgandhi | 18:6a4db94011d3 | 174 | } |
sahilmgandhi | 18:6a4db94011d3 | 175 | |
sahilmgandhi | 18:6a4db94011d3 | 176 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) { |
sahilmgandhi | 18:6a4db94011d3 | 177 | timer_mr tid = pwm_timer_map[obj->pwm]; |
sahilmgandhi | 18:6a4db94011d3 | 178 | LPC_TMR_TypeDef *timer = Timers[tid.timer]; |
sahilmgandhi | 18:6a4db94011d3 | 179 | uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1)); |
sahilmgandhi | 18:6a4db94011d3 | 180 | |
sahilmgandhi | 18:6a4db94011d3 | 181 | timer->TCR = TCR_RESET; |
sahilmgandhi | 18:6a4db94011d3 | 182 | if (t_on > timer->MR3) { |
sahilmgandhi | 18:6a4db94011d3 | 183 | pwmout_period_us(obj, us); |
sahilmgandhi | 18:6a4db94011d3 | 184 | } |
sahilmgandhi | 18:6a4db94011d3 | 185 | uint32_t t_off = timer->MR3 - t_on; |
sahilmgandhi | 18:6a4db94011d3 | 186 | timer->MR[tid.mr] = t_off; |
sahilmgandhi | 18:6a4db94011d3 | 187 | timer->TCR = TCR_CNT_EN; |
sahilmgandhi | 18:6a4db94011d3 | 188 | } |