Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / P9221R / v1 / alld_P9221R_v1.c @ 73345246

History | View | Annotate | Download (7.435 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_P9221R_v1.c
21
 *
22
 * @brief   Power Monitor function implementations
23
 *
24
 * @addtogroup lld_power
25
 * @{
26
 */
27

    
28
#include <alld_P9221R.h>
29

    
30
#if (defined(AMIROLLD_CFG_P9221R) && (AMIROLLD_CFG_P9221R == 1)) || defined(__DOXYGEN__)
31

    
32
/******************************************************************************/
33
/* LOCAL DEFINITIONS                                                          */
34
/******************************************************************************/
35

    
36
/******************************************************************************/
37
/* EXPORTED VARIABLES                                                         */
38
/******************************************************************************/
39

    
40
/******************************************************************************/
41
/* LOCAL TYPES                                                                */
42
/******************************************************************************/
43

    
44
/******************************************************************************/
45
/* LOCAL VARIABLES                                                            */
46
/******************************************************************************/
47

    
48
/******************************************************************************/
49
/* LOCAL FUNCTIONS                                                            */
50
/******************************************************************************/
51

    
52
/******************************************************************************/
53
/* EXPORTED FUNCTIONS                                                         */
54
/******************************************************************************/
55

    
56

    
57
/**
58
 * @brief Read the value of one or more of the registers.
59
 * @param[in]   i2cd        i2c driver
60
 * @param[in]   PRd         p9221r driver
61
 * @param[in]   addr        register address
62
 * @param[out]  data        register content
63
 * @param[in]   num         number of subsequent registers to read
64
 * @param[in]   timeout     timeout
65
 * @return                  An indicator whether the call was successfull
66
 */
67
apalExitStatus_t p9221r_lld_read_register(const P9221RDriver* const p9221r, const p9221r_lld_register_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
68
{
69
  apalDbgAssert(p9221r != NULL);
70
  apalDbgAssert(p9221r->i2cd != NULL);
71
  apalDbgAssert(data != NULL);
72

    
73
  uint8_t tx[2] = {(addr & 0xFF00) >> 8, addr & 0x00FF};
74

    
75
  apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), tx, 2, data, num, timeout);
76

    
77
  return status;
78
}
79

    
80
/**
81
 * @brief Write the value of one or more of the registers.
82
 * @param[in]   i2cd        i2c driver
83
 * @param[in]   PRd         p9221r driver
84
 * @param[in]   addr        register address
85
 * @param[in]   data        data to write
86
 * @param[in]   num         number of subsequent registers to read
87
 * @param[in]   timeout     timeout
88
 * @return                  An indicator whether the call was successfull
89
 */
90
apalExitStatus_t p9221r_lld_write_register(const P9221RDriver* const p9221r, const p9221r_lld_register_t addr, const uint16_t* const data, const uint8_t num, const apalTime_t timeout)
91
{
92
  apalDbgAssert(p9221r != NULL);
93
  apalDbgAssert(p9221r->i2cd != NULL);
94
  apalDbgAssert(data != NULL);
95

    
96
  uint8_t buffer[1+2*num];
97
  buffer[0] = addr;
98
  for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) {
99
    buffer[dataIdx*2+1] = data[dataIdx] >> 8;
100
    buffer[dataIdx*2+2] = data[dataIdx] & (0x00FFu);
101
  }
102
  return apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), buffer, 1+2*num, NULL, 0, timeout);
103
}
104

    
105
/**
106
 * @brief Read the x_alignment.
107
 * @param[in]   i2cd        i2c driver
108
 * @param[in]   PRd         p9221r driver
109
 * @param[out]  x_alignment alignment register content
110
 * @param[in]   timeout     timeout
111
 * @return                  An indicator whether the call was successfull
112
 */
113
apalExitStatus_t p9221r_lld_read_x_alignment(const P9221RDriver* const p9221r, int8_t* const x_alignment, const apalTime_t timeout)
114
{
115
    uint8_t address = P9221R_LLD_REGISTER_ALIGNMENT_X;
116
    apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), &address, 2, x_alignment, 1, timeout); // TODO check if implicit cast is ok (uint)
117

    
118
    return status;
119
}
120

    
121
/**
122
 * @brief Read the y_alignment.
123
 * @param[in]   i2cd        i2c driver
124
 * @param[in]   PRd         p9221r driver
125
 * @param[out]  y_alignment alignment register content
126
 * @param[in]   timeout     timeout
127
 * @return                  An indicator whether the call was successfull
128
 */
129
apalExitStatus_t p9221r_lld_read_y_alignment(const P9221RDriver* const p9221r, int8_t* const y_alignment, const apalTime_t timeout)
130
{
131
    uint8_t address = P9221R_LLD_REGISTER_ALIGNMENT_Y;
132
    apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), &address, 2, y_alignment, 1, timeout); // TODO check if implicit cast is ok (uint)
133

    
134
    return status;
135
}
136

    
137
/**
138
 * @brief Read the voltage.
139
 * @param[in]   i2cd        i2c driver
140
 * @param[in]   PRd         p9221r driver
141
 * @param[out]  voltage     voltage register content
142
 * @param[in]   timeout     timeout
143
 * @return                  An indicator whether the call was successfull
144
 */
145
apalExitStatus_t p9221r_lld_read_voltage(const P9221RDriver* const p9221r, float* const voltage, const apalTime_t timeout)
146
{
147
    uint8_t addr = P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB;
148
    uint8_t buffer[2];
149
    uint16_t value;
150

    
151
    apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), (uint8_t*)&addr, 1, buffer, 2, timeout);
152
    value = ((buffer[1] << 8) & 0x0F) | buffer[0]; // 4MSB are in buffer[1] so we shift and mask, 8LSB are in buffer[0]
153
    *voltage = (((float) value) * 6 * 2.1)/4095.0;
154

    
155
    return status;
156
}
157

    
158
/**
159
 * @brief Read the current.
160
 * @param[in]   i2cd        i2c driver
161
 * @param[in]   PRd         p9221r driver
162
 * @param[out]  current     register content
163
 * @param[in]   timeout     timeout
164
 * @return                  An indicator whether the call was successfull
165
 */
166
apalExitStatus_t p9221r_lld_read_current(const P9221RDriver* const p9221r, float* const current, const apalTime_t timeout)
167
{
168
    uint8_t addr = P9221R_LLD_REGISTER_IOUT_CURRENT_LSB;
169
    uint8_t buffer[2];
170
    uint16_t value;
171

    
172
    apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), (uint8_t*)&addr, 1, buffer, 2, timeout);
173
    value = (buffer[1] << 8) | buffer[0]; // 8MSB are in buffer[1] so we shift, 8LSB are in buffer[0]
174
    *current = (((float) value) * 2 * 2.1)/4095.0;
175

    
176
    return status;
177
}
178

    
179

    
180
#endif /* defined(AMIROLLD_CFG_P9221R) && (AMIROLLD_CFG_P9221R == 1) */
181

    
182

    
183

    
184

    
185

    
186

    
187

    
188

    
189

    
190

    
191

    
192

    
193

    
194

    
195

    
196

    
197

    
198

    
199

    
200

    
201

    
202

    
203

    
204

    
205

    
206

    
207

    
208

    
209