Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXOS8700CQ HIH6130 IS31SE5000 MAG3110 MAX44000 MAX44005 MAX44008 MMA8451Q MMA8452Q MPL3115A2 VEML6040 VEML6075 mbed vt100 LM75B FXAS21002 MAX30101 VCNL4020 VCNL4100
testSensor.cpp
00001 /** testSensor 00002 * I2C and I2C sensor test program 00003 * For the initial release, 00004 * following MSU sensors are supported 00005 * FXOS8700CQ 00006 * HIH6130 00007 * MAG3110 00008 * MMA8451Q 00009 * MMA8452Q 00010 * MPL3115A2 00011 * MAX44000 00012 * MAX44005 (not tested) 00013 * MAX44008 00014 * 00015 * For untested sensor(s) or I2C devices 00016 * open / close / read / write 00017 * commands are avilable 00018 * please refer to the help message and source code 00019 * for details. 00020 */ 00021 #include "mbed.h" 00022 #include <string.h> 00023 #include <ctype.h> 00024 #include "vt100.h" 00025 #include "MSS.h" 00026 #include "MSU.h" 00027 #include "dumb_i2c.h" 00028 #define TEST_LOOP 10 00029 00030 DUMB_I2C *i2c = 0 ; 00031 vt100 *tty = 0 ; 00032 int test_loop = TEST_LOOP ; 00033 int interval = 100 ; /* ms wait interval */ 00034 00035 void doSWReset(void) 00036 { 00037 printf("Software Reset\n") ; 00038 SCB->AIRCR = 0x05FA0004 ; 00039 } 00040 00041 void doClose(void) 00042 { 00043 if (i2c != 0) { 00044 printf("Closing I2C at 0x%02X ... ", i2c->address()) ; 00045 delete i2c ; 00046 i2c = 0 ; 00047 printf("Done\n") ; 00048 } 00049 } 00050 00051 void doOpen(void) 00052 { 00053 uint8_t address ; 00054 scanf("%X", &address) ; 00055 if (i2c != 0) { 00056 doClose() ; 00057 } 00058 printf("Opening I2C at 0x%02X ... ", address) ; 00059 i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ; 00060 printf("Done\n") ; 00061 } 00062 00063 void doRead(void) 00064 { 00065 int addr ; 00066 int len ; 00067 uint8_t *data ; 00068 int result ; 00069 if (i2c == 0) { 00070 printf("I2C is not opened\n") ; 00071 return ; 00072 } 00073 scanf("%X %X", &addr, &len) ; 00074 if (len > 0) { 00075 data = new uint8_t[len] ; 00076 } 00077 // i2c->read(addr, data, len) ; 00078 printf("0x%02X : ", (unsigned int)addr) ; 00079 for (int i = 0 ; i < len ; i++ ) { 00080 result = i2c->read(addr+i, &data[i], 1) ; 00081 if (result == 0) { 00082 printf("%02X ", data[i]) ; 00083 if (((i+1) < len)&&(((i+1)%0x10) == 0)) { 00084 printf("\n") ; 00085 printf("0x%02X : ", (unsigned int)(addr + i + 1)) ; 00086 } 00087 } else { 00088 printf("failed to read 0x%02X\n", addr+i) ; 00089 } 00090 } 00091 printf("\n") ; 00092 } 00093 00094 void doWrite(void) 00095 { 00096 int addr ; 00097 uint8_t len ; 00098 uint8_t data ; 00099 int ack ; 00100 if (i2c == 0) { 00101 printf("I2C is not opened\n") ; 00102 return ; 00103 } 00104 scanf("%X %X", &addr, &data) ; 00105 ack = i2c->write(addr, &data, 1) ; 00106 if (ack != 0) { 00107 printf("NAK at writing data[0x%02X] to address[0x%02X]\n", 00108 data, addr ) ; 00109 } 00110 } 00111 00112 void doStatus(void) 00113 { 00114 if (i2c == 0) { 00115 printf("i2c is not opened\n") ; 00116 } else { 00117 printf("i2c device at 0x%02X is opened at %d Hz\n", 00118 i2c->address(), i2c->frequency()) ; 00119 } 00120 printf("test loop number = %d\n", test_loop) ; 00121 printf("loop interval = %d ms\n", interval) ; 00122 } 00123 00124 void doHelp(void) 00125 { 00126 printf("Simple I2C test program %s for %s\n", __DATE__, BOARD_NAME) ; 00127 printf("\n === usage ===\n") ; 00128 printf("open <7bit addr> : open i2c device at <7bit addr>\n") ; 00129 printf("close : close currently opened i2c\n") ; 00130 printf("read <addr> <len> : read <len> data from <addr>\n") ; 00131 printf("write <addr> <data> : write <data> to register <addr>\n") ; 00132 printf("frequency <freq> : change frequency to <freq> Hz\n") ; 00133 printf("bus : bus scan for existing I2C addresses\n") ; 00134 printf("test <sensor or address> : test a sensor\n") ; 00135 printf("loop <number> : specify loop count for test\n") ; 00136 printf("interval <numver> : ms interval for each loop\n") ; 00137 printf("status : print current status\n") ; 00138 printf("demo : mulitple sensor demo\n") ; 00139 printf("help : print this help\n") ; 00140 printf("zzz : cause software reset\n") ; 00141 printf("\nPlease set local-echo to see what you are typing.\n") ; 00142 printf("\n") ; 00143 } 00144 00145 void doFreq(void) 00146 { 00147 int freq = 0 ; 00148 scanf("%d", &freq) ; 00149 if (i2c != 0) { 00150 i2c->frequency(freq) ; 00151 } 00152 } 00153 00154 00155 void print_sensor_name(int address) 00156 { 00157 int i ; 00158 for(i = 0; i2c_sensor[i].address != 0 ; i++) { 00159 if (i2c_sensor[i].address == address) { 00160 printf("%s ", i2c_sensor[i].name) ; 00161 } 00162 } 00163 } 00164 00165 void doBusScan(void) 00166 { 00167 int address ; 00168 uint8_t data[10] ; 00169 int num_data = 1 ; 00170 int result ; 00171 if (i2c != 0) { 00172 printf("Closing I2C at 0x%02X ... ", i2c->address()) ; 00173 delete i2c ; 00174 i2c = 0 ; 00175 printf("Done\n") ; 00176 } 00177 00178 for (address = 1; address < 127 ; address++) { 00179 i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ; 00180 num_data = 2 ; 00181 result = i2c->read(0, data, num_data) ; 00182 /* 00183 data[0] = address << 1 ; 00184 i2c->start() ; 00185 result = i2c->write(0, data, 1) ; 00186 i2c->stop() ; 00187 */ 00188 if (result == 0) { 00189 printf("%02X : ", address) ; 00190 print_sensor_name(address) ; 00191 printf("\n") ; 00192 } 00193 delete i2c ; 00194 i2c = 0 ; 00195 } 00196 printf("\n") ; 00197 } 00198 00199 void setTestLoop(void) 00200 { 00201 int num ; 00202 scanf("%d", &num) ; 00203 if (num < 0) { num = 1 ; } 00204 test_loop = num ; 00205 printf("test loop count set to %d\n", test_loop) ; 00206 } 00207 00208 void setTestInterval(void) 00209 { 00210 int num ; 00211 scanf("%d", &num) ; 00212 if (num < 0) num = 100 ; 00213 interval = num ; 00214 printf("wait %d ms for each loop\n", interval) ; 00215 } 00216 00217 void str2upper(char *str) 00218 { 00219 while(str && *str) { 00220 if (('a' <= *str) && (*str <= 'z')) { 00221 *str -= 'a' - 'A' ; 00222 } 00223 str++ ; 00224 } 00225 } 00226 00227 void doTestSensor(void) 00228 { 00229 int i ; 00230 int address = 0 ; 00231 char name[32] ; 00232 scanf("%s", name) ; 00233 str2upper(name) ; 00234 if (('0' <= *name)&&(*name <= '9')) { /* assume it's the address */ 00235 sscanf(name, "%X", &address) ; 00236 for (i = 0 ; i < *(i2c_sensor[i].name) != 0 ; i++ ) { 00237 if (i2c_sensor[i].address == address) { 00238 strcpy(name, i2c_sensor[i].name) ; 00239 break ; 00240 } 00241 } 00242 } else { 00243 for (i = 0 ; i2c_sensor[i].address != 0 ; i++) { 00244 if (strcmp(name, i2c_sensor[i].name) == 0) { /* found */ 00245 break ; 00246 } 00247 } 00248 } 00249 if (i2c_sensor[i].name != 0) { /* name found */ 00250 i2c_sensor[i].test_func() ; 00251 } 00252 00253 } 00254 00255 void doCommand(char *str) 00256 { 00257 switch(*str) { 00258 case 'o': case 'O': /* open */ 00259 doOpen() ; break ; 00260 case 'c': case 'C': /* close */ 00261 doClose() ; break ; 00262 case 'd': case 'D': /* demo */ 00263 doDemo() ; break ; 00264 case 'r': case 'R': /* read */ 00265 doRead() ; break ; 00266 case 'w': case 'W': /* write */ 00267 doWrite() ; break ; 00268 case 's': case 'S': /* status */ 00269 doStatus() ; break ; 00270 case 'f': case 'F': /* Frequency */ 00271 doFreq() ; break ; 00272 case 't': case 'T': /* test sensor */ 00273 doTestSensor() ; break ; 00274 case 'l': case 'L': /* set test_loop */ 00275 setTestLoop() ; break ; 00276 case 'i': case 'I': /* set interval */ 00277 setTestInterval() ; break ; 00278 case 'b': case 'B': /* Bus Scan */ 00279 doBusScan() ; break ; 00280 case 'z': case 'Z': /* Software Reset */ 00281 doSWReset() ; break ; 00282 default: 00283 doHelp() ; break ; 00284 } 00285 } 00286 00287 int main() { 00288 char cmd[32] ; 00289 uint32_t baud = 115200 ; 00290 #if defined (TARGET_MAX32600MBED) 00291 baud = 57600 ; 00292 #endif 00293 tty = new vt100(baud) ; 00294 tty->cls() ; 00295 doHelp() ; 00296 while(1) { 00297 printf("> ") ; 00298 scanf("%s", cmd) ; 00299 doCommand(cmd) ; 00300 } 00301 }
Generated on Fri Jul 15 2022 06:46:16 by
1.7.2