From c3162c968ea7e701700b1f2730f403d751043e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Wed, 18 Nov 2020 01:11:00 +0000 Subject: [PATCH 1/5] Small fix when using write_68, read_68, and cmd_68 - write_68, read_68, and cmd_68 explicitly define that the command input should be an array with two bytes. - also fixed a small indent error in LTC681x_stsctrl() --- LTSketchbook/libraries/LTC681x/LTC681x.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/LTSketchbook/libraries/LTC681x/LTC681x.cpp b/LTSketchbook/libraries/LTC681x/LTC681x.cpp index 272bd8f4..25266958 100644 --- a/LTSketchbook/libraries/LTC681x/LTC681x.cpp +++ b/LTSketchbook/libraries/LTC681x/LTC681x.cpp @@ -368,7 +368,7 @@ void LTC681x_adax(uint8_t MD, //ADC Mode uint8_t CHG //GPIO Channels to be measured ) { - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t md_bits; md_bits = (MD & 0x02) >> 1; @@ -384,7 +384,7 @@ void LTC681x_adstat(uint8_t MD, //ADC Mode uint8_t CHST //Stat Channels to be measured ) { - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t md_bits; md_bits = (MD & 0x02) >> 1; @@ -1039,7 +1039,7 @@ void LTC681x_adaxd(uint8_t MD, //ADC Mode uint8_t CHG //GPIO Channels to be measured ) { - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t md_bits; md_bits = (MD & 0x02) >> 1; @@ -1741,7 +1741,7 @@ int8_t LTC681x_rdpwm(uint8_t total_ic, //Number of ICs in the system ) { const uint8_t BYTES_IN_REG = 8; - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t read_buffer[256]; int8_t pec_error = 0; uint16_t data_pec; @@ -1829,7 +1829,7 @@ int8_t LTC681x_rdsctrl(uint8_t total_ic, // Number of ICs in the daisy chain cell_asic *ic // A two dimensional array that the function stores the read data ) { - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t read_buffer[256]; int8_t pec_error = 0; uint16_t data_pec; @@ -1878,7 +1878,7 @@ This command will start the sctrl pulse communication over the spins */ void LTC681x_stsctrl() { - uint8_t cmd[4]; + uint8_t cmd[4]; uint16_t cmd_pec; cmd[0] = 0x00; From f57b0ab60178c4a13215165d75eeec9b4a62b9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Wed, 18 Nov 2020 01:23:25 +0000 Subject: [PATCH 2/5] Remove data from read_68 and use rx_data This saves about 130 bytes of memory (at least when I compile for ATmega2560 and ATmega2561). The for loop I removed basically just copies data to rx_data with exactly the same indexes so I don't really see why we need data. Simplifies the code a little bit :) --- LTSketchbook/libraries/LTC681x/LTC681x.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/LTSketchbook/libraries/LTC681x/LTC681x.cpp b/LTSketchbook/libraries/LTC681x/LTC681x.cpp index 272bd8f4..67436872 100644 --- a/LTSketchbook/libraries/LTC681x/LTC681x.cpp +++ b/LTSketchbook/libraries/LTC681x/LTC681x.cpp @@ -144,7 +144,6 @@ int8_t read_68( uint8_t total_ic, // Number of ICs in the system { const uint8_t BYTES_IN_REG = 8; uint8_t cmd[4]; - uint8_t data[256]; int8_t pec_error = 0; uint16_t cmd_pec; uint16_t data_pec; @@ -157,15 +156,11 @@ int8_t read_68( uint8_t total_ic, // Number of ICs in the system cmd[3] = (uint8_t)(cmd_pec); cs_low(CS_PIN); - spi_write_read(cmd, 4, data, (BYTES_IN_REG*total_ic)); //Transmits the command and reads the configuration data of all ICs on the daisy chain into rx_data[] array + spi_write_read(cmd, 4, rx_data, (BYTES_IN_REG*total_ic)); //Transmits the command and reads the configuration data of all ICs on the daisy chain into rx_data[] array cs_high(CS_PIN); - for (uint8_t current_ic = 0; current_ic < total_ic; current_ic++) //Executes for each LTC681x in the daisy chain and packs the data - { //into the rx_data array as well as check the received data for any bit errors - for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++) - { - rx_data[(current_ic*8)+current_byte] = data[current_byte + (current_ic*BYTES_IN_REG)]; - } + for (uint8_t current_ic = 0; current_ic < total_ic; current_ic++) // Executes for each LTC681x in the daisy chain + { // and check the received data for any bit errors received_pec = (rx_data[(current_ic*8)+6]<<8) + rx_data[(current_ic*8)+7]; data_pec = pec15_calc(6, &rx_data[current_ic*8]); @@ -176,7 +171,7 @@ int8_t read_68( uint8_t total_ic, // Number of ICs in the system } } - return(pec_error); + return pec_error; } /* Calculates and returns the CRC15 */ From 46dbd6fecb550a8477de5de1bb3bc1d3f04a45cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 6 Dec 2020 16:15:39 +0000 Subject: [PATCH 3/5] - Removed two unused variables in LTC6810_rds_reg - Changed cmd[4] to cmd[2] in various places. --- LTSketchbook/libraries/LTC6810/LTC6810.cpp | 6 ++---- LTSketchbook/libraries/LTC6812/LTC6812.cpp | 6 +++--- LTSketchbook/libraries/LTC6813/LTC6813.cpp | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/LTSketchbook/libraries/LTC6810/LTC6810.cpp b/LTSketchbook/libraries/LTC6810/LTC6810.cpp index aba1598f..e6fb3b92 100644 --- a/LTSketchbook/libraries/LTC6810/LTC6810.cpp +++ b/LTSketchbook/libraries/LTC6810/LTC6810.cpp @@ -258,9 +258,7 @@ void LTC6810_rds_reg(uint8_t reg, //Determines which S voltage register is read uint8_t *data //An array of the unparsed cell codes ) { - const uint8_t REG_LEN = 8; //Number of bytes in each ICs register + 2 bytes for the PEC - uint8_t cmd[4]; - uint16_t cmd_pec; + uint8_t cmd[2]; if (reg == 1) // RDSA { @@ -586,7 +584,7 @@ uint8_t LTC6810_rdsid(uint8_t total_ic, // The number of ICs in the system cell_asic *ic //A two dimensional array that the function stores the read data. ) { - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t read_buffer[256]; int8_t pec_error = 0; uint16_t data_pec; diff --git a/LTSketchbook/libraries/LTC6812/LTC6812.cpp b/LTSketchbook/libraries/LTC6812/LTC6812.cpp index a5979486..ef12877f 100644 --- a/LTSketchbook/libraries/LTC6812/LTC6812.cpp +++ b/LTSketchbook/libraries/LTC6812/LTC6812.cpp @@ -527,7 +527,7 @@ int8_t LTC6812_rdpsb(uint8_t total_ic, //Number of ICs in the daisy chain cell_asic *ic //A two dimensional array that the function stores the read data ) { - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t read_buffer[256]; int8_t pec_error = 0; uint16_t data_pec; @@ -535,8 +535,8 @@ int8_t LTC6812_rdpsb(uint8_t total_ic, //Number of ICs in the daisy chain uint8_t c_ic = 0; - cmd[0] = 0x00; - cmd[1] = 0x1E; + cmd[0] = 0x00; + cmd[1] = 0x1E; pec_error = read_68(total_ic, cmd, read_buffer); diff --git a/LTSketchbook/libraries/LTC6813/LTC6813.cpp b/LTSketchbook/libraries/LTC6813/LTC6813.cpp index a5338de9..3f0ed997 100644 --- a/LTSketchbook/libraries/LTC6813/LTC6813.cpp +++ b/LTSketchbook/libraries/LTC6813/LTC6813.cpp @@ -534,7 +534,7 @@ uint8_t LTC6813_rdpsb(uint8_t total_ic, //< number of ICs in the daisy chain cell_asic *ic //< a two dimensional array that the function stores the read data ) { - uint8_t cmd[4]; + uint8_t cmd[2]; uint8_t read_buffer[256]; int8_t pec_error = 0; uint16_t data_pec; From f008145c991071b84b38581707b6909fb6fa95de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Wed, 9 Dec 2020 00:24:57 +0000 Subject: [PATCH 4/5] - Simplified read_68() further. - Fix for LTC6813_rdpsb(). Now returns proper -1. - Fix potential overflow in LTC6810_rdsid. It also returns proper -1. - Removed unused variables in LTC6810_rds_reg() - LTC681x_rdaux_reg now uses read_68() - LTC681x_rdcv_reg now uses read_68() - LTC681x_rdstat_reg now uses read_68() --- LTSketchbook/libraries/LTC6810/LTC6810.cpp | 32 +++++----- LTSketchbook/libraries/LTC6810/LTC6810.h | 2 +- LTSketchbook/libraries/LTC6812/LTC6812.cpp | 9 ++- LTSketchbook/libraries/LTC6813/LTC6813.cpp | 7 +-- LTSketchbook/libraries/LTC6813/LTC6813.h | 2 +- LTSketchbook/libraries/LTC681x/LTC681x.cpp | 73 ++++++---------------- LTSketchbook/libraries/LTC681x/LTC681x.h | 8 +-- 7 files changed, 48 insertions(+), 85 deletions(-) diff --git a/LTSketchbook/libraries/LTC6810/LTC6810.cpp b/LTSketchbook/libraries/LTC6810/LTC6810.cpp index aba1598f..b2f6bdf1 100644 --- a/LTSketchbook/libraries/LTC6810/LTC6810.cpp +++ b/LTSketchbook/libraries/LTC6810/LTC6810.cpp @@ -258,9 +258,7 @@ void LTC6810_rds_reg(uint8_t reg, //Determines which S voltage register is read uint8_t *data //An array of the unparsed cell codes ) { - const uint8_t REG_LEN = 8; //Number of bytes in each ICs register + 2 bytes for the PEC - uint8_t cmd[4]; - uint16_t cmd_pec; + uint8_t cmd[2]; if (reg == 1) // RDSA { @@ -582,7 +580,7 @@ void LTC6810_stcomm(uint8_t len) //Length of data to be transmitted } /* Reads Serial ID registers group. */ -uint8_t LTC6810_rdsid(uint8_t total_ic, // The number of ICs in the system +int8_t LTC6810_rdsid(uint8_t total_ic, // The number of ICs in the system cell_asic *ic //A two dimensional array that the function stores the read data. ) { @@ -597,23 +595,29 @@ uint8_t LTC6810_rdsid(uint8_t total_ic, // The number of ICs in the system cmd[0] = 0x00; cmd[1] = 0x2C; - pec_error = read_68(total_ic, cmd, read_buffer); + read_68(total_ic, cmd, read_buffer); for(uint8_t current_ic =0; current_ic> 8); - cmd[3] = (uint8_t)(cmd_pec); - - cs_low(CS_PIN); - spi_write_read(cmd,4,data,(REG_LEN*total_ic)); - cs_high(CS_PIN); + read_68(total_ic, cmd, data); } /* @@ -745,9 +721,7 @@ void LTC681x_rdaux_reg(uint8_t reg, //Determines which GPIO voltage register is uint8_t *data //Array of the unparsed auxiliary codes ) { - const uint8_t REG_LEN = 8; // Number of bytes in the register + 2 bytes for the PEC - uint8_t cmd[4]; - uint16_t cmd_pec; + uint8_t cmd[2]; if (reg == 1) //Read back auxiliary group A { @@ -775,13 +749,7 @@ void LTC681x_rdaux_reg(uint8_t reg, //Determines which GPIO voltage register is cmd[0] = 0x00; } - cmd_pec = pec15_calc(2, cmd); - cmd[2] = (uint8_t)(cmd_pec >> 8); - cmd[3] = (uint8_t)(cmd_pec); - - cs_low(CS_PIN); - spi_write_read(cmd,4,data,(REG_LEN*total_ic)); - cs_high(CS_PIN); + read_68(total_ic, cmd, data); } /* @@ -794,9 +762,7 @@ void LTC681x_rdstat_reg(uint8_t reg, //Determines which stat register is read ba uint8_t *data //Array of the unparsed stat codes ) { - const uint8_t REG_LEN = 8; // number of bytes in the register + 2 bytes for the PEC - uint8_t cmd[4]; - uint16_t cmd_pec; + uint8_t cmd[2]; if (reg == 1) //Read back status group A { @@ -815,13 +781,7 @@ void LTC681x_rdstat_reg(uint8_t reg, //Determines which stat register is read ba cmd[0] = 0x00; } - cmd_pec = pec15_calc(2, cmd); - cmd[2] = (uint8_t)(cmd_pec >> 8); - cmd[3] = (uint8_t)(cmd_pec); - - cs_low(CS_PIN); - spi_write_read(cmd,4,data,(REG_LEN*total_ic)); - cs_high(CS_PIN); + read_68(total_ic, cmd, data); } /* Helper function that parses voltage measurement registers */ @@ -1754,7 +1714,7 @@ int8_t LTC681x_rdpwm(uint8_t total_ic, //Number of ICs in the system cmd[1] = 0x1E; } - pec_error = read_68(total_ic, cmd, read_buffer); + read_68(total_ic, cmd, read_buffer); for (uint8_t current_ic =0; current_icisospi_reverse == false) @@ -1775,6 +1735,7 @@ int8_t LTC681x_rdpwm(uint8_t total_ic, //Number of ICs in the system data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8); if (calc_pec != data_pec ) { + pec_error = -1; ic[c_ic].pwm.rx_pec_match = 1; } else ic[c_ic].pwm.rx_pec_match = 0; @@ -1842,7 +1803,7 @@ int8_t LTC681x_rdsctrl(uint8_t total_ic, // Number of ICs in the daisy chain cmd[1] = 0x1E; } - pec_error = read_68(total_ic, cmd, read_buffer); + read_68(total_ic, cmd, read_buffer); for(uint8_t current_ic =0; current_ic Date: Wed, 9 Dec 2020 00:41:58 +0000 Subject: [PATCH 5/5] Small indent fixes --- LTSketchbook/libraries/LTC681x/LTC681x.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/LTSketchbook/libraries/LTC681x/LTC681x.cpp b/LTSketchbook/libraries/LTC681x/LTC681x.cpp index f1d9710a..fa4f0aa7 100644 --- a/LTSketchbook/libraries/LTC681x/LTC681x.cpp +++ b/LTSketchbook/libraries/LTC681x/LTC681x.cpp @@ -271,7 +271,7 @@ int8_t LTC681x_rdcfg(uint8_t total_ic, //Number of ICs in the system data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8); if (calc_pec != data_pec ) { - pec_error = -1; + pec_error = -1; ic[c_ic].config.rx_pec_match = 1; } else ic[c_ic].config.rx_pec_match = 0; @@ -315,7 +315,7 @@ int8_t LTC681x_rdcfgb(uint8_t total_ic, //Number of ICs in the system data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8); if (calc_pec != data_pec ) { - pec_error = -1; + pec_error = -1; ic[c_ic].configb.rx_pec_match = 1; } else ic[c_ic].configb.rx_pec_match = 0; @@ -708,7 +708,7 @@ void LTC681x_rdcv_reg(uint8_t reg, //Determines which cell voltage register is r cmd[0] = 0x00; } - read_68(total_ic, cmd, data); + read_68(total_ic, cmd, data); } /* @@ -749,7 +749,7 @@ void LTC681x_rdaux_reg(uint8_t reg, //Determines which GPIO voltage register is cmd[0] = 0x00; } - read_68(total_ic, cmd, data); + read_68(total_ic, cmd, data); } /* @@ -781,7 +781,7 @@ void LTC681x_rdstat_reg(uint8_t reg, //Determines which stat register is read ba cmd[0] = 0x00; } - read_68(total_ic, cmd, data); + read_68(total_ic, cmd, data); } /* Helper function that parses voltage measurement registers */ @@ -1735,7 +1735,7 @@ int8_t LTC681x_rdpwm(uint8_t total_ic, //Number of ICs in the system data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8); if (calc_pec != data_pec ) { - pec_error = -1; + pec_error = -1; ic[c_ic].pwm.rx_pec_match = 1; } else ic[c_ic].pwm.rx_pec_match = 0; @@ -1749,7 +1749,7 @@ void LTC681x_wrsctrl(uint8_t total_ic, // Number of ICs in the daisy chain cell_asic *ic // A two dimensional array that stores the data to be written ) { - uint8_t cmd[2]; + uint8_t cmd[2]; uint8_t write_buffer[256]; uint8_t write_count = 0; uint8_t c_ic = 0; @@ -1835,7 +1835,7 @@ This command will start the sctrl pulse communication over the spins */ void LTC681x_stsctrl() { - uint8_t cmd[4]; + uint8_t cmd[4]; uint16_t cmd_pec; cmd[0] = 0x00; @@ -1923,7 +1923,7 @@ int8_t LTC681x_rdcomm(uint8_t total_ic, //Number of ICs in the system data_pec = read_buffer[7+(8*current_ic)] | (read_buffer[6+(8*current_ic)]<<8); if (calc_pec != data_pec ) { - pec_error = -1; + pec_error = -1; ic[c_ic].com.rx_pec_match = 1; } else ic[c_ic].com.rx_pec_match = 0;