amiro-lld / source / AT42QT1050 / v1 / alld_AT42QT1050_v1.c @ 7394be9e
History | View | Annotate | Download (9.288 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_AT42QT1050_v1.c
|
21 |
* @brief Touch sensor function implementations.
|
22 |
*
|
23 |
* @addtogroup lld_touch
|
24 |
* @{
|
25 |
*/
|
26 |
|
27 |
#include <alld_AT42QT1050.h> |
28 |
|
29 |
#if (defined(AMIROLLD_CFG_AT42QT1050) && (AMIROLLD_CFG_AT42QT1050 == 1)) || defined(__DOXYGEN__) |
30 |
|
31 |
#include <math.h> |
32 |
|
33 |
/******************************************************************************/
|
34 |
/* LOCAL DEFINITIONS */
|
35 |
/******************************************************************************/
|
36 |
#define AT42QT1050_LLD_WATCHDOGTIME_MAX 125000 |
37 |
#define AT42QT1050_LLD_INITIALIZATION_TIME_MAX 30000 |
38 |
|
39 |
/******************************************************************************/
|
40 |
/* EXPORTED VARIABLES */
|
41 |
/******************************************************************************/
|
42 |
|
43 |
/******************************************************************************/
|
44 |
/* LOCAL TYPES */
|
45 |
/******************************************************************************/
|
46 |
|
47 |
/******************************************************************************/
|
48 |
/* LOCAL VARIABLES */
|
49 |
/******************************************************************************/
|
50 |
|
51 |
/******************************************************************************/
|
52 |
/* LOCAL FUNCTIONS */
|
53 |
/******************************************************************************/
|
54 |
|
55 |
/******************************************************************************/
|
56 |
/* EXPORTED FUNCTIONS */
|
57 |
/******************************************************************************/
|
58 |
|
59 |
/**
|
60 |
* @brief Read 8bit data from any register.
|
61 |
*
|
62 |
* @param[in] at42qt1050d The AT42QT1050 driver to use.
|
63 |
* @param[in] reg Register address to read from.
|
64 |
* @param[out] data Pointer to store the register data to.
|
65 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
66 |
*
|
67 |
* @return Indicator whether the function call was successful or a timeout occurred.
|
68 |
*/
|
69 |
apalExitStatus_t at42qt1050_lld_read_reg(const AT42QT1050Driver* at42qt1050d, const at42qt1050_lld_register_t reg, uint8_t* const data, const apalTime_t timeout) |
70 |
{ |
71 |
apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL); |
72 |
apalDbgAssert(data != NULL);
|
73 |
|
74 |
const uint8_t txbuf = (uint8_t)reg;
|
75 |
return apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, &txbuf, 1, data, 1, timeout); |
76 |
} |
77 |
|
78 |
/**
|
79 |
* @brief Write 8bit data to any (writable) register.
|
80 |
*
|
81 |
* @param[in] at42qt1050d The AT42QT1050 driver to use.
|
82 |
* @param[in] reg Register address to write to.
|
83 |
* @param[in] data Data to transmit.
|
84 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
85 |
*
|
86 |
* @return Indicator whether the function call was successful or a timeout occurred.
|
87 |
*/
|
88 |
apalExitStatus_t at42qt1050_lld_write_reg(const AT42QT1050Driver* at42qt1050d, const at42qt1050_lld_register_t reg, const uint8_t data, const apalTime_t timeout) |
89 |
{ |
90 |
apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL); |
91 |
|
92 |
const uint8_t txbuf[2] = { (uint8_t)reg, data }; |
93 |
return apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, txbuf, 2, NULL, 0, timeout); |
94 |
} |
95 |
|
96 |
/**
|
97 |
* @brief Read signal information of a key.
|
98 |
*
|
99 |
* @param[in] at42qt1050d The AT42QT1050 driver to use.
|
100 |
* @param[in] key Key to read the signal information of.
|
101 |
* @param[out] signal Pointer to store the data to.
|
102 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
103 |
*
|
104 |
* @return Indicator whether the function call was successful or a timeout occurred.
|
105 |
*/
|
106 |
apalExitStatus_t at42qt1050_lld_read_keyssignal(const AT42QT1050Driver* at42qt1050d, const uint8_t key, uint16_t* signal, const apalTime_t timeout) |
107 |
{ |
108 |
apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL); |
109 |
apalDbgAssert(key < AT42QT1050_LLD_NUM_KEYS); |
110 |
apalDbgAssert(signal != NULL);
|
111 |
|
112 |
const at42qt1050_lld_register_t txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_KEYSIGNAL_0, key);
|
113 |
uint8_t rxbuf[2];
|
114 |
const apalExitStatus_t status = apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, &txbuf, 1, rxbuf, 2, timeout); |
115 |
*signal = (rxbuf[0] << 8) | rxbuf[1]; |
116 |
return status;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* @brief Read reference data of a key.
|
121 |
*
|
122 |
* @param[in] at42qt1050d The AT42QT1050 driver to use.
|
123 |
* @param[in] key Key to read the signal information of.
|
124 |
* @param[out] refdata Pointer to store the data to.
|
125 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
126 |
*
|
127 |
* @return Indicator whether the function call was successful or a timeout occurred.
|
128 |
*/
|
129 |
apalExitStatus_t at42qt1050_lld_read_referencedata(const AT42QT1050Driver* at42qt1050d, const uint8_t key, uint16_t* refdata, const apalTime_t timeout) |
130 |
{ |
131 |
apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL); |
132 |
apalDbgAssert(key < AT42QT1050_LLD_NUM_KEYS); |
133 |
apalDbgAssert(refdata != NULL);
|
134 |
|
135 |
const at42qt1050_lld_register_t txbuf = at42qt1050_lld_addr_calc(AT42QT1050_LLD_REG_REFERENCEDATA_0, key);
|
136 |
uint8_t rxbuf[2];
|
137 |
const apalExitStatus_t status = apalI2CMasterTransmit(at42qt1050d->i2cd, at42qt1050d->addr, &txbuf, 1, rxbuf, 2, timeout); |
138 |
*refdata = (rxbuf[0] << 8) | rxbuf[1]; |
139 |
return status;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* @brief Soft Reset of the device
|
144 |
*
|
145 |
* @param[in] at42qt1050d The AT42QT1050 driver to use.
|
146 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
147 |
* @param[in] wait4wakeup Wait for device wakeup (timeout must be > 155 ms)
|
148 |
*
|
149 |
* @return Indicator whether the function call was successful or a timeout occurred.
|
150 |
*/
|
151 |
inline apalExitStatus_t at42qt1050_lld_reset_safe(const AT42QT1050Driver* at42qt1050d, const bool wait4wakeup, const apalTime_t timeout) { |
152 |
if(wait4wakeup)
|
153 |
apalDbgAssert(timeout >= AT42QT1050_LLD_WATCHDOGTIME_MAX+AT42QT1050_LLD_INITIALIZATION_TIME_MAX); |
154 |
|
155 |
return at42qt1050_lld_reset(at42qt1050d, timeout-(AT42QT1050_LLD_WATCHDOGTIME_MAX+AT42QT1050_LLD_INITIALIZATION_TIME_MAX), wait4wakeup);
|
156 |
} |
157 |
|
158 |
/**
|
159 |
* @brief Soft Reset of the device
|
160 |
*
|
161 |
* @param[in] at42qt1050d The AT42QT1050 driver to use.
|
162 |
* @param[in] timeout Timeout for the i2c call (in microseconds).
|
163 |
* @param[in] wait4wakeup Wait for device wakeup (155 ms)
|
164 |
*
|
165 |
* @return Indicator whether the function call was successful or a timeout occurred.
|
166 |
*/
|
167 |
inline apalExitStatus_t at42qt1050_lld_reset(const AT42QT1050Driver* at42qt1050d, const apalTime_t timeout, const bool wait4wakeup) { |
168 |
apalDbgAssert(at42qt1050d != NULL && at42qt1050d->i2cd != NULL); |
169 |
|
170 |
const apalExitStatus_t status = at42qt1050_lld_write_reg(
|
171 |
at42qt1050d, AT42QT1050_LLD_REG_RESET_CALIBRATE, AT42QT1050_LLD_RESETCALIBRATE_RESET, timeout); |
172 |
if(wait4wakeup)
|
173 |
usleep(AT42QT1050_LLD_WATCHDOGTIME_MAX+AT42QT1050_LLD_INITIALIZATION_TIME_MAX+timeout); // watchdog timer+initialization -> datasheet
|
174 |
return status;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* @brief Convert a 4 bit pulse value to the representing number of samples.
|
179 |
* @details Calculation: <#samples> = 2^(<pulse value>)
|
180 |
*
|
181 |
* @param[in] pulse Pulse value.
|
182 |
*
|
183 |
* @return Resulting sample count.
|
184 |
*/
|
185 |
uint16_t at42qt1050_lld_pulse2samples(const uint8_t pulse)
|
186 |
{ |
187 |
apalDbgAssert(pulse <= 0x0Fu);
|
188 |
|
189 |
return (1 << pulse); |
190 |
} |
191 |
|
192 |
/**
|
193 |
* @brief Convert a desired number of samples to the according (theoretical) pulse value.
|
194 |
* @details Calculation: <pulse value> = log2(<#samples>)
|
195 |
*
|
196 |
* @param[in] samples Desired number of samples.
|
197 |
*
|
198 |
* @return The (theoretical) value to set to the pulse register.
|
199 |
*/
|
200 |
float at42qt1050_lld_samples2pulse(const uint16_t samples) |
201 |
{ |
202 |
return log2f(samples);
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* @brief Convert a 4 bit scale value to the accoring scaling factor.
|
207 |
* @details Calculation: <scaling factor> = 2^(<scale value>)
|
208 |
*
|
209 |
* @param[in] scale Scale value.
|
210 |
*
|
211 |
* @return Resulting scaling factor.
|
212 |
*/
|
213 |
uint16_t at42qt1050_lld_scale2scaling(const uint8_t scale)
|
214 |
{ |
215 |
apalDbgAssert(scale <= 0x0Fu);
|
216 |
|
217 |
return (1 << scale); |
218 |
} |
219 |
|
220 |
/**
|
221 |
* @brief Convert a desired scaling factor to the according (theoretical) scale value.
|
222 |
* @details Calculation: <scale value> = log2(<scaling factor>
|
223 |
* )
|
224 |
* @param[in] factor Desired scaling factor.
|
225 |
*
|
226 |
* @return The (theoretcial) value to set to the scale register.
|
227 |
*/
|
228 |
float at42qt1050_lld_scaling2scale(const uint16_t factor) |
229 |
{ |
230 |
return log2f(factor);
|
231 |
} |
232 |
|
233 |
#endif /* defined(AMIROLLD_CFG_AT42QT1050) && (AMIROLLD_CFG_AT42QT1050 == 1) */ |
234 |
|
235 |
/** @} */
|