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.
Dependencies: DebugLibrary FM24Vxx_I2C mbed
main.cpp
00001 #include <string> 00002 #include <iostream> 00003 #include <iomanip> 00004 00005 #include "FM24Vxx_I2C.h" 00006 00007 struct UserChoice { 00008 char choice; 00009 unsigned char moduleId; 00010 }; 00011 00012 /* 00013 * Declare functions 00014 */ 00015 void AvailableIndicator(); // LED1 flashing for program while program is alive 00016 UserChoice DisplayMenuAndGetChoice(); // Display and get the user choice 00017 void RunStateMachine_WithStdStringValue(); // Write and read a string 00018 void RunStateMachine_WithByteValue(); // Write and read a byte 00019 void RunStateMachine_WithShortValue(CFM24VXX_I2C::Mode p_mode); // Write and read a short 00020 void RunStateMachine_WithIntegerValue(CFM24VXX_I2C::Mode p_mode); // Write and read an integer 00021 void RunStateMachine_WithStdVectorOfByteValue(); // Write and read a buffer of bytes 00022 void RunStateMachine_WithLengthPlusStdVectorOfByteValue(); // Write and read a buffer of bytes 00023 void ReadStdStringValue(const int p_address); // Read a string 00024 00025 enum States { 00026 Idle, // Idle state / Read memory operation done 00027 Write, // Write memory operation 00028 Written, // Write memory operation done 00029 Read // Read memory operation 00030 }; 00031 States g_state; // Process states for memory operations 00032 00033 DigitalOut g_availableLed(LED1); // To verify if program in running 00034 Ticker g_available; // LED1 will flash with a period of 2s 00035 UserChoice g_userChoice; // Used to store user choice from displayed menu 00036 00037 CFM24VXX_I2C g_myFM24V10G(p28, p27, 0x00, p12); // Create an instance of the class CFM24VXX_I2C, p9/28: SDA, p10/29:SDL, p12: wired to WP input of FM24Vxx, address: A2=0,A1=1, on I2C bus 00038 00039 int main() { 00040 unsigned char memoryPage = 0x00; 00041 00042 // Launch available indicator 00043 g_available.attach(&AvailableIndicator, 2.0); 00044 00045 g_myFM24V10G.WriteProtect(false); 00046 00047 while (true) { // Interrupt driven processing 00048 g_state = Idle; // Set initial state 00049 g_userChoice = DisplayMenuAndGetChoice(); // Retrieve the user selection 00050 DEBUG("Page select is set to %d", memoryPage) 00051 switch (g_userChoice.choice) { 00052 case 'a': 00053 do { 00054 RunStateMachine_WithStdStringValue(); 00055 } while (g_state != Idle); 00056 break; 00057 case 'b': 00058 do { 00059 RunStateMachine_WithByteValue(); 00060 } while (g_state != Idle); 00061 break; 00062 case 'c': 00063 do { 00064 RunStateMachine_WithShortValue(CFM24VXX_I2C::BigEndian); 00065 } while (g_state != Idle); 00066 break; 00067 case 'd': 00068 do { 00069 RunStateMachine_WithShortValue(CFM24VXX_I2C::LittleEndian); 00070 } while (g_state != Idle); 00071 break; 00072 case 'e': 00073 do { 00074 RunStateMachine_WithIntegerValue(CFM24VXX_I2C::BigEndian); 00075 } while (g_state != Idle); 00076 break; 00077 case 'f': 00078 do { 00079 RunStateMachine_WithIntegerValue(CFM24VXX_I2C::LittleEndian); 00080 } while (g_state != Idle); 00081 break; 00082 case 'g': 00083 do { 00084 RunStateMachine_WithStdVectorOfByteValue(); 00085 } while (g_state != Idle); 00086 break; 00087 case 'h': 00088 do { 00089 RunStateMachine_WithLengthPlusStdVectorOfByteValue(); 00090 } while (g_state != Idle); 00091 break; 00092 case 'i': 00093 ReadStdStringValue(0x100); 00094 break; 00095 case 'j': 00096 ReadStdStringValue(0x100); 00097 break; 00098 case 'k': 00099 #if defined(__DEBUG) 00100 g_myFM24V10G.DumpMemoryArea(0, 0x14); 00101 #else // __DEBUG 00102 std::cout << "DEBUG mode is not set, nothing to do!\r" << std::endl; 00103 #endif // __DEBUG 00104 break; 00105 case 'l': 00106 g_myFM24V10G.EraseMemoryArea(0, 0x14); 00107 break; 00108 case 'm': 00109 { 00110 std::string test("Test"); 00111 g_myFM24V10G.Write(0x20, test); 00112 } 00113 break; 00114 case 'n': 00115 ReadStdStringValue(0x20); 00116 break; 00117 case 'o': { 00118 const CFM24VXX_SN *sn = g_myFM24V10G.GetSerialNumber(); 00119 std::cout << "\tCustomerID\t\t: " << std::setw(4) << std::setfill('0') << std::hex << sn->GetCustomerID() << "\r" << std::endl; 00120 std::cout << "\tChecksum\t\t: " << std::setw(2) << std::setfill('0') << std::hex << sn->GetChecksum() << "\r" << std::endl; 00121 } 00122 break; 00123 case 'p': { 00124 const CFM24VXX_IDs *deviceId = g_myFM24V10G.GetDeviceID(); 00125 #ifdef __DEBUG 00126 DEBUG("\tManufacturerID\t\t: %02x", deviceId->GetManufacturerID()); 00127 DEBUG("\tProductID\t\t: %02x", deviceId->GetProductID()); 00128 DEBUG("\tRevisionID\t\t: %02x", deviceId->GetRevisionID()); 00129 DEBUG("\tDensity\t\t\t: %02x", deviceId->GetDensity()); 00130 DEBUG("\tVariation\t\t: %02x", deviceId->GetVariation()); 00131 #else 00132 std::cout << "\tManufacturerID\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetManufacturerID() << "\r" << std::endl; 00133 std::cout << "\tProductID\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetProductID() << "\r" << std::endl; 00134 std::cout << "\tRevisionID\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetRevisionID() << "\r" << std::endl; 00135 std::cout << "\tDensity\t\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetDensity() << "\r" << std::endl; 00136 std::cout << "\tVariation\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetVariation() << "\r" << std::endl; 00137 #endif // __DEBUG 00138 } 00139 break; 00140 case 's': 00141 memoryPage = (memoryPage + 1) % 2; 00142 DEBUG("New page select is set to %d", memoryPage) 00143 g_myFM24V10G.SelectMemoryPage(memoryPage); 00144 do { 00145 RunStateMachine_WithStdStringValue(); 00146 } while (g_state != Idle); 00147 break; 00148 default: 00149 std::cout << "Invalid user choice\r" << std::endl; 00150 break; 00151 } // End of 'switch' statement 00152 } // End of 'while' statement 00153 } // End of program - nerver reached 00154 00155 void AvailableIndicator() 00156 { 00157 g_availableLed = !g_availableLed; 00158 } // End of AvailableIndicator 00159 00160 UserChoice DisplayMenuAndGetChoice() 00161 { 00162 static UserChoice userChoice; 00163 00164 // Display the title 00165 std::cout << "\r" << std::endl << "FM24Vxx_I2C v0.2\r" << std::endl; 00166 // Display the menu 00167 std::cout << "\tWrite/Read with std::string:\t\t\ta\r" << std::endl; 00168 std::cout << "\tWrite/Read with a byte:\t\t\t\tb\r" << std::endl; 00169 std::cout << "\tWrite/Read with a short (Big Endian):\t\tc\r" << std::endl; 00170 std::cout << "\tWrite/Read with a short (Little Endian):\td\r" << std::endl; 00171 std::cout << "\tWrite/Read with an integer (Big Endian):\te\r" << std::endl; 00172 std::cout << "\tWrite/Read with an integer (Little Endian):\tf\r" << std::endl; 00173 std::cout << "\tWrite/Read with std::vector<byte>:\t\tg\r" << std::endl; 00174 std::cout << "\tWrite/Read with std::vector<byte> + length:\th\r" << std::endl; 00175 std::cout << "\tRead with std::string:\t\t\t\ti\r" << std::endl; 00176 std::cout << "\tRead a string is address 0x0000:\t\tj\r" << std::endl; 00177 std::cout << "\tHexadump from address 0x0000:\t\t\tk\r" << std::endl; 00178 std::cout << "\tErase from address 0x0000:\t\t\tl\r" << std::endl; 00179 std::cout << "\tWrite 'Test' at address 0x0020:\t\t\tm\r" << std::endl; 00180 std::cout << "\tRead from address 0x0020:\t\t\tn\r" << std::endl; 00181 std::cout << "\tGet serial number:\t\t\t\to\r" << std::endl; 00182 std::cout << "\tGet devide ID:\t\t\t\t\tp\r" << std::endl; 00183 std::cout << "\tSwitch page select:\t\t\t\ts\r" << std::endl; 00184 std::cout << "Enter your choice: " << std::flush; 00185 userChoice.choice = getchar(); 00186 return userChoice; 00187 } 00188 00189 void RunStateMachine_WithStdStringValue() 00190 { 00191 DEBUG_ENTER("RunStateMachine_WithStdStringValue") 00192 00193 switch (g_state) { 00194 case Idle: 00195 g_state = Write; 00196 break; 00197 case Write: { 00198 DEBUG("Writing data..."); 00199 time_t ctTime; 00200 ctTime = time(NULL); 00201 std::string str("Current time is: "); 00202 str += ctime(&ctTime); 00203 str += " UTC"; 00204 std::cout << "RunStateMachine_WithStdStringValue: Write '" << str << "'\r" << std::endl; 00205 if (!g_myFM24V10G.Write(0x40, str)) { // Write the string, including the length indicator 00206 #ifdef __DEBUG 00207 DEBUG_FATAL("RunStateMachine_WithStdStringValue: write failed at address 256") 00208 #else // __DEBUG 00209 std::cout << "RunStateMachine_WithStdStringValue: write failed at address 256\r" << std::endl; 00210 #endif // __DEBUG 00211 g_state = Idle; 00212 } else { 00213 g_state = Written; 00214 } 00215 } 00216 break; 00217 case Written: 00218 g_state = Read; 00219 wait(1.0); // To prevent I2C bus capacity memeory 00220 break; 00221 case Read: { 00222 DEBUG("Reading datas..."); 00223 std::string readtext; 00224 if (!g_myFM24V10G.Read(0x40, readtext)) { // Read the string, including the length indicator 00225 #ifdef __DEBUG 00226 DEBUG_FATAL("RunStateMachine_WithStdStringValue: write failed at address 256") 00227 #else // __DEBUG 00228 std::cout << "RunStateMachine_WithStdStringValue: write failed at address 256\r" << std::endl; 00229 #endif // __DEBUG 00230 } else { 00231 std::cout << "RunStateMachine_WithStdStringValue: Read: " << readtext << "\r" << std::endl; 00232 } 00233 } 00234 g_state = Idle; 00235 break; 00236 default: 00237 std::cout << "RunStateMachine_WithStdStringValue: Default!\r" << std::endl; 00238 g_state = Idle; 00239 break; 00240 } 00241 00242 DEBUG_LEAVE("RunStateMachine_WithStdStringValue") 00243 } 00244 00245 void RunStateMachine_WithByteValue() { 00246 DEBUG_ENTER("RunStateMachine_WithByteValue") 00247 00248 switch (g_state) { 00249 case Idle: 00250 g_state = Write; 00251 break; 00252 case Write: 00253 DEBUG("Writing data...") 00254 std::cout << "RunStateMachine_WithByteValue: Write 0xaa\r" << std::endl; 00255 if (!g_myFM24V10G.Write(128, (unsigned char)0xaa)) { 00256 #ifdef __DEBUG 00257 DEBUG_FATAL("RunStateMachine_WithByteValue: write failed at address 128") 00258 #else // __DEBUG 00259 std::cout << "RunStateMachine_WithByteValue: write failed at address 128\r" << std::endl; 00260 #endif // __DEBUG 00261 g_state = Idle; 00262 } else { 00263 g_state = Written; 00264 } 00265 break; 00266 case Written: 00267 g_state = Read; 00268 wait(1.0); // To prevent I2C bus capacity memeory 00269 break; 00270 case Read: { 00271 DEBUG("Reading datas...") 00272 unsigned char value = 0x00; 00273 if (!g_myFM24V10G.Read(128, &value)) { 00274 #ifdef __DEBUG 00275 DEBUG_FATAL("RunStateMachine_WithByteValue: Read operation failed at address 128") 00276 #else // __DEBUG 00277 std::cout << "RunStateMachine_WithByteValue: Read operation failed at address 128\r" << std::endl; 00278 #endif // __DEBUG 00279 } else { 00280 std::cout << "RunStateMachine_WithByteValue: Read '0x" << std::setw(2) << std::setfill('0') << std::ios::hex << value << "'\r" << std::endl; 00281 } 00282 } 00283 g_state = Idle; 00284 break; 00285 default: 00286 std::cout << "RunStateMachine_WithByteValue: Default!\r" << std::endl; 00287 g_state = Idle; 00288 break; 00289 } 00290 00291 DEBUG_LEAVE("RunStateMachine_WithByteValue") 00292 } 00293 00294 void RunStateMachine_WithShortValue(CFM24VXX_I2C::Mode p_mode) { 00295 DEBUG_ENTER("RunStateMachine_WithShortValue") 00296 00297 switch (g_state) { 00298 case Idle: 00299 g_state = Write; 00300 break; 00301 case Write: 00302 DEBUG("Writing data...") 00303 std::cout << "RunStateMachine_WithShortValue: Write 0xbeef\r" << std::endl; 00304 if (!g_myFM24V10G.Write(64, (short)0xbeef, p_mode)) { // See http://en.wikipedia.org/wiki/Hexspeak for more ideas on hexadecimal wording!!! 00305 DEBUG_FATAL("RunStateMachine_WithShortValue: write failed at address 64") 00306 g_state = Idle; 00307 } else { 00308 g_state = Written; 00309 } 00310 break; 00311 case Written: 00312 g_state = Read; 00313 wait(1.0); // To prevent I2C bus capacity memeory 00314 break; 00315 case Read: { 00316 DEBUG("Reading datas..."); 00317 short value = 0; 00318 if (!g_myFM24V10G.Read(64, &value, p_mode)) { 00319 #ifdef __DEBUG 00320 DEBUG_FATAL("RunStateMachine_WithShortValue: write failed at address 64") 00321 #else // __DEBUG 00322 std::cout << "RunStateMachine_WithShortValue: write failed at address 64\r" << std::endl; 00323 #endif // __DEBUG 00324 } else { 00325 std::cout << "RunStateMachine_WithShortValue: Read '0x" << std::setw(4) << std::setfill('0') << std::hex << value << "' / '" << (unsigned short)value << "'\r" << std::endl; 00326 } 00327 } 00328 g_state = Idle; 00329 break; 00330 default: 00331 std::cout << "RunStateMachine_WithShortValue: Default!\r" << std::endl; 00332 g_state = Idle; 00333 break; 00334 } 00335 00336 DEBUG_LEAVE("RunStateMachine_WithShortValue") 00337 } 00338 00339 void RunStateMachine_WithIntegerValue(CFM24VXX_I2C::Mode p_mode) { 00340 DEBUG_ENTER("RunStateMachine_WithIntegerValue") 00341 00342 switch (g_state) { 00343 case Idle: 00344 g_state = Write; 00345 break; 00346 case Write: 00347 DEBUG("Writing data...") 00348 std::cout << "RunStateMachine_WithIntegerValue: Write 0xdeaddead\r" << std::endl; 00349 if (!g_myFM24V10G.Write(0, (int)0xdeaddead, p_mode)) { 00350 DEBUG_FATAL("RunStateMachine_WithIntegerValue: write failed at address 32") 00351 g_state = Idle; 00352 } else { 00353 g_state = Written; 00354 } 00355 break; 00356 case Written: 00357 g_state = Read; 00358 wait(1.0); // To prevent I2C bus capacity memeory 00359 break; 00360 case Read: { 00361 DEBUG("Reading datas...") 00362 int value = 0; 00363 if (!g_myFM24V10G.Read(0, &value, p_mode)) { 00364 #ifdef __DEBUG 00365 DEBUG_FATAL("RunStateMachine_WithIntegerValue: write failed at address 32") 00366 #else // __DEBUG 00367 std::cout << "RunStateMachine_WithIntegerValue: write failed at address 32\r" << std::endl; 00368 #endif // __DEBUG 00369 } else { 00370 std::cout << std::setw(8) << std::setfill('0') << std::hex << "RunStateMachine_WithIntegerValue: Read '0x" << value << "'/ '" << (unsigned int)value << "'\r" << std::endl; 00371 } 00372 } 00373 g_state = Idle; 00374 break; 00375 default: 00376 std::cout << "RunStateMachine_WithIntegerValue: Default!\r" << std::endl; 00377 g_state = Idle; 00378 break; 00379 } 00380 00381 DEBUG_LEAVE("RunStateMachine_WithIntegerValue") 00382 } 00383 00384 void RunStateMachine_WithStdVectorOfByteValue() { 00385 DEBUG_ENTER("RunStateMachine_WithStdVectorOfByteValue") 00386 00387 switch (g_state) { 00388 case Idle: 00389 g_state = Write; 00390 break; 00391 case Write: { 00392 std::vector<unsigned char> datas; 00393 datas.push_back(0xfe); 00394 datas.push_back(0xed); 00395 datas.push_back(0xfa); 00396 datas.push_back(0xce); 00397 DEBUG("Writing data...") 00398 std::cout << "RunStateMachine_WithStdVectorOfByteValue: Write {0xfe, 0xed, 0xfa, 0xce}\r" << std::endl; 00399 if (!g_myFM24V10G.Write(0, datas, false)) { // Write the full buffer, not including the length indication 00400 DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 16") 00401 g_state = Idle; 00402 } else { 00403 g_state = Written; 00404 } 00405 } 00406 break; 00407 case Written: 00408 g_state = Read; 00409 wait(1.0); // To prevent I2C bus capacity memeory 00410 break; 00411 case Read: { 00412 DEBUG("Reading datas...") 00413 std::vector<unsigned char> datas(4); 00414 if (!g_myFM24V10G.Read(0, datas, false)) { // Read bytes, without the lenght indication, buffer size shall be set before the call 00415 #ifdef __DEBUG 00416 DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 16") 00417 #else // __DEBUG 00418 std::cout << "RunStateMachine_WithStdVectorOfByteValue: write failed at address 16\r" << std::endl; 00419 #endif // __DEBUG 00420 } else { 00421 std::cout << "RunStateMachine_WithStdVectorOfByteValue: Read {" << "', '" << datas[0] << "', '" << datas[1] << "', '" << datas[2] << "', '" << datas[3] <<"'}\r" << std::endl; 00422 } 00423 } 00424 g_state = Idle; 00425 break; 00426 default: 00427 std::cout << "RunStateMachine_WithStdVectorOfByteValue: Default!\r" << std::endl; 00428 g_state = Idle; 00429 break; 00430 } 00431 00432 DEBUG_LEAVE("RunStateMachine_WithStdVectorOfByteValue") 00433 } 00434 00435 void RunStateMachine_WithLengthPlusStdVectorOfByteValue() { 00436 DEBUG_ENTER("RunStateMachine_WithLengthPlusStdVectorOfByteValue") 00437 00438 switch (g_state) { 00439 case Idle: 00440 g_state = Write; 00441 break; 00442 case Write: { 00443 DEBUG("Writing data...") 00444 std::vector<unsigned char> datas; 00445 datas.push_back(0xde); 00446 datas.push_back(0x5e); 00447 datas.push_back(0xa5); 00448 datas.push_back(0xed); 00449 std::cout << "RunStateMachine_WithLengthPlusStdVectorOfByteValue: Write {0xde, 0x5e, 0xa5, 0xed}\r" << std::endl; 00450 if (!g_myFM24V10G.Write(0, datas)) { // Write the full buffer, including the length indication 00451 DEBUG_FATAL("RunStateMachine_WithLengthPlusStdVectorOfByteValue: write failed at address 8") 00452 g_state = Idle; 00453 } else { 00454 g_state = Written; 00455 } 00456 } 00457 break; 00458 case Written: 00459 g_state = Read; 00460 wait(1.0); // To prevent I2C bus capacity memeory 00461 break; 00462 case Read: { 00463 DEBUG("Reading datas...") 00464 std::vector<unsigned char> datas; 00465 if (!g_myFM24V10G.Read(8, datas)) { // Read bytes, including the lenght indication, buffer size is not set before the call 00466 #ifdef __DEBUG 00467 DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 8") 00468 #else // __DEBUG 00469 std::cout << "RunStateMachine_WithStdVectorOfByteValue: write failed at address 8\r" << std::endl; 00470 #endif // __DEBUG 00471 } else { 00472 std::cout << "RunStateMachine_WithLengthPlusStdVectorOfByteValue: Read bytes:\r" << std::endl; 00473 vector<unsigned char>::iterator it; 00474 for (it = datas.begin() ; it < datas.end(); it++) { 00475 std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << *it << " "; 00476 } 00477 std::cout << "\r" << std::endl; 00478 } 00479 } 00480 g_state = Idle; 00481 break; 00482 default: 00483 std::cout << "RunStateMachine_WithLengthPlusStdVectorOfByteValue: Default!\r" << std::endl; 00484 g_state = Idle; 00485 break; 00486 } 00487 00488 DEBUG_LEAVE("RunStateMachine_WithLengthPlusStdVectorOfByteValue") 00489 } 00490 00491 void ReadStdStringValue(const int p_address) { 00492 DEBUG_ENTER("ReadStdStringValue: %d", p_address) 00493 00494 DEBUG("Reading datas..."); 00495 std::string readtext; 00496 if (!g_myFM24V10G.Read(p_address, readtext)) { // Read the string, including the length indicator 00497 #ifdef __DEBUG 00498 DEBUG_FATAL("ReadStdStringValue: write failed at address 256") 00499 #else // __DEBUG 00500 std::cout << "ReadStdStringValue: write failed at address 256\r" << std::endl; 00501 #endif // __DEBUG 00502 } else { 00503 std::cout << "ReadStdStringValue: Read:'" << readtext << "'\r" << std::endl; 00504 } 00505 00506 DEBUG_LEAVE("ReadStdStringValue") 00507 }
Generated on Wed Jul 13 2022 23:35:49 by
1.7.2
Ramtron FM24Vxx F-RAM