Statistics
| Branch: | Tag: | Revision:

amiro-lld / include / VL53L0X / v1 / Api_vl53l0x / platform / src / vl53l0x_platform.c @ 6ebebd4d

History | View | Annotate | Download (8.998 KB)

1
/*******************************************************************************
2
Copyright � 2015, STMicroelectronics International N.V.
3
All rights reserved.
4

5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
7
    * Redistributions of source code must retain the above copyright
8
      notice, this list of conditions and the following disclaimer.
9
    * Redistributions in binary form must reproduce the above copyright
10
      notice, this list of conditions and the following disclaimer in the
11
      documentation and/or other materials provided with the distribution.
12
    * Neither the name of STMicroelectronics nor the
13
      names of its contributors may be used to endorse or promote products
14
      derived from this software without specific prior written permission.
15

16
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
19
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
20
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
21
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
********************************************************************************/
28

    
29
/**
30
 * @file VL53L0X_i2c.c
31
 *
32
 * Copyright (C) 2014 ST MicroElectronics
33
 *
34
 * provide variable word size byte/Word/dword VL6180x register access via i2c
35
 *
36
 */
37
#include "vl53l0x_platform.h"
38
//#include "vl53l0x_i2c_platform.h"
39
//#include "vl53l0x_api.h"
40

    
41
#define LOG_FUNCTION_START(fmt, ... )           _LOG_FUNCTION_START(TRACE_MODULE_PLATFORM, fmt, ##__VA_ARGS__)
42
#define LOG_FUNCTION_END(status, ... )          _LOG_FUNCTION_END(TRACE_MODULE_PLATFORM, status, ##__VA_ARGS__)
43
#define LOG_FUNCTION_END_FMT(status, fmt, ... ) _LOG_FUNCTION_END_FMT(TRACE_MODULE_PLATFORM, status, fmt, ##__VA_ARGS__)
44

    
45
/**
46
 * @def I2C_BUFFER_CONFIG
47
 *
48
 * @brief Configure Device register I2C access
49
 *
50
 * @li 0 : one GLOBAL buffer \n
51
 *   Use one global buffer of MAX_I2C_XFER_SIZE byte in data space \n
52
 *   This solution is not multi-Device compliant nor multi-thread cpu safe \n
53
 *   It can be the best option for small 8/16 bit MCU without stack and limited ram  (STM8s, 80C51 ...)
54
 *
55
 * @li 1 : ON_STACK/local \n
56
 *   Use local variable (on stack) buffer \n
57
 *   This solution is multi-thread with use of i2c resource lock or mutex see VL6180x_GetI2CAccess() \n
58
 *
59
 * @li 2 : User defined \n
60
 *    Per Device potentially dynamic allocated. Requires VL6180x_GetI2cBuffer() to be implemented.
61
 * @ingroup Configuration
62
 */
63
#define I2C_BUFFER_CONFIG 1
64
/** Maximum buffer size to be used in i2c */
65
#define VL53L0X_MAX_I2C_XFER_SIZE   64 /* Maximum buffer size to be used in i2c */
66

    
67
#if I2C_BUFFER_CONFIG == 0
68
    /* GLOBAL config buffer */
69
    uint8_t i2c_global_buffer[VL53L0X_MAX_I2C_XFER_SIZE];
70

    
71
    #define DECL_I2C_BUFFER
72
    #define VL53L0X_GetLocalBuffer(Dev, n_byte)  i2c_global_buffer
73

    
74
#elif I2C_BUFFER_CONFIG == 1
75
    /* ON STACK */
76
    #define DECL_I2C_BUFFER  uint8_t LocBuffer[VL53L0X_MAX_I2C_XFER_SIZE];
77
    #define VL53L0X_GetLocalBuffer(Dev, n_byte)  LocBuffer
78
#elif I2C_BUFFER_CONFIG == 2
79
    /* user define buffer type declare DECL_I2C_BUFFER  as access  via VL53L0X_GetLocalBuffer */
80
    #define DECL_I2C_BUFFER
81
#else
82
#error "invalid I2C_BUFFER_CONFIG "
83
#endif
84

    
85

    
86
#define VL53L0X_I2C_USER_VAR         /* none but could be for a flag var to get/pass to mutex interruptible  return flags and try again */
87
#define VL53L0X_GetI2CAccess(Dev)    /* todo mutex acquire */
88
#define VL53L0X_DoneI2CAcces(Dev)    /* todo mutex release */
89

    
90

    
91
VL53L0X_Error VL53L0X_LockSequenceAccess(VL53L0X_DEV Dev){
92
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
93

    
94
    return Status;
95
}
96

    
97
VL53L0X_Error VL53L0X_UnlockSequenceAccess(VL53L0X_DEV Dev){
98
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
99

    
100
    return Status;
101
}
102

    
103
// the ranging_sensor_comms.dll will take care of the page selection
104
VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count){
105

    
106
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
107
    int32_t status_int = 0;
108
    uint16_t deviceAddress;
109

    
110
    if (count>=VL53L0X_MAX_I2C_XFER_SIZE){
111
        Status = VL53L0X_ERROR_INVALID_PARAMS;
112
    }
113

    
114
    deviceAddress = Dev->I2cDevAddr;
115

    
116
    status_int = VL53L0X_write_multi(Dev->i2cd, deviceAddress, index, pdata, count, Dev->timeout);
117

    
118
    if (status_int != 0)
119
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
120

    
121
    return Status;
122
}
123

    
124
// the ranging_sensor_comms.dll will take care of the page selection
125
VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count){
126
    VL53L0X_I2C_USER_VAR
127
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
128
    int32_t status_int;
129
    uint8_t deviceAddress;
130

    
131
    if (count>=VL53L0X_MAX_I2C_XFER_SIZE){
132
        Status = VL53L0X_ERROR_INVALID_PARAMS;
133
    }
134

    
135
    deviceAddress = Dev->I2cDevAddr;
136

    
137
    status_int = VL53L0X_read_multi(Dev->i2cd, deviceAddress, index, pdata, count, Dev->timeout);
138

    
139
    if (status_int != 0)
140
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
141

    
142
    return Status;
143
}
144

    
145

    
146
VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data){
147
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
148
    int32_t status_int;
149
    uint8_t deviceAddress;
150

    
151
    deviceAddress = Dev->I2cDevAddr;
152

    
153
    status_int = VL53L0X_write_byte(Dev->i2cd, deviceAddress, index, data, Dev->timeout);
154

    
155
    if (status_int != 0)
156
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
157

    
158
    return Status;
159
}
160

    
161
VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data){
162
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
163
    int32_t status_int;
164
    uint8_t deviceAddress;
165

    
166
    deviceAddress = Dev->I2cDevAddr;
167

    
168
    status_int = VL53L0X_write_word(Dev->i2cd, deviceAddress, index, data, Dev->timeout);
169

    
170
    if (status_int != 0)
171
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
172

    
173
    return Status;
174
}
175

    
176
VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data){
177
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
178
    int32_t status_int;
179
    uint8_t deviceAddress;
180

    
181
    deviceAddress = Dev->I2cDevAddr;
182

    
183
    status_int = VL53L0X_write_dword(Dev->i2cd, deviceAddress, index, data, Dev->timeout);
184

    
185
    if (status_int != 0)
186
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
187

    
188
    return Status;
189
}
190

    
191
VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData){
192
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
193
    int32_t status_int;
194
    uint8_t deviceAddress;
195
    uint8_t data;
196

    
197
    deviceAddress = Dev->I2cDevAddr;
198

    
199
    status_int = VL53L0X_read_byte(Dev->i2cd, deviceAddress, index, &data, Dev->timeout);
200

    
201
    if (status_int != 0)
202
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
203

    
204
    if (Status == VL53L0X_ERROR_NONE) {
205
        data = (data & AndData) | OrData;
206
        status_int = VL53L0X_write_byte(Dev->i2cd, deviceAddress, index, data, Dev->timeout);
207

    
208
        if (status_int != 0)
209
            Status = VL53L0X_ERROR_CONTROL_INTERFACE;
210
    }
211

    
212
    return Status;
213
}
214

    
215
VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data){
216
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
217
    int32_t status_int;
218
    uint8_t deviceAddress;
219

    
220
    deviceAddress = Dev->I2cDevAddr;
221

    
222
    status_int = VL53L0X_read_byte(Dev->i2cd, deviceAddress, index, data, Dev->timeout);
223

    
224
    if (status_int != 0)
225
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
226

    
227
    return Status;
228
}
229

    
230
VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data){
231
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
232
    int32_t status_int;
233
    uint8_t deviceAddress;
234

    
235
    deviceAddress = Dev->I2cDevAddr;
236

    
237
    status_int = VL53L0X_read_word(Dev->i2cd, deviceAddress, index, data, Dev->timeout);
238

    
239
    if (status_int != 0)
240
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
241

    
242
    return Status;
243
}
244

    
245
VL53L0X_Error  VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data){
246
    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
247
    int32_t status_int;
248
    uint8_t deviceAddress;
249

    
250
    deviceAddress = Dev->I2cDevAddr;
251

    
252
    status_int = VL53L0X_read_dword(Dev->i2cd, deviceAddress, index, data, Dev->timeout);
253

    
254
    if (status_int != 0)
255
        Status = VL53L0X_ERROR_CONTROL_INTERFACE;
256

    
257
    return Status;
258
}
259

    
260
#define VL53L0X_POLLINGDELAY_LOOPNB  250
261
VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev){
262
    VL53L0X_Error status = VL53L0X_ERROR_NONE;
263
    LOG_FUNCTION_START("");
264

    
265
    //const DWORD cTimeout_ms = 1;
266
    //const uint16  cTimeout_ms = 1;
267
    // 1000 microseconds == 1 milliesecound
268
     usleep(1000);
269

    
270
     //    HANDLE hEvent = CreateEvent(0, TRUE, FALSE, 0);
271
//    if(hEvent != NULL)
272
//    {
273
//        WaitForSingleObject(hEvent,cTimeout_ms);
274
//    }
275

    
276
    LOG_FUNCTION_END(status);
277
    return status;
278
}