SAMV71 Xplained Ultra Software Package 1.3

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2011, 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 /**
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 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 SAM V71 Xplained Ultra 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 SAM V71 Xplained Ultra board. 
00051  *     Please refer to the Getting Started with SAM V71 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         for(i=0, pTmpS = pSrc; i<len; i++) {
00169             *pTmpD++ = *pTmpS++;
00170         }
00171         for(i=0, pTmpS = pSrc; i<len; i++) {
00172             *pTmpD++ = *pTmpS++;
00173         }
00174         for(i=0, pTmpS = pSrc; i<len; i++) {
00175             *pTmpD++ = *pTmpS++;
00176         }
00177     }
00178     GET_CYCLE_COUNTER(CycleCounter);
00179     return CycleCounter ;
00180 }
00181 
00182 
00183 static uint32_t Normal_memcpy(uint8_t *pDest, uint8_t *pSrc, uint16_t len)
00184 {
00185     memset(SrcBuff, 0, len);
00186     __DSB();
00187     __ISB();
00188     memset(DestBuff, 0, len);
00189     __DSB();
00190     __ISB();
00191     memcpy(SrcBuff, pBuffer,len); 
00192     __DSB();
00193     __ISB();
00194 
00195     // Disable and reset DWT cycle counter
00196     RESET_CYCLE_COUNTER();
00197     // Copy from DTCM source buffer to DTCM destination buffer
00198     {
00199         uint32_t i;
00200         uint8_t * pTmpD = pDest;
00201         uint8_t * pTmpS;
00202         for(i=0, pTmpS = pSrc; i<len; i++) {
00203             *pTmpD++ = *pTmpS++;
00204         }
00205         for(i=0, pTmpS = pSrc; i<len; i++) {
00206             *pTmpD++ = *pTmpS++;
00207         }
00208         for(i=0, pTmpS = pSrc; i<len; i++) {
00209             *pTmpD++ = *pTmpS++;
00210         }
00211     }
00212     GET_CYCLE_COUNTER(CycleCounter);
00213     return CycleCounter ;
00214 }
00215 
00216 static uint32_t Recursive(uint32_t n)
00217 {
00218     volatile uint32_t tmp = n;
00219     if(0 == tmp)
00220         return 0;
00221     else
00222         return n + Recursive(tmp - 1);
00223 }
00224 
00225 static uint32_t Stack_Test(uint32_t n)
00226 {
00227     uint32_t sum;
00228     RESET_CYCLE_COUNTER();
00229     sum = Recursive(n);
00230     GET_CYCLE_COUNTER(CycleCounter);
00231     printf("\r\n Sum of 1 to %u is %u, ", (unsigned)n, (unsigned)sum);
00232     return CycleCounter;
00233 }
00234 
00235 /*----------------------------------------------------------------------------
00236  *        Exported functions
00237  *----------------------------------------------------------------------------*/
00238 /**
00239  *  \brief Application entry point for TCM example.
00240  *
00241  *  \return Unused (ANSI-C compatibility).
00242  */
00243 extern int main( void )
00244 {
00245     uint32_t CycleCounterOffset = 0;
00246     uint32_t Cycles=0;
00247 
00248     /* Disable watchdog */
00249     WDT_Disable( WDT ) ;
00250    
00251     /* Output example information */
00252     printf( "\n\r-- TCM memory Example %s --\n\r", SOFTPACK_VERSION ) ;
00253     printf( "-- %s\n\r", BOARD_NAME ) ;
00254     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00255 
00256     RESET_CYCLE_COUNTER();
00257     __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
00258     CycleCounterOffset = DWT->CYCCNT;
00259     __DMB();
00260     CycleCounterOffset -=10;
00261 
00262     printf("\n\r");
00263     TRACE_INFO(" ------ ICache & DCache Disabled ------\n\r");
00264     TRACE_INFO(" ------ Stack In SRAM ------\n\r");
00265 
00266     /* stack test: in SRAM vs in DTCM */
00267     Cycles = Stack_Test(100);
00268     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00269 
00270     Cycles = Stack_Test(100);
00271     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00272 
00273     printf("\n\r");
00274     TRACE_INFO(" ------ Stack In DTCM ------\n\r");
00275     TCM_StackInit();
00276 
00277     Cycles = Stack_Test(100);
00278     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00279 
00280     Cycles = Stack_Test(100);
00281     printf("Inst Cycles passed is %u\r\n", (unsigned)(CycleCounter - CycleCounterOffset));
00282 
00283     /* memory copy test */
00284     Cycles = Normal_memcpy(DestBuff, SrcBuff, BUFF_SIZE);
00285     printf("\n\r Number of Inst Cycles passed without TCM is %u  \n\r", 
00286             (unsigned)(Cycles - CycleCounterOffset)  );
00287 
00288     Cycles = TCM_memcpy(TCM_DestBuff, TCM_SrcBuff, BUFF_SIZE);
00289     printf("\n\r Number of Inst Cycles passed with TCM is %u \n\r", 
00290             (unsigned)(Cycles - CycleCounterOffset)  );
00291 
00292     SCB_EnableICache();
00293     SCB_EnableDCache();
00294 
00295     printf("\n\r");
00296     TRACE_INFO(" ------ ICache & DCache Enabled ------\n\r");
00297 
00298     Cycles = Normal_memcpy(DestBuff, SrcBuff, BUFF_SIZE);
00299     printf("\n\r Number of Inst Cycles passed without TCM is %u  \n\r", 
00300             (unsigned)(Cycles - CycleCounterOffset));
00301 
00302     Cycles = TCM_memcpy(TCM_DestBuff, TCM_SrcBuff, BUFF_SIZE);
00303     printf("\n\r Number of Inst Cycles passed with TCM is %u  \n\r", 
00304             (unsigned)(Cycles - CycleCounterOffset));
00305 
00306     while ( 1 );
00307 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines