SAMV71 Xplained Ultra Software Package 1.5

Media.c

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 
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  *  \brief  Unlocks all the regions in the given address range
00111  *  \param  media    Pointer to a Media instance
00112  *  \param start  Start address of unlock range.
00113  *  \param end  End address of unlock range.
00114  *  \param pActualStart  Start address of the actual unlock range (optional).
00115  *  \param pActualEnd  End address of the actual unlock range (optional).
00116  *  \return 0 if successful; otherwise returns an error code.
00117  */
00118 extern uint8_t MED_Unlock(sMedia *pMedia,
00119                            uint32_t start, uint32_t end,
00120                            uint32_t *pActualStart, uint32_t *pActualEnd)
00121 {
00122     if (pMedia->unlock)
00123         return pMedia->unlock(pMedia, start, end, pActualStart, pActualEnd);
00124     else
00125         return MED_STATUS_SUCCESS;
00126 }
00127 
00128 /**
00129  *  \brief
00130  *  \param  media Pointer to the Media instance to use
00131  */
00132 extern uint8_t MED_Flush(sMedia *pMedia)
00133 {
00134     if (pMedia->flush)
00135         return pMedia->flush(pMedia);
00136     else
00137         return MED_STATUS_SUCCESS;
00138 }
00139 
00140 /**
00141  *  \brief  Invokes the interrupt handler of the specified media
00142  *  \param  media Pointer to the Media instance to use
00143  */
00144 extern void MED_Handler(sMedia *pMedia)
00145 {
00146     if (pMedia->handler)
00147         pMedia->handler(pMedia);
00148 }
00149 
00150 /**
00151  *  \brief  Reset the media interface to un-configured state.
00152  *  \param  media Pointer to the Media instance to use
00153  */
00154 extern void MED_DeInit(sMedia *pMedia)
00155 {
00156     pMedia->state = MED_STATE_NOT_READY;
00157 }
00158 
00159 /**
00160  *  \brief  Check if the Media instance is ready to use.
00161  *  \param  media Pointer to the Media instance to use
00162  */
00163 extern uint8_t MED_IsInitialized(sMedia *pMedia)
00164 {
00165     return (pMedia->state != MED_STATE_NOT_READY);
00166 }
00167 
00168 /**
00169  *  \brief  Check if the Media instance is busy in transfer.
00170  *  \param  pMedia Pointer to the Media instance to use
00171  */
00172 extern uint8_t MED_IsBusy(sMedia *pMedia)
00173 {
00174     return (pMedia->state == MED_STATE_BUSY);
00175 }
00176 
00177 /**
00178  *  \brief  Check if the Media supports mapped reading.
00179  *  \param  pMedia Pointer to the Media instance to use
00180  */
00181 extern uint8_t MED_IsMappedRDSupported(sMedia *pMedia)
00182 {
00183     return pMedia->mappedRD;
00184 }
00185 
00186 /**
00187  *  \brief  Check if the Media supports mapped writing.
00188  *  \param  pMedia Pointer to the Media instance to use
00189  */
00190 extern uint8_t MED_IsMappedWRSupported(sMedia *pMedia)
00191 {
00192     return pMedia->mappedRD;
00193 }
00194 
00195 /**
00196  *  \brief  Check if the Media is write protected.
00197  *  \param  pMedia Pointer to the Media instance to use
00198  */
00199 extern uint8_t MED_IsProtected(sMedia *pMedia)
00200 {
00201     return pMedia->protected;
00202 }
00203 
00204 /**
00205  *  \brief  Return current state of the Media.
00206  *  \param  pMedia Pointer to the Media instance to use
00207  */
00208 extern uint8_t MED_GetState(sMedia *pMedia)
00209 {
00210     return pMedia->state;
00211 }
00212 
00213 /**
00214  *  \brief  Return block size in bytes.
00215  *  \param  pMedia Pointer to the Media instance to use
00216  */
00217 extern uint32_t MED_GetBlockSize(sMedia *pMedia)
00218 {
00219     return pMedia->blockSize;
00220 }
00221 
00222 /**
00223  *  \brief  Return Media size in number of blocks.
00224  *  \param  pMedia Pointer to the Media instance to use
00225  */
00226 extern uint32_t MED_GetSize(sMedia *pMedia)
00227 {
00228     return pMedia->size;
00229 }
00230 
00231 /**
00232  *  \brief  Return mapped memory address for a block on media.
00233  *  \param  pMedia Pointer to the Media instance to use
00234  */
00235 extern uint32_t MED_GetMappedAddress(sMedia *pMedia,
00236                                      uint32_t dwBlk)
00237 {
00238     return ((pMedia->baseAddress + dwBlk) * pMedia->blockSize);
00239 }
00240 
00241 /**
00242  *  \brief  Handle interrupts on specified media
00243  *  \param  pMedia    List of media
00244  *  \param  bNumMedia Number of media in list
00245  *  \see    S_media
00246  */
00247 extern void MED_HandleAll(sMedia *pMedia, uint8_t bNumMedia)
00248 {
00249     uint32_t i;
00250 
00251     for (i = 0; i < bNumMedia; i++)
00252         MED_Handler(&(pMedia[i]));
00253 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines