amiro-lld / include / VL53L0X / v1 / Api_vl53l0x / platform / src / vl53l0x_i2c_platform.c @ 6ebebd4d
History | View | Annotate | Download (7.92 KB)
1 | 6ebebd4d | Andre Raming | |
---|---|---|---|
2 | |||
3 | /*!
|
||
4 | * \file VL53L0X_platform.c
|
||
5 | * \brief Code function defintions for Doppler Testchip Platform Layer
|
||
6 | *
|
||
7 | */
|
||
8 | |||
9 | |||
10 | #include <stdio.h> // sprintf(), vsnprintf(), printf() |
||
11 | |||
12 | #ifdef _MSC_VER
|
||
13 | #define snprintf _snprintf
|
||
14 | #endif
|
||
15 | |||
16 | |||
17 | |||
18 | #include "vl53l0x_i2c_platform.h" |
||
19 | #include "vl53l0x_def.h" |
||
20 | |||
21 | #include <stdio.h> |
||
22 | #include <stdlib.h> |
||
23 | #include <time.h> |
||
24 | //#include "SERIAL_COMMS.h"
|
||
25 | //#include "comms_platform.h"
|
||
26 | |||
27 | #include "vl53l0x_platform_log.h" |
||
28 | |||
29 | //#include <aos_thread.h>
|
||
30 | |||
31 | |||
32 | |||
33 | #ifdef VL53L0X_LOG_ENABLE
|
||
34 | #define trace_print(level, ...) trace_print_module_function(TRACE_MODULE_PLATFORM, level, TRACE_FUNCTION_NONE, ##__VA_ARGS__) |
||
35 | #define trace_i2c(...) trace_print_module_function(TRACE_MODULE_NONE, TRACE_LEVEL_NONE, TRACE_FUNCTION_I2C, ##__VA_ARGS__) |
||
36 | #endif
|
||
37 | |||
38 | //char debug_string[VL53L0X_MAX_STRING_LENGTH_PLT];
|
||
39 | |||
40 | uint8_t cached_page = 0;
|
||
41 | |||
42 | #define MIN_COMMS_VERSION_MAJOR 1 |
||
43 | #define MIN_COMMS_VERSION_MINOR 8 |
||
44 | #define MIN_COMMS_VERSION_BUILD 1 |
||
45 | #define MIN_COMMS_VERSION_REVISION 0 |
||
46 | |||
47 | |||
48 | #define MAX_STR_SIZE 255 |
||
49 | #define MAX_MSG_SIZE 100 |
||
50 | #define MAX_DEVICES 4 |
||
51 | #define STATUS_OK 0x00 |
||
52 | #define STATUS_FAIL 0x01 |
||
53 | |||
54 | |||
55 | static unsigned char _dataBytes[MAX_MSG_SIZE]; |
||
56 | |||
57 | bool_t _check_min_version(void)
|
||
58 | { |
||
59 | return TRUE;
|
||
60 | } |
||
61 | |||
62 | |||
63 | uint8_t txbuf[10];
|
||
64 | |||
65 | |||
66 | /**
|
||
67 | * @brief VL53L0X_write_multi
|
||
68 | * @param address
|
||
69 | * @param reg register to write to
|
||
70 | * @param pdata
|
||
71 | * @param count
|
||
72 | * @return int32_t because the vl53l0x has more error messages than the chibiOS
|
||
73 | */
|
||
74 | int32_t VL53L0X_write_multi(apalI2CDriver_t* i2cd, uint8_t address, uint8_t reg, uint8_t *pdata, int32_t count, apalTime_t timeout) |
||
75 | { |
||
76 | |||
77 | |||
78 | int32_t status = APAL_STATUS_OK; |
||
79 | #ifdef VL53L0X_LOG_ENABLE
|
||
80 | int32_t i = 0;
|
||
81 | char value_as_str[VL53L0X_MAX_STRING_LENGTH_PLT];
|
||
82 | char *pvalue_as_str;
|
||
83 | #endif
|
||
84 | |||
85 | |||
86 | /* For multi writes, the serial comms dll requires multiples 4 bytes or
|
||
87 | * anything less than 4 bytes. So if an irregular size is required, the
|
||
88 | * message is broken up into two writes.
|
||
89 | *
|
||
90 | *
|
||
91 | * with the apalI2CMasterTransmit it is possible to write ore than one think -> TODO!
|
||
92 | */
|
||
93 | |||
94 | #ifdef VL53L0X_LOG_ENABLE
|
||
95 | |||
96 | pvalue_as_str = value_as_str; |
||
97 | |||
98 | for(i = 0 ; i < count ; i++) |
||
99 | { |
||
100 | sprintf(pvalue_as_str,"%02X", *(pdata+i));
|
||
101 | |||
102 | pvalue_as_str += 2;
|
||
103 | } |
||
104 | trace_i2c("Write reg : 0x%04X, Val : 0x%s\n", reg, value_as_str);
|
||
105 | #endif
|
||
106 | |||
107 | |||
108 | if(status == APAL_STATUS_OK)
|
||
109 | { |
||
110 | //max is 6 + 1 and allcoate the max space of 7 ... TODO
|
||
111 | int i = 0; |
||
112 | txbuf[0] = reg;
|
||
113 | for (i = 0; i < (int)count; i++){ |
||
114 | txbuf[i+1] = pdata[i];
|
||
115 | } |
||
116 | |||
117 | if (timeout == 0){ |
||
118 | status = apalI2CMasterTransmit(i2cd, (apalI2Caddr_t)address, txbuf, count+1, NULL, 0, TIME_INFINITE); |
||
119 | }else{
|
||
120 | status = apalI2CMasterTransmit(i2cd, (apalI2Caddr_t)address, txbuf, count+1, NULL, 0, timeout); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | //convet error status to vl53l0x-api
|
||
125 | if (status != APAL_STATUS_OK)
|
||
126 | { |
||
127 | status = VL53L0X_ERROR_NONE; |
||
128 | //SERIAL_COMMS_Get_Error_Text(debug_string);
|
||
129 | apalDbgAssert(status != STATUS_OK); |
||
130 | } |
||
131 | |||
132 | return status;
|
||
133 | } |
||
134 | |||
135 | |||
136 | |||
137 | |||
138 | /**
|
||
139 | * @brief VL53L0X_read_multi
|
||
140 | * @param address
|
||
141 | * @param index address from register
|
||
142 | * @param pdata
|
||
143 | * @param count likly 6
|
||
144 | * @return
|
||
145 | */
|
||
146 | int32_t VL53L0X_read_multi(apalI2CDriver_t* i2cd, uint8_t address, uint8_t index, uint8_t* pdata, size_t count, apalTime_t timeout) |
||
147 | { |
||
148 | |||
149 | apalDbgAssert(i2cd != NULL);
|
||
150 | |||
151 | int32_t status = STATUS_OK; |
||
152 | // #ifdef VL53L0X_LOG_ENABLE
|
||
153 | // int32_t i = 0;
|
||
154 | // char value_as_str[VL53L0X_MAX_STRING_LENGTH_PLT];
|
||
155 | // char *pvalue_as_str;
|
||
156 | // #endif
|
||
157 | |||
158 | if(status == STATUS_OK)
|
||
159 | { |
||
160 | |||
161 | // status = apalI2CMasterTransmit(i2cd, (apalI2Caddr_t)address, (uint8_t*)&index, 1, pdata, count, timeout);
|
||
162 | if (timeout == 0){ |
||
163 | status = apalI2CMasterTransmit(i2cd, (apalI2Caddr_t)address, (uint8_t*)&index, 1, pdata, count, TIME_INFINITE);
|
||
164 | }else{
|
||
165 | status = apalI2CMasterTransmit(i2cd, (apalI2Caddr_t)address, (uint8_t*)&index, 1, pdata, count, timeout);
|
||
166 | } |
||
167 | |||
168 | } |
||
169 | |||
170 | |||
171 | // #ifdef VL53L0X_LOG_ENABLE
|
||
172 | |||
173 | // // Build value as string;
|
||
174 | // pvalue_as_str = value_as_str;
|
||
175 | |||
176 | // for(i = 0 ; i < count ; i++)
|
||
177 | // {
|
||
178 | // sprintf(pvalue_as_str, "%02X", *(pdata+i));
|
||
179 | // pvalue_as_str += 2;
|
||
180 | // }
|
||
181 | |||
182 | // //trace_i2c("Read reg : 0x%04X, Val : 0x%s\n", index, value_as_str);
|
||
183 | // #endif
|
||
184 | |||
185 | |||
186 | return status;
|
||
187 | } |
||
188 | |||
189 | |||
190 | |||
191 | /**
|
||
192 | * @brief VL53L0X_write_byte
|
||
193 | * @param address
|
||
194 | * @param index
|
||
195 | * @param data
|
||
196 | * @return
|
||
197 | */
|
||
198 | int32_t VL53L0X_write_byte(apalI2CDriver_t* i2cd, uint8_t address, uint8_t index, uint8_t data, apalTime_t timeout) |
||
199 | { |
||
200 | |||
201 | int32_t status = STATUS_OK; |
||
202 | const size_t cbyte_count = 1; |
||
203 | |||
204 | #ifdef VL53L0X_LOG_ENABLE
|
||
205 | trace_print(TRACE_LEVEL_INFO,"Write reg : 0x%02X, Val : 0x%02X\n", index, data);
|
||
206 | #endif
|
||
207 | |||
208 | |||
209 | status = VL53L0X_write_multi(i2cd, address, index, (uint8_t*)&data, cbyte_count, timeout); |
||
210 | |||
211 | |||
212 | |||
213 | return status;
|
||
214 | |||
215 | } |
||
216 | |||
217 | |||
218 | /**
|
||
219 | * @brief VL53L0X_write_word
|
||
220 | * @param address
|
||
221 | * @param index
|
||
222 | * @param data
|
||
223 | * @return
|
||
224 | */
|
||
225 | int32_t VL53L0X_write_word(apalI2CDriver_t* i2cd, uint8_t address, uint8_t index, uint16_t data, apalTime_t timeout) |
||
226 | { |
||
227 | int32_t status = STATUS_OK; |
||
228 | |||
229 | uint8_t buffer[BYTES_PER_WORD]; |
||
230 | |||
231 | // Split 16-bit word into MS and LS uint8_t
|
||
232 | buffer[0] = (uint8_t)(data >> 8); |
||
233 | buffer[1] = (uint8_t)(data & 0x00FF); |
||
234 | |||
235 | if(index%2 == 1) |
||
236 | { |
||
237 | status = VL53L0X_write_multi(i2cd, address, index, (uint8_t*)&buffer[0],1, timeout); |
||
238 | status = VL53L0X_write_multi(i2cd, address, index + 1, (uint8_t*) &buffer[1],1, timeout); |
||
239 | // serial comms cannot handle word writes to non 2-byte aligned registers.
|
||
240 | } |
||
241 | else
|
||
242 | { |
||
243 | status = VL53L0X_write_multi(i2cd, address, index, (uint8_t*)&buffer[0], BYTES_PER_WORD, timeout);
|
||
244 | } |
||
245 | |||
246 | return status;
|
||
247 | |||
248 | } |
||
249 | |||
250 | |||
251 | |||
252 | |||
253 | |||
254 | /**
|
||
255 | * @brief VL53L0X_write_dword
|
||
256 | * @param address
|
||
257 | * @param index
|
||
258 | * @param data
|
||
259 | * @return
|
||
260 | */
|
||
261 | int32_t VL53L0X_write_dword(apalI2CDriver_t* i2cd, uint8_t address, uint8_t index, uint32_t data, apalTime_t timeout) |
||
262 | { |
||
263 | int32_t status = STATUS_OK; |
||
264 | uint8_t buffer[BYTES_PER_DWORD]; |
||
265 | |||
266 | // Split 32-bit word into MS ... LS bytes
|
||
267 | buffer[0] = (uint8_t) (data >> 24); |
||
268 | buffer[1] = (uint8_t)((data & 0x00FF0000) >> 16); |
||
269 | buffer[2] = (uint8_t)((data & 0x0000FF00) >> 8); |
||
270 | buffer[3] = (uint8_t) (data & 0x000000FF); |
||
271 | |||
272 | status = VL53L0X_write_multi(i2cd, address, index, (uint8_t*)buffer[0], BYTES_PER_DWORD, timeout);
|
||
273 | |||
274 | return status;
|
||
275 | |||
276 | } |
||
277 | |||
278 | |||
279 | |||
280 | /**
|
||
281 | * @brief VL53L0X_read_byte
|
||
282 | * @param address
|
||
283 | * @param index
|
||
284 | * @param pdata
|
||
285 | * @return
|
||
286 | */
|
||
287 | int32_t VL53L0X_read_byte(apalI2CDriver_t* i2cd, uint8_t address, uint8_t index, uint8_t *pdata, apalTime_t timeout) |
||
288 | { |
||
289 | int32_t status = STATUS_OK; |
||
290 | size_t cbyte_count = 1;
|
||
291 | |||
292 | status = VL53L0X_read_multi(i2cd, address, index, pdata, cbyte_count, timeout); |
||
293 | |||
294 | #ifdef VL53L0X_LOG_ENABLE
|
||
295 | trace_print(TRACE_LEVEL_INFO,"Read reg : 0x%02X, Val : 0x%02X\n", index, *pdata);
|
||
296 | #endif
|
||
297 | |||
298 | return status;
|
||
299 | |||
300 | } |
||
301 | |||
302 | |||
303 | |||
304 | |||
305 | /**
|
||
306 | * @brief VL53L0X_read_word
|
||
307 | * @param address
|
||
308 | * @param index
|
||
309 | * @param pdata
|
||
310 | * @return
|
||
311 | */
|
||
312 | int32_t VL53L0X_read_word(apalI2CDriver_t* i2cd, uint8_t address, uint8_t index, uint16_t *pdata ,apalTime_t timeout) |
||
313 | { |
||
314 | int32_t status = STATUS_OK; |
||
315 | uint8_t buffer[BYTES_PER_WORD]; |
||
316 | |||
317 | status = VL53L0X_read_multi(i2cd, address, index, buffer, BYTES_PER_WORD, timeout); |
||
318 | *pdata = ((uint16_t)buffer[0]<<8) + (uint16_t)buffer[1]; |
||
319 | |||
320 | return status;
|
||
321 | |||
322 | } |
||
323 | |||
324 | |||
325 | |||
326 | |||
327 | /**
|
||
328 | * @brief VL53L0X_read_dword
|
||
329 | * @param address
|
||
330 | * @param index
|
||
331 | * @param pdata
|
||
332 | * @return
|
||
333 | */
|
||
334 | int32_t VL53L0X_read_dword(apalI2CDriver_t* i2cd, uint8_t address, uint8_t index, uint32_t *pdata, apalTime_t timeout) |
||
335 | { |
||
336 | int32_t status = STATUS_OK; |
||
337 | uint8_t buffer[BYTES_PER_DWORD]; |
||
338 | |||
339 | status = VL53L0X_read_multi(i2cd, address, index, buffer, BYTES_PER_DWORD, timeout); |
||
340 | *pdata = ((uint32_t)buffer[0]<<24) + ((uint32_t)buffer[1]<<16) + ((uint32_t)buffer[2]<<8) + (uint32_t)buffer[3]; |
||
341 | |||
342 | return status;
|
||
343 | |||
344 | } |
||
345 | |||
346 | |||
347 | |||
348 | |||
349 | |||
350 | |||
351 | |||
352 |