Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_mpr121.c @ ef078306

History | View | Annotate | Download (8.793 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
/* LOCAL DEFINITIONS                                                          */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* EXPORTED VARIABLES                                                         */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL TYPES                                                                */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* LOCAL VARIABLES                                                            */
47
/******************************************************************************/
48

    
49
/******************************************************************************/
50
/* LOCAL FUNCTIONS                                                            */
51
/******************************************************************************/
52

    
53
/******************************************************************************/
54
/* EXPORTED FUNCTIONS                                                         */
55
/******************************************************************************/
56

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

    
74
  uint8_t addr = regaddr + offset;
75
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, &addr, 1, data, size, timeout);
76
}
77

    
78
/**
79
 * @brief Write to one or more of the internal registers of the mpr121.
80
 * @param[in]   mprd    MPR121 Driver
81
 * @param[in]   regaddr Address of the register
82
 * @param[in]   offset  Offset to be added to the register address
83
 * @param[in]   size    Size of the data to be read
84
 * @param[in]   data    The data that will be written
85
 * @param[in]   timeout Timeout for the I2C Bus
86
 *
87
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
88
 */
89
inline apalExitStatus_t
90
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)
91
{
92
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
93
  apalDbgAssert(data != NULL);
94

    
95
  uint8_t buffer[size+1];
96
  buffer[0] = regaddr + offset;
97
  memcpy(buffer+1, data, size);
98
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, buffer, size+1, NULL, 0, timeout);
99
}
100

    
101
/**
102
 * @brief Perform a soft reset of the mpr121.
103
 * @param[in]   mprd    MPR121 Driver
104
 * @param[in]   timeout Timeout for the I2C Bus
105
 *
106
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
107
 */
108
inline apalExitStatus_t
109
mpr121_lld_soft_reset(const MPR121Driver* const mprd, const  apalTime_t timeout)
110
{
111
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
112

    
113
  uint8_t buffer[2] = {MPR121_LLD_REGISTER_SOFT_RESET, MPR121_LLD_I2C_SOFTRESET};
114
  return apalI2CMasterTransmit(mprd->i2cd, MPR121_LLD_I2C_ADDR_FIXED, buffer, 2, NULL, 0, timeout);
115
}
116

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

    
132
  uint8_t buffer[2*num];
133
  apalExitStatus_t status = mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_FILTERED_DATA, index*2, num*2, buffer, timeout);
134
  for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) {
135
    data[dataIdx] = (((uint16_t)buffer[2*dataIdx+1]) << 8) | buffer[dataIdx*2];
136
  }
137
  return status;
138
}
139

    
140
/**
141
 * @brief Read the baseline data of the mpr121 sensors.
142
 * @param[in]   mprd    MPR121 Driver
143
 * @param[in]   index   The index of the sensor from which reading should be started
144
 * @param[in]   num     Number of consecutive sensors to read
145
 * @param[in]   data    The data that has been read
146
 * @param[in]   timeout Timeout for the I2C Bus
147
 *
148
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
149
 */
150
inline apalExitStatus_t
151
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)
152
{
153
  return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_BASELINE, index, num, data, timeout);
154
}
155

    
156
/**
157
 * @brief Read the electrode data of the mpr121 sensors.
158
 * @param[in]   mprd    MPR121 Driver
159
 * @param[in]   index   The index of the sensor from which reading should be started
160
 * @param[in]   num     Number of consecutive sensors to read
161
 * @param[in]   data    The data that has been read
162
 * @param[in]   timeout Timeout for the I2C Bus
163
 *
164
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
165
 */
166
inline apalExitStatus_t
167
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)
168
{
169
  return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_ELE_CURRENT, index, num, data, timeout);
170
}
171

    
172
/**
173
 * @brief Write a configuration to the mpr121.
174
 * @param[in]   mprd    MPR121 Driver
175
 * @param[in]   cfg     The configuration to be written
176
 * @param[in]   timeout Timeout for the I2C Bus
177
 *
178
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
179
 */
180
inline apalExitStatus_t
181
mpr121_lld_write_config(const MPR121Driver* const mprd, const mpr121_lld_config_t cfg, const apalTime_t timeout)
182
{
183
  const apalExitStatus_t status = mpr121_lld_write_register(mprd, MPR121_LLD_REGISTER_AUTOCFG_CTRL_0, 0, 5, cfg.values, timeout);
184
  if (status != APAL_STATUS_OK) {
185
    return status;
186
  } else {
187
    return mpr121_lld_write_register(mprd, MPR121_LLD_REGISTER_CONFIG_1, 0, 3, &(cfg.values[5]), timeout);
188
  }
189
}
190

    
191
/**
192
 * @brief Read a configuration from the mpr121.
193
 * @param[in]   mprd    MPR121 Driver
194
 * @param[in]   cfg     The configuration to be written
195
 * @param[in]   timeout Timeout for the I2C Bus
196
 *
197
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
198
 */
199
inline apalExitStatus_t
200
mpr121_lld_read_config(const MPR121Driver* const mprd, mpr121_lld_config_t* const cfg, const apalTime_t timeout)
201
{
202
  const apalExitStatus_t status = mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_AUTOCFG_CTRL_0, 0, 5, cfg->values, timeout);
203
  if (status != APAL_STATUS_OK) {
204
    return status;
205
  } else {
206
    return mpr121_lld_read_register(mprd, MPR121_LLD_REGISTER_CONFIG_1, 0, 3, &(cfg->values[5]), timeout);
207
  }
208
}
209

    
210
#endif /* defined(AMIROLLD_CFG_USE_MPR121) */
211

    
212
/** @} */