This program illustrates how to use the 24LCxx_I2C library In this version, NTP service was added to set RTC time
Dependencies: NetServices mbed
main.cpp
00001 #include <string> 00002 00003 #include "24LCxx_I2C.h" 00004 #include "EthernetNetIf.h" 00005 #include "NTPClient.h" 00006 00007 /* 00008 * Declare functions 00009 */ 00010 00011 void AvailableIndicator(); // LED1 flashing for program while program is alive 00012 char DisplayMenuAndGetChoice(); // Display and get the user choice 00013 void RunStateMachine_WithStdStringValue(); // Write and read a string 00014 void RunStateMachine_WithByteValue(); // Write and read a byte 00015 void RunStateMachine_WithShortValue(C2424LCXX_I2C::Mode p_mode); // Write and read a short 00016 void RunStateMachine_WithIntegerValue(C2424LCXX_I2C::Mode p_mode); // Write and read an integer 00017 void RunStateMachine_WithStdVectorOfByteValue(); // Write and read a buffer of bytes 00018 void RunStateMachine_WithLengthPlusStdVectorOfByteValue(); // Write and read a buffer of bytes 00019 00020 /* 00021 * Declare statics 00022 */ 00023 EthernetNetIf g_eth; 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 00036 #define __3_3V__ 00037 #ifdef __3_3V__ 00038 C2424LCXX_I2C g_myEEPROM(p28, p27, 0x01, p5, 400000); // Create an instance of the class C2424LCXX_I2C, p28: SDA, p29:SDL, p5: wired to WP input of 24LCxx, address: A3=0,A2=0,A=1, on 3.3V I2C bus 00039 #else // __3_3V__ 00040 C2424LCXX_I2C g_myEEPROM(p28, p27, 0x07, p7, 400000); // Create an instance of the class C2424LCXX_I2C, p28: SDA, p29:SDL, p7: wired to WP input of 24LCxx, address: A3=1,A2=1,A=1, on 5V I2C bus 00041 #endif // __3_3V__ 00042 00043 int main() { 00044 00045 // Launch available indicator 00046 g_available.attach(&AvailableIndicator, 2.0); 00047 00048 // Initialize Ethernet 00049 g_eth.setup(); 00050 00051 wait(1); // Needed after Ethernet startup 00052 00053 IpAddr ip = g_eth.getIp(); 00054 printf("IP address: %d:%d:%d:%d\r\n", ip[0], ip[1], ip[2], ip[3]); 00055 00056 // Update time 00057 Host ntpServer(IpAddr(), 123, "ntp.unice.fr"); 00058 NTPClient ntp; 00059 ntp.setTime(ntpServer); 00060 00061 g_myEEPROM.WriteProtect(false); // Disabe WP (pin 7) - See DS21203M - Clause 2.4 Write-Protect (WP) 00062 00063 while (true) { // Interrupt driven processing 00064 g_state = Idle; 00065 switch (DisplayMenuAndGetChoice()) { 00066 case 'a': 00067 do { 00068 RunStateMachine_WithStdStringValue(); 00069 } while (g_state != Idle); 00070 break; 00071 case 'b': 00072 do { 00073 RunStateMachine_WithByteValue(); 00074 } while (g_state != Idle); 00075 break; 00076 case 'c': 00077 do { 00078 RunStateMachine_WithShortValue(C2424LCXX_I2C::BigEndian); 00079 } while (g_state != Idle); 00080 break; 00081 case 'd': 00082 do { 00083 RunStateMachine_WithShortValue(C2424LCXX_I2C::LittleEndian); 00084 } while (g_state != Idle); 00085 break; 00086 case 'e': 00087 do { 00088 RunStateMachine_WithIntegerValue(C2424LCXX_I2C::BigEndian); 00089 } while (g_state != Idle); 00090 break; 00091 case 'f': 00092 do { 00093 RunStateMachine_WithIntegerValue(C2424LCXX_I2C::LittleEndian); 00094 } while (g_state != Idle); 00095 break; 00096 case 'g': 00097 do { 00098 RunStateMachine_WithStdVectorOfByteValue(); 00099 } while (g_state != Idle); 00100 break; 00101 case 'h': 00102 do { 00103 RunStateMachine_WithLengthPlusStdVectorOfByteValue(); 00104 } while (g_state != Idle); 00105 break; 00106 default: 00107 printf("Invalid user choice\r\n"); 00108 break; 00109 } // End of 'switch' statement 00110 } // End of 'while' statement 00111 } // End of program - nerver reached 00112 00113 void AvailableIndicator() 00114 { 00115 g_availableLed = !g_availableLed; 00116 } // End of AvailableIndicator 00117 00118 char DisplayMenuAndGetChoice() 00119 { 00120 printf("\r\n\r\n24LCxx_I2C v0.1\r\n"); 00121 printf("\tWrite/Read with std::string:\t\t\ta\r\n"); 00122 printf("\tWrite/Read with a byte:\t\t\t\tb\r\n"); 00123 printf("\tWrite/Read with a short (Big Endian):\t\tc\r\n"); 00124 printf("\tWrite/Read with a short (Little Endian):\td\r\n"); 00125 printf("\tWrite/Read with an integer (Big Endian):\te\r\n"); 00126 printf("\tWrite/Read with an integer (Little Endian):\tf\r\n"); 00127 printf("\tWrite/Read with std::vector<byte>:\t\tg\r\n"); 00128 printf("\tWrite/Read with std::vector<byte> + length:\th\r\n"); 00129 printf("Enter your choice:\r\n"); 00130 return getchar(); 00131 } 00132 00133 void RunStateMachine_WithStdStringValue() 00134 { 00135 DEBUG_ENTER("RunStateMachine_WithStdStringValue") 00136 00137 switch (g_state) { 00138 case Idle: 00139 g_state = Write; 00140 break; 00141 case Write: { 00142 DEBUG("Writing data..."); 00143 time_t ctTime; 00144 ctTime = time(NULL); 00145 std::string str("Current time is: "); 00146 str += ctime(&ctTime); 00147 str += " UTC"; 00148 printf("RunStateMachine_WithStdStringValue: Write '%s'\r\n", str.c_str()); 00149 if (!g_myEEPROM.Write(256, str)) { // Write the string, including the length indicator 00150 #ifdef __DEBUG 00151 DEBUG_FATAL("RunStateMachine_WithStdStringValue: write failed at address 256") 00152 #else // __DEBUG 00153 printf("RunStateMachine_WithStdStringValue: write failed at address 256\r\n"); 00154 #endif // __DEBUG 00155 g_state = Idle; 00156 } else { 00157 g_state = Written; 00158 } 00159 } 00160 break; 00161 case Written: 00162 g_state = Read; 00163 break; 00164 case Read: { 00165 DEBUG("Reading datas..."); 00166 std::string readtext; 00167 if (!g_myEEPROM.Read(256, readtext)) { // Read the string, including the length indicator 00168 #ifdef __DEBUG 00169 DEBUG_FATAL("RunStateMachine_WithStdStringValue: write failed at address 256") 00170 #else // __DEBUG 00171 printf("RunStateMachine_WithStdStringValue: write failed at address 256\r\n"); 00172 #endif // __DEBUG 00173 } else { 00174 printf("RunStateMachine_WithStdStringValue: Read:'%s'\r\n", readtext.c_str()); 00175 } 00176 } 00177 g_state = Idle; 00178 break; 00179 default: 00180 printf("RunStateMachine_WithStdStringValue: Default!\r\n"); 00181 g_state = Idle; 00182 break; 00183 } 00184 00185 DEBUG_LEAVE("RunStateMachine_WithStdStringValue") 00186 } 00187 00188 void RunStateMachine_WithByteValue() { 00189 DEBUG_ENTER("RunStateMachine_WithByteValue") 00190 00191 switch (g_state) { 00192 case Idle: 00193 g_state = Write; 00194 break; 00195 case Write: 00196 DEBUG("Writing data...") 00197 printf("RunStateMachine_WithByteValue: Write 0xaa\r\n"); 00198 if (!g_myEEPROM.Write(128, (unsigned char)0xaa)) { 00199 #ifdef __DEBUG 00200 DEBUG_FATAL("RunStateMachine_WithByteValue: write failed at address 128") 00201 #else // __DEBUG 00202 printf("RunStateMachine_WithByteValue: write failed at address 128\r\n"); 00203 #endif // __DEBUG 00204 g_state = Idle; 00205 } else { 00206 g_state = Written; 00207 } 00208 break; 00209 case Written: 00210 g_state = Read; 00211 break; 00212 case Read: { 00213 DEBUG("Reading datas...") 00214 unsigned char value = 0x00; 00215 if (!g_myEEPROM.Read(128, &value)) { 00216 #ifdef __DEBUG 00217 DEBUG_FATAL("RunStateMachine_WithByteValue: Read operation failed at address 128") 00218 #else // __DEBUG 00219 printf("RunStateMachine_WithByteValue: Read operation failed at address 128\r\n"); 00220 #endif // __DEBUG 00221 } else { 00222 printf("RunStateMachine_WithByteValue: Read '0x%02x'\r\n", value); 00223 } 00224 } 00225 g_state = Idle; 00226 break; 00227 default: 00228 printf("RunStateMachine_WithByteValue: Default!\r\n"); 00229 g_state = Idle; 00230 break; 00231 } 00232 00233 DEBUG_LEAVE("RunStateMachine_WithByteValue") 00234 } 00235 00236 void RunStateMachine_WithShortValue(C2424LCXX_I2C::Mode p_mode) { 00237 DEBUG_ENTER("RunStateMachine_WithShortValue") 00238 00239 switch (g_state) { 00240 case Idle: 00241 g_state = Write; 00242 break; 00243 case Write: 00244 DEBUG("Writing data...") 00245 printf("RunStateMachine_WithShortValue: Write 0xbeef\r\n"); 00246 if (!g_myEEPROM.Write(64, (short)0xbeef, p_mode)) { // See http://en.wikipedia.org/wiki/Hexspeak for more ideas on hexadecimal wording!!! 00247 DEBUG_FATAL("RunStateMachine_WithShortValue: write failed at address 64") 00248 g_state = Idle; 00249 } else { 00250 g_state = Written; 00251 } 00252 break; 00253 case Written: 00254 g_state = Read; 00255 break; 00256 case Read: { 00257 DEBUG("Reading datas..."); 00258 short value = 0; 00259 if (!g_myEEPROM.Read(64, &value, p_mode)) { 00260 #ifdef __DEBUG 00261 DEBUG_FATAL("RunStateMachine_WithShortValue: write failed at address 64") 00262 #else // __DEBUG 00263 printf("RunStateMachine_WithShortValue: write failed at address 64\r\n"); 00264 #endif // __DEBUG 00265 } else { 00266 printf("RunStateMachine_WithShortValue: Read '0x%04x' / '%hu'\r\n", value, (unsigned short)value); 00267 } 00268 } 00269 g_state = Idle; 00270 break; 00271 default: 00272 printf("RunStateMachine_WithShortValue: Default!\r\n"); 00273 g_state = Idle; 00274 break; 00275 } 00276 00277 DEBUG_LEAVE("RunStateMachine_WithShortValue") 00278 } 00279 00280 void RunStateMachine_WithIntegerValue(C2424LCXX_I2C::Mode p_mode) { 00281 DEBUG_ENTER("RunStateMachine_WithIntegerValue") 00282 00283 switch (g_state) { 00284 case Idle: 00285 g_state = Write; 00286 break; 00287 case Write: 00288 DEBUG("Writing data...") 00289 printf("RunStateMachine_WithIntegerValue: Write 0xdeaddead\r\n"); 00290 if (!g_myEEPROM.Write(32, (int)0xdeaddead, p_mode)) { 00291 DEBUG_FATAL("RunStateMachine_WithIntegerValue: write failed at address 32") 00292 g_state = Idle; 00293 } else { 00294 g_state = Written; 00295 } 00296 break; 00297 case Written: 00298 g_state = Read; 00299 break; 00300 case Read: { 00301 DEBUG("Reading datas...") 00302 int value = 0; 00303 if (!g_myEEPROM.Read(32, &value, p_mode)) { 00304 #ifdef __DEBUG 00305 DEBUG_FATAL("RunStateMachine_WithIntegerValue: write failed at address 32") 00306 #else // __DEBUG 00307 printf("RunStateMachine_WithIntegerValue: write failed at address 32\r\n"); 00308 #endif // __DEBUG 00309 } else { 00310 printf("RunStateMachine_WithIntegerValue: Read '0x%08x' / '%d'\r\n", value, (unsigned int)value); 00311 } 00312 } 00313 g_state = Idle; 00314 break; 00315 default: 00316 printf("RunStateMachine_WithIntegerValue: Default!\r\n"); 00317 g_state = Idle; 00318 break; 00319 } 00320 00321 DEBUG_LEAVE("RunStateMachine_WithIntegerValue") 00322 } 00323 00324 void RunStateMachine_WithStdVectorOfByteValue() { 00325 DEBUG_ENTER("RunStateMachine_WithStdVectorOfByteValue") 00326 00327 switch (g_state) { 00328 case Idle: 00329 g_state = Write; 00330 break; 00331 case Write: { 00332 std::vector<unsigned char> datas; 00333 datas.push_back(0xfe); 00334 datas.push_back(0xed); 00335 datas.push_back(0xfa); 00336 datas.push_back(0xce); 00337 DEBUG("Writing data...") 00338 printf("RunStateMachine_WithStdVectorOfByteValue: Write {0xfe, 0xed, 0xfa, 0xce}\r\n"); 00339 if (!g_myEEPROM.Write(16, datas, false)) { // Write the full buffer, not including the length indication 00340 DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 16") 00341 g_state = Idle; 00342 } else { 00343 g_state = Written; 00344 } 00345 } 00346 break; 00347 case Written: 00348 g_state = Read; 00349 break; 00350 case Read: { 00351 DEBUG("Reading datas...") 00352 std::vector<unsigned char> datas(4); 00353 if (!g_myEEPROM.Read(16, datas, false)) { // Read bytes, without the lenght indication, buffer size shall be set before the call 00354 #ifdef __DEBUG 00355 DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 16") 00356 #else // __DEBUG 00357 printf("RunStateMachine_WithStdVectorOfByteValue: write failed at address 16\r\n"); 00358 #endif // __DEBUG 00359 } else { 00360 printf("RunStateMachine_WithStdVectorOfByteValue: Read {%02x, %02x, %02x, %02x}\r\n", datas[0], datas[1], datas[2], datas[3]); 00361 } 00362 } 00363 g_state = Idle; 00364 break; 00365 default: 00366 printf("RunStateMachine_WithStdVectorOfByteValue: Default!\r\n"); 00367 g_state = Idle; 00368 break; 00369 } 00370 00371 DEBUG_LEAVE("RunStateMachine_WithStdVectorOfByteValue") 00372 } 00373 00374 void RunStateMachine_WithLengthPlusStdVectorOfByteValue() { 00375 DEBUG_ENTER("RunStateMachine_WithLengthPlusStdVectorOfByteValue") 00376 00377 switch (g_state) { 00378 case Idle: 00379 g_state = Write; 00380 break; 00381 case Write: { 00382 DEBUG("Writing data...") 00383 std::vector<unsigned char> datas; 00384 datas.push_back(0xde); 00385 datas.push_back(0x5e); 00386 datas.push_back(0xa5); 00387 datas.push_back(0xed); 00388 printf("RunStateMachine_WithLengthPlusStdVectorOfByteValue: Write {0xde, 0x5e, 0xa5, 0xed}\r\n"); 00389 if (!g_myEEPROM.Write(8, datas)) { // Write the full buffer, including the length indication 00390 DEBUG_FATAL("RunStateMachine_WithLengthPlusStdVectorOfByteValue: write failed at address 8") 00391 g_state = Idle; 00392 } else { 00393 g_state = Written; 00394 } 00395 } 00396 break; 00397 case Written: 00398 g_state = Read; 00399 break; 00400 case Read: { 00401 DEBUG("Reading datas...") 00402 std::vector<unsigned char> datas; 00403 if (!g_myEEPROM.Read(8, datas)) { // Read bytes, including the lenght indication, buffer size is not set before the call 00404 #ifdef __DEBUG 00405 DEBUG_FATAL("RunStateMachine_WithStdVectorOfByteValue: write failed at address 8") 00406 #else // __DEBUG 00407 printf("RunStateMachine_WithStdVectorOfByteValue: write failed at address 8\r\n"); 00408 #endif // __DEBUG 00409 } else { 00410 printf("RunStateMachine_WithLengthPlusStdVectorOfByteValue: Read bytes: "); 00411 vector<unsigned char>::iterator it; 00412 for (it = datas.begin() ; it < datas.end(); it++) { 00413 printf("0x%02x ", *it); 00414 } 00415 printf("\r\n"); 00416 } 00417 } 00418 g_state = Idle; 00419 break; 00420 default: 00421 printf("RunStateMachine_WithLengthPlusStdVectorOfByteValue: Default!\r\n"); 00422 g_state = Idle; 00423 break; 00424 } 00425 00426 DEBUG_LEAVE("RunStateMachine_WithLengthPlusStdVectorOfByteValue") 00427 }
Generated on Tue Jul 12 2022 22:20:40 by 1.7.2