Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
NVM.cpp
00001 #include <cstdlib> 00002 #include <string> 00003 #include "main.h" 00004 #include "Functions.h" 00005 #include "Definitions.h" 00006 #include "Boolean.h" 00007 #include "NextionLCD.h" 00008 #include "mbed.h" 00009 #include "platform/CircularBuffer.h" 00010 #include "rtos.h" 00011 #include "Threads.h" 00012 #include "Languages.h" 00013 #include "Ser25lcxxx.h" 00014 #include "NVM.h" 00015 #include "FastPWM.h" 00016 00017 extern Ser25LCxxx eeprom; 00018 extern Serial pc;//Debug Port via USB 00019 extern SPI spi;//SPI 00020 00021 const char romNVM[NVM_SIZE]= { //Deafault NVM Parameters written to empty EEPROM 00022 RPM,//Flow Units 00023 MANUAL,//Run Mode 00024 AUTO_RESTART_OFF, 00025 PUMP_HEAD_OK, 00026 ALARM_OFF, 00027 HEAD_LEFT, 00028 PUMP_OFF, 00029 0x00, 00030 0x00,//8 MAX speed limit, 125.0 00031 0x00, 00032 0xFA, 00033 0x42, 00034 0xCD,//12 Flow Unit Value, 0.1 00035 0xCC, 00036 0xCC, 00037 0x3D, 00038 0x33,//16 Analogue low cal float value loc 0, 4.10 00039 0x33, 00040 0x84, 00041 0x40, 00042 0x0A,//20 Analogue high cal float loc 0, 19.80 00043 0xD7, 00044 0x9F, 00045 0x41, 00046 0x00,//24 Flow % low cal float loc 0, 0.00 00047 0x00, 00048 0x00, 00049 0x00, 00050 0x00,//28 Flow % high cal float loc 0, 100.00 00051 0x00, 00052 0xC8, 00053 0x42, 00054 0xCD,//32 ADC min V 0.1360V 00055 0xCC, 00056 0x4C, 00057 0x3C, 00058 0xCF,//36 ADC max V 3.31200V 00059 0xF7, 00060 0x53, 00061 0x40, 00062 0x03,//40 DAC V low 0.1348 = (0.3030*0.445)//445mV 00063 0x09, 00064 0x0A, 00065 0x3E, 00066 0xA6,//44 DAC V high 0.90597 = (0.3030*2.99)//2.99V 00067 0xED, 00068 0x67, 00069 0x3F, 00070 RMT_STOP_PUMP_HIGH,//48 00071 OUT1_GENERAL_ALARM, 00072 OUT1_LOGIC_LO, 00073 OUT2_RUN_STATUS, 00074 OUT2_LOGIC_LO, 00075 _4_20MA_OUT_FULL_SCALE//53 00076 }; 00077 00078 00079 00080 void clearNVM(void){ 00081 /* 00082 Clears NVM EEPROM space to 0xFF 00083 */ 00084 #ifdef DEBUG_NVM 00085 pc.printf("\r\n\r\nClear All NMV settings (page 0 - 1)");//Display the buffer 00086 #endif 00087 00088 eeprom.clearMem(); 00089 } 00090 00091 void wrtDefNVM(struct settingsNVM *data){ 00092 00093 #ifdef DEBUG_NVM 00094 pc.printf("\r\n\nWriting default NVM Settings from NVM ROM to NVM data structure and EEPROM"); 00095 #endif 00096 00097 memcpy(data,romNVM,NVM_SIZE); 00098 00099 writeNVM(NVM_START,NVM_SIZE,data);//Write NVM structure to EEPROM 00100 } 00101 00102 void loadNVM(struct settingsNVM *data, bool loadDefaults) 00103 { 00104 char* tmp; 00105 00106 if((blkChkEEPROM() == true)||(loadDefaults == true))//Virgin EEPROM 00107 { 00108 #ifdef DEBUG_NVM 00109 pc.printf("\r\n\nVirgin EEPROM so load the default NVM ROM data"); 00110 #endif 00111 00112 clearNVM(); 00113 wrtDefNVM(data); 00114 } 00115 else 00116 { 00117 tmp = eeprom.read(NVM_START,NVM_SIZE); 00118 00119 memcpy(data,tmp,NVM_SIZE); 00120 00121 #ifdef DEBUG_NVM 00122 pc.printf("\r\n\nEEPROM Contents"); 00123 for(int i=0; i < NVM_SIZE; i++) 00124 pc.printf("\r\n|(%d) \t 0x%02X |",i,tmp[i]); 00125 00126 pc.printf("\r\n"); 00127 #endif 00128 00129 free(tmp); 00130 } 00131 } 00132 00133 bool writeNVM(uint16_t start_addr,uint8_t len,struct settingsNVM *data){ 00134 /* 00135 Writes All contents of data structure to EEPROM 00136 */ 00137 bool writeOK = true; 00138 00139 if(!eeprom.write(start_addr,len,(char*)data)){ 00140 #ifdef DEBUG_NVM 00141 pc.printf("\r\n\nWritting All NMV Settings\r\n");//Display the buffer 00142 #endif 00143 writeOK = false; 00144 } 00145 else 00146 { 00147 #ifdef DEBUG_NVM 00148 pc.printf("\r\n\nNMV Write FAILED\r\n");//Display the buffer 00149 #endif 00150 } 00151 00152 return(writeOK); 00153 } 00154 00155 bool writeNVMfloat(uint16_t addr,float val){ 00156 00157 bool writeOK = true; 00158 00159 #ifdef DEBUG_NVM 00160 pc.printf("\r\n\r\nWriting Float %f to EEPROM at base Addr %d", val, addr);//Display the buffer 00161 #endif 00162 00163 if(!eeprom.write(addr, sizeof(val), (char*)&val)){ 00164 #ifdef DEBUG_NVM 00165 pc.printf("\r\n\r\nEEPROM Write Fail"); 00166 #endif 00167 writeOK = false; 00168 } 00169 00170 return(writeOK); 00171 } 00172 00173 bool writeNVMByte(uint16_t addr,uint8_t val){ 00174 00175 static uint16_t lastAddr = 0; 00176 static uint8_t lastVal = 0; 00177 bool writeOK = true; 00178 00179 if((addr != lastAddr)||(val != lastVal)){ 00180 #ifdef DEBUG_NVM 00181 pc.printf("\r\n\r\nWriting Byte to 0x%02X to EEPROM at Addr %d", val, addr); 00182 #endif 00183 00184 if(!eeprom.write(addr, sizeof(val), (char*)&val)){ 00185 #ifdef DEBUG_NVM 00186 pc.printf("\r\n\r\nEEPROM Write Fail"); 00187 #endif 00188 writeOK = false; 00189 } 00190 else 00191 #ifdef DEBUG_NVM 00192 pc.printf("\tWrite OK"); 00193 #endif 00194 00195 lastAddr = addr; 00196 lastVal = val; 00197 } 00198 00199 return(writeOK); 00200 } 00201 00202 bool blkChkEEPROM(void) 00203 { 00204 /* 00205 Checks if ANY NVM location in the EEPROM is 0xFF, if so the EEPROM is deemed to be a virgin EEPROM 00206 */ 00207 00208 bool blank = true; 00209 uint8_t i; 00210 00211 char* store = eeprom.read(NVM_START,NVM_SIZE); 00212 00213 for (i=0;i<NVM_SIZE;i++) 00214 { 00215 #ifdef DEBUG_NVM 00216 pc.printf("\r\n\nBlank Check EEPROM = 0x%02X, Index = %d",store[i],i); 00217 #endif 00218 00219 if(store[i] != BLANK) 00220 { 00221 #ifdef DEBUG_NVM 00222 pc.printf("\r\n\nEEPROM has NVM settings so load up the NVM data into NVM RAM"); 00223 for(int i=0; i < NVM_SIZE; i++) 00224 pc.printf("\r\n|(%d) \t 0x%02X |",i,store[i]); 00225 00226 pc.printf("\r\n"); 00227 #endif 00228 00229 blank = false; 00230 break; 00231 } 00232 } 00233 00234 if(i==NVM_SIZE) 00235 { 00236 #ifdef DEBUG_NVM 00237 pc.printf("\r\n\nNVM EEPROM IS BLANK :-)"); 00238 #endif 00239 } 00240 00241 return(blank); 00242 } 00243 00244 void initNVM(void){ 00245 spi.format(8,3); 00246 spi.frequency(SPI_FREQ); 00247 Ser25LCxxx eeprom(&spi,SSEL,BYTE_SIZE,PAGE_SIZE);//CS SSEL 00248 }
Generated on Tue Jul 19 2022 00:58:42 by
1.7.2