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:
Fri Mar 10 04:19:05 2017 +0000
Revision:
13:91e4be27e7c1
Parent:
12:b3dff3bbb1eb
Child:
14:1e6e4fdf90d0
MAX32600MBED support added.; Note: use 57600 for terminal baud

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 1:9450e20cf688 132 printf("help : print this help\n") ;
Rhyme 1:9450e20cf688 133 printf("\nPlease set local-echo to see what you are typing.\n") ;
Rhyme 1:9450e20cf688 134 printf("\n") ;
Rhyme 1:9450e20cf688 135 }
Rhyme 1:9450e20cf688 136
Rhyme 1:9450e20cf688 137 void doFreq(void)
Rhyme 1:9450e20cf688 138 {
Rhyme 1:9450e20cf688 139 int freq = 0 ;
Rhyme 1:9450e20cf688 140 scanf("%d", &freq) ;
Rhyme 1:9450e20cf688 141 if (i2c != 0) {
Rhyme 1:9450e20cf688 142 i2c->frequency(freq) ;
Rhyme 1:9450e20cf688 143 }
Rhyme 1:9450e20cf688 144 }
Rhyme 1:9450e20cf688 145
Rhyme 1:9450e20cf688 146
Rhyme 1:9450e20cf688 147 void print_sensor_name(int address)
Rhyme 1:9450e20cf688 148 {
Rhyme 1:9450e20cf688 149 int i ;
Rhyme 1:9450e20cf688 150 for(i = 0; i2c_sensor[i].address != 0 ; i++) {
Rhyme 1:9450e20cf688 151 if (i2c_sensor[i].address == address) {
Rhyme 1:9450e20cf688 152 printf("%s ", i2c_sensor[i].name) ;
Rhyme 1:9450e20cf688 153 }
Rhyme 1:9450e20cf688 154 }
Rhyme 1:9450e20cf688 155 }
Rhyme 1:9450e20cf688 156
Rhyme 1:9450e20cf688 157 void doBusScan(void)
Rhyme 1:9450e20cf688 158 {
Rhyme 1:9450e20cf688 159 int address ;
Rhyme 9:d4f76e6fa35f 160 uint8_t data[10] ;
Rhyme 9:d4f76e6fa35f 161 int num_data = 1 ;
Rhyme 1:9450e20cf688 162 int result ;
Rhyme 1:9450e20cf688 163 if (i2c != 0) {
Rhyme 1:9450e20cf688 164 printf("Closing I2C at 0x%02X ... ", i2c->address()) ;
Rhyme 1:9450e20cf688 165 delete i2c ;
Rhyme 1:9450e20cf688 166 i2c = 0 ;
Rhyme 1:9450e20cf688 167 printf("Done\n") ;
Rhyme 1:9450e20cf688 168 }
Rhyme 1:9450e20cf688 169
Rhyme 1:9450e20cf688 170 for (address = 1; address < 127 ; address++) {
Rhyme 1:9450e20cf688 171 i2c = new DUMB_I2C(PIN_SDA, PIN_SCL, address) ;
Rhyme 9:d4f76e6fa35f 172 // result = i2c->read(address, &data, 1) ;
Rhyme 9:d4f76e6fa35f 173 if (address == 0x10) {
Rhyme 9:d4f76e6fa35f 174 num_data = 2 ;
Rhyme 9:d4f76e6fa35f 175 } else {
Rhyme 9:d4f76e6fa35f 176 num_data = 1 ;
Rhyme 9:d4f76e6fa35f 177 }
Rhyme 9:d4f76e6fa35f 178 result = i2c->read(0, data, num_data) ;
Rhyme 1:9450e20cf688 179 if (result == 0) {
Rhyme 1:9450e20cf688 180 printf("%02X : ", address) ;
Rhyme 1:9450e20cf688 181 print_sensor_name(address) ;
Rhyme 1:9450e20cf688 182 printf("\n") ;
Rhyme 1:9450e20cf688 183 }
Rhyme 1:9450e20cf688 184 delete i2c ;
Rhyme 1:9450e20cf688 185 i2c = 0 ;
Rhyme 1:9450e20cf688 186 }
Rhyme 1:9450e20cf688 187 printf("\n") ;
Rhyme 1:9450e20cf688 188 }
Rhyme 1:9450e20cf688 189
Rhyme 1:9450e20cf688 190 void setTestLoop(void)
Rhyme 1:9450e20cf688 191 {
Rhyme 1:9450e20cf688 192 int num ;
Rhyme 1:9450e20cf688 193 scanf("%d", &num) ;
Rhyme 1:9450e20cf688 194 if (num < 0) { num = 1 ; }
Rhyme 1:9450e20cf688 195 test_loop = num ;
Rhyme 1:9450e20cf688 196 printf("test loop count set to %d\n", test_loop) ;
Rhyme 1:9450e20cf688 197 }
Rhyme 1:9450e20cf688 198
Rhyme 9:d4f76e6fa35f 199 void setTestInterval(void)
Rhyme 9:d4f76e6fa35f 200 {
Rhyme 9:d4f76e6fa35f 201 int num ;
Rhyme 9:d4f76e6fa35f 202 scanf("%d", &num) ;
Rhyme 9:d4f76e6fa35f 203 if (num < 0) num = 100 ;
Rhyme 9:d4f76e6fa35f 204 interval = num ;
Rhyme 9:d4f76e6fa35f 205 printf("wait %d ms for each loop\n", interval) ;
Rhyme 9:d4f76e6fa35f 206 }
Rhyme 9:d4f76e6fa35f 207
Rhyme 4:c10b1aa9925c 208 void str2upper(char *str)
Rhyme 4:c10b1aa9925c 209 {
Rhyme 4:c10b1aa9925c 210 while(str && *str) {
Rhyme 4:c10b1aa9925c 211 if (('a' <= *str) && (*str <= 'z')) {
Rhyme 4:c10b1aa9925c 212 *str -= 'a' - 'A' ;
Rhyme 4:c10b1aa9925c 213 }
Rhyme 4:c10b1aa9925c 214 str++ ;
Rhyme 4:c10b1aa9925c 215 }
Rhyme 4:c10b1aa9925c 216 }
Rhyme 4:c10b1aa9925c 217
Rhyme 1:9450e20cf688 218 void doTestSensor(void)
Rhyme 1:9450e20cf688 219 {
Rhyme 1:9450e20cf688 220 int i ;
Rhyme 12:b3dff3bbb1eb 221 int address = 0 ;
Rhyme 1:9450e20cf688 222 char name[32] ;
Rhyme 1:9450e20cf688 223 scanf("%s", name) ;
Rhyme 4:c10b1aa9925c 224 str2upper(name) ;
Rhyme 12:b3dff3bbb1eb 225 if (('0' <= *name)&&(*name <= '9')) { /* assume it's the address */
Rhyme 12:b3dff3bbb1eb 226 sscanf(name, "%X", &address) ;
Rhyme 12:b3dff3bbb1eb 227 for (i = 0 ; i < *(i2c_sensor[i].name) != 0 ; i++ ) {
Rhyme 12:b3dff3bbb1eb 228 if (i2c_sensor[i].address == address) {
Rhyme 12:b3dff3bbb1eb 229 strcpy(name, i2c_sensor[i].name) ;
Rhyme 12:b3dff3bbb1eb 230 break ;
Rhyme 12:b3dff3bbb1eb 231 }
Rhyme 12:b3dff3bbb1eb 232 }
Rhyme 12:b3dff3bbb1eb 233 } else {
Rhyme 12:b3dff3bbb1eb 234 for (i = 0 ; i2c_sensor[i].address != 0 ; i++) {
Rhyme 12:b3dff3bbb1eb 235 if (strcmp(name, i2c_sensor[i].name) == 0) { /* found */
Rhyme 12:b3dff3bbb1eb 236 break ;
Rhyme 12:b3dff3bbb1eb 237 }
Rhyme 1:9450e20cf688 238 }
Rhyme 1:9450e20cf688 239 }
Rhyme 12:b3dff3bbb1eb 240 if (i2c_sensor[i].name != 0) { /* name found */
Rhyme 12:b3dff3bbb1eb 241 i2c_sensor[i].test_func() ;
Rhyme 12:b3dff3bbb1eb 242 }
Rhyme 12:b3dff3bbb1eb 243
Rhyme 1:9450e20cf688 244 }
Rhyme 1:9450e20cf688 245
Rhyme 1:9450e20cf688 246 void doCommand(char *str)
Rhyme 1:9450e20cf688 247 {
Rhyme 1:9450e20cf688 248 switch(*str) {
Rhyme 1:9450e20cf688 249 case 'o': case 'O': /* open */
Rhyme 1:9450e20cf688 250 doOpen() ; break ;
Rhyme 1:9450e20cf688 251 case 'c': case 'C': /* close */
Rhyme 1:9450e20cf688 252 doClose() ; break ;
Rhyme 1:9450e20cf688 253 case 'r': case 'R': /* read */
Rhyme 1:9450e20cf688 254 doRead() ; break ;
Rhyme 1:9450e20cf688 255 case 'w': case 'W': /* write */
Rhyme 1:9450e20cf688 256 doWrite() ; break ;
Rhyme 1:9450e20cf688 257 case 's': case 'S': /* status */
Rhyme 1:9450e20cf688 258 doStatus() ; break ;
Rhyme 1:9450e20cf688 259 case 'f': case 'F': /* Frequency */
Rhyme 1:9450e20cf688 260 doFreq() ; break ;
Rhyme 1:9450e20cf688 261 case 't': case 'T': /* test sensor */
Rhyme 1:9450e20cf688 262 doTestSensor() ; break ;
Rhyme 1:9450e20cf688 263 case 'l': case 'L': /* set test_loop */
Rhyme 1:9450e20cf688 264 setTestLoop() ; break ;
Rhyme 9:d4f76e6fa35f 265 case 'i': case 'I': /* set interval */
Rhyme 9:d4f76e6fa35f 266 setTestInterval() ; break ;
Rhyme 1:9450e20cf688 267 case 'b': case 'B': /* Bus Scan */
Rhyme 1:9450e20cf688 268 doBusScan() ; break ;
Rhyme 1:9450e20cf688 269 default:
Rhyme 1:9450e20cf688 270 doHelp() ; break ;
Rhyme 1:9450e20cf688 271 }
Rhyme 1:9450e20cf688 272 }
Rhyme 1:9450e20cf688 273
Rhyme 1:9450e20cf688 274 int main() {
Rhyme 1:9450e20cf688 275 char cmd[32] ;
Rhyme 13:91e4be27e7c1 276 uint32_t baud = 115200 ;
Rhyme 13:91e4be27e7c1 277 #if defined (TARGET_MAX32600MBED)
Rhyme 13:91e4be27e7c1 278 baud = 57600 ;
Rhyme 13:91e4be27e7c1 279 #endif
Rhyme 13:91e4be27e7c1 280 tty = new vt100(baud) ;
Rhyme 1:9450e20cf688 281 tty->cls() ;
Rhyme 1:9450e20cf688 282 doHelp() ;
Rhyme 1:9450e20cf688 283 while(1) {
Rhyme 1:9450e20cf688 284 printf("> ") ;
Rhyme 1:9450e20cf688 285 scanf("%s", cmd) ;
Rhyme 1:9450e20cf688 286 doCommand(cmd) ;
Rhyme 1:9450e20cf688 287 }
Rhyme 1:9450e20cf688 288 }