Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_mpr121.c @ 8c47f14b

History | View | Annotate | Download (7.36 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_mpr121.c
21
 * @brief   Touch Sensor function implementations.
22
 *
23
 * @addtogroup lld_touch
24
 * @{
25
 */
26

    
27
#include <alld_mpr121.h>
28

    
29
#if defined(AMIROLLD_CFG_USE_MPR121) || defined(__DOXYGEN__)
30

    
31
#include <string.h>
32

    
33
/**
34
 * @brief Read one or more of the internal registers of the mpr121.
35
 * @param[in]   mprd    MPR121 Driver
36
 * @param[in]   regaddr Address of the register
37
 * @param[in]   offset  Offset to be added to the register address
38
 * @param[in]   size    Size of the data to be read
39
 * @param[out]  data    The data that has been Read
40
 * @param[in]   timeout Timeout for the I2C Bus
41
 *
42
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
43
 */
44
inline apalExitStatus_t
45
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)
46
{
47
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
48
  apalDbgAssert(data != NULL);
49

    
50
  uint8_t addr = regaddr + offset;
51
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, &addr, 1, data, size, timeout);
52
}
53

    
54
/**
55
 * @brief Write to one or more of the internal registers of the mpr121.
56
 * @param[in]   mprd    MPR121 Driver
57
 * @param[in]   regaddr Address of the register
58
 * @param[in]   offset  Offset to be added to the register address
59
 * @param[in]   size    Size of the data to be read
60
 * @param[in]   data    The data that will be written
61
 * @param[in]   timeout Timeout for the I2C Bus
62
 *
63
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
64
 */
65
inline apalExitStatus_t
66
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)
67
{
68
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
69
  apalDbgAssert(data != NULL);
70

    
71
  uint8_t buffer[size+1];
72
  buffer[0] = regaddr + offset;
73
  memcpy(buffer+1, data, size);
74
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, buffer, size+1, NULL, 0, timeout);
75
}
76

    
77
/**
78
 * @brief Perform a soft reset of the mpr121.
79
 * @param[in]   mprd    MPR121 Driver
80
 * @param[in]   timeout Timeout for the I2C Bus
81
 *
82
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
83
 */
84
inline apalExitStatus_t
85
mpr121_lld_soft_reset(const MPR121Driver* const mprd, const  apalTime_t timeout)
86
{
87
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
88

    
89
  uint8_t buffer[2] = {MPR121_LLD_REGISTER_SOFT_RESET, MPR121_LLD_I2C_SOFTRESET};
90
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, buffer, 2, NULL, 0, timeout);
91
}
92

    
93
/**
94
 * @brief Read the filtered data of the mpr121 sensors.
95
 * @param[in]   mprd    MPR121 Driver
96
 * @param[in]   index   The index of the sensor from which reading should be started
97
 * @param[in]   num     Number of consecutive sensors to read
98
 * @param[in]   data    The data that has been read
99
 * @param[in]   timeout Timeout for the I2C Bus
100
 *
101
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
102
 */
103
inline apalExitStatus_t
104
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)
105
{
106
  apalDbgAssert(data != NULL);
107

    
108
  uint8_t buffer[2*num];
109
  apalExitStatus_t status = mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_FILTERED_DATA, index*2, num*2, buffer, timeout);
110
  for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) {
111
    data[dataIdx] = (((uint16_t)buffer[2*dataIdx+1]) << 8) | buffer[dataIdx*2];
112
  }
113
  return status;
114
}
115

    
116
/**
117
 * @brief Read the baseline data of the mpr121 sensors.
118
 * @param[in]   mprd    MPR121 Driver
119
 * @param[in]   index   The index of the sensor from which reading should be started
120
 * @param[in]   num     Number of consecutive sensors to read
121
 * @param[in]   data    The data that has been read
122
 * @param[in]   timeout Timeout for the I2C Bus
123
 *
124
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
125
 */
126
inline apalExitStatus_t
127
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)
128
{
129
  return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_BASELINE, index, num, data, timeout);
130
}
131

    
132
/**
133
 * @brief Read the electrode data of the mpr121 sensors.
134
 * @param[in]   mprd    MPR121 Driver
135
 * @param[in]   index   The index of the sensor from which reading should be started
136
 * @param[in]   num     Number of consecutive sensors to read
137
 * @param[in]   data    The data that has been read
138
 * @param[in]   timeout Timeout for the I2C Bus
139
 *
140
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
141
 */
142
inline apalExitStatus_t
143
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)
144
{
145
  return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_ELE_CURRENT, index, num, data, timeout);
146
}
147

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

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

    
186
#endif /* defined(AMIROLLD_CFG_USE_MPR121) */
187

    
188
/** @} */