Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / bq27500 / v1 / alld_bq27500.c @ 9466e34d

History | View | Annotate | Download (8.469 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
#include <string.h>
29

    
30
/******************************************************************************/
31
/* LOCAL DEFINITIONS                                                          */
32
/******************************************************************************/
33

    
34
/******************************************************************************/
35
/* EXPORTED VARIABLES                                                         */
36
/******************************************************************************/
37

    
38
/******************************************************************************/
39
/* LOCAL TYPES                                                                */
40
/******************************************************************************/
41

    
42
/******************************************************************************/
43
/* LOCAL VARIABLES                                                            */
44
/******************************************************************************/
45

    
46
/******************************************************************************/
47
/* LOCAL FUNCTIONS                                                            */
48
/******************************************************************************/
49

    
50
/******************************************************************************/
51
/* EXPORTED FUNCTIONS                                                         */
52
/******************************************************************************/
53

    
54
/**
55
 * @brief Read the battery low Gpio pin.
56
 * @param[in]   bqd       The bq27500 driver
57
 * @param[out]  batlow    The battery low state
58
 *
59
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
60
 */
61
apalExitStatus_t bq27500_lld_read_batlow(const BQ27500Driver* const bq27500, bq27500_lld_batlow_t* const batlow)
62
{
63
  apalDbgAssert(bq27500 != NULL);
64
  apalDbgAssert(batlow != NULL);
65

    
66
  apalControlGpioState_t gpio_state;
67
  apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batlow, &gpio_state);
68
  *batlow = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_LOW : BQ27500_LLD_BATTERY_NOT_LOW;
69
  return status;
70
}
71

    
72
/**
73
 * @brief Read the battery good Gpio pin.
74
 * @param[in]   bqd       The bq27500 driver
75
 * @param[out]  batgood   The battery good state
76
 *
77
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
78
 */
79
apalExitStatus_t bq27500_lld_read_batgood(const BQ27500Driver* const bq27500, bq27500_lld_batgood_t* const batgood)
80
{
81
  apalDbgAssert(bq27500 != NULL);
82
  apalDbgAssert(batgood != NULL);
83

    
84
  apalControlGpioState_t gpio_state;
85
  apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batgood, &gpio_state);
86
  *batgood = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_GOOD : BQ27500_LLD_BATTERY_NOT_GOOD;
87
  return status;
88
}
89

    
90
/**
91
 * @brief Execute one of the standard commands.
92
 * @param[in]   i2cd      The I2C Driver
93
 * @param[in]   cmd       The command to be executed
94
 * @param[out]  dst       The return value of the command
95
 * @param[in]   timeout   Timeout of the I2C bus
96
 *
97
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
98
 */
99
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)
100
{
101
  apalDbgAssert(bq27500 != NULL);
102
  apalDbgAssert(dst != NULL);
103

    
104
  uint8_t buffer[2];
105
  apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, (uint8_t*)&cmd, 1, buffer, 2, timeout);
106
  *dst = (uint16_t)((buffer[1] << 8) | buffer[0]);
107
  return status;
108
}
109

    
110
/**
111
 * @brief Execute one of the control sub commands.
112
 * @param[in]   i2cd      The I2C Driver
113
 * @param[in]   cmd       The sub command to be executed
114
 * @param[in]   timeout   Timeout of the I2C bus
115
 *
116
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
117
 */
118
apalExitStatus_t bq27500_lld_sub_command_call(const BQ27500Driver* const bq27500, const bq27500_lld_control_subcmd_t cmd, const apalTime_t timeout)
119
{
120
  apalDbgAssert(bq27500 != NULL);
121

    
122
  uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(cmd & 0x00FFu), (uint8_t)((cmd & 0xFF00u) >> 8)};
123
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout);
124
}
125

    
126
/**
127
 * @brief Read the return value of a previously executed control sub command.
128
 * @param[in]   i2cd      The I2C Driver
129
 * @param[out]  data      The data read from the sub command
130
 * @param[in]   timeout   Timeout of the I2C bus
131
 *
132
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
133
 */
134
apalExitStatus_t bq27500_lld_sub_command_read(const BQ27500Driver* const bq27500, uint16_t* const data, const apalTime_t timeout)
135
{
136
  apalDbgAssert(bq27500 != NULL);
137
  apalDbgAssert(data != NULL);
138

    
139
  uint8_t buffer[3] = {0, 0, 0};
140
  apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 1, &buffer[1], 2, timeout);
141
  *data = (uint16_t)((buffer[2] << 8) | buffer[1]);
142
  return status;
143
}
144

    
145
/**
146
 * @brief Execute on of the extended commands.
147
 * @param[in]   i2cd      The I2C Driver
148
 * @param[in]   cmd       The ext command to be executed
149
 * @param[in]   rw        Use the command in read or write mode
150
 * @param[in]   buffer    A buffer for input and output
151
 * @param[in]   length    length of the buffer
152
 * @param[in]   offset    Offset for reading or writing
153
 * @param[in]   timeout   Timeout of the I2C bus
154
 *
155
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
156
 */
157
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)
158
{
159
  apalDbgAssert(bq27500 != NULL);
160
  apalDbgAssert(buffer != NULL);
161

    
162
  uint8_t in_buffer[1+length];
163
  in_buffer[0] = cmd + offset;
164
  if (rw == BQ27500_LLD_EXT_CMD_WRITE) {
165
    memcpy(in_buffer+1, buffer, length);
166
  }
167
  uint8_t txbytes = 1 + ((rw == BQ27500_LLD_EXT_CMD_WRITE) ? length : 0);
168
  uint8_t rxbytes = ((rw == BQ27500_LLD_EXT_CMD_READ) ? length : 0);
169
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, in_buffer, txbytes, ((rw == BQ27500_LLD_EXT_CMD_READ) ? buffer : NULL), rxbytes, timeout);
170
}
171

    
172
/**
173
 * @brief Send data via the CTNL command.
174
 * @param[in]   i2cd      The I2C Driver
175
 * @param[in]   data      Data to be sent
176
 * @param[in]   timeout   Timeout of the I2C bus
177
 *
178
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
179
 */
180
apalExitStatus_t bq27500_lld_send_ctnl_data(const BQ27500Driver* const bq27500, const uint16_t data, const apalTime_t timeout)
181
{
182
  apalDbgAssert(bq27500 != NULL);
183

    
184
  uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(data & 0x00FFu), (uint8_t)((data & 0xFF00u) >> 8)};
185
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout);
186
}
187

    
188
/**
189
 * @bried Computes the checksum of blockdata.
190
 * @param[in]   blockdata   Data to compute the checksum of (32 byte buffer)
191
 * @param[out]  sum         The computed checksum
192
 *
193
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
194
 */
195
apalExitStatus_t bq27500_lld_compute_blockdata_checksum(const uint8_t* const blockdata, uint8_t* const sum)
196
{
197
  apalDbgAssert(blockdata != NULL);
198
  apalDbgAssert(sum != NULL);
199

    
200
  *sum = 0;
201
  for (uint8_t dataIdx = 0; dataIdx < 32; dataIdx++) {
202
    *sum += blockdata[dataIdx];
203
  }
204
  *sum = 0xFF - *sum;
205
  return APAL_STATUS_SUCCESS;
206
}
207

    
208
/** @} */
209