Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

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
sahilmgandhi 18:6a4db94011d3 19 #include "cmsis.h"
sahilmgandhi 18:6a4db94011d3 20 #include "pinmap.h"
sahilmgandhi 18:6a4db94011d3 21 #include "clk_freqs.h"
sahilmgandhi 18:6a4db94011d3 22 #include "PeripheralPins.h"
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 static float pwm_clock;
sahilmgandhi 18:6a4db94011d3 25
sahilmgandhi 18:6a4db94011d3 26 void pwmout_init(pwmout_t* obj, PinName pin) {
sahilmgandhi 18:6a4db94011d3 27 // determine the channel
sahilmgandhi 18:6a4db94011d3 28 PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 29 MBED_ASSERT(pwm != (PWMName)NC);
sahilmgandhi 18:6a4db94011d3 30
sahilmgandhi 18:6a4db94011d3 31 uint32_t clkdiv = 0;
sahilmgandhi 18:6a4db94011d3 32 float clkval;
sahilmgandhi 18:6a4db94011d3 33
sahilmgandhi 18:6a4db94011d3 34 #if defined(TARGET_KL43Z)
sahilmgandhi 18:6a4db94011d3 35 if (mcgirc_frequency()) {
sahilmgandhi 18:6a4db94011d3 36 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(3); // Clock source: MCGIRCLK
sahilmgandhi 18:6a4db94011d3 37 clkval = mcgirc_frequency() / 1000000.0f;
sahilmgandhi 18:6a4db94011d3 38 } else {
sahilmgandhi 18:6a4db94011d3 39 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: IRC48M
sahilmgandhi 18:6a4db94011d3 40 clkval = CPU_INT_IRC_CLK_HZ / 1000000.0f;
sahilmgandhi 18:6a4db94011d3 41 }
sahilmgandhi 18:6a4db94011d3 42 #else
sahilmgandhi 18:6a4db94011d3 43 if (mcgpllfll_frequency()) {
sahilmgandhi 18:6a4db94011d3 44 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // Clock source: MCGFLLCLK or MCGPLLCLK
sahilmgandhi 18:6a4db94011d3 45 clkval = mcgpllfll_frequency() / 1000000.0f;
sahilmgandhi 18:6a4db94011d3 46 } else {
sahilmgandhi 18:6a4db94011d3 47 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(2); // Clock source: ExtOsc
sahilmgandhi 18:6a4db94011d3 48 clkval = extosc_frequency() / 1000000.0f;
sahilmgandhi 18:6a4db94011d3 49 }
sahilmgandhi 18:6a4db94011d3 50 #endif
sahilmgandhi 18:6a4db94011d3 51 while (clkval > 1) {
sahilmgandhi 18:6a4db94011d3 52 clkdiv++;
sahilmgandhi 18:6a4db94011d3 53 clkval /= 2.0;
sahilmgandhi 18:6a4db94011d3 54 if (clkdiv == 7)
sahilmgandhi 18:6a4db94011d3 55 break;
sahilmgandhi 18:6a4db94011d3 56 }
sahilmgandhi 18:6a4db94011d3 57
sahilmgandhi 18:6a4db94011d3 58 pwm_clock = clkval;
sahilmgandhi 18:6a4db94011d3 59 unsigned int port = (unsigned int)pin >> PORT_SHIFT;
sahilmgandhi 18:6a4db94011d3 60 unsigned int tpm_n = (pwm >> TPM_SHIFT);
sahilmgandhi 18:6a4db94011d3 61 unsigned int ch_n = (pwm & 0xFF);
sahilmgandhi 18:6a4db94011d3 62
sahilmgandhi 18:6a4db94011d3 63 SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port);
sahilmgandhi 18:6a4db94011d3 64 SIM->SCGC6 |= 1 << (SIM_SCGC6_TPM0_SHIFT + tpm_n);
sahilmgandhi 18:6a4db94011d3 65
sahilmgandhi 18:6a4db94011d3 66 TPM_Type *tpm = (TPM_Type *)(TPM0_BASE + 0x1000 * tpm_n);
sahilmgandhi 18:6a4db94011d3 67 tpm->SC = TPM_SC_CMOD(1) | TPM_SC_PS(clkdiv); // (clock)MHz / clkdiv ~= (0.75)MHz
sahilmgandhi 18:6a4db94011d3 68 tpm->CONTROLS[ch_n].CnSC = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK); /* No Interrupts; High True pulses on Edge Aligned PWM */
sahilmgandhi 18:6a4db94011d3 69
sahilmgandhi 18:6a4db94011d3 70 obj->CnV = &tpm->CONTROLS[ch_n].CnV;
sahilmgandhi 18:6a4db94011d3 71 obj->MOD = &tpm->MOD;
sahilmgandhi 18:6a4db94011d3 72 obj->CNT = &tpm->CNT;
sahilmgandhi 18:6a4db94011d3 73
sahilmgandhi 18:6a4db94011d3 74 // default to 20ms: standard for servos, and fine for e.g. brightness control
sahilmgandhi 18:6a4db94011d3 75 pwmout_period_ms(obj, 20);
sahilmgandhi 18:6a4db94011d3 76 pwmout_write (obj, 0);
sahilmgandhi 18:6a4db94011d3 77
sahilmgandhi 18:6a4db94011d3 78 // Wire pinout
sahilmgandhi 18:6a4db94011d3 79 pinmap_pinout(pin, PinMap_PWM);
sahilmgandhi 18:6a4db94011d3 80 }
sahilmgandhi 18:6a4db94011d3 81
sahilmgandhi 18:6a4db94011d3 82 void pwmout_free(pwmout_t* obj) {}
sahilmgandhi 18:6a4db94011d3 83
sahilmgandhi 18:6a4db94011d3 84 void pwmout_write(pwmout_t* obj, float value) {
sahilmgandhi 18:6a4db94011d3 85 if (value < 0.0) {
sahilmgandhi 18:6a4db94011d3 86 value = 0.0;
sahilmgandhi 18:6a4db94011d3 87 } else if (value > 1.0) {
sahilmgandhi 18:6a4db94011d3 88 value = 1.0;
sahilmgandhi 18:6a4db94011d3 89 }
sahilmgandhi 18:6a4db94011d3 90
sahilmgandhi 18:6a4db94011d3 91 *obj->CnV = (uint32_t)((float)(*obj->MOD + 1) * value);
sahilmgandhi 18:6a4db94011d3 92 *obj->CNT = 0;
sahilmgandhi 18:6a4db94011d3 93 }
sahilmgandhi 18:6a4db94011d3 94
sahilmgandhi 18:6a4db94011d3 95 float pwmout_read(pwmout_t* obj) {
sahilmgandhi 18:6a4db94011d3 96 float v = (float)(*obj->CnV) / (float)(*obj->MOD + 1);
sahilmgandhi 18:6a4db94011d3 97 return (v > 1.0) ? (1.0) : (v);
sahilmgandhi 18:6a4db94011d3 98 }
sahilmgandhi 18:6a4db94011d3 99
sahilmgandhi 18:6a4db94011d3 100 void pwmout_period(pwmout_t* obj, float seconds) {
sahilmgandhi 18:6a4db94011d3 101 pwmout_period_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 102 }
sahilmgandhi 18:6a4db94011d3 103
sahilmgandhi 18:6a4db94011d3 104 void pwmout_period_ms(pwmout_t* obj, int ms) {
sahilmgandhi 18:6a4db94011d3 105 pwmout_period_us(obj, ms * 1000);
sahilmgandhi 18:6a4db94011d3 106 }
sahilmgandhi 18:6a4db94011d3 107
sahilmgandhi 18:6a4db94011d3 108 // Set the PWM period, keeping the duty cycle the same.
sahilmgandhi 18:6a4db94011d3 109 void pwmout_period_us(pwmout_t* obj, int us) {
sahilmgandhi 18:6a4db94011d3 110 float dc = pwmout_read(obj);
sahilmgandhi 18:6a4db94011d3 111 *obj->MOD = (uint32_t)(pwm_clock * (float)us) - 1;
sahilmgandhi 18:6a4db94011d3 112 pwmout_write(obj, dc);
sahilmgandhi 18:6a4db94011d3 113 }
sahilmgandhi 18:6a4db94011d3 114
sahilmgandhi 18:6a4db94011d3 115 void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
sahilmgandhi 18:6a4db94011d3 116 pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
sahilmgandhi 18:6a4db94011d3 117 }
sahilmgandhi 18:6a4db94011d3 118
sahilmgandhi 18:6a4db94011d3 119 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
sahilmgandhi 18:6a4db94011d3 120 pwmout_pulsewidth_us(obj, ms * 1000);
sahilmgandhi 18:6a4db94011d3 121 }
sahilmgandhi 18:6a4db94011d3 122
sahilmgandhi 18:6a4db94011d3 123 void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
sahilmgandhi 18:6a4db94011d3 124 *obj->CnV = (uint32_t)(pwm_clock * (float)us);
sahilmgandhi 18:6a4db94011d3 125 }