]> rtime.felk.cvut.cz Git - sysless.git/commitdiff
1-wire driver and driver for ds18s20 temperature 1-wire sensor. First release - timig...
authorJiri Kubias <jiri.kubias@gmail.com>
Fri, 18 Sep 2009 05:16:02 +0000 (07:16 +0200)
committerJiri Kubias <jiri.kubias@gmail.com>
Fri, 18 Sep 2009 05:16:02 +0000 (07:16 +0200)
arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.c [new file with mode: 0644]
arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.h [new file with mode: 0644]
arch/arm/mach-lpc23xx/libs/1-wire/Makefile [new file with mode: 0644]
arch/arm/mach-lpc23xx/libs/1-wire/Makefile.omk [new file with mode: 0644]
arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.c [new file with mode: 0644]
arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.h [new file with mode: 0644]

diff --git a/arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.c b/arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.c
new file mode 100644 (file)
index 0000000..d91ea76
--- /dev/null
@@ -0,0 +1,256 @@
+// 1-wire drv
+
+#include <lpc2xxx.h>                    /* LPC23xx definitions                */
+#include <types.h>
+
+
+               
+
+#define SET    1
+#define CLEAR  0
+
+
+
+
+
+typedef uint32_t reg_size;
+static reg_size dir;
+static reg_size set;
+static reg_size clr;
+static reg_size port;
+static uint8_t pin;
+static uint32_t tim_1us;
+
+
+
+unsigned char macro_delay;
+
+/**
+ *  Nastavi pin na vystupni a nastavi log. 0
+ *
+ */
+void drive_OW_low (void)
+{
+               *((reg_size *)dir) |= (1<<pin);         
+               *((reg_size *)clr) = (1<<pin);
+}
+
+/**********************************************************************
+* Function:        void drive_OW_high (void)
+* PreCondition:    None
+* Input:                  None 
+* Output:                 None 
+* Overview:               Configure the OW_PIN as Output and drive the OW_PIN HIGH.    
+***********************************************************************/
+void drive_OW_high (void)
+{
+       *((reg_size *)dir) &= ~(1<<pin);                        // tohle neni uplne clear
+//             *((reg_size *)dir) |= (1<<pin);         
+//             *((reg_size *)set) = (1<<pin);  
+}
+
+/**********************************************************************
+* Function:        unsigned char read_OW (void)
+* PreCondition:    None
+* Input:                  None 
+* Output:                 Return the status of OW pin. 
+* Overview:               Configure as Input pin and Read the status of OW_PIN         
+***********************************************************************/
+unsigned char read_OW (void)
+{
+       //unsigned char read_data=0;
+       
+       *((reg_size *)dir) &= ~(1<<pin);        
+       return (unsigned char) ((*((reg_size *)port) >> pin) & 1);
+       
+
+}
+
+/**********************************************************************
+* Function:        unsigned char OW_reset_pulse(void)
+* PreCondition:    None
+* Input:                  None 
+* Output:                 Return the Presense Pulse from the slave.    
+* Overview:               Initialization sequence start with reset pulse.
+*                                 This code generates reset sequence as per the protocol
+***********************************************************************/
+unsigned char OW_reset_pulse(void)
+
+{
+       unsigned char presence_detect = 0;;
+       
+       drive_OW_low();                                 // Drive the bus low
+       
+       T3TCR = 1;
+       while(T3TC < ( tim_1us * 480));
+       T3TCR = 3;
+
+
+  read_OW();// drive_OW_high ();                               // Release the bus
+       
+       T3TCR = 1;
+       while(T3TC < ( tim_1us * 70));
+       T3TCR = 3;      
+       
+       presence_detect = read_OW();    //Sample for presence pulse from slave
+
+       T3TCR = 1;
+       while(T3TC < ( tim_1us * 410));
+       T3TCR = 3;      
+       
+       drive_OW_high ();                       // Release the bus
+                                                
+       return presence_detect;
+}      
+
+/**********************************************************************
+* Function:        void OW_write_bit (unsigned char write_data)
+* PreCondition:    None
+* Input:                  Write a bit to 1-wire slave device.
+* Output:                 None
+* Overview:               This function used to transmit a single bit to slave device.
+*                                 
+***********************************************************************/
+
+void OW_write_bit (unsigned char write_bit)
+{
+       if (write_bit)
+       {
+               //writing a bit '1'
+               drive_OW_low();                                 // Drive the bus low
+               T3TCR = 1;
+               while(T3TC < ( tim_1us * 6));
+               T3TCR = 3;      
+               drive_OW_high ();                               // Release the bus
+               T3TCR = 1;
+               while(T3TC < ( tim_1us * 64));
+               T3TCR = 3;      
+       }
+       else
+       {
+               //writing a bit '0'
+               drive_OW_low();                                 // Drive the bus low
+               T3TCR = 1;
+               while(T3TC < ( tim_1us * 60)); // delay 60 microsecond (us)
+               T3TCR = 3;      
+               drive_OW_high ();                               // Release the bus
+               T3TCR = 1;
+               while(T3TC < ( tim_1us * 10)); // delay 10 microsecond (us)
+               T3TCR = 3;              
+       }
+}      
+
+
+/**********************************************************************
+* Function:        unsigned char OW_read_bit (void)
+* PreCondition:    None
+* Input:                  None
+* Output:                 Return the status of the OW PIN
+* Overview:               This function used to read a single bit from the slave device.
+*                                 
+***********************************************************************/
+
+unsigned char OW_read_bit (void)
+{
+       unsigned char read_data; 
+       //reading a bit 
+       drive_OW_low();                                                 // Drive the bus low
+
+       T3TCR = 1;
+       while(T3TC < ( tim_1us * 6));
+       T3TCR = 3;
+
+       drive_OW_high ();                                               // Release the bus
+
+       T3TCR = 1;
+       while(T3TC < ( tim_1us * 9));
+       T3TCR = 3;
+
+       read_data = read_OW();                                  //Read the status of OW_PIN
+
+       T3TCR = 1;
+       while(T3TC < ( tim_1us * 55));
+       T3TCR = 3;
+       return read_data;
+}
+
+/**********************************************************************
+* Function:        void OW_write_byte (unsigned char write_data)
+* PreCondition:    None
+* Input:                  Send byte to 1-wire slave device
+* Output:                 None
+* Overview:               This function used to transmit a complete byte to slave device.
+*                                 
+***********************************************************************/
+void OW_write_byte (unsigned char write_data)
+{
+       unsigned char loop;
+       
+       for (loop = 0; loop < 8; loop++)
+       {
+               OW_write_bit(write_data & 0x01);        //Sending LS-bit first
+               write_data >>= 1;                                       // shift the data byte for the next bit to send
+       }       
+}      
+
+/**********************************************************************
+* Function:        unsigned char OW_read_byte (void)
+* PreCondition:    None
+* Input:                  None
+* Output:                 Return the read byte from slave device
+* Overview:               This function used to read a complete byte from the slave device.
+*                                 
+***********************************************************************/
+
+unsigned char OW_read_byte (void)
+{
+       unsigned char loop, result=0;
+       
+       for (loop = 0; loop < 8; loop++)
+       {
+               
+               result >>= 1;                           // shift the result to get it ready for the next bit to receive
+               if (OW_read_bit())
+               result |= 0x80;                         // if result is one, then set MS-bit
+       }
+       return result;                                  
+}      
+
+                                                                                                                                                                                                                                                                                
+void init_1_wire(uint8_t port2, uint8_t pin_num, uint32_t f_ahb)
+{
+       dir = (uint32_t) &FIO0DIR + port2 * 0x20;
+       set = (uint32_t) &FIO0SET + port2 * 0x20;
+       clr = (uint32_t) &FIO0CLR + port2 * 0x20;
+       port = (uint32_t) &FIO0PIN + port2 * 0x20;
+       pin = pin_num;
+
+       PCONP |= (1<<23);
+       T3TCR = 3; // Enable and reset, timer is set as free runing
+       T3CTCR = 0;
+       T3PR = 0;
+       
+       tim_1us = f_ahb / 1000000;
+
+//     PINMODE1 |= 3;
+
+
+}      
+
+
+/**********************************************************************
+* Function:        unsigned char Detect_Slave_Device(void)
+* PreCondition:    None
+* Input:                  None 
+* Output:                 1 - Not Present   0 - Present        
+* Overview:       To check the presence of slave device.    
+***********************************************************************/
+
+unsigned char Detect_Slave_Device(void)
+{
+       if (OW_reset_pulse())           
+               return 1;
+       else            
+               return 0;
+}
+
diff --git a/arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.h b/arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.h
new file mode 100644 (file)
index 0000000..d506cb0
--- /dev/null
@@ -0,0 +1,21 @@
+// 1-wire driver
+
+#ifndef __1_WIRE_DRW_H 
+#define __1_WIRE_DRW_H   
+
+#include <types.h>
+
+
+void drive_one_wire_low (void);
+void drive_one_wire_high (void);
+unsigned char read__one_wire (void);
+void OW_write_bit (unsigned char write_data);
+unsigned char OW_read_bit (void);
+unsigned char OW_reset_pulse(void);
+void OW_write_byte (unsigned char write_data);
+unsigned char OW_read_byte (void);
+
+void init_1_wire(uint8_t port2, uint8_t pin_num, uint32_t f_ahb);
+unsigned char Detect_Slave_Device(void);
+
+#endif //__1-WIRE-DRW_H
diff --git a/arch/arm/mach-lpc23xx/libs/1-wire/Makefile b/arch/arm/mach-lpc23xx/libs/1-wire/Makefile
new file mode 100644 (file)
index 0000000..76b56fd
--- /dev/null
@@ -0,0 +1,14 @@
+# Generic directory or leaf node makefile for OCERA make framework
+
+ifndef MAKERULES_DIR
+MAKERULES_DIR := $(shell ( old_pwd="" ;  while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd`  ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) )
+endif
+
+ifeq ($(MAKERULES_DIR),)
+all : default
+.DEFAULT::
+       @echo -e "\nThe Makefile.rules has not been found in this or parent directory\n"
+else
+include $(MAKERULES_DIR)/Makefile.rules
+endif
+
diff --git a/arch/arm/mach-lpc23xx/libs/1-wire/Makefile.omk b/arch/arm/mach-lpc23xx/libs/1-wire/Makefile.omk
new file mode 100644 (file)
index 0000000..8947c4a
--- /dev/null
@@ -0,0 +1,12 @@
+# -*- makefile -*-
+
+lib_LIBRARIES = 1wire-drv
+1wire-drv_SOURCES = 1-wire-drv.c
+
+
+
+lib_LIBRARIES = ds18s20
+ds18s20_SOURCES = ds18s20.c
+ds18s20_libs = 1wire-drv
+
+include_HEADERS = 1-wire-drv.h ds18s20.h
diff --git a/arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.c b/arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.c
new file mode 100644 (file)
index 0000000..18e776d
--- /dev/null
@@ -0,0 +1,80 @@
+
+
+#include "ds18s20.h"
+#include "1-wire-drv.h"
+//#include <string.h>
+#include <lpc23xx.h>                    /* LPC23xx definitions                */
+
+
+
+
+
+
+/* Update 8-bit CRC value
+  using polynomial  X^8 + X^5 + X^4 + 1 */
+
+#define POLYVAL 0x8C
+
+void update_crc(unsigned char new, unsigned char *crc)
+{
+  unsigned char c, i;
+
+  c = *crc;
+  for (i = 0; i < 8; i++) 
+  {
+     if ((c ^ new) & 1) 
+       c = (c >> 1 ) ^ POLYVAL;
+     else 
+       c >>= 1;
+     new >>= 1;
+  }
+ *crc = c;
+}
+
+
+
+uint8_t read_temp(struct ds18s20_dev * dev)
+{
+       uint8_t i, crc =0, data[9];
+       int16_t temp =0;
+
+       init_1_wire(dev->port, dev->pin, 72000000);
+               
+       if( Detect_Slave_Device())
+               return DEVICE_NOT_FOUND;
+               
+       
+       OW_reset_pulse();
+       OW_write_byte (0xCC);           // Send a command to prepare read temp
+       OW_write_byte (0x44);
+
+       T3TCR = 1;
+       while(T3TC < ( (72000000/100000) * 6));
+       T3TCR = 3;
+
+       OW_reset_pulse();
+       OW_write_byte (0xCC);           // Read temp
+       OW_write_byte (0xBE);   
+
+       for(i = 0; i < 9; i++)
+                       data[i] = OW_read_byte();       // Read 64-bit registration (48-bit serial number) number from 1-wire Slave Device
+
+       for(i = 0; i < 8; i++)
+               update_crc(data[i], &crc);
+
+
+       if(crc != data[8])
+               return DEVICE_CRC_FAIL; 
+               
+       temp = ((uint16_t)(data[7] - data[6]) * 100)/data[7] - 25; 
+       temp = (temp + 5) / 10; 
+       temp += ((data[0] | ((uint16_t)data[1] << 8)) & ~(0x0001)) * 5;
+       dev->temp = temp;
+
+
+       
+       return 0;
+}      
+
+
+
diff --git a/arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.h b/arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.h
new file mode 100644 (file)
index 0000000..dad8df5
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef __DS18S20_H
+#define __DS18S20_H
+
+#include <types.h>
+
+#define DEVICE_OK                      0
+#define DEVICE_NOT_FOUND               1
+#define DEVICE_CRC_FAIL                        2
+#define DEVICE_SHORT_CIRCUIT           3       // kdyz je zkrat na lince (prectou se samy nuly)
+
+
+struct ds18s20_dev 
+{
+       uint8_t port;           // 0 for P0, 1 for P1 ...
+       uint8_t pin;
+       int16_t temp;
+};     
+
+
+
+uint8_t read_temp(struct ds18s20_dev * dev);
+
+#endif //__DS18S20_H