1 /* vl53l0x_platform.h - Zephyr customization of ST vl53l0x library.
2  * (library is located in ext/hal/st/lib/sensor/vl53l0x/)
3  */
4 
5 /*
6  * Copyright (c) 2017 STMicroelectronics
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_SENSOR_VL53L0X_VL53L0X_PLATFORM_H_
12 #define ZEPHYR_DRIVERS_SENSOR_VL53L0X_VL53L0X_PLATFORM_H_
13 
14 #include "vl53l0x_def.h"
15 #include "vl53l0x_platform_log.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /**
22  * @struct VL53L0X_Dev_t
23  * @brief  Generic PAL device type that does link between API and platform
24  * abstraction layer
25  *
26  */
27 typedef struct {
28 	VL53L0X_DevData_t Data;    /* embed ST Ewok Dev  data as "Data"*/
29 	/*!< user specific field */
30 	uint8_t   I2cDevAddr;      /* i2c device address user specific field */
31 	uint8_t   comms_type;      /* VL53L0X_COMMS_I2C or VL53L0X_COMMS_SPI */
32 	uint16_t  comms_speed_khz; /* Comms speed [kHz] */
33 	const struct device *i2c;
34 } VL53L0X_Dev_t;
35 
36 
37 /**
38  * @brief Declare the device Handle as a pointer of the structure VL53L0X_Dev_t
39  *
40  */
41 typedef VL53L0X_Dev_t *VL53L0X_DEV;
42 
43 /**
44  * @brief Get ST private structure @a VL53L0X_DevData_t data access
45  *
46  * @param Dev       Device Handle
47  * @param field     ST structure field name
48  * It maybe used and as real data "ref" not just as "get" for sub-structure item
49  * like PALDevDataGet(FilterData.field)[i]
50  * or PALDevDataGet(FilterData.MeasurementIndex)++
51  */
52 #define PALDevDataGet(Dev, field) (Dev->Data.field)
53 
54 /**
55  * @brief  Set ST private structure @a VL53L0X_DevData_t data field
56  * @param Dev       Device Handle
57  * @param field     ST structure field name
58  * @param data      Data to be set
59  */
60 #define PALDevDataSet(Dev, field, data) ((Dev->Data.field) = (data))
61 
62 
63 /**
64  * @defgroup VL53L0X_registerAccess_group PAL Register Access Functions
65  * @brief    PAL Register Access Functions
66  *  @{
67  */
68 
69 /**
70  * Writes the supplied byte buffer to the device
71  * @param Dev    Device Handle
72  * @param index  The register index
73  * @param pdata  Pointer to uint8_t buffer containing the data to be written
74  * @param count  Number of bytes in the supplied byte buffer
75  * @return  VL53L0X_ERROR_NONE        Success
76  * @return  "Other error code"    See ::VL53L0X_Error
77  */
78 VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata,
79 				 uint32_t count);
80 
81 /**
82  * Reads the requested number of bytes from the device
83  * @param   Dev       Device Handle
84  * @param   index     The register index
85  * @param   pdata     Pointer to the uint8_t buffer to store read data
86  * @param   count     Number of uint8_t's to read
87  * @return  VL53L0X_ERROR_NONE        Success
88  * @return  "Other error code"    See ::VL53L0X_Error
89  */
90 VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata,
91 				uint32_t count);
92 
93 /**
94  * Write single byte register
95  * @param   Dev       Device Handle
96  * @param   index     The register index
97  * @param   data      8 bit register data
98  * @return  VL53L0X_ERROR_NONE        Success
99  * @return  "Other error code"    See ::VL53L0X_Error
100  */
101 VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data);
102 
103 /**
104  * Write word register
105  * @param   Dev       Device Handle
106  * @param   index     The register index
107  * @param   data      16 bit register data
108  * @return  VL53L0X_ERROR_NONE        Success
109  * @return  "Other error code"    See ::VL53L0X_Error
110  */
111 VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data);
112 
113 /**
114  * Write double word (4 byte) register
115  * @param   Dev       Device Handle
116  * @param   index     The register index
117  * @param   data      32 bit register data
118  * @return  VL53L0X_ERROR_NONE        Success
119  * @return  "Other error code"    See ::VL53L0X_Error
120  */
121 VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data);
122 
123 /**
124  * Read single byte register
125  * @param   Dev       Device Handle
126  * @param   index     The register index
127  * @param   data      pointer to 8 bit data
128  * @return  VL53L0X_ERROR_NONE        Success
129  * @return  "Other error code"    See ::VL53L0X_Error
130  */
131 VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data);
132 
133 /**
134  * Read word (2byte) register
135  * @param   Dev       Device Handle
136  * @param   index     The register index
137  * @param   data      pointer to 16 bit data
138  * @return  VL53L0X_ERROR_NONE        Success
139  * @return  "Other error code"    See ::VL53L0X_Error
140  */
141 VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data);
142 
143 /**
144  * Read dword (4byte) register
145  * @param   Dev       Device Handle
146  * @param   index     The register index
147  * @param   data      pointer to 32 bit data
148  * @return  VL53L0X_ERROR_NONE        Success
149  * @return  "Other error code"    See ::VL53L0X_Error
150  */
151 VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data);
152 
153 /**
154  * Threat safe Update (read/modify/write) single byte register
155  *
156  * Final_reg = (Initial_reg & and_data) |or_data
157  *
158  * @param   Dev        Device Handle
159  * @param   index      The register index
160  * @param   AndData    8 bit and data
161  * @param   OrData     8 bit or data
162  * @return  VL53L0X_ERROR_NONE        Success
163  * @return  "Other error code"    See ::VL53L0X_Error
164  */
165 VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index,
166 				 uint8_t AndData, uint8_t OrData);
167 
168 /** @} end of VL53L0X_registerAccess_group */
169 
170 
171 /**
172  * @brief execute delay in all polling API call
173  *
174  * A typical multi-thread or RTOs implementation is to sleep the task
175  * for some 5ms (with 100Hz max rate faster polling is not needed)
176  * if nothing specific is need you can define it as an empty/void macro
177  * @code
178  * #define VL53L0X_PollingDelay(...) (void)0
179  * @endcode
180  * @param Dev       Device Handle
181  * @return  VL53L0X_ERROR_NONE        Success
182  * @return  "Other error code"    See ::VL53L0X_Error
183  */
184 VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev);
185 /* usually best implemented as a real function */
186 
187 /** @} end of VL53L0X_platform_group */
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #endif  /* ZEPHYR_DRIVERS_SENSOR_VL53L0X_VL53L0X_PLATFORM_H_ */
194