SAMV71 Xplained Ultra Software Package 1.3

MSDDStateMachine.h

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /** \file
00031  * \section Purpose
00032  *
00033  * Definitions, structs, functions  required by a Mass Storage device driver
00034  * state machine..
00035  *
00036  * \section Usage
00037  *
00038  * - For a USB device:
00039  *     -# MSDD_StateMachine is invoked to run the MSD state machine.
00040  *
00041  *-----------------------------------------------------------------------------*/
00042 
00043 #ifndef MSDDSTATEMACHINE_H
00044 #define MSDDSTATEMACHINE_H
00045 
00046 /** \addtogroup usbd_msd
00047  *@{
00048  */
00049 
00050 /*-----------------------------------------------------------------------------
00051  *         Headers
00052  *-----------------------------------------------------------------------------*/
00053 
00054 #include "MSD.h"
00055 #include "MSDLun.h"
00056 #include <USBDDriver.h>
00057 #include <USBLib_Trace.h>
00058 
00059 /*-----------------------------------------------------------------------------
00060  *      Definitions
00061  *-----------------------------------------------------------------------------*/
00062 
00063 /** \addtogroup usbd_msd_driver_possible_states MSD Driver Possible states
00064  *      @{
00065  *
00066  * - MSDD_STATE_READ_CBW
00067  * - MSDD_STATE_WAIT_CBW
00068  * - MSDD_STATE_PROCESS_CBW
00069  * - MSDD_STATE_WAIT_HALT
00070  * - MSDD_STATE_SEND_CSW
00071  * - MSDD_STATE_WAIT_CSW
00072  * - MSDD_STATE_WAIT_RESET
00073  */
00074 
00075 /** \brief  Driver is expecting a command block wrapper */
00076 #define MSDD_STATE_READ_CBW              (1 << 0)
00077 
00078 /** \brief  Driver is waiting for the transfer to finish */
00079 #define MSDD_STATE_WAIT_CBW              (1 << 1)
00080 
00081 /** \brief  Driver is processing the received command */
00082 #define MSDD_STATE_PROCESS_CBW           (1 << 2)
00083 
00084 /** \brief  Driver is halted because pipe halt or wait reset */
00085 #define MSDD_STATE_WAIT_HALT             (1 << 3)
00086 
00087 /** \brief  Driver is starting the transmission of a command status wrapper */
00088 #define MSDD_STATE_SEND_CSW              (1 << 4)
00089 
00090 /** \brief  Driver is waiting for the CSW transmission to finish */
00091 #define MSDD_STATE_WAIT_CSW              (1 << 5)
00092 
00093 /** \brief  Driver is waiting for the MassStorageReset */
00094 #define MSDD_STATE_WAIT_RESET            (1 << 6)
00095 /**      @}*/
00096 
00097 /** \addtogroup usbd_msd_driver_result_codes MSD Driver Result Codes
00098  *      @{
00099  * This page lists result codes for MSD functions.
00100  *
00101  * \section Codes
00102  * - MSDD_STATUS_SUCCESS
00103  * - MSDD_STATUS_ERROR
00104  * - MSDD_STATUS_INCOMPLETE
00105  * - MSDD_STATUS_PARAMETER
00106  * - MSDD_STATUS_RW
00107  */
00108 
00109 /** \brief  Method was successful */
00110 #define MSDD_STATUS_SUCCESS              0x00
00111 
00112 /** \brief  There was an error when trying to perform a method */
00113 #define MSDD_STATUS_ERROR                0x01
00114 
00115 /** \brief  No error was encountered but the application should call the
00116             method again to continue the operation */
00117 #define MSDD_STATUS_INCOMPLETE           0x02
00118 
00119 /** \brief  A wrong parameter has been passed to the method */
00120 #define MSDD_STATUS_PARAMETER            0x03
00121 
00122 /** \brief An error when reading/writing disk (protected or not present) */
00123 #define MSDD_STATUS_RW                   0x04
00124 /**      @}*/
00125 
00126 /** \addtogroup usbd_msd_driver_action_cases MSD Driver Action Cases
00127  *      @{
00128  * This page lists actions to perform during the post-processing phase of a
00129  * command.
00130  *
00131  * \section Actions
00132  * - MSDD_CASE_PHASE_ERROR
00133  * - MSDD_CASE_STALL_IN
00134  * - MSDD_CASE_STALL_OUT
00135  */
00136 
00137 /** \brief  Indicates that the CSW should report a phase error */
00138 #define MSDD_CASE_PHASE_ERROR            (1 << 0)
00139 
00140 /** \brief  The driver should halt the Bulk IN pipe after the transfer */
00141 #define MSDD_CASE_STALL_IN               (1 << 1)
00142 
00143 /** \brief  The driver should halt the Bulk OUT pipe after the transfer */
00144 #define MSDD_CASE_STALL_OUT              (1 << 2)
00145 /**      @}*/
00146 
00147 /*------------------------------------------------------------------------------ */
00148 
00149 /** \addtogroup usbd_msd_driver_xfr_directions MSD Driver Xfr Directions
00150  *      @{
00151  * This page lists possible direction values for a data transfer
00152  * - MSDD_DEVICE_TO_HOST
00153  * - MSDD_HOST_TO_DEVICE
00154  * - MSDD_NO_TRANSFER
00155  */
00156 /** Data transfer from device to host (READ) */
00157 #define MSDD_DEVICE_TO_HOST              0
00158 /** Data transfer from host to device (WRITE) */
00159 #define MSDD_HOST_TO_DEVICE              1
00160 /** No data transfer */
00161 #define MSDD_NO_TRANSFER                 2
00162 /**      @}*/
00163 
00164 /*-----------------------------------------------------------------------------
00165  *         Types
00166  *-----------------------------------------------------------------------------*/
00167 
00168 /**
00169  * \typedef MSDTransfer
00170  * \brief  Structure for holding the result of a USB transfer
00171  * \see    MSDDriver_Callback
00172  */
00173 typedef struct _MSDTransfer {
00174 
00175              uint32_t transferred; /** Number of bytes transferred */
00176              uint32_t remaining;   /** Number of bytes not transferred */
00177              uint16_t semaphore;   /** Semaphore to indicate transfer completion */
00178     //volatile uint16_t semaphore;   /** Semaphore to indicate transfer completion */
00179              uint16_t status;      /** Operation result code */
00180 } MSDTransfer;
00181 
00182 /**
00183  * \typedef MSDCommandState
00184  * \brief  Status of an executing command
00185  * \see    MSDCbw
00186  * \see    MSDCsw
00187  * \see    MSDTransfer
00188  *------------------------------------------------------------------------------*/
00189 typedef struct _MSDCommandState {
00190 
00191     MSDTransfer     transfer;    /**< Current transfer status (USB) */
00192     MSDTransfer     disktransfer;/**< Current transfer status (Disk) */
00193     uint32_t        length;      /**< Remaining length of command */
00194     MSCbw           cbw;         /**< Received CBW (31 bytes) */
00195     uint8_t         state;       /**< Current command state */
00196     MSCsw           csw;         /**< CSW to send  (13 bytes) */
00197     uint8_t         postprocess; /**< Actions to perform when command is complete */
00198     uint8_t         pipeIN;      /**< Pipe ID for input */
00199     uint8_t         pipeOUT;     /**< Pipe ID for output */
00200 } MSDCommandState;
00201 
00202 /**
00203  * \typedef MSDDriver
00204  * \brief  MSD driver state variables
00205  * \see    MSDCommandState
00206  * \see    MSDLun
00207  */
00208 typedef struct _MSDDriver {
00209 
00210     /** USB Driver for the %device. */
00211     USBDDriver *pUsbd;
00212     /** LUN list for the %device. */
00213     MSDLun *luns;
00214     /** State of the currently executing command */
00215     MSDCommandState commandState;
00216     /** Associated interface number */
00217     uint8_t interfaceNb;
00218     /** Maximum LUN index */
00219     uint8_t maxLun;
00220     /** Current state of the driver */
00221     uint8_t state;
00222     /** Indicates if the driver is waiting for a reset recovery */
00223     uint8_t waitResetRecovery;
00224 } MSDDriver;
00225 
00226 /*-----------------------------------------------------------------------------
00227  *      Inline functions
00228  *-----------------------------------------------------------------------------*/
00229 
00230 /*-----------------------------------------------------------------------------
00231  *      Exported functions
00232  *-----------------------------------------------------------------------------*/
00233 
00234 extern void MSDD_StateMachine(MSDDriver * pMsdDriver);
00235 
00236 /**@}*/
00237 
00238 #endif /* #define MSDDSTATEMACHINE_H */
00239 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines