Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

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?

UserRevisionLine numberNew 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, 2}, {P1_13, PWM_1, 2}, /* MR0 */
sahilmgandhi 18:6a4db94011d3 32 {P0_9 , PWM_2, 2}, {P1_14, PWM_2, 2}, /* MR1 */
sahilmgandhi 18:6a4db94011d3 33 {P0_10, PWM_3, 3}, {P1_15, PWM_3, 2}, /* MR2 */
sahilmgandhi 18:6a4db94011d3 34
sahilmgandhi 18:6a4db94011d3 35 /* CT16B1 */
sahilmgandhi 18:6a4db94011d3 36 {P0_21, PWM_4, 1}, /* MR0 */
sahilmgandhi 18:6a4db94011d3 37 {P0_22, PWM_5, 2}, {P1_23, PWM_5, 1}, /* MR1 */
sahilmgandhi 18:6a4db94011d3 38
sahilmgandhi 18:6a4db94011d3 39 /* CT32B0 */
sahilmgandhi 18:6a4db94011d3 40 {P0_18, PWM_6, 2}, {P1_24, PWM_6, 1}, /* MR0 */
sahilmgandhi 18:6a4db94011d3 41 {P0_19, PWM_7, 2}, {P1_25, PWM_7, 1}, /* MR1 */
sahilmgandhi 18:6a4db94011d3 42 {P0_1 , PWM_8, 2}, {P1_26, PWM_8, 1}, /* MR2 */
sahilmgandhi 18:6a4db94011d3 43
sahilmgandhi 18:6a4db94011d3 44 /* CT32B1 */
sahilmgandhi 18:6a4db94011d3 45 {P0_13, PWM_9 , 3}, //{P1_0, PWM_9 , 1}, /* MR0 */
sahilmgandhi 18:6a4db94011d3 46 {P0_14, PWM_10, 3}, //{P1_1, PWM_10, 1}, /* MR1 */
sahilmgandhi 18:6a4db94011d3 47 {P0_15, PWM_11, 3}, //{P1_2, PWM_11, 1}, /* MR2 */
sahilmgandhi 18:6a4db94011d3 48
sahilmgandhi 18:6a4db94011d3 49 {NC, NC, 0}
sahilmgandhi 18:6a4db94011d3 50 };
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 typedef struct {
sahilmgandhi 18:6a4db94011d3 53 uint8_t timer;
sahilmgandhi 18:6a4db94011d3 54 uint8_t mr;
sahilmgandhi 18:6a4db94011d3 55 } timer_mr;
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 static timer_mr pwm_timer_map[11] = {
sahilmgandhi 18:6a4db94011d3 58 {0, 0}, {0, 1}, {0, 2},
sahilmgandhi 18:6a4db94011d3 59 {1, 0}, {1, 1},
sahilmgandhi 18:6a4db94011d3 60 {2, 0}, {2, 1}, {2, 2},
sahilmgandhi 18:6a4db94011d3 61 {3, 0}, {3, 1}, {3, 2},
sahilmgandhi 18:6a4db94011d3 62 };
sahilmgandhi 18:6a4db94011d3 63
sahilmgandhi 18:6a4db94011d3 64 static LPC_CTxxBx_Type *Timers[4] = {
sahilmgandhi 18:6a4db94011d3 65 LPC_CT16B0, LPC_CT16B1,
sahilmgandhi 18:6a4db94011d3 66 LPC_CT32B0, LPC_CT32B1
sahilmgandhi 18:6a4db94011d3 67 };
sahilmgandhi 18:6a4db94011d3 68
sahilmgandhi 18:6a4db94011d3 69 void pwmout_init(pwmout_t* obj, PinName pin) {
sahilmgandhi 18:6a4db94011d3 70 // determine the channel
sahilmgandhi 18:6a4db94011d3 71 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 72 MBED_ASSERT(pwm != (PWMName)NC);
sahilmgandhi 18:6a4db94011d3 73
sahilmgandhi 18:6a4db94011d3 74 obj->pwm = pwm;
sahilmgandhi 18:6a4db94011d3 75
sahilmgandhi 18:6a4db94011d3 76 // Timer registers
sahilmgandhi 18:6a4db94011d3 77 timer_mr tid = pwm_timer_map[pwm];
sahilmgandhi 18:6a4db94011d3 78 LPC_CTxxBx_Type *timer = Timers[tid.timer];
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 // Disable timer
sahilmgandhi 18:6a4db94011d3 81 timer->TCR = 0;
sahilmgandhi 18:6a4db94011d3 82
sahilmgandhi 18:6a4db94011d3 83 // Power the correspondent timer
sahilmgandhi 18:6a4db94011d3 84 LPC_SYSCON->SYSAHBCLKCTRL |= 1 << (tid.timer + 7);
sahilmgandhi 18:6a4db94011d3 85
sahilmgandhi 18:6a4db94011d3 86 /* Enable PWM function */
sahilmgandhi 18:6a4db94011d3 87 timer->PWMC = (1 << 3)|(1 << 2)|(1 << 1)|(1 << 0);
sahilmgandhi 18:6a4db94011d3 88
sahilmgandhi 18:6a4db94011d3 89 /* Reset Functionality on MR3 controlling the PWM period */
sahilmgandhi 18:6a4db94011d3 90 timer->MCR = 1 << 10;
sahilmgandhi 18:6a4db94011d3 91
sahilmgandhi 18:6a4db94011d3 92 if (timer == LPC_CT16B0 || timer == LPC_CT16B1) {
sahilmgandhi 18:6a4db94011d3 93 /* Set 16-bit timer prescaler to avoid timer expire for default 20ms
sahilmgandhi 18:6a4db94011d3 94 This can be also modified by user application, but the prescaler value
sahilmgandhi 18:6a4db94011d3 95 might be trade-off to timer accuracy */
sahilmgandhi 18:6a4db94011d3 96 timer->PR = 30;
sahilmgandhi 18:6a4db94011d3 97 }
sahilmgandhi 18:6a4db94011d3 98
sahilmgandhi 18:6a4db94011d3 99 // default to 20ms: standard for servos, and fine for e.g. brightness control
sahilmgandhi 18:6a4db94011d3 100 pwmout_period_ms(obj, 20);
sahilmgandhi 18:6a4db94011d3 101 pwmout_write (obj, 0);
sahilmgandhi 18:6a4db94011d3 102
sahilmgandhi 18:6a4db94011d3 103 // Wire pinout
sahilmgandhi 18:6a4db94011d3 104 pinmap_pinout(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 105 }
sahilmgandhi 18:6a4db94011d3 106
sahilmgandhi 18:6a4db94011d3 107 void pwmout_free(pwmout_t* obj) {
sahilmgandhi 18:6a4db94011d3 108 // [TODO]
sahilmgandhi 18:6a4db94011d3 109 }
sahilmgandhi 18:6a4db94011d3 110
sahilmgandhi 18:6a4db94011d3 111 void pwmout_write(pwmout_t* obj, float value) {
sahilmgandhi 18:6a4db94011d3 112 if (value < 0.0f) {
sahilmgandhi 18:6a4db94011d3 113 value = 0.0;
sahilmgandhi 18:6a4db94011d3 114 } else if (value > 1.0f) {
sahilmgandhi 18:6a4db94011d3 115 value = 1.0;
sahilmgandhi 18:6a4db94011d3 116 }
sahilmgandhi 18:6a4db94011d3 117
sahilmgandhi 18:6a4db94011d3 118 timer_mr tid = pwm_timer_map[obj->pwm];
sahilmgandhi 18:6a4db94011d3 119 LPC_CTxxBx_Type *timer = Timers[tid.timer];
sahilmgandhi 18:6a4db94011d3 120 uint32_t t_off = timer->MR3 - (uint32_t)((float)(timer->MR3) * value);
sahilmgandhi 18:6a4db94011d3 121
sahilmgandhi 18:6a4db94011d3 122 // to avoid spike pulse when duty is 0%
sahilmgandhi 18:6a4db94011d3 123 if (value == 0) {
sahilmgandhi 18:6a4db94011d3 124 t_off++;
sahilmgandhi 18:6a4db94011d3 125 }
sahilmgandhi 18:6a4db94011d3 126
sahilmgandhi 18:6a4db94011d3 127 timer->TCR = TCR_RESET;
sahilmgandhi 18:6a4db94011d3 128 timer->MR[tid.mr] = t_off;
sahilmgandhi 18:6a4db94011d3 129 timer->TCR = TCR_CNT_EN;
sahilmgandhi 18:6a4db94011d3 130 }
sahilmgandhi 18:6a4db94011d3 131
sahilmgandhi 18:6a4db94011d3 132 float pwmout_read(pwmout_t* obj) {
sahilmgandhi 18:6a4db94011d3 133 timer_mr tid = pwm_timer_map[obj->pwm];
sahilmgandhi 18:6a4db94011d3 134 LPC_CTxxBx_Type *timer = Timers[tid.timer];
sahilmgandhi 18:6a4db94011d3 135
sahilmgandhi 18:6a4db94011d3 136 float v = (float)(timer->MR3 - timer->MR[tid.mr]) / (float)(timer->MR3);
sahilmgandhi 18:6a4db94011d3 137 if (timer->MR[tid.mr] > timer->MR3) {
sahilmgandhi 18:6a4db94011d3 138 v = 0.0f;
sahilmgandhi 18:6a4db94011d3 139 }
sahilmgandhi 18:6a4db94011d3 140 return (v > 1.0f) ? (1.0f) : (v);
sahilmgandhi 18:6a4db94011d3 141 }
sahilmgandhi 18:6a4db94011d3 142
sahilmgandhi 18:6a4db94011d3 143 void pwmout_period(pwmout_t* obj, float seconds) {
sahilmgandhi 18:6a4db94011d3 144 pwmout_period_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 145 }
sahilmgandhi 18:6a4db94011d3 146
sahilmgandhi 18:6a4db94011d3 147 void pwmout_period_ms(pwmout_t* obj, int ms) {
sahilmgandhi 18:6a4db94011d3 148 pwmout_period_us(obj, ms * 1000);
sahilmgandhi 18:6a4db94011d3 149 }
sahilmgandhi 18:6a4db94011d3 150
sahilmgandhi 18:6a4db94011d3 151 // Set the PWM period, keeping the duty cycle the same.
sahilmgandhi 18:6a4db94011d3 152 void pwmout_period_us(pwmout_t* obj, int us) {
sahilmgandhi 18:6a4db94011d3 153 int i = 0;
sahilmgandhi 18:6a4db94011d3 154
sahilmgandhi 18:6a4db94011d3 155 timer_mr tid = pwm_timer_map[obj->pwm];
sahilmgandhi 18:6a4db94011d3 156 LPC_CTxxBx_Type *timer = Timers[tid.timer];
sahilmgandhi 18:6a4db94011d3 157 uint32_t old_period_ticks = timer->MR3;
sahilmgandhi 18:6a4db94011d3 158 uint32_t period_ticks = (SystemCoreClock / 1000000 * us) / (timer->PR + 1);
sahilmgandhi 18:6a4db94011d3 159
sahilmgandhi 18:6a4db94011d3 160 timer->TCR = TCR_RESET;
sahilmgandhi 18:6a4db94011d3 161 timer->MR3 = period_ticks;
sahilmgandhi 18:6a4db94011d3 162
sahilmgandhi 18:6a4db94011d3 163 // Scale the pulse width to preserve the duty ratio
sahilmgandhi 18:6a4db94011d3 164 if (old_period_ticks > 0) {
sahilmgandhi 18:6a4db94011d3 165 for (i=0; i<3; i++) {
sahilmgandhi 18:6a4db94011d3 166 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 167 timer->MR[i] = t_off;
sahilmgandhi 18:6a4db94011d3 168 }
sahilmgandhi 18:6a4db94011d3 169 }
sahilmgandhi 18:6a4db94011d3 170 timer->TCR = TCR_CNT_EN;
sahilmgandhi 18:6a4db94011d3 171 }
sahilmgandhi 18:6a4db94011d3 172
sahilmgandhi 18:6a4db94011d3 173 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
sahilmgandhi 18:6a4db94011d3 174 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 175 }
sahilmgandhi 18:6a4db94011d3 176
sahilmgandhi 18:6a4db94011d3 177 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
sahilmgandhi 18:6a4db94011d3 178 pwmout_pulsewidth_us(obj, ms * 1000);
sahilmgandhi 18:6a4db94011d3 179 }
sahilmgandhi 18:6a4db94011d3 180
sahilmgandhi 18:6a4db94011d3 181 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
sahilmgandhi 18:6a4db94011d3 182 timer_mr tid = pwm_timer_map[obj->pwm];
sahilmgandhi 18:6a4db94011d3 183 LPC_CTxxBx_Type *timer = Timers[tid.timer];
sahilmgandhi 18:6a4db94011d3 184 uint32_t t_on = (uint32_t)((((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) / (timer->PR + 1));
sahilmgandhi 18:6a4db94011d3 185
sahilmgandhi 18:6a4db94011d3 186 timer->TCR = TCR_RESET;
sahilmgandhi 18:6a4db94011d3 187 if (t_on > timer->MR3) {
sahilmgandhi 18:6a4db94011d3 188 pwmout_period_us(obj, us);
sahilmgandhi 18:6a4db94011d3 189 }
sahilmgandhi 18:6a4db94011d3 190 uint32_t t_off = timer->MR3 - t_on;
sahilmgandhi 18:6a4db94011d3 191 timer->MR[tid.mr] = t_off;
sahilmgandhi 18:6a4db94011d3 192 timer->TCR = TCR_CNT_EN;
sahilmgandhi 18:6a4db94011d3 193 }