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

📄 serial_example.c

📁 AT91系列芯片的USB虚拟串口的源代码
💻 C
字号:
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support  -  ROUSSET  -
 * ----------------------------------------------------------------------------
 * Copyright (c) 2006, Atmel Corporation

 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaiimer below.
 *
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the disclaimer below in the documentation and/or
 * other materials provided with the distribution.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

/*
$Id: serial_example.c 107 2006-10-16 08:28:50Z jjoannic $
*/

//------------------------------------------------------------------------------
//      Includes
//------------------------------------------------------------------------------

#include "core/common.h"
#include "core/device.h"
#include "core/board.h"
#include "core/trace.h"
#include "core/usb.h"
#include "core/standard.h"
#include "cdc.h"
#include "serial_driver.h"

//------------------------------------------------------------------------------
//      Definitions
//------------------------------------------------------------------------------

//! \brief  Length of the loopback data buffer
#define USB_BUFFER_SIZE             50

//------------------------------------------------------------------------------
//      Prototypes
//------------------------------------------------------------------------------

//! \brief  Initialization callback
static void CBK_Init(const S_usb *pUsb);

//! \brief  Suspend callback
static void CBK_Suspend(const S_usb *pUsb);

//! \brief  Resume callback
static void CBK_Resume(const S_usb *pUsb);

//! \brief  New request callback
static void CBK_NewRequest(const S_usb *pUsb);

//! \brief  New reset callback
//static void CBK_Reset(const S_usb *pUsb);

//! \brief  New SOF callback
//static void CBK_SOF(const S_usb *pUsb);

//------------------------------------------------------------------------------
//      Internal variables
//------------------------------------------------------------------------------

//! \brief  List of endpoints (including endpoint 0) used by the device.
//! \see    S_usb_endpoint
static S_usb_endpoint pEndpoints[] = {

    USB_ENDPOINT_SINGLEBANK, // Control endpoint 0
    USB_ENDPOINT_DUALBANK,   // Data out endpoint
    USB_ENDPOINT_DUALBANK,   // Data in endpoint
    USB_ENDPOINT_SINGLEBANK, // Notification endpoint
};

//! \brief  Variable used to store the last received SETUP packet.
//! \see    S_usb_request
//! \see    S_usb
static S_usb_request sSetup;

//! \brief  Variable used to store the current device state
//! \see    S_usb
static unsigned int dState;

//! \brief  List of implemented callbacks
//! \see    S_usb_callbacks
//! \see    S_usb
static const S_usb_callbacks sCallbacks = {

    CBK_Init,
    0, // CBK_Reset
    CBK_Suspend,
    CBK_Resume,
    CBK_NewRequest,
    0  // CBK_SOF
};

//! \brief  USB driver instance
//! \see    S_usb
static const S_usb sUsb = {

    &sDefaultDriver,
    pEndpoints,
    SER_NUM_ENDPOINTS,
    &sCallbacks,
    &sSetup,
    &dState
};

//! \brief  CDC serial class driver instance
//! \see    S_ser
static S_ser sSer;

// Buffer for receiving data from the USB
static unsigned char pUsbBuffer[USB_BUFFER_SIZE];

//------------------------------------------------------------------------------
//      Internal Functions
//------------------------------------------------------------------------------

// Interrupt Service Routines
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief  Handler for the USB controller interrupt
//!
//!         Defers the call to the USB_Handler function.
//------------------------------------------------------------------------------
void ISR_Driver(void)
{
    USB_Handler(&sUsb);
}

//------------------------------------------------------------------------------
//! \brief  Handler for the VBus state change interrupt
//!
//!         This method calls the USB_Attach function to perform the necessary
//!         operations.
//------------------------------------------------------------------------------
#if !defined(USB_BUS_POWERED)
void ISR_VBus(void)
{
    USB_Attach(&sUsb);

    // Acknowledge the interrupt
    AT91F_PIO_GetInterruptStatus(AT91C_PIO_VBUS);
}
#endif // !defined(USB_BUS_POWERED)

// Callbacks
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief  Callback invoked during the initialization of the USB driver
//!
//!         Configures and enables USB controller and VBus monitoring interrupts
//! \param  pUsb    Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Init(const S_usb *pUsb)
{
    // Configure and enable the USB controller interrupt
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
                          USB_GetDriverID(pUsb),
                          AT91C_AIC_PRIOR_LOWEST,
                          0, //AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
                          ISR_Driver);

    AT91F_AIC_EnableIt(AT91C_BASE_AIC, USB_GetDriverID(pUsb));

#ifndef USB_BUS_POWERED
    // Configure VBus monitoring
    BRD_ConfigureVBus(USB_GetDriverInterface(pUsb));

    // Configure and enable the Vbus detection interrupt
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
                          AT91C_ID_VBUS,
                          AT91C_AIC_PRIOR_LOWEST,
                          0, //AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
                          ISR_VBus);

    AT91F_PIO_InterruptEnable(AT91C_PIO_VBUS, AT91C_VBUS);
    AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_VBUS);
#else
    // Power up the USB controller
    USB_Attach(pUsb);
#endif
}
//------------------------------------------------------------------------------
//! \brief  Callback invoked when the device becomes suspended
//!
//!         Disables LEDs (if they are used) and then puts the device into
//!         low-power mode. When traces are used, the device does not enter
//!         low-power mode to avoid losing some outputs.
//! \param  pUsb    Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Suspend(const S_usb *pUsb)
{
    LED_OFF(LED_POWER);
    LED_OFF(LED_USB);
    LED_OFF(LED_MEM);

#if defined(NOTRACES)
    DEV_Suspend();
#endif
}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when the device leaves the suspended state
//!
//!         The device is first returned to a normal operating mode and LEDs are
//!         re-enabled. When traces are used, the device does not enter
//!         low-power mode to avoid losing some outputs.
//! \param  pUsb    Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Resume(const S_usb *pUsb)
{
#if defined(NOTRACES)
    DEV_Resume();
#endif

    LED_INIT();
    LED_ON(LED_POWER);
    LED_OFF(LED_USB);
    LED_OFF(LED_MEM);
}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when a new SETUP request is received
//!
//!         The new request if forwarded to the standard request handler,
//!         which performs the enumeration of the device.
//! \param  pUsb   Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_NewRequest(const S_usb *pUsb)
{
    SER_RequestHandler(&sSer);
}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when a Reset request is received
//!
//! \param  pUsb   Pointer to a S_usb instance
//------------------------------------------------------------------------------
//static void CBK_Reset(const S_usb *pUsb)
//{
    // Put your reset handler here
//}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when a SOF is received
//!
//! \param  pUsb   Pointer to a S_usb instance
//------------------------------------------------------------------------------
//static void CBK_SOF(const S_usb *pUsb)
//{
    // Put your SOF handler here
//}

// Other functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief  Forwards data receiving from the USB host through the USART
//!
//!         This function operates asynchronously.
//! \param  pBuffer           Must be 0 when function is first called
//! \param  bStatus           Same as above
//! \param  dBytesTransferred Same as above
//! \param  dBytesRemaining   Same as above
//------------------------------------------------------------------------------
static void ForwardUSB2Serial(unsigned char *pBuffer,
                              unsigned char bStatus,
                              unsigned int dBytesTransferred,
                              unsigned int dBytesRemaining)
{
    if (pBuffer != 0) {

        if (bStatus != SER_STATUS_SUCCESS) {

            TRACE_WARNING("W: ForwardUSB2Serial: Transfer error\n\r");
        }
        else {

            pBuffer[dBytesTransferred] = 0;
            TRACE_DEBUG_M("%d bytes received, %d bytes remaining, forwarding 0x%02X\n\r",
                          dBytesTransferred, dBytesRemaining, pBuffer[0]);

            AT91F_US_SendFrame(AT91C_BASE_US0, (char *)pBuffer,
                               dBytesTransferred, 0, 0);
        }
    }

    do {

        bStatus = SER_Read(&sSer,
                           pUsbBuffer,
                           USB_BUFFER_SIZE,
                           (Callback_f) ForwardUSB2Serial,
                           pUsbBuffer);
    }
    while (bStatus != SER_STATUS_SUCCESS);
}

//------------------------------------------------------------------------------
//! \brief  Forwards data from the USART to the USB host
//!
//!         This function is synchronous and blocks the program execution when
//!         no data is received.
//------------------------------------------------------------------------------
static void ForwardSerial2USB() {

    unsigned char bChar;
    unsigned char bStatus;

    // Receive a character from the USART
    while (AT91F_US_RxReady(AT91C_BASE_US0) != 1) {}
    bChar = (unsigned char) AT91F_US_GetChar(AT91C_BASE_US0);

    // Send data to USB host
    do {

        bStatus = SER_Write(&sSer, &bChar, 1, 0, 0);
    }
    while (bStatus != SER_STATUS_SUCCESS);
}

//------------------------------------------------------------------------------
//          Main
//------------------------------------------------------------------------------
int main()
{
    TRACE_INIT();
    TRACE_INFO("\n\rMain CDC Serial\n\r");

    // Initialize the serial driver
    SER_Init(&sSer, &sUsb);

    // Configure USART
    AT91F_US0_CfgPMC();
    AT91F_US0_CfgPIO();
    AT91F_US_Configure(AT91C_BASE_US0, AT91C_MASTER_CLOCK,
                       AT91C_US_ASYNC_MODE, 115200, 0);
    AT91F_US_EnableRx(AT91C_BASE_US0);
    AT91F_US_EnableTx(AT91C_BASE_US0);

    TRACE_INFO("Connecting ... ");

    // Wait for the device to be powered before connecting it
    while (!ISSET(USB_GetState(&sUsb), USB_STATE_POWERED));

    USB_Connect(&sUsb);

    TRACE_INFO("OK\n\r");

    // Start USB->Serial forward mechanism
    ForwardUSB2Serial(0, 0, 0, 0);

    // Main loop
    while (1) {

        // Perform Serial->USB bridging
        ForwardSerial2USB();
    }
}

⌨️ 快捷键说明

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