/
vl53l0x_platform.c
237 lines (200 loc) · 6.42 KB
/
vl53l0x_platform.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//?????#include "hal.h"
#include "vl53l0x_platform.h"
#include "vl53l0x_api.h"
#include "stm32f4xx_hal.h"
#include <string.h>
#define I2C_TIME_OUT_BASE 10
#define I2C_TIME_OUT_BYTE 1
#define VL53L0X_OsDelay(...) HAL_Delay(2)
#ifndef HAL_I2C_MODULE_ENABLED
#warning "HAL I2C module must be enable "
#endif
//extern I2C_HandleTypeDef hi2c1;
//#define VL53L0X_pI2cHandle (&hi2c1)
/* when not customized by application define dummy one */
#ifndef VL53L0X_GetI2cBus
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
*/
# define VL53L0X_GetI2cBus(...) (void)0
#endif
#ifndef VL53L0X_PutI2cBus
/** This macro can be overloaded by user to enforce i2c sharing in RTOS context
*/
# define VL53L0X_PutI2cBus(...) (void)0
#endif
#ifndef VL53L0X_OsDelay
# define VL53L0X_OsDelay(...) (void)0
#endif
uint8_t _I2CBuffer[64];
int _I2CWrite(VL53L0X_DEV Dev, uint8_t *pdata, uint32_t count) {
int status;
int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
status = HAL_I2C_Master_Transmit(Dev->I2cHandle, Dev->I2cDevAddr, pdata, count, i2c_time_out);
if (status) {
//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
}
return status;
}
int _I2CRead(VL53L0X_DEV Dev, uint8_t *pdata, uint32_t count) {
int status;
int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
status = HAL_I2C_Master_Receive(Dev->I2cHandle, Dev->I2cDevAddr|1, pdata, count, i2c_time_out);
if (status) {
//VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
//XNUCLEO6180XA1_I2C1_Init(&hi2c1);
}
return status;
}
// the ranging_sensor_comms.dll will take care of the page selection
VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) {
int status_int;
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
if (count > sizeof(_I2CBuffer) - 1) {
return VL53L0X_ERROR_INVALID_PARAMS;
}
_I2CBuffer[0] = index;
memcpy(&_I2CBuffer[1], pdata, count);
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, count + 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
// the ranging_sensor_comms.dll will take care of the page selection
VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, pdata, count);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index;
_I2CBuffer[1] = data;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, 2);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index;
_I2CBuffer[1] = data >> 8;
_I2CBuffer[2] = data & 0x00FF;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, 3);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
_I2CBuffer[0] = index;
_I2CBuffer[1] = (data >> 24) & 0xFF;
_I2CBuffer[2] = (data >> 16) & 0xFF;
_I2CBuffer[3] = (data >> 8) & 0xFF;
_I2CBuffer[4] = (data >> 0 ) & 0xFF;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, _I2CBuffer, 5);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
uint8_t data;
Status = VL53L0X_RdByte(Dev, index, &data);
if (Status) {
goto done;
}
data = (data & AndData) | OrData;
Status = VL53L0X_WrByte(Dev, index, data);
done:
return Status;
}
VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if( status_int ){
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, data, 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
}
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if( status_int ){
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, _I2CBuffer, 2);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
*data = ((uint16_t)_I2CBuffer[0]<<8) + (uint16_t)_I2CBuffer[1];
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data) {
VL53L0X_Error Status = VL53L0X_ERROR_NONE;
int32_t status_int;
VL53L0X_GetI2cBus();
status_int = _I2CWrite(Dev, &index, 1);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
status_int = _I2CRead(Dev, _I2CBuffer, 4);
if (status_int != 0) {
Status = VL53L0X_ERROR_CONTROL_INTERFACE;
goto done;
}
*data = ((uint32_t)_I2CBuffer[0]<<24) + ((uint32_t)_I2CBuffer[1]<<16) + ((uint32_t)_I2CBuffer[2]<<8) + (uint32_t)_I2CBuffer[3];
done:
VL53L0X_PutI2cBus();
return Status;
}
VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev) {
VL53L0X_Error status = VL53L0X_ERROR_NONE;
// do nothing
VL53L0X_OsDelay();
return status;
}
//end of file