Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
martydd3
Date:
Sat Oct 25 15:54:19 2014 +0000
Revision:
18:915a235bc099
Parent:
17:c9ce210f6654
Martin's Testing branch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martydd3 6:6a04210a3f4f 1 #include "FanPump.h"
martydd3 6:6a04210a3f4f 2
pspatel321 17:c9ce210f6654 3 static FanPump* instance[6] = { NULL }; // Access pwm object by channel#
pspatel321 17:c9ce210f6654 4 const int PCLK = 24e6; // 24Mhz clock
martydd3 18:915a235bc099 5 uint32_t FanPump::period_us = 0.0;
martydd3 6:6a04210a3f4f 6
pspatel321 13:fbd9b3f5a07c 7 // Interrupt handler, must be called from static context, calls all the slew functions
pspatel321 13:fbd9b3f5a07c 8 void pwmIRQ() {
pspatel321 17:c9ce210f6654 9 int sum = 0;
pspatel321 17:c9ce210f6654 10 int items = 0;
pspatel321 13:fbd9b3f5a07c 11 if (LPC_PWM1->IR & 1) {
pspatel321 13:fbd9b3f5a07c 12 for (int i = 0; i < 6; i++) {
pspatel321 17:c9ce210f6654 13 if (instance[i] != NULL) {
pspatel321 17:c9ce210f6654 14 items++;
pspatel321 17:c9ce210f6654 15 sum += instance[i]->slew();
pspatel321 17:c9ce210f6654 16 }
pspatel321 13:fbd9b3f5a07c 17 }
pspatel321 13:fbd9b3f5a07c 18 }
pspatel321 17:c9ce210f6654 19 LPC_PWM1->IR = 0x73F; // Clear interrupts
pspatel321 17:c9ce210f6654 20 if (items == sum) LPC_PWM1->MCR = 0; // Detach all the interrupts, every pin is already where it needs to be
martydd3 6:6a04210a3f4f 21 }
martydd3 6:6a04210a3f4f 22
pspatel321 13:fbd9b3f5a07c 23 // Called on each timer expire for each pwm object
pspatel321 17:c9ce210f6654 24 int FanPump::slew() {
pspatel321 17:c9ce210f6654 25 uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks
pspatel321 17:c9ce210f6654 26 uint32_t setPointT = setPoint_us * (PCLK/1e6); // Convert us into ticks
pspatel321 17:c9ce210f6654 27 if (currPulseT == setPointT) return 1; // Nothing to slew here, already at its setpoint
pspatel321 17:c9ce210f6654 28
pspatel321 17:c9ce210f6654 29 uint32_t currPulse_us = currPulseT / (PCLK/1e6); // Convert to us
pspatel321 13:fbd9b3f5a07c 30 if (currPulseT < setPointT) {
pspatel321 17:c9ce210f6654 31 if (setPoint_us - currPulse_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly
pspatel321 13:fbd9b3f5a07c 32 else pwm.pulsewidth_us(currPulse_us + maxChange_us);
pspatel321 13:fbd9b3f5a07c 33 } else {
pspatel321 17:c9ce210f6654 34 if (currPulse_us - setPoint_us <= maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly
pspatel321 13:fbd9b3f5a07c 35 else pwm.pulsewidth_us(currPulse_us - maxChange_us);
martydd3 6:6a04210a3f4f 36 }
pspatel321 17:c9ce210f6654 37 return 0;
martydd3 6:6a04210a3f4f 38 }
martydd3 6:6a04210a3f4f 39
pspatel321 13:fbd9b3f5a07c 40 FanPump::FanPump(PinName pin, float period, float slew) : pwm(pin) {
pspatel321 17:c9ce210f6654 41
pspatel321 17:c9ce210f6654 42 // Match the pin# to the PWM object for the interrupt
pspatel321 17:c9ce210f6654 43 int chan=0;
pspatel321 17:c9ce210f6654 44 if (pin == p26 || pin == LED1) chan = 1;
pspatel321 17:c9ce210f6654 45 if (pin == p25 || pin == LED2) chan = 2;
pspatel321 17:c9ce210f6654 46 if (pin == p24 || pin == LED3) chan = 3;
pspatel321 17:c9ce210f6654 47 if (pin == p23 || pin == LED4) chan = 4;
pspatel321 17:c9ce210f6654 48 if (pin == p22) chan = 5;
pspatel321 17:c9ce210f6654 49 if (pin == p21) chan = 6;
pspatel321 17:c9ce210f6654 50 if (chan == 0) return; // Invalid pin
pspatel321 17:c9ce210f6654 51 instance[chan-1] = this;
pspatel321 17:c9ce210f6654 52
pspatel321 13:fbd9b3f5a07c 53 setPoint_us = 0;
pspatel321 13:fbd9b3f5a07c 54 period_us = period / 1.0e6;
pspatel321 13:fbd9b3f5a07c 55 pwm.period_us(period_us);
pspatel321 13:fbd9b3f5a07c 56 maxChange_us = (period / slew) * period_us;
pspatel321 17:c9ce210f6654 57
pspatel321 17:c9ce210f6654 58 LPC_PWM1->IR = 0x73F; // Clear interrupts
pspatel321 13:fbd9b3f5a07c 59 NVIC_SetVector(PWM1_IRQn, (uint32_t)&pwmIRQ);
pspatel321 13:fbd9b3f5a07c 60 NVIC_SetPriority(PWM1_IRQn, 0);
pspatel321 13:fbd9b3f5a07c 61 NVIC_EnableIRQ(PWM1_IRQn);
pspatel321 17:c9ce210f6654 62 LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires)
martydd3 6:6a04210a3f4f 63 }
pspatel321 13:fbd9b3f5a07c 64 void FanPump::write(float duty) {
pspatel321 13:fbd9b3f5a07c 65 if (duty < 0) duty = 0;
pspatel321 13:fbd9b3f5a07c 66 if (duty > 1) duty = 1;
pspatel321 13:fbd9b3f5a07c 67 setPoint_us = duty * period_us;
pspatel321 17:c9ce210f6654 68 LPC_PWM1->MCR = 1; // Enable interrupt on MR0 (when the pwm period expires)
pspatel321 17:c9ce210f6654 69 }
pspatel321 17:c9ce210f6654 70 void FanPump::directOff() {
pspatel321 17:c9ce210f6654 71 __disable_irq();
pspatel321 17:c9ce210f6654 72 pwm.pulsewidth_us(0);
pspatel321 17:c9ce210f6654 73 setPoint_us = 0;
pspatel321 17:c9ce210f6654 74 __enable_irq();
pspatel321 17:c9ce210f6654 75 }
pspatel321 17:c9ce210f6654 76 float FanPump::read() {
pspatel321 17:c9ce210f6654 77 return (float)(setPoint_us)/(float)(period_us);
pspatel321 17:c9ce210f6654 78 }
pspatel321 17:c9ce210f6654 79 float FanPump::readRaw() {
pspatel321 17:c9ce210f6654 80 uint32_t currPulseT = *((uint32_t*)(&LPC_PWM1->MR0)+chan); // Get the current pulsewidth ticks
pspatel321 17:c9ce210f6654 81 return ((float)(currPulseT) / (float)(PCLK/1e6)) / (float)(period_us);
martydd3 6:6a04210a3f4f 82 }