BQ28Z610 Driver
BQ28Z610 Battery fuel gauge device driver for Arduino
Macros | Functions
utils.h File Reference

Util functions headers. More...

#include <Arduino.h>
#include <Wire.h>
#include "globals.h"

Go to the source code of this file.

Macros

#define KELVIN_TO_CELSIUS(k)   (k - 273.15)
 
#define PGM_PRINT(s)   Serial.print(stringFromProgmem(PSTR(s)))
 
#define PGM_PRINTLN(s)   Serial.println(stringFromProgmem(PSTR(s)))
 

Functions

bool isAllowedRequestPayloadSize (int len)
 
int sendCommand (byte command)
 
int sendCommand (byte reg, word command)
 
int sendData (byte reg, byte *data, int len)
 
int requestBlock (byte *buf)
 
byte requestByte ()
 
int requestBytes (byte *buf, int len)
 
word requestWord ()
 
byte checksum (byte *data, byte len)
 
bool validate (byte *data)
 
word composeWord (byte *buf, int lowerByteIndex=0, bool littleEndian=true)
 
u32 composeDoubleWord (byte *buf)
 
u32 composeValue (byte *buf, int from, int till)
 
String stringFromProgmem (PGM_P stringPtr)
 
void printBin (u32 val, int n, bool newLine=false)
 
void printLongSplitBin (u32 val)
 
void printLongBin (u32 val, bool newLine=false)
 
void printWordBin (word val, bool newLine=false)
 
void printByteBin (byte val, bool newLine=false)
 
void printLongHex (u32 val, bool newLine=false)
 
void printWordHex (word val, bool newLine=false)
 
void printWordHex (PGM_P caption, word val, bool newLine=true)
 
void printByteHex (byte val, bool newLine=false)
 
void printBytesHex (byte *buf, int len)
 
void printInteger (PGM_P caption, int value, bool newLine=true)
 
void printInteger (PGM_P caption, int value, PGM_P units, bool newLine=true)
 
void printInteger (int value, PGM_P units, bool newLine=false)
 
void printFloat (float value, int format, PGM_P units, bool newLine=false)
 
void printFloat (PGM_P caption, float value, int format, PGM_P units)
 
void printFloat (PGM_P caption, float value, int format, PGM_P(*unitsFn)())
 
void printPremil (PGM_P caption, int value, PGM_P units)
 
void printPremil (PGM_P caption, int value, PGM_P(*unitsFn)())
 
void printFlag (PGM_P caption, u32 flags, int n)
 
void printFlag (PGM_P caption, u32 flags, Flag(*flagFn)())
 
void printFlag (PGM_P caption, u32 flags, Flag flag)
 
void printFlag (u32 flags, Flag flag)
 

Detailed Description

Util functions headers.

MIT License

Copyright (c) 2024 Oleksii Sylichenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Macro Definition Documentation

◆ PGM_PRINT

#define PGM_PRINT (   s)    Serial.print(stringFromProgmem(PSTR(s)))

Put defined string into program memory, in the runtime read the string from the program memory and print it.

◆ PGM_PRINTLN

#define PGM_PRINTLN (   s)    Serial.println(stringFromProgmem(PSTR(s)))

Put defined string into program memory, in the runtime read the string from the program memory and print it.

Function Documentation

◆ checksum()

byte checksum ( byte *  data,
byte  len 
)

The checksum is the bitwise inversion of the sum of bytes.

For example:

  ~(0x35 + 0x00 + 0x23 + 0x01 + 0x67 + 0x45 + 0xAB + 0x89 + 0xEF + 0xCD)
  = ~(0xF5) = ~(0b11110101) = 0b00001010 = 0x0A
See also
https://github.com/tibms/kernel-4.4/blob/release/drivers/power/bq28z610/bq28z610_fg.c#L378

◆ composeDoubleWord()

u32 composeDoubleWord ( byte *  buf)

Returns the Unsigned integer 32-bit value of 4 bytes from the buffer in Little Endian format.

Parameters
buf- data array
a- index of the least significant byte; bits: [0..7]
b- index of the most significant byte
Returns
  • [0x11, 0x22, 0x33, 0x44] -> 0x44332211
  • [0x11, 0x22, 0x00, 0x00] -> 0x00002211

◆ composeValue()

u32 composeValue ( byte *  buf,
int  from,
int  till 
)

Compose value from the bytes of the buffer in Little Endian format.

Parameters
buf- data array
from- index of the least significant byte; bits: [0..7]
till- index of the most significant byte
Returns
  • [0x11, 0x22, 0x33, 0x44] -> 0x44332211
  • [0x11, 0x22, 0x00, 0x00] -> 0x00002211

◆ composeWord()

word composeWord ( byte *  buf,
int  lsbIndex,
bool  littleEndian 
)

Compose the word value from the two bytes of the buffer.

Parameters
buf- data array
lowerByteIndex- Index of the byte that should be placed in the lower position of the result.
littleEndian- Ending indicator:
  • true - Little Endian
  • false - Big Endian
Returns
  • Little endian: [0x11, 0x22] -> 0x2211
  • Big Endian: [0x11, 0x22] -> 0x1122

◆ isAllowedRequestPayloadSize()

bool isAllowedRequestPayloadSize ( int  len)

Check whether length is greater than 0 and lower that 32.

Max Data length in the Block Protocol is 32.

◆ printBin()

void printBin ( u32  val,
int  n,
bool  newLine 
)

Print value in binary format with leading zeros.

◆ printBytesHex()

void printBytesHex ( byte *  buf,
int  len 
)

Print content of the byte array in one line in hex format with leading zeros if necessary. [ AA BB CC DD ... ]

◆ printFlag() [1/4]

void printFlag ( PGM_P  caption,
u32  flags,
Flag  flag 
)

Print the Flag value with the caption.

◆ printFlag() [2/4]

void printFlag ( PGM_P  caption,
u32  flags,
Flag(*)()  flagFn 
)

Print the Flag value with the caption. The Flag supplier function is passed as a parameter.

◆ printFlag() [3/4]

void printFlag ( PGM_P  caption,
u32  flags,
int  n 
)

Print the Flag number with the caption.

◆ printFlag() [4/4]

void printFlag ( u32  flags,
Flag  flag 
)

Print the Flag value, caption is taken from the Flag definition.

◆ printInteger() [1/3]

void printInteger ( int  value,
PGM_P  units,
bool  newLine = false 
)

Print in format: "value units"

◆ printInteger() [2/3]

void printInteger ( PGM_P  caption,
int  value,
bool  newLine = true 
)

Print in format: "Caption: value"

◆ printInteger() [3/3]

void printInteger ( PGM_P  caption,
int  value,
PGM_P  units,
bool  newLine = true 
)

Print in format: "Caption: value units"

◆ printLongHex()

void printLongHex ( u32  val,
bool  newLine 
)

Print value in the HEX format with leading 0x and zero if necessary.

◆ printLongSplitBin()

void printLongSplitBin ( u32  val)

Print long value bits (32) in two lines: 31 - 16 15 - 0

◆ printPremil()

void printPremil ( PGM_P  caption,
int  value,
PGM_P  units 
)

Print the integer value as a float divided by 1000 with 3 decimal places in format: "Caption: 65.536"

◆ requestBlock()

int requestBlock ( byte *  buf)

Request the device for 36 bytes per multiple requests using the Block Protocol.

  • The first 2 bytes represent the requested address.
  • 32 bytes contain data.
  • 1 byte is allocated for the checksum.
  • 1 byte denotes the total length.

◆ requestBytes()

int requestBytes ( byte *  buf,
int  len 
)

Request the device for len bytes per single request.

Length of the requested data must not be greater than 32, because it cannot response more.

◆ requestWord()

word requestWord ( )

Read word from the Device in Little Endian and return as normal word.

If response: [0xaa, 0xAA] then return: 0xAAaa

◆ sendCommand()

int sendCommand ( byte  reg,
word  command 
)

Send word command in Little Endian to the register.

Bytes of command should be in normal ordering. Ordering will be changed to Little Endian by the function.

0x4321 to REG:

write: [ REG, 0x21, 0x43 ]

Sending word command in Little Endian to the register.

Bytes of command should be in normal ordering. Ordering will be changed to Little Endian by the function.

0x4321 to REG:

write: [ REG, 0x21, 0x43 ]

◆ sendData()

int sendData ( byte  reg,
byte *  data,
int  len 
)

Send byte array data with specified length into specified register.

Order of the bytes in the data array should be prepared to sending.

The length should not be greater than 32 and less than 1.

◆ stringFromProgmem()

String stringFromProgmem ( PGM_P  stringPtr)

Read string from PROGMEM and return as String in RAM.

◆ validate()

bool validate ( byte *  data)

Validate the data via checksum: the sum of the data bytes and the checksum should equal a full byte.

Length of the data array according to the Block Protocol should be equal 36.

The last two bytes should represent the checksum and the total length.