Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished Refactoring LTC4015 Ticket #167

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions general/include/LTC4015.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,47 @@
#include <stdint.h>
#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 (*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);

}LTC4015_T;
typedef struct {
uint16_t chargeFaults; //Stores error faults from CHGSTATE register
uint16_t qcount;
uint16_t limitAlerts;

Read_Ptr read;
Write_Ptr write;
} LTC4015_T;
dnakhooda marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Initializes the LTC4015EUHF#PBF
Expand All @@ -59,8 +61,7 @@ typedef struct
* @param hi2c
* @return HAL_StatusTypeDef
*/
HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle);

int LTC4015_Init(LTC4015_T *dev, Read_Ptr read, Write_Ptr write);

/**
* @brief Reads from the LTC4015EUHF#PBF load switch
Expand All @@ -69,8 +70,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
Expand All @@ -80,8 +80,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
Expand All @@ -93,12 +92,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
27 changes: 13 additions & 14 deletions general/src/LTC4015.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@

#include "LTC4015.h"

HAL_StatusTypeDef LTC4015_Init(LTC4015_T *dev, I2C_HandleTypeDef *i2cHandle) {
dev->i2cHandle = i2cHandle;
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);
//Gets the value from the Charging state register
LtC4015_read(dev, LTC4015_CHGSTATE, dev->CHGdata);
dnakhooda marked this conversation as resolved.
Show resolved Hide resolved
}

HAL_StatusTypeDef 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)

int LTC4015_read(LTC4015_T *dev, uint16_t reg, uint16_t *data) {
return dev->read(LTC4015_ADDR_68, reg, 0x00000001U, data, 1);
}

HAL_StatusTypeDef 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)}
int LTC4015_write(LTC4015_T *dev, uint16_t reg, uint16_t data) {
return dev->write(LTC4015_ADDR_68, reg, 0x00000001U, data, 1);
}

HAL_StatusTypeDef LTC4015_Qcounter(LTC4015_T *dev, uint16_t prescaler,
uint16_t highAlert, uint16_t lowAlert) {
// Increases integration time, at which QCOUNT is updated
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);

// Sets the High amount for the QCOUNT before error is flagged
Expand Down
Loading