Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / P9221R / v1 / alld_P9221R.c @ 0b0996ce

History | View | Annotate | Download (6.219 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.c
21
 *
22
 * @brief   qi charger
23
 *
24
 * @addtogroup lld_power
25
 * @{
26
 */
27

    
28

    
29
#include <alld_P9221R.h>
30

    
31
/******************************************************************************/
32
/* LOCAL DEFINITIONS                                                          */
33
/******************************************************************************/
34

    
35
/******************************************************************************/
36
/* EXPORTED VARIABLES                                                         */
37
/******************************************************************************/
38

    
39
/******************************************************************************/
40
/* LOCAL TYPES                                                                */
41
/******************************************************************************/
42

    
43
/******************************************************************************/
44
/* LOCAL VARIABLES                                                            */
45
/******************************************************************************/
46

    
47
/******************************************************************************/
48
/* LOCAL FUNCTIONS                                                            */
49
/******************************************************************************/
50

    
51
/******************************************************************************/
52
/* EXPORTED FUNCTIONS                                                         */
53
/******************************************************************************/
54

    
55
/**
56
 * @brief Read the value of one or more of the registers.
57
 * @param[in]   i2cd        i2c driver
58
 * @param[in]   PRd         p9221r driver
59
 * @param[in]   addr        register address
60
 * @param[out]  data        register content
61
 * @param[in]   num         number of subsequent registers to read
62
 * @param[in]   timeout     timeout
63
 * @return                  An indicator whether the call was successfull
64
 */
65
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)
66
{
67
  apalDbgAssert(p9221r != NULL);
68
  apalDbgAssert(p9221r->i2cd != NULL);
69
  apalDbgAssert(data != NULL);
70

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

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

    
75
  return status;
76
}
77

    
78
/**
79
 * @brief Write the value of one or more of the registers.
80
 * @param[in]   i2cd        i2c driver
81
 * @param[in]   PRd         p9221r driver
82
 * @param[in]   addr        register address
83
 * @param[in]   data        data to write
84
 * @param[in]   num         number of subsequent registers to read
85
 * @param[in]   timeout     timeout
86
 * @return                  An indicator whether the call was successfull
87
 */
88
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)
89
{
90
  apalDbgAssert(p9221r != NULL);
91
  apalDbgAssert(p9221r->i2cd != NULL);
92
  apalDbgAssert(data != NULL);
93

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

    
103
/**
104
 * @brief Read the alignment.
105
 * @param[in]   PRd         p9221r driver
106
 * @param[out]  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_alignment(const P9221RDriver* const p9221r, int8_t* const alignment, const apalTime_t timeout)
111
{
112
    apalExitStatus_t status = p9221r_lld_read_register(p9221r, P9221R_LLD_REGISTER_ALIGNMENT_X, (uint8_t*) alignment, 2, timeout);
113

    
114
    return status;
115
}
116

    
117
/**
118
 * @brief Read the voltage.
119
 * @param[in]   i2cd        i2c driver
120
 * @param[in]   PRd         p9221r driver
121
 * @param[out]  voltage     voltage register content
122
 * @param[in]   timeout     timeout
123
 * @return                  An indicator whether the call was successfull
124
 */
125
apalExitStatus_t p9221r_lld_read_voltage(const P9221RDriver* const p9221r, float* const voltage, const apalTime_t timeout)
126
{
127
    uint8_t buffer[2];
128
    uint16_t value;
129

    
130
    apalExitStatus_t status = p9221r_lld_read_register(p9221r, P9221R_LLD_I2C_ADDR_FIXED, buffer, 2, timeout);
131

    
132
    value = ((buffer[1] & 0x0F) << 8) | buffer[0]; // 4MSB are in buffer[1] so we shift and mask, 8LSB are in buffer[0]
133
    *voltage = (((float) value) * 6 * 2.1f)/4095.0f;
134

    
135
    return status;
136
}
137

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

    
151
    apalExitStatus_t status = p9221r_lld_read_register(p9221r, P9221R_LLD_REGISTER_IOUT_CURRENT_LSB, buffer, 2, timeout);
152
    value = (buffer[1] << 8) | buffer[0]; // 8MSB are in buffer[1] so we shift, 8LSB are in buffer[0]
153
    *current = (((float) value) * 2 * 2.1f)/4095.0f;
154

    
155
    return status;
156
}
157

    
158
/** @} */
159