-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/bms #39
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
base: main
Are you sure you want to change the base?
Feat/bms #39
Conversation
| 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; | ||
| } |
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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.
| if (!WriteReg(0x3E, lsb)) | ||
| return false; | ||
| if (!WriteReg(0x3F, msb)) | ||
| return false; |
There was a problem hiding this comment.
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.
No description provided.