Virtual COM AO31
Dependencies: USBDevice
Diff: main.cpp
- Revision:
- 28:f86ec29d719c
- Parent:
- 27:6e67717398b6
diff -r 6e67717398b6 -r f86ec29d719c main.cpp --- a/main.cpp Fri Aug 14 04:08:46 2020 +0000 +++ b/main.cpp Wed Dec 29 07:01:43 2021 +0000 @@ -1,3 +1,9 @@ +// This program is for interacting with the MAX20361 (AO31) +// The PMIC registers: I2C address 0x50/0x51 +// The Haptic driver registers: I2C address 0xA0/0xA1 +// The MAX17260 m5 Fuel Gauge: I2C address 0x6C/0x6D. The register's length is 16 bits. + + #include "mbed.h" #include "USBSerial.h" @@ -83,7 +89,7 @@ } // ***************************************************************************** -// I2C_read_register(char, char, char *, int) writes single byte to OT07 +// I2C_read_register(char, char, char *, int) reads byte(s) from OT07 // char I2C address // char OT07 register address // char * data vector for read bytes to be stored in @@ -102,8 +108,6 @@ int I2C_read(char I2C_add, char *bytes, int n) { int error; - -// error = i2c.write(I2C_add, 0, 0, 1); i2c.start(); // create a start bit error = i2c.write(I2C_add); @@ -127,39 +131,33 @@ // ***************************************************************************** void process_command(char rx_buff[128]) { - char data[130]; + char data[130]; // for storing read results + char wdata[10]; // for storing write bytes int i; int num_bytes; char c; int arg1 = 0; int arg2 = 0; int arg3 = 0; - int n = sscanf(rx_buff, " %c %x %x %x", &c, &arg1, &arg2, &arg3); + int arg4 = 0; +// int n = sscanf(rx_buff, " %c %x %x %x", &c, &arg1, &arg2, &arg3); + int n = sscanf(rx_buff, " %c %x %x %x %x", &c, &arg1, &arg2, &arg3, &arg4); //process input if (n > 0) {//got input so process it switch (c) { case 'r': case 'R': - //read register "r slave.add radd.start radd.end" - if (n == 2) { //read device + //read register "r slave.add" + if (n == 2) { int error = I2C_read(arg1, data, 2); if (error) pc.printf("nack\r\n"); else pc.printf("%02X %02X\r\n", data[0], data[1]); - - // use PICO AIN0 to cross check - //nalogIn ain(AIN_0); -// int samples[5]; -// for (int i = 0; i < 5; i++) { -// samples[i] = ain.read_u16(); -// pc.printf("Attempt %d is: %d \r\n", i, samples[i]); -// } -// pc.printf("Average is: %d \r\n", (samples[0] + samples[1] + samples[2] + samples[3] + samples[4]) / 5); } - //read register "r slave.add radd.start radd.end" + //read register "r slave.add radd" if (n == 3) { //read single register from selected device int error = I2C_read_register(arg1, arg2, data, 1); if (error) @@ -167,7 +165,9 @@ else pc.printf("%02X\r\n", data[0]); } - if (n == 4) { //read a range of regesters from selected device + + //read register "r slave.add radd.start radd.end" + if (n == 4) { //read a range of registers from selected device num_bytes = arg3 - arg2 + 1;// calculate number of bytes to read if (num_bytes < 1) num_bytes = 1;// if arg2 <= arg 1 just read arg1 address. I2C_read_register(arg1, arg2, data, num_bytes); @@ -175,14 +175,39 @@ pc.printf("%02X\r\n", data[i]); } } + + //read m5 fuel gauge register "r slave.add radd 2 5" + if (n == 5) { + I2C_read_register(arg1, arg2, data, 2); +// pc.printf("m5\r\n"); + pc.printf("%02X%02X\r\n", data[1], data[0]); + } + break; case 'w': - case 'W': //write register "w device w.addr data" - int error = I2C_write_register(arg1, arg2, arg3); - if (error) - pc.printf("nack\r\n"); - else - pc.printf("ack\r\n"); + case 'W': + + //write register "w device w.addr data" + if (n == 4) { + int error = I2C_write_register(arg1, arg2, arg3); //int I2C_write_register(char I2C_add, char reg_add, char byte) + if (error) + pc.printf("nack\r\n"); + else + pc.printf("ack\r\n"); + } + + //write m5 fuel gauge register "w device w.addr data.upperbyte data.lowerbyte" + if (n == 5) { + wdata[0] = arg4; + wdata[1] = arg3; +// pc.printf("%02X%02X\r\n", wdata[1], wdata[0]); + int error = I2C_write_register(arg1, arg2, wdata, 2); // I2C_write_register(char I2C_add, char reg_add, char *bytes, int n) + if (error) + pc.printf("nack\r\n"); + else + pc.printf("ack\r\n"); + } + break; }//end switch(c) }