Revision e7af07e7
drivers/P9221R/v1/alld_P9221R.c | ||
---|---|---|
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.c |
|
21 |
* |
|
22 |
* @brief Power Monitor function implementations |
|
23 |
* |
|
24 |
* @addtogroup lld_power |
|
25 |
* @{ |
|
26 |
*/ |
|
27 |
|
|
28 |
#include <alld_P9221R.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 value of one or more of the registers. |
|
56 |
* @param[in] i2cd i2c driver |
|
57 |
* @param[in] PRd p9221r driver |
|
58 |
* @param[in] addr register address |
|
59 |
* @param[out] data register content |
|
60 |
* @param[in] num number of subsequent registers to read |
|
61 |
* @param[in] timeout timeout |
|
62 |
* @return An indicator whether the call was successfull |
|
63 |
*/ |
|
64 |
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) |
|
65 |
{ |
|
66 |
apalDbgAssert(p9221r != NULL); |
|
67 |
apalDbgAssert(p9221r->i2cd != NULL); |
|
68 |
apalDbgAssert(data != NULL); |
|
69 |
|
|
70 |
uint8_t tx[2] = {(addr & 0xFF00) >> 8, addr & 0x00FF}; |
|
71 |
|
|
72 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), tx, 2, data, num, timeout); |
|
73 |
|
|
74 |
return status; |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* @brief Write the value of one or more of the registers. |
|
79 |
* @param[in] i2cd i2c driver |
|
80 |
* @param[in] PRd p9221r driver |
|
81 |
* @param[in] addr register address |
|
82 |
* @param[in] data data to write |
|
83 |
* @param[in] num number of subsequent registers to read |
|
84 |
* @param[in] timeout timeout |
|
85 |
* @return An indicator whether the call was successfull |
|
86 |
*/ |
|
87 |
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) |
|
88 |
{ |
|
89 |
apalDbgAssert(p9221r != NULL); |
|
90 |
apalDbgAssert(p9221r->i2cd != NULL); |
|
91 |
apalDbgAssert(data != NULL); |
|
92 |
|
|
93 |
uint8_t buffer[1+2*num]; |
|
94 |
buffer[0] = addr; |
|
95 |
for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) { |
|
96 |
buffer[dataIdx*2+1] = data[dataIdx] >> 8; |
|
97 |
buffer[dataIdx*2+2] = data[dataIdx] & (0x00FFu); |
|
98 |
} |
|
99 |
return apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), buffer, 1+2*num, NULL, 0, timeout); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* @brief Read the x_alignment. |
|
104 |
* @param[in] i2cd i2c driver |
|
105 |
* @param[in] PRd p9221r driver |
|
106 |
* @param[out] x_alignment alignment register content |
|
107 |
* @param[in] timeout timeout |
|
108 |
* @return An indicator whether the call was successfull |
|
109 |
*/ |
|
110 |
apalExitStatus_t p9221r_lld_read_x_alignment(const P9221RDriver* const p9221r, int8_t* const x_alignment, const apalTime_t timeout) |
|
111 |
{ |
|
112 |
uint8_t addr[2] = {(P9221R_LLD_REGISTER_ALIGNMENT_X & 0xFF00) >> 8, P9221R_LLD_REGISTER_ALIGNMENT_X & 0x00FF}; |
|
113 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, (uint8_t*)x_alignment, 2, timeout); // TODO check if implicit cast is ok (uint) |
|
114 |
|
|
115 |
return status; |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* @brief Read the y_alignment. |
|
120 |
* @param[in] i2cd i2c driver |
|
121 |
* @param[in] PRd p9221r driver |
|
122 |
* @param[out] y_alignment alignment register content |
|
123 |
* @param[in] timeout timeout |
|
124 |
* @return An indicator whether the call was successfull |
|
125 |
*/ |
|
126 |
apalExitStatus_t p9221r_lld_read_y_alignment(const P9221RDriver* const p9221r, int8_t* const y_alignment, const apalTime_t timeout) |
|
127 |
{ |
|
128 |
uint8_t addr[2] = {(P9221R_LLD_REGISTER_ALIGNMENT_Y & 0xFF00) >> 8, P9221R_LLD_REGISTER_ALIGNMENT_Y & 0x00FF}; |
|
129 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, (uint8_t*)y_alignment, 2, timeout); // TODO check if implicit cast is ok (uint) |
|
130 |
|
|
131 |
return status; |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* @brief Read the voltage. |
|
136 |
* @param[in] i2cd i2c driver |
|
137 |
* @param[in] PRd p9221r driver |
|
138 |
* @param[out] voltage voltage register content |
|
139 |
* @param[in] timeout timeout |
|
140 |
* @return An indicator whether the call was successfull |
|
141 |
*/ |
|
142 |
apalExitStatus_t p9221r_lld_read_voltage(const P9221RDriver* const p9221r, float* const voltage, const apalTime_t timeout) |
|
143 |
{ |
|
144 |
uint8_t addr[2] = {(P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB & 0xFF00) >> 8, P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB & 0x00FF}; |
|
145 |
uint8_t buffer[2]; |
|
146 |
uint16_t value; |
|
147 |
|
|
148 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, buffer, 2, timeout); |
|
149 |
value = ((buffer[1] & 0x0F) << 8) | buffer[0]; // 4MSB are in buffer[1] so we shift and mask, 8LSB are in buffer[0] |
|
150 |
*voltage = (((float) value) * 6 * 2.1f)/4095.0f; |
|
151 |
|
|
152 |
return status; |
|
153 |
} |
|
154 |
|
|
155 |
/** |
|
156 |
* @brief Read the current. |
|
157 |
* @param[in] i2cd i2c driver |
|
158 |
* @param[in] PRd p9221r driver |
|
159 |
* @param[out] current register content |
|
160 |
* @param[in] timeout timeout |
|
161 |
* @return An indicator whether the call was successfull |
|
162 |
*/ |
|
163 |
apalExitStatus_t p9221r_lld_read_current(const P9221RDriver* const p9221r, float* const current, const apalTime_t timeout) |
|
164 |
{ |
|
165 |
uint8_t addr[2] = {(P9221R_LLD_REGISTER_IOUT_CURRENT_LSB & 0xFF00) >> 8, P9221R_LLD_REGISTER_IOUT_CURRENT_LSB & 0x00FF}; |
|
166 |
uint8_t buffer[2]; |
|
167 |
uint16_t value; |
|
168 |
|
|
169 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, buffer, 2, timeout); |
|
170 |
value = (buffer[1] << 8) | buffer[0]; // 8MSB are in buffer[1] so we shift, 8LSB are in buffer[0] |
|
171 |
*current = (((float) value) * 2 * 2.1f)/4095.0f; |
|
172 |
|
|
173 |
return status; |
|
174 |
} |
|
175 |
|
|
176 |
/** @} */ |
|
177 |
|
drivers/P9221R/v1/alld_P9221R.h | ||
---|---|---|
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.h |
|
21 |
* @brief Power Monitor macros and structures. |
|
22 |
* |
|
23 |
* @addtogroup lld_power |
|
24 |
* @{ |
|
25 |
*/ |
|
26 |
|
|
27 |
#ifndef AMIROLLD_P9221R_H |
|
28 |
#define AMIROLLD_P9221R_H |
|
29 |
|
|
30 |
#include <amiro-lld.h> |
|
31 |
|
|
32 |
/******************************************************************************/ |
|
33 |
/* CONSTANTS */ |
|
34 |
/******************************************************************************/ |
|
35 |
|
|
36 |
/** |
|
37 |
* @brief Maximum I2C frequency. |
|
38 |
*/ |
|
39 |
#define P9221R_LLD_I2C_MAXFREQUENCY 400000 |
|
40 |
|
|
41 |
/******************************************************************************/ |
|
42 |
/* SETTINGS */ |
|
43 |
/******************************************************************************/ |
|
44 |
|
|
45 |
/******************************************************************************/ |
|
46 |
/* CHECKS */ |
|
47 |
/******************************************************************************/ |
|
48 |
|
|
49 |
/******************************************************************************/ |
|
50 |
/* DATA STRUCTURES AND TYPES */ |
|
51 |
/******************************************************************************/ |
|
52 |
|
|
53 |
/** |
|
54 |
* @brief I2C address masks. |
|
55 |
*/ |
|
56 |
enum { |
|
57 |
P9221R_LLD_I2C_ADDR_FIXED = 0x0061u, |
|
58 |
}; |
|
59 |
|
|
60 |
/** |
|
61 |
* @brief Registers. |
|
62 |
*/ |
|
63 |
typedef enum { |
|
64 |
P9221R_LLD_REGISTER_PARTNUMBER = 0x0000, |
|
65 |
P9221R_LLD_REGISTER_INTERRUPTENABLE = 0x0038, |
|
66 |
// Writeable Registers |
|
67 |
P9221R_LLD_REGISTER_BATTERY_STATUS_SENT = 0x003A, |
|
68 |
P9221R_LLD_REGISTER_END_POWER_TRANSFER = 0x003B, |
|
69 |
// Read-Only Registers |
|
70 |
P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB = 0x003C, |
|
71 |
P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_MSB = 0x003D, |
|
72 |
P9221R_LLD_REGISTER_IOUT_CURRENT_LSB = 0x0044, |
|
73 |
P9221R_LLD_REGISTER_IOUT_CURRENT_MSB = 0x0045, |
|
74 |
P9221R_LLD_REGISTER_OPERATING_FREQUENCY_MSB = 0x0048, |
|
75 |
P9221R_LLD_REGISTER_OPERATING_FREQUENCY_LSB = 0x0049, |
|
76 |
P9221R_LLD_REGISTER_ALIGNMENT_X = 0x004B, |
|
77 |
P9221R_LLD_REGISTER_ALIGNMENT_Y = 0x004C, |
|
78 |
} p9221r_lld_register_t; |
|
79 |
|
|
80 |
|
|
81 |
typedef struct { |
|
82 |
apalI2CDriver_t* i2cd; |
|
83 |
apalI2Caddr_t addr; |
|
84 |
uint16_t current_lsb_uA; |
|
85 |
//P9221R_lld_cfg_t *config; |
|
86 |
} P9221RDriver; |
|
87 |
|
|
88 |
|
|
89 |
/******************************************************************************/ |
|
90 |
/* MACROS */ |
|
91 |
/******************************************************************************/ |
|
92 |
|
|
93 |
/******************************************************************************/ |
|
94 |
/* EXTERN DECLARATIONS */ |
|
95 |
/******************************************************************************/ |
|
96 |
|
|
97 |
#ifdef __cplusplus |
|
98 |
extern "C" { |
|
99 |
#endif |
|
100 |
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); |
|
101 |
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); |
|
102 |
apalExitStatus_t p9221r_lld_read_x_alignment(const P9221RDriver* const p9221r, int8_t* const x_alignment, const apalTime_t timeout); |
|
103 |
apalExitStatus_t p9221r_lld_read_y_alignment(const P9221RDriver* const p9221r, int8_t* const y_alignment, const apalTime_t timeout); |
|
104 |
apalExitStatus_t p9221r_lld_read_voltage(const P9221RDriver* const p9221r, float* const voltage, const apalTime_t timeout); |
|
105 |
apalExitStatus_t p9221r_lld_read_current(const P9221RDriver* const p9221r, float* const current, const apalTime_t timeout); |
|
106 |
|
|
107 |
#ifdef __cplusplus |
|
108 |
} |
|
109 |
#endif |
|
110 |
|
|
111 |
/******************************************************************************/ |
|
112 |
/* INLINE FUNCTIONS */ |
|
113 |
/******************************************************************************/ |
|
114 |
|
|
115 |
#endif /* AMIROLLD_P9221R_H */ |
|
116 |
|
|
117 |
/** @} */ |
|
118 |
|
include/P9221R/alld_P9221R.h | ||
---|---|---|
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.h |
|
21 |
* |
|
22 |
* @addtogroup lld_power |
|
23 |
* @{ |
|
24 |
*/ |
|
25 |
|
|
26 |
#ifndef AMIROLLD_P9221R_H |
|
27 |
#define AMIROLLD_P9221R_H |
|
28 |
|
|
29 |
#include <amiro-lld.h> |
|
30 |
|
|
31 |
#if defined(AMIROLLD_CFG_P9221R) |
|
32 |
|
|
33 |
#if (AMIROLLD_CFG_P9221R == 1) |
|
34 |
#include "v1/alld_P9221R_v1.h" |
|
35 |
#else |
|
36 |
#error "invalid value assigned to AMIROLLD_CFG_P9221R in alldconf.h" |
|
37 |
#endif |
|
38 |
|
|
39 |
#endif /* defined(AMIROLLD_CFG_P9221R) */ |
|
40 |
|
|
41 |
#endif /* AMIROLLD_P9221R_H */ |
|
42 |
|
|
43 |
/** @} */ |
include/P9221R/v1/alld_P9221R_v1.h | ||
---|---|---|
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.h |
|
21 |
* @brief Power Monitor macros and structures. |
|
22 |
* |
|
23 |
* @addtogroup lld_power |
|
24 |
* @{ |
|
25 |
*/ |
|
26 |
|
|
27 |
#ifndef AMIROLLD_P9221R_V1_H |
|
28 |
#define AMIROLLD_P9221R_V1_H |
|
29 |
|
|
30 |
#include <amiro-lld.h> |
|
31 |
|
|
32 |
#if (defined(AMIROLLD_CFG_P9221R) && (AMIROLLD_CFG_P9221R == 1)) || defined(__DOXYGEN__) |
|
33 |
|
|
34 |
/******************************************************************************/ |
|
35 |
/* CONSTANTS */ |
|
36 |
/******************************************************************************/ |
|
37 |
|
|
38 |
/** |
|
39 |
* @brief Maximum I2C frequency. |
|
40 |
*/ |
|
41 |
#define P9221R_LLD_I2C_MAXFREQUENCY 400000 |
|
42 |
|
|
43 |
/******************************************************************************/ |
|
44 |
/* SETTINGS */ |
|
45 |
/******************************************************************************/ |
|
46 |
|
|
47 |
/******************************************************************************/ |
|
48 |
/* CHECKS */ |
|
49 |
/******************************************************************************/ |
|
50 |
|
|
51 |
/******************************************************************************/ |
|
52 |
/* DATA STRUCTURES AND TYPES */ |
|
53 |
/******************************************************************************/ |
|
54 |
|
|
55 |
/** |
|
56 |
* @brief I2C address masks. |
|
57 |
*/ |
|
58 |
enum { |
|
59 |
P9221R_LLD_I2C_ADDR_FIXED = 0x0061u, |
|
60 |
}; |
|
61 |
|
|
62 |
/** |
|
63 |
* @brief Registers. |
|
64 |
*/ |
|
65 |
typedef enum { |
|
66 |
P9221R_LLD_REGISTER_PARTNUMBER = 0x0000, |
|
67 |
P9221R_LLD_REGISTER_INTERRUPTENABLE = 0x0038, |
|
68 |
// Writeable Registers |
|
69 |
P9221R_LLD_REGISTER_BATTERY_STATUS_SENT = 0x003A, |
|
70 |
P9221R_LLD_REGISTER_END_POWER_TRANSFER = 0x003B, |
|
71 |
// Read-Only Registers |
|
72 |
P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB = 0x003C, |
|
73 |
P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_MSB = 0x003D, |
|
74 |
P9221R_LLD_REGISTER_IOUT_CURRENT_LSB = 0x0044, |
|
75 |
P9221R_LLD_REGISTER_IOUT_CURRENT_MSB = 0x0045, |
|
76 |
P9221R_LLD_REGISTER_OPERATING_FREQUENCY_MSB = 0x0048, |
|
77 |
P9221R_LLD_REGISTER_OPERATING_FREQUENCY_LSB = 0x0049, |
|
78 |
P9221R_LLD_REGISTER_ALIGNMENT_X = 0x004B, |
|
79 |
P9221R_LLD_REGISTER_ALIGNMENT_Y = 0x004C, |
|
80 |
} p9221r_lld_register_t; |
|
81 |
|
|
82 |
|
|
83 |
typedef struct { |
|
84 |
apalI2CDriver_t* i2cd; |
|
85 |
apalI2Caddr_t addr; |
|
86 |
uint16_t current_lsb_uA; |
|
87 |
//P9221R_lld_cfg_t *config; |
|
88 |
}P9221RDriver; |
|
89 |
|
|
90 |
|
|
91 |
/******************************************************************************/ |
|
92 |
/* MACROS */ |
|
93 |
/******************************************************************************/ |
|
94 |
|
|
95 |
/******************************************************************************/ |
|
96 |
/* EXTERN DECLARATIONS */ |
|
97 |
/******************************************************************************/ |
|
98 |
|
|
99 |
#ifdef __cplusplus |
|
100 |
extern "C" { |
|
101 |
#endif |
|
102 |
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); |
|
103 |
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); |
|
104 |
apalExitStatus_t p9221r_lld_read_x_alignment(const P9221RDriver* const p9221r, int8_t* const x_alignment, const apalTime_t timeout); |
|
105 |
apalExitStatus_t p9221r_lld_read_y_alignment(const P9221RDriver* const p9221r, int8_t* const y_alignment, const apalTime_t timeout); |
|
106 |
apalExitStatus_t p9221r_lld_read_voltage(const P9221RDriver* const p9221r, float* const voltage, const apalTime_t timeout); |
|
107 |
apalExitStatus_t p9221r_lld_read_current(const P9221RDriver* const p9221r, float* const current, const apalTime_t timeout); |
|
108 |
|
|
109 |
#ifdef __cplusplus |
|
110 |
} |
|
111 |
#endif |
|
112 |
|
|
113 |
/******************************************************************************/ |
|
114 |
/* INLINE FUNCTIONS */ |
|
115 |
/******************************************************************************/ |
|
116 |
|
|
117 |
#endif /* defined(AMIROLLD_CFG_P9221R) && (AMIROLLD_CFG_P9221R == 1) */ |
|
118 |
|
|
119 |
#endif /* AMIROLLD_P9221R_V1_H */ |
source/P9221R/v1/alld_P9221R_v1.c | ||
---|---|---|
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 addr[2] = {(P9221R_LLD_REGISTER_ALIGNMENT_X & 0xFF00) >> 8, P9221R_LLD_REGISTER_ALIGNMENT_X & 0x00FF}; |
|
116 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, (uint8_t*)x_alignment, 2, 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 addr[2] = {(P9221R_LLD_REGISTER_ALIGNMENT_Y & 0xFF00) >> 8, P9221R_LLD_REGISTER_ALIGNMENT_Y & 0x00FF}; |
|
132 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, (uint8_t*)y_alignment, 2, 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[2] = {(P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB & 0xFF00) >> 8, P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB & 0x00FF}; |
|
148 |
uint8_t buffer[2]; |
|
149 |
uint16_t value; |
|
150 |
|
|
151 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, buffer, 2, timeout); |
|
152 |
value = ((buffer[1] & 0x0F) << 8) | buffer[0]; // 4MSB are in buffer[1] so we shift and mask, 8LSB are in buffer[0] |
|
153 |
*voltage = (((float) value) * 6 * 2.1f)/4095.0f; |
|
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[2] = {(P9221R_LLD_REGISTER_IOUT_CURRENT_LSB & 0xFF00) >> 8, P9221R_LLD_REGISTER_IOUT_CURRENT_LSB & 0x00FF}; |
|
169 |
uint8_t buffer[2]; |
|
170 |
uint16_t value; |
|
171 |
|
|
172 |
apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), addr, 2, 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.1f)/4095.0f; |
|
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 |
|
Also available in: Unified diff