PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
simple_flash_example.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 #include "pic24_all.h"
30 #include <stdio.h>
31 
32 /** \file
33 A simple example of run time self programming - reads/writes a single 16-bit value to flash.
34 */
35 
36 #ifdef __PIC24F__
37 #warning For a PIC24F, the DATA_FLASH_PAGE is set to the second to last flash page instead of the last page,
38 #warning as the last flash page in the 24F family contains the configuration bits.
39 #endif
40 
41 
42 #if defined(__PIC24HJ64GP502__) || defined(__PIC24FJ64GA002__)
43 #define LAST_IMPLEMENTED_PMEM 0x00ABFF
44 #elif defined(__PIC24HJ32GP202__) || defined(__PIC24FJ32GA002__) || defined(__dsPIC33FJ32GP202__)
45 #define LAST_IMPLEMENTED_PMEM 0x0057FF
46 #elif defined(__dsPIC33FJ128GP802__)
47 #define LAST_IMPLEMENTED_PMEM 0x0157FF
48 #elif defined(__PIC24EP64GP202__) //PIC24E test
49 #define LAST_IMPLEMENTED_PMEM 0x00AFFF
50 #else
51 #error "Define LAST_IMPLEMENTED_PMEM for your processor!
52 #endif
53 
54 
55 //calculate starting address of a flash page to store data
56 //some PIC24H/dsPIC33 store configuration bits on last page, so skip that
57 #if (defined(__PIC24F__) || defined(__PIC24E__)|| defined(__dsPIC33E__))
58 #define DATA_FLASH_PAGE (((LAST_IMPLEMENTED_PMEM/FLASH_PAGESIZE)*FLASH_PAGESIZE)-FLASH_PAGESIZE) //2nd to last page of flash
59 #endif
60 #if (defined(__PIC24H__)|| defined(__dsPIC33F__))
61 #define DATA_FLASH_PAGE ((LAST_IMPLEMENTED_PMEM/FLASH_PAGESIZE)*FLASH_PAGESIZE) //last page of flash
62 #endif
63 
64 
65 
66 typedef struct _REC {
67  uint16_t u16_val; //single 16-bit value
68 } REC;
69 
70 #define NUM_ROWS (((sizeof(REC))/FLASH_ROWBYTES) + 1)
71 #define FLASH_DATA_SIZE (NUM_ROWS*FLASH_ROWBYTES)
72 
73 typedef union _UFDATA {
74  REC dat;
75  char fill[FLASH_DATA_SIZE]; //worst case allocates extra row, but ensures RAM data block is multiple of row size
76 } UFDATA;
77 
78 UFDATA fdata;
79 
80 void doCommit(UFDATA* p_ufdata) {
81  union32 u_memaddr;
82  u_memaddr.u32 = DATA_FLASH_PAGE;
83  doWritePageFlash(u_memaddr, (uint8_t *) p_ufdata, FLASH_DATA_SIZE);
84 }
85 
86 void doRead(UFDATA* p_ufdata) {
87  union32 u_memaddr;
88  u_memaddr.u32 = DATA_FLASH_PAGE;
89  doReadPageFlash(u_memaddr, (uint8_t *) p_ufdata, FLASH_DATA_SIZE);
90 }
91 
92 
93 uint8_t printMenu() {
94  printf("1 Read 16-bit value from flash.\n");
95  printf("2 Write 16-bit value to flash. \n");
96  printf(" Enter number (1-2): ");
97  return inCharEcho();
98 }
99 
100 void doMenu() {
101  uint8_t u8_c;
102  char data[32];
103  uint16_t u16_val;
104  u8_c = printMenu();
105  printf("\n");
106  switch(u8_c) {
107  case '1':
108  doRead(&fdata);
109  printf("The 16-bit value read from memory is: %u (%x hex)\n",fdata.dat.u16_val,fdata.dat.u16_val);
110  break;
111  case '2':
112  printf("Enter 16-bit value+\\n (0 to 65535, decimal): ");
113  inStringEcho(data,31);
114  sscanf(data,"%d", (int *) &u16_val);
115  fdata.dat.u16_val = u16_val;
116  doCommit(&fdata); //write the data
117  break;
118  }
119 }
120 
121 int main (void) {
122  configBasic(HELLO_MSG);
123  while(1) {
124  doMenu(1);
125  } //end while
126 }//end main