MAX30208 I2C Terminal Interface

Dependencies:   max32630fthr USBDevice

Committer:
elloway
Date:
Fri Sep 04 21:15:38 2020 +0000
Revision:
17:65cd58826416
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elloway 17:65cd58826416 1 #include "mbed.h"
elloway 17:65cd58826416 2 #include "max32630fthr.h"
elloway 17:65cd58826416 3 #include "USBSerial.h"
elloway 17:65cd58826416 4
elloway 17:65cd58826416 5 //#define OT07_ADDRESS 0xA0 // OTO7 Base Address
elloway 17:65cd58826416 6
elloway 17:65cd58826416 7 #define DEBUG 1 // 0->off, 1-> on, debug prints to daplink port
elloway 17:65cd58826416 8
elloway 17:65cd58826416 9 //OT07 Registers
elloway 17:65cd58826416 10 #define OT07_STATUS 0x00 // OT07 status regiter
elloway 17:65cd58826416 11 #define OT07_INT_EN 0x01 // OT07 Interrupt Enable
elloway 17:65cd58826416 12 #define OT07_FIFO_W 0x04 // OT07 FIFO Write Pointer
elloway 17:65cd58826416 13 #define OT07_FIFO_R 0x05 // OT07 FIFO Read Pointer
elloway 17:65cd58826416 14 #define OT07_FIFO_OF 0x06 // OT07 FIFO Overflow Counter
elloway 17:65cd58826416 15 #define OT07_FIFO_COUNT 0x07 // OT07 FIFO Data Count
elloway 17:65cd58826416 16 #define OT07_FIFO_DATA 0x08 // OT07 FIFO Data
elloway 17:65cd58826416 17 #define OT07_FIFO_CNFG1 0x09 // OT07 FIFO Configuration 1 (FIFO_A_FULL)
elloway 17:65cd58826416 18 #define OT07_FIFO_CNFG2 0x0A // OT07 FIFO Configuration 2
elloway 17:65cd58826416 19 #define OT07_SYS 0x0C // OT07 System Configuration
elloway 17:65cd58826416 20 #define OT07_ALARM_HIGH_MSB 0x10 // OT07 Alarm High MSB
elloway 17:65cd58826416 21 #define OT07_ALARM_HIGH_LSB 0x11 // OT07 Alarm High LSB
elloway 17:65cd58826416 22 #define OT07_ALARM_LOW_MSB 0x12 // OT07 Alarm Low MSB
elloway 17:65cd58826416 23 #define OT07_ALARM_LOW_LSB 0x13 // OT07 Alarm LOW LSB
elloway 17:65cd58826416 24 #define OT07_ADC_SETUP 0x14 // OT07 Temp Seneor Setup (ADC_RES[7:6]) & Convert Temperature [0]
elloway 17:65cd58826416 25 #define OT07_GPIO_SETUP 0x20 // OT07 GPIO Setup, sets GPIO modes
elloway 17:65cd58826416 26 #define OT07_GPIO_CTRL 0x21 // OT07 GPIO control
elloway 17:65cd58826416 27 #define OT07_ROM_ID 0x30 // OT07 ROM_ID address of LSB?
elloway 17:65cd58826416 28
elloway 17:65cd58826416 29 #define ID_LENGTH 8 // Rom ID length in bytes
elloway 17:65cd58826416 30 #define BS 8 // ASCII Back Space
elloway 17:65cd58826416 31 #define CR 13 // ASCII Carriage Return
elloway 17:65cd58826416 32
elloway 17:65cd58826416 33 //global variable
elloway 17:65cd58826416 34
elloway 17:65cd58826416 35 struct OT07_struct {
elloway 17:65cd58826416 36 char rom_id[ID_LENGTH]; // device 8 byte ROM ID
elloway 17:65cd58826416 37 char I2C_address; // I2C addess, based on GPIO0 and GPIO1 at power up
elloway 17:65cd58826416 38
elloway 17:65cd58826416 39 };
elloway 17:65cd58826416 40
elloway 17:65cd58826416 41
elloway 17:65cd58826416 42 bool sample_flag; // global flag indicating its time to take next sample.
elloway 17:65cd58826416 43
elloway 17:65cd58826416 44 //******************** init Feather Boared Hardware ***********************
elloway 17:65cd58826416 45
elloway 17:65cd58826416 46 MAX32630FTHR pegasus(MAX32630FTHR::VIO_1V8);
elloway 17:65cd58826416 47
elloway 17:65cd58826416 48 //Configure serial ports
elloway 17:65cd58826416 49 Serial db(P2_1, P2_0); // Hardware serial debug port over DAPLink, 9600 defult baudrate
elloway 17:65cd58826416 50 USBSerial pc; // Virtual serial port over USB
elloway 17:65cd58826416 51
elloway 17:65cd58826416 52 // I2C setup
elloway 17:65cd58826416 53 I2C i2c(P3_4,P3_5); // P3_4 -> I2C1_SDA, P3_5-> I2C1_SCL
elloway 17:65cd58826416 54
elloway 17:65cd58826416 55 //Timer setup
elloway 17:65cd58826416 56 Ticker timer_1; // timer for blinking led heartbeat and setting tick_flag
elloway 17:65cd58826416 57 Ticker timer_2; // timer for controling sample rate.
elloway 17:65cd58826416 58
elloway 17:65cd58826416 59 //LED blink setup
elloway 17:65cd58826416 60 DigitalOut rLED(LED1);
elloway 17:65cd58826416 61 DigitalOut gLED(LED2);
elloway 17:65cd58826416 62 DigitalOut bLED(LED3);
elloway 17:65cd58826416 63
elloway 17:65cd58826416 64 // *****************************************************************************
elloway 17:65cd58826416 65 // LED_blink_callback() attached to timer1 interupt
elloway 17:65cd58826416 66 // blinks Feather board led and sets tick_flag true
elloway 17:65cd58826416 67 // *****************************************************************************
elloway 17:65cd58826416 68
elloway 17:65cd58826416 69 void LED_blink_callback(){ // LED Heart beat
elloway 17:65cd58826416 70
elloway 17:65cd58826416 71 bLED=!bLED; //toggle Green LED
elloway 17:65cd58826416 72
elloway 17:65cd58826416 73 }
elloway 17:65cd58826416 74
elloway 17:65cd58826416 75
elloway 17:65cd58826416 76 // *****************************************************************************
elloway 17:65cd58826416 77 // set_sample_flag_callback() sets sample_flag = true//
elloway 17:65cd58826416 78 // *****************************************************************************
elloway 17:65cd58826416 79
elloway 17:65cd58826416 80 void set_sample_flag_callback(){ //set sample flag interrupt
elloway 17:65cd58826416 81 sample_flag = true;
elloway 17:65cd58826416 82
elloway 17:65cd58826416 83 }
elloway 17:65cd58826416 84
elloway 17:65cd58826416 85 // *****************************************************************************
elloway 17:65cd58826416 86 // OT07_write_register(char, char, char) writes single byte to OT07
elloway 17:65cd58826416 87 // char I2C address
elloway 17:65cd58826416 88 // char OT07 register address
elloway 17:65cd58826416 89 // char data byte to be writen
elloway 17:65cd58826416 90 // returns 0 on success ACK, 1 on NACK
elloway 17:65cd58826416 91 // *****************************************************************************
elloway 17:65cd58826416 92
elloway 17:65cd58826416 93 int OT07_write_register(char I2C_add, char reg_add, char byte){
elloway 17:65cd58826416 94 char data[2];
elloway 17:65cd58826416 95 int error;
elloway 17:65cd58826416 96 data[0] = reg_add;
elloway 17:65cd58826416 97 data[1] = byte;
elloway 17:65cd58826416 98 error = i2c.write(I2C_add,data,2);
elloway 17:65cd58826416 99 //if(DEBUG)db.printf("wr[%02X %02X %d]\r\n", data[0], data[1], error);
elloway 17:65cd58826416 100 return error;
elloway 17:65cd58826416 101
elloway 17:65cd58826416 102 }
elloway 17:65cd58826416 103
elloway 17:65cd58826416 104 /// ****************************************************************************
elloway 17:65cd58826416 105 // OT07_write_register(char, char, char *, int) writes multiple bytes to OT07
elloway 17:65cd58826416 106 // char I2C address
elloway 17:65cd58826416 107 // char OT07 register address
elloway 17:65cd58826416 108 // char * data vector of bytes to be written
elloway 17:65cd58826416 109 // int number of bytes to write
elloway 17:65cd58826416 110 // returns 0 on success ACK, 1 on NACK
elloway 17:65cd58826416 111 // *****************************************************************************
elloway 17:65cd58826416 112
elloway 17:65cd58826416 113 int OT07_write_register(char I2C_add, char reg_add, char *bytes, int n){
elloway 17:65cd58826416 114 int i;
elloway 17:65cd58826416 115 //set start address
elloway 17:65cd58826416 116 char data[16];
elloway 17:65cd58826416 117 int error;
elloway 17:65cd58826416 118 data[0] = reg_add;
elloway 17:65cd58826416 119 for(i=1;i<=n;i++){
elloway 17:65cd58826416 120 data[i] = bytes[i-1];
elloway 17:65cd58826416 121 }
elloway 17:65cd58826416 122 error = i2c.write(I2C_add,data,n+1); // send n bytes of data
elloway 17:65cd58826416 123
elloway 17:65cd58826416 124 return error;
elloway 17:65cd58826416 125 }
elloway 17:65cd58826416 126
elloway 17:65cd58826416 127 // *****************************************************************************
elloway 17:65cd58826416 128 // OT07_read_register(char, char, char *, int) writes single byte to OT07
elloway 17:65cd58826416 129 // char I2C address
elloway 17:65cd58826416 130 // char OT07 register address
elloway 17:65cd58826416 131 // char * data vector for read bytes to be stored in
elloway 17:65cd58826416 132 // int number of bytes to read
elloway 17:65cd58826416 133 // returns 0 on success, 1 on fail
elloway 17:65cd58826416 134 // *****************************************************************************
elloway 17:65cd58826416 135
elloway 17:65cd58826416 136 int OT07_read_register(char I2C_add, char reg_add, char *bytes, int n){
elloway 17:65cd58826416 137 int error;
elloway 17:65cd58826416 138 error = i2c.write(I2C_add,&reg_add,1,1);
elloway 17:65cd58826416 139 if(error)return error;
elloway 17:65cd58826416 140 error = i2c.read(I2C_add,bytes,n);
elloway 17:65cd58826416 141 //if(DEBUG)db.printf("rr e[%d]\r\n",error);
elloway 17:65cd58826416 142 return error;
elloway 17:65cd58826416 143 }
elloway 17:65cd58826416 144
elloway 17:65cd58826416 145 // *****************************************************************************
elloway 17:65cd58826416 146 // search_I2C_bus(OT07_struct *) searches I2C address 0xA0, 0xA2, 0xA4 and 0xA6
elloway 17:65cd58826416 147 // OT07_struct * structure array to holds I2C address and rom_ids
elloway 17:65cd58826416 148 // returns number of devices found
elloway 17:65cd58826416 149 // *****************************************************************************
elloway 17:65cd58826416 150
elloway 17:65cd58826416 151 int search_I2C_bus(OT07_struct OT07[]){
elloway 17:65cd58826416 152 char data[16];
elloway 17:65cd58826416 153 char I2C_add;
elloway 17:65cd58826416 154 //char GPIO;
elloway 17:65cd58826416 155 int error;
elloway 17:65cd58826416 156 int device_count = 0;
elloway 17:65cd58826416 157 int i;
elloway 17:65cd58826416 158 int j;
elloway 17:65cd58826416 159 for(i = 0;i<4;i++){
elloway 17:65cd58826416 160 I2C_add = 0xA0 + i*2;
elloway 17:65cd58826416 161 error = OT07_read_register(I2C_add,0xff,data,1);
elloway 17:65cd58826416 162 //if(DEBUG)db.printf("i2c[%02X] e[%d]\n\r",I2C_add,error);
elloway 17:65cd58826416 163 if(error == 0){
elloway 17:65cd58826416 164 if(data[0] == 0x30){
elloway 17:65cd58826416 165
elloway 17:65cd58826416 166 OT07_read_register(I2C_add,OT07_GPIO_CTRL,data,1); //read GPIO location address
elloway 17:65cd58826416 167 //if(DEBUG)db.printf("Found device at address 0x%02X\n\rGPIO location %02X dc[%d]\r\n",I2C_add,data[0],device_count);
elloway 17:65cd58826416 168 OT07[device_count].I2C_address = I2C_add;
elloway 17:65cd58826416 169
elloway 17:65cd58826416 170 OT07_read_register(I2C_add,OT07_ROM_ID,data,8);
elloway 17:65cd58826416 171 for(j=7;j>=0;j--){
elloway 17:65cd58826416 172 OT07[device_count].rom_id[j] = data[j];
elloway 17:65cd58826416 173
elloway 17:65cd58826416 174 }
elloway 17:65cd58826416 175 device_count++;
elloway 17:65cd58826416 176 }
elloway 17:65cd58826416 177 }
elloway 17:65cd58826416 178
elloway 17:65cd58826416 179 }//end for(i...)
elloway 17:65cd58826416 180 return device_count;
elloway 17:65cd58826416 181 }// end search_I2C()
elloway 17:65cd58826416 182
elloway 17:65cd58826416 183
elloway 17:65cd58826416 184
elloway 17:65cd58826416 185 // *****************************************************************************
elloway 17:65cd58826416 186 // convert_temperature(char) sends convert command to OT07 device
elloway 17:65cd58826416 187 // char I2C address
elloway 17:65cd58826416 188 // *****************************************************************************
elloway 17:65cd58826416 189
elloway 17:65cd58826416 190 void convert_temperature(char I2C_add){ // set convert bit to start conversion
elloway 17:65cd58826416 191
elloway 17:65cd58826416 192 char data[2];
elloway 17:65cd58826416 193
elloway 17:65cd58826416 194 //read ADC_SETUP register 0x14
elloway 17:65cd58826416 195 OT07_read_register(I2C_add,OT07_ADC_SETUP,data,1);
elloway 17:65cd58826416 196
elloway 17:65cd58826416 197 //mask convert register value with 0x01 and write back register 0x14
elloway 17:65cd58826416 198 OT07_write_register(I2C_add,OT07_ADC_SETUP, data[0]|0x01);
elloway 17:65cd58826416 199 }
elloway 17:65cd58826416 200
elloway 17:65cd58826416 201 //******************************************************************************
elloway 17:65cd58826416 202 // get_temperature(char) read temperature from OT07 device FIFO register
elloway 17:65cd58826416 203 // char I2C address
elloway 17:65cd58826416 204 // returns double temperature in oC
elloway 17:65cd58826416 205 //******************************************************************************
elloway 17:65cd58826416 206
elloway 17:65cd58826416 207 double get_temperature(char I2C_add){
elloway 17:65cd58826416 208 char data[2];
elloway 17:65cd58826416 209 double T;
elloway 17:65cd58826416 210 int count;
elloway 17:65cd58826416 211
elloway 17:65cd58826416 212 OT07_read_register(I2C_add,OT07_FIFO_DATA,data,2); // Read temperature from FIFO, 2 bytes
elloway 17:65cd58826416 213 //if(DEBUG)db.printf("get_temperature -- FIFO[%02X %02X]\r\n",data[0],data[1]);
elloway 17:65cd58826416 214 //calculate temperture from data
elloway 17:65cd58826416 215 count = (int)(data[0]*256 + data[1]);
elloway 17:65cd58826416 216 if (count >= 32768)count = count - 65536; // 2s comp
elloway 17:65cd58826416 217 T = (double)count*0.005;
elloway 17:65cd58826416 218
elloway 17:65cd58826416 219 return T;
elloway 17:65cd58826416 220 }
elloway 17:65cd58826416 221
elloway 17:65cd58826416 222 //******************************************************************************
elloway 17:65cd58826416 223 // print_device_list(OT07_struct *, int) prints list of devices found
elloway 17:65cd58826416 224 // OT07_struct* pointer to OT07 device structure array
elloway 17:65cd58826416 225 // int number of devices i array
elloway 17:65cd58826416 226 // returns 0
elloway 17:65cd58826416 227 //******************************************************************************
elloway 17:65cd58826416 228
elloway 17:65cd58826416 229 int print_device_list(OT07_struct OT07[], int n){
elloway 17:65cd58826416 230 int i;
elloway 17:65cd58826416 231 int j;
elloway 17:65cd58826416 232 for(j=0;j<n;j++){ // loop thru n devices
elloway 17:65cd58826416 233 pc.printf("%02d, ",j); // print device index
elloway 17:65cd58826416 234 pc.printf("%02X, ",OT07[j].I2C_address); // print I2C address
elloway 17:65cd58826416 235 pc.printf("%02X, ",OT07[j].rom_id[7]); // print CRC
elloway 17:65cd58826416 236 for(i=6;i>=1;i--){
elloway 17:65cd58826416 237 pc.printf("%02X",OT07[j].rom_id[i]); // printf ROM ID
elloway 17:65cd58826416 238 }
elloway 17:65cd58826416 239 pc.printf(", %02X",OT07[j].rom_id[0]); // print family code
elloway 17:65cd58826416 240 pc.printf("\r\n");
elloway 17:65cd58826416 241 }
elloway 17:65cd58826416 242 return 0;
elloway 17:65cd58826416 243 }
elloway 17:65cd58826416 244
elloway 17:65cd58826416 245
elloway 17:65cd58826416 246 //******************************************************************************
elloway 17:65cd58826416 247 // main()
elloway 17:65cd58826416 248 //******************************************************************************
elloway 17:65cd58826416 249
elloway 17:65cd58826416 250 int main()
elloway 17:65cd58826416 251 {
elloway 17:65cd58826416 252
elloway 17:65cd58826416 253 OT07_struct OT07[4]; // structure that holds I2C address and ROM_ID
elloway 17:65cd58826416 254
elloway 17:65cd58826416 255 char data[130]; // char vector used for passing data to read and write register calls
elloway 17:65cd58826416 256 char s[128]; // temp char array
elloway 17:65cd58826416 257 int device_count = 0; // number of I2C devices found by search_I2C_bus()
elloway 17:65cd58826416 258 int i; // loop counter
elloway 17:65cd58826416 259 int j; // loop counter
elloway 17:65cd58826416 260 double T[4]; // holds temperature reading
elloway 17:65cd58826416 261
elloway 17:65cd58826416 262 // i/o variables for serial com port
elloway 17:65cd58826416 263 char rx_buff[128]; // comport input buffer
elloway 17:65cd58826416 264 int rx_index; // rx_buffer pointer
elloway 17:65cd58826416 265 char c; // command type character
elloway 17:65cd58826416 266 char str[16]; // temp string
elloway 17:65cd58826416 267 int n; // argument count
elloway 17:65cd58826416 268 int arg0; // device argument
elloway 17:65cd58826416 269 int arg1; // argumnet 1
elloway 17:65cd58826416 270 int arg2; // argument 2
elloway 17:65cd58826416 271
elloway 17:65cd58826416 272 int num_bytes; // calculated number of byte to read
elloway 17:65cd58826416 273
elloway 17:65cd58826416 274 int period = 1000; // sample period in ms
elloway 17:65cd58826416 275 int sample_count = 0; // sample index for log
elloway 17:65cd58826416 276 int delay = 25;
elloway 17:65cd58826416 277
elloway 17:65cd58826416 278 int i2c_freq = 1; // i2c clock frequency 0->100000kHz, 1->400000kHz
elloway 17:65cd58826416 279
elloway 17:65cd58826416 280 bool log_flag = false; // true -> log temperature to serial port, false -> do not log
elloway 17:65cd58826416 281 bool ext_convert_flag = true;
elloway 17:65cd58826416 282 int echo = 1; // 0 -> echo off, 1 -> echo terminal input and use verbose output
elloway 17:65cd58826416 283
elloway 17:65cd58826416 284
elloway 17:65cd58826416 285 int L3out = 1800; // Sets mV for L3OUT (3V3 on DS2484 Feather Wing board) 1800 to 3600 mV allowed.
elloway 17:65cd58826416 286
elloway 17:65cd58826416 287
elloway 17:65cd58826416 288 //************* init ticker timer callbacks ****************
elloway 17:65cd58826416 289 timer_1.attach(&LED_blink_callback,1.0); //start ticker interupt, once per sec.
elloway 17:65cd58826416 290
elloway 17:65cd58826416 291 //********************* init I2C port ***********************
elloway 17:65cd58826416 292 i2c.frequency(i2c_freq); //set I2C clock 0-->100kHz, 1-->400kHz
elloway 17:65cd58826416 293
elloway 17:65cd58826416 294
elloway 17:65cd58826416 295 //******************* init VDD (L3OUT) **********************
elloway 17:65cd58826416 296 if(DEBUG)db.printf("set LDO3 to %dmV\r\n",L3out);
elloway 17:65cd58826416 297 pegasus.max14690.ldo3SetVoltage(L3out); // sets VDD and I2C to L3out
elloway 17:65cd58826416 298
elloway 17:65cd58826416 299 //********* echo firmware info to both serial ports *********
elloway 17:65cd58826416 300 wait(1); //wait 1 sec for USB serial port to init.
elloway 17:65cd58826416 301 sprintf(s,"--- MAX30208 I2C Terminal Interface V0.3 ---\r\n");
elloway 17:65cd58826416 302 if(DEBUG)db.printf("%s",s);
elloway 17:65cd58826416 303 pc.printf("%s",s);
elloway 17:65cd58826416 304 pc.printf("VDD = %dmV, I2C clock = %dkHz\r\n",L3out, 100*(i2c_freq*3 + 1));
elloway 17:65cd58826416 305
elloway 17:65cd58826416 306 //********************** init LEDs *************************
elloway 17:65cd58826416 307 rLED = LED_OFF;
elloway 17:65cd58826416 308 gLED = LED_ON;
elloway 17:65cd58826416 309 bLED = LED_OFF;
elloway 17:65cd58826416 310
elloway 17:65cd58826416 311 //******************** Find I2C devices on bus **************
elloway 17:65cd58826416 312 pc.printf("<search for devices>\r\n");
elloway 17:65cd58826416 313 device_count = search_I2C_bus(OT07);
elloway 17:65cd58826416 314 pc.printf("<device, I2C Add, CRC, ROM ID, Family>\r\n");
elloway 17:65cd58826416 315 print_device_list(OT07, device_count);
elloway 17:65cd58826416 316
elloway 17:65cd58826416 317
elloway 17:65cd58826416 318 //************* init rx input buffer index ******************
elloway 17:65cd58826416 319 rx_index = 0;
elloway 17:65cd58826416 320 sample_flag = false;
elloway 17:65cd58826416 321
elloway 17:65cd58826416 322 //************** start main loop ***************************
elloway 17:65cd58826416 323 if(DEBUG)db.printf("start Main loop\r\n");
elloway 17:65cd58826416 324
elloway 17:65cd58826416 325 while(1) { // start main loop, take data if log flag true, check for input, repeat
elloway 17:65cd58826416 326
elloway 17:65cd58826416 327 //wait(0.2);
elloway 17:65cd58826416 328 if(log_flag == true){
elloway 17:65cd58826416 329 if(sample_flag == true){// take sample.
elloway 17:65cd58826416 330 sample_flag = false;
elloway 17:65cd58826416 331 convert_temperature(OT07[0].I2C_address);
elloway 17:65cd58826416 332 wait_ms(delay);
elloway 17:65cd58826416 333 T[0] = get_temperature(OT07[0].I2C_address);
elloway 17:65cd58826416 334 pc.printf("%5d, %7.3f\r\n",sample_count++,T[0]);
elloway 17:65cd58826416 335 }
elloway 17:65cd58826416 336 }// end if(log_flag == true)
elloway 17:65cd58826416 337
elloway 17:65cd58826416 338 // ----------------------------------------------------------------------------
elloway 17:65cd58826416 339 // test for charater input for USB com port
elloway 17:65cd58826416 340 // ----------------------------------------------------------------------------
elloway 17:65cd58826416 341
elloway 17:65cd58826416 342 //test if PC sent some charaters
elloway 17:65cd58826416 343 while(pc.readable()){ //characters in buffer, get them
elloway 17:65cd58826416 344 rx_buff[rx_index] = pc.getc();
elloway 17:65cd58826416 345 if(echo)pc.putc(rx_buff[rx_index]); //echo character
elloway 17:65cd58826416 346 //pc.printf("<[%02x] %c i[%d]>",rx_buff[i],rx_buff[i],i); //echo charater
elloway 17:65cd58826416 347
elloway 17:65cd58826416 348
elloway 17:65cd58826416 349 if(rx_buff[rx_index] == CR){
elloway 17:65cd58826416 350 //if(DEBUG)db.printf("\r\n");
elloway 17:65cd58826416 351 if(echo)pc.printf("\r\n");
elloway 17:65cd58826416 352 rx_buff[++rx_index] = 0;
elloway 17:65cd58826416 353 if(DEBUG)db.printf("%s",rx_buff);
elloway 17:65cd58826416 354 //pc.printf("%s\r\n",rx_buff);
elloway 17:65cd58826416 355 rx_index = -1; // because i++ at end of while give i=0 on next loop
elloway 17:65cd58826416 356 arg0 = 0;
elloway 17:65cd58826416 357 arg1 = 0;
elloway 17:65cd58826416 358 arg2 = 0;
elloway 17:65cd58826416 359
elloway 17:65cd58826416 360 n = sscanf(rx_buff, " %c %d %x %x", &c, &arg0, &arg1, &arg2);
elloway 17:65cd58826416 361
elloway 17:65cd58826416 362 //if(DEBUG)db.printf("c[%c] d[%d] a1[%x] a2[%x] n[%d]\r\n",c,arg0,arg1,arg2,n); //echo values read in
elloway 17:65cd58826416 363 //process input
elloway 17:65cd58826416 364 if(n > 0){//got input so process it
elloway 17:65cd58826416 365 switch(c){
elloway 17:65cd58826416 366
elloway 17:65cd58826416 367
elloway 17:65cd58826416 368 case 'd':
elloway 17:65cd58826416 369 case 'D': // Delay after convert command sent (ms)
elloway 17:65cd58826416 370 if(n == 2){
elloway 17:65cd58826416 371 delay = arg0;
elloway 17:65cd58826416 372 if(delay < 5)delay = 5;
elloway 17:65cd58826416 373 if(delay > 1000)delay = 1000;
elloway 17:65cd58826416 374 }
elloway 17:65cd58826416 375 if(echo)pc.printf("<delay = %dms>\r\n",delay);
elloway 17:65cd58826416 376 break;
elloway 17:65cd58826416 377
elloway 17:65cd58826416 378 case 'e':
elloway 17:65cd58826416 379 case 'E': // Turn echo ON/OFF
elloway 17:65cd58826416 380 if(arg0 == 0){
elloway 17:65cd58826416 381 echo = 0;
elloway 17:65cd58826416 382 }else{
elloway 17:65cd58826416 383 echo = 1;
elloway 17:65cd58826416 384 pc.printf("<echo on>\r\n");
elloway 17:65cd58826416 385 }
elloway 17:65cd58826416 386 break;
elloway 17:65cd58826416 387
elloway 17:65cd58826416 388 case 'f':
elloway 17:65cd58826416 389 case 'F': // change I2C Frequency
elloway 17:65cd58826416 390 if(n == 2){
elloway 17:65cd58826416 391 if((arg0 == 0)||(arg0 == 1)){
elloway 17:65cd58826416 392 i2c_freq = arg0;
elloway 17:65cd58826416 393 i2c.frequency(i2c_freq);
elloway 17:65cd58826416 394 }
elloway 17:65cd58826416 395 }
elloway 17:65cd58826416 396 if(echo)pc.printf("<I2C Frequency = %dkHz>\r\n",100*(i2c_freq*3 + 1));
elloway 17:65cd58826416 397 break;
elloway 17:65cd58826416 398
elloway 17:65cd58826416 399 case 'h':
elloway 17:65cd58826416 400 case 'H':
elloway 17:65cd58826416 401 case '?': //print command list
elloway 17:65cd58826416 402
elloway 17:65cd58826416 403
elloway 17:65cd58826416 404 pc.printf("Command Help\r\n");
elloway 17:65cd58826416 405
elloway 17:65cd58826416 406 pc.printf("\r\n");
elloway 17:65cd58826416 407 pc.printf("d ms Set delay time for read command after convert command in ms\r\n");
elloway 17:65cd58826416 408 pc.printf(" ms, Delay time in ms (int) \r\n");
elloway 17:65cd58826416 409 pc.printf(" 5ms through 10000ms allowed \r\n");
elloway 17:65cd58826416 410 pc.printf("\r\n");
elloway 17:65cd58826416 411
elloway 17:65cd58826416 412 pc.printf("e n Terminal command echo on/off\r\n");
elloway 17:65cd58826416 413 pc.printf(" n, Echo ON/OFF flag (int)\n\r");
elloway 17:65cd58826416 414 pc.printf(" 0 -> echo off\r\n");
elloway 17:65cd58826416 415 pc.printf(" 1 -> echo on\r\n");
elloway 17:65cd58826416 416 pc.printf("\r\n");
elloway 17:65cd58826416 417
elloway 17:65cd58826416 418 pc.printf("f n I2C Clock Frequency set\r\n");
elloway 17:65cd58826416 419 pc.printf(" n, Frequency Index (int)\n\r");
elloway 17:65cd58826416 420 pc.printf(" 0 -> 100kHz\r\n");
elloway 17:65cd58826416 421 pc.printf(" 1 -> 400kHz on\r\n");
elloway 17:65cd58826416 422 pc.printf("\r\n");
elloway 17:65cd58826416 423
elloway 17:65cd58826416 424 pc.printf("h,? Prints Command Help\r\n");
elloway 17:65cd58826416 425 pc.printf("\r\n");
elloway 17:65cd58826416 426
elloway 17:65cd58826416 427 pc.printf("l [ms] Start/Stop interval logging output to terminal\r\n");
elloway 17:65cd58826416 428 pc.printf(" ms, if included sets logging interval in ms (int)\r\n");
elloway 17:65cd58826416 429 pc.printf(" ms = 0 turns off loging\r\n");
elloway 17:65cd58826416 430 pc.printf("\r\n");
elloway 17:65cd58826416 431
elloway 17:65cd58826416 432 pc.printf("p ms Set logging sample period in ms\r\n");
elloway 17:65cd58826416 433 pc.printf(" ms, Convert period in ms (int) \r\n");
elloway 17:65cd58826416 434 pc.printf(" 20ms through 60,000ms allowed \r\n");
elloway 17:65cd58826416 435 pc.printf("\r\n");
elloway 17:65cd58826416 436
elloway 17:65cd58826416 437
elloway 17:65cd58826416 438 pc.printf("r d add [end_add] Read device regester\r\n");
elloway 17:65cd58826416 439 pc.printf(" d, device index (int)\r\n");
elloway 17:65cd58826416 440 pc.printf(" add, address to read (hex)\r\n");
elloway 17:65cd58826416 441 pc.printf(" end_add, if included read all registers from add to end_add (hex)\r\n");
elloway 17:65cd58826416 442 pc.printf("\r\n");
elloway 17:65cd58826416 443 pc.printf("s Search I2C bus for all attached devices\r\n");
elloway 17:65cd58826416 444 pc.printf("\r\n");
elloway 17:65cd58826416 445 pc.printf("t d [end_d] Send convert temperature command to device and read temperature\r\n");
elloway 17:65cd58826416 446 pc.printf(" d, device index (int)\r\n");
elloway 17:65cd58826416 447 pc.printf(" end_d, if included sends skip ROM convert command (int)\r\n");
elloway 17:65cd58826416 448 pc.printf(" and reads back from device d through end_d\r\n");
elloway 17:65cd58826416 449 pc.printf("\r\n");
elloway 17:65cd58826416 450 pc.printf("w d add data Write to device register\r\n");
elloway 17:65cd58826416 451 pc.printf(" d, device index (int)\r\n");
elloway 17:65cd58826416 452 pc.printf(" add, write address (hex)\r\n");
elloway 17:65cd58826416 453 pc.printf(" data, data to write (hex)\r\n");
elloway 17:65cd58826416 454 pc.printf("\r\n");
elloway 17:65cd58826416 455 pc.printf("v mV Set VDD of device\r\n");
elloway 17:65cd58826416 456 pc.printf(" mV, VDD in mV (int)\r\n");
elloway 17:65cd58826416 457 pc.printf(" 1800mV to 3600mV allowed in 100mV steps");
elloway 17:65cd58826416 458 pc.printf("\r\n");
elloway 17:65cd58826416 459
elloway 17:65cd58826416 460 break;
elloway 17:65cd58826416 461
elloway 17:65cd58826416 462
elloway 17:65cd58826416 463 case 'l':
elloway 17:65cd58826416 464 case 'L': // Toggle logging
elloway 17:65cd58826416 465 if((n==2)&&(arg0 == 0)){ //force stop
elloway 17:65cd58826416 466 if(echo)pc.printf("<stop logging [by 0]>\r\n");
elloway 17:65cd58826416 467 log_flag = false;
elloway 17:65cd58826416 468 timer_2.detach();
elloway 17:65cd58826416 469 sample_flag = false;
elloway 17:65cd58826416 470 }else{
elloway 17:65cd58826416 471
elloway 17:65cd58826416 472 if(n == 2){
elloway 17:65cd58826416 473 period = arg0;
elloway 17:65cd58826416 474 if(period < 5)period = 5;
elloway 17:65cd58826416 475 if(period > 60000)period = 60000;
elloway 17:65cd58826416 476 }
elloway 17:65cd58826416 477 if(log_flag == false){ //start logging
elloway 17:65cd58826416 478 if(echo)pc.printf("<start logging, period = %dms>\r\n",period);
elloway 17:65cd58826416 479 sample_count = 0;
elloway 17:65cd58826416 480 if(period > 1000){// dont wait for first tick
elloway 17:65cd58826416 481 sample_flag = true;
elloway 17:65cd58826416 482 }else{
elloway 17:65cd58826416 483 sample_flag = false;
elloway 17:65cd58826416 484 }
elloway 17:65cd58826416 485 log_flag = true;
elloway 17:65cd58826416 486 timer_2.attach_us(&set_sample_flag_callback,period*1000); //start ticker interupt.
elloway 17:65cd58826416 487
elloway 17:65cd58826416 488 }else{ // n != 2 and log_flag = true
elloway 17:65cd58826416 489 if(echo)pc.printf("<stop logging>\r\n");
elloway 17:65cd58826416 490 log_flag = false;
elloway 17:65cd58826416 491 timer_2.detach();
elloway 17:65cd58826416 492 sample_flag = false;
elloway 17:65cd58826416 493 }
elloway 17:65cd58826416 494 }// end else if((n==2)&&(arg0==0))
elloway 17:65cd58826416 495 break;
elloway 17:65cd58826416 496
elloway 17:65cd58826416 497 case 'p':
elloway 17:65cd58826416 498 case 'P': //Set sample period in ms
elloway 17:65cd58826416 499
elloway 17:65cd58826416 500 if(n == 2){
elloway 17:65cd58826416 501 period = arg0;
elloway 17:65cd58826416 502 if(period < 20)period = 20;
elloway 17:65cd58826416 503 if(period > 60000)period = 60000;
elloway 17:65cd58826416 504 }
elloway 17:65cd58826416 505 if(log_flag == true){ //update sample_flag interrupt period
elloway 17:65cd58826416 506 timer_2.detach(); // stop current timer
elloway 17:65cd58826416 507 if(echo)pc.printf("<period = %dmS>\r\n",period);
elloway 17:65cd58826416 508 timer_2.attach_us(&set_sample_flag_callback,period*1000); // restart with new period
elloway 17:65cd58826416 509 }else{
elloway 17:65cd58826416 510 if(echo)pc.printf("<set sampling period, %dmS>\r\n",period);
elloway 17:65cd58826416 511 }
elloway 17:65cd58826416 512 break;
elloway 17:65cd58826416 513
elloway 17:65cd58826416 514
elloway 17:65cd58826416 515 case 'r':
elloway 17:65cd58826416 516 case 'R': //read register "r device radd.start radd.end"
elloway 17:65cd58826416 517 if(n==3){ //read single register from selected device
elloway 17:65cd58826416 518 OT07_read_register(OT07[arg0].I2C_address,arg1,data,1);
elloway 17:65cd58826416 519 if(echo){
elloway 17:65cd58826416 520 pc.printf("device[%02d] add[%02X] data[%02X] \r\n",arg0,arg1,data[0]);
elloway 17:65cd58826416 521 }else{
elloway 17:65cd58826416 522 pc.printf("%02d, %02X, %02X\r\n",arg0,arg1,data[0]);
elloway 17:65cd58826416 523 }
elloway 17:65cd58826416 524 }
elloway 17:65cd58826416 525 if(n==4){ //read a range of regesters from selected device
elloway 17:65cd58826416 526 num_bytes = arg2-arg1 + 1; // calculate number of bytes to read
elloway 17:65cd58826416 527 if (num_bytes < 1) num_bytes = 1; // if arg2 <= arg 1 just read arg1 address.
elloway 17:65cd58826416 528 OT07_read_register(OT07[arg0].I2C_address,arg1,data,num_bytes);
elloway 17:65cd58826416 529 for(i=0;i<num_bytes;i++){
elloway 17:65cd58826416 530 if(echo){
elloway 17:65cd58826416 531 pc.printf("\r\ndevice[%02d] add[%02X] data[%02X]",arg0,arg1+i,data[i]);
elloway 17:65cd58826416 532 }else{
elloway 17:65cd58826416 533 pc.printf("%02d, %02X, %02X\r\n",arg0,arg1+i,data[i]);
elloway 17:65cd58826416 534 }
elloway 17:65cd58826416 535 }
elloway 17:65cd58826416 536 if(echo)pc.printf("\r\n");
elloway 17:65cd58826416 537 }
elloway 17:65cd58826416 538 break;
elloway 17:65cd58826416 539
elloway 17:65cd58826416 540 case 's':
elloway 17:65cd58826416 541 case 'S':
elloway 17:65cd58826416 542 // ****************** search for I2C devices on bus *****************
elloway 17:65cd58826416 543
elloway 17:65cd58826416 544 device_count = search_I2C_bus(OT07);
elloway 17:65cd58826416 545 if(echo){
elloway 17:65cd58826416 546 pc.printf("<search I2C bus>\r\n");
elloway 17:65cd58826416 547 pc.printf("<count>\r\n");
elloway 17:65cd58826416 548 pc.printf("<device, I2C Add, CRC, ROM ID, Family>\r\n");
elloway 17:65cd58826416 549 }
elloway 17:65cd58826416 550 pc.printf("%2d\r\n",device_count);
elloway 17:65cd58826416 551 print_device_list(OT07,device_count);
elloway 17:65cd58826416 552
elloway 17:65cd58826416 553 break;
elloway 17:65cd58826416 554
elloway 17:65cd58826416 555 case 'T':
elloway 17:65cd58826416 556 case 't':
elloway 17:65cd58826416 557 if(n == 2){//get temperatures from selected device
elloway 17:65cd58826416 558
elloway 17:65cd58826416 559 convert_temperature(OT07[arg0].I2C_address); //send OW convert selected device
elloway 17:65cd58826416 560 wait(0.02); //wait 20 ms for convert temperature to complete
elloway 17:65cd58826416 561 T[arg0] = get_temperature(OT07[arg0].I2C_address);
elloway 17:65cd58826416 562 if(echo)pc.printf("<temperature>\r\n");
elloway 17:65cd58826416 563 pc.printf("%02d, %7.3f\r\n",arg0, T[arg0]);
elloway 17:65cd58826416 564 }
elloway 17:65cd58826416 565
elloway 17:65cd58826416 566 if(n == 3){ // "t 1 3" get temperature for devices 1 thru 3
elloway 17:65cd58826416 567 sprintf(str,"%x",arg1); //convert arg1 input as hex to decimal i.e. 0x10 becomes 10 dec
elloway 17:65cd58826416 568 sscanf(str,"%d",&arg1);
elloway 17:65cd58826416 569
elloway 17:65cd58826416 570 for(j=arg0;j<=arg1;j++){
elloway 17:65cd58826416 571 convert_temperature(OT07[j].I2C_address); //send convert to all devices
elloway 17:65cd58826416 572 wait(0.02); //wait 20ms for convert temperature to complete
elloway 17:65cd58826416 573 T[j] = get_temperature(OT07[j].I2C_address);
elloway 17:65cd58826416 574 }
elloway 17:65cd58826416 575
elloway 17:65cd58826416 576 if(echo)pc.printf("<device, temperature>\r\n");
elloway 17:65cd58826416 577 for(j=arg0;j<=arg1;j++){
elloway 17:65cd58826416 578 pc.printf("%02d, %7.3f\r\n",j,T[j]);
elloway 17:65cd58826416 579 }
elloway 17:65cd58826416 580 }
elloway 17:65cd58826416 581
elloway 17:65cd58826416 582 break;
elloway 17:65cd58826416 583 case 'w':
elloway 17:65cd58826416 584 case 'W': //write register
elloway 17:65cd58826416 585 //data[0] = arg2;
elloway 17:65cd58826416 586 OT07_write_register(OT07[arg0].I2C_address,arg1, arg2);
elloway 17:65cd58826416 587 if(echo)pc.printf("write -- add[%02X] data[%02X]\r\n",arg1,arg2);
elloway 17:65cd58826416 588
elloway 17:65cd58826416 589 break;
elloway 17:65cd58826416 590
elloway 17:65cd58826416 591
elloway 17:65cd58826416 592 case 'v':
elloway 17:65cd58826416 593 case 'V': //Set MAX32630FTHR I/O Voltage 0 -> 1.8V, 1 -> 3.3V
elloway 17:65cd58826416 594
elloway 17:65cd58826416 595 if(n == 2){ //set LDO3
elloway 17:65cd58826416 596 L3out = arg0;
elloway 17:65cd58826416 597 if(L3out < 1600)L3out = 1600; // check for valid mV range
elloway 17:65cd58826416 598 if(L3out > 3600)L3out = 3600;
elloway 17:65cd58826416 599 L3out = ((L3out+50)/100)*100; // round to 100mV steps
elloway 17:65cd58826416 600 pegasus.max14690.ldo3SetVoltage(L3out); // set ldo3 to L3out
elloway 17:65cd58826416 601 if(echo)pc.printf("<Set ldo3 (VDD) to %4dmV, ",L3out);
elloway 17:65cd58826416 602 if(DEBUG)db.printf("<ldo3 to %4dmV>\r\n",L3out);
elloway 17:65cd58826416 603
elloway 17:65cd58826416 604 if(L3out >= 1900){ //set VIO to 3300mV
elloway 17:65cd58826416 605 // set VIO to 3v3
elloway 17:65cd58826416 606 pegasus.vddioh(P3_4, pegasus.VIO_3V3); // SDA use LDO2
elloway 17:65cd58826416 607 pegasus.vddioh(P3_5, pegasus.VIO_3V3); // SCL use LDO2
elloway 17:65cd58826416 608 pegasus.vddioh(P5_3, pegasus.VIO_3V3); // GPIO0 use LDO2
elloway 17:65cd58826416 609 pegasus.vddioh(P5_6, pegasus.VIO_3V3); // GPIO1 use LDO2
elloway 17:65cd58826416 610 if(echo)pc.printf(" Set VIO=3V3>\r\n");
elloway 17:65cd58826416 611 }else{//set VIO to 1V8
elloway 17:65cd58826416 612 pegasus.vddioh(P3_4, pegasus.VIO_1V8); // SDA use 1V8 buck
elloway 17:65cd58826416 613 pegasus.vddioh(P3_5, pegasus.VIO_1V8); // SCL use 1V8 buck
elloway 17:65cd58826416 614 pegasus.vddioh(P5_3, pegasus.VIO_1V8); // GPIO0 use 1V8 buck
elloway 17:65cd58826416 615 pegasus.vddioh(P5_6, pegasus.VIO_1V8); // GPIO1 use 1V8 buck
elloway 17:65cd58826416 616 if(echo)pc.printf(" Set VIO=1V8>\r\n");
elloway 17:65cd58826416 617 }
elloway 17:65cd58826416 618 }else{
elloway 17:65cd58826416 619 if(echo)pc.printf("<ldo3 (VDD) %4dmV>\r\n",L3out);
elloway 17:65cd58826416 620 }
elloway 17:65cd58826416 621 break;
elloway 17:65cd58826416 622
elloway 17:65cd58826416 623 }//end switch(c)
elloway 17:65cd58826416 624 }//if(n>0)
elloway 17:65cd58826416 625 }//end if(CR)
elloway 17:65cd58826416 626 if(rx_buff[rx_index] == BS){ //backspace received, back up buffer pointer
elloway 17:65cd58826416 627 if(rx_index>0)rx_index--; //remove last char from buffer if not at start.
elloway 17:65cd58826416 628 }else rx_index++;
elloway 17:65cd58826416 629 }//end while(pc.redable())
elloway 17:65cd58826416 630
elloway 17:65cd58826416 631 }//end while(1) Main Loop
elloway 17:65cd58826416 632 }// end_Main