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 #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_KL43Z)
00034 I2C i2c(PTE0, PTE1);
00035 
00036 #elif defined(TARGET_KL82Z)
00037 I2C i2c(PTC11, PTC10);
00038 
00039 #elif defined(TARGET_K64F)
00040 I2C i2c(PTE25, PTE24);
00041 
00042 #elif defined(TARGET_K66F)
00043 I2C i2c(PTD9, PTD8);
00044 
00045 #elif defined(TARGET_K22F)
00046 I2C i2c(PTE0, PTE1);
00047 
00048 #elif defined(TARGET_K20D50M)
00049 I2C i2c(PTB3, PTB2);
00050 
00051 #elif defined(TARGET_LPC812)
00052 I2C i2c(P0_10, P0_11);
00053 
00054 #elif defined(TARGET_LPC1549)
00055 I2C i2c(P0_23, P0_22);
00056 
00057 #elif defined(TARGET_LPC11U68)
00058 I2C i2c(SDA, SCL);
00059 
00060 #elif defined(TARGET_DELTA_DFCM_NNN40)
00061 I2C i2c(I2C_SDA0, I2C_SCL0);
00062 
00063 #elif defined(TARGET_NUCLEO_F030R8) || \
00064       defined(TARGET_NUCLEO_F070RB) || \
00065       defined(TARGET_NUCLEO_F072RB) || \
00066       defined(TARGET_NUCLEO_F091RC) || \
00067       defined(TARGET_NUCLEO_F103RB) || \
00068       defined(TARGET_NUCLEO_F302R8) || \
00069       defined(TARGET_NUCLEO_F303RE) || \
00070       defined(TARGET_NUCLEO_F334R8) || \
00071       defined(TARGET_NUCLEO_F401RE) || \
00072       defined(TARGET_NUCLEO_F410RB) || \
00073       defined(TARGET_NUCLEO_F411RE) || \
00074       defined(TARGET_NUCLEO_L053R8) || \
00075       defined(TARGET_NUCLEO_L073RZ) || \
00076       defined(TARGET_NUCLEO_L152RE) || \
00077       defined(TARGET_FF_ARDUINO)    || \
00078       defined(TARGET_VK_RZ_A1H)
00079 I2C i2c(I2C_SDA, I2C_SCL);
00080 
00081 #else
00082 I2C i2c(p28, p27);
00083 #endif
00084 
00085 namespace {
00086 const int ntests = 10000;
00087 const int i2c_freq_hz = 400000;
00088 const int i2c_delay_us = 0;
00089 }
00090 
00091 int main() {
00092     MBED_HOSTTEST_TIMEOUT(15);
00093     MBED_HOSTTEST_SELECT(default_auto);
00094     MBED_HOSTTEST_DESCRIPTION(I2C EEPROM read write test);
00095     MBED_HOSTTEST_START("MBED_A19");
00096 
00097     const int EEPROM_MEM_ADDR = 0xA0;
00098     const char MARK = 0x66;
00099     int fw = 0;
00100     int fr = 0;
00101     int fc = 0;
00102     int i2c_stat = 0;
00103     bool result = true;
00104 
00105     i2c.frequency(i2c_freq_hz);
00106     printf("I2C: I2C Frequency: %d Hz\r\n", i2c_freq_hz);
00107 
00108     printf("I2C: Write 0x%2X at address 0x0000 test ... \r\n", MARK);
00109     // Data write
00110     {
00111         char data[] = { 0, 0, MARK };
00112         if ((i2c_stat = i2c.write(EEPROM_MEM_ADDR, data, sizeof(data))) != 0) {
00113             printf("Unable to write data to EEPROM (i2c_stat = 0x%02X), aborting\r\n", i2c_stat);
00114             notify_completion(false);
00115             return 1;
00116         }
00117 
00118         // ACK polling (assumes write will be successful eventually)
00119         while (i2c.write(EEPROM_MEM_ADDR, data, 0) != 0)
00120             ;
00121     }
00122 
00123     printf("I2C: Read data from address 0x0000 test ... \r\n");
00124     // Data read (actual test)
00125     for (int i = 0; i < ntests; i++) {
00126         // Write data to EEPROM memory
00127         {
00128             char data[] = { 0, 0 };
00129             if ((i2c_stat = i2c.write(EEPROM_MEM_ADDR, data, 2, true)) != 0) {
00130                 printf("Test %d failed at write, i2c_stat is 0x%02X\r\n", i, i2c_stat);
00131                 fw++;
00132                 continue;
00133             }
00134         }
00135 
00136         // us delay if specified
00137         if (i2c_delay_us != 0)
00138             wait_us(i2c_delay_us);
00139 
00140         // Read data
00141         {
00142             char data[1] = { 0 };
00143             if ((i2c_stat = i2c.read(EEPROM_MEM_ADDR, data, 1)) != 0) {
00144                 printf("Test %d failed at read, i2c_stat is 0x%02X\r\n", i, i2c_stat);
00145                 fr++;
00146                 continue;
00147             }
00148 
00149             if (data[0] != MARK) {
00150                 printf("Test %d failed at data match\r\n", i);
00151                 fc++;
00152             }
00153         }
00154     }
00155 
00156     result = (fw + fr + fc == 0);
00157     printf("EEPROM: Test result ... [%s]\r\n", result ? "OK" : "FAIL");
00158 
00159     if (!result) {
00160         printf("Test Statistics:\r\n");
00161         printf("\tTotal tests:     %d\r\n", ntests);
00162         printf("\tFailed at write: %d\r\n", fw);
00163         printf("\tFailed at read:  %d\r\n", fr);
00164         printf("\tData mismatch:   %d\r\n", fc);
00165         printf("\tTotal failures:  %d\r\n", fw + fr + fc);
00166     }
00167 
00168     MBED_HOSTTEST_RESULT(result);
00169 }