SAMV71 Xplained Ultra Software Package 1.5

main.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 /**
00031  *  \page tcm TCM (Tightly Coupled Memory) Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  The tcm example will help new users get familiar with Atmel's
00036  *  SAMV7/E7 family of Microcontroller's tcm memory and there are benefits to
00037  *  run code with low latency.
00038  *
00039  *  \section Requirements
00040  *
00041  *  This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board.
00042  *
00043  *  \section Description
00044  *
00045  *  The demonstration program compares the performance between normal 'memcpy'
00046  * and 'TCM memcpy'.
00047  *
00048  *  \section Usage
00049  *
00050  *  -# Build the program and download it inside the board.
00051  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00052  *  -# On the computer, open and configure a terminal application
00053  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00054  *    - 115200 baud rates
00055  *    - 8 bits of data
00056  *    - No parity
00057  *    - 1 stop bit
00058  *    - No flow control
00059  *  -# Start the application.
00060  *  -# In the terminal window, the following text should appear
00061  *  (values depend on the board and chip used):
00062  *     \code
00063  *      -- TCM memory Example xxx --
00064  *      -- xxxxxx-xx
00065  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00066  *     \endcode
00067  *
00068  *  \section References
00069  *  - tcm/main.c
00070  *  - board.h
00071  */
00072 
00073 /** \file
00074  *
00075  *  This file contains all the specific code for the TCM example.
00076  *
00077  */
00078 
00079 /*----------------------------------------------------------------------------
00080  *        Headers
00081  *----------------------------------------------------------------------------*/
00082 
00083 #include "board.h"
00084 
00085 #include <stdbool.h>
00086 #include <stdio.h>
00087 #include <stdlib.h>
00088 #include <string.h>
00089 #include "..\..\..\..\utils\utility.h"
00090 /*----------------------------------------------------------------------------
00091  *        Global definitions
00092  *----------------------------------------------------------------------------*/
00093 extern void TCM_StackInit(void);
00094 
00095 /*----------------------------------------------------------------------------
00096  *        Local definitions
00097  *----------------------------------------------------------------------------*/
00098 #define BUFF_SIZE       0x1000
00099 
00100 /*----------------------------------------------------------------------------
00101  *        Local variables
00102  *----------------------------------------------------------------------------*/
00103 
00104 #if defined ( __ICCARM__ ) /* IAR Ewarm */
00105 #pragma location = ".data_TCM"
00106 #elif defined (  __GNUC__  ) || defined (__CC_ARM)  /* GCC || MDK */
00107 __attribute__((__section__(".data_TCM")))
00108 #endif
00109 uint8_t TCM_DestBuff[3*BUFF_SIZE];
00110 
00111 #if defined ( __ICCARM__ ) /* IAR Ewarm */
00112 #pragma location = ".data_TCM"
00113 #elif defined (  __GNUC__  ) || defined (__CC_ARM)  /* GCC || MDK */
00114 __attribute__((__section__(".data_TCM")))
00115 #endif
00116 uint8_t TCM_SrcBuff[BUFF_SIZE];
00117 
00118 const char *pBuffer = "The ARM architecture is defined in a hierarchical manner,\
00119 where the features are described in Chapter A2\
00120 Application Level Programmers?Model at the application level,\
00121 with underlying system support. What features are available and how they are \
00122 supported is defined in the architecture profiles, making the system \
00123 level support profile specific. Deprecated features can be found in an \
00124 appendix to this manual. See page. As stated in Privileged execution on page \
00125 A2-13, programs can execute in a privileged or unprivileged manner. System \
00126 level support requires privileged access, allowing it the access permissions \
00127 to configure and control the resources. This is typically supported by an \
00128 operating system, which provides system services to the applications, either \
00129 transparently, or through application initiated service calls. The operating \
00130 system is also responsible for servicing interrupts and other system events, \
00131 making exceptions a key component of the system level programmers?model.";
00132 
00133 uint8_t DestBuff[3*BUFF_SIZE], SrcBuff[BUFF_SIZE];
00134 
00135 static uint32_t CycleCounter;
00136 
00137 /*----------------------------------------------------------------------------
00138  *        Local functions
00139  *----------------------------------------------------------------------------*/
00140 
00141 #if defined ( __ICCARM__ ) /* IAR Ewarm */
00142 #pragma default_function_attributes = @ ".code_TCM"
00143 #elif defined (  __GNUC__  ) || defined (__CC_ARM)  /* GCC || MDK */
00144 __attribute__((__section__(".code_TCM")))
00145 #endif
00146 static uint32_t TCM_memcpy(uint8_t *pDest, uint8_t *pSrc, uint16_t len)
00147 {
00148     // clean destination and source buffer
00149     memset(TCM_SrcBuff, 0, len);
00150     __DSB();
00151     __ISB();
00152     memset(TCM_DestBuff, 0, len);
00153     __DSB();
00154     __ISB();
00155 
00156     // copy buffer to TCM source buffer
00157     memcpy(pSrc, pBuffer,len);
00158     __DSB();
00159     __ISB();
00160 
00161     // Disable and reset DWT cycle counter
00162     RESET_CYCLE_COUNTER();
00163     // Copy from DTCM source buffer to DTCM destination buffer
00164     {
00165         uint32_t i;
00166         uint8_t * pTmpD = pDest;
00167         uint8_t * pTmpS;
00168 
00169         for (i = 0, pTmpS = pSrc; i < len; i++)
00170             *pTmpD++ = *pTmpS++;
00171         for (i = 0, pTmpS = pSrc; i < len; i++)
00172             *pTmpD++ = *pTmpS++;
00173         for (i = 0, pTmpS = pSrc; i < len; i++)
00174             *pTmpD++ = *pTmpS++;
00175     }
00176     GET_CYCLE_COUNTER(CycleCounter);
00177     return CycleCounter;
00178 }
00179 
00180 
00181 static uint32_t Normal_memcpy(uint8_t *pDest, uint8_t *pSrc, uint16_t len)
00182 {
00183     memset(SrcBuff, 0, len);
00184     __DSB();
00185     __ISB();
00186     memset(DestBuff, 0, len);
00187     __DSB();
00188     __ISB();
00189     memcpy(SrcBuff, pBuffer,len);
00190     __DSB();
00191     __ISB();
00192 
00193     // Disable and reset DWT cycle counter
00194     RESET_CYCLE_COUNTER();
00195     // Copy from DTCM source buffer to DTCM destination buffer
00196     {
00197         uint32_t i;
00198         uint8_t * pTmpD = pDest;
00199         uint8_t * pTmpS;
00200         for (i = 0, pTmpS = pSrc; i < len; i++)
00201             *pTmpD++ = *pTmpS++;
00202         for (i = 0, pTmpS = pSrc; i < len; i++)
00203             *pTmpD++ = *pTmpS++;
00204         for (i = 0, pTmpS = pSrc; i < len; i++)
00205             *pTmpD++ = *pTmpS++;
00206     }
00207     GET_CYCLE_COUNTER(CycleCounter);
00208     return CycleCounter;
00209 }
00210 
00211 static uint32_t Recursive(uint32_t n)
00212 {
00213     volatile uint32_t tmp = n;
00214 
00215     if (0 == tmp)
00216         return 0;
00217     else
00218         return n + Recursive(tmp - 1);
00219 }
00220 
00221 static uint32_t Stack_Test(uint32_t n)
00222 {
00223     uint32_t sum;
00224     RESET_CYCLE_COUNTER();
00225     sum = Recursive(n);
00226     GET_CYCLE_COUNTER(CycleCounter);
00227     printf("\r\n Sum of 1 to %u is %u, ", (unsigned)n, (unsigned)sum);
00228     return CycleCounter;
00229 }
00230 
00231 /*----------------------------------------------------------------------------
00232  *        Exported functions
00233  *----------------------------------------------------------------------------*/
00234 /**
00235  *  \brief Application entry point for TCM example.
00236  *
00237  *  \return Unused (ANSI-C compatibility).
00238  */
00239 extern int main( void )
00240 {
00241     uint32_t CycleCounterOffset = 0;
00242     uint32_t Cycles=0;
00243 
00244     /* Disable watchdog */
00245     WDT_Disable(WDT);
00246 
00247     /* Output example information */
00248     printf("\n\r-- TCM memory Example %s --\n\r", SOFTPACK_VERSION);
00249     printf("-- %s\n\r", BOARD_NAME);
00250     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00251 
00252     RESET_CYCLE_COUNTER();
00253     __NOP();
00254     __NOP();
00255     __NOP();
00256     __NOP();
00257     __NOP();
00258     __NOP();
00259     __NOP();
00260     __NOP();
00261     __NOP();
00262     __NOP();
00263 
00264     CycleCounterOffset = DWT->CYCCNT;
00265     __DMB();
00266     CycleCounterOffset -=10;
00267 
00268     printf("\n\r");
00269     TRACE_INFO(" ------ ICache & DCache Disabled ------\n\r");
00270     TRACE_INFO(" ------ Stack In SRAM ------\n\r");
00271 
00272     /* stack test: in SRAM vs in DTCM */
00273     Cycles = Stack_Test(100);
00274     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00275 
00276     Cycles = Stack_Test(100);
00277     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00278 
00279     printf("\n\r");
00280     TRACE_INFO(" ------ Stack In DTCM ------\n\r");
00281     TCM_StackInit();
00282 
00283     Cycles = Stack_Test(100);
00284     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00285 
00286     Cycles = Stack_Test(100);
00287     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00288 
00289     /* memory copy test */
00290     Cycles = Normal_memcpy(DestBuff, SrcBuff, BUFF_SIZE);
00291     printf("\n\r Number of Inst Cycles passed without TCM is %u  \n\r",
00292             (unsigned)(Cycles - CycleCounterOffset));
00293 
00294     Cycles = TCM_memcpy(TCM_DestBuff, TCM_SrcBuff, BUFF_SIZE);
00295     printf("\n\r Number of Inst Cycles passed with TCM is %u \n\r",
00296             (unsigned)(Cycles - CycleCounterOffset));
00297 
00298     SCB_EnableICache();
00299     SCB_EnableDCache();
00300 
00301     printf("\n\r");
00302     TRACE_INFO(" ------ ICache & DCache Enabled ------\n\r");
00303 
00304     Cycles = Normal_memcpy(DestBuff, SrcBuff, BUFF_SIZE);
00305     printf("\n\r Number of Inst Cycles passed without TCM is %u  \n\r",
00306             (unsigned)(Cycles - CycleCounterOffset));
00307 
00308     Cycles = TCM_memcpy(TCM_DestBuff, TCM_SrcBuff, BUFF_SIZE);
00309     printf("\n\r Number of Inst Cycles passed with TCM is %u  \n\r",
00310             (unsigned)(Cycles - CycleCounterOffset));
00311 
00312     while (1);
00313 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines