00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "pic24_all.h"
00030 #include <stdio.h>
00031
00036 #ifdef __PIC24F__
00037 #warning For a PIC24F, the DATA_FLASH_PAGE is set to the second to last flash page instead of the last page,
00038 #warning as the last flash page in the 24F family contains the configuration bits.
00039 #endif
00040
00041
00042 #if defined(__PIC24HJ64GP502__) || defined(__PIC24FJ64GA002__)
00043 #define LAST_IMPLEMENTED_PMEM 0x00ABFF
00044 #elif defined(__PIC24HJ32GP202__) || defined(__PIC24FJ32GA002__)
00045 #define LAST_IMPLEMENTED_PMEM 0x0057FF
00046 #else
00047 #error "Define LAST_IMPLEMENTED_PMEM for your processor!
00048 #endif
00049
00050
00051
00052 #ifdef __PIC24F__
00053 #define DATA_FLASH_PAGE (((LAST_IMPLEMENTED_PMEM/FLASH_PAGESIZE)*FLASH_PAGESIZE)-FLASH_PAGESIZE) //2nd to last page of flash
00054 #endif
00055 #ifdef __PIC24H__
00056 #define DATA_FLASH_PAGE ((LAST_IMPLEMENTED_PMEM/FLASH_PAGESIZE)*FLASH_PAGESIZE) //last page of flash
00057 #endif
00058
00059
00060
00061 #define RECORD_SIZE 15 //arbitrarily chosen
00062 #define NUM_RECORDS 8 //arbitrarily chosen
00063 #define FREE_STATUS 0xA5 //arbitrarily chosen, different from USED_STATUS
00064 #define USED_STATUS 0x5A //arbitrarily chosen, different from FREE_STATUS
00065
00066 typedef struct _REC {
00067 uint8 status;
00068 char data[RECORD_SIZE];
00069 }REC;
00070
00071 #define NUM_ROWS (((NUM_RECORDS*sizeof(REC))/FLASH_ROWBYTES) + 1)
00072 #define FLASH_DATA_SIZE (NUM_ROWS*FLASH_ROWBYTES)
00073
00074 typedef union _UFDATA{
00075 REC records[NUM_RECORDS];
00076 char fill[FLASH_DATA_SIZE];
00077 }UFDATA;
00078
00079 UFDATA fdata;
00080
00081 void doFormat(UFDATA* p_ufdata) {
00082 uint16 u16_i;
00083 for (u16_i = 0; u16_i < NUM_RECORDS; u16_i++) {
00084 p_ufdata->records[u16_i].status = FREE_STATUS;
00085 }
00086 }
00087
00088 void doCommit(UFDATA* p_ufdata) {
00089 union32 u_memaddr;
00090 u_memaddr.u32 = DATA_FLASH_PAGE;
00091 doWritePageFlash(u_memaddr, (uint8 *) p_ufdata, FLASH_DATA_SIZE);
00092 }
00093
00094 void doRead(UFDATA* p_ufdata) {
00095 union32 u_memaddr;
00096 u_memaddr.u32 = DATA_FLASH_PAGE;
00097 doReadPageFlash(u_memaddr, (uint8 *) p_ufdata, FLASH_DATA_SIZE);
00098 }
00099
00100 void doDelete(uint16 u16_recnum) {
00101 fdata.records[u16_recnum].status = FREE_STATUS;
00102 }
00103
00104 void doPrint(UFDATA* p_ufdata){
00105 uint16 u16_i;
00106 for (u16_i = 0; u16_i < NUM_RECORDS; u16_i++) {
00107 printf("%d: %s, Data: %s\n",
00108 u16_i, (p_ufdata->records[u16_i].status == FREE_STATUS ? "Free" : "Used"),
00109 (p_ufdata->records[u16_i].status == FREE_STATUS ? "n/a" : p_ufdata->records[u16_i].data));
00110 }
00111 }
00112
00113 void doInsert(UFDATA* p_ufdata, char* sz_1){
00114 uint16 u16_i, u16_j;
00115 for (u16_i = 0; u16_i < NUM_RECORDS; u16_i++) {
00116 if (p_ufdata->records[u16_i].status == FREE_STATUS) {
00117 u16_j = 0;
00118 while (*sz_1) {
00119 p_ufdata->records[u16_i].data[u16_j] = *sz_1;
00120 sz_1++; u16_j++;
00121 }
00122 p_ufdata->records[u16_i].data[u16_j] = *sz_1;
00123 p_ufdata->records[u16_i].status = USED_STATUS;
00124 break;
00125 }
00126 }
00127 }
00128
00129 uint8 printMenu() {
00130 printf("1 Format data\n");
00131 printf("2 Enter one record\n");
00132 printf("3 Delete a record\n");
00133 printf("4 Commit data\n");
00134 printf("5 Read and print all records\n");
00135 printf(" Enter number (1-5): ");
00136 return inCharEcho();
00137 }
00138
00139 void doMenu(){
00140 uint8 u8_c;
00141 char data[RECORD_SIZE];
00142 uint16 u16_recnum;
00143 u8_c = printMenu();
00144 printf("\n");
00145 switch(u8_c) {
00146 case '1': doFormat(&fdata); break;
00147 case '2': printf("Enter string+\\n (14 chars max): ");
00148 inStringEcho(data,RECORD_SIZE-1);
00149 doInsert(&fdata, data);
00150 break;
00151 case '3': printf("Enter record number+\\n (0 to 15, decimal): ");
00152 inStringEcho(data,RECORD_SIZE-1);
00153 sscanf(data,"%d", (int *) &u16_recnum);
00154 doDelete(u16_recnum);
00155 break;
00156 case '4': doCommit(&fdata); break;
00157 case '5': doRead(&fdata);
00158 doPrint(&fdata);
00159 break;
00160 }
00161 }
00162
00163 int main (void) {
00164 configBasic(HELLO_MSG);
00165 while(1) {
00166 doMenu(1);
00167 }
00168 }