nkjnm

Dependencies:   MAX44000 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 // Test configuration block
00025 namespace {
00026 const int ntests = 1000;
00027 const int i2c_freq_hz = 400000;
00028 const int i2c_delay_us = 0;
00029 // const int EEPROM_24LC256_SIZE = (256 * 1024 / 8);   // 256 kbit memory
00030 }
00031 
00032 // End of test configuration block
00033 
00034 #if defined(TARGET_KL25Z)
00035 I2C i2c(PTC9, PTC8);
00036 
00037 #elif defined(TARGET_KL27Z)
00038 I2C i2c(PTD6, PTD7);
00039 
00040 #elif defined(TARGET_KL46Z)
00041 I2C i2c(PTC9, PTC8);
00042 
00043 #elif defined(TARGET_K64F)
00044 I2C i2c(PTE25, PTE24);
00045 
00046 #elif defined(TARGET_K66F)
00047 I2C i2c(PTD9, PTD8);
00048 
00049 #elif defined(TARGET_K22F)
00050 I2C i2c(PTE0, PTE1);
00051 
00052 #elif defined(TARGET_K20D50M)
00053 I2C i2c(PTB3, PTB2);
00054 
00055 #elif defined(TARGET_LPC812)
00056 I2C i2c(P0_10, P0_11);
00057 
00058 #elif defined(TARGET_LPC1549)
00059 I2C i2c(P0_23, P0_22);
00060 
00061 #elif defined(TARGET_LPC11U68)
00062 I2C i2c(SDA, SCL);
00063 
00064 #elif defined(TARGET_DELTA_DFCM_NNN40)
00065 I2C i2c(I2C_SDA0, I2C_SCL0);
00066 
00067 #elif defined(TARGET_NUCLEO_F030R8) || \
00068       defined(TARGET_NUCLEO_F070RB) || \
00069       defined(TARGET_NUCLEO_F072RB) || \
00070       defined(TARGET_NUCLEO_F091RC) || \
00071       defined(TARGET_NUCLEO_F103RB) || \
00072       defined(TARGET_NUCLEO_F302R8) || \
00073       defined(TARGET_NUCLEO_F303RE) || \
00074       defined(TARGET_NUCLEO_F334R8) || \
00075       defined(TARGET_NUCLEO_F401RE) || \
00076       defined(TARGET_NUCLEO_F410RB) || \
00077       defined(TARGET_NUCLEO_F411RE) || \
00078       defined(TARGET_NUCLEO_L053R8) || \
00079       defined(TARGET_NUCLEO_L073RZ) || \
00080       defined(TARGET_NUCLEO_L152RE) || \
00081       defined(TARGET_FF_ARDUINO)    || \
00082       defined(TARGET_VK_RZ_A1H)
00083 I2C i2c(I2C_SDA, I2C_SCL);
00084 
00085 #else
00086 I2C i2c(p28, p27);
00087 #endif
00088 
00089 #define PATTERN_MASK 0x66, ~0x66, 0x00, 0xFF, 0xA5, 0x5A, 0xF0, 0x0F
00090 
00091 int main() {
00092     MBED_HOSTTEST_TIMEOUT(15);
00093     MBED_HOSTTEST_SELECT(default_auto);
00094     MBED_HOSTTEST_DESCRIPTION(I2C EEPROM line read write test);
00095     MBED_HOSTTEST_START("MBED_A25");
00096 
00097     const int EEPROM_MEM_ADDR = 0xA0;
00098     bool result = true;
00099 
00100     i2c.frequency(i2c_freq_hz);
00101     printf("I2C: I2C Frequency: %d Hz\r\n", i2c_freq_hz);
00102 
00103     printf("I2C: Lines pattern write test ... ");
00104     int write_errors = 0;
00105     for (int i = 0; i < ntests; i++) {
00106         char data[] = { 0 /*MSB*/, 0 /*LSB*/, PATTERN_MASK };
00107         const int addr = i * 8;   // 8 bytes of data in data array
00108         data[0] = ((0xFF00 & addr) >> 8) & 0x00FF;
00109         data[1] = (addr & 0x00FF);
00110 
00111         if (i2c.write(EEPROM_MEM_ADDR, data, sizeof(data)) != 0) {
00112             write_errors++;
00113         }
00114 
00115 #if defined(TARGET_VK_RZ_A1H)
00116         while (i2c.write(EEPROM_MEM_ADDR, data, 1)) ; // wait to complete
00117 #else
00118         while (i2c.write(EEPROM_MEM_ADDR, NULL, 0)) ; // wait to complete
00119 #endif
00120         // us delay if specified
00121         if (i2c_delay_us != 0) {
00122             wait_us(i2c_delay_us);
00123         }
00124     }
00125 
00126     printf("[%s]\r\n", write_errors ? "FAIL" : "OK");
00127     printf("I2C: Write errors: %d ... [%s]\r\n", write_errors, write_errors ? "FAIL" : "OK");
00128 
00129     printf("I2C: Lines pattern read test ... ");
00130     int read_errors = 0;
00131     int pattern_errors = 0;
00132     for (int i = 0; i < ntests; i++) {
00133         char data[8] = { 0 };   // General puspose buffer
00134         const int addr = i * 8; // 8 bytes of data in data array
00135         data[0] = ((0xFF00 & addr) >> 8) & 0x00FF;
00136         data[1] = (addr & 0x00FF);
00137 
00138         // Set address for read
00139         if (i2c.write(EEPROM_MEM_ADDR, data, 2, true) != 0) {
00140         }
00141 
00142         if (i2c.read(EEPROM_MEM_ADDR, data, 8) != 0) {
00143             read_errors++;
00144         }
00145 
00146         static char pattern[] = { PATTERN_MASK };
00147         if (memcmp(pattern, data, sizeof(data))) {
00148             pattern_errors++;
00149         }
00150     }
00151 
00152     printf("[%s]\r\n", read_errors ? "FAIL" : "OK");
00153     printf("I2C: Read errors: %d/%d ... [%s]\r\n", read_errors, ntests, read_errors ? "FAIL" : "OK");
00154     printf("EEPROM: Pattern match errors: %d/%d ... [%s]\r\n", pattern_errors, ntests, pattern_errors ? "FAIL" : "OK");
00155 
00156     result = write_errors == 0 && read_errors == 0;
00157     MBED_HOSTTEST_RESULT(result);
00158 }