]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.c
lpc23xx library - updated 1 wire driver and driver for ds18S20. It still needs some...
[sysless.git] / arch / arm / mach-lpc23xx / libs / 1-wire / ds18s20.c
1
2
3 #include "ds18s20.h"
4 #include "1-wire-drv.h"
5 #include <lpc23xx.h>                    /* LPC23xx definitions                */
6
7
8
9
10
11
12 /* Update 8-bit CRC value
13   using polynomial  X^8 + X^5 + X^4 + 1 */
14
15 #define POLYVAL 0x8C
16
17 void update_crc(unsigned char new, unsigned char *crc)
18 {
19   unsigned char c, i;
20
21   c = *crc;
22   for (i = 0; i < 8; i++) 
23   {
24      if ((c ^ new) & 1) 
25         c = (c >> 1 ) ^ POLYVAL;
26      else 
27         c >>= 1;
28      new >>= 1;
29   }
30  *crc = c;
31 }
32
33
34
35 uint8_t read_temp(struct ds18s20_dev * dev)
36 {
37         uint8_t i, crc =0, data[9];
38         int16_t temp =0;
39
40         init_1_wire(dev->port, dev->pin_read, dev->pin_write, 72000000/4);
41                 
42         if(  wire_detect_device())
43                 return DEVICE_NOT_FOUND;
44                 
45         
46         wire_reset();
47         wire_write_byte (0xCC);         // Send a command to prepare read temp
48         wire_write_byte (0x44);
49
50         wire_delay(6);
51
52         wire_reset();
53         wire_write_byte (0xCC);         // Read temp
54         wire_write_byte (0xBE); 
55
56         for(i = 0; i < 9; i++)
57                         data[i] = wire_read_byte();     // Read 64-bit registration (48-bit serial number) number from 1-wire Slave Device
58
59         for(i = 0; i < 8; i++)
60                 update_crc(data[i], &crc);
61
62
63         if(crc != data[8])
64                 return DEVICE_CRC_FAIL; 
65                 
66         temp = ((uint16_t)(data[7] - data[6]) * 100)/data[7] - 25; 
67         temp = (temp + 5) / 10; 
68         temp += ((data[0] | ((uint16_t)data[1] << 8)) & ~(0x0001)) * 5;
69         dev->temp = temp;
70
71
72         
73         return 0;
74 }       
75
76
77