PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 Illustrates run time self programming of the flash memory
34 */
35 
36 #if (defined(__PIC24F__) || defined(__PIC24E__)|| defined(__dsPIC33E__))
37 #warning For all PIC24F and some PIC24E/dsPIC33E family members, 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 #if (defined(__PIC24F__) || defined(__PIC24E__)|| defined(__dsPIC33E__))
57 #define DATA_FLASH_PAGE (((LAST_IMPLEMENTED_PMEM/FLASH_PAGESIZE)*FLASH_PAGESIZE)-FLASH_PAGESIZE) //2nd to last page of flash
58 #endif
59 #if (defined(__PIC24H__)|| defined(__dsPIC33F__))
60 #define DATA_FLASH_PAGE ((LAST_IMPLEMENTED_PMEM/FLASH_PAGESIZE)*FLASH_PAGESIZE) //last page of flash
61 #endif
62 
63 
64 
65 #define RECORD_SIZE 15 //arbitrarily chosen
66 #define NUM_RECORDS 8 //arbitrarily chosen
67 #define FREE_STATUS 0xA5 //arbitrarily chosen, different from USED_STATUS
68 #define USED_STATUS 0x5A //arbitrarily chosen, different from FREE_STATUS
69 
70 typedef struct _REC { //one record
71  uint8_t status; //indicates if free or used
72  char data[RECORD_SIZE]; //holds the data
73 } REC;
74 
75 #define NUM_ROWS (((NUM_RECORDS*sizeof(REC))/FLASH_ROWBYTES) + 1)
76 #define FLASH_DATA_SIZE (NUM_ROWS*FLASH_ROWBYTES)
77 
78 typedef union _UFDATA {
79  REC records[NUM_RECORDS];
80  char fill[FLASH_DATA_SIZE]; //worst case allocates extra row, but ensures RAM data block is multiple of row size
81 } UFDATA;
82 
83 UFDATA fdata __attribute__ ((aligned(4)));
84 
85 void doFormat(UFDATA* p_ufdata) {
86  uint16_t u16_i;
87  for (u16_i = 0; u16_i < NUM_RECORDS; u16_i++) {
88  p_ufdata->records[u16_i].status = FREE_STATUS;
89  }
90 }
91 
92 void doCommit(UFDATA* p_ufdata) {
93  union32 u_memaddr;
94  u_memaddr.u32 = DATA_FLASH_PAGE;
95  doWritePageFlash(u_memaddr, (uint8_t *) p_ufdata, FLASH_DATA_SIZE);
96 }
97 
98 void doRead(UFDATA* p_ufdata) {
99  union32 u_memaddr;
100  u_memaddr.u32 = DATA_FLASH_PAGE;
101  doReadPageFlash(u_memaddr, (uint8_t *) p_ufdata, FLASH_DATA_SIZE);
102 }
103 
104 void doDelete(uint16_t u16_recnum) {
105  fdata.records[u16_recnum].status = FREE_STATUS;
106 }
107 
108 void doPrint(UFDATA* p_ufdata) {
109  uint16_t u16_i;
110  for (u16_i = 0; u16_i < NUM_RECORDS; u16_i++) {
111  printf("%d: %s, Data: %s\n",
112  u16_i, (p_ufdata->records[u16_i].status == FREE_STATUS ? "Free" : "Used"),
113  (p_ufdata->records[u16_i].status == FREE_STATUS ? "n/a" : p_ufdata->records[u16_i].data));
114  }
115 }
116 
117 void doInsert(UFDATA* p_ufdata, char* sz_1) {
118  uint16_t u16_i, u16_j;
119  for (u16_i = 0; u16_i < NUM_RECORDS; u16_i++) {
120  if (p_ufdata->records[u16_i].status == FREE_STATUS) {
121  u16_j = 0;
122  while (*sz_1) { //copy data
123  p_ufdata->records[u16_i].data[u16_j] = *sz_1;
124  sz_1++;
125  u16_j++;
126  } //end while
127  p_ufdata->records[u16_i].data[u16_j] = *sz_1; //write null
128  p_ufdata->records[u16_i].status = USED_STATUS; //mark as used
129  break; //exit if
130  }//end if
131  }//end for
132 }//end function
133 
134 uint8_t printMenu() {
135  printf("1 Format data\n");
136  printf("2 Enter one record\n");
137  printf("3 Delete a record\n");
138  printf("4 Commit data\n");
139  printf("5 Read and print all records\n");
140  printf(" Enter number (1-5): ");
141  return inCharEcho();
142 }
143 
144 void doMenu() {
145  uint8_t u8_c;
146  char data[RECORD_SIZE];
147  uint16_t u16_recnum;
148  u8_c = printMenu();
149  printf("\n");
150  switch(u8_c) {
151  case '1':
152  doFormat(&fdata);
153  break;
154  case '2':
155  printf("Enter string+\\n (14 chars max): ");
156  inStringEcho(data,RECORD_SIZE-1);
157  doInsert(&fdata, data);
158  break;
159  case '3':
160  printf("Enter record number+\\n (0 to 15, decimal): ");
161  inStringEcho(data,RECORD_SIZE-1);
162  sscanf(data,"%d", (int *) &u16_recnum);
163  doDelete(u16_recnum);
164  break;
165  case '4':
166  doCommit(&fdata);
167  break;
168  case '5':
169  doRead(&fdata);
170  doPrint(&fdata);
171  break;
172  }
173 }
174 
175 int main (void) {
176  configBasic(HELLO_MSG);
177  while(1) {
178  doMenu(1);
179  } //end while
180 }//end main