Skip to content

Conversation

@alihsar1kaya
Copy link

No description provided.

Comment on lines 22 to 30
void BQ76942_Init(void)
{
// init SPI
SERCOM0_SPI_Initialize();

// configure CS pin as output and set HIGH (inactive)
PORT_REGS->GROUP[BQ_CS_GROUP].PORT_DIRSET = BQ_CS_MASK;
PORT_REGS->GROUP[BQ_CS_GROUP].PORT_OUTSET = BQ_CS_MASK;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SERCOM0 initialize should already be called by the Sys_Initialize function.

I think this function should be used to initialize the IC to use the correct settings.

Comment on lines 50 to 87
bool WriteReg(uint8_t regAddr, uint8_t value)
{
/* First byte: MSB=1 to indicate WRITE, lower 7 bits = register address */
uint8_t tx[2];
tx[0] = (uint8_t)(0x80 | (regAddr & 0x7F)); /* W flag + address */
tx[1] = value;

if (SERCOM0_SPI_IsBusy())
return false;

BQ_CS_Low();
bool ok = SERCOM0_SPI_Write(tx, 2U);
BQ_CS_High();



return ok;
}


bool ReadReg(uint8_t regAddr, uint8_t *value)
{
uint8_t tx[2] = { (regAddr & 0x7F), 0x00 };
uint8_t rx[2] = { 0, 0 };

if (SERCOM0_SPI_IsBusy())
return false;

BQ_CS_Low();
bool ok = SERCOM0_SPI_WriteRead(tx, 2U, rx, 2U);
BQ_CS_High();

if (ok)
*value = rx[1];


return ok;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all registers 1 byte? I think it would make sense to have the length as an input so you could configure multiple registers in the same function call instead of hard coding the length like this

Also the convention for functions in C are snake case.

Comment on lines 152 to 155
if (!WriteReg(0x3E, lsb))
return false;
if (!WriteReg(0x3F, msb))
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the earlier comments, instead of having multiple functions calls and SPI transmission this could be done in one transmission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants