SAMV71 Xplained Ultra Software Package 1.4

Media.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2012, 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 
00032 /*---------------------------------------------------------------------------
00033  *         Headers
00034  *---------------------------------------------------------------------------*/
00035 
00036 #include "Media.h"
00037 
00038 /*---------------------------------------------------------------------------
00039  *      Exported Global Variables
00040  *---------------------------------------------------------------------------*/
00041 
00042 /** Number of medias which are effectively used. */
00043 uint32_t gNbMedias = 0 ;
00044 
00045 /*---------------------------------------------------------------------------
00046  *      Exported Functions
00047  *---------------------------------------------------------------------------*/
00048 
00049 /**
00050  *  \brief  Writes data on a media
00051  *  \param  media    Pointer to a Media instance
00052  *  \param  address  Address at which to write
00053  *  \param  data     Pointer to the data to write
00054  *  \param  length   Size of the data buffer
00055  *  \param  callback Optional pointer to a callback function to invoke when
00056  *                    the write operation terminates
00057  *  \param  argument Optional argument for the callback function
00058  *  \return Operation result code
00059  *  \see    TransferCallback
00060  */
00061 extern uint8_t MED_Write( sMedia* pMedia,
00062                           uint32_t address,
00063                           void* data, uint32_t length,
00064                           MediaCallback callback, void* argument )
00065 {
00066     return pMedia->write( pMedia, address, data, length, callback, argument ) ;
00067 }
00068 
00069 /**
00070  *  \brief  Reads a specified amount of data from a media
00071  *
00072  *  \param  media    Pointer to a Media instance
00073  *  \param  address  Address of the data to read
00074  *  \param  data     Pointer to the buffer in which to store the retrieved
00075  *                    data
00076  *  \param  length   Length of the buffer
00077  *  \param  callback Optional pointer to a callback function to invoke when
00078  *                    the operation is finished
00079  *  \param  argument Optional pointer to an argument for the callback
00080  *  \return Operation result code
00081  *  \see    TransferCallback
00082  */
00083 extern uint8_t MED_Read( sMedia* pMedia, uint32_t address,
00084                          void* data, uint32_t length,
00085                          MediaCallback callback, void* argument )
00086 {
00087     return pMedia->read( pMedia, address, data, length, callback, argument ) ;
00088 }
00089 
00090 /**
00091  *  \brief  Locks all the regions in the given address range.
00092  *  \param  media    Pointer to a Media instance
00093  *  \param  start  Start address of lock range.
00094  *  \param  end  End address of lock range.
00095  *  \param  pActualStart  Start address of the actual lock range (optional).
00096  *  \param  pActualEnd  End address of the actual lock range (optional).
00097  *  \return 0 if successful; otherwise returns an error code.
00098  */
00099 extern uint8_t MED_Lock( sMedia* pMedia,
00100                                 uint32_t start, uint32_t end,
00101                                 uint32_t *pActualStart, uint32_t *pActualEnd )
00102 {
00103     if ( pMedia->lock ) {
00104         return pMedia->lock( pMedia, start, end, pActualStart, pActualEnd ) ;
00105     } else {
00106         return MED_STATUS_SUCCESS ;
00107     }
00108 }
00109 
00110 /**
00111  *  \brief  Unlocks all the regions in the given address range
00112  *  \param  media    Pointer to a Media instance
00113  *  \param start  Start address of unlock range.
00114  *  \param end  End address of unlock range.
00115  *  \param pActualStart  Start address of the actual unlock range (optional).
00116  *  \param pActualEnd  End address of the actual unlock range (optional).
00117  *  \return 0 if successful; otherwise returns an error code.
00118  */
00119 extern uint8_t MED_Unlock( sMedia* pMedia,
00120                                 uint32_t start, uint32_t end,
00121                                 uint32_t *pActualStart, uint32_t *pActualEnd )
00122 {
00123     if ( pMedia->unlock ) {
00124         return pMedia->unlock( pMedia, start, end, pActualStart, pActualEnd ) ;
00125     } else {
00126         return MED_STATUS_SUCCESS ;
00127     }
00128 }
00129 
00130 /**
00131  *  \brief
00132  *  \param  media Pointer to the Media instance to use
00133  */
00134 extern uint8_t MED_Flush( sMedia* pMedia )
00135 {
00136     if ( pMedia->flush ) {
00137         return pMedia->flush( pMedia ) ;
00138     } else {
00139         return MED_STATUS_SUCCESS ;
00140     }
00141 }
00142 
00143 /**
00144  *  \brief  Invokes the interrupt handler of the specified media
00145  *  \param  media Pointer to the Media instance to use
00146  */
00147 extern void MED_Handler( sMedia* pMedia )
00148 {
00149     if ( pMedia->handler ) {
00150         pMedia->handler( pMedia ) ;
00151     }
00152 }
00153 
00154 /**
00155  *  \brief  Reset the media interface to un-configured state.
00156  *  \param  media Pointer to the Media instance to use
00157  */
00158 extern void MED_DeInit( sMedia* pMedia )
00159 {
00160     pMedia->state = MED_STATE_NOT_READY ;
00161 }
00162 
00163 /**
00164  *  \brief  Check if the Media instance is ready to use.
00165  *  \param  media Pointer to the Media instance to use
00166  */
00167 extern uint8_t MED_IsInitialized( sMedia* pMedia )
00168 {
00169     return (pMedia->state != MED_STATE_NOT_READY) ;
00170 }
00171 
00172 /**
00173  *  \brief  Check if the Media instance is busy in transfer.
00174  *  \param  pMedia Pointer to the Media instance to use
00175  */
00176 extern uint8_t MED_IsBusy(sMedia * pMedia)
00177 {
00178     return (pMedia->state == MED_STATE_BUSY);
00179 }
00180 
00181 /**
00182  *  \brief  Check if the Media supports mapped reading.
00183  *  \param  pMedia Pointer to the Media instance to use
00184  */
00185 extern uint8_t MED_IsMappedRDSupported(sMedia *pMedia)
00186 {
00187     return pMedia->mappedRD;
00188 }
00189 
00190 /**
00191  *  \brief  Check if the Media supports mapped writing.
00192  *  \param  pMedia Pointer to the Media instance to use
00193  */
00194 extern uint8_t MED_IsMappedWRSupported(sMedia *pMedia)
00195 {
00196     return pMedia->mappedRD;
00197 }
00198 
00199 /**
00200  *  \brief  Check if the Media is write protected.
00201  *  \param  pMedia Pointer to the Media instance to use
00202  */
00203 extern uint8_t MED_IsProtected(sMedia * pMedia)
00204 {
00205     return pMedia->protected;
00206 }
00207 
00208 /**
00209  *  \brief  Return current state of the Media.
00210  *  \param  pMedia Pointer to the Media instance to use
00211  */
00212 extern uint8_t MED_GetState(sMedia *pMedia)
00213 {
00214     return pMedia->state;
00215 }
00216 
00217 /**
00218  *  \brief  Return block size in bytes.
00219  *  \param  pMedia Pointer to the Media instance to use
00220  */
00221 extern uint32_t MED_GetBlockSize(sMedia * pMedia)
00222 {
00223     return pMedia->blockSize;
00224 }
00225 
00226 /**
00227  *  \brief  Return Media size in number of blocks.
00228  *  \param  pMedia Pointer to the Media instance to use
00229  */
00230 extern uint32_t MED_GetSize(sMedia * pMedia)
00231 {
00232     return pMedia->size;
00233 }
00234 
00235 /**
00236  *  \brief  Return mapped memory address for a block on media.
00237  *  \param  pMedia Pointer to the Media instance to use
00238  */
00239 extern uint32_t MED_GetMappedAddress(sMedia * pMedia,
00240                                     uint32_t dwBlk)
00241 {
00242     return ((pMedia->baseAddress + dwBlk) * pMedia->blockSize);
00243 }
00244 
00245 /**
00246  *  \brief  Handle interrupts on specified media
00247  *  \param  pMedia    List of media
00248  *  \param  bNumMedia Number of media in list
00249  *  \see    S_media
00250  */
00251 extern void MED_HandleAll( sMedia* pMedia, uint8_t bNumMedia )
00252 {
00253     uint32_t i ;
00254 
00255     for ( i = 0 ; i < bNumMedia ; i++ ) {
00256         MED_Handler( &(pMedia[i]) ) ;
00257     }
00258 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines