hh

Dependents:   VL53L0X-mbedOS-master VL53L0X-mbedOS-masterbb

Committer:
andcor02
Date:
Mon Sep 11 16:44:05 2017 +0000
Revision:
1:5f79559961c7
Parent:
0:e6fcdb78a136
Child:
2:a1dffa1ff38a
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjarvisal 0:e6fcdb78a136 1 #include "mbed.h"
mjarvisal 0:e6fcdb78a136 2 #include "vl53l0x_i2c_platform.h"
mjarvisal 0:e6fcdb78a136 3 #include "vl53l0x_platform.h"
mjarvisal 0:e6fcdb78a136 4
andcor02 1:5f79559961c7 5 I2C i2c(PF_0, PF_1);
mjarvisal 0:e6fcdb78a136 6
mjarvisal 0:e6fcdb78a136 7 int VL53L0X_scan( void )
mjarvisal 0:e6fcdb78a136 8 {
mjarvisal 0:e6fcdb78a136 9 int Status;
mjarvisal 0:e6fcdb78a136 10 int i;
mjarvisal 0:e6fcdb78a136 11 char cmd[2];
mjarvisal 0:e6fcdb78a136 12 cmd[0]=0x01;
mjarvisal 0:e6fcdb78a136 13 cmd[1]=0x00;
mjarvisal 0:e6fcdb78a136 14 i2c.frequency(100000);
mjarvisal 0:e6fcdb78a136 15 for (i = 0; i<0xFF; i++)
mjarvisal 0:e6fcdb78a136 16 {
mjarvisal 0:e6fcdb78a136 17 cmd[0]=i;
mjarvisal 0:e6fcdb78a136 18 Status = i2c.write(i, cmd, 1);
mjarvisal 0:e6fcdb78a136 19 if (Status == 0)
mjarvisal 0:e6fcdb78a136 20 break;
mjarvisal 0:e6fcdb78a136 21 }
mjarvisal 0:e6fcdb78a136 22 return i;
mjarvisal 0:e6fcdb78a136 23 }
mjarvisal 0:e6fcdb78a136 24
mjarvisal 0:e6fcdb78a136 25 int32_t VL53L0X_write_multi(uint8_t address, uint8_t index, uint8_t *pdata, int32_t count)
mjarvisal 0:e6fcdb78a136 26 {
mjarvisal 0:e6fcdb78a136 27 int Status;
mjarvisal 0:e6fcdb78a136 28 char data[count+1];
mjarvisal 0:e6fcdb78a136 29 data[0]=index;
mjarvisal 0:e6fcdb78a136 30 for (int i = 1; i< count+1; i++)
mjarvisal 0:e6fcdb78a136 31 {
mjarvisal 0:e6fcdb78a136 32 data[i]=pdata[i-1];
mjarvisal 0:e6fcdb78a136 33 }
mjarvisal 0:e6fcdb78a136 34 Status = i2c.write(address, data, count+1);
mjarvisal 0:e6fcdb78a136 35 return Status;
mjarvisal 0:e6fcdb78a136 36 }
mjarvisal 0:e6fcdb78a136 37
mjarvisal 0:e6fcdb78a136 38 int32_t VL53L0X_read_multi(uint8_t address, uint8_t index, uint8_t *pdata, int32_t count)
mjarvisal 0:e6fcdb78a136 39 {
mjarvisal 0:e6fcdb78a136 40 int Status;
mjarvisal 0:e6fcdb78a136 41 char cmd[1];
mjarvisal 0:e6fcdb78a136 42 char data[count];
mjarvisal 0:e6fcdb78a136 43 cmd[0]=index;
mjarvisal 0:e6fcdb78a136 44 Status = i2c.write(address, cmd, 1);
mjarvisal 0:e6fcdb78a136 45 Status = i2c.read(address, data, count);
mjarvisal 0:e6fcdb78a136 46 for (int i = 0; i< count; i++)
mjarvisal 0:e6fcdb78a136 47 {
mjarvisal 0:e6fcdb78a136 48 pdata[i] = data[i];
mjarvisal 0:e6fcdb78a136 49 }
mjarvisal 0:e6fcdb78a136 50 return Status;
mjarvisal 0:e6fcdb78a136 51 }
mjarvisal 0:e6fcdb78a136 52
mjarvisal 0:e6fcdb78a136 53 int32_t VL53L0X_write_byte(uint8_t address, uint8_t index, uint8_t data)
mjarvisal 0:e6fcdb78a136 54 {
mjarvisal 0:e6fcdb78a136 55 int Status;
mjarvisal 0:e6fcdb78a136 56 char cmd[2];
mjarvisal 0:e6fcdb78a136 57 cmd[0]=index;
mjarvisal 0:e6fcdb78a136 58 cmd[1]=data;
mjarvisal 0:e6fcdb78a136 59 Status = i2c.write(address, cmd, 2);
mjarvisal 0:e6fcdb78a136 60 return Status;
mjarvisal 0:e6fcdb78a136 61 }
mjarvisal 0:e6fcdb78a136 62
mjarvisal 0:e6fcdb78a136 63 int32_t VL53L0X_write_word(uint8_t address, uint8_t index, uint16_t data)
mjarvisal 0:e6fcdb78a136 64 {
mjarvisal 0:e6fcdb78a136 65 int Status;
mjarvisal 0:e6fcdb78a136 66 char cmd[2];
mjarvisal 0:e6fcdb78a136 67 char ind;
mjarvisal 0:e6fcdb78a136 68 ind = index;
mjarvisal 0:e6fcdb78a136 69 cmd[1]=data>>0&0xFF;
mjarvisal 0:e6fcdb78a136 70 cmd[0]=data>>8&0xFF;
mjarvisal 0:e6fcdb78a136 71 Status = i2c.write(address, &ind, 1);
mjarvisal 0:e6fcdb78a136 72 Status |= i2c.write(address, cmd, 2);
mjarvisal 0:e6fcdb78a136 73 return Status;
mjarvisal 0:e6fcdb78a136 74 }
mjarvisal 0:e6fcdb78a136 75
mjarvisal 0:e6fcdb78a136 76 int32_t VL53L0X_write_dword(uint8_t address, uint8_t index, uint32_t data)
mjarvisal 0:e6fcdb78a136 77 {
mjarvisal 0:e6fcdb78a136 78 int Status;
mjarvisal 0:e6fcdb78a136 79 char cmd[4];
mjarvisal 0:e6fcdb78a136 80 char ind;
mjarvisal 0:e6fcdb78a136 81 ind = index;
mjarvisal 0:e6fcdb78a136 82 cmd[3]=data>>0&0xFF;
mjarvisal 0:e6fcdb78a136 83 cmd[2]=data>>8&0xFF;
mjarvisal 0:e6fcdb78a136 84 cmd[1]=data>>16&0xFF;
mjarvisal 0:e6fcdb78a136 85 cmd[0]=data>>24&0xFF;
mjarvisal 0:e6fcdb78a136 86 Status = i2c.write(address, &ind, 1);
mjarvisal 0:e6fcdb78a136 87 Status |= i2c.write(address, cmd, 4);
mjarvisal 0:e6fcdb78a136 88 return Status;
mjarvisal 0:e6fcdb78a136 89 }
mjarvisal 0:e6fcdb78a136 90
mjarvisal 0:e6fcdb78a136 91 int32_t VL53L0X_read_byte(uint8_t address, uint8_t index, uint8_t *pdata)
mjarvisal 0:e6fcdb78a136 92 {
mjarvisal 0:e6fcdb78a136 93 int Status;
mjarvisal 0:e6fcdb78a136 94 char data;
mjarvisal 0:e6fcdb78a136 95 char cmd[1];
mjarvisal 0:e6fcdb78a136 96 cmd[0]=index;
mjarvisal 0:e6fcdb78a136 97 Status = i2c.write(address, cmd, 1);
mjarvisal 0:e6fcdb78a136 98 Status |= i2c.read(address, &data, 1);
mjarvisal 0:e6fcdb78a136 99 *pdata = data;
mjarvisal 0:e6fcdb78a136 100 return Status;
mjarvisal 0:e6fcdb78a136 101 }
mjarvisal 0:e6fcdb78a136 102
mjarvisal 0:e6fcdb78a136 103 int32_t VL53L0X_read_word(uint8_t address, uint8_t index, uint16_t *pdata)
mjarvisal 0:e6fcdb78a136 104 {
mjarvisal 0:e6fcdb78a136 105 int Status;
mjarvisal 0:e6fcdb78a136 106 uint16_t tmp;
mjarvisal 0:e6fcdb78a136 107 char data[2];
mjarvisal 0:e6fcdb78a136 108 char cmd[1];
mjarvisal 0:e6fcdb78a136 109 tmp=0;
mjarvisal 0:e6fcdb78a136 110 cmd[0]=index;
mjarvisal 0:e6fcdb78a136 111 Status = i2c.write(address, cmd, 1);
mjarvisal 0:e6fcdb78a136 112 Status |= i2c.read(address, data, 2);
mjarvisal 0:e6fcdb78a136 113 tmp |= data[1]<<0;
mjarvisal 0:e6fcdb78a136 114 tmp |= data[0]<<8;
mjarvisal 0:e6fcdb78a136 115 *pdata = tmp;
mjarvisal 0:e6fcdb78a136 116 return Status;
mjarvisal 0:e6fcdb78a136 117 }
mjarvisal 0:e6fcdb78a136 118
mjarvisal 0:e6fcdb78a136 119 int32_t VL53L0X_read_dword(uint8_t address, uint8_t index, uint32_t *pdata)
mjarvisal 0:e6fcdb78a136 120 {
mjarvisal 0:e6fcdb78a136 121 int Status;
mjarvisal 0:e6fcdb78a136 122 uint32_t tmp;
mjarvisal 0:e6fcdb78a136 123 char data[4];
mjarvisal 0:e6fcdb78a136 124 char cmd[1];
mjarvisal 0:e6fcdb78a136 125 tmp=0;
mjarvisal 0:e6fcdb78a136 126 cmd[0]=index;
mjarvisal 0:e6fcdb78a136 127 Status = i2c.write(address, cmd, 1);
mjarvisal 0:e6fcdb78a136 128 Status |= i2c.read(address, data, 4);
mjarvisal 0:e6fcdb78a136 129 tmp |= data[3]<<0;
mjarvisal 0:e6fcdb78a136 130 tmp |= data[2]<<8;
mjarvisal 0:e6fcdb78a136 131 tmp |= data[1]<<16;
mjarvisal 0:e6fcdb78a136 132 tmp |= data[0]<<24;
mjarvisal 0:e6fcdb78a136 133 *pdata=tmp;
mjarvisal 0:e6fcdb78a136 134 return Status;
mjarvisal 0:e6fcdb78a136 135 }
mjarvisal 0:e6fcdb78a136 136
mjarvisal 0:e6fcdb78a136 137