From 23300d2c68084c129fc28becc4325c7c3f05f015 Mon Sep 17 00:00:00 2001 From: Daniel Nakhooda Date: Sun, 6 Oct 2024 17:33:39 -0400 Subject: [PATCH 1/4] Finished Refactoring LTC4015 Ticket --- general/include/LTC4015.h | 56 ++++++++++++--------------- general/src/LTC4015.c | 81 ++++++++++++++++++++------------------- 2 files changed, 66 insertions(+), 71 deletions(-) diff --git a/general/include/LTC4015.h b/general/include/LTC4015.h index 76a59b5..2601f02 100644 --- a/general/include/LTC4015.h +++ b/general/include/LTC4015.h @@ -12,45 +12,43 @@ #include #include "stm32xx_hal.h" -//Device address -#define LTC4015_ADDR_68 (0x68) +//Device address +#define LTC4015_ADDR_68 (0x68) //Enable Register for Specified alert register, page 49-50 -#define EN_LIMIT_ALERTS 0x0D //LIMIT_ALERTS +#define EN_LIMIT_ALERTS 0x0D //LIMIT_ALERTS #define EN_CHARGER_STATE_ALERTS 0x0E //CHARGER_STATUS_ALERTS #define EN_CHARGE_STATUS_ALERTS 0x0F //CHARGE_STATUS_ALERTS //Register storing if specifed fault has occurred, each bit represents different fault, page 52-53 #define LTC4015_CHGSTATE 0x34 //Real time battery charger state indicator -#define CHARGE_STATUS 0x35 +#define CHARGE_STATUS 0x35 -//Errors need to be enabled, with remain high once flagged till reset or disabled -#define LIMIT_ALERTS 0x36 +//Errors need to be enabled, with remain high once flagged till reset or disabled +#define LIMIT_ALERTS 0x36 #define CHARGER_STATE_ALERTS 0x37 #define CHARGE_STATUS_ALERTS 0x38 -#define SYSTEM_STATUS 0x39 - - +#define SYSTEM_STATUS 0x39 //Coulomb Counter -#define QCOUNT_LO_ALERT_LIMIT 0x10 -#define QCOUNT_HI_ALERT_LIMIT 0x11 +#define QCOUNT_LO_ALERT_LIMIT 0x10 +#define QCOUNT_HI_ALERT_LIMIT 0x11 #define QCOUNT_PRESCALE_FACTOR 0x12 -#define QCOUNT 0x13 -#define CONFIG_BITS 0x14 +#define QCOUNT 0x13 +#define CONFIG_BITS 0x14 //Enable Coulomb Counter Low Alert.(0xOD Bit 13) //Enable Coulomb Counter High Alert.(0xOD Bit 12) -typedef struct -{ - I2C_HandleTypeDef *i2cHandle; - uint16_t chargeFaults; //Stores error faults from CHGSTATE register - uint16_t qcount; - uint16_t limitAlerts; - +typedef int (*Mem_Read)(uint16_t DevAddress, uint16_t MemAddress, + uint16_t MemAddSize, uint8_t *pData, uint16_t Size); -}LTC4015_T; +typedef struct { + uint16_t chargeFaults; //Stores error faults from CHGSTATE register + uint16_t qcount; + uint16_t limitAlerts; + Mem_Read local_mem_read; +} LTC4015_T; /** * @brief Initializes the LTC4015EUHF#PBF @@ -59,8 +57,7 @@ typedef struct * @param hi2c * @return HAL_StatusTypeDef */ -HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle); - +int LTC4015_Init(LTC4015_T *dev); /** * @brief Reads from the LTC4015EUHF#PBF load switch @@ -69,8 +66,7 @@ HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle); * @param dev * @param i2c_handle */ -HAL_StatusTypeDef LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data); - +int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data); /** * @brief Writes to the LTC4015EUHF#PBF load switch @@ -80,8 +76,7 @@ HAL_StatusTypeDef LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data); * @param i2c_handle * @return HAL_StatusTypeDef */ -HAL_StatusTypeDef LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data); - +int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data); /** * @brief Checks coulombs of charger to see if within a given range, more info on page 38-40 @@ -93,12 +88,11 @@ HAL_StatusTypeDef LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data); * @param lowAlert * @return HAL_StatusTypeDef, QCOUNT, Faulted */ -HAL_StatusTypeDef LTC4015_Qcounter(LTC4015_T *dev,uint16_t prescaler, uint16_t highAlert, uint16_t lowAlert); - +int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert, + uint16_t lowAlert); #ifndef MAX_NUM_LTC4015_INSTANCES #define MAX_NUM_LTC4015_INSTANCES 1 #endif - -#endif +#endif diff --git a/general/src/LTC4015.c b/general/src/LTC4015.c index 3d8d9b3..0346138 100644 --- a/general/src/LTC4015.c +++ b/general/src/LTC4015.c @@ -8,58 +8,59 @@ #include "LTC4015.h" -HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle) +int LTC4015_Init(LTC4015_T *dev) { - dev->i2cHandle = i2cHandle; - - //Gets the value from the Charging state register - LtC4015_read(dev, LTC4015_CHGSTATE, dev->CHGdata); - - + //Gets the value from the Charging state register + LtC4015_read(dev, LTC4015_CHGSTATE, dev->CHGdata); } - -HAL_StatusTypeDef LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data) +int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data) { - return HAL_I2C_Mem_Read(dev->i2c_handle, LTC4015_ADDR_68, reg, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY) - + return dev->local_mem_read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); } -HAL_StatusTypeDef LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data) +int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data) { - return HAL_I2C_Mem_Write(dev->i2c_handle, LTC4015_ADDR_68, reg, I2C_MEMADD_SIZE_8BIT, data, 1, HAL_MAX_DELAY) + return dev->local_mem_read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); } -HAL_StatusTypeDef LTC4015_Qcounter(LTC4015_T *dev,uint16_t prescaler, uint16_t highAlert, uint16_t lowAlert) +int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert, + uint16_t lowAlert) { - //Increases integration time, at which QCOUNT is updated - LTC4015_write(dev, QCOUNT_PRESCALE_FACTOR, prescaler); + //Increases integration time, at which QCOUNT is updated + LTC4015_write(dev, QCOUNT_PRESCALE_FACTOR, prescaler); - //Sets the High amount for the QCOUNT before error is flagged - LTC4015_write(dev, QCOUNT_HI_ALERT_LIMIT, highAlert); - LTC4015_write(dev, EN_LIMIT_ALERTS, 0x1000); //Enable bit is in 12th bit postion - - //Sets the low amount for the QCOUNT before error is flagged - LTC4015_write(dev, QCOUNT_LO_ALERT_LIMIT, lowAlert); - LTC4015_write(dev, EN_LIMIT_ALERTS, 0x2000); //Enable bit is in 13th bit postion + //Sets the High amount for the QCOUNT before error is flagged + LTC4015_write(dev, QCOUNT_HI_ALERT_LIMIT, highAlert); + LTC4015_write(dev, EN_LIMIT_ALERTS, + 0x1000); //Enable bit is in 12th bit postion + //Sets the low amount for the QCOUNT before error is flagged + LTC4015_write(dev, QCOUNT_LO_ALERT_LIMIT, lowAlert); + LTC4015_write(dev, EN_LIMIT_ALERTS, + 0x2000); //Enable bit is in 13th bit postion - LTC4015_write(dev, CONFIG_BITS, 0x0008); //enable QCOUNT, in bit postion 2 - LTC4015_read(dev, LIMIT_ALERTS, dev->limitAlerts); - LTC4015_read(dev, QCOUNT, dev->qcount); + LTC4015_write(dev, CONFIG_BITS, + 0x0008); //enable QCOUNT, in bit postion 2 + LTC4015_read(dev, LIMIT_ALERTS, dev->limitAlerts); + LTC4015_read(dev, QCOUNT, dev->qcount); - //This all could be put into a while loop if you want to continually check for errors - LTC4015_write(dev, CONFIG_BITS, (CONFIG_BITS ^ 0x1000)); //should re-enable charging if was disabled - //Sees if the alerts are being flagged, and then will return the QCOUNT - if(LIMIT_ALERTS | 0x1000 == 0x1000){ - LTC4015_write(dev, EN_LIMIT_ALERTS, (EN_LIMIT_ALERTS^0x1000)); //Should just reset the enable but touch nothing else - LTC4015_write(dev, CONFIG_BITS, 0x1000); //suspends charger - return(QCOUNT, highAlert) //Need away to tell its being flagged, but not really sure what to return - }else if (LIMIT_ALERTS | 0x2000 == 0x2000) - { - LTC4015_write(dev, EN_LIMIT_ALERTS, EN_LIMIT_ALERTS^0x2000); - LTC4015_write(dev, CONFIG_BITS, 0x1000); //suspends charger - return(QCOUNT, lowAlert) //sames issue as above - } - + //This all could be put into a while loop if you want to continually check for errors + LTC4015_write(dev, CONFIG_BITS, + (CONFIG_BITS ^ + 0x1000)); //should re-enable charging if was disabled + //Sees if the alerts are being flagged, and then will return the QCOUNT + if (LIMIT_ALERTS | 0x1000 == 0x1000) { + LTC4015_write( + dev, EN_LIMIT_ALERTS, + (EN_LIMIT_ALERTS ^ + 0x1000)); //Should just reset the enable but touch nothing else + LTC4015_write(dev, CONFIG_BITS, 0x1000); //suspends charger + return (QCOUNT, + highAlert); //Need away to tell its being flagged, but not really sure what to return + } else if (LIMIT_ALERTS | 0x2000 == 0x2000) { + LTC4015_write(dev, EN_LIMIT_ALERTS, EN_LIMIT_ALERTS ^ 0x2000); + LTC4015_write(dev, CONFIG_BITS, 0x1000); //suspends charger + return (QCOUNT, lowAlert); //sames issue as above + } } \ No newline at end of file From 9a1a8052b84f0015f5826b4fe062df77f3c70bf5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 13 Oct 2024 23:50:15 -0400 Subject: [PATCH 2/4] Added Function Pointer as Argument --- general/include/LTC4015.h | 10 +++++++--- general/src/LTC4015.c | 9 ++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/general/include/LTC4015.h b/general/include/LTC4015.h index 2601f02..b85a708 100644 --- a/general/include/LTC4015.h +++ b/general/include/LTC4015.h @@ -39,7 +39,10 @@ //Enable Coulomb Counter Low Alert.(0xOD Bit 13) //Enable Coulomb Counter High Alert.(0xOD Bit 12) -typedef int (*Mem_Read)(uint16_t DevAddress, uint16_t MemAddress, +typedef int (*Read_Ptr)(uint16_t DevAddress, uint16_t MemAddress, + uint16_t MemAddSize, uint8_t *pData, uint16_t Size); + +typedef int (*Write_Ptr)(uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size); typedef struct { @@ -47,7 +50,8 @@ typedef struct { uint16_t qcount; uint16_t limitAlerts; - Mem_Read local_mem_read; + Read_Ptr read; + Write_Ptr write; } LTC4015_T; /** @@ -57,7 +61,7 @@ typedef struct { * @param hi2c * @return HAL_StatusTypeDef */ -int LTC4015_Init(LTC4015_T *dev); +int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write); /** * @brief Reads from the LTC4015EUHF#PBF load switch diff --git a/general/src/LTC4015.c b/general/src/LTC4015.c index 790aafa..1108760 100644 --- a/general/src/LTC4015.c +++ b/general/src/LTC4015.c @@ -8,17 +8,20 @@ #include "LTC4015.h" -int LTC4015_Init(LTC4015_T *dev) { +int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write) { + dev->read = read; + dev->write = write; + //Gets the value from the Charging state register LtC4015_read(dev, LTC4015_CHGSTATE, dev->CHGdata); } int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data) { - return dev->local_mem_read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); + return dev->read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); } int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data) { - return dev->local_mem_read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); + return dev->write(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); } int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert, From d3a7a3a6f93347b00cf4ab8e5d9588d6837dec9b Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Thu, 17 Oct 2024 13:30:30 -0400 Subject: [PATCH 3/4] merged --- general/include/LTC4015.h | 15 +++---- general/src/LTC4015.c | 82 +++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/general/include/LTC4015.h b/general/include/LTC4015.h index b85a708..55647f8 100644 --- a/general/include/LTC4015.h +++ b/general/include/LTC4015.h @@ -43,7 +43,7 @@ typedef int (*Read_Ptr)(uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size); typedef int (*Write_Ptr)(uint16_t DevAddress, uint16_t MemAddress, - uint16_t MemAddSize, uint8_t *pData, uint16_t Size); + uint16_t MemAddSize, uint8_t *pData, uint16_t Size); typedef struct { uint16_t chargeFaults; //Stores error faults from CHGSTATE register @@ -59,7 +59,7 @@ typedef struct { * * @param dev * @param hi2c - * @return HAL_StatusTypeDef + * @return int */ int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write); @@ -68,17 +68,18 @@ int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write); * @note always reads from both input registers * * @param dev - * @param i2c_handle + * @param reg + * @param data */ int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data); /** * @brief Writes to the LTC4015EUHF#PBF load switch * - * @param device + * @param dev + * @param reg * @param data - * @param i2c_handle - * @return HAL_StatusTypeDef + * @return int */ int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data); @@ -90,7 +91,7 @@ int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data); * @param highAlert * @param prescaler * @param lowAlert - * @return HAL_StatusTypeDef, QCOUNT, Faulted + * @return int */ int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert, uint16_t lowAlert); diff --git a/general/src/LTC4015.c b/general/src/LTC4015.c index 1108760..76f1e92 100644 --- a/general/src/LTC4015.c +++ b/general/src/LTC4015.c @@ -8,58 +8,64 @@ #include "LTC4015.h" -int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write) { - dev->read = read; - dev->write = write; +int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write) +{ + dev->read = read; + dev->write = write; //Gets the value from the Charging state register - LtC4015_read(dev, LTC4015_CHGSTATE, dev->CHGdata); + LtC4015_read(dev, LTC4015_CHGSTATE, &dev->chargeFaults); } -int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data) { +int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data) +{ return dev->read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); } -int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data) { +int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data) +{ return dev->write(LTC4015_ADDR_68, reg, 0x00000001U, data, 1); } int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert, - uint16_t lowAlert) { + uint16_t lowAlert) +{ // Increases integration time, at which QCOUNT is updated - LTC4015_write(dev, QCOUNT_PRESCALE_FACTOR, prescaler); + LTC4015_write(dev, QCOUNT_PRESCALE_FACTOR, prescaler); - // Sets the High amount for the QCOUNT before error is flagged - LTC4015_write(dev, QCOUNT_HI_ALERT_LIMIT, highAlert); - LTC4015_write(dev, EN_LIMIT_ALERTS, - 0x1000); // Enable bit is in 12th bit postion + // Sets the High amount for the QCOUNT before error is flagged + LTC4015_write(dev, QCOUNT_HI_ALERT_LIMIT, highAlert); + LTC4015_write(dev, EN_LIMIT_ALERTS, + 0x1000); // Enable bit is in 12th bit postion - // Sets the low amount for the QCOUNT before error is flagged - LTC4015_write(dev, QCOUNT_LO_ALERT_LIMIT, lowAlert); - LTC4015_write(dev, EN_LIMIT_ALERTS, - 0x2000); // Enable bit is in 13th bit postion + // Sets the low amount for the QCOUNT before error is flagged + LTC4015_write(dev, QCOUNT_LO_ALERT_LIMIT, lowAlert); + LTC4015_write(dev, EN_LIMIT_ALERTS, + 0x2000); // Enable bit is in 13th bit postion - LTC4015_write(dev, CONFIG_BITS, 0x0008); // enable QCOUNT, in bit postion 2 - LTC4015_read(dev, LIMIT_ALERTS, dev->limitAlerts); - LTC4015_read(dev, QCOUNT, dev->qcount); + LTC4015_write(dev, CONFIG_BITS, + 0x0008); // enable QCOUNT, in bit postion 2 + LTC4015_read(dev, LIMIT_ALERTS, dev->limitAlerts); + LTC4015_read(dev, QCOUNT, dev->qcount); - // This all could be put into a while loop if you want to continually check - // for errors - LTC4015_write( - dev, CONFIG_BITS, - (CONFIG_BITS ^ 0x1000)); // should re-enable charging if was disabled - // Sees if the alerts are being flagged, and then will return the QCOUNT - if (LIMIT_ALERTS | 0x1000 == 0x1000) { - LTC4015_write( - dev, EN_LIMIT_ALERTS, - (EN_LIMIT_ALERTS ^ - 0x1000)); // Should just reset the enable but touch nothing else - LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger - return (QCOUNT, highAlert) // Need away to tell its being flagged, but not - // really sure what to return - } else if (LIMIT_ALERTS | 0x2000 == 0x2000) { - LTC4015_write(dev, EN_LIMIT_ALERTS, EN_LIMIT_ALERTS ^ 0x2000); - LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger - return (QCOUNT, lowAlert) // sames issue as above - } + // This all could be put into a while loop if you want to continually check + // for errors + LTC4015_write(dev, CONFIG_BITS, + (CONFIG_BITS ^ + 0x1000)); // should re-enable charging if was disabled + // Sees if the alerts are being flagged, and then will return the QCOUNT + if (LIMIT_ALERTS | 0x1000 == 0x1000) { + LTC4015_write( + dev, EN_LIMIT_ALERTS, + (EN_LIMIT_ALERTS ^ + 0x1000)); // Should just reset the enable but touch nothing else + LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger + return (QCOUNT, + highAlert) // Need away to tell its being flagged, but not + // really sure what to return + } else if (LIMIT_ALERTS | 0x2000 == 0x2000) { + LTC4015_write(dev, EN_LIMIT_ALERTS, EN_LIMIT_ALERTS ^ 0x2000); + LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger + return (QCOUNT, lowAlert) // sames issue as above + } } \ No newline at end of file From b8953e91f6bfc0bfedd76f0c8bd68256197e44b2 Mon Sep 17 00:00:00 2001 From: dyldonahue Date: Thu, 17 Oct 2024 13:37:37 -0400 Subject: [PATCH 4/4] idk man --- general/src/LTC4015.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/general/src/LTC4015.c b/general/src/LTC4015.c index 76f1e92..56b70ae 100644 --- a/general/src/LTC4015.c +++ b/general/src/LTC4015.c @@ -61,11 +61,11 @@ int LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler, uint16_t highAlert, 0x1000)); // Should just reset the enable but touch nothing else LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger return (QCOUNT, - highAlert) // Need away to tell its being flagged, but not + highAlert); // Need away to tell its being flagged, but not // really sure what to return } else if (LIMIT_ALERTS | 0x2000 == 0x2000) { LTC4015_write(dev, EN_LIMIT_ALERTS, EN_LIMIT_ALERTS ^ 0x2000); LTC4015_write(dev, CONFIG_BITS, 0x1000); // suspends charger - return (QCOUNT, lowAlert) // sames issue as above + return (QCOUNT, lowAlert); // sames issue as above } } \ No newline at end of file