Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_bq27500.c @ 3fb3c6e7

History | View | Annotate | Download (7.202 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
Copyright (C) 2016..2018  Thomas Schöpping et al.
4

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
/**
34
 * @brief Read the battery low Gpio pin.
35
 * @param[in]   bqd       The bq27500 driver
36
 * @param[out]  batlow    The battery low state
37
 *
38
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
39
 */
40
inline apalExitStatus_t
41
bq27500_lld_read_batlow(const BQ27500Driver* const bq27500, bq27500_lld_batlow_t* const batlow)
42
{
43
  apalDbgAssert(bq27500 != NULL);
44
  apalDbgAssert(batlow != NULL);
45
46
  apalControlGpioState_t gpio_state;
47 cf1f756b Thomas Schöpping
  apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batlow, &gpio_state);
48 d6728c5b Thomas Schöpping
  *batlow = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_LOW : BQ27500_LLD_BATTERY_NOT_LOW;
49
  return status;
50
}
51
52
/**
53
 * @brief Read the battery good Gpio pin.
54
 * @param[in]   bqd       The bq27500 driver
55
 * @param[out]  batgood   The battery good state
56
 *
57
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
58
 */
59
inline apalExitStatus_t
60
bq27500_lld_read_batgood(const BQ27500Driver* const bq27500, bq27500_lld_batgood_t* const batgood)
61
{
62
  apalDbgAssert(bq27500 != NULL);
63
  apalDbgAssert(batgood != NULL);
64
65
  apalControlGpioState_t gpio_state;
66 cf1f756b Thomas Schöpping
  apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batgood, &gpio_state);
67 d6728c5b Thomas Schöpping
  *batgood = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_GOOD : BQ27500_LLD_BATTERY_NOT_GOOD;
68
  return status;
69
}
70
71
/**
72
 * @brief Execute one of the standard commands.
73
 * @param[in]   i2cd      The I2C Driver
74
 * @param[in]   cmd       The command to be executed
75
 * @param[out]  dst       The return value of the command
76
 * @param[in]   timeout   Timeout of the I2C bus
77
 *
78
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
79
 */
80
inline apalExitStatus_t
81
bq27500_lld_std_command(const BQ27500Driver* const bq27500, const bq27500_lld_std_command_t cmd, uint16_t* const dst, const apalTime_t timeout)
82
{
83
  apalDbgAssert(bq27500 != NULL);
84
  apalDbgAssert(dst != NULL);
85
86
  uint8_t buffer[2];
87
  apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, (uint8_t*)&cmd, 1, buffer, 2, timeout);
88
  *dst = (uint16_t)((buffer[1] << 8) | buffer[0]);
89
  return status;
90
}
91
92
/**
93
 * @brief Execute one of the control sub commands.
94
 * @param[in]   i2cd      The I2C Driver
95
 * @param[in]   cmd       The sub command to be executed
96
 * @param[in]   timeout   Timeout of the I2C bus
97
 *
98
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
99
 */
100
inline apalExitStatus_t
101
bq27500_lld_sub_command_call(const BQ27500Driver* const bq27500, const bq27500_lld_control_subcmd_t cmd, const apalTime_t timeout)
102
{
103
  apalDbgAssert(bq27500 != NULL);
104
105
  uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(cmd & 0x00FFu), (uint8_t)((cmd & 0xFF00u) >> 8)};
106
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout);
107
}
108
109
/**
110
 * @brief Read the return value of a previously executed control sub command.
111
 * @param[in]   i2cd      The I2C Driver
112
 * @param[out]  data      The data read from the sub command
113
 * @param[in]   timeout   Timeout of the I2C bus
114
 *
115
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
116
 */
117
inline apalExitStatus_t
118
bq27500_lld_sub_command_read(const BQ27500Driver* const bq27500, uint16_t* const data, const apalTime_t timeout)
119
{
120
  apalDbgAssert(bq27500 != NULL);
121
  apalDbgAssert(data != NULL);
122
123
  uint8_t buffer[3] = {0, 0, 0};
124
  apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 1, &buffer[1], 2, timeout);
125
  *data = (uint16_t)((buffer[2] << 8) | buffer[1]);
126
  return status;
127
}
128
129
/**
130
 * @brief Execute on of the extended commands.
131
 * @param[in]   i2cd      The I2C Driver
132
 * @param[in]   cmd       The ext command to be executed
133
 * @param[in]   rw        Use the command in read or write mode
134
 * @param[in]   buffer    A buffer for input and output
135
 * @param[in]   length    length of the buffer
136
 * @param[in]   offset    Offset for reading or writing
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_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)
143
{
144
  apalDbgAssert(bq27500 != NULL);
145
  apalDbgAssert(buffer != NULL);
146
147
  uint8_t in_buffer[1+length];
148
  in_buffer[0] = cmd + offset;
149
  if (rw == BQ27500_LLD_EXT_CMD_WRITE) {
150
    memcpy(in_buffer+1, buffer, length);
151
  }
152
  uint8_t txbytes = 1 + ((rw == BQ27500_LLD_EXT_CMD_WRITE) ? length : 0);
153
  uint8_t rxbytes = ((rw == BQ27500_LLD_EXT_CMD_READ) ? length : 0);
154
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, in_buffer, txbytes, ((rw == BQ27500_LLD_EXT_CMD_READ) ? buffer : NULL), rxbytes, timeout);
155
}
156
157
/**
158
 * @brief Send data via the CTNL command.
159
 * @param[in]   i2cd      The I2C Driver
160
 * @param[in]   data      Data to be sent
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_send_ctnl_data(const BQ27500Driver* const bq27500, const uint16_t data, const apalTime_t timeout)
167
{
168
  apalDbgAssert(bq27500 != NULL);
169
170
  uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(data & 0x00FFu), (uint8_t)((data & 0xFF00u) >> 8)};
171
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout);
172
}
173
174
/**
175
 * @bried Computes the checksum of blockdata.
176
 * @param[in]   blockdata   Data to compute the checksum of (32 byte buffer)
177
 * @param[out]  sum         The computed checksum
178
 *
179
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
180
 */
181
inline apalExitStatus_t
182
bq27500_lld_compute_blockdata_checksum(const uint8_t* const blockdata, uint8_t* const sum)
183
{
184
  apalDbgAssert(blockdata != NULL);
185
  apalDbgAssert(sum != NULL);
186
187
  *sum = 0;
188
  for (uint8_t dataIdx = 0; dataIdx < 32; dataIdx++) {
189
    *sum += blockdata[dataIdx];
190
  }
191
  *sum = 0xFF - *sum;
192
  return APAL_STATUS_SUCCESS;
193
}
194
195
#endif /* defined(AMIROLLD_CFG_USE_BQ27500) */
196 5e2f673b Marc Rothmann
197
/** @} */