amiro-lld / source / alld_bq27500.c @ ef078306
History | View | Annotate | Download (8.63 KB)
1 |
/*
|
---|---|
2 |
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU Lesser General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU Lesser General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU Lesser General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
/**
|
20 |
* @file alld_bq27500.c
|
21 |
* @brief Fuel Gauge function implementations.
|
22 |
*
|
23 |
* @addtogroup lld_gauge
|
24 |
* @{
|
25 |
*/
|
26 |
|
27 |
#include <alld_bq27500.h> |
28 |
|
29 |
#if defined(AMIROLLD_CFG_USE_BQ27500) || defined(__DOXYGEN__)
|
30 |
|
31 |
#include <string.h> |
32 |
|
33 |
/******************************************************************************/
|
34 |
/* LOCAL DEFINITIONS */
|
35 |
/******************************************************************************/
|
36 |
|
37 |
/******************************************************************************/
|
38 |
/* EXPORTED VARIABLES */
|
39 |
/******************************************************************************/
|
40 |
|
41 |
/******************************************************************************/
|
42 |
/* LOCAL TYPES */
|
43 |
/******************************************************************************/
|
44 |
|
45 |
/******************************************************************************/
|
46 |
/* LOCAL VARIABLES */
|
47 |
/******************************************************************************/
|
48 |
|
49 |
/******************************************************************************/
|
50 |
/* LOCAL FUNCTIONS */
|
51 |
/******************************************************************************/
|
52 |
|
53 |
/******************************************************************************/
|
54 |
/* EXPORTED FUNCTIONS */
|
55 |
/******************************************************************************/
|
56 |
|
57 |
/**
|
58 |
* @brief Read the battery low Gpio pin.
|
59 |
* @param[in] bqd The bq27500 driver
|
60 |
* @param[out] batlow The battery low state
|
61 |
*
|
62 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
63 |
*/
|
64 |
inline apalExitStatus_t
|
65 |
bq27500_lld_read_batlow(const BQ27500Driver* const bq27500, bq27500_lld_batlow_t* const batlow) |
66 |
{ |
67 |
apalDbgAssert(bq27500 != NULL);
|
68 |
apalDbgAssert(batlow != NULL);
|
69 |
|
70 |
apalControlGpioState_t gpio_state; |
71 |
apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batlow, &gpio_state); |
72 |
*batlow = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_LOW : BQ27500_LLD_BATTERY_NOT_LOW; |
73 |
return status;
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* @brief Read the battery good Gpio pin.
|
78 |
* @param[in] bqd The bq27500 driver
|
79 |
* @param[out] batgood The battery good state
|
80 |
*
|
81 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
82 |
*/
|
83 |
inline apalExitStatus_t
|
84 |
bq27500_lld_read_batgood(const BQ27500Driver* const bq27500, bq27500_lld_batgood_t* const batgood) |
85 |
{ |
86 |
apalDbgAssert(bq27500 != NULL);
|
87 |
apalDbgAssert(batgood != NULL);
|
88 |
|
89 |
apalControlGpioState_t gpio_state; |
90 |
apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batgood, &gpio_state); |
91 |
*batgood = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_GOOD : BQ27500_LLD_BATTERY_NOT_GOOD; |
92 |
return status;
|
93 |
} |
94 |
|
95 |
/**
|
96 |
* @brief Execute one of the standard commands.
|
97 |
* @param[in] i2cd The I2C Driver
|
98 |
* @param[in] cmd The command to be executed
|
99 |
* @param[out] dst The return value of the command
|
100 |
* @param[in] timeout Timeout of the I2C bus
|
101 |
*
|
102 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
103 |
*/
|
104 |
inline apalExitStatus_t
|
105 |
bq27500_lld_std_command(const BQ27500Driver* const bq27500, const bq27500_lld_std_command_t cmd, uint16_t* const dst, const apalTime_t timeout) |
106 |
{ |
107 |
apalDbgAssert(bq27500 != NULL);
|
108 |
apalDbgAssert(dst != NULL);
|
109 |
|
110 |
uint8_t buffer[2];
|
111 |
apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, (uint8_t*)&cmd, 1, buffer, 2, timeout); |
112 |
*dst = (uint16_t)((buffer[1] << 8) | buffer[0]); |
113 |
return status;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* @brief Execute one of the control sub commands.
|
118 |
* @param[in] i2cd The I2C Driver
|
119 |
* @param[in] cmd The sub command to be executed
|
120 |
* @param[in] timeout Timeout of the I2C bus
|
121 |
*
|
122 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
123 |
*/
|
124 |
inline apalExitStatus_t
|
125 |
bq27500_lld_sub_command_call(const BQ27500Driver* const bq27500, const bq27500_lld_control_subcmd_t cmd, const apalTime_t timeout) |
126 |
{ |
127 |
apalDbgAssert(bq27500 != NULL);
|
128 |
|
129 |
uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(cmd & 0x00FFu), (uint8_t)((cmd & 0xFF00u) >> 8)}; |
130 |
return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* @brief Read the return value of a previously executed control sub command.
|
135 |
* @param[in] i2cd The I2C Driver
|
136 |
* @param[out] data The data read from the sub command
|
137 |
* @param[in] timeout Timeout of the I2C bus
|
138 |
*
|
139 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
140 |
*/
|
141 |
inline apalExitStatus_t
|
142 |
bq27500_lld_sub_command_read(const BQ27500Driver* const bq27500, uint16_t* const data, const apalTime_t timeout) |
143 |
{ |
144 |
apalDbgAssert(bq27500 != NULL);
|
145 |
apalDbgAssert(data != NULL);
|
146 |
|
147 |
uint8_t buffer[3] = {0, 0, 0}; |
148 |
apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 1, &buffer[1], 2, timeout); |
149 |
*data = (uint16_t)((buffer[2] << 8) | buffer[1]); |
150 |
return status;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* @brief Execute on of the extended commands.
|
155 |
* @param[in] i2cd The I2C Driver
|
156 |
* @param[in] cmd The ext command to be executed
|
157 |
* @param[in] rw Use the command in read or write mode
|
158 |
* @param[in] buffer A buffer for input and output
|
159 |
* @param[in] length length of the buffer
|
160 |
* @param[in] offset Offset for reading or writing
|
161 |
* @param[in] timeout Timeout of the I2C bus
|
162 |
*
|
163 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
164 |
*/
|
165 |
inline apalExitStatus_t
|
166 |
bq27500_lld_ext_command(const BQ27500Driver* const bq27500, const bq27500_lld_ext_command_t cmd, const bq27500_lld_ext_cmd_access_t rw, uint8_t* const buffer, const uint8_t length, const uint8_t offset, const apalTime_t timeout) |
167 |
{ |
168 |
apalDbgAssert(bq27500 != NULL);
|
169 |
apalDbgAssert(buffer != NULL);
|
170 |
|
171 |
uint8_t in_buffer[1+length];
|
172 |
in_buffer[0] = cmd + offset;
|
173 |
if (rw == BQ27500_LLD_EXT_CMD_WRITE) {
|
174 |
memcpy(in_buffer+1, buffer, length);
|
175 |
} |
176 |
uint8_t txbytes = 1 + ((rw == BQ27500_LLD_EXT_CMD_WRITE) ? length : 0); |
177 |
uint8_t rxbytes = ((rw == BQ27500_LLD_EXT_CMD_READ) ? length : 0);
|
178 |
return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, in_buffer, txbytes, ((rw == BQ27500_LLD_EXT_CMD_READ) ? buffer : NULL), rxbytes, timeout); |
179 |
} |
180 |
|
181 |
/**
|
182 |
* @brief Send data via the CTNL command.
|
183 |
* @param[in] i2cd The I2C Driver
|
184 |
* @param[in] data Data to be sent
|
185 |
* @param[in] timeout Timeout of the I2C bus
|
186 |
*
|
187 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
188 |
*/
|
189 |
inline apalExitStatus_t
|
190 |
bq27500_lld_send_ctnl_data(const BQ27500Driver* const bq27500, const uint16_t data, const apalTime_t timeout) |
191 |
{ |
192 |
apalDbgAssert(bq27500 != NULL);
|
193 |
|
194 |
uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(data & 0x00FFu), (uint8_t)((data & 0xFF00u) >> 8)}; |
195 |
return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout); |
196 |
} |
197 |
|
198 |
/**
|
199 |
* @bried Computes the checksum of blockdata.
|
200 |
* @param[in] blockdata Data to compute the checksum of (32 byte buffer)
|
201 |
* @param[out] sum The computed checksum
|
202 |
*
|
203 |
* @return The return status indicates whether the function call was succesfull or a timeout occurred.
|
204 |
*/
|
205 |
inline apalExitStatus_t
|
206 |
bq27500_lld_compute_blockdata_checksum(const uint8_t* const blockdata, uint8_t* const sum) |
207 |
{ |
208 |
apalDbgAssert(blockdata != NULL);
|
209 |
apalDbgAssert(sum != NULL);
|
210 |
|
211 |
*sum = 0;
|
212 |
for (uint8_t dataIdx = 0; dataIdx < 32; dataIdx++) { |
213 |
*sum += blockdata[dataIdx]; |
214 |
} |
215 |
*sum = 0xFF - *sum;
|
216 |
return APAL_STATUS_SUCCESS;
|
217 |
} |
218 |
|
219 |
#endif /* defined(AMIROLLD_CFG_USE_BQ27500) */ |
220 |
|
221 |
/** @} */
|