SAMV71 Xplained Ultra Software Package 1.5

MSDDStateMachine.h

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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              (1 << 3)
00158 /** Data transfer from host to device (WRITE) */
00159 #define MSDD_HOST_TO_DEVICE              (1 << 4)
00160 /** No data transfer */
00161 #define MSDD_NO_TRANSFER                 (1 << 5)
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     MSCbw           cbw;         /**< Received CBW (31 bytes) */
00192     uint8_t         state;       /**< Current command state */
00193     MSCsw           csw;         /**< CSW to send  (13 bytes) */
00194     MSDTransfer     transfer;    /**< Current transfer status (USB) */
00195     MSDTransfer     disktransfer;/**< Current transfer status (Disk) */
00196     uint32_t        length;      /**< Remaining length of command */ 
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     /** State of the currently executing command */
00210     MSDCommandState commandState;
00211     /** USB Driver for the %device. */
00212     USBDDriver *pUsbd;
00213     /** LUN list for the %device. */
00214     MSDLun *luns;
00215     /** Associated interface number */
00216     uint8_t interfaceNb;
00217     /** Maximum LUN index */
00218     uint8_t maxLun;
00219     /** Current state of the driver */
00220     uint8_t state;
00221     /** Indicates if the driver is waiting for a reset recovery */
00222     uint8_t waitResetRecovery;
00223 } MSDDriver;
00224 
00225 /*-----------------------------------------------------------------------------
00226  *      Inline functions
00227  *-----------------------------------------------------------------------------*/
00228 
00229 /*-----------------------------------------------------------------------------
00230  *      Exported functions
00231  *-----------------------------------------------------------------------------*/
00232 
00233 extern void MSDD_StateMachine(MSDDriver * pMsdDriver);
00234 
00235 /**@}*/
00236 
00237 #endif /* #define MSDDSTATEMACHINE_H */
00238 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines