SAMV71 Xplained Ultra Software Package 1.4

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, 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 periph_protect Peripheral Protect Example
00032  *
00033  *  \section Purpose
00034  *
00035  *  The Peripheral Protect example demonstrates how to prevent a program from
00036  *  corrupting a PIO controller behaviour.
00037  *
00038  *  \section Requirements
00039  *
00040  *  This package can be used with SAM V71 Xplained Ultra board.
00041  *
00042  *  \section Description
00043  *
00044  *  The application shows the protective mechanism of the PIO controller.
00045  *  The example enables or disables write-protection of PIOB user interfaces.
00046  *  When the write-protection is enabled, any write attempt to the 
00047  *  write-protected registers will be detected and the write operation aborts.
00048  *  So the value of the register won't be modified. Besides, the Write Protect  
00049  *  Status Register will indicate the offset address of the register.
00050  *
00051  *  \section Usage
00052  *
00053  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. 
00054  *     Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00055  *  -# On the computer, open and configure a terminal application
00056  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00057  *    - 115200 baud rate
00058  *    - 8 bits of data
00059  *    - No parity
00060  *    - 1 stop bit
00061  *    - No flow control
00062  *  -# Start application.
00063  *  -# In the terminal window, the
00064  *     following text should appear (values depend on the board and chip used):
00065  *     \code
00066  *      -- Peripheral Protect Example xxx --
00067  *      -- SAMxxxxx-xx
00068  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00069  *     \endcode
00070  *  -# Press one of the keys listed in the menu to perform the corresponding 
00071  *     action.
00072  *
00073  *  \section References
00074  *  - periph_protect/main.c
00075  *  - pio.h
00076  *  - pio.c
00077  */
00078 
00079 /** \file
00080  *
00081  *  This file contains all the specific code for the periph_protect.
00082  */
00083 
00084 /*----------------------------------------------------------------------------
00085  *        Headers
00086  *----------------------------------------------------------------------------*/
00087 
00088 #include "board.h"
00089  
00090 #include <stdbool.h>
00091 #include <stdio.h>
00092 
00093 /*----------------------------------------------------------------------------
00094  *        Local variables
00095  *----------------------------------------------------------------------------*/
00096 
00097 /** pin instance */
00098 Pin pinwp = PIN_LED_1;
00099 
00100 /** Offset table */
00101 uint32_t offset[] = 
00102 { 
00103     0x0000, 0x0004, 0x0010, 0x0014, 0x0020, 0x0024, 0x0050, 0x0054, 
00104     0x0060, 0x0064, 0x0070, 0x0074, 0x00A0, 0x00A4, 0x0090, 0x0094
00105 };
00106 
00107 /*----------------------------------------------------------------------------
00108  *        Local functions
00109  *----------------------------------------------------------------------------*/
00110 
00111 /**
00112  *  \brief Get the PIO controller address 
00113  *
00114  *  Return the address of the PIO controller the Pin structure contains
00115  */
00116 static uint32_t  *_GetPioFromPin(const Pin *pin)
00117 {
00118     Pio *pio = pin->pio;
00119     return (uint32_t *)pio;
00120 }
00121 
00122 /**
00123  *  \brief Display main menu
00124  *
00125  *  Display the menu to show how to use this example
00126  */
00127 static void _DisplayMenu(void)
00128 {
00129     printf("\n\r\n\r");
00130     printf("Enter 'l' to enable Write Protect and ");
00131     printf("enter 'u' to disable Write Protect.\r\n");
00132     printf("\n\r");
00133     printf("Select the register to be written by a value(0x12345678).\n\r");
00134     printf("  0 : PIO Enable Register\t(0x0000)\n\r");
00135     printf("  1 : PIO Disable Register\t(0x0004)\n\r");
00136     printf("  2 : PIO Output Enable Register\t(0x0010)\n\r");
00137     printf("  3 : PIO Output Disable Register\t(0x0014)\n\r");
00138     printf("  4 : PIO Input Filter Enable Register\t(0x0020)\n\r");
00139     printf("  5 : PIO Input Filter Disable Register\t(0x0024)\n\r");
00140     printf("  6 : PIO Multi-driver Enable Register\t(0x0050)\n\r");
00141     printf("  7 : PIO Multi-driver Disable Register\t(0x0054)\n\r");
00142     printf("  8 : PIO Pull Up Disable Register\t(0x0060)\n\r");
00143     printf("  9 : PIO Pull Up Enable Register\t(0x0064)\n\r");
00144     printf("  a : PIO Peripheral ABCD Select Register 1\t(0x0070)\n\r");
00145     printf("  b : PIO Peripheral ABCD Select Register 2\t(0x0074)\n\r");
00146     printf("  c : PIO Output Write Enable Register\t(0x00A0)\n\r");
00147     printf("  d : PIO Output Write Disable Register\t(0x00A4)\n\r");
00148     printf("  e : PIO Pad Pull Down Disable Register\t(0x0090)\n\r");
00149     printf("  f : PIO Pad Pull Down Enable Register\t(0x0094)\n\r");
00150     printf("\n\r\n\r");
00151 }
00152 
00153 /*----------------------------------------------------------------------------
00154  *        Global functions
00155  *----------------------------------------------------------------------------*/
00156 
00157 /**
00158  *  \brief periph-protect Application entry point.
00159  *
00160  *  \return Unused (ANSI-C compatibility).
00161  */
00162 extern int main(void)
00163 {
00164     uint8_t cmd;
00165     uint32_t wpsr;
00166     uint32_t *pReg;
00167     const uint32_t dummy = 0x12345678;
00168 
00169     /* Disable watchdog */
00170     WDT_Disable(WDT);
00171     /* Enable I and D cache */
00172     SCB_EnableICache();
00173     SCB_EnableDCache();
00174 
00175     /* Output example information */
00176     printf("-- Peripheral Protect Example %s --\n\r", SOFTPACK_VERSION);
00177     printf("-- %s\n\r", BOARD_NAME);
00178     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00179     _DisplayMenu();
00180 
00181     /* Enable PIO controller peripheral clock */
00182     PMC_EnablePeripheral(pinwp.id);
00183     /* Get the address of PIO controller to be write-protected */
00184     pReg = _GetPioFromPin(&pinwp);
00185 
00186     while (1) {
00187 
00188         cmd = DBG_GetChar();
00189 
00190         switch (cmd) {
00191         case 'm':
00192             _DisplayMenu();
00193             break;
00194 
00195         case 'l':
00196             PIO_EnableWriteProtect(&pinwp);
00197             printf("The Write Protect is enabled.\r\n");
00198             break;
00199 
00200         case 'u':
00201             PIO_DisableWriteProtect(&pinwp);
00202             printf("The Write Protect is disabled.\r\n");
00203             break;
00204 
00205         case '0':
00206         case '1':
00207         case '2':
00208         case '3':
00209         case '4':
00210         case '5':
00211         case '6':
00212         case '7':
00213         case '8':
00214         case '9':
00215             *(pReg + offset[cmd - '0'] / 4) = dummy;
00216             break;
00217 
00218         case 'a':
00219         case 'b':
00220         case 'c':
00221         case 'd':
00222         case 'e':
00223         case 'f':
00224             *(pReg + offset[cmd - 'a' + 10] / 4) = dummy;
00225             break;
00226 
00227         default:
00228             cmd = 'x';
00229             break;
00230         }
00231 
00232         if ((cmd != 'x') && (cmd != 'm') && (cmd != 'l') && (cmd != 'u')) {
00233             /* A write access has been attempted */
00234             wpsr = PIO_GetWriteProtectViolationInfo(&pinwp);
00235             if ((wpsr & PIO_WPMR_WPEN_EN) == PIO_WPMR_WPEN_EN) {
00236                 /* Write protect violation is detected */
00237                 printf("Write protect violation is detected!\r\n");
00238                 printf("The offset of write-protected register is 0x%04x.\r\n", 
00239                     (unsigned int)((wpsr & 0x00FFFF00) >> 8));
00240             } else {
00241                 /* No write protect violation*/
00242                 printf("No write protect violation is detected.\r\n");
00243             }
00244         }
00245     }
00246 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines