Test application for the library FM24Vxx_I2C
Dependencies: DebugLibrary FM24Vxx_I2C mbed
Diff: main.cpp
- Revision:
- 1:e3c92ff30be3
- Parent:
- 0:30f4bcb2a3c0
--- a/main.cpp Sat Mar 23 15:54:36 2013 +0000 +++ b/main.cpp Wed Apr 03 12:51:44 2013 +0000 @@ -32,24 +32,22 @@ DigitalOut g_availableLed(LED1); // To verify if program in running Ticker g_available; // LED1 will flash with a period of 2s -DigitalOut g_singleSeqNumScope(p6); // To synchronize the num scope to capture I2C trame UserChoice g_userChoice; // Used to store user choice from displayed menu -CFM24VXX_I2C g_myEEPROM(p9, p10, 0x01, p5, 400000); // Create an instance of the class CFM24VXX_I2C, p9/28: SDA, p10/29:SDL, p5: wired to WP input of FM24Vxx, address: A2=0,A1=1, on I2C bus +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 int main() { - - g_singleSeqNumScope = 0; + unsigned char memoryPage = 0x00; // Launch available indicator g_available.attach(&AvailableIndicator, 2.0); - g_myEEPROM.WriteProtect(false); // Disabe WP (pin 7) - See DS21203M - Clause 2.4 Write-Protect (WP) - + g_myFM24V10G.WriteProtect(false); + while (true) { // Interrupt driven processing g_state = Idle; // Set initial state g_userChoice = DisplayMenuAndGetChoice(); // Retrieve the user selection - DEBUG("User selects I2C Module %x", g_userChoice.moduleId) + DEBUG("Page select is set to %d", memoryPage) switch (g_userChoice.choice) { case 'a': do { @@ -92,31 +90,62 @@ } while (g_state != Idle); break; case 'i': - ReadStdStringValue(0x0100); + ReadStdStringValue(0x100); break; case 'j': - ReadStdStringValue(0); + ReadStdStringValue(0x100); break; case 'k': #if defined(__DEBUG) - g_myEEPROM.DumpMemoryArea(0, 0x14); + g_myFM24V10G.DumpMemoryArea(0, 0x14); #else // __DEBUG std::cout << "DEBUG mode is not set, nothing to do!\r" << std::endl; #endif // __DEBUG break; case 'l': - g_myEEPROM.EraseMemoryArea(0, 0x14); + g_myFM24V10G.EraseMemoryArea(0, 0x14); break; case 'm': { std::string test("Test"); - g_myEEPROM.Write(10, test); + g_myFM24V10G.Write(0x20, test); } break; case 'n': - ReadStdStringValue(10); + ReadStdStringValue(0x20); + break; + case 'o': { + const CFM24VXX_SN *sn = g_myFM24V10G.GetSerialNumber(); + std::cout << "\tCustomerID\t\t: " << std::setw(4) << std::setfill('0') << std::hex << sn->GetCustomerID() << "\r" << std::endl; + std::cout << "\tChecksum\t\t: " << std::setw(2) << std::setfill('0') << std::hex << sn->GetChecksum() << "\r" << std::endl; + } break; - default: + case 'p': { + const CFM24VXX_IDs *deviceId = g_myFM24V10G.GetDeviceID(); +#ifdef __DEBUG + DEBUG("\tManufacturerID\t\t: %02x", deviceId->GetManufacturerID()); + DEBUG("\tProductID\t\t: %02x", deviceId->GetProductID()); + DEBUG("\tRevisionID\t\t: %02x", deviceId->GetRevisionID()); + DEBUG("\tDensity\t\t\t: %02x", deviceId->GetDensity()); + DEBUG("\tVariation\t\t: %02x", deviceId->GetVariation()); +#else + std::cout << "\tManufacturerID\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetManufacturerID() << "\r" << std::endl; + std::cout << "\tProductID\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetProductID() << "\r" << std::endl; + std::cout << "\tRevisionID\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetRevisionID() << "\r" << std::endl; + std::cout << "\tDensity\t\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetDensity() << "\r" << std::endl; + std::cout << "\tVariation\t\t: " << std::setw(2) << std::setfill('0') << std::hex << deviceId->GetVariation() << "\r" << std::endl; +#endif // __DEBUG + } + break; + case 's': + memoryPage = (memoryPage + 1) % 2; + DEBUG("New page select is set to %d", memoryPage) + g_myFM24V10G.SelectMemoryPage(memoryPage); + do { + RunStateMachine_WithStdStringValue(); + } while (g_state != Idle); + break; + default: std::cout << "Invalid user choice\r" << std::endl; break; } // End of 'switch' statement @@ -133,7 +162,7 @@ static UserChoice userChoice; // Display the title - std::cout << "\r" << std::endl << "FM24Vxx_I2C v0.1\r" << std::endl; + std::cout << "\r" << std::endl << "FM24Vxx_I2C v0.2\r" << std::endl; // Display the menu std::cout << "\tWrite/Read with std::string:\t\t\ta\r" << std::endl; std::cout << "\tWrite/Read with a byte:\t\t\t\tb\r" << std::endl; @@ -147,8 +176,11 @@ std::cout << "\tRead a string is address 0x0000:\t\tj\r" << std::endl; std::cout << "\tHexadump from address 0x0000:\t\t\tk\r" << std::endl; std::cout << "\tErase from address 0x0000:\t\t\tl\r" << std::endl; - std::cout << "\tWrite 'Test' at address 0x000a:\t\t\tm\r" << std::endl; - std::cout << "\tRead from address 0x000a:\t\t\tn\r" << std::endl; + std::cout << "\tWrite 'Test' at address 0x0020:\t\t\tm\r" << std::endl; + std::cout << "\tRead from address 0x0020:\t\t\tn\r" << std::endl; + std::cout << "\tGet serial number:\t\t\t\to\r" << std::endl; + std::cout << "\tGet devide ID:\t\t\t\t\tp\r" << std::endl; + std::cout << "\tSwitch page select:\t\t\t\ts\r" << std::endl; std::cout << "Enter your choice: " << std::flush; userChoice.choice = getchar(); return userChoice; @@ -170,8 +202,7 @@ str += ctime(&ctTime); str += " UTC"; std::cout << "RunStateMachine_WithStdStringValue: Write '" << str << "'\r" << std::endl; - g_singleSeqNumScope = 1; - if (!g_myEEPROM.Write(256, str)) { // Write the string, including the length indicator + if (!g_myFM24V10G.Write(0x40, str)) { // Write the string, including the length indicator #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithStdStringValue: write failed at address 256") #else // __DEBUG @@ -181,7 +212,6 @@ } else { g_state = Written; } - g_singleSeqNumScope = 0; } break; case Written: @@ -191,7 +221,7 @@ case Read: { DEBUG("Reading datas..."); std::string readtext; - if (!g_myEEPROM.Read(256, readtext)) { // Read the string, including the length indicator + if (!g_myFM24V10G.Read(0x40, readtext)) { // Read the string, including the length indicator #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithStdStringValue: write failed at address 256") #else // __DEBUG @@ -222,8 +252,7 @@ case Write: DEBUG("Writing data...") std::cout << "RunStateMachine_WithByteValue: Write 0xaa\r" << std::endl; - g_singleSeqNumScope = 1; - if (!g_myEEPROM.Write(128, (unsigned char)0xaa)) { + if (!g_myFM24V10G.Write(128, (unsigned char)0xaa)) { #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithByteValue: write failed at address 128") #else // __DEBUG @@ -233,7 +262,6 @@ } else { g_state = Written; } - g_singleSeqNumScope = 0; break; case Written: g_state = Read; @@ -242,7 +270,7 @@ case Read: { DEBUG("Reading datas...") unsigned char value = 0x00; - if (!g_myEEPROM.Read(128, &value)) { + if (!g_myFM24V10G.Read(128, &value)) { #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithByteValue: Read operation failed at address 128") #else // __DEBUG @@ -273,7 +301,7 @@ case Write: DEBUG("Writing data...") std::cout << "RunStateMachine_WithShortValue: Write 0xbeef\r" << std::endl; - if (!g_myEEPROM.Write(64, (short)0xbeef, p_mode)) { // See http://en.wikipedia.org/wiki/Hexspeak for more ideas on hexadecimal wording!!! + if (!g_myFM24V10G.Write(64, (short)0xbeef, p_mode)) { // See http://en.wikipedia.org/wiki/Hexspeak for more ideas on hexadecimal wording!!! DEBUG_FATAL("RunStateMachine_WithShortValue: write failed at address 64") g_state = Idle; } else { @@ -287,14 +315,14 @@ case Read: { DEBUG("Reading datas..."); short value = 0; - if (!g_myEEPROM.Read(64, &value, p_mode)) { + if (!g_myFM24V10G.Read(64, &value, p_mode)) { #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithShortValue: write failed at address 64") #else // __DEBUG std::cout << "RunStateMachine_WithShortValue: write failed at address 64\r" << std::endl; #endif // __DEBUG } else { - std::cout << "RunStateMachine_WithShortValue: Read '0x" << std::cout << std::setw(4) << std::setfill('0') << std::hex << value << "' / '" << (unsigned short)value << "'\r" << std::endl; + std::cout << "RunStateMachine_WithShortValue: Read '0x" << std::setw(4) << std::setfill('0') << std::hex << value << "' / '" << (unsigned short)value << "'\r" << std::endl; } } g_state = Idle; @@ -318,7 +346,7 @@ case Write: DEBUG("Writing data...") std::cout << "RunStateMachine_WithIntegerValue: Write 0xdeaddead\r" << std::endl; - if (!g_myEEPROM.Write(32, (int)0xdeaddead, p_mode)) { + if (!g_myFM24V10G.Write(0, (int)0xdeaddead, p_mode)) { DEBUG_FATAL("RunStateMachine_WithIntegerValue: write failed at address 32") g_state = Idle; } else { @@ -332,7 +360,7 @@ case Read: { DEBUG("Reading datas...") int value = 0; - if (!g_myEEPROM.Read(32, &value, p_mode)) { + if (!g_myFM24V10G.Read(0, &value, p_mode)) { #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithIntegerValue: write failed at address 32") #else // __DEBUG @@ -368,7 +396,7 @@ datas.push_back(0xce); DEBUG("Writing data...") std::cout << "RunStateMachine_WithStdVectorOfByteValue: Write {0xfe, 0xed, 0xfa, 0xce}\r" << std::endl; - if (!g_myEEPROM.Write(16, datas, false)) { // Write the full buffer, not including the length indication + if (!g_myFM24V10G.Write(0, datas, false)) { // Write the full buffer, not including the length indication DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 16") g_state = Idle; } else { @@ -383,7 +411,7 @@ case Read: { DEBUG("Reading datas...") std::vector<unsigned char> datas(4); - if (!g_myEEPROM.Read(16, datas, false)) { // Read bytes, without the lenght indication, buffer size shall be set before the call + if (!g_myFM24V10G.Read(0, datas, false)) { // Read bytes, without the lenght indication, buffer size shall be set before the call #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 16") #else // __DEBUG @@ -419,7 +447,7 @@ datas.push_back(0xa5); datas.push_back(0xed); std::cout << "RunStateMachine_WithLengthPlusStdVectorOfByteValue: Write {0xde, 0x5e, 0xa5, 0xed}\r" << std::endl; - if (!g_myEEPROM.Write(8, datas)) { // Write the full buffer, including the length indication + if (!g_myFM24V10G.Write(0, datas)) { // Write the full buffer, including the length indication DEBUG_FATAL("RunStateMachine_WithLengthPlusStdVectorOfByteValue: write failed at address 8") g_state = Idle; } else { @@ -434,7 +462,7 @@ case Read: { DEBUG("Reading datas...") std::vector<unsigned char> datas; - if (!g_myEEPROM.Read(8, datas)) { // Read bytes, including the lenght indication, buffer size is not set before the call + if (!g_myFM24V10G.Read(8, datas)) { // Read bytes, including the lenght indication, buffer size is not set before the call #ifdef __DEBUG DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 8") #else // __DEBUG @@ -465,7 +493,7 @@ DEBUG("Reading datas..."); std::string readtext; - if (!g_myEEPROM.Read(p_address, readtext)) { // Read the string, including the length indicator + if (!g_myFM24V10G.Read(p_address, readtext)) { // Read the string, including the length indicator #ifdef __DEBUG DEBUG_FATAL("ReadStdStringValue: write failed at address 256") #else // __DEBUG