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