IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
vl53l1_platform.c File Reference

Platform-specific implementation for using the VL53L1X API on the EFM32HG322. More...

#include "vl53l1_platform.h"
#include <string.h>
#include <time.h>
#include <math.h>
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_i2c.h"
#include "debug_dbprint.h"
#include "i2c.h"

Go to the source code of this file.

Functions

int8_t VL53L1_WriteMulti (uint16_t dev, uint16_t index, uint8_t *pdata, uint32_t count)
 This method is not used by the API and thereby not implemented. More...
 
int8_t VL53L1_ReadMulti (uint16_t dev, uint16_t index, uint8_t *pdata, uint32_t count)
 TODO. More...
 
int8_t VL53L1_WrByte (uint16_t dev, uint16_t index, uint8_t data)
 Write a byte (8 bits) to the I2C bus. More...
 
int8_t VL53L1_WrWord (uint16_t dev, uint16_t index, uint16_t data)
 Write a word (16 bits) to the I2C bus. More...
 
int8_t VL53L1_WrDWord (uint16_t dev, uint16_t index, uint32_t data)
 Write a double word (32 bits) to the I2C bus. More...
 
int8_t VL53L1_RdByte (uint16_t dev, uint16_t index, uint8_t *data)
 Read a byte (8 bits) from the I2C bus. More...
 
int8_t VL53L1_RdWord (uint16_t dev, uint16_t index, uint16_t *data)
 Read a word (16 bits) from the I2C bus. More...
 
int8_t VL53L1_RdDWord (uint16_t dev, uint16_t index, uint32_t *data)
 Read a double word (32 bits) from the I2C bus. More...
 
int8_t VL53L1_WaitMs (uint16_t dev, int32_t wait_ms)
 This method is not used by the API and thereby not implemented. More...
 

Detailed Description

Platform-specific implementation for using the VL53L1X API on the EFM32HG322.

Version
1.0
Author
Brecht Van Eeckhoudt

Versions

  • v1.0:

License

Copyright (C) 2019 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.


This file is part of VL53L1 Platform, the method names remained but they were filled in with code by Brecht Van Eeckhoudt.

Copyright (c) 2016, STMicroelectronics - All Rights Reserved

License terms: BSD 3-clause "New" or "Revised" License.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file vl53l1_platform.c.

Function Documentation

◆ VL53L1_RdByte()

int8_t VL53L1_RdByte ( uint16_t  dev,
uint16_t  index,
uint8_t *  data 
)

Read a byte (8 bits) from the I2C bus.

VL53L1_RdByte() definition.
To be implemented by the developer.

This method also initializes i2C if necessary.

Parameters
[in]devDevice ID (uint16_t).
[in]indexIndex (uint16_t).
[in]dataPointer to the data (uint8_t) to put the read byte in.
Returns
  • 0 - Byte read correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 341 of file vl53l1_platform.c.

342 {
343  /* Initialize I2C if not already the case */
344  if (initI2C() != 0) return (-6);
345 
346  /* Separate index in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
347  uint8_t index_buf[2] = {((index & 0xFF00) >> 8), (index & 0x00FF)};
348 
349  /* Transfer structure */
350  I2C_TransferSeq_TypeDef i2cTransfer;
351  I2C_TransferReturn_TypeDef result;
352 
353  /* Initializing I2C transfer */
354  i2cTransfer.addr = dev;
355  i2cTransfer.flags = I2C_FLAG_WRITE_READ; /* Data written from buf[0].data and read into buf[1].data */
356  i2cTransfer.buf[0].data = index_buf;
357  i2cTransfer.buf[0].len = 2; /* Index is two bytes */
358  i2cTransfer.buf[1].data = data;
359  i2cTransfer.buf[1].len = 1; /* Data itself is one byte */
360  result = I2C_TransferInit(I2C0, &i2cTransfer);
361 
362  /* Transmitting I2C data (busy-wait until transfer is complete) */
363  while (result == i2cTransferInProgress) result = I2C_Transfer(I2C0);
364 
365  return (result);
366 }
MeasurementData_t data
Definition: main.c:86
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ VL53L1_RdDWord()

int8_t VL53L1_RdDWord ( uint16_t  dev,
uint16_t  index,
uint32_t *  data 
)

Read a double word (32 bits) from the I2C bus.

VL53L1_RdDWord() definition.
To be implemented by the developer.

This method also initializes i2C if necessary.

Parameters
[in]devDevice ID (uint16_t).
[in]indexIndex (uint16_t).
[in]dataPointer to the data (uint32_t) to put the read double word in.
Returns
  • 0 - Double word read correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 453 of file vl53l1_platform.c.

454 {
455  /* Initialize I2C if not already the case */
456  if (initI2C() != 0) return (-6);
457 
458  /* Separate index in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
459  uint8_t index_buf[2] = {((index & 0xFF00) >> 8), (index & 0x00FF)};
460 
461  /* Create buffer for received bytes */
462  uint8_t data_buf[4] = {0, 0, 0, 0};
463 
464  /* Transfer structure */
465  I2C_TransferSeq_TypeDef i2cTransfer;
466  I2C_TransferReturn_TypeDef result;
467 
468  /* Initializing I2C transfer */
469  i2cTransfer.addr = dev;
470  i2cTransfer.flags = I2C_FLAG_WRITE_READ; /* Data written from buf[0].data and read into buf[1].data */
471  i2cTransfer.buf[0].data = index_buf;
472  i2cTransfer.buf[0].len = 2; /* Index is two bytes */
473  i2cTransfer.buf[1].data = data_buf;
474  i2cTransfer.buf[1].len = 4; /* Data itself is four bytes */
475  result = I2C_TransferInit(I2C0, &i2cTransfer);
476 
477  /* Transmitting I2C data (busy-wait until transfer is complete) */
478  while (result == i2cTransferInProgress) result = I2C_Transfer(I2C0);
479 
480  /* Store received bytes correctly in given data location */
481  (*data) = ((uint32_t)data_buf[0] << 24) | ((uint32_t)data_buf[1] << 16) | ((uint32_t)data_buf[2] << 8) | data_buf[3];
482 
483  return (result);
484 }
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98
unsigned int uint32_t
Typedef defining 32 bit unsigned int type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:78

◆ VL53L1_RdWord()

int8_t VL53L1_RdWord ( uint16_t  dev,
uint16_t  index,
uint16_t *  data 
)

Read a word (16 bits) from the I2C bus.

VL53L1_RdWord() definition.
To be implemented by the developer.

This method also initializes i2C if necessary.

Parameters
[in]devDevice ID (uint16_t).
[in]indexIndex (uint16_t).
[in]dataPointer to the data (uint16_t) to put the read word in.
Returns
  • 0 - Word read correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 394 of file vl53l1_platform.c.

395 {
396  /* Initialize I2C if not already the case */
397  if (initI2C() != 0) return (-6);
398 
399  /* Separate index in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
400  uint8_t index_buf[2] = {((index & 0xFF00) >> 8), (index & 0x00FF)};
401 
402  /* Create buffer for received bytes */
403  uint8_t data_buf[2] = {0, 0};
404 
405  /* Transfer structure */
406  I2C_TransferSeq_TypeDef i2cTransfer;
407  I2C_TransferReturn_TypeDef result;
408 
409  /* Initializing I2C transfer */
410  i2cTransfer.addr = dev;
411  i2cTransfer.flags = I2C_FLAG_WRITE_READ; /* Data written from buf[0].data and read into buf[1].data */
412  i2cTransfer.buf[0].data = index_buf;
413  i2cTransfer.buf[0].len = 2; /* Index is two bytes */
414  i2cTransfer.buf[1].data = data_buf;
415  i2cTransfer.buf[1].len = 2; /* Data itself is two bytes */
416  result = I2C_TransferInit(I2C0, &i2cTransfer);
417 
418  /* Transmitting I2C data (busy-wait until transfer is complete) */
419  while (result == i2cTransferInProgress) result = I2C_Transfer(I2C0);
420 
421  /* Store received bytes correctly in given data location */
422  (*data) = ((uint16_t)data_buf[0] << 8) | data_buf[1];
423 
424  return (result);
425 }
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ VL53L1_ReadMulti()

int8_t VL53L1_ReadMulti ( uint16_t  dev,
uint16_t  index,
uint8_t *  pdata,
uint32_t  count 
)

TODO.

VL53L1_ReadMulti() definition.
To be implemented by the developer.

This method also initializes i2C if necessary.
This method is only used in VL53L1X_GetResult.

Parameters
[in]devDevice ID (uint16_t).
[in]indexIndex (uint16_t).
[in]pdataPointer to the data (uint8_t) to put the read bytes in.
[in]countCounter (uint32_t).
Returns
  • 0 - TODO
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 134 of file vl53l1_platform.c.

135 {
136  /* Initialize I2C if not already the case */
137  if (initI2C() != 0) return (-6);
138 
139 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
140  dbcrit("Not yet implemented!");
141 #endif /* DEBUG_DBPRINT */
142 
143  return (0); // to be implemented
144 }
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:541

◆ VL53L1_WaitMs()

int8_t VL53L1_WaitMs ( uint16_t  dev,
int32_t  wait_ms 
)

This method is not used by the API and thereby not implemented.

VL53L1_WaitMs() definition.
To be implemented by the developer.

Definition at line 491 of file vl53l1_platform.c.

492 {
493  /* Not used */
494 
495 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
496  dbcrit("Method not implemented since it doesn't seem to be used!");
497 #endif /* DEBUG_DBPRINT */
498 
499  return (0);
500 }
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:541

◆ VL53L1_WrByte()

int8_t VL53L1_WrByte ( uint16_t  dev,
uint16_t  index,
uint8_t  data 
)

Write a byte (8 bits) to the I2C bus.

VL53L1_WrByte() definition.
To be implemented by the developer.

This method also initializes i2C if necessary.

Parameters
[in]devDevice ID (uint16_t).
[in]indexIndex (uint16_t).
[in]dataThe data (uint8_t) to put on the I2C bus.
Returns
  • 0 - Byte written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 173 of file vl53l1_platform.c.

174 {
175  /* Initialize I2C if not already the case */
176  if (initI2C() != 0) return (-6);
177 
178  /* Separate index in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
179  uint8_t index_buf[2] = {((index & 0xFF00) >> 8), (index & 0x00FF)};
180 
181  /* Separate data in bytes */
182  uint8_t data_buf[1] = {data};
183 
184  /* Transfer structure */
185  I2C_TransferSeq_TypeDef i2cTransfer;
186  I2C_TransferReturn_TypeDef result;
187 
188  /* Initializing I2C transfer */
189  i2cTransfer.addr = dev;
190  i2cTransfer.flags = I2C_FLAG_WRITE_WRITE; /* Data written from buf[0].data and buf[1].data */
191  i2cTransfer.buf[0].data = index_buf;
192  i2cTransfer.buf[0].len = 2; /* Index is two bytes */
193  i2cTransfer.buf[1].data = data_buf;
194  i2cTransfer.buf[1].len = 1; /* Data itself is one byte */
195  result = I2C_TransferInit(I2C0, &i2cTransfer);
196 
197  /* Transmitting I2C data (busy-wait until transfer is complete) */
198  while (result == i2cTransferInProgress) result = I2C_Transfer(I2C0);
199 
200  return (result);
201 }
MeasurementData_t data
Definition: main.c:86
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ VL53L1_WrDWord()

int8_t VL53L1_WrDWord ( uint16_t  dev,
uint16_t  index,
uint32_t  data 
)

Write a double word (32 bits) to the I2C bus.

VL53L1_WrDWord() definition.
To be implemented by the developer.

This method also initializes i2C if necessary.

Parameters
[in]devDevice ID (uint16_t).
[in]indexIndex (uint16_t).
[in]dataThe data (uint32_t) to put on the I2C bus.
Returns
  • 0 - Double word written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 285 of file vl53l1_platform.c.

286 {
287  /* Initialize I2C if not already the case */
288  if (initI2C() != 0) return (-6);
289 
290  /* Separate index in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
291  uint8_t index_buf[2] = {((index & 0xFF00) >> 8), (index & 0x00FF)};
292 
293  /* Separate data in bytes */
294  uint8_t data_buf[4] = {((data & 0xFF000000) >> 24), ((data & 0xFF0000) >> 16), ((data & 0xFF00) >> 8), (data & 0x00FF)};
295 
296  /* Transfer structure */
297  I2C_TransferSeq_TypeDef i2cTransfer;
298  I2C_TransferReturn_TypeDef result;
299 
300  /* Initializing I2C transfer */
301  i2cTransfer.addr = dev;
302  i2cTransfer.flags = I2C_FLAG_WRITE_WRITE; /* Data written from buf[0].data and buf[1].data */
303  i2cTransfer.buf[0].data = index_buf;
304  i2cTransfer.buf[0].len = 2; /* Index is two bytes */
305  i2cTransfer.buf[1].data = data_buf;
306  i2cTransfer.buf[1].len = 4; /* Data itself is four byte */
307  result = I2C_TransferInit(I2C0, &i2cTransfer);
308 
309  /* Transmitting I2C data (busy-wait until transfer is complete) */
310  while (result == i2cTransferInProgress) result = I2C_Transfer(I2C0);
311 
312  return (result);
313 }
MeasurementData_t data
Definition: main.c:86
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98

◆ VL53L1_WriteMulti()

int8_t VL53L1_WriteMulti ( uint16_t  dev,
uint16_t  index,
uint8_t *  pdata,
uint32_t  count 
)

This method is not used by the API and thereby not implemented.

VL53L1_WriteMulti() definition.
To be implemented by the developer.

Definition at line 93 of file vl53l1_platform.c.

94 {
95  /* Not used */
96 
97 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
98  dbcrit("Method not implemented since it doesn't seem to be used!");
99 #endif /* DEBUG_DBPRINT */
100 
101  return (0);
102 }
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
Definition: dbprint.c:541

◆ VL53L1_WrWord()

int8_t VL53L1_WrWord ( uint16_t  dev,
uint16_t  index,
uint16_t  data 
)

Write a word (16 bits) to the I2C bus.

VL53L1_WrWord() definition.
To be implemented by the developer.

This method also initializes i2C if necessary.

Parameters
[in]devDevice ID (uint16_t).
[in]indexIndex (uint16_t).
[in]dataThe data (uint16_t) to put on the I2C bus.
Returns
  • 0 - Word written correctly.
  • -1 - i2cTransferNack, NACK received during transfer.
  • -2 - i2cTransferBusErr, Bus error during transfer (misplaced START/STOP).
  • -3 - i2cTransferArbLost, Arbitration lost during transfer.
  • -4 - i2cTransferUsageFault, Usage fault.
  • -5 - i2cTransferSwFault, SW fault.
  • -6 - Coudldn't configure I2C.

Definition at line 229 of file vl53l1_platform.c.

230 {
231  /* Initialize I2C if not already the case */
232  if (initI2C() != 0) return (-6);
233 
234  /* Separate index in two bytes (ANDing with a mask and shifting one byte (8 bits) per position) */
235  uint8_t index_buf[2] = {((index & 0xFF00) >> 8), (index & 0x00FF)};
236 
237  /* Separate data in bytes */
238  uint8_t data_buf[2] = {((data & 0xFF00) >> 8), (data & 0x00FF)};
239 
240  /* Transfer structure */
241  I2C_TransferSeq_TypeDef i2cTransfer;
242  I2C_TransferReturn_TypeDef result;
243 
244  /* Initializing I2C transfer */
245  i2cTransfer.addr = dev;
246  i2cTransfer.flags = I2C_FLAG_WRITE_WRITE; /* Data written from buf[0].data and buf[1].data */
247  i2cTransfer.buf[0].data = index_buf;
248  i2cTransfer.buf[0].len = 2; /* Index is two bytes */
249  i2cTransfer.buf[1].data = data_buf;
250  i2cTransfer.buf[1].len = 2; /* Data itself is two byte */
251  result = I2C_TransferInit(I2C0, &i2cTransfer);
252 
253  /* Transmitting I2C data (busy-wait until transfer is complete) */
254  while (result == i2cTransferInProgress) result = I2C_Transfer(I2C0);
255 
256  return (result);
257 }
MeasurementData_t data
Definition: main.c:86
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98