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

📄 bot_example.c

📁 This software package contains the USB framework core developped by ATMEL, as well as a Mass stora
💻 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: bot_example.c 198 2006-10-30 10:01:09Z 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 "msd.h"
#include "media.h"
#include "media_flash.h"
#include "sbc.h"
#include "lun.h"
#include "bot_driver.h"
#include "sbc_methods.h"

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

//! \brief  Callbacks used by the BOT driver
static void CBK_Init(const S_usb *);
static void CBK_Reset(const S_usb *);
static void CBK_Suspend(const S_usb *);
static void CBK_Resume(const S_usb *);
static void CBK_NewRequest(const S_usb *);

//------------------------------------------------------------------------------
//      Global variables
//------------------------------------------------------------------------------

//! \brief  Endpoints used by the BOT driver
//! \see    S_usb_endpoint
//! \see    S_usb
S_usb_endpoint pEndpoints[] = {

    USB_ENDPOINT_SINGLEBANK,
    USB_ENDPOINT_DUALBANK,
    USB_ENDPOINT_DUALBANK
};

//! \brief  Setup packet structure used by the USB driver
//! \see    S_usb_setup
//! \see    S_usb
S_usb_request sSetup;

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

//! \brief  List of callbacks used by the BOT driver
//! \see    S_usb_callbacks
//! \see    S_usb
const S_usb_callbacks sCallbacks = {

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

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

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

//! \brief  Internal flash media instance
//! \see    S_media
S_media sFlash;

//! \brief  Bulk-Only Transport (BOT) driver instance
//! \see    S_bot
S_bot sBot;

//! \brief  LUNs used by the BOT driver
//! \see    S_lun
//! \see    S_bot
S_lun pLun[2];

//! \brief  Read/write buffer for LUNs
//! \see    S_lun
unsigned char pBuffer[512];

//------------------------------------------------------------------------------
//      Interrupt handlers
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief  Media interrupt handler
//------------------------------------------------------------------------------
void ISR_Media()
{
    MED_HandleAll(&sFlash, 1);
}

//------------------------------------------------------------------------------
//! \brief  Interrupt handler for the USB controller.
//!
//!         Calls USB_handler with the USB driver as a parameter.
//------------------------------------------------------------------------------
void ISR_Driver()
{
    USB_Handler(&sUsb);
}

//------------------------------------------------------------------------------
//! \brief  VBus state change interrupt handler
//!
//!         Forwards the call to the USB_Attach method which acts depending on
//!         the new state of VBus.
//------------------------------------------------------------------------------
#ifndef USB_BUS_POWERED
void ISR_VBus()
{
    USB_Attach(&sUsb);

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

//------------------------------------------------------------------------------
//      Callbacks
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief  Configures USB controller and VBUS monitoring interrupts
//! \param  pUsb Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Init(const S_usb *pUsb)
{
    // Configure 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  Performs the BOT driver initialization upon USB reset
//! \param  pUsb Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Reset(S_usb const *pUsb)
{
    BOT_Reset(&sBot);
}

//------------------------------------------------------------------------------
//! \brief  Dispatches new requests to the BOT handler
//! \param  pUsb Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_NewRequest(S_usb const *pUsb)
{
    BOT_RequestHandler(&sBot);
}

//------------------------------------------------------------------------------
//! \brief  Puts the device in low-power mode except when traces are enabled.
//! \param  pUsb Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Suspend(const S_usb * psUsb)
{
    // Low-power mode
#ifdef NOTRACES
    DEV_Suspend();
#endif
}

//------------------------------------------------------------------------------
//! \brief  Puts the device in normal operation mode
//! \param  pUsb Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Resume(const S_usb * psUsb)
{
    // Normal mode
#ifdef NOTRACES
    DEV_Resume();
#endif
}

//------------------------------------------------------------------------------
//      Main
//------------------------------------------------------------------------------
int main()
{
    // Initialize traces (if not disabled by the NOTRACES option)
    TRACE_INIT();
    TRACE_INFO("I: MSD main\n\r");

    // LEDs (if not disabled by the NOLEDS option)
    LED_INIT();
    LED_ON(LED_POWER);
    LED_OFF(LED_USB);
    LED_OFF(LED_MEM);

    // Memory initialization
    FLA_Init(&sFlash);

    // Install handler for flash interrupt
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
                          AT91C_ID_SYS,
                          AT91C_AIC_PRIOR_LOWEST,
                          AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
                          ISR_Media);
    AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS);

    // LUNs initialization
    LUN_Init(&(pLun[0]), &sFlash, pBuffer, 20*1024, 22*1024, 512);
    LUN_Init(&(pLun[1]), &sFlash, pBuffer, 42*1024, 22*1024, 512);

    // BOT driver initialization
    BOT_Init(&sBot, &sUsb, pLun, 2);

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

    // Wait for the device to be in the Configured state
    while (!ISSET(USB_GetState(&sUsb), USB_STATE_CONFIGURED));

    TRACE_INFO("I: Connected\n\r");

    // Main loop
    while (1) {

        // BOT state machine
        BOT_StateMachine(&sBot);
    }
}

⌨️ 快捷键说明

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