Rtos API example

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