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 #include "mbed_error.h"
sahilmgandhi 18:6a4db94011d3 21
sahilmgandhi 18:6a4db94011d3 22 static LPC_SCT0_Type *SCTs[4] = {
sahilmgandhi 18:6a4db94011d3 23 (LPC_SCT0_Type*)LPC_SCT0,
sahilmgandhi 18:6a4db94011d3 24 (LPC_SCT0_Type*)LPC_SCT1,
sahilmgandhi 18:6a4db94011d3 25 (LPC_SCT0_Type*)LPC_SCT2,
sahilmgandhi 18:6a4db94011d3 26 (LPC_SCT0_Type*)LPC_SCT3,
sahilmgandhi 18:6a4db94011d3 27 };
sahilmgandhi 18:6a4db94011d3 28
sahilmgandhi 18:6a4db94011d3 29 // bit flags for used SCTs
sahilmgandhi 18:6a4db94011d3 30 static unsigned char sct_used = (1 << 3);
sahilmgandhi 18:6a4db94011d3 31 static int get_available_sct(void) {
sahilmgandhi 18:6a4db94011d3 32 int i;
sahilmgandhi 18:6a4db94011d3 33 for (i=0; i<4; i++) {
sahilmgandhi 18:6a4db94011d3 34 if ((sct_used & (1 << i)) == 0)
sahilmgandhi 18:6a4db94011d3 35 return i;
sahilmgandhi 18:6a4db94011d3 36 }
sahilmgandhi 18:6a4db94011d3 37 return -1;
sahilmgandhi 18:6a4db94011d3 38 }
sahilmgandhi 18:6a4db94011d3 39
sahilmgandhi 18:6a4db94011d3 40 void pwmout_init(pwmout_t* obj, PinName pin) {
sahilmgandhi 18:6a4db94011d3 41 MBED_ASSERT(pin != (uint32_t)NC);
sahilmgandhi 18:6a4db94011d3 42
sahilmgandhi 18:6a4db94011d3 43 int sct_n = get_available_sct();
sahilmgandhi 18:6a4db94011d3 44 if (sct_n == -1) {
sahilmgandhi 18:6a4db94011d3 45 error("No available SCT");
sahilmgandhi 18:6a4db94011d3 46 }
sahilmgandhi 18:6a4db94011d3 47
sahilmgandhi 18:6a4db94011d3 48 sct_used |= (1 << sct_n);
sahilmgandhi 18:6a4db94011d3 49 obj->pwm = SCTs[sct_n];
sahilmgandhi 18:6a4db94011d3 50 obj->pwm_ch = sct_n;
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 LPC_SCT0_Type* pwm = obj->pwm;
sahilmgandhi 18:6a4db94011d3 53
sahilmgandhi 18:6a4db94011d3 54 // Enable the SCT clock
sahilmgandhi 18:6a4db94011d3 55 LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << (obj->pwm_ch + 2));
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 // Clear peripheral reset the SCT:
sahilmgandhi 18:6a4db94011d3 58 LPC_SYSCON->PRESETCTRL1 |= (1 << (obj->pwm_ch + 2));
sahilmgandhi 18:6a4db94011d3 59 LPC_SYSCON->PRESETCTRL1 &= ~(1 << (obj->pwm_ch + 2));
sahilmgandhi 18:6a4db94011d3 60
sahilmgandhi 18:6a4db94011d3 61 switch(obj->pwm_ch) {
sahilmgandhi 18:6a4db94011d3 62 case 0:
sahilmgandhi 18:6a4db94011d3 63 // SCT0_OUT0
sahilmgandhi 18:6a4db94011d3 64 LPC_SWM->PINASSIGN[7] &= ~0x0000FF00;
sahilmgandhi 18:6a4db94011d3 65 LPC_SWM->PINASSIGN[7] |= (pin << 8);
sahilmgandhi 18:6a4db94011d3 66 break;
sahilmgandhi 18:6a4db94011d3 67 case 1:
sahilmgandhi 18:6a4db94011d3 68 // SCT1_OUT0
sahilmgandhi 18:6a4db94011d3 69 LPC_SWM->PINASSIGN[8] &= ~0x000000FF;
sahilmgandhi 18:6a4db94011d3 70 LPC_SWM->PINASSIGN[8] |= (pin);
sahilmgandhi 18:6a4db94011d3 71 break;
sahilmgandhi 18:6a4db94011d3 72 case 2:
sahilmgandhi 18:6a4db94011d3 73 // SCT2_OUT0
sahilmgandhi 18:6a4db94011d3 74 LPC_SWM->PINASSIGN[8] &= ~0xFF000000;
sahilmgandhi 18:6a4db94011d3 75 LPC_SWM->PINASSIGN[8] |= (pin << 24);
sahilmgandhi 18:6a4db94011d3 76 break;
sahilmgandhi 18:6a4db94011d3 77 case 3:
sahilmgandhi 18:6a4db94011d3 78 // SCT3_OUT0
sahilmgandhi 18:6a4db94011d3 79 LPC_SWM->PINASSIGN[9] &= ~0x00FF0000;
sahilmgandhi 18:6a4db94011d3 80 LPC_SWM->PINASSIGN[9] |= (pin << 16);
sahilmgandhi 18:6a4db94011d3 81 break;
sahilmgandhi 18:6a4db94011d3 82 default:
sahilmgandhi 18:6a4db94011d3 83 break;
sahilmgandhi 18:6a4db94011d3 84 }
sahilmgandhi 18:6a4db94011d3 85
sahilmgandhi 18:6a4db94011d3 86 // Unified 32-bit counter, autolimit
sahilmgandhi 18:6a4db94011d3 87 pwm->CONFIG |= ((0x3 << 17) | 0x01);
sahilmgandhi 18:6a4db94011d3 88
sahilmgandhi 18:6a4db94011d3 89 // halt and clear the counter
sahilmgandhi 18:6a4db94011d3 90 pwm->CTRL |= (1 << 2) | (1 << 3);
sahilmgandhi 18:6a4db94011d3 91
sahilmgandhi 18:6a4db94011d3 92 pwm->OUT0_SET = (1 << 0); // event 0
sahilmgandhi 18:6a4db94011d3 93 pwm->OUT0_CLR = (1 << 1); // event 1
sahilmgandhi 18:6a4db94011d3 94 // Resolve conflicts on output 0 to set output
sahilmgandhi 18:6a4db94011d3 95 // This allows duty cycle = 1.0 to work, where the MATCH registers for set and clear are equal
sahilmgandhi 18:6a4db94011d3 96 pwm->RES = 0x01;
sahilmgandhi 18:6a4db94011d3 97
sahilmgandhi 18:6a4db94011d3 98 pwm->EV0_CTRL = (1 << 12);
sahilmgandhi 18:6a4db94011d3 99 pwm->EV0_STATE = 0xFFFFFFFF;
sahilmgandhi 18:6a4db94011d3 100 pwm->EV1_CTRL = (1 << 12) | (1 << 0);
sahilmgandhi 18:6a4db94011d3 101 pwm->EV1_STATE = 0xFFFFFFFF;
sahilmgandhi 18:6a4db94011d3 102
sahilmgandhi 18:6a4db94011d3 103 // default to 20ms: standard for servos, and fine for e.g. brightness control
sahilmgandhi 18:6a4db94011d3 104 pwmout_period_ms(obj, 20);
sahilmgandhi 18:6a4db94011d3 105 pwmout_write (obj, 0);
sahilmgandhi 18:6a4db94011d3 106 }
sahilmgandhi 18:6a4db94011d3 107
sahilmgandhi 18:6a4db94011d3 108 void pwmout_free(pwmout_t* obj) {
sahilmgandhi 18:6a4db94011d3 109 // Disable the SCT clock
sahilmgandhi 18:6a4db94011d3 110 LPC_SYSCON->SYSAHBCLKCTRL1 &= ~(1 << (obj->pwm_ch + 2));
sahilmgandhi 18:6a4db94011d3 111 sct_used &= ~(1 << obj->pwm_ch);
sahilmgandhi 18:6a4db94011d3 112 }
sahilmgandhi 18:6a4db94011d3 113
sahilmgandhi 18:6a4db94011d3 114 void pwmout_write(pwmout_t* obj, float value) {
sahilmgandhi 18:6a4db94011d3 115 LPC_SCT0_Type* pwm = obj->pwm;
sahilmgandhi 18:6a4db94011d3 116 if (value < 0.0f) {
sahilmgandhi 18:6a4db94011d3 117 value = 0.0;
sahilmgandhi 18:6a4db94011d3 118 } else if (value > 1.0f) {
sahilmgandhi 18:6a4db94011d3 119 value = 1.0;
sahilmgandhi 18:6a4db94011d3 120 }
sahilmgandhi 18:6a4db94011d3 121 uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
sahilmgandhi 18:6a4db94011d3 122 if (t_on > 0) {
sahilmgandhi 18:6a4db94011d3 123 pwm->MATCHREL1 = t_on - 1;
sahilmgandhi 18:6a4db94011d3 124
sahilmgandhi 18:6a4db94011d3 125 // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
sahilmgandhi 18:6a4db94011d3 126 if (pwm->CTRL & (1 << 2)) {
sahilmgandhi 18:6a4db94011d3 127 pwm->MATCH1 = pwm->MATCHREL1;
sahilmgandhi 18:6a4db94011d3 128 pwm->CTRL &= ~(1 << 2);
sahilmgandhi 18:6a4db94011d3 129 }
sahilmgandhi 18:6a4db94011d3 130 } else {
sahilmgandhi 18:6a4db94011d3 131 // Halt the timer and force the output low
sahilmgandhi 18:6a4db94011d3 132 pwm->CTRL |= (1 << 2) | (1 << 3);
sahilmgandhi 18:6a4db94011d3 133 pwm->OUTPUT = 0x00000000;
sahilmgandhi 18:6a4db94011d3 134 }
sahilmgandhi 18:6a4db94011d3 135 }
sahilmgandhi 18:6a4db94011d3 136
sahilmgandhi 18:6a4db94011d3 137 float pwmout_read(pwmout_t* obj) {
sahilmgandhi 18:6a4db94011d3 138 LPC_SCT0_Type* pwm = obj->pwm;
sahilmgandhi 18:6a4db94011d3 139 uint32_t t_off = pwm->MATCHREL0 + 1;
sahilmgandhi 18:6a4db94011d3 140 uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
sahilmgandhi 18:6a4db94011d3 141 float v = (float)t_on/(float)t_off;
sahilmgandhi 18:6a4db94011d3 142 return (v > 1.0f) ? (1.0f) : (v);
sahilmgandhi 18:6a4db94011d3 143 }
sahilmgandhi 18:6a4db94011d3 144
sahilmgandhi 18:6a4db94011d3 145 void pwmout_period(pwmout_t* obj, float seconds) {
sahilmgandhi 18:6a4db94011d3 146 pwmout_period_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 147 }
sahilmgandhi 18:6a4db94011d3 148
sahilmgandhi 18:6a4db94011d3 149 void pwmout_period_ms(pwmout_t* obj, int ms) {
sahilmgandhi 18:6a4db94011d3 150 pwmout_period_us(obj, ms * 1000);
sahilmgandhi 18:6a4db94011d3 151 }
sahilmgandhi 18:6a4db94011d3 152
sahilmgandhi 18:6a4db94011d3 153 // Set the PWM period, keeping the duty cycle the same.
sahilmgandhi 18:6a4db94011d3 154 void pwmout_period_us(pwmout_t* obj, int us) {
sahilmgandhi 18:6a4db94011d3 155 LPC_SCT0_Type* pwm = obj->pwm;
sahilmgandhi 18:6a4db94011d3 156 uint32_t t_off = pwm->MATCHREL0 + 1;
sahilmgandhi 18:6a4db94011d3 157 uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
sahilmgandhi 18:6a4db94011d3 158 float v = (float)t_on/(float)t_off;
sahilmgandhi 18:6a4db94011d3 159 uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
sahilmgandhi 18:6a4db94011d3 160 uint32_t pulsewidth_ticks = period_ticks * v;
sahilmgandhi 18:6a4db94011d3 161 pwm->MATCHREL0 = period_ticks - 1;
sahilmgandhi 18:6a4db94011d3 162 if (pulsewidth_ticks > 0) {
sahilmgandhi 18:6a4db94011d3 163 pwm->MATCHREL1 = pulsewidth_ticks - 1;
sahilmgandhi 18:6a4db94011d3 164
sahilmgandhi 18:6a4db94011d3 165 // Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
sahilmgandhi 18:6a4db94011d3 166 if (pwm->CTRL & (1 << 2)) {
sahilmgandhi 18:6a4db94011d3 167 pwm->MATCH0 = pwm->MATCHREL0;
sahilmgandhi 18:6a4db94011d3 168 pwm->MATCH1 = pwm->MATCHREL1;
sahilmgandhi 18:6a4db94011d3 169 pwm->CTRL &= ~(1 << 2);
sahilmgandhi 18:6a4db94011d3 170 }
sahilmgandhi 18:6a4db94011d3 171 } else {
sahilmgandhi 18:6a4db94011d3 172 // Halt the timer and force the output low
sahilmgandhi 18:6a4db94011d3 173 pwm->CTRL |= (1 << 2) | (1 << 3);
sahilmgandhi 18:6a4db94011d3 174 pwm->OUTPUT = 0x00000000;
sahilmgandhi 18:6a4db94011d3 175
sahilmgandhi 18:6a4db94011d3 176 // Ensure the new period will take immediate effect when the timer is un-halted
sahilmgandhi 18:6a4db94011d3 177 pwm->MATCH0 = pwm->MATCHREL0;
sahilmgandhi 18:6a4db94011d3 178 }
sahilmgandhi 18:6a4db94011d3 179 }
sahilmgandhi 18:6a4db94011d3 180
sahilmgandhi 18:6a4db94011d3 181 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
sahilmgandhi 18:6a4db94011d3 182 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 183 }
sahilmgandhi 18:6a4db94011d3 184
sahilmgandhi 18:6a4db94011d3 185 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
sahilmgandhi 18:6a4db94011d3 186 pwmout_pulsewidth_us(obj, ms * 1000);
sahilmgandhi 18:6a4db94011d3 187 }
sahilmgandhi 18:6a4db94011d3 188
sahilmgandhi 18:6a4db94011d3 189 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
sahilmgandhi 18:6a4db94011d3 190 LPC_SCT0_Type* pwm = obj->pwm;
sahilmgandhi 18:6a4db94011d3 191 if (us > 0) {
sahilmgandhi 18:6a4db94011d3 192 pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
sahilmgandhi 18:6a4db94011d3 193
sahilmgandhi 18:6a4db94011d3 194 // Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
sahilmgandhi 18:6a4db94011d3 195 if (pwm->CTRL & (1 << 2)) {
sahilmgandhi 18:6a4db94011d3 196 pwm->MATCH1 = pwm->MATCHREL1;
sahilmgandhi 18:6a4db94011d3 197 pwm->CTRL &= ~(1 << 2);
sahilmgandhi 18:6a4db94011d3 198 }
sahilmgandhi 18:6a4db94011d3 199 } else {
sahilmgandhi 18:6a4db94011d3 200 // Halt the timer and force the output low
sahilmgandhi 18:6a4db94011d3 201 pwm->CTRL |= (1 << 2) | (1 << 3);
sahilmgandhi 18:6a4db94011d3 202 pwm->OUTPUT = 0x00000000;
sahilmgandhi 18:6a4db94011d3 203 }
sahilmgandhi 18:6a4db94011d3 204 }
sahilmgandhi 18:6a4db94011d3 205