Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / MPR121 / v1 / alld_MPR121_v1.c @ 73345246

History | View | Annotate | Download (8.791 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_MPR121) && (AMIROLLD_CFG_MPR121 == 1)) || 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
apalExitStatus_t 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)
69
{
70
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
71
  apalDbgAssert(data != NULL);
72

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

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

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

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

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

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

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

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

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

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

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

    
202
#endif /* defined(AMIROLLD_CFG_MPR121) && (AMIROLLD_CFG_MPR121 == 1) */
203

    
204
/** @} */