Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_mpr121.c @ 616f3584

History | View | Annotate | Download (7.235 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_mpr121.h>
20

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

    
23
#include <string.h>
24

    
25
/**
26
 * @brief Read one or more of the internal registers of the mpr121.
27
 * @param[in]   mprd    MPR121 Driver
28
 * @param[in]   regaddr Address of the register
29
 * @param[in]   offset  Offset to be added to the register address
30
 * @param[in]   size    Size of the data to be read
31
 * @param[out]  data    The data that has been Read
32
 * @param[in]   timeout Timeout for the I2C Bus
33
 *
34
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
35
 */
36
inline apalExitStatus_t
37
mpr121_lld_read_register(const MPR121Driver* const mprd, const mpr121_lld_register_t regaddr, const uint8_t offset, const uint8_t size, uint8_t* const data, const apalTime_t timeout)
38
{
39
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
40
  apalDbgAssert(data != NULL);
41

    
42
  uint8_t addr = regaddr + offset;
43
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, &addr, 1, data, size, timeout);
44
}
45

    
46
/**
47
 * @brief Write to one or more of the internal registers of the mpr121.
48
 * @param[in]   mprd    MPR121 Driver
49
 * @param[in]   regaddr Address of the register
50
 * @param[in]   offset  Offset to be added to the register address
51
 * @param[in]   size    Size of the data to be read
52
 * @param[in]   data    The data that will be written
53
 * @param[in]   timeout Timeout for the I2C Bus
54
 *
55
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
56
 */
57
inline apalExitStatus_t
58
mpr121_lld_write_register(const MPR121Driver* const mprd, const mpr121_lld_register_t regaddr, const uint8_t offset, const uint8_t size, const uint8_t* const data, const apalTime_t timeout)
59
{
60
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
61
  apalDbgAssert(data != NULL);
62

    
63
  uint8_t buffer[size+1];
64
  buffer[0] = regaddr + offset;
65
  memcpy(buffer+1, data, size);
66
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, buffer, size+1, NULL, 0, timeout);
67
}
68

    
69
/**
70
 * @brief Perform a soft reset of the mpr121.
71
 * @param[in]   mprd    MPR121 Driver
72
 * @param[in]   timeout Timeout for the I2C Bus
73
 *
74
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
75
 */
76
inline apalExitStatus_t
77
mpr121_lld_soft_reset(const MPR121Driver* const mprd, const  apalTime_t timeout)
78
{
79
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
80

    
81
  uint8_t buffer[2] = {MPR121_LLD_REGISTER_SOFT_RESET, MPR121_LLD_I2C_SOFTRESET};
82
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, buffer, 2, NULL, 0, timeout);
83
}
84

    
85
/**
86
 * @brief Read the filtered data of the mpr121 sensors.
87
 * @param[in]   mprd    MPR121 Driver
88
 * @param[in]   index   The index of the sensor from which reading should be started
89
 * @param[in]   num     Number of consecutive sensors to read
90
 * @param[in]   data    The data that has been read
91
 * @param[in]   timeout Timeout for the I2C Bus
92
 *
93
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
94
 */
95
inline apalExitStatus_t
96
mpr121_lld_read_filtered_data(const MPR121Driver* const mprd, const uint8_t index, const uint8_t num, uint16_t* const data, const apalTime_t timeout)
97
{
98
  apalDbgAssert(data != NULL);
99

    
100
  uint8_t buffer[2*num];
101
  apalExitStatus_t status = mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_FILTERED_DATA, index*2, num*2, buffer, timeout);
102
  for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) {
103
    data[dataIdx] = (((uint16_t)buffer[2*dataIdx+1]) << 8) | buffer[dataIdx*2];
104
  }
105
  return status;
106
}
107

    
108
/**
109
 * @brief Read the baseline data of the mpr121 sensors.
110
 * @param[in]   mprd    MPR121 Driver
111
 * @param[in]   index   The index of the sensor from which reading should be started
112
 * @param[in]   num     Number of consecutive sensors to read
113
 * @param[in]   data    The data that has been read
114
 * @param[in]   timeout Timeout for the I2C Bus
115
 *
116
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
117
 */
118
inline apalExitStatus_t
119
mpr121_lld_read_baseline_data(const MPR121Driver* const mprd, const uint8_t index, const uint8_t num, uint8_t* const data, const apalTime_t timeout)
120
{
121
  return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_BASELINE, index, num, data, timeout);
122
}
123

    
124
/**
125
 * @brief Read the electrode data of the mpr121 sensors.
126
 * @param[in]   mprd    MPR121 Driver
127
 * @param[in]   index   The index of the sensor from which reading should be started
128
 * @param[in]   num     Number of consecutive sensors to read
129
 * @param[in]   data    The data that has been read
130
 * @param[in]   timeout Timeout for the I2C Bus
131
 *
132
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
133
 */
134
inline apalExitStatus_t
135
mpr121_lld_read_electrode_data(const MPR121Driver* const mprd, const uint8_t index, const uint8_t num, uint8_t* const data, const apalTime_t timeout)
136
{
137
  return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_ELE_CURRENT, index, num, data, timeout);
138
}
139

    
140
/**
141
 * @brief Write a configuration to the mpr121.
142
 * @param[in]   mprd    MPR121 Driver
143
 * @param[in]   cfg     The configuration to be written
144
 * @param[in]   timeout Timeout for the I2C Bus
145
 *
146
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
147
 */
148
inline apalExitStatus_t
149
mpr121_lld_write_config(const MPR121Driver* const mprd, const mpr121_lld_config_t cfg, const apalTime_t timeout)
150
{
151
  const apalExitStatus_t status = mpr121_lld_write_register(mprd, MPR121_LLD_REGISTER_AUTOCFG_CTRL_0, 0, 5, cfg.values, timeout);
152
  if (status != APAL_STATUS_OK) {
153
    return status;
154
  } else {
155
    return mpr121_lld_write_register(mprd, MPR121_LLD_REGISTER_CONFIG_1, 0, 3, &(cfg.values[5]), timeout);
156
  }
157
}
158

    
159
/**
160
 * @brief Read a configuration from the mpr121.
161
 * @param[in]   mprd    MPR121 Driver
162
 * @param[in]   cfg     The configuration to be written
163
 * @param[in]   timeout Timeout for the I2C Bus
164
 *
165
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
166
 */
167
inline apalExitStatus_t
168
mpr121_lld_read_config(const MPR121Driver* const mprd, mpr121_lld_config_t* const cfg, const apalTime_t timeout)
169
{
170
  const apalExitStatus_t status = mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_AUTOCFG_CTRL_0, 0, 5, cfg->values, timeout);
171
  if (status != APAL_STATUS_OK) {
172
    return status;
173
  } else {
174
    return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_CONFIG_1, 0, 3, &(cfg->values[5]), timeout);
175
  }
176
}
177

    
178
#endif /* defined(AMIROLLD_CFG_USE_MPR121) */