Skip to content

Commit

Permalink
firmware: devices: battery_monitor: Added functions to read missing r…
Browse files Browse the repository at this point in the history
…esult registers #157
  • Loading branch information
ramonborba committed Aug 14, 2023
1 parent ca59618 commit 2d82374
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
39 changes: 36 additions & 3 deletions firmware/devices/battery_monitor/battery_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
* \brief Battery Monitor device implementation.
*
* \author Vinicius Pimenta Bernardo <[email protected]>
* \author Ramon de Araujo Borba <[email protected]>
*
* \version 0.1.12
* \version 0.4.0
*
* \date 2021/09/18
*
Expand Down Expand Up @@ -120,15 +121,15 @@ int bm_get_raac_mah(uint16_t *data)
{
uint8_t rd_buf[2] = {0};
if (ds277Xg_read_data(&battery_monitor_config, DS277XG_RAAC_REGISTER_MSB, rd_buf, 2) != 0) {return -1;}
*data = (uint16_t)((rd_buf[1] << 8) + rd_buf[0]);
*data = (uint16_t)(((rd_buf[0] << 8) + rd_buf[1]) * 1.6);
return 0;
}

int bm_get_rsac_mah(uint16_t *data)
{
uint8_t rd_buf[2] = {0};
if (ds277Xg_read_data(&battery_monitor_config, DS277XG_RSAC_REGISTER_MSB, rd_buf, 2) != 0) {return -1;}
*data = (uint16_t)((rd_buf[1] << 8) + rd_buf[0]);
*data = (uint16_t)(((rd_buf[0] << 8) + rd_buf[1]) * 1.6);
return 0;
}

Expand All @@ -143,3 +144,35 @@ int bm_get_rsrc_percent(uint8_t *data)
if (ds277Xg_read_data(&battery_monitor_config, DS277XG_RSRC_REGISTER, data, 1) != 0) {return -1;}
return 0;
}

int bm_get_acc_current_mah(uint16_t *data)
{
uint8_t rd_buf[2] = {0};
if (ds277Xg_read_data(&battery_monitor_config, DS277XG_ACCUMULATED_CURRENT_MSB, rd_buf, 2) != 0) { return -1; }
*data = ds277Xg_accumulated_current_raw_to_mah((uint16_t)((rd_buf[0] << 8) + rd_buf[1]));
return 0;
}

int bm_get_full_capacity_ppm(uint32_t *data)
{
uint8_t rd_buf[2] = {0};
if (ds277Xg_read_data(&battery_monitor_config, DS277XG_FULL_REGISTER_MSB, rd_buf, 2) != 0) { return -1; }
*data = ((uint32_t)(((rd_buf[0] << 8) + rd_buf[1]) >> 1) * 61);
return 0;
}

int bm_get_active_empty_capacity_ppm(uint32_t *data)
{
uint8_t rd_buf[2] = {0};
if (ds277Xg_read_data(&battery_monitor_config, DS277XG_ACTIVE_EMPTY_REGISTER_MSB, rd_buf, 2) != 0) { return -1; }
*data = ((uint32_t)(((rd_buf[0] << 8) + rd_buf[1]) >> 1) * 61);
return 0;
}

int bm_get_standby_empty_capacity_ppm(uint32_t *data)
{
uint8_t rd_buf[2] = {0};
if (ds277Xg_read_data(&battery_monitor_config, DS277XG_STANDBY_EMPTY_REGISTER_MSB, rd_buf, 2) != 0) { return -1; }
*data = ((uint32_t)(((rd_buf[0] << 8) + rd_buf[1]) >> 1) * 61);
return 0;
}
35 changes: 34 additions & 1 deletion firmware/devices/battery_monitor/battery_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
* \brief Battery Monitor device definition.
*
* \author Vinicius Pimenta Bernardo <[email protected]>
* \author Ramon de Araujo Borba <[email protected]>
*
* \version 0.1.12
* \version 0.4.0
*
* \date 2021/09/18
*
Expand Down Expand Up @@ -148,6 +149,38 @@ int bm_get_rarc_percent(uint8_t *data);
*/
int bm_get_rsrc_percent(uint8_t *data);

/**
* \brief Get the battery monitor accumulated current in mAh.
*
* \param[in,out] data Register data.
* \return int The status/error code.
*/
int bm_get_acc_current_mah(uint16_t *data);

/**
* \brief Get the battery monitor full capacity result register in ppm.
*
* \param[in,out] data Register data.
* \return int The status/error code.
*/
int bm_get_full_capacity_ppm(uint32_t *data);

/**
* \brief Get the battery monitor active empty capacity result register in ppm.
*
* \param[in,out] data Register data.
* \return int The status/error code.
*/
int bm_get_active_empty_capacity_ppm(uint32_t *data);

/**
* \brief Get the battery monitor standby empty capacity result register in ppm.
*
* \param[in,out] data Register data.
* \return int The status/error code.
*/
int bm_get_standby_empty_capacity_ppm(uint32_t *data);

#endif /* BATTERY_MONITOR_H_ */

/** \} End of battery_monitor group */

0 comments on commit 2d82374

Please sign in to comment.