bot_example.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 348 行

C
348
字号
/* ----------------------------------------------------------------------------
 *         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,v 1.1.2.1 2006/12/05 08:33:25 danielru Exp $
*/

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

#include <intrinsics.h>
#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 "media_sdram.h"
#include "sbc.h"
#include "lun.h"
#include "bot_driver.h"
#include "sbc_methods.h"

#if defined(AT91SAM9260EK) || defined(AT91SAM9261EK)
#pragma data_alignment = 4
__no_init static unsigned char Lun0Drv[22*1024];
#pragma data_alignment = 4
__no_init static unsigned char Lun1Drv[22*1024];
#else
#pragma location = (20*1024)
__no_init static unsigned char Lun0Drv[22*1024];
#pragma location = (42*1024)
__no_init static unsigned char Lun1Drv[22*1024];
#endif

//------------------------------------------------------------------------------
//      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/sdram media instance
//! \see    S_media
#if defined(AT91SAM9261EK) || defined(AT91SAM9260EK)
S_media sSDRAM;
#else
S_media sFlash;
#endif

//! \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()
{
#if defined(AT91SAM9261EK) || defined(AT91SAM9260EK)
    MED_HandleAll(&sSDRAM, 1);
#else
    MED_HandleAll(&sFlash, 1);
#endif
}

//------------------------------------------------------------------------------
//! \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()
{
    __enable_interrupt();
    // 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
#if defined(AT91SAM9261EK) || defined(AT91SAM9260EK)
    SDRAM_Init(&sSDRAM);
#else
    FLA_Init(&sFlash);
#endif

    // Install handler for flash interrupt
#if defined(AT91SAM9260EK)
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
                          AT91C_ID_SYS,
                          AT91C_AIC_PRIOR_LOWEST,
                          AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
                          ISR_Media);
#else
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
                          AT91C_ID_SYS,
                          AT91C_AIC_PRIOR_LOWEST,
                          AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
                          ISR_Media);
#endif
    AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS);

    // LUNs initialization
#if defined(AT91SAM9261EK) || defined(AT91SAM9260EK)
    LUN_Init(&(pLun[0]),
             &sSDRAM,
             pBuffer,
            (unsigned int)Lun0Drv - sSDRAM.dBaseAddress,
             sizeof(Lun0Drv), 512);
    LUN_Init(&(pLun[1]),
             &sSDRAM, pBuffer,
            (unsigned int)Lun1Drv - sSDRAM.dBaseAddress,
             sizeof(Lun1Drv),
             512);
#else
    LUN_Init(&(pLun[0]), &sFlash, pBuffer, (unsigned int)Lun0Drv, sizeof(Lun0Drv), 512);
    LUN_Init(&(pLun[1]), &sFlash, pBuffer, (unsigned int)Lun1Drv, sizeof(Lun1Drv), 512);
#endif

    // 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 + =
减小字号Ctrl + -
显示快捷键?