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 eefc_pgm EEFC programming example
00032  *
00033  * \section Purpose
00034  * This basic example shows how to use the Enhance Embedded Flash (EEFC)
00035  * peripheral available on the newest Atmel samv7 Microcontrollers.
00036  * It details steps required to
00037  * program the internal flash, and manage secure and lock bits.
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  * The samv7/e7 ROM code embeds small In Application Programming Procedure.
00045  * Since this function is executed from ROM, this allows Flash programming
00046  * (such as sector write) to be done by code running in Flash.\n
00047  *
00048  * \section Note
00049  * The IAP function entry point is retrieved by reading the NMI vector in ROM ().
00050  * This function takes two argument in parameter: bank index (0 or 1) and the
00051  * command to be sent to the EEFC.
00052  *    \code
00053  *    static uint32_t  (*IAP_PerformCommand)(uint32_t, uint32_t);
00054  *    IAP_PerformCommand = (uint32_t (*)(uint32_t, uint32_t)) *((uint32_t *)
00055  *    0x00100008);
00056  *    IAP_PerformCommand(0, (0x5A << 24) | (argument << 8) | command);
00057  *    \endcode
00058  * IAP function returns the value of the MC_FSR register.
00059  * The required steps are:
00060  * - Unlock a page.
00061  * - Program a page of the embedded flash with incremental values
00062  * (0x0, 0x1, 0x2, 0x3...) by using the IAP function.
00063  * - Check the flash is correctly programmed by reading all the values programmed.
00064  * - Lock the page.
00065  * - Set the security bit.
00066  *
00067  * The samv7/e7 features a security bit, based on a specific General Purpose NVM bit 0.
00068  * When the security is enabled, any access to the Flash, SRAM, Core Registers
00069  * and Internal Peripherals either through the ICE interface is forbidden.
00070  * This example will reproduce this scene.
00071  *
00072  * \section Usage
00073  *
00074  * -# Build the program and download it inside the board.
00075  * Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00076  * -# On the computer, open and configure a terminal application
00077  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00078  *   - 115200 baud rate
00079  *   - 8 bits of data
00080  *   - No parity
00081  *   - 1 stop bit
00082  *   - No flow control
00083  * -# Start the application.
00084  * -# In the terminal window, the following text should appear:
00085  *    \code
00086  *     -- EEFC Programming Example xxx --
00087  *     -- xxxxxx-xx
00088  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00089  *     -I- Unlocking last page
00090  *     -I- Writing last page with walking bit pattern
00091  *     -I- Checking page contents .................. ok
00092  *     -I- Locking last page
00093  *     -I- Try to program the locked page...
00094  *     -I- Please open Segger's JMem program
00095  *     -I- Read memory at address 0x0043FF00 to check contents
00096  *     -I- Press any key to continue...
00097  *     -I- Good job!
00098  *     -I- Now set the security bit
00099  *     -I- Press any key to continue to see what happened...
00100  *     -I- Setting GPNVM #0
00101  *     -I- All tests done
00102  *    \endcode
00103 *
00104 * \section References
00105 * - eefc_pgm/main.c
00106 * - efc.c
00107 * - efc.h
00108 * - flashd.c
00109 * - flashd.h
00110 */
00111 /**
00112  * \file
00113  *
00114  * This file contains all the specific code for the eefc_pgm example.
00115  *
00116  */
00117 /*----------------------------------------------------------------------------
00118  *        Headers
00119  *----------------------------------------------------------------------------*/
00120 
00121 #include "board.h"
00122 #include <stdio.h>
00123 #include <stdarg.h>
00124 #include <assert.h>
00125 
00126 /*----------------------------------------------------------------------------
00127  *         Global functions
00128  *----------------------------------------------------------------------------*/
00129 
00130 /**
00131  * \brief Application entry point for EEFC programming example.
00132  *
00133  * \return Unused (ANSI-C compatibility).
00134  */
00135 
00136 extern int main( void )
00137 {
00138     uint32_t dwCnt;
00139     uint8_t  ucError;
00140     uint32_t adwBuffer[IFLASH_PAGE_SIZE / 4];
00141     uint32_t dwLastPageAddress;
00142     volatile uint32_t *pdwLastPageData;
00143 
00144     /* Disable watchdog */
00145     WDT_Disable(WDT);
00146 
00147     /* Enable I and D cache */
00148     SCB_EnableICache();
00149     SCB_EnableDCache();
00150 
00151      /* Update internal flash Region to Full Access*/
00152     MPU_UpdateRegions(MPU_DEFAULT_IFLASH_REGION, IFLASH_START_ADDRESS, \
00153         MPU_AP_FULL_ACCESS |
00154         INNER_NORMAL_WB_NWA_TYPE( NON_SHAREABLE ) |
00155         MPU_CalMPURegionSize(IFLASH_END_ADDRESS - IFLASH_START_ADDRESS) |
00156         MPU_REGION_ENABLE);
00157 
00158     /* Set 6 WS for internal Flash writing (refer to errata) */
00159     EFC_SetWaitState(EFC, 6);
00160 
00161     /* Output example information */
00162     printf("\n\r\n\r\n\r");
00163     printf("EEFC Programming Example %s --\n\r", SOFTPACK_VERSION);
00164     printf("%s\n\r", BOARD_NAME);
00165     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00166 
00167     /* Initialize flash driver */
00168     FLASHD_Initialize(BOARD_MCK, 0);
00169 
00170     /* Performs tests on last page (to avoid overriding existing program).*/
00171     dwLastPageAddress = IFLASH_ADDR + IFLASH_SIZE - IFLASH_PAGE_SIZE;
00172     pdwLastPageData = (volatile uint32_t *)dwLastPageAddress;
00173 
00174     /* Unlock page */
00175     printf("-I- Unlocking last page\n\r");
00176     ucError = FLASHD_Unlock(dwLastPageAddress, dwLastPageAddress + IFLASH_PAGE_SIZE,
00177             0, 0);
00178     assert( !ucError );
00179 
00180     /* Write page with walking bit pattern (0x00000001, 0x00000002, ...) */
00181     printf("-I- Writing last page with walking bit pattern\n\r");
00182     for (dwCnt = 0; dwCnt < (IFLASH_PAGE_SIZE / 4); dwCnt++){
00183         adwBuffer[dwCnt] = 1 << (dwCnt % 32);
00184     }
00185     ucError = FLASHD_Write(dwLastPageAddress, adwBuffer, IFLASH_PAGE_SIZE);
00186     assert(!ucError);
00187 
00188     /* Check page contents */
00189     printf("-I- Checking page contents ");
00190     for (dwCnt = 0; dwCnt < (IFLASH_PAGE_SIZE / 4); dwCnt++) {
00191         printf(".");
00192         if (pdwLastPageData[dwCnt] != (1u << (dwCnt % 32))) {
00193             printf("\n\r-F- Expected 0x%08X at address 0x%08X, found 0x%08X\n\r",
00194                     (1u << (dwCnt % 32)), (unsigned int) &(pdwLastPageData[dwCnt]),
00195                     (unsigned)(pdwLastPageData[dwCnt]));
00196             while (1);
00197         }
00198     }
00199     printf(" OK \n\r");
00200 
00201     /* Lock page */
00202     printf("-I- Locking last page\n\r");
00203     ucError = FLASHD_Lock(dwLastPageAddress, dwLastPageAddress + IFLASH_PAGE_SIZE,
00204             0, 0 );
00205     assert(!ucError);
00206 
00207     /* Check that associated region is locked*/
00208     printf("-I- Try to program the locked page... \n\r");
00209     ucError = FLASHD_Write(dwLastPageAddress, adwBuffer, IFLASH_PAGE_SIZE);
00210     if (ucError)
00211         printf("-I- The page to be programmed belongs to a locked region.\n\r");
00212 
00213     printf("-I- Please open Segger's JMem program \n\r");
00214     printf("-I- Read memory at address 0x%08x to check contents\n\r",
00215             (unsigned int)dwLastPageAddress);
00216     printf("-I- Press any key to continue...\n\r");
00217     while ( !DBG_GetChar());
00218 
00219     printf("-I- Good job!\n\r");
00220     printf("-I- Now set the security bit \n\r");
00221     printf("-I- Press any key to continue to see what happened...\n\r");
00222     while (!DBG_GetChar());
00223 
00224     /* Set GPNVM bit 0 (security bit) */
00225 
00226     /* SAMS7 features a security bit based on the GPNVM bit 0. When security is
00227         enabled, any access to the Flash, SRAM, core registers and internal
00228         peripherals, either through the SW-DP/JTAG-DP interface or through the
00229         Fast Flash Programming Interface, is forbidden. */
00230 
00231     printf("-I- Setting GPNVM #%d\n\r", 0);
00232     ucError = FLASHD_SetGPNVM(0);
00233     assert(!ucError);
00234 
00235     printf("-I- All tests done\n\r");
00236 
00237     /* Update internal flash Region to previous configurations */
00238     MPU_UpdateRegions(MPU_DEFAULT_IFLASH_REGION, IFLASH_START_ADDRESS, \
00239         MPU_AP_READONLY |
00240         INNER_NORMAL_WB_NWA_TYPE( NON_SHAREABLE ) |
00241         MPU_CalMPURegionSize(IFLASH_END_ADDRESS - IFLASH_START_ADDRESS) |
00242         MPU_REGION_ENABLE);
00243     return 0;
00244 }
00245 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines