I2C sensor test program, derived from testI2C program. Simple test for FXOS8700CQ, HIH6130, MAG3110, MMA8451Q, MMA8452Q, MPL3115A2, MAX44000, MAX44005, MAX44008, MAX30101 included beside simple I2C read/write from testI2C.

Dependencies:   FXOS8700CQ HIH6130 IS31SE5000 MAG3110 MAX44000 MAX44005 MAX44008 MMA8451Q MMA8452Q MPL3115A2 VEML6040 VEML6075 mbed vt100 LM75B FXAS21002 MAX30101 VCNL4020 VCNL4100

Committer:
Rhyme
Date:
Tue Apr 26 05:39:37 2016 +0000
Revision:
1:9450e20cf688
Child:
4:c10b1aa9925c
Commit before initial publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 1:9450e20cf688 1 /** testSensor
Rhyme 1:9450e20cf688 2 * I2C and I2C sensor test program
Rhyme 1:9450e20cf688 3 * For the initial release,
Rhyme 1:9450e20cf688 4 * following MSU sensors are supported
Rhyme 1:9450e20cf688 5 * FXOS8700CQ
Rhyme 1:9450e20cf688 6 * HIH6130
Rhyme 1:9450e20cf688 7 * MAG3110
Rhyme 1:9450e20cf688 8 * MMA8451Q
Rhyme 1:9450e20cf688 9 * MMA8452Q
Rhyme 1:9450e20cf688 10 * MPL3115A2
Rhyme 1:9450e20cf688 11 * MAX44000
Rhyme 1:9450e20cf688 12 * MAX44005 (not tested)
Rhyme 1:9450e20cf688 13 * MAX44008
Rhyme 1:9450e20cf688 14 *
Rhyme 1:9450e20cf688 15 * For untested sensor(s) or I2C devices
Rhyme 1:9450e20cf688 16 * open / close / read / write
Rhyme 1:9450e20cf688 17 * commands are avilable
Rhyme 1:9450e20cf688 18 * please refer to the help message and source code
Rhyme 1:9450e20cf688 19 * for details.
Rhyme 1:9450e20cf688 20 */
Rhyme 1:9450e20cf688 21 #include "mbed.h"
Rhyme 1:9450e20cf688 22 #include <string.h>
Rhyme 1:9450e20cf688 23 #include <ctype.h>
Rhyme 1:9450e20cf688 24 #include "vt100.h"
Rhyme 1:9450e20cf688 25 #include "MSS.h"
Rhyme 1:9450e20cf688 26 #include "MSU.h"
Rhyme 1:9450e20cf688 27 #include "dumb_i2c.h"
Rhyme 1:9450e20cf688 28 #define TEST_LOOP 10
Rhyme 1:9450e20cf688 29
Rhyme 1:9450e20cf688 30 DUMB_I2C *i2c = 0 ;
Rhyme 1:9450e20cf688 31 vt100 *tty = 0 ;
Rhyme 1:9450e20cf688 32 int test_loop = TEST_LOOP ;
Rhyme 1:9450e20cf688 33
Rhyme 1:9450e20cf688 34 void doClose(void)
Rhyme 1:9450e20cf688 35 {
Rhyme 1:9450e20cf688 36 if (i2c != 0) {
Rhyme 1:9450e20cf688 37 printf("Closing I2C at 0x%02X ... ", i2c->address()) ;
Rhyme 1:9450e20cf688 38 delete i2c ;
Rhyme 1:9450e20cf688 39 i2c = 0 ;
Rhyme 1:9450e20cf688 40 printf("Done\n") ;
Rhyme 1:9450e20cf688 41 }
Rhyme 1:9450e20cf688 42 }
Rhyme 1:9450e20cf688 43
Rhyme 1:9450e20cf688 44 void doOpen(void)
Rhyme 1:9450e20cf688 45 {
Rhyme 1:9450e20cf688 46 uint8_t address ;
Rhyme 1:9450e20cf688 47 scanf("%X", &address) ;
Rhyme 1:9450e20cf688 48 if (i2c != 0) {
Rhyme 1:9450e20cf688 49 doClose() ;
Rhyme 1:9450e20cf688 50 }
Rhyme 1:9450e20cf688 51 printf("Opening I2C at 0x%02X ... ", address) ;
Rhyme 1:9450e20cf688 52 i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ;
Rhyme 1:9450e20cf688 53 printf("Done\n") ;
Rhyme 1:9450e20cf688 54 }
Rhyme 1:9450e20cf688 55
Rhyme 1:9450e20cf688 56 void doRead(void)
Rhyme 1:9450e20cf688 57 {
Rhyme 1:9450e20cf688 58 int addr ;
Rhyme 1:9450e20cf688 59 int len ;
Rhyme 1:9450e20cf688 60 uint8_t *data ;
Rhyme 1:9450e20cf688 61 int result ;
Rhyme 1:9450e20cf688 62 if (i2c == 0) {
Rhyme 1:9450e20cf688 63 printf("I2C is not opened\n") ;
Rhyme 1:9450e20cf688 64 return ;
Rhyme 1:9450e20cf688 65 }
Rhyme 1:9450e20cf688 66 scanf("%X %X", &addr, &len) ;
Rhyme 1:9450e20cf688 67 if (len > 0) {
Rhyme 1:9450e20cf688 68 data = new uint8_t[len] ;
Rhyme 1:9450e20cf688 69 }
Rhyme 1:9450e20cf688 70 // i2c->read(addr, data, len) ;
Rhyme 1:9450e20cf688 71 printf("0x%02X : ", (unsigned int)addr) ;
Rhyme 1:9450e20cf688 72 for (int i = 0 ; i < len ; i++ ) {
Rhyme 1:9450e20cf688 73 result = i2c->read(addr+i, &data[i], 1) ;
Rhyme 1:9450e20cf688 74 if (result == 0) {
Rhyme 1:9450e20cf688 75 printf("%02X ", data[i]) ;
Rhyme 1:9450e20cf688 76 if (((i+1) < len)&&(((i+1)%0x10) == 0)) {
Rhyme 1:9450e20cf688 77 printf("\n") ;
Rhyme 1:9450e20cf688 78 printf("0x%02X : ", (unsigned int)(addr + i + 1)) ;
Rhyme 1:9450e20cf688 79 }
Rhyme 1:9450e20cf688 80 } else {
Rhyme 1:9450e20cf688 81 printf("failed to read 0x%02X\n", addr+i) ;
Rhyme 1:9450e20cf688 82 }
Rhyme 1:9450e20cf688 83 }
Rhyme 1:9450e20cf688 84 printf("\n") ;
Rhyme 1:9450e20cf688 85 }
Rhyme 1:9450e20cf688 86
Rhyme 1:9450e20cf688 87 void doWrite(void)
Rhyme 1:9450e20cf688 88 {
Rhyme 1:9450e20cf688 89 int addr ;
Rhyme 1:9450e20cf688 90 uint8_t len ;
Rhyme 1:9450e20cf688 91 uint8_t data ;
Rhyme 1:9450e20cf688 92 int ack ;
Rhyme 1:9450e20cf688 93 if (i2c == 0) {
Rhyme 1:9450e20cf688 94 printf("I2C is not opened\n") ;
Rhyme 1:9450e20cf688 95 return ;
Rhyme 1:9450e20cf688 96 }
Rhyme 1:9450e20cf688 97 scanf("%X %X", &addr, &data) ;
Rhyme 1:9450e20cf688 98 ack = i2c->write(addr, &data, 1) ;
Rhyme 1:9450e20cf688 99 if (ack != 0) {
Rhyme 1:9450e20cf688 100 printf("NAK at writing data[0x%02X] to address[0x%02X]\n",
Rhyme 1:9450e20cf688 101 data, addr ) ;
Rhyme 1:9450e20cf688 102 }
Rhyme 1:9450e20cf688 103 }
Rhyme 1:9450e20cf688 104
Rhyme 1:9450e20cf688 105 void doStatus(void)
Rhyme 1:9450e20cf688 106 {
Rhyme 1:9450e20cf688 107 if (i2c == 0) {
Rhyme 1:9450e20cf688 108 printf("i2c is not opened\n") ;
Rhyme 1:9450e20cf688 109 } else {
Rhyme 1:9450e20cf688 110 printf("i2c device at 0x%02X is opened at %d Hz\n",
Rhyme 1:9450e20cf688 111 i2c->address(), i2c->frequency()) ;
Rhyme 1:9450e20cf688 112 }
Rhyme 1:9450e20cf688 113 }
Rhyme 1:9450e20cf688 114
Rhyme 1:9450e20cf688 115 void doHelp(void)
Rhyme 1:9450e20cf688 116 {
Rhyme 1:9450e20cf688 117 printf("Simple I2C test program %s for %s\n", __DATE__, BOARD_NAME) ;
Rhyme 1:9450e20cf688 118 printf("\n === usage ===\n") ;
Rhyme 1:9450e20cf688 119 printf("open <7bit addr> : open i2c device at <7bit addr>\n") ;
Rhyme 1:9450e20cf688 120 printf("close : close currently opened i2c\n") ;
Rhyme 1:9450e20cf688 121 printf("read <addr> <len> : read <len> data from <addr>\n") ;
Rhyme 1:9450e20cf688 122 printf("write <addr> <data> : write <data> to register <addr>\n") ;
Rhyme 1:9450e20cf688 123 printf("frequency <freq> : change frequency to <freq> Hz\n") ;
Rhyme 1:9450e20cf688 124 printf("bus : bus scan for existing I2C addresses\n") ;
Rhyme 1:9450e20cf688 125 printf("test <sensor> : test a sensor\n") ;
Rhyme 1:9450e20cf688 126 printf("loop <number> : specify loop count for test\n") ;
Rhyme 1:9450e20cf688 127 printf("status : print current status\n") ;
Rhyme 1:9450e20cf688 128 printf("help : print this help\n") ;
Rhyme 1:9450e20cf688 129 printf("\nPlease set local-echo to see what you are typing.\n") ;
Rhyme 1:9450e20cf688 130 printf("\n") ;
Rhyme 1:9450e20cf688 131 }
Rhyme 1:9450e20cf688 132
Rhyme 1:9450e20cf688 133 void doFreq(void)
Rhyme 1:9450e20cf688 134 {
Rhyme 1:9450e20cf688 135 int freq = 0 ;
Rhyme 1:9450e20cf688 136 scanf("%d", &freq) ;
Rhyme 1:9450e20cf688 137 if (i2c != 0) {
Rhyme 1:9450e20cf688 138 i2c->frequency(freq) ;
Rhyme 1:9450e20cf688 139 }
Rhyme 1:9450e20cf688 140 }
Rhyme 1:9450e20cf688 141
Rhyme 1:9450e20cf688 142
Rhyme 1:9450e20cf688 143 void print_sensor_name(int address)
Rhyme 1:9450e20cf688 144 {
Rhyme 1:9450e20cf688 145 int i ;
Rhyme 1:9450e20cf688 146 for(i = 0; i2c_sensor[i].address != 0 ; i++) {
Rhyme 1:9450e20cf688 147 if (i2c_sensor[i].address == address) {
Rhyme 1:9450e20cf688 148 printf("%s ", i2c_sensor[i].name) ;
Rhyme 1:9450e20cf688 149 }
Rhyme 1:9450e20cf688 150 }
Rhyme 1:9450e20cf688 151 }
Rhyme 1:9450e20cf688 152
Rhyme 1:9450e20cf688 153 void doBusScan(void)
Rhyme 1:9450e20cf688 154 {
Rhyme 1:9450e20cf688 155 int address ;
Rhyme 1:9450e20cf688 156 uint8_t data ;
Rhyme 1:9450e20cf688 157 int result ;
Rhyme 1:9450e20cf688 158 if (i2c != 0) {
Rhyme 1:9450e20cf688 159 printf("Closing I2C at 0x%02X ... ", i2c->address()) ;
Rhyme 1:9450e20cf688 160 delete i2c ;
Rhyme 1:9450e20cf688 161 i2c = 0 ;
Rhyme 1:9450e20cf688 162 printf("Done\n") ;
Rhyme 1:9450e20cf688 163 }
Rhyme 1:9450e20cf688 164
Rhyme 1:9450e20cf688 165 for (address = 1; address < 127 ; address++) {
Rhyme 1:9450e20cf688 166 i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ;
Rhyme 1:9450e20cf688 167 result = i2c->read(address, &data, 1) ;
Rhyme 1:9450e20cf688 168 if (result == 0) {
Rhyme 1:9450e20cf688 169 printf("%02X : ", address) ;
Rhyme 1:9450e20cf688 170 print_sensor_name(address) ;
Rhyme 1:9450e20cf688 171 printf("\n") ;
Rhyme 1:9450e20cf688 172 }
Rhyme 1:9450e20cf688 173 delete i2c ;
Rhyme 1:9450e20cf688 174 i2c = 0 ;
Rhyme 1:9450e20cf688 175 }
Rhyme 1:9450e20cf688 176 printf("\n") ;
Rhyme 1:9450e20cf688 177 }
Rhyme 1:9450e20cf688 178
Rhyme 1:9450e20cf688 179 void setTestLoop(void)
Rhyme 1:9450e20cf688 180 {
Rhyme 1:9450e20cf688 181 int num ;
Rhyme 1:9450e20cf688 182 scanf("%d", &num) ;
Rhyme 1:9450e20cf688 183 if (num < 0) { num = 1 ; }
Rhyme 1:9450e20cf688 184 test_loop = num ;
Rhyme 1:9450e20cf688 185 printf("test loop count set to %d\n", test_loop) ;
Rhyme 1:9450e20cf688 186 }
Rhyme 1:9450e20cf688 187
Rhyme 1:9450e20cf688 188 void doTestSensor(void)
Rhyme 1:9450e20cf688 189 {
Rhyme 1:9450e20cf688 190 int i ;
Rhyme 1:9450e20cf688 191 char name[32] ;
Rhyme 1:9450e20cf688 192 scanf("%s", name) ;
Rhyme 1:9450e20cf688 193 for (i = 0 ; i2c_sensor[i].address != 0 ; i++) {
Rhyme 1:9450e20cf688 194 if (strcmp(name, i2c_sensor[i].name) == 0) { /* found */
Rhyme 1:9450e20cf688 195 i2c_sensor[i].test_func() ;
Rhyme 1:9450e20cf688 196 break ;
Rhyme 1:9450e20cf688 197 }
Rhyme 1:9450e20cf688 198 }
Rhyme 1:9450e20cf688 199 }
Rhyme 1:9450e20cf688 200
Rhyme 1:9450e20cf688 201 void doCommand(char *str)
Rhyme 1:9450e20cf688 202 {
Rhyme 1:9450e20cf688 203 switch(*str) {
Rhyme 1:9450e20cf688 204 case 'o': case 'O': /* open */
Rhyme 1:9450e20cf688 205 doOpen() ; break ;
Rhyme 1:9450e20cf688 206 case 'c': case 'C': /* close */
Rhyme 1:9450e20cf688 207 doClose() ; break ;
Rhyme 1:9450e20cf688 208 case 'r': case 'R': /* read */
Rhyme 1:9450e20cf688 209 doRead() ; break ;
Rhyme 1:9450e20cf688 210 case 'w': case 'W': /* write */
Rhyme 1:9450e20cf688 211 doWrite() ; break ;
Rhyme 1:9450e20cf688 212 case 's': case 'S': /* status */
Rhyme 1:9450e20cf688 213 doStatus() ; break ;
Rhyme 1:9450e20cf688 214 case 'f': case 'F': /* Frequency */
Rhyme 1:9450e20cf688 215 doFreq() ; break ;
Rhyme 1:9450e20cf688 216 case 't': case 'T': /* test sensor */
Rhyme 1:9450e20cf688 217 doTestSensor() ; break ;
Rhyme 1:9450e20cf688 218 case 'l': case 'L': /* set test_loop */
Rhyme 1:9450e20cf688 219 setTestLoop() ; break ;
Rhyme 1:9450e20cf688 220 case 'b': case 'B': /* Bus Scan */
Rhyme 1:9450e20cf688 221 doBusScan() ; break ;
Rhyme 1:9450e20cf688 222 default:
Rhyme 1:9450e20cf688 223 doHelp() ; break ;
Rhyme 1:9450e20cf688 224 }
Rhyme 1:9450e20cf688 225 }
Rhyme 1:9450e20cf688 226
Rhyme 1:9450e20cf688 227 int main() {
Rhyme 1:9450e20cf688 228 char cmd[32] ;
Rhyme 1:9450e20cf688 229 tty = new vt100() ;
Rhyme 1:9450e20cf688 230 tty->cls() ;
Rhyme 1:9450e20cf688 231 doHelp() ;
Rhyme 1:9450e20cf688 232 while(1) {
Rhyme 1:9450e20cf688 233 printf("> ") ;
Rhyme 1:9450e20cf688 234 scanf("%s", cmd) ;
Rhyme 1:9450e20cf688 235 doCommand(cmd) ;
Rhyme 1:9450e20cf688 236 }
Rhyme 1:9450e20cf688 237 }