-
Notifications
You must be signed in to change notification settings - Fork 0
/
d6t32l01a.c
129 lines (111 loc) · 2.45 KB
/
d6t32l01a.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* d6t32l01a.c
*
* Created on: 2023-11-07
* Author: Gintaras Drukteinis
*/
#include "d6t32l01a.h"
#include "cyhal.h"
/*RDK3 I2C Read/Write Implementation*/
extern cyhal_i2c_t I2C_scb3;
static uint32_t d6t32_read(uint8_t devAddr, uint8_t regAddr, uint8_t *data, int length)
{
cy_rslt_t result;
result = cyhal_i2c_master_write( &I2C_scb3, (uint16_t)devAddr, ®Addr, 1, 100, false );
if (result != CY_RSLT_SUCCESS)
{
return D6T_READ_FAIL;
}
result = (int8_t)cyhal_i2c_master_read(&I2C_scb3,(uint16_t)devAddr, data, (uint16_t)length, 100, true);
if (result != CY_RSLT_SUCCESS)
{
return D6T_READ_FAIL;
}
return D6T_OK;
}
static uint32_t d6t32_write(uint8_t devAddr, uint8_t *data, int length)
{
cy_rslt_t result;
result = cyhal_i2c_master_write( &I2C_scb3, (uint16_t)devAddr, data, length, 100, true);
if (result != CY_RSLT_SUCCESS)
{
return D6T_WRITE_FAIL;
}
return D6T_OK;
}
/*RDK3 Delay Implementation*/
static void d6t32_delay_ms(uint32_t msec)
{
CyDelay(msec);
}
static unsigned char calc_crc(unsigned char data)
{
int index;
unsigned char temp;
for(index=0; index<8; index++)
{
temp = data;
data <<= 1;
if(temp & 0x80) data ^= 0x07;
}
return data;
}
static int16_t conv8us_s16_le(uint8_t *buf, int n)
{
uint16_t ret;
ret = (uint16_t)buf[n];
ret += ((uint16_t)buf[n + 1]) << 8;
return (int16_t)ret; // and convert negative.
}
static int D6T_checkPEC(uint8_t *buf, int pPEC)
{
unsigned char crc;
int i;
crc = calc_crc( 0x15 );
for(i=0; i<pPEC; i++)
{
crc = calc_crc(buf[i] ^ crc );
}
return (crc == buf[pPEC]);
}
uint32_t d6t32_init(void)
{
d6t32_delay_ms(20);
uint8_t dat1[] = {D6T_SET_ADD, (((uint8_t)D6T_IIR << 4)&&0xF0) | (0x0F && (uint8_t)D6T_AVERAGE)};
return d6t32_write(D6T_ADDR, dat1, sizeof(dat1));
}
int D6T_getvalue(uint8_t *buf, float *ptat, float *pix_data)
{
int i;
uint32_t ret;
int16_t itemp;
memset(buf, 0, N_READ);
for (i = 0; i < 10; i++)
{
ret = d6t32_read(D6T_ADDR, D6T_CMD, buf, N_READ);
if (ret == 0)
{
break;
}
else if (ret == 23)
{ /* write error */
d6t32_delay_ms(60);
}
else if (ret == 24)
{ /* read error */
d6t32_delay_ms(60);
}
}
if(!D6T_checkPEC(buf, N_READ - 1))
{
return D6T_PEC_FAIL;
}
/* Convert to temperature data (degC) */
*ptat = (float)conv8us_s16_le(buf, 0) / 10.0;
for (i = 0; i < N_PIXEL; i++)
{
itemp = conv8us_s16_le(buf, 2 + 2*i);
pix_data[i] = (float)itemp / 10.0;
}
return D6T_OK;
}