API
usbtemp.c
Go to the documentation of this file.
1 #include "usbtemp.h"
2 
3 #ifdef __linux__
4 #include "linux.c"
5 void wait_1s(void)
6 {
7  struct timeval wait_tv;
8 
9  wait_tv.tv_usec = 0;
10  wait_tv.tv_sec = 1;
11  select(0, NULL, NULL, NULL, &wait_tv);
12 }
13 
14 int is_fd_valid(HANDLE fd)
15 {
16  return (fd > 0);
17 }
18 #else
19 #include "windows.c"
20 void wait_1s(void)
21 {
22  Sleep(1000);
23 }
24 
26 {
27  return (fd != INVALID_HANDLE_VALUE);
28 }
29 #endif
30 
31 extern int ut_errno;
32 
33 static char* ut_msgs[] = {
34  "",
35  "Error, could not get baudrate!",
36  "Error, could not set baudrate!",
37  "Error, serial port does not exist!",
38  "Error, you don't have rw permission to access serial port!",
39  "Error, failed to open serial port device!",
40  "Error, sensor not found!", /* 6 */
41  "Error, sensor CRC mismatch!",
42  "Warining, not expected sensor response!",
43  "Error, could not send data!"
44 };
45 
46 static unsigned char lsb_crc8(unsigned char *data_in, unsigned int len, const unsigned char generator)
47 {
48  unsigned char i;
49  unsigned char crc = 0;
50 
51  for (i = 0; i < len; i++)
52  {
53  crc ^= *(data_in + i);
54  unsigned char bit_counter = 8;
55  do {
56  if (crc & 0x01) {
57  crc = (((crc >> 1) & 0x7f) ^ generator);
58  }
59  else {
60  crc = (crc >> 1) & 0x7f;
61  }
62  bit_counter--;
63  } while (bit_counter > 0);
64  }
65  return crc;
66 }
67 
68 char *DS18B20_errmsg(void)
69 {
70  return ut_msgs[ut_errno];
71 }
72 
73 static int DS18B20_start(HANDLE fd)
74 {
75  if (owReset(fd) < 0) {
76  ut_errno = 6;
77  return -1;
78  }
79  if (owWrite(fd, 0xcc) < 0) {
80  ut_errno = 8;
81  return -1;
82  }
83  return 0;
84 }
85 
86 static int DS18B20_sp(HANDLE fd, unsigned char *sp)
87 {
88  unsigned char i, crc;
89 
90  if (DS18B20_start(fd) < 0) {
91  return -1;
92  }
93  if (owWrite(fd, 0xbe) < 0) {
94  ut_errno = 8;
95  return -1;
96  }
97  for (i = 0; i < DS18X20_SP_SIZE; i++) {
98  *(sp + i) = owRead(fd);
99  }
100 
101  if ((*(sp + 4) & 0x9f) != 0x1f) {
102  ut_errno = 6;
103  return -1;
104  }
105 
107  if (*(sp + DS18X20_SP_SIZE - 1) != crc) {
108  ut_errno = 7;
109  return -1;
110  }
111 
112  return 0;
113 }
114 
116 {
117  if (DS18B20_start(fd) < 0) {
118  return -1;
119  }
120  if (owWrite(fd, 0x44) < 0) {
121  ut_errno = 8;
122  return -1;
123  }
124  return 0;
125 }
126 
127 int DS18B20_setprecision(HANDLE fd, int precision)
128 {
129  int i, rv;
130  unsigned char cfg_old, cfg[4], sp_sensor[DS18X20_SP_SIZE];
131  unsigned char *p;
132 
133  p = cfg + 3;
134  *p = 0x1f | ((unsigned char)(precision - 9) << 5);
135 
136  rv = DS18B20_sp(fd, sp_sensor);
137  if (rv < 0) {
138  return rv;
139  }
140 
141  cfg_old = sp_sensor[DS18B20_SP_CONFIG];
142  if (cfg_old == *p) {
143  return 0;
144  }
145 
146  p--;
147  *p-- = sp_sensor[DS18B20_SP_TL];
148  *p-- = sp_sensor[DS18B20_SP_TH];
149  *p = DS18B20_SP_WRITE;
150 
151  if (DS18B20_start(fd) < 0) {
152  return -1;
153  }
154  for (i = 0; i < 4; i++) {
155  if (owWrite(fd, *p++) < 0) {
156  ut_errno = 8;
157  return -1;
158  }
159  }
160 
161  if (DS18B20_start(fd) < 0) {
162  return -1;
163  }
164  if (owWrite(fd, DS18B20_SP_SAVE) < 0) {
165  ut_errno = 8;
166  return -1;
167  }
168 
169  return 0;
170 }
171 
172 int DS18B20_acquire(HANDLE fd, float *temperature)
173 {
174  int rv;
175  unsigned short T;
176  unsigned char sp_sensor[DS18X20_SP_SIZE];
177 
178  rv = DS18B20_sp(fd, sp_sensor);
179  if (rv < 0) {
180  return rv;
181  }
182 
183  T = (sp_sensor[1] << 8) + (sp_sensor[0] & 0xff);
184  if ((T >> 15) & 0x01) {
185  T--;
186  T ^= 0xffff;
187  T *= -1;
188  }
189  *temperature = (float)T / 16;
190 
191  return 0;
192 }
193 
194 int DS18B20_rom(HANDLE fd, unsigned char *rom)
195 {
196  unsigned char i, crc;
197 
198  if (owReset(fd) < 0) {
199  ut_errno = 6;
200  return -1;
201  }
202  if (owWrite(fd, 0x33) < 0) {
203  ut_errno = 8;
204  return -1;
205  }
206 
207  for (i = 0; i < DS18X20_ROM_SIZE; i++) {
208  rom[i] = owRead(fd);
209  }
210 
212  if (*(rom + DS18X20_ROM_SIZE - 1) != crc) {
213  ut_errno = 7;
214  return -1;
215  }
216 
217  return 0;
218 }
219 
220 HANDLE DS18B20_open(const char *serial_port)
221 {
222  return owOpen(serial_port);
223 }
224 
226 {
227  return owClose(fd);
228 }
unsigned char owRead(int fd)
Definition: linux.c:138
int owOpen(const char *serial_port)
Definition: linux.c:155
void owClose(int fd)
Definition: linux.c:196
int owReset(int fd)
Definition: linux.c:17
int owWrite(int fd, unsigned char wbuff)
Definition: linux.c:143
#define HANDLE
Definition: platform.h:8
static unsigned char lsb_crc8(unsigned char *data_in, unsigned int len, const unsigned char generator)
Definition: usbtemp.c:46
void wait_1s(void)
Definition: usbtemp.c:20
static int DS18B20_start(HANDLE fd)
Definition: usbtemp.c:73
static int DS18B20_sp(HANDLE fd, unsigned char *sp)
Definition: usbtemp.c:86
char * DS18B20_errmsg(void)
Definition: usbtemp.c:68
int DS18B20_setprecision(HANDLE fd, int precision)
Definition: usbtemp.c:127
int DS18B20_acquire(HANDLE fd, float *temperature)
Definition: usbtemp.c:172
static char * ut_msgs[]
Definition: usbtemp.c:33
int ut_errno
Definition: linux.c:15
int DS18B20_measure(HANDLE fd)
Definition: usbtemp.c:115
HANDLE DS18B20_open(const char *serial_port)
Definition: usbtemp.c:220
void DS18B20_close(HANDLE fd)
Definition: usbtemp.c:225
int is_fd_valid(HANDLE fd)
Definition: usbtemp.c:25
int DS18B20_rom(HANDLE fd, unsigned char *rom)
Definition: usbtemp.c:194
#define DS18B20_SP_TL
Definition: usbtemp.h:16
#define DS18B20_SP_WRITE
Definition: usbtemp.h:19
#define DS18X20_SP_SIZE
Definition: usbtemp.h:14
#define DS18B20_SP_CONFIG
Definition: usbtemp.h:17
#define DS18X20_GENERATOR
Definition: usbtemp.h:12
#define DS18B20_SP_TH
Definition: usbtemp.h:15
#define DS18X20_ROM_SIZE
Definition: usbtemp.h:13
#define DS18B20_SP_SAVE
Definition: usbtemp.h:20