WORKS

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "test_env.h"
00002 
00003 /******************************************************************************
00004 *  This will test an I2C EEPROM connected to mbed by writing a predefined byte at
00005 *  address 0 and then reading it back and comparing it with the known byte value a
00006 *  number of times. This test was written specifically for reproducing the bug
00007 *  reported here:
00008 *
00009 *  https://mbed.org/forum/bugs-suggestions/topic/4128/
00010 *
00011 *  Test configuration:
00012 *
00013 * set 'ntests' to the number of iterations
00014 * set 'i2c_speed_hz' to the desired speed of the I2C interface
00015 * set 'i2c_delay_us' to the delay that will be inserted between 'write' and
00016 *  'read' I2C operations (https://mbed.org/users/mbed_official/code/mbed/issues/1
00017 *  for more details). '0' disables the delay.
00018 * define I2C_EEPROM_VERBOSE to get verbose output
00019 *
00020 *  The test ran with a 24LC256 external EEPROM memory, but any I2C EEPROM memory
00021 *  that uses two byte addresses should work.
00022 ******************************************************************************/
00023 
00024 #if defined(TARGET_KL25Z)
00025 I2C i2c(PTC9, PTC8);
00026 
00027 #elif defined(TARGET_KL27Z)
00028 I2C i2c(PTD6, PTD7);
00029 
00030 #elif defined(TARGET_KL46Z)
00031 I2C i2c(PTC9, PTC8);
00032 
00033 #elif defined(TARGET_K64F)
00034 I2C i2c(PTE25, PTE24);
00035 
00036 #elif defined(TARGET_K66F)
00037 I2C i2c(PTD9, PTD8);
00038 
00039 #elif defined(TARGET_K22F)
00040 I2C i2c(PTE0, PTE1);
00041 
00042 #elif defined(TARGET_K20D50M)
00043 I2C i2c(PTB3, PTB2);
00044 
00045 #elif defined(TARGET_LPC812)
00046 I2C i2c(P0_10, P0_11);
00047 
00048 #elif defined(TARGET_LPC1549)
00049 I2C i2c(P0_23, P0_22);
00050 
00051 #elif defined(TARGET_LPC11U68)
00052 I2C i2c(SDA, SCL);
00053 
00054 #elif defined(TARGET_DELTA_DFCM_NNN40)
00055 I2C i2c(I2C_SDA0, I2C_SCL0);
00056 
00057 #elif defined(TARGET_NUCLEO_F030R8) || \
00058       defined(TARGET_NUCLEO_F070RB) || \
00059       defined(TARGET_NUCLEO_F072RB) || \
00060       defined(TARGET_NUCLEO_F091RC) || \
00061       defined(TARGET_NUCLEO_F103RB) || \
00062       defined(TARGET_NUCLEO_F302R8) || \
00063       defined(TARGET_NUCLEO_F303RE) || \
00064       defined(TARGET_NUCLEO_F334R8) || \
00065       defined(TARGET_NUCLEO_F401RE) || \
00066       defined(TARGET_NUCLEO_F410RB) || \
00067       defined(TARGET_NUCLEO_F411RE) || \
00068       defined(TARGET_NUCLEO_L053R8) || \
00069       defined(TARGET_NUCLEO_L073RZ) || \
00070       defined(TARGET_NUCLEO_L152RE) || \
00071       defined(TARGET_FF_ARDUINO)    || \
00072       defined(TARGET_VK_RZ_A1H)
00073 I2C i2c(I2C_SDA, I2C_SCL);
00074 
00075 #else
00076 I2C i2c(p28, p27);
00077 #endif
00078 
00079 namespace {
00080 const int ntests = 10000;
00081 const int i2c_freq_hz = 400000;
00082 const int i2c_delay_us = 0;
00083 }
00084 
00085 int main() {
00086     MBED_HOSTTEST_TIMEOUT(15);
00087     MBED_HOSTTEST_SELECT(default_auto);
00088     MBED_HOSTTEST_DESCRIPTION(I2C EEPROM read write test);
00089     MBED_HOSTTEST_START("MBED_A19");
00090 
00091     const int EEPROM_MEM_ADDR = 0xA0;
00092     const char MARK = 0x66;
00093     int fw = 0;
00094     int fr = 0;
00095     int fc = 0;
00096     int i2c_stat = 0;
00097     bool result = true;
00098 
00099     i2c.frequency(i2c_freq_hz);
00100     printf("I2C: I2C Frequency: %d Hz\r\n", i2c_freq_hz);
00101 
00102     printf("I2C: Write 0x%2X at address 0x0000 test ... \r\n", MARK);
00103     // Data write
00104     {
00105         char data[] = { 0, 0, MARK };
00106         if ((i2c_stat = i2c.write(EEPROM_MEM_ADDR, data, sizeof(data))) != 0) {
00107             printf("Unable to write data to EEPROM (i2c_stat = 0x%02X), aborting\r\n", i2c_stat);
00108             notify_completion(false);
00109             return 1;
00110         }
00111 
00112         // ACK polling (assumes write will be successful eventually)
00113         while (i2c.write(EEPROM_MEM_ADDR, data, 0) != 0)
00114             ;
00115     }
00116 
00117     printf("I2C: Read data from address 0x0000 test ... \r\n");
00118     // Data read (actual test)
00119     for (int i = 0; i < ntests; i++) {
00120         // Write data to EEPROM memory
00121         {
00122             char data[] = { 0, 0 };
00123             if ((i2c_stat = i2c.write(EEPROM_MEM_ADDR, data, 2, true)) != 0) {
00124                 printf("Test %d failed at write, i2c_stat is 0x%02X\r\n", i, i2c_stat);
00125                 fw++;
00126                 continue;
00127             }
00128         }
00129 
00130         // us delay if specified
00131         if (i2c_delay_us != 0)
00132             wait_us(i2c_delay_us);
00133 
00134         // Read data
00135         {
00136             char data[1] = { 0 };
00137             if ((i2c_stat = i2c.read(EEPROM_MEM_ADDR, data, 1)) != 0) {
00138                 printf("Test %d failed at read, i2c_stat is 0x%02X\r\n", i, i2c_stat);
00139                 fr++;
00140                 continue;
00141             }
00142 
00143             if (data[0] != MARK) {
00144                 printf("Test %d failed at data match\r\n", i);
00145                 fc++;
00146             }
00147         }
00148     }
00149 
00150     result = (fw + fr + fc == 0);
00151     printf("EEPROM: Test result ... [%s]\r\n", result ? "OK" : "FAIL");
00152 
00153     if (!result) {
00154         printf("Test Statistics:\r\n");
00155         printf("\tTotal tests:     %d\r\n", ntests);
00156         printf("\tFailed at write: %d\r\n", fw);
00157         printf("\tFailed at read:  %d\r\n", fr);
00158         printf("\tData mismatch:   %d\r\n", fc);
00159         printf("\tTotal failures:  %d\r\n", fw + fr + fc);
00160     }
00161 
00162     MBED_HOSTTEST_RESULT(result);
00163 }