Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
pspatel321
Date:
Sat Oct 25 03:28:55 2014 +0000
Revision:
17:c9ce210f6654
Parent:
13:fbd9b3f5a07c
Child:
18:915a235bc099
Parth's edits for 10/24/14, items remaining: DC-DC, XBee, reading AMS/IMD error, main code

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