MSS / Mbed 2 deprecated testSensor

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

Committer:
Rhyme
Date:
Mon Mar 27 08:04:29 2017 +0000
Revision:
14:1e6e4fdf90d0
Parent:
13:91e4be27e7c1
Child:
17:514f67d94d12
First version of MAX30101 lib added

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 9:d4f76e6fa35f 33 int interval = 100 ; /* ms wait interval */
Rhyme 1:9450e20cf688 34
Rhyme 1:9450e20cf688 35 void doClose(void)
Rhyme 1:9450e20cf688 36 {
Rhyme 1:9450e20cf688 37 if (i2c != 0) {
Rhyme 1:9450e20cf688 38 printf("Closing I2C at 0x%02X ... ", i2c->address()) ;
Rhyme 1:9450e20cf688 39 delete i2c ;
Rhyme 1:9450e20cf688 40 i2c = 0 ;
Rhyme 1:9450e20cf688 41 printf("Done\n") ;
Rhyme 1:9450e20cf688 42 }
Rhyme 1:9450e20cf688 43 }
Rhyme 1:9450e20cf688 44
Rhyme 1:9450e20cf688 45 void doOpen(void)
Rhyme 1:9450e20cf688 46 {
Rhyme 1:9450e20cf688 47 uint8_t address ;
Rhyme 1:9450e20cf688 48 scanf("%X", &address) ;
Rhyme 1:9450e20cf688 49 if (i2c != 0) {
Rhyme 1:9450e20cf688 50 doClose() ;
Rhyme 1:9450e20cf688 51 }
Rhyme 1:9450e20cf688 52 printf("Opening I2C at 0x%02X ... ", address) ;
Rhyme 1:9450e20cf688 53 i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ;
Rhyme 1:9450e20cf688 54 printf("Done\n") ;
Rhyme 1:9450e20cf688 55 }
Rhyme 1:9450e20cf688 56
Rhyme 1:9450e20cf688 57 void doRead(void)
Rhyme 1:9450e20cf688 58 {
Rhyme 1:9450e20cf688 59 int addr ;
Rhyme 1:9450e20cf688 60 int len ;
Rhyme 1:9450e20cf688 61 uint8_t *data ;
Rhyme 1:9450e20cf688 62 int result ;
Rhyme 1:9450e20cf688 63 if (i2c == 0) {
Rhyme 1:9450e20cf688 64 printf("I2C is not opened\n") ;
Rhyme 1:9450e20cf688 65 return ;
Rhyme 1:9450e20cf688 66 }
Rhyme 1:9450e20cf688 67 scanf("%X %X", &addr, &len) ;
Rhyme 1:9450e20cf688 68 if (len > 0) {
Rhyme 1:9450e20cf688 69 data = new uint8_t[len] ;
Rhyme 1:9450e20cf688 70 }
Rhyme 1:9450e20cf688 71 // i2c->read(addr, data, len) ;
Rhyme 1:9450e20cf688 72 printf("0x%02X : ", (unsigned int)addr) ;
Rhyme 1:9450e20cf688 73 for (int i = 0 ; i < len ; i++ ) {
Rhyme 1:9450e20cf688 74 result = i2c->read(addr+i, &data[i], 1) ;
Rhyme 1:9450e20cf688 75 if (result == 0) {
Rhyme 1:9450e20cf688 76 printf("%02X ", data[i]) ;
Rhyme 1:9450e20cf688 77 if (((i+1) < len)&&(((i+1)%0x10) == 0)) {
Rhyme 1:9450e20cf688 78 printf("\n") ;
Rhyme 1:9450e20cf688 79 printf("0x%02X : ", (unsigned int)(addr + i + 1)) ;
Rhyme 1:9450e20cf688 80 }
Rhyme 1:9450e20cf688 81 } else {
Rhyme 1:9450e20cf688 82 printf("failed to read 0x%02X\n", addr+i) ;
Rhyme 1:9450e20cf688 83 }
Rhyme 1:9450e20cf688 84 }
Rhyme 1:9450e20cf688 85 printf("\n") ;
Rhyme 1:9450e20cf688 86 }
Rhyme 1:9450e20cf688 87
Rhyme 1:9450e20cf688 88 void doWrite(void)
Rhyme 1:9450e20cf688 89 {
Rhyme 1:9450e20cf688 90 int addr ;
Rhyme 1:9450e20cf688 91 uint8_t len ;
Rhyme 1:9450e20cf688 92 uint8_t data ;
Rhyme 1:9450e20cf688 93 int ack ;
Rhyme 1:9450e20cf688 94 if (i2c == 0) {
Rhyme 1:9450e20cf688 95 printf("I2C is not opened\n") ;
Rhyme 1:9450e20cf688 96 return ;
Rhyme 1:9450e20cf688 97 }
Rhyme 1:9450e20cf688 98 scanf("%X %X", &addr, &data) ;
Rhyme 1:9450e20cf688 99 ack = i2c->write(addr, &data, 1) ;
Rhyme 1:9450e20cf688 100 if (ack != 0) {
Rhyme 1:9450e20cf688 101 printf("NAK at writing data[0x%02X] to address[0x%02X]\n",
Rhyme 1:9450e20cf688 102 data, addr ) ;
Rhyme 1:9450e20cf688 103 }
Rhyme 1:9450e20cf688 104 }
Rhyme 1:9450e20cf688 105
Rhyme 1:9450e20cf688 106 void doStatus(void)
Rhyme 1:9450e20cf688 107 {
Rhyme 1:9450e20cf688 108 if (i2c == 0) {
Rhyme 1:9450e20cf688 109 printf("i2c is not opened\n") ;
Rhyme 1:9450e20cf688 110 } else {
Rhyme 1:9450e20cf688 111 printf("i2c device at 0x%02X is opened at %d Hz\n",
Rhyme 1:9450e20cf688 112 i2c->address(), i2c->frequency()) ;
Rhyme 1:9450e20cf688 113 }
Rhyme 9:d4f76e6fa35f 114 printf("test loop number = %d\n", test_loop) ;
Rhyme 9:d4f76e6fa35f 115 printf("loop interval = %d ms\n", interval) ;
Rhyme 1:9450e20cf688 116 }
Rhyme 1:9450e20cf688 117
Rhyme 1:9450e20cf688 118 void doHelp(void)
Rhyme 1:9450e20cf688 119 {
Rhyme 1:9450e20cf688 120 printf("Simple I2C test program %s for %s\n", __DATE__, BOARD_NAME) ;
Rhyme 1:9450e20cf688 121 printf("\n === usage ===\n") ;
Rhyme 1:9450e20cf688 122 printf("open <7bit addr> : open i2c device at <7bit addr>\n") ;
Rhyme 1:9450e20cf688 123 printf("close : close currently opened i2c\n") ;
Rhyme 1:9450e20cf688 124 printf("read <addr> <len> : read <len> data from <addr>\n") ;
Rhyme 1:9450e20cf688 125 printf("write <addr> <data> : write <data> to register <addr>\n") ;
Rhyme 1:9450e20cf688 126 printf("frequency <freq> : change frequency to <freq> Hz\n") ;
Rhyme 1:9450e20cf688 127 printf("bus : bus scan for existing I2C addresses\n") ;
Rhyme 12:b3dff3bbb1eb 128 printf("test <sensor or address> : test a sensor\n") ;
Rhyme 1:9450e20cf688 129 printf("loop <number> : specify loop count for test\n") ;
Rhyme 9:d4f76e6fa35f 130 printf("interval <numver> : ms interval for each loop\n") ;
Rhyme 1:9450e20cf688 131 printf("status : print current status\n") ;
Rhyme 14:1e6e4fdf90d0 132 printf("demo : mulitple sensor demo\n") ;
Rhyme 1:9450e20cf688 133 printf("help : print this help\n") ;
Rhyme 1:9450e20cf688 134 printf("\nPlease set local-echo to see what you are typing.\n") ;
Rhyme 1:9450e20cf688 135 printf("\n") ;
Rhyme 1:9450e20cf688 136 }
Rhyme 1:9450e20cf688 137
Rhyme 1:9450e20cf688 138 void doFreq(void)
Rhyme 1:9450e20cf688 139 {
Rhyme 1:9450e20cf688 140 int freq = 0 ;
Rhyme 1:9450e20cf688 141 scanf("%d", &freq) ;
Rhyme 1:9450e20cf688 142 if (i2c != 0) {
Rhyme 1:9450e20cf688 143 i2c->frequency(freq) ;
Rhyme 1:9450e20cf688 144 }
Rhyme 1:9450e20cf688 145 }
Rhyme 1:9450e20cf688 146
Rhyme 1:9450e20cf688 147
Rhyme 1:9450e20cf688 148 void print_sensor_name(int address)
Rhyme 1:9450e20cf688 149 {
Rhyme 1:9450e20cf688 150 int i ;
Rhyme 1:9450e20cf688 151 for(i = 0; i2c_sensor[i].address != 0 ; i++) {
Rhyme 1:9450e20cf688 152 if (i2c_sensor[i].address == address) {
Rhyme 1:9450e20cf688 153 printf("%s ", i2c_sensor[i].name) ;
Rhyme 1:9450e20cf688 154 }
Rhyme 1:9450e20cf688 155 }
Rhyme 1:9450e20cf688 156 }
Rhyme 1:9450e20cf688 157
Rhyme 1:9450e20cf688 158 void doBusScan(void)
Rhyme 1:9450e20cf688 159 {
Rhyme 1:9450e20cf688 160 int address ;
Rhyme 9:d4f76e6fa35f 161 uint8_t data[10] ;
Rhyme 9:d4f76e6fa35f 162 int num_data = 1 ;
Rhyme 1:9450e20cf688 163 int result ;
Rhyme 1:9450e20cf688 164 if (i2c != 0) {
Rhyme 1:9450e20cf688 165 printf("Closing I2C at 0x%02X ... ", i2c->address()) ;
Rhyme 1:9450e20cf688 166 delete i2c ;
Rhyme 1:9450e20cf688 167 i2c = 0 ;
Rhyme 1:9450e20cf688 168 printf("Done\n") ;
Rhyme 1:9450e20cf688 169 }
Rhyme 1:9450e20cf688 170
Rhyme 1:9450e20cf688 171 for (address = 1; address < 127 ; address++) {
Rhyme 1:9450e20cf688 172 i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ;
Rhyme 9:d4f76e6fa35f 173 // result = i2c->read(address, &data, 1) ;
Rhyme 9:d4f76e6fa35f 174 if (address == 0x10) {
Rhyme 9:d4f76e6fa35f 175 num_data = 2 ;
Rhyme 9:d4f76e6fa35f 176 } else {
Rhyme 9:d4f76e6fa35f 177 num_data = 1 ;
Rhyme 9:d4f76e6fa35f 178 }
Rhyme 9:d4f76e6fa35f 179 result = i2c->read(0, data, num_data) ;
Rhyme 1:9450e20cf688 180 if (result == 0) {
Rhyme 1:9450e20cf688 181 printf("%02X : ", address) ;
Rhyme 1:9450e20cf688 182 print_sensor_name(address) ;
Rhyme 1:9450e20cf688 183 printf("\n") ;
Rhyme 1:9450e20cf688 184 }
Rhyme 1:9450e20cf688 185 delete i2c ;
Rhyme 1:9450e20cf688 186 i2c = 0 ;
Rhyme 1:9450e20cf688 187 }
Rhyme 1:9450e20cf688 188 printf("\n") ;
Rhyme 1:9450e20cf688 189 }
Rhyme 1:9450e20cf688 190
Rhyme 1:9450e20cf688 191 void setTestLoop(void)
Rhyme 1:9450e20cf688 192 {
Rhyme 1:9450e20cf688 193 int num ;
Rhyme 1:9450e20cf688 194 scanf("%d", &num) ;
Rhyme 1:9450e20cf688 195 if (num < 0) { num = 1 ; }
Rhyme 1:9450e20cf688 196 test_loop = num ;
Rhyme 1:9450e20cf688 197 printf("test loop count set to %d\n", test_loop) ;
Rhyme 1:9450e20cf688 198 }
Rhyme 1:9450e20cf688 199
Rhyme 9:d4f76e6fa35f 200 void setTestInterval(void)
Rhyme 9:d4f76e6fa35f 201 {
Rhyme 9:d4f76e6fa35f 202 int num ;
Rhyme 9:d4f76e6fa35f 203 scanf("%d", &num) ;
Rhyme 9:d4f76e6fa35f 204 if (num < 0) num = 100 ;
Rhyme 9:d4f76e6fa35f 205 interval = num ;
Rhyme 9:d4f76e6fa35f 206 printf("wait %d ms for each loop\n", interval) ;
Rhyme 9:d4f76e6fa35f 207 }
Rhyme 9:d4f76e6fa35f 208
Rhyme 4:c10b1aa9925c 209 void str2upper(char *str)
Rhyme 4:c10b1aa9925c 210 {
Rhyme 4:c10b1aa9925c 211 while(str && *str) {
Rhyme 4:c10b1aa9925c 212 if (('a' <= *str) && (*str <= 'z')) {
Rhyme 4:c10b1aa9925c 213 *str -= 'a' - 'A' ;
Rhyme 4:c10b1aa9925c 214 }
Rhyme 4:c10b1aa9925c 215 str++ ;
Rhyme 4:c10b1aa9925c 216 }
Rhyme 4:c10b1aa9925c 217 }
Rhyme 4:c10b1aa9925c 218
Rhyme 1:9450e20cf688 219 void doTestSensor(void)
Rhyme 1:9450e20cf688 220 {
Rhyme 1:9450e20cf688 221 int i ;
Rhyme 12:b3dff3bbb1eb 222 int address = 0 ;
Rhyme 1:9450e20cf688 223 char name[32] ;
Rhyme 1:9450e20cf688 224 scanf("%s", name) ;
Rhyme 4:c10b1aa9925c 225 str2upper(name) ;
Rhyme 12:b3dff3bbb1eb 226 if (('0' <= *name)&&(*name <= '9')) { /* assume it's the address */
Rhyme 12:b3dff3bbb1eb 227 sscanf(name, "%X", &address) ;
Rhyme 12:b3dff3bbb1eb 228 for (i = 0 ; i < *(i2c_sensor[i].name) != 0 ; i++ ) {
Rhyme 12:b3dff3bbb1eb 229 if (i2c_sensor[i].address == address) {
Rhyme 12:b3dff3bbb1eb 230 strcpy(name, i2c_sensor[i].name) ;
Rhyme 12:b3dff3bbb1eb 231 break ;
Rhyme 12:b3dff3bbb1eb 232 }
Rhyme 12:b3dff3bbb1eb 233 }
Rhyme 12:b3dff3bbb1eb 234 } else {
Rhyme 12:b3dff3bbb1eb 235 for (i = 0 ; i2c_sensor[i].address != 0 ; i++) {
Rhyme 12:b3dff3bbb1eb 236 if (strcmp(name, i2c_sensor[i].name) == 0) { /* found */
Rhyme 12:b3dff3bbb1eb 237 break ;
Rhyme 12:b3dff3bbb1eb 238 }
Rhyme 1:9450e20cf688 239 }
Rhyme 1:9450e20cf688 240 }
Rhyme 12:b3dff3bbb1eb 241 if (i2c_sensor[i].name != 0) { /* name found */
Rhyme 12:b3dff3bbb1eb 242 i2c_sensor[i].test_func() ;
Rhyme 12:b3dff3bbb1eb 243 }
Rhyme 12:b3dff3bbb1eb 244
Rhyme 1:9450e20cf688 245 }
Rhyme 1:9450e20cf688 246
Rhyme 1:9450e20cf688 247 void doCommand(char *str)
Rhyme 1:9450e20cf688 248 {
Rhyme 1:9450e20cf688 249 switch(*str) {
Rhyme 1:9450e20cf688 250 case 'o': case 'O': /* open */
Rhyme 1:9450e20cf688 251 doOpen() ; break ;
Rhyme 1:9450e20cf688 252 case 'c': case 'C': /* close */
Rhyme 1:9450e20cf688 253 doClose() ; break ;
Rhyme 14:1e6e4fdf90d0 254 case 'd': case 'D': /* demo */
Rhyme 14:1e6e4fdf90d0 255 doDemo() ; break ;
Rhyme 1:9450e20cf688 256 case 'r': case 'R': /* read */
Rhyme 1:9450e20cf688 257 doRead() ; break ;
Rhyme 1:9450e20cf688 258 case 'w': case 'W': /* write */
Rhyme 1:9450e20cf688 259 doWrite() ; break ;
Rhyme 1:9450e20cf688 260 case 's': case 'S': /* status */
Rhyme 1:9450e20cf688 261 doStatus() ; break ;
Rhyme 1:9450e20cf688 262 case 'f': case 'F': /* Frequency */
Rhyme 1:9450e20cf688 263 doFreq() ; break ;
Rhyme 1:9450e20cf688 264 case 't': case 'T': /* test sensor */
Rhyme 1:9450e20cf688 265 doTestSensor() ; break ;
Rhyme 1:9450e20cf688 266 case 'l': case 'L': /* set test_loop */
Rhyme 1:9450e20cf688 267 setTestLoop() ; break ;
Rhyme 9:d4f76e6fa35f 268 case 'i': case 'I': /* set interval */
Rhyme 9:d4f76e6fa35f 269 setTestInterval() ; break ;
Rhyme 1:9450e20cf688 270 case 'b': case 'B': /* Bus Scan */
Rhyme 1:9450e20cf688 271 doBusScan() ; break ;
Rhyme 1:9450e20cf688 272 default:
Rhyme 1:9450e20cf688 273 doHelp() ; break ;
Rhyme 1:9450e20cf688 274 }
Rhyme 1:9450e20cf688 275 }
Rhyme 1:9450e20cf688 276
Rhyme 1:9450e20cf688 277 int main() {
Rhyme 1:9450e20cf688 278 char cmd[32] ;
Rhyme 13:91e4be27e7c1 279 uint32_t baud = 115200 ;
Rhyme 13:91e4be27e7c1 280 #if defined (TARGET_MAX32600MBED)
Rhyme 13:91e4be27e7c1 281 baud = 57600 ;
Rhyme 13:91e4be27e7c1 282 #endif
Rhyme 13:91e4be27e7c1 283 tty = new vt100(baud) ;
Rhyme 1:9450e20cf688 284 tty->cls() ;
Rhyme 1:9450e20cf688 285 doHelp() ;
Rhyme 1:9450e20cf688 286 while(1) {
Rhyme 1:9450e20cf688 287 printf("> ") ;
Rhyme 1:9450e20cf688 288 scanf("%s", cmd) ;
Rhyme 1:9450e20cf688 289 doCommand(cmd) ;
Rhyme 1:9450e20cf688 290 }
Rhyme 1:9450e20cf688 291 }