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) 2015-2016 Nuvoton
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
sahilmgandhi 18:6a4db94011d3 17 #include "pwmout_api.h"
sahilmgandhi 18:6a4db94011d3 18
sahilmgandhi 18:6a4db94011d3 19 #if DEVICE_PWMOUT
sahilmgandhi 18:6a4db94011d3 20
sahilmgandhi 18:6a4db94011d3 21 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 22 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 23 #include "PeripheralPins.h"
sahilmgandhi 18:6a4db94011d3 24 #include "nu_modutil.h"
sahilmgandhi 18:6a4db94011d3 25 #include "nu_miscutil.h"
sahilmgandhi 18:6a4db94011d3 26 #include "nu_bitutil.h"
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 struct nu_pwm_var {
sahilmgandhi 18:6a4db94011d3 29 uint32_t en_msk;
sahilmgandhi 18:6a4db94011d3 30 };
sahilmgandhi 18:6a4db94011d3 31
sahilmgandhi 18:6a4db94011d3 32 static struct nu_pwm_var pwm0_var = {
sahilmgandhi 18:6a4db94011d3 33 .en_msk = 0
sahilmgandhi 18:6a4db94011d3 34 };
sahilmgandhi 18:6a4db94011d3 35
sahilmgandhi 18:6a4db94011d3 36 static struct nu_pwm_var pwm1_var = {
sahilmgandhi 18:6a4db94011d3 37 .en_msk = 0
sahilmgandhi 18:6a4db94011d3 38 };
sahilmgandhi 18:6a4db94011d3 39
sahilmgandhi 18:6a4db94011d3 40 static uint32_t pwm_modinit_mask = 0;
sahilmgandhi 18:6a4db94011d3 41
sahilmgandhi 18:6a4db94011d3 42 static const struct nu_modinit_s pwm_modinit_tab[] = {
sahilmgandhi 18:6a4db94011d3 43 {PWM_0_0, PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0, PWM0_RST, PWM0P0_IRQn, &pwm0_var},
sahilmgandhi 18:6a4db94011d3 44 {PWM_0_1, PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0, PWM0_RST, PWM0P0_IRQn, &pwm0_var},
sahilmgandhi 18:6a4db94011d3 45 {PWM_0_2, PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0, PWM0_RST, PWM0P1_IRQn, &pwm0_var},
sahilmgandhi 18:6a4db94011d3 46 {PWM_0_3, PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0, PWM0_RST, PWM0P1_IRQn, &pwm0_var},
sahilmgandhi 18:6a4db94011d3 47 {PWM_0_4, PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0, PWM0_RST, PWM0P2_IRQn, &pwm0_var},
sahilmgandhi 18:6a4db94011d3 48 {PWM_0_5, PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0, PWM0_RST, PWM0P2_IRQn, &pwm0_var},
sahilmgandhi 18:6a4db94011d3 49
sahilmgandhi 18:6a4db94011d3 50 {PWM_1_0, PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0, PWM1_RST, PWM1P0_IRQn, &pwm1_var},
sahilmgandhi 18:6a4db94011d3 51 {PWM_1_1, PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0, PWM1_RST, PWM1P0_IRQn, &pwm1_var},
sahilmgandhi 18:6a4db94011d3 52 {PWM_1_2, PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0, PWM1_RST, PWM1P1_IRQn, &pwm1_var},
sahilmgandhi 18:6a4db94011d3 53 {PWM_1_3, PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0, PWM1_RST, PWM1P1_IRQn, &pwm1_var},
sahilmgandhi 18:6a4db94011d3 54 {PWM_1_4, PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0, PWM1_RST, PWM1P2_IRQn, &pwm1_var},
sahilmgandhi 18:6a4db94011d3 55 {PWM_1_5, PWM1_MODULE, CLK_CLKSEL2_PWM1SEL_PCLK1, 0, PWM1_RST, PWM1P2_IRQn, &pwm1_var},
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 {NC, 0, 0, 0, 0, (IRQn_Type) 0, NULL}
sahilmgandhi 18:6a4db94011d3 58 };
sahilmgandhi 18:6a4db94011d3 59
sahilmgandhi 18:6a4db94011d3 60 static void pwmout_config(pwmout_t* obj);
sahilmgandhi 18:6a4db94011d3 61
sahilmgandhi 18:6a4db94011d3 62 void pwmout_init(pwmout_t* obj, PinName pin)
sahilmgandhi 18:6a4db94011d3 63 {
sahilmgandhi 18:6a4db94011d3 64 obj->pwm = (PWMName) pinmap_peripheral(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 65 MBED_ASSERT((int) obj->pwm != NC);
sahilmgandhi 18:6a4db94011d3 66
sahilmgandhi 18:6a4db94011d3 67 const struct nu_modinit_s *modinit = get_modinit(obj->pwm, pwm_modinit_tab);
sahilmgandhi 18:6a4db94011d3 68 MBED_ASSERT(modinit != NULL);
sahilmgandhi 18:6a4db94011d3 69 MBED_ASSERT(modinit->modname == obj->pwm);
sahilmgandhi 18:6a4db94011d3 70
sahilmgandhi 18:6a4db94011d3 71 // NOTE: All channels (identified by PWMName) share a PWM module. This reset will also affect other channels of the same PWM module.
sahilmgandhi 18:6a4db94011d3 72 if (! ((struct nu_pwm_var *) modinit->var)->en_msk) {
sahilmgandhi 18:6a4db94011d3 73 // Reset this module if no channel enabled
sahilmgandhi 18:6a4db94011d3 74 SYS_ResetModule(modinit->rsetidx);
sahilmgandhi 18:6a4db94011d3 75 }
sahilmgandhi 18:6a4db94011d3 76
sahilmgandhi 18:6a4db94011d3 77 PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm);
sahilmgandhi 18:6a4db94011d3 78 uint32_t chn = NU_MODSUBINDEX(obj->pwm);
sahilmgandhi 18:6a4db94011d3 79
sahilmgandhi 18:6a4db94011d3 80 // NOTE: Channels 0/1/2/3/4/5 share a clock source.
sahilmgandhi 18:6a4db94011d3 81 if ((((struct nu_pwm_var *) modinit->var)->en_msk & 0x3F) == 0) {
sahilmgandhi 18:6a4db94011d3 82 // Select clock source of paired channels
sahilmgandhi 18:6a4db94011d3 83 CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
sahilmgandhi 18:6a4db94011d3 84 // Enable clock of paired channels
sahilmgandhi 18:6a4db94011d3 85 CLK_EnableModuleClock(modinit->clkidx);
sahilmgandhi 18:6a4db94011d3 86 }
sahilmgandhi 18:6a4db94011d3 87
sahilmgandhi 18:6a4db94011d3 88 // Wire pinout
sahilmgandhi 18:6a4db94011d3 89 pinmap_pinout(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 90
sahilmgandhi 18:6a4db94011d3 91 // Default: period = 10 ms, pulse width = 0 ms
sahilmgandhi 18:6a4db94011d3 92 obj->period_us = 1000 * 10;
sahilmgandhi 18:6a4db94011d3 93 obj->pulsewidth_us = 0;
sahilmgandhi 18:6a4db94011d3 94 pwmout_config(obj);
sahilmgandhi 18:6a4db94011d3 95
sahilmgandhi 18:6a4db94011d3 96 // Enable output of the specified PWM channel
sahilmgandhi 18:6a4db94011d3 97 PWM_EnableOutput(pwm_base, 1 << chn);
sahilmgandhi 18:6a4db94011d3 98 PWM_Start(pwm_base, 1 << chn);
sahilmgandhi 18:6a4db94011d3 99
sahilmgandhi 18:6a4db94011d3 100 ((struct nu_pwm_var *) modinit->var)->en_msk |= 1 << chn;
sahilmgandhi 18:6a4db94011d3 101
sahilmgandhi 18:6a4db94011d3 102 if (((struct nu_pwm_var *) modinit->var)->en_msk) {
sahilmgandhi 18:6a4db94011d3 103 // Mark this module to be inited.
sahilmgandhi 18:6a4db94011d3 104 int i = modinit - pwm_modinit_tab;
sahilmgandhi 18:6a4db94011d3 105 pwm_modinit_mask |= 1 << i;
sahilmgandhi 18:6a4db94011d3 106 }
sahilmgandhi 18:6a4db94011d3 107 }
sahilmgandhi 18:6a4db94011d3 108
sahilmgandhi 18:6a4db94011d3 109 void pwmout_free(pwmout_t* obj)
sahilmgandhi 18:6a4db94011d3 110 {
sahilmgandhi 18:6a4db94011d3 111 PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm);
sahilmgandhi 18:6a4db94011d3 112 uint32_t chn = NU_MODSUBINDEX(obj->pwm);
sahilmgandhi 18:6a4db94011d3 113 PWM_ForceStop(pwm_base, 1 << chn);
sahilmgandhi 18:6a4db94011d3 114
sahilmgandhi 18:6a4db94011d3 115 const struct nu_modinit_s *modinit = get_modinit(obj->pwm, pwm_modinit_tab);
sahilmgandhi 18:6a4db94011d3 116 MBED_ASSERT(modinit != NULL);
sahilmgandhi 18:6a4db94011d3 117 MBED_ASSERT(modinit->modname == obj->pwm);
sahilmgandhi 18:6a4db94011d3 118 ((struct nu_pwm_var *) modinit->var)->en_msk &= ~(1 << chn);
sahilmgandhi 18:6a4db94011d3 119
sahilmgandhi 18:6a4db94011d3 120
sahilmgandhi 18:6a4db94011d3 121 if ((((struct nu_pwm_var *) modinit->var)->en_msk & 0x3F) == 0) {
sahilmgandhi 18:6a4db94011d3 122 CLK_DisableModuleClock(modinit->clkidx);
sahilmgandhi 18:6a4db94011d3 123 }
sahilmgandhi 18:6a4db94011d3 124
sahilmgandhi 18:6a4db94011d3 125 if (((struct nu_pwm_var *) modinit->var)->en_msk == 0) {
sahilmgandhi 18:6a4db94011d3 126 // Mark this module to be deinited.
sahilmgandhi 18:6a4db94011d3 127 int i = modinit - pwm_modinit_tab;
sahilmgandhi 18:6a4db94011d3 128 pwm_modinit_mask &= ~(1 << i);
sahilmgandhi 18:6a4db94011d3 129 }
sahilmgandhi 18:6a4db94011d3 130 }
sahilmgandhi 18:6a4db94011d3 131
sahilmgandhi 18:6a4db94011d3 132 void pwmout_write(pwmout_t* obj, float value)
sahilmgandhi 18:6a4db94011d3 133 {
sahilmgandhi 18:6a4db94011d3 134 obj->pulsewidth_us = NU_CLAMP((uint32_t) (value * obj->period_us), 0, obj->period_us);
sahilmgandhi 18:6a4db94011d3 135 pwmout_config(obj);
sahilmgandhi 18:6a4db94011d3 136 }
sahilmgandhi 18:6a4db94011d3 137
sahilmgandhi 18:6a4db94011d3 138 float pwmout_read(pwmout_t* obj)
sahilmgandhi 18:6a4db94011d3 139 {
sahilmgandhi 18:6a4db94011d3 140 return NU_CLAMP((((float) obj->pulsewidth_us) / obj->period_us), 0.0f, 1.0f);
sahilmgandhi 18:6a4db94011d3 141 }
sahilmgandhi 18:6a4db94011d3 142
sahilmgandhi 18:6a4db94011d3 143 void pwmout_period(pwmout_t* obj, float seconds)
sahilmgandhi 18:6a4db94011d3 144 {
sahilmgandhi 18:6a4db94011d3 145 pwmout_period_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 146 }
sahilmgandhi 18:6a4db94011d3 147
sahilmgandhi 18:6a4db94011d3 148 void pwmout_period_ms(pwmout_t* obj, int ms)
sahilmgandhi 18:6a4db94011d3 149 {
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 {
sahilmgandhi 18:6a4db94011d3 156 uint32_t period_us_old = obj->period_us;
sahilmgandhi 18:6a4db94011d3 157 uint32_t pulsewidth_us_old = obj->pulsewidth_us;
sahilmgandhi 18:6a4db94011d3 158 obj->period_us = us;
sahilmgandhi 18:6a4db94011d3 159 obj->pulsewidth_us = NU_CLAMP(obj->period_us * pulsewidth_us_old / period_us_old, 0, obj->period_us);
sahilmgandhi 18:6a4db94011d3 160 pwmout_config(obj);
sahilmgandhi 18:6a4db94011d3 161 }
sahilmgandhi 18:6a4db94011d3 162
sahilmgandhi 18:6a4db94011d3 163 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
sahilmgandhi 18:6a4db94011d3 164 {
sahilmgandhi 18:6a4db94011d3 165 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 166 }
sahilmgandhi 18:6a4db94011d3 167
sahilmgandhi 18:6a4db94011d3 168 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
sahilmgandhi 18:6a4db94011d3 169 {
sahilmgandhi 18:6a4db94011d3 170 pwmout_pulsewidth_us(obj, ms * 1000);
sahilmgandhi 18:6a4db94011d3 171 }
sahilmgandhi 18:6a4db94011d3 172
sahilmgandhi 18:6a4db94011d3 173 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
sahilmgandhi 18:6a4db94011d3 174 {
sahilmgandhi 18:6a4db94011d3 175 obj->pulsewidth_us = NU_CLAMP(us, 0, obj->period_us);
sahilmgandhi 18:6a4db94011d3 176 pwmout_config(obj);
sahilmgandhi 18:6a4db94011d3 177 }
sahilmgandhi 18:6a4db94011d3 178
sahilmgandhi 18:6a4db94011d3 179 int pwmout_allow_powerdown(void)
sahilmgandhi 18:6a4db94011d3 180 {
sahilmgandhi 18:6a4db94011d3 181 uint32_t modinit_mask = pwm_modinit_mask;
sahilmgandhi 18:6a4db94011d3 182 while (modinit_mask) {
sahilmgandhi 18:6a4db94011d3 183 int pwm_idx = nu_ctz(modinit_mask);
sahilmgandhi 18:6a4db94011d3 184 const struct nu_modinit_s *modinit = pwm_modinit_tab + pwm_idx;
sahilmgandhi 18:6a4db94011d3 185 if (modinit->modname != NC) {
sahilmgandhi 18:6a4db94011d3 186 PWM_T *pwm_base = (PWM_T *) NU_MODBASE(modinit->modname);
sahilmgandhi 18:6a4db94011d3 187 uint32_t chn = NU_MODSUBINDEX(modinit->modname);
sahilmgandhi 18:6a4db94011d3 188 // Disallow entering power-down mode if PWM counter is enabled.
sahilmgandhi 18:6a4db94011d3 189 if ((pwm_base->CNTEN & (1 << chn)) && pwm_base->CMPDAT[chn]) {
sahilmgandhi 18:6a4db94011d3 190 return 0;
sahilmgandhi 18:6a4db94011d3 191 }
sahilmgandhi 18:6a4db94011d3 192 }
sahilmgandhi 18:6a4db94011d3 193 modinit_mask &= ~(1 << pwm_idx);
sahilmgandhi 18:6a4db94011d3 194 }
sahilmgandhi 18:6a4db94011d3 195
sahilmgandhi 18:6a4db94011d3 196 return 1;
sahilmgandhi 18:6a4db94011d3 197 }
sahilmgandhi 18:6a4db94011d3 198
sahilmgandhi 18:6a4db94011d3 199 static void pwmout_config(pwmout_t* obj)
sahilmgandhi 18:6a4db94011d3 200 {
sahilmgandhi 18:6a4db94011d3 201 PWM_T *pwm_base = (PWM_T *) NU_MODBASE(obj->pwm);
sahilmgandhi 18:6a4db94011d3 202 uint32_t chn = NU_MODSUBINDEX(obj->pwm);
sahilmgandhi 18:6a4db94011d3 203 // NOTE: Support period < 1s
sahilmgandhi 18:6a4db94011d3 204 //PWM_ConfigOutputChannel(pwm_base, chn, 1000 * 1000 / obj->period_us, obj->pulsewidth_us * 100 / obj->period_us);
sahilmgandhi 18:6a4db94011d3 205 PWM_ConfigOutputChannel2(pwm_base, chn, 1000 * 1000, obj->pulsewidth_us * 100 / obj->period_us, obj->period_us);
sahilmgandhi 18:6a4db94011d3 206 }
sahilmgandhi 18:6a4db94011d3 207
sahilmgandhi 18:6a4db94011d3 208 #endif