Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / MPR121 / v1 / alld_MPR121.c @ 3ed0cc4d

History | View | Annotate | Download (8.633 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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
#include <string.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 one or more of the internal registers of the mpr121.
57
 * @param[in]   mprd    MPR121 Driver
58
 * @param[in]   regaddr Address of the register
59
 * @param[in]   offset  Offset to be added to the register address
60
 * @param[in]   size    Size of the data to be read
61
 * @param[out]  data    The data that has been Read
62
 * @param[in]   timeout Timeout for the I2C Bus
63
 *
64
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
65
 */
66
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)
67
{
68
  apalDbgAssert(mprd != NULL && mprd->i2cd != NULL);
69
  apalDbgAssert(data != NULL);
70

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

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

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

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

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

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

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

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

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

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

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

    
200
/** @} */
201