Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

FanPump/FanPump.cpp

Committer:
pspatel321
Date:
2014-10-24
Revision:
13:fbd9b3f5a07c
Parent:
12:e0adb697fcdb
Child:
17:c9ce210f6654

File content as of revision 13:fbd9b3f5a07c:

#include "FanPump.h"

static FanPump* instance[6] = { NULL };

// Interrupt handler, must be called from static context, calls all the slew functions
void pwmIRQ() {
    if (LPC_PWM1->IR & 1) {
        for (int i = 0; i < 6; i++) {
            if (instance[i] != NULL) instance[i]->slew();   
        }
    }
    LPC_PWM1->IR = 0x73F;   // Clear interrupts
}

// Called on each timer expire for each pwm object
void FanPump::slew() {
    uint32_t currPulseT = *(&LPC_PWM1->MR0+pin);
    uint32_t currPulse_us = currPulseT / 24;
    uint32_t setPointT = setPoint_us * 24;      // Convert us into ticks
    if (currPulseT == setPointT) return;        // Nothing to slew here, already at its setpoint

    if (currPulseT < setPointT) {
        if (setPoint_us - currPulse_us < maxChange_us) pwm.pulsewidth_us(setPoint_us); // Close to the setpoint, write it directly
        else pwm.pulsewidth_us(currPulse_us + maxChange_us);       
    } else {
        if (currPulse_us - setPoint_us < maxChange_us) pwm.pulsewidth_us(setPoint_us);
        else pwm.pulsewidth_us(currPulse_us - maxChange_us);
    }
}

FanPump::FanPump(PinName pin, float period, float slew) : pwm(pin) {
    setPoint_us = 0;
    period_us = period / 1.0e6;
    pwm.period_us(period_us);
    maxChange_us = (period / slew) * period_us;
    
    // Match the pin# to the PWM object for the interrupt
    if (pin == p26 || pin == LED1) pin = 1;
    if (pin == p25 || pin == LED2) pin = 2;
    if (pin == p24 || pin == LED3) pin = 3;
    if (pin == p23 || pin == LED4) pin = 4;
    if (pin == p22)                pin = 5;
    if (pin == p21)                pin = 6;
    instance[pin-1] = this;
    
    LPC_PWM1->IR = 0x73F;   // Clear interrupts
    NVIC_SetVector(PWM1_IRQn, (uint32_t)&pwmIRQ);
    NVIC_SetPriority(PWM1_IRQn, 0);
    NVIC_EnableIRQ(PWM1_IRQn);
    LPC_PWM1->MR |= 1;  // Enable interrupt on MR0 (when the pwm period expires)
}
void FanPump::write(float duty) {
    if (duty < 0) duty = 0;
    if (duty > 1) duty = 1;
    setPoint_us = duty * period_us;
}