📄 example_saverestore_psvpag.c
字号:
/* Example illustrating how to Save and Restore the PSVPAG register */
/***********************************************************************
* In this example, the constant status_string is located in the *
* compiler-managed PSV section, while the constant gamma_factor is *
* located in a separate PSV section. You are required to place a *
* constant in a separate PSV section if the compiler-managed PSV *
* section is full or if you want to locate the constant at a specific *
* address. A PSV section has a capacity of 32 Kbytes. *
* *
* The compiler will initialize the PSV bit in the CORCON register and *
* the PSVPAG register only for the compiler-managed PSV section *
* (auto_psv) on startup (in crt0.s). To properly access gamma_factor, *
* you must manually manage the PSVPAG register. Namely, save the *
* PSVPAG register before accessing gamma_factor, set the PSVPAG to *
* access gamma_factor and restore the original PSVPAG after accessing *
* gamma_factor. To determine the PSVPAG of a constant stored in *
* program memory, one can use the __builtin_psvpage() helper function.*
* *
* When the PSVPAG is modified to access gamma_factor, be careful not *
* to access constants stored in the compiler-managed PSV section, *
* such as string constants used with printf(). Any attempts to access *
* constants stored in the compiler-managed PSV section with an *
* incorrect PSVPAG will fail. *
* *
* On devices with less than 16K instruction words, there is only one *
* PSVPAG and manual mangement of the PSVPAG register is not required. *
* *
* You can view the results of the printf statements by enabling the *
* MPLAB SIM tool then enabling the UART1 I/O with Output set to *
* Window. The printf results are then displayed in the output window. *
* *
* A defined heap size is required since we are using a standard I/O *
* function. *
* *
* *
* For additional information about dsPIC architecture and language *
* tools, refer to the following documents: *
* *
* MPLAB C30 Compiler User's Guide : DS51284 *
* dsPIC 30F MPLAB ASM30, MPLAB LINK30 and Utilities *
* User's Guide : DS51317 *
* Getting Started with dsPIC DSC Language Tools : DS51316 *
* dsPIC 30F Language Tools Quick Reference Card : DS51322 *
* dsPIC 30F 16-bit MCU Family Reference Manual : DS70046 *
* dsPIC 30F Programmer's Reference Manual : DS70030 *
* *
* Example file has been compiled with MPLAB C30 v1.30.00 *
* *
************************************************************************
* *
* Author: *
* Company: *
* Filename: Ex_SaveRestore_PSVPAG.c *
* Date: 02/01/2005 *
* File Version: 1.00 *
* Other Files Required: p30f6014.gld *
* Tools Used: MPLAB IDE -> 7.01.00 *
* Compiler -> 1.30.00 *
* Assembler -> 1.30.00 *
* Linker -> 1.30.00 *
* *
***********************************************************************/
/* Include the appropriate header (.h) file, depending on device used */
/* Example (for dsPIC30F5013): #include <p30f5013.h> */
/* Alternatively include the generic header file, p30fxxxx.h, as shown
below. */
#include <p30fxxxx.h>
#include "stdio.h"
const char __attribute__ ((space(auto_psv))) status_string[2][10] =
{"System OK", "Key Made"};
const int __attribute__ ((space(psv))) gamma_factor[3] = {13, 23, 7};
int main( void )
{
unsigned psv_shadow;
unsigned key, seed = 17231;
/* print the first status string */
printf ("%s\n", status_string[0]);
/* save the PSVPAG */
psv_shadow = PSVPAG;
/* set the PSVPAG for accessing gamma_factor[] */
PSVPAG = __builtin_psvpage (gamma_factor);
/* build the key from gamma_factor */
key = (seed + gamma_factor[0] + gamma_factor[1]) / gamma_factor[2];
/* restore the PSVPAG for the compiler-managed PSVPAG */
PSVPAG = psv_shadow;
/* print the second status message */
printf ("%s \n", status_string[1]);
while ( 1 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -