Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_bq27500.c @ cf1f756b

History | View | Annotate | Download (7.08 KB)

1
/*
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
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
#include <alld_bq27500.h>
20

    
21
#if defined(AMIROLLD_CFG_USE_BQ27500) || defined(__DOXYGEN__)
22

    
23
#include <string.h>
24

    
25
/**
26
 * @brief Read the battery low Gpio pin.
27
 * @param[in]   bqd       The bq27500 driver
28
 * @param[out]  batlow    The battery low state
29
 *
30
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
31
 */
32
inline apalExitStatus_t
33
bq27500_lld_read_batlow(const BQ27500Driver* const bq27500, bq27500_lld_batlow_t* const batlow)
34
{
35
  apalDbgAssert(bq27500 != NULL);
36
  apalDbgAssert(batlow != NULL);
37

    
38
  apalControlGpioState_t gpio_state;
39
  apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batlow, &gpio_state);
40
  *batlow = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_LOW : BQ27500_LLD_BATTERY_NOT_LOW;
41
  return status;
42
}
43

    
44
/**
45
 * @brief Read the battery good Gpio pin.
46
 * @param[in]   bqd       The bq27500 driver
47
 * @param[out]  batgood   The battery good state
48
 *
49
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
50
 */
51
inline apalExitStatus_t
52
bq27500_lld_read_batgood(const BQ27500Driver* const bq27500, bq27500_lld_batgood_t* const batgood)
53
{
54
  apalDbgAssert(bq27500 != NULL);
55
  apalDbgAssert(batgood != NULL);
56

    
57
  apalControlGpioState_t gpio_state;
58
  apalExitStatus_t status = apalControlGpioGet(bq27500->gpio_batgood, &gpio_state);
59
  *batgood = (gpio_state == APAL_GPIO_ON) ? BQ27500_LLD_BATTERY_GOOD : BQ27500_LLD_BATTERY_NOT_GOOD;
60
  return status;
61
}
62

    
63
/**
64
 * @brief Execute one of the standard commands.
65
 * @param[in]   i2cd      The I2C Driver
66
 * @param[in]   cmd       The command to be executed
67
 * @param[out]  dst       The return value of the command
68
 * @param[in]   timeout   Timeout of the I2C bus
69
 *
70
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
71
 */
72
inline apalExitStatus_t
73
bq27500_lld_std_command(const BQ27500Driver* const bq27500, const bq27500_lld_std_command_t cmd, uint16_t* const dst, const apalTime_t timeout)
74
{
75
  apalDbgAssert(bq27500 != NULL);
76
  apalDbgAssert(dst != NULL);
77

    
78
  uint8_t buffer[2];
79
  apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, (uint8_t*)&cmd, 1, buffer, 2, timeout);
80
  *dst = (uint16_t)((buffer[1] << 8) | buffer[0]);
81
  return status;
82
}
83

    
84
/**
85
 * @brief Execute one of the control sub commands.
86
 * @param[in]   i2cd      The I2C Driver
87
 * @param[in]   cmd       The sub command to be executed
88
 * @param[in]   timeout   Timeout of the I2C bus
89
 *
90
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
91
 */
92
inline apalExitStatus_t
93
bq27500_lld_sub_command_call(const BQ27500Driver* const bq27500, const bq27500_lld_control_subcmd_t cmd, const apalTime_t timeout)
94
{
95
  apalDbgAssert(bq27500 != NULL);
96

    
97
  uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(cmd & 0x00FFu), (uint8_t)((cmd & 0xFF00u) >> 8)};
98
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout);
99
}
100

    
101
/**
102
 * @brief Read the return value of a previously executed control sub command.
103
 * @param[in]   i2cd      The I2C Driver
104
 * @param[out]  data      The data read from the sub command
105
 * @param[in]   timeout   Timeout of the I2C bus
106
 *
107
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
108
 */
109
inline apalExitStatus_t
110
bq27500_lld_sub_command_read(const BQ27500Driver* const bq27500, uint16_t* const data, const apalTime_t timeout)
111
{
112
  apalDbgAssert(bq27500 != NULL);
113
  apalDbgAssert(data != NULL);
114

    
115
  uint8_t buffer[3] = {0, 0, 0};
116
  apalExitStatus_t status = apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 1, &buffer[1], 2, timeout);
117
  *data = (uint16_t)((buffer[2] << 8) | buffer[1]);
118
  return status;
119
}
120

    
121
/**
122
 * @brief Execute on of the extended commands.
123
 * @param[in]   i2cd      The I2C Driver
124
 * @param[in]   cmd       The ext command to be executed
125
 * @param[in]   rw        Use the command in read or write mode
126
 * @param[in]   buffer    A buffer for input and output
127
 * @param[in]   length    length of the buffer
128
 * @param[in]   offset    Offset for reading or writing
129
 * @param[in]   timeout   Timeout of the I2C bus
130
 *
131
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
132
 */
133
inline apalExitStatus_t
134
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)
135
{
136
  apalDbgAssert(bq27500 != NULL);
137
  apalDbgAssert(buffer != NULL);
138

    
139
  uint8_t in_buffer[1+length];
140
  in_buffer[0] = cmd + offset;
141
  if (rw == BQ27500_LLD_EXT_CMD_WRITE) {
142
    memcpy(in_buffer+1, buffer, length);
143
  }
144
  uint8_t txbytes = 1 + ((rw == BQ27500_LLD_EXT_CMD_WRITE) ? length : 0);
145
  uint8_t rxbytes = ((rw == BQ27500_LLD_EXT_CMD_READ) ? length : 0);
146
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, in_buffer, txbytes, ((rw == BQ27500_LLD_EXT_CMD_READ) ? buffer : NULL), rxbytes, timeout);
147
}
148

    
149
/**
150
 * @brief Send data via the CTNL command.
151
 * @param[in]   i2cd      The I2C Driver
152
 * @param[in]   data      Data to be sent
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
inline apalExitStatus_t
158
bq27500_lld_send_ctnl_data(const BQ27500Driver* const bq27500, const uint16_t data, const apalTime_t timeout)
159
{
160
  apalDbgAssert(bq27500 != NULL);
161

    
162
  uint8_t buffer[3] = {BQ27500_LLD_STD_CMD_Control, (uint8_t)(data & 0x00FFu), (uint8_t)((data & 0xFF00u) >> 8)};
163
  return apalI2CMasterTransmit(bq27500->i2cd, BQ27500_LLD_I2C_ADDR, buffer, 3, NULL, 0, timeout);
164
}
165

    
166
/**
167
 * @bried Computes the checksum of blockdata.
168
 * @param[in]   blockdata   Data to compute the checksum of (32 byte buffer)
169
 * @param[out]  sum         The computed checksum
170
 *
171
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
172
 */
173
inline apalExitStatus_t
174
bq27500_lld_compute_blockdata_checksum(const uint8_t* const blockdata, uint8_t* const sum)
175
{
176
  apalDbgAssert(blockdata != NULL);
177
  apalDbgAssert(sum != NULL);
178

    
179
  *sum = 0;
180
  for (uint8_t dataIdx = 0; dataIdx < 32; dataIdx++) {
181
    *sum += blockdata[dataIdx];
182
  }
183
  *sum = 0xFF - *sum;
184
  return APAL_STATUS_SUCCESS;
185
}
186

    
187
#endif /* defined(AMIROLLD_CFG_USE_BQ27500) */