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