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

📄 ui_serial.c

📁 Luminary Micro BLDC motor control software
💻 C
📖 第 1 页 / 共 4 页
字号:
            //
            case CMD_DISABLE_DATA_ITEM:
            {
                //
                // Fill in the response.
                //
                g_pucUISerialResponse[0] = TAG_STATUS;
                g_pucUISerialResponse[1] = 0x04;
                g_pucUISerialResponse[2] = CMD_DISABLE_DATA_ITEM;

                //
                // Disable the data item if it is was validly specified.
                //
                ucSum = g_pucUISerialReceive[(g_ulUISerialReceiveRead + 3) %
                                                        UISERIAL_MAX_RECV];
                if((ucSize == 5) && (ucSum < DATA_NUM_ITEMS))
                {
                    g_pulUIRealTimeData[ucSum / 32] &= ~(1 << (ucSum % 32));
                }

                //
                // Send the response.
                //
                UISerialTransmit(g_pucUISerialResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to start the real-time data stream.
            //
            case CMD_START_DATA_STREAM:
            {
                //
                // Fill in the response.
                //
                g_pucUISerialResponse[0] = TAG_STATUS;
                g_pucUISerialResponse[1] = 0x04;
                g_pucUISerialResponse[2] = CMD_START_DATA_STREAM;

                //
                // Send the response.
                //
                UISerialTransmit(g_pucUISerialResponse);

                //
                // Enable the real-time data stream.
                //
                g_bEnableRealTimeData = true;

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to stop the real-time data stream.
            //
            case CMD_STOP_DATA_STREAM:
            {
                //
                // Fill in the response.
                //
                g_pucUISerialResponse[0] = TAG_STATUS;
                g_pucUISerialResponse[1] = 0x04;
                g_pucUISerialResponse[2] = CMD_STOP_DATA_STREAM;

                //
                // Disable the real-time data stream.
                //
                g_bEnableRealTimeData = false;

                //
                // Send the response.
                //
                UISerialTransmit(g_pucUISerialResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to start the motor drive.
            //
            case CMD_RUN:
            {
                //
                // Pass the run request to the application.
                //
                UIRun();

                //
                // Fill in the response.
                //
                g_pucUISerialResponse[0] = TAG_STATUS;
                g_pucUISerialResponse[1] = 0x04;
                g_pucUISerialResponse[2] = CMD_RUN;

                //
                // Send the response.
                //
                UISerialTransmit(g_pucUISerialResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command to stop the motor drive.
            //
            case CMD_STOP:
            {
                //
                // Pass the stop request to the application.
                //
                UIStop();

                //
                // Fill in the response.
                //
                g_pucUISerialResponse[0] = TAG_STATUS;
                g_pucUISerialResponse[1] = 0x04;
                g_pucUISerialResponse[2] = CMD_STOP;

                //
                // Send the response.
                //
                UISerialTransmit(g_pucUISerialResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // The command for an emmergency stop of the motor drive.
            //
            case CMD_EMERGENCY_STOP:
            {
                //
                // Pass the emergency stop request to the application.
                //
                UIEmergencyStop();

                //
                // Fill in the response.
                //
                g_pucUISerialResponse[0] = TAG_STATUS;
                g_pucUISerialResponse[1] = 0x04;
                g_pucUISerialResponse[2] = CMD_EMERGENCY_STOP;

                //
                // Send the response.
                //
                UISerialTransmit(g_pucUISerialResponse);

                //
                // Done with this command.
                //
                break;
            }

            //
            // An unrecognized command was received.  Simply ignore it.
            //
            default:
            {
                //
                // Done with this command.
                //
                break;
            }
        }

        //
        // Skip this command packet.
        //
        g_ulUISerialReceiveRead =
            (g_ulUISerialReceiveRead + ucSize) % UISERIAL_MAX_RECV;
    }
}

//*****************************************************************************
//
//! Handles the UART interrupt.
//!
//! This is the interrupt handler for the UART.  It will write new data to the
//! UART when there is data to be written, and read new data from the UART when
//! it is available.  Reception of new data results in the receive buffer being
//! scanned for command packets.
//!
//! \return None.
//
//*****************************************************************************
void
UART0IntHandler(void)
{
    unsigned long ulStatus;

    //
    // Get the current UART interrupt status.
    //
    ulStatus = UARTIntStatus(UART0_BASE, true);

    //
    // Clear the UART interrupts that will be handled.
    //
    UARTIntClear(UART0_BASE, ulStatus);

    //
    // See if the transmit interrupt is being asserted.
    //
    if(ulStatus & UART_INT_TX)
    {
        //
        // Loop while there is data in the transmit buffer and space in the
        // UART FIFO.
        //
        while((g_ulUISerialTransmitRead != g_ulUISerialTransmitWrite) &&
              UARTSpaceAvail(UART0_BASE))
        {
            //
            // Send the next character from the transmit buffer.
            //
            UARTCharPut(UART0_BASE,
                        g_pucUISerialTransmit[g_ulUISerialTransmitRead]);

            //
            // Increment the transmit buffer read pointer.
            //
            g_ulUISerialTransmitRead =
                (g_ulUISerialTransmitRead + 1) % UISERIAL_MAX_XMIT;
        }
    }

    //
    // See if the receive interrupt is being asserted.
    //
    if(ulStatus & (UART_INT_RX | UART_INT_RT))
    {
        //
        // Loop while there are characters in the UART FIFO.
        //
        while(UARTCharsAvail(UART0_BASE))
        {
            //
            // Read the next character into the receive buffer.
            //
            g_pucUISerialReceive[g_ulUISerialReceiveWrite] =
                UARTCharGet(UART0_BASE);

            //
            // Increment the receive buffer write pointer.
            //
            g_ulUISerialReceiveWrite =
                (g_ulUISerialReceiveWrite + 1) % UISERIAL_MAX_RECV;

            //
            // Scan the receive buffer for command packets if it is full.
            //
            if(((g_ulUISerialReceiveWrite + 1) % UISERIAL_MAX_RECV) ==
               g_ulUISerialReceiveRead)
            {
                UISerialScanReceive();
            }
        }

        //
        // Scan the receive buffer for command packets.
        //
        UISerialScanReceive();
    }
}

//*****************************************************************************
//
//! Sends a real-time data packet.
//!
//! This function will construct a real-time data packet with the current
//! values of the enabled real-time data items.  Once constructed, the packet
//! will be sent out.
//!
//! \return None.
//
//*****************************************************************************
void
UISerialSendRealTimeData(void)
{
    unsigned long ulIdx, ulPos, ulItem, ulCount;
    unsigned char pucValue[4];

    //
    // Do nothing if the real-time data stream is not enabled.
    //
    if(!g_bEnableRealTimeData)
    {
        return;
    }

    //
    // Loop through the available real-time data items.
    //
    for(ulItem = 0, ulPos = 2; ulItem < g_ulUINumRealTimeData; ulItem++)
    {
        //
        // Get the ID for this real-time data item.
        //
        ulIdx = g_sUIRealTimeData[ulItem].ucID;

        //
        // See if this real-time data item is enabled.
        //
        if(g_pulUIRealTimeData[ulIdx / 32] & (1 << (ulIdx % 32)))
        {
            //
            // Perform an atomic copy of the value to a local variable.  This
            // prevents the value from being changed midway through adding it
            // to the output real-time data stream.
            //
            *((unsigned long *)pucValue) =
                *((unsigned long *)g_sUIRealTimeData[ulItem].pucValue);

            //
            // Copy the value of this real-time data item, byte by byte, to the
            // packet.
            //
            for(ulCount = 0; ulCount < g_sUIRealTimeData[ulItem].ucSize;
                ulCount++)
            {
                g_pucUISerialData[ulPos++] = pucValue[ulCount];
            }
        }
    }

    //
    // Put the header and length on the real-time data packet.
    //
    g_pucUISerialData[0] = TAG_DATA;
    g_pucUISerialData[1] = ulPos + 1;

    //
    // Send the real-time data packet.  The UART interrupt is disabled during
    // this time to prevent a UART interrupt from inserting a status packet in
    // the middle of the real-time data packet in the UART output stream.
    //
    IntDisable(INT_UART0);
    UISerialTransmit(g_pucUISerialData);
    IntEnable(INT_UART0);
}

//*****************************************************************************
//
//! Initializes the serial user interface.
//!
//! This function prepares the serial user interface for operation.  The UART
//! is configured for 115,200, 8-N-1 operation.  This function should be called
//! before any other serial user interface operations.
//!
//! \return None.
//
//*****************************************************************************
void
UISerialInit(void)
{
    //
    // Enable GPIO port A and UART 0.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Configure PA0 and PA1 as UART pins.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE |
                         UART_CONFIG_STOP_ONE));

    //
    // Enable the UART transmit, receive, and receive timeout interrupts.
    //
    UARTIntEnable(UART0_BASE, UART_INT_TX | UART_INT_RX | UART_INT_RT);
    IntEnable(INT_UART0);
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

⌨️ 快捷键说明

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