@@ -67,7 +67,7 @@ void AP_BattMonitor_TIBQ76952::init(void)
6767 }
6868
6969 // Register periodic callback at 10hz for reading voltage
70- dev->register_periodic_callback (1000000 , FUNCTOR_BIND_MEMBER (&AP_BattMonitor_TIBQ76952::timer, void ));
70+ dev->register_periodic_callback (100000 , FUNCTOR_BIND_MEMBER (&AP_BattMonitor_TIBQ76952::timer, void ));
7171}
7272
7373// / read the battery_voltage, should be called at 10hz
@@ -88,14 +88,14 @@ void AP_BattMonitor_TIBQ76952::read(void)
8888 const uint32_t tnow = AP_HAL::micros();
8989 _state.last_time_micros = tnow;
9090 */
91- _state.healthy = true ;
92- _state.voltage = accumulate.voltage ;
93- _state.current_amps = accumulate.current ;
94- _state.temperature = accumulate.temp ;
95- _state.last_time_micros = AP_HAL::micros ();
96-
97- // debug toggle 1st LED on each read (10hz)
98- hal.gpio ->toggle (HAL_GPIO_PIN_BMS_LED1);
91+ _state.healthy = true ;
92+ _state.voltage = accumulate.voltage ;
93+ _state.current_amps = accumulate.current ;
94+ _state.temperature = accumulate.temp ;
95+ _state.last_time_micros = AP_HAL::micros ();
96+
97+ // debug toggle 1st LED on each read (10hz)
98+ hal.gpio ->toggle (HAL_GPIO_PIN_BMS_LED1);
9999}
100100
101101/*
@@ -106,13 +106,23 @@ bool AP_BattMonitor_TIBQ76952::read_word(uint8_t reg, bool use_crc, uint16_t& da
106106{
107107 // BQ76952 uses 7-bit direct commands for voltage readings
108108 // Data is stored in little endian format as per datasheet
109- uint8_t buf[3 ];
110- if (!dev->read_registers (reg, (uint8_t *)&buf, use_crc ? 3 : 2 )) {
109+ // uint8_t buf[3];
110+ /* if (!dev->read_registers(reg, (uint8_t *)&buf, use_crc ? 3 : 2)) {
111+ return false;
112+ }*/
113+ // const uint8_t send_buf[] = {0x14, 0xFF, 0xF0};
114+ const uint8_t send_buf[] = {reg, 0xFF , 0xF0 };
115+ const uint8_t bytes_to_send = use_crc ? 3 : 2 ;
116+ uint8_t recv_buf[3 ];
117+ if (!dev->transfer (send_buf, bytes_to_send, recv_buf, bytes_to_send)) {
118+ return false ;
119+ }
120+ if (!dev->transfer (send_buf, bytes_to_send, recv_buf, bytes_to_send)) {
111121 return false ;
112122 }
113123
114124 // BQ76952 uses little endian byte order
115- data = le16toh (UINT16_VALUE (buf [1 ], buf [0 ]));
125+ data = le16toh (UINT16_VALUE (recv_buf [1 ], recv_buf [0 ]));
116126 return true ;
117127}
118128
@@ -136,29 +146,28 @@ void AP_BattMonitor_TIBQ76952::timer(void)
136146
137147 // Read stack voltage from BQ76952 using direct commands 0x34/0x35
138148 // According to datasheet Table 4-1: Stack (VC16 pin) voltage in µV units
139- /* int16_t voltage_lsb;
140- if (!read_word(REG_STACK_VOLTAGE_L, voltage_lsb)) {
149+ uint16_t voltage_lsb;
150+ if (!read_word (REG_STACK_VOLTAGE_L, false , voltage_lsb)) {
141151 voltage_lsb = 99 ;
142- }*/
143- // int16_t voltage_msb;
144- /* if (!read_word(REG_STACK_VOLTAGE_L, voltage_lsb) ||
145- !read_word(REG_STACK_VOLTAGE_H, voltage_msb)) {
146- return;
147- }*/
152+ }
153+ uint16_t voltage_msb;
154+ if (!read_word (REG_STACK_VOLTAGE_H, false , voltage_msb)) {
155+ voltage_msb = 99 ;
156+ }
148157 /* if (!read_word(REG_PACK_VOLTAGE_L, voltage_lsb) ||
149158 !read_word(REG_PACK_VOLTAGE_H, voltage_msb)) {
150159 return;
151160 }*/
152- uint16_t otp_check;
153- if (!read_word (REG_OTP_CHECK, true , otp_check)) {
161+ /* uint16_t otp_check;
162+ if (!read_word(REG_OTP_CHECK, false , otp_check)) {
154163 otp_check = 99;
155- }
164+ }*/
156165
157166 // calculate voltage
158167 // float voltage_v = UINT16_VALUE(LOWBYTE(voltage_msb),LOWBYTE(voltage_lsb)) * 0.001;
159168
160169 WITH_SEMAPHORE (accumulate.sem );
161- accumulate.voltage = otp_check ;
170+ accumulate.voltage = float ( UINT32_VALUE ( HIGHBYTE (voltage_msb), LOWBYTE (voltage_msb), HIGHBYTE (voltage_lsb), LOWBYTE (voltage_lsb))) * 0.000001 ;
162171 accumulate.temp = 4 ;
163172 accumulate.current = 3 ;
164173 accumulate.count = 1 ;
0 commit comments