Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
vl53l5cx_platform.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (C) 2024 Fabien-B <name.surname@gmail.com>
4  *
5  * This file is part of paparazzi
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  */
12 
13 
15 #include "hal.h"
16 
17 #define VL53L5_I2C_TIMEOUT chTimeMS2I(100)
18 
19 
22  uint16_t index,
23  uint8_t *p_data)
24 {
25  return RdMulti(dev, index, p_data, 1);
26 }
27 
30  uint16_t index,
31  uint8_t data)
32 {
33  return WrMulti(dev, index, &data, 1);
34 }
35 
38  uint16_t index,
39  uint8_t *pdata,
40  uint32_t count)
41 {
42  I2CDriver * i2cd = (I2CDriver *)dev->i2cdev->reg_addr;
43 
44  i2cAcquireBus(i2cd);
45 
46  msg_t ret;
47 
48  for(size_t offset = 0; offset<count; offset += VL53L5CX_I2C_BUF_SIZE) {
49  uint16_t reg_addr = index + offset;
50  dev->buf[0] = (reg_addr & 0xFF00) >> 8; // MSB first
51  dev->buf[1] = (reg_addr & 0x00FF);
52  size_t size = Min(count-offset, VL53L5CX_I2C_BUF_SIZE);
53  memcpy((uint8_t *) dev->buf + 2, (pdata + offset), size);
54  cacheBufferFlush(dev->buf, size+2);
55  ret = i2cMasterTransmitTimeout(i2cd, dev->address, dev->buf, size+2, NULL, 0, VL53L5_I2C_TIMEOUT);
56  }
57 
58 
59  i2cReleaseBus(i2cd);
60 
61  return ret;
62 }
63 
66  uint16_t index,
67  uint8_t *pdata,
68  uint32_t count)
69 {
70  I2CDriver * i2cd = (I2CDriver *)dev->i2cdev->reg_addr;
71 
72  i2cAcquireBus(i2cd);
73 
74  msg_t ret = 0;
75  for(size_t offset = 0; offset<count; offset += VL53L5CX_I2C_BUF_SIZE) {
76  uint16_t reg_addr = index + offset;
77  dev->buf[0] = (reg_addr & 0xFF00) >> 8; // MSB first
78  dev->buf[1] = (reg_addr & 0x00FF);
79  size_t size = Min(count-offset, VL53L5CX_I2C_BUF_SIZE);
80 
81  cacheBufferFlush(dev->buf, 2);
82  ret = i2cMasterTransmitTimeout(i2cd, dev->address, dev->buf, 2, dev->buf, size, VL53L5_I2C_TIMEOUT);
83  cacheBufferInvalidate(dev->buf, count);
84  memcpy(pdata+offset, dev->buf, count);
85  }
86 
87  i2cReleaseBus(i2cd);
88 
89  return ret;
90 }
91 
93  uint8_t *buffer,
94  uint16_t size)
95 {
96  uint32_t i, tmp;
97 
98  /* Example of possible implementation using <string.h> */
99  for(i = 0; i < size; i = i + 4)
100  {
101  tmp = (
102  buffer[i]<<24)
103  |(buffer[i+1]<<16)
104  |(buffer[i+2]<<8)
105  |(buffer[i+3]);
106 
107  memcpy(&(buffer[i]), &tmp, 4);
108  }
109 }
110 
112  VL53L5CX_Platform *p_platform,
113  uint32_t TimeMs)
114 {
115  (void)p_platform;
116  chThdSleepMilliseconds(TimeMs);
117  return 0;
118 }
static const float offset[]
#define Min(x, y)
Definition: esc_dshot.c:109
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
uint8_t RdByte(VL53L5CX_Platform *dev, uint16_t index, uint8_t *p_data)
uint8_t WaitMs(VL53L5CX_Platform *p_platform, uint32_t TimeMs)
Mandatory function, used to wait during an amount of time.
#define VL53L5_I2C_TIMEOUT
uint8_t WrMulti(VL53L5CX_Platform *dev, uint16_t index, uint8_t *pdata, uint32_t count)
Mandatory function used to write multiples bytes.
void SwapBuffer(uint8_t *buffer, uint16_t size)
Optional function, only used to perform an hardware reset of the sensor.
uint8_t RdMulti(VL53L5CX_Platform *dev, uint16_t index, uint8_t *pdata, uint32_t count)
Mandatory function used to read multiples bytes.
uint8_t WrByte(VL53L5CX_Platform *dev, uint16_t index, uint8_t data)
Mandatory function used to write one single byte.
#define VL53L5CX_I2C_BUF_SIZE
Structure VL53L5CX_Platform needs to be filled by the customer, depending on his platform.