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