⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ui.c

📁 Luminary Micro BLDC motor control software
💻 C
📖 第 1 页 / 共 5 页
字号:
UIDecayMode(void)
{
    //
    // Update the decay mode flag in the flags variable.
    //
    HWREGBITH(&(g_sParameters.usFlags), FLAG_DECAY_BIT) = g_ucDecayMode;
}

//*****************************************************************************
//
//! Starts the motor drive.
//!
//! This function is called by the serial user interface when the run command
//! is received.  The motor drive will be started as a result; this is a no
//! operation if the motor drive is already running.
//!
//! \return None.
//
//*****************************************************************************
void
UIRun(void)
{
    //
    // Start the motor drive.
    //
    MainRun();
}

//*****************************************************************************
//
//! Stops the motor drive.
//!
//! This function is called by the serial user interface when the stop command
//! is received.  The motor drive will be stopped as a result; this is a no
//! operation if the motor drive is already stopped.
//!
//! \return None.
//
//*****************************************************************************
void
UIStop(void)
{
    //
    // Stop the motor drive.
    //
    MainStop();
}

//*****************************************************************************
//
//! Emergency stops the motor drive.
//!
//! This function is called by the serial user interface when the emergency
//! stop command is received.
//!
//! \return None.
//
//*****************************************************************************
void
UIEmergencyStop(void)
{
    //
    // Emergency stop the motor drive.
    //
    MainEmergencyStop();

    //
    // Indicate that the emergency stop fault has occurred.
    //
    MainSetFault(FAULT_EMERGENCY_STOP);
}

//*****************************************************************************
//
//! Loads the motor drive parameter block from flash.
//!
//! This function is called by the serial user interface when the load
//! parameter block function is called.  If the motor drive is running, the
//! parameter block is not loaded (since that may result in detrimental
//! changes, such as changing the motor drive from sine to trapezoid).
//! If the motor drive is not running and a valid parameter block exists in
//! flash, the contents of the parameter block are loaded from flash.
//!
//! \return None.
//
//*****************************************************************************
void
UIParamLoad(void)
{
    unsigned char *pucBuffer;
    unsigned long ulIdx;

    //
    // Return without doing anything if the motor drive is running.
    //
    if(MainIsRunning())
    {
        return;
    }

    //
    // Get a pointer to the latest parameter block in flash.
    //
    pucBuffer = FlashPBGet();

    //
    // See if a parameter block was found in flash.
    //
    if(pucBuffer)
    {
        //
        // Loop through the words of the parameter block to copy its contents
        // from flash to SRAM.
        //
        for(ulIdx = 0; ulIdx < (sizeof(tDriveParameters) / 4); ulIdx++)
        {
            ((unsigned long *)&g_sParameters)[ulIdx] =
                ((unsigned long *)pucBuffer)[ulIdx];
        }

        //
        // See if this is a version zero parameter block.
        //
        if(g_sParameters.ucVersion == 0)
        {
            //
            // Fill in default values for the new parameters added to the
            // version one parameter block.
            //
            g_sParameters.sAccelCurrent = 5000;

            //
            // Set the parameter block version to one.
            //
            g_sParameters.ucVersion = 1;
        }
    }

    //
    // Set the local variables (used by the serial interface) based on the
    // values in the parameter block values.
    //
    g_ucEncoder = HWREGBITH(&(g_sParameters.usFlags), FLAG_ENCODER_BIT);
    g_ucModulation = HWREGBITH(&(g_sParameters.usFlags), FLAG_DRIVE_BIT);
    g_ucDirection = HWREGBITH(&(g_sParameters.usFlags), FLAG_DIR_BIT);
    g_ucFrequency = g_sParameters.usFlags & FLAG_PWM_FREQUENCY_MASK;
    g_ucUpdateRate = g_sParameters.ucUpdateRate;
    g_lFAdjI = g_sParameters.lFAdjI;
    g_ucDynamicBrake = HWREGBITH(&(g_sParameters.usFlags), FLAG_BRAKE_BIT);
    g_ucSensor = HWREGBITH(&(g_sParameters.usFlags), FLAG_SENSOR_BIT);
    g_ucSensorType = HWREGBITH(&(g_sParameters.usFlags), FLAG_SENSOR_TYPE_BIT);
    g_ucDecayMode =  HWREGBITH(&(g_sParameters.usFlags), FLAG_DECAY_BIT);
    g_ucSensorPolarity =
        HWREGBITH(&(g_sParameters.usFlags), FLAG_SENSOR_POLARITY_BIT);

    //
    // Loop through all of the parameters.
    //
    for(ulIdx = 0; ulIdx < g_ulUINumParameters; ulIdx++)
    {
        //
        // If there is an update function for this parameter, then call it now
        // since the parameter value may have changed as a result of the load.
        //
        if(g_sUIParameters[ulIdx].pfnUpdate)
        {
            g_sUIParameters[ulIdx].pfnUpdate();
        }
    }
}

//*****************************************************************************
//
//! Saves the motor drive parameter block to flash.
//!
//! This function is called by the serial user interface when the save
//! parameter block function is called.  The parameter block is written to
//! flash for use the next time a load occurs (be it from an explicit request
//! or a power cycle of the drive).
//!
//! \return None.
//
//*****************************************************************************
void
UIParamSave(void)
{
    //
    // Return without doing anything if the motor drive is running.
    //
    if(MainIsRunning())
    {
        return;
    }

    //
    // Save the parameter block to flash.
    //
    FlashPBSave((unsigned char *)&g_sParameters);
}

//*****************************************************************************
//
//! Starts a firmware upgrade.
//!
//! This function is called by the serial user interface when a firmware
//! upgrade has been requested.  This will branch directly to the boot loader
//! and relinquish all control, never returning.
//!
//! \return Never returns.
//
//*****************************************************************************
void
UIUpgrade(void)
{
    //
    // Emergency stop the motor drive.
    //
    MainEmergencyStop();

    //
    // Disable the watchdog timer.
    //
    WatchdogResetDisable(WATCHDOG_BASE);
    
    //
    // Disable all processor interrupts.  Instead of disabling them one at a
    // time (and possibly missing an interrupt if new sources are added), a
    // direct write to NVIC is done to disable all peripheral interrupts.
    //
    HWREG(NVIC_DIS0) = 0xffffffff;

    //
    // Also disable the SysTick interrupt.
    //
    SysTickIntDisable();

    //
    // Turn off all the on-board LEDs.
    //
    UIRunLEDBlink(0, 0);
    UIFaultLEDBlink(0, 0);

    //
    // Return control to the boot loader.  This is a call to the SVC handler in
    // the boot loader.
    //
    (*((void (*)(void))(*(unsigned long *)0x2c)))();

    //
    // Control should never return here, but just in case it does.
    //
    while(1)
    {
    }
}

//*****************************************************************************
//
//! Handles button presses.
//!
//! This function is called when a press of the on-board push button has been
//! detected.  If the motor drive is running, it will be stopped.  If it is
//! stopped, the direction will be reversed and the motor drive will be
//! started.
//!
//! \return None.
//
//*****************************************************************************
void
UIButtonPress(void)
{
    //
    // See if the motor drive is running.
    //
    if(MainIsRunning())
    {
        //
        // Stop the motor drive.
        //
        MainStop();
    }
    else
    {
        //
        // Reverse the motor drive direction.
        //
        g_ucDirection ^= 1;
        UIDirectionSet();

        //
        // Start the motor drive.
        //
        MainRun();
    }
}

//*****************************************************************************
//
//! Handles button holds.
//!
//! This function is called when a hold of the on-board push button has been
//! detected.  The modulation type of the motor will be toggled between sine
//! wave and space vector modulation, but only if a three phase motor is in
//! use.
//!
//! \return None.
//
//*****************************************************************************
static void
UIButtonHold(void)
{
    //
    // Toggle the modulation type.  UIModulationType() will take care of
    // forcing sine wave modulation for single phase motors.
    //
    //g_ucModulation ^= 1;
    //UIModulationType();
}

//*****************************************************************************
//
//! Sets the blink rate for an LED.
//!
//! \param ulIdx is the number of the LED to configure.
//! \param usRate is the rate to blink the LED.
//! \param usPeriod is the amount of time to turn on the LED.
//!
//! This function sets the rate at which an LED should be blinked.  A blink
//! period of zero means that the LED should be turned off, and a blink period
//! equal to the blink rate means that the LED should be turned on.  Otherwise,
//! the blink rate determines the number of user interface interrupts during
//! the blink cycle of the LED, and the blink period is the number of those
//! user interface interrupts during which the LED is turned on.
//!
//! \return None.
//
//*****************************************************************************
static void
UILEDBlink(unsigned long ulIdx, unsigned short usRate, unsigned short usPeriod)
{
    //
    // Clear the blink rate for this LED.
    //
    g_pusBlinkRate[ulIdx] = 0;

    //
    // A blink period of zero means that the LED should be turned off.
    //
    if(usPeriod == 0)
    {
        //
        // Turn off the LED.
        //
        GPIOPinWrite(g_pulLEDBase[ulIdx], g_pucLEDPin[ulIdx],
                     (ulIdx == 0) ? g_pucLEDPin[0] : 0);
    }

    //
    // A blink rate equal to the blink period means that the LED should be
    // turned on.
    //
    else if(usRate == usPeriod)
    {
        //
        // Turn on the LED.
        //
        GPIOPinWrite(g_pulLEDBase[ulIdx], g_pucLEDPin[ulIdx],
                     (ulIdx == 0) ? 0 : g_pucLEDPin[ulIdx]);
    }

    //
    // Otherwise, the LED should be blinked at the given rate.
    //
    else
    {
        //
        // Save the blink rate and period for this LED.
        //
        g_pusBlinkRate[ulIdx] = usRate;
        g_pusBlinkPeriod[ulIdx] = usPeriod;
    }
}

//*****************************************************************************
//
//! Sets the blink rate for the run LED.
//!
//! \param usRate is the rate to blink the run LED.
//! \param usPeriod is the amount of time to turn on the run LED.
//!
//! This function sets the rate at which the run LED should be blinked.  A
//! blink period of zero means that the LED should be turned off, and a blink
//! period equal to the blink rate means that the LED should be turned on.
//! Otherwise, the blink rate determines the number of user interface
//! interrupts during the blink cycle of the run LED, and the blink period
//! is the number of those user interface interrupts during which the LED is
//! turned on.
//!
//! \return None.
//
//*****************************************************************************
void
UIRunLEDBlink(unsigned short usRate, unsigned short usPeriod)
{
    //
    // The run LED is the first LED.
    //
    UILEDBlink(0, usRate, usPeriod);
}

//*****************************************************************************
//
//! Sets the blink rate for the fault LED.
//!
//! \param usRate is the rate to blink the fault LED.
//! \param usPeriod is the amount of time to turn on the fault LED.
//!
//! This function sets the rate at which the fault LED should be blinked.  A
//! blink period of zero means that the LED should be turned off, and a blink
//! period equal to the blink rate means that the LED should be turned on.
//! Otherwise, the blink rate determines the number of user interface
//! interrupts during the blink cycle of the fault LED, and the blink period
//! is the number of those user interface interrupts during which the LED is
//! turned on.
//!
//! \return None.
//
//*****************************************************************************
void
UIFaultLEDBlink(unsigned short usRate, unsigned short usPeriod)
{
    //
    // The fault LED is the second LED.
    //
    UILEDBlink(1, usRate, usPeriod);
}

//*****************************************************************************
//
//! This function returns the current number of system ticks.
//!
//! \return The number of system timer ticks.
//
//*****************************************************************************
unsigned long
UIGetTicks(void)
{
    unsigned long ulTime1;
    unsigned long ulTime2;
    unsigned long ulTicks;

    //
    // We read the SysTick value twice, sandwiching taking the snapshot of
    // the tick count value. If the second SysTick read gives us a higher
    // number than the first read, we know that it wrapped somewhere between
    // the two reads so our tick count value is suspect.  If this occurs,
    // we go round again. Note that it is not sufficient merely to read the
    // values with interrupts disabled since the SysTick counter keeps
    // counting regardless of whether or not the wrap interrupt has been
    // servi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -