Skip to content

Commit 852dabd

Browse files
author
Jim Lindblom
committedMay 9, 2016
Initial commit of library.
1 parent d8633d9 commit 852dabd

8 files changed

+828
-2
lines changed
 

‎LICENSE.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
SparkFun License Information
2+
============================
3+
4+
SparkFun uses two different licenses for our files - one for hardware and one for code.
5+
6+
Hardware
7+
---------
8+
9+
**SparkFun hardware is released under [Creative Commons Share-alike 4.0 International](http://creativecommons.org/licenses/by-sa/4.0/).**
10+
11+
Note: This is a human-readable summary of (and not a substitute for) the [license](http://creativecommons.org/licenses/by-sa/4.0/legalcode).
12+
13+
You are free to:
14+
15+
Share — copy and redistribute the material in any medium or format
16+
Adapt — remix, transform, and build upon the material
17+
for any purpose, even commercially.
18+
The licensor cannot revoke these freedoms as long as you follow the license terms.
19+
Under the following terms:
20+
21+
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
22+
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
23+
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
24+
Notices:
25+
26+
You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
27+
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
28+
29+
30+
Code
31+
--------
32+
33+
**SparkFun code, firmware, and software is released under the [MIT License](http://opensource.org/licenses/MIT).**
34+
35+
The MIT License (MIT)
36+
37+
Copyright (c) 2016 SparkFun Electronics, Inc.
38+
39+
Permission is hereby granted, free of charge, to any person obtaining a copy
40+
of this software and associated documentation files (the "Software"), to deal
41+
in the Software without restriction, including without limitation the rights
42+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
copies of the Software, and to permit persons to whom the Software is
44+
furnished to do so, subject to the following conditions:
45+
46+
The above copyright notice and this permission notice shall be included in all
47+
copies or substantial portions of the Software.
48+
49+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55+
SOFTWARE.
56+
57+

‎README.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
# SparkFun_BQ27441_Arduino_Library
2-
Arduino library for the BQ27441 LiPo Fuel Gauge.
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/******************************************************************************
2+
BQ27441_Basic.ino
3+
Basic BQ27441 Example Sketch
4+
Jim Lindblom @ SparkFun Electronics
5+
May 9, 2016
6+
https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
7+
8+
This example demonstrates how to initialize a BQ27441, set the capacity of
9+
your LiPo battery, and read state-of-charge (SoC), voltage, current, and
10+
remaining capacity.
11+
12+
Hardware Resources:
13+
- Arduino Development Board
14+
- SparkFun Battery Babysitter
15+
16+
Development environment specifics:
17+
Arduino 1.6.7
18+
SparkFun Battery Babysitter v1.0
19+
Arduino Uno (any 'duino should do)
20+
******************************************************************************/
21+
#include <SparkFunBQ27441.h>
22+
23+
const int BATTERY_CAPACITY = 850; // e.g. 850mAh battery
24+
25+
void setupBQ27441(void)
26+
{
27+
if (!lipo.begin())
28+
{
29+
Serial.println("Error: Unable to communicate with BQ27441.");
30+
Serial.println(" Check wiring and try again.");
31+
Serial.println(" (Battery must be plugged into Battery Babysitter!)");
32+
while (1) ;
33+
}
34+
Serial.println("Connected to BQ27441!");
35+
36+
lipo.setCapacity(BATTERY_CAPACITY);
37+
Serial.print("Full charge capacity set to: ");
38+
Serial.println(String(lipo.fullChargeCapacity()) + " mAh");
39+
}
40+
41+
void printBatteryStats()
42+
{
43+
uint16_t soc = lipo.soc();
44+
uint16_t vRaw = lipo.voltage();
45+
float volts = lipo.convertVoltage(vRaw);
46+
int16_t current = lipo.current();
47+
uint16_t capacity = lipo.capacity();
48+
49+
String toPrint = String(soc) + " % |\t";
50+
toPrint += String(volts) + " V |\t";
51+
toPrint += String(current) + " mA |\t";
52+
toPrint += String(capacity) + " mAh";
53+
Serial.println(toPrint);
54+
}
55+
56+
void setup()
57+
{
58+
Serial.begin(115200);
59+
setupBQ27441();
60+
}
61+
62+
void loop()
63+
{
64+
printBatteryStats();
65+
delay(1000);
66+
}

‎keywords.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###############################################################
2+
# Syntax Coloring Map for SparkFun BQ27441 LiPo Gauge Library #
3+
###############################################################
4+
# Class
5+
###############################################################
6+
7+
BQ27441 KEYWORD1
8+
SparkFunBQ27441 KEYWORD1
9+
lipo KEYWORD1
10+
11+
###############################################################
12+
# Methods and Functions
13+
###############################################################
14+
15+
begin KEYWORD2
16+
17+
###############################################################
18+
# Constants
19+
###############################################################
20+
21+
BQ27441_CONTROL_DEVICE_TYPE LITERAL1

‎library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SparkFun BQ72441 LiPo Fuel Gauge Arduino Library
2+
version=0.0.1
3+
author=SparkFun Electronics
4+
maintainer=SparkFun Electronics
5+
sentence=An Arduino library for interfacing with the BQ72441-G1 LiPo Fuel Gauge
6+
paragraph=An Arduino library for interfacing with the BQ72441-G1 LiPo Fuel Gauge
7+
category=sensors
8+
url=https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
9+
architectures=*

‎src/BQ27441_Definitions.h

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/******************************************************************************
2+
BQ27441_Definitions.h
3+
BQ27441 LiPo Fuel Gauge Definitions
4+
Jim Lindblom @ SparkFun Electronics
5+
May 9, 2016
6+
https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
7+
8+
BQ27441 hardware constants, register addresses, and bit positions.
9+
10+
Hardware Resources:
11+
- Arduino Development Board
12+
- SparkFun Battery Babysitter
13+
14+
Development environment specifics:
15+
Arduino 1.6.7
16+
SparkFun Battery Babysitter v1.0
17+
Arduino Uno (any 'duino should do)
18+
******************************************************************************/
19+
#define BQ72441_I2C_ADDRESS 0x55
20+
21+
///////////////////////
22+
// General Constants //
23+
///////////////////////
24+
#define BQ27441_UNSEAL_KEY 0x8000
25+
#define BQ27441_DEVICE_ID 0x0421
26+
27+
///////////////////////
28+
// Standard Commands //
29+
///////////////////////
30+
#define BQ27441_COMMAND_CONTROL 0x00
31+
#define BQ27441_COMMAND_TEMP 0x02
32+
#define BQ27441_COMMAND_VOLTAGE 0x04
33+
#define BQ27441_COMMAND_FLAGS 0x06
34+
#define BQ27441_COMMAND_NOM_CAPACITY 0x08
35+
#define BQ27441_COMMAND_AVAIL_CAPACITY 0x0A
36+
#define BQ27441_COMMAND_REM_CAPACITY 0x0C
37+
#define BQ27441_COMMAND_FULL_CAPACITY 0x0E
38+
#define BQ27441_COMMAND_AVG_CURRENT 0x10
39+
#define BQ27441_COMMAND_STDBY_CURRENT 0x12
40+
#define BQ27441_COMMAND_MAX_CURRENT 0x14
41+
#define BQ27441_COMMAND_AVG_POWER 0x18
42+
#define BQ27441_COMMAND_SOC 0x1C
43+
#define BQ27441_COMMAND_INT_TEMP 0x1E
44+
#define BQ27441_COMMAND_SOH 0x20
45+
#define BQ27441_COMMAND_REM_CAP_UNFL 0x28
46+
#define BQ27441_COMMAND_REM_CAP_FIL 0x2A
47+
#define BQ27441_COMMAND_FULL_CAP_UNFL 0x2C
48+
#define BQ27441_COMMAND_FULL_CAP_FIL 0x2E
49+
#define BQ27441_COMMAND_SOC_UNFL 0x30
50+
51+
//////////////////////////
52+
// Control Sub-commands //
53+
//////////////////////////
54+
#define BQ27441_CONTROL_STATUS 0x00
55+
#define BQ27441_CONTROL_DEVICE_TYPE 0x01
56+
#define BQ27441_CONTROL_FW_VERSION 0x02
57+
#define BQ27441_CONTROL_DM_CODE 0x04
58+
#define BQ27441_CONTROL_PREV_MACWRITE 0x07
59+
#define BQ27441_CONTROL_CHEM_ID 0x08
60+
#define BQ27441_CONTROL_BAT_INSERT 0x0C
61+
#define BQ27441_CONTROL_BAT_REMOVE 0x0D
62+
#define BQ27441_CONTROL_SET_HIBERNATE 0x11
63+
#define BQ27441_CONTROL_CLEAR_HIBERNATE 0x12
64+
#define BQ27441_CONTROL_SET_CFGUPDATE 0x13
65+
#define BQ27441_CONTROL_SHUTDOWN_ENABLE 0x1B
66+
#define BQ27441_CONTROL_SHUTDOWN 0x1C
67+
#define BQ27441_CONTROL_SEALED 0x20
68+
#define BQ27441_CONTROL_PULSE_SOC_INT 0x23
69+
#define BQ27441_CONTROL_RESET 0x41
70+
#define BQ27441_CONTROL_SOFT_RESET 0x42
71+
#define BQ27441_CONTROL_EXIT_CFGUPDATE 0x43
72+
#define BQ27441_CONTROL_EXIT_RESIM 0x44
73+
74+
///////////////////////////////////////////
75+
// Control Status Word - Bit Definitions //
76+
///////////////////////////////////////////
77+
#define BQ27441_STATUS_SHUTDOWNEN (1<<15)
78+
#define BQ27441_STATUS_WDRESET (1<<14)
79+
#define BQ27441_STATUS_SS (1<<13)
80+
#define BQ27441_STATUS_CALMODE (1<<12)
81+
#define BQ27441_STATUS_CCA (1<<11)
82+
#define BQ27441_STATUS_BCA (1<<10)
83+
#define BQ27441_STATUS_QMAX_UP (1<<9)
84+
#define BQ27441_STATUS_RES_UP (1<<8)
85+
#define BQ27441_STATUS_INITCOMP (1<<7)
86+
#define BQ27441_STATUS_HIBERNATE (1<<6)
87+
#define BQ27441_STATUS_SLEEP (1<<4)
88+
#define BQ27441_STATUS_LDMD (1<<3)
89+
#define BQ27441_STATUS_RUP_DIS (1<<2)
90+
#define BQ27441_STATUS_VOK (1<<1)
91+
92+
////////////////////////////////////
93+
// Flag Command - Bit Definitions //
94+
////////////////////////////////////
95+
#define BQ27441_FLAG_OT (1<<15)
96+
#define BQ27441_FLAG_UT (1<<14)
97+
#define BQ27441_FLAG_FC (1<<9)
98+
#define BQ27441_FLAG_CHG (1<<8)
99+
#define BQ27441_FLAG_OCVTAKEN (1<<7)
100+
#define BQ27441_FLAG_ITPOR (1<<5)
101+
#define BQ27441_FLAG_CFGUPMODE (1<<4)
102+
#define BQ27441_FLAG_BAT_DET (1<<3)
103+
#define BQ27441_FLAG_SOC1 (1<<2)
104+
#define BQ27441_FLAG_SOCF (1<<1)
105+
#define BQ27441_FLAG_DSG (1<<0)
106+
107+
////////////////////////////
108+
// Extended Data Commands //
109+
////////////////////////////
110+
#define BQ27441_EXTENDED_OPCONFIG 0x3A
111+
#define BQ27441_EXTENDED_CAPACITY 0x3C
112+
#define BQ27441_EXTENDED_DATACLASS 0x3E
113+
#define BQ27441_EXTENDED_DATABLOCK 0x3F
114+
#define BQ27441_EXTENDED_BLOCKDATA 0x40
115+
#define BQ27441_EXTENDED_CHECKSUM 0x60
116+
#define BQ27441_EXTENDED_CONTROL 0x61
117+
118+
////////////////////////////////////////
119+
// Configuration Class, Subclass ID's //
120+
////////////////////////////////////////
121+
#define BQ27441_ID_SAFETY 2
122+
#define BQ27441_ID_CHG_TERMINATION 36
123+
#define BQ27441_ID_CONFIG_DATA 48
124+
#define BQ27441_ID_DISCHARGE 49
125+
#define BQ27441_ID_REGISTERS 64
126+
#define BQ27441_ID_POWER 68
127+
#define BQ27441_ID_IT_CFG 80
128+
#define BQ27441_ID_CURRENT_THRESH 81
129+
#define BQ27441_ID_STATE 82
130+
#define BQ27441_ID_R_A_RAM 89
131+
#define BQ27441_ID_CALIB_DATA 104
132+
#define BQ27441_ID_CC_CAL 105
133+
#define BQ27441_ID_CURRENT 107
134+
#define BQ27441_ID_CODES 112

‎src/SparkFunBQ27441.cpp

+436
Large diffs are not rendered by default.

‎src/SparkFunBQ27441.h

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/******************************************************************************
2+
SparkFunBQ27441.h
3+
BQ27441 Arduino Library Main Header File
4+
Jim Lindblom @ SparkFun Electronics
5+
May 9, 2016
6+
https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
7+
8+
Definition of the BQ27441 library, which implements all features of the
9+
BQ27441 LiPo Fuel Gauge.
10+
11+
Hardware Resources:
12+
- Arduino Development Board
13+
- SparkFun Battery Babysitter
14+
15+
Development environment specifics:
16+
Arduino 1.6.7
17+
SparkFun Battery Babysitter v1.0
18+
Arduino Uno (any 'duino should do)
19+
******************************************************************************/
20+
21+
#ifndef SparkFunBQ27441_h
22+
#define SparkFunBQ27441_h
23+
24+
#include "Arduino.h"
25+
#include "BQ27441_Definitions.h"
26+
27+
#define BQ72441_I2C_TIMEOUT 2000
28+
29+
class BQ27441 {
30+
public:
31+
BQ27441();
32+
bool begin(void);
33+
bool setCapacity(uint16_t capacity);
34+
35+
uint16_t temperature(void);
36+
uint16_t voltage(void);
37+
int16_t current(void);
38+
uint16_t capacity(void);
39+
40+
uint16_t nominalAvailableCapacity(void);
41+
uint16_t fullAvailableCapacity(void);
42+
uint16_t remainingCapacity(void);
43+
uint16_t fullChargeCapacity(void);
44+
45+
int16_t averageCurrent(void);
46+
int16_t standbyCurrent(void);
47+
int16_t maxLoadCurrent(void);
48+
int16_t averagePower(void);
49+
50+
uint16_t soc(void);
51+
uint16_t soh(void);
52+
53+
uint16_t internalTemperature(void);
54+
55+
uint16_t remainingCapacityUnfilitered(void);
56+
uint16_t remainingCapacityFiltered(void);
57+
uint16_t fullChargeCapacityUnfiltered(void);
58+
uint16_t fullChargeCapacityFiltered(void);
59+
uint16_t socUnfiltered(void);
60+
61+
uint16_t flags(void);
62+
63+
//////////////////////////
64+
// Control Sub-commands //
65+
//////////////////////////
66+
uint16_t status(void);
67+
uint16_t deviceType(void);
68+
69+
bool sealed(void);
70+
bool seal(void);
71+
bool unseal(void);
72+
73+
bool enterConfig(void);
74+
bool exitConfig(bool resim = true);
75+
bool softReset(void);
76+
77+
uint16_t readOpConfig(void);
78+
uint16_t readDesignCapacity(void);
79+
80+
bool blockDataControl(void);
81+
bool blockDataClass(uint8_t id);
82+
bool blockDataOffset(uint8_t offset);
83+
uint8_t blockDataChecksum(void);
84+
uint8_t readBlockData(uint8_t offset);
85+
bool writeBlockData(uint8_t offset, uint8_t data);
86+
uint8_t computeBlockChecksum(void);
87+
bool writeBlockChecksum(uint8_t csum);
88+
bool writeExtendedData(uint8_t classID, uint8_t offset, uint8_t * data, uint8_t len);
89+
90+
float convertVoltage(uint16_t rawVoltage);
91+
92+
//private:
93+
uint8_t _deviceAddress;
94+
95+
uint16_t readWord(uint16_t subAddress);
96+
uint16_t readControlWord(uint16_t function);
97+
bool executeControlWord(uint16_t function);
98+
99+
int16_t i2cReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
100+
uint16_t i2cWriteBytes(uint8_t subAddress, uint8_t * src, uint8_t count);
101+
};
102+
103+
extern BQ27441 lipo;
104+
105+
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.