]> rtime.felk.cvut.cz Git - sysless.git/commitdiff
lpc23xx library - updated 1 wire driver and driver for ds18S20. It still needs some...
authorJiri Kubias <jiri.kubias@gmail.com>
Sat, 17 Oct 2009 21:10:35 +0000 (23:10 +0200)
committerJiri Kubias <jiri.kubias@gmail.com>
Sat, 17 Oct 2009 21:10:35 +0000 (23:10 +0200)
arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.c
arch/arm/mach-lpc23xx/libs/1-wire/1-wire-drv.h
arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.c
arch/arm/mach-lpc23xx/libs/1-wire/ds18s20.h

index d91ea76db7639e4640b6c9016c6d0db98f0edcc0..7c2defc24a234b8f8627155bbf153307afe08eac 100644 (file)
-// 1-wire drv
 
-#include <lpc2xxx.h>                    /* LPC23xx definitions                */
-#include <types.h>
-
-
-               
+/**
+ *     @file   1-wire-drv.c
+ *     @author Bc. Jiri Kubias jiri.kubias@gmail.com 
+ *
+ *     @brief  Software common 1wire driver
+ *     This driver is based on Maxim implementation. The driver supports
+ *     one (one pin is used for IO operation) or two pin implementation 
+ *     (one pin as input, and second pin as output). External pullup is mandatory 
+ *     for output pin. When hi level driver is done the driver can be reinitiated 
+ *     for another pin sets. The bus is always left in HI state. 
+ *
+ *     @note   This driver uses TIMER3 
+ *             
+ */
 
-#define SET    1
-#define CLEAR  0
 
+#include <lpc2xxx.h>                    /* LPC23xx definitions                */
+#include <types.h>
 
+typedef uint32_t reg_size;     ///< reg_size must be the addres size of register 
+static reg_size dir;           ///< GPIO DIRX register
+static reg_size set;           ///< GPIO SETX register
+static reg_size clr;           ///< GPIO CLRX register
+static reg_size port_wire;     ///< GPIO PORTX register
+static uint8_t pin_read;       ///< place where the 1-wire is connected for reading
+static uint8_t pin_write;      ///< place where the 1-wire is connected for writing
+static uint32_t tim_1us;
 
 
 
-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;
 
 
+/**
+ *  Wait specified time in ms
+ *  @param tim_ms      time to wait
+ *
+ */
+void inline wire_delay(uint32_t tim_ms)        
+{
+       T3TCR = 1; 
+       while(T3TC < ( tim_1us * (tim_ms))); 
+       T3TCR = 3;
+}
 
-unsigned char macro_delay;
 
 /**
- *  Nastavi pin na vystupni a nastavi log. 0
+ *  Sets the output pin to output and drive it low
  *
  */
-void drive_OW_low (void)
+static inline void wire_low (void)
 {
-               *((reg_size *)dir) |= (1<<pin);         
-               *((reg_size *)clr) = (1<<pin);
+       *((reg_size *)dir) |= (1<<pin_write);           
+       *((reg_size *)clr) = (1<<pin_write);
 }
 
-/**********************************************************************
-* 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)
+/**
+ *  Release the pin and sets it as input. The high level is produced by pullup
+ *
+ */
+static inline void wire_high (void)
 {
-       *((reg_size *)dir) &= ~(1<<pin);                        // tohle neni uplne clear
-//             *((reg_size *)dir) |= (1<<pin);         
-//             *((reg_size *)set) = (1<<pin);  
+       *((reg_size *)dir) &= ~(1<<pin_write);     
 }
 
-/**********************************************************************
-* 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)
+/**
+ *  Setrs the pin as input and return its value
+ *
+ */
+static inline uint8_t wire_read (void)
 {
-       //unsigned char read_data=0;
-       
-       *((reg_size *)dir) &= ~(1<<pin);        
-       return (unsigned char) ((*((reg_size *)port) >> pin) & 1);
        
-
+       *((reg_size *)dir) &= ~(1<<pin_read);   
+       return (unsigned char) ((*((reg_size *)port_wire) >> pin_read) & 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)
+/**
+ *  Reset the 1wire bus, read and return the presence bit
+ *     @return         presence detect state
+ */
+uint8_t wire_reset(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;      
+       uint8_t presence_detect = 0;;
        
-       presence_detect = read_OW();    //Sample for presence pulse from slave
-
-       T3TCR = 1;
-       while(T3TC < ( tim_1us * 410));
-       T3TCR = 3;      
+       wire_low();                     // Drive the bus low
+       wire_delay(480);
+       wire_read();                    // Release the bus
+       wire_delay(70); 
+       presence_detect = wire_read();  //Sample for presence pulse from slave
+       wire_delay(410);
+       wire_high ();                   // Release the bus
        
-       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)
+/**
+ *     Write one bit to 1-wire 
+ *     @param  write_bit value to write 
+ */
+static void wire_write_bit (uint8_t 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;      
+               wire_low();                             // Drive the bus low
+               wire_delay(6);
+               wire_high ();                           // Release the bus
+               wire_delay(64);
        }
        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;              
+               wire_low();                             // Drive the bus low
+               wire_delay(60); // delay 60 microsecond (us)
+               wire_high ();                           // Release the bus
+               wire_delay(10); // delay 10 microsecond (us)
        }
 }      
 
 
-/**********************************************************************
-* 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)
+/**
+ *     Read one bit from 1-wire 
+ *     @return         readed bit state
+ */
+static uint8_t wire_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;
+       uint8_t read_data;  
+       wire_low();
+       wire_delay(6);
+       wire_high ();
+       wire_delay(9);
+       read_data = wire_read();
+       wire_delay(55);
        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)
+/**
+ *     Write one byte to 1-wire 
+ *     @param  write_data value to write 
+ */
+void wire_write_byte(uint8_t write_data)
 {
-       unsigned char loop;
+       uint8_t loop;
        
        for (loop = 0; loop < 8; loop++)
        {
-               OW_write_bit(write_data & 0x01);        //Sending LS-bit first
+               wire_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)
+/**
+ *     Read one byte from 1-wire 
+ *     @return         readed byte from 1wire
+ */
+uint8_t wire_read_byte(void)
 {
        unsigned char loop, result=0;
        
@@ -210,45 +160,48 @@ unsigned char OW_read_byte (void)
        {
                
                result >>= 1;                           // shift the result to get it ready for the next bit to receive
-               if (OW_read_bit())
+               if (wire_read_bit())
                result |= 0x80;                         // if result is one, then set MS-bit
        }
-       return result;                                  
+       return result;
 }      
 
-                                                                                                                                                                                                                                                                                
-void init_1_wire(uint8_t port2, uint8_t pin_num, uint32_t f_ahb)
+
+/**
+ *     Initialize the 1wire driver for given parameters
+ *     @param  port    The the port where the IO pins are located
+ *     @param  pin_in_num      The number of input pin
+ *     @param  pin_out_num     The number of output pin
+ *     @param  f_tim   The frequency which is set for timer3
+ *
+ *     @example init_1_wire(FIO1, 10, 10, 72000000);   // when the input and output pin is the same
+ *     @example init_1_wire(FIO1, 10, 11, 72000000);   // when the input and output pin are different
+ */
+void init_1_wire(uint8_t port, uint8_t pin_in_num, uint8_t pin_out_num, uint32_t f_tim)
 {
-       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;
+       dir = (uint32_t) &FIO0DIR + port * 0x20;
+       set = (uint32_t) &FIO0SET + port * 0x20;
+       clr = (uint32_t) &FIO0CLR + port * 0x20;
+       port_wire = (uint32_t) &FIO0PIN + port * 0x20;
+       pin_read  = pin_in_num;
+       pin_write = pin_out_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;
-
-
+       tim_1us = f_tim / 1000000;
 }      
 
 
-/**********************************************************************
-* 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)
+/**
+ *     Reset 1-wire  and test for presence bit
+ *     @return         1 if no device found
+ */
+uint8_t wire_detect_device(void)
 {
-       if (OW_reset_pulse())           
+       if (wire_reset())               
                return 1;
        else            
                return 0;
index d506cb050fc41ac52f276dc062f40e8ecdd2cd6c..385298e35524dcefb23024f0b725031bfe92fea4 100644 (file)
@@ -5,17 +5,47 @@
 
 #include <types.h>
 
+/**
+ *     Initialize the 1wire driver for given parameters
+ *     @param  port    The the port where the IO pins are located
+ *     @param  pin_in_num      The number of input pin
+ *     @param  pin_out_num     The number of output pin
+ *     @param  f_tim   The frequency which is set for timer3
+ *
+ *     @example init_1_wire(FIO1, 10, 10, 72000000);   // when the input and output pin is the same
+ *     @example init_1_wire(FIO1, 10, 11, 72000000);   // when the input and output pin are different
+ */
+void init_1_wire(uint8_t port, uint8_t pin_in_num, uint8_t pin_out_num, uint32_t f_tim);
 
-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);
+/**
+ *  Reset the 1wire bus, read and return the presence bit
+ *     @return         presence detect state
+ */
+uint8_t wire_detect_device(void);
+
+/**
+ *     Reset 1-wire  and test for presence bit
+ *     @return         1 if no device found
+ */
+uint8_t wire_reset(void);
+
+/**
+ *     Write one byte to 1-wire 
+ *     @param  write_data value to write 
+ */
+void wire_write_byte(uint8_t write_data);
+
+/**
+ *     Read one byte from 1-wire 
+ *     @return         readed byte from 1wire
+ */
+uint8_t wire_read_byte(void);
+
+/**
+ *  Wait specified time in ms
+ *  @param tim_ms      time to wait
+ *
+ */
+void inline wire_delay(uint32_t tim_ms);
 
 #endif //__1-WIRE-DRW_H
index afa10787a4a748e6f815d76a8c81d346f9e920c5..f6db496c57ec41ce089ef8b852406ae164c98745 100644 (file)
@@ -37,26 +37,24 @@ 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/4);
+       init_1_wire(dev->port, dev->pin_read, dev->pin_write, 72000000/4);
                
-       if( Detect_Slave_Device())
+       if(  wire_detect_device())
                return DEVICE_NOT_FOUND;
                
        
-       OW_reset_pulse();
-       OW_write_byte (0xCC);           // Send a command to prepare read temp
-       OW_write_byte (0x44);
+       wire_reset();
+       wire_write_byte (0xCC);         // Send a command to prepare read temp
+       wire_write_byte (0x44);
 
-       T3TCR = 1;
-       while(T3TC < ( (72000000/4/100000) * 6));
-       T3TCR = 3;
+       wire_delay(6);
 
-       OW_reset_pulse();
-       OW_write_byte (0xCC);           // Read temp
-       OW_write_byte (0xBE);   
+       wire_reset();
+       wire_write_byte (0xCC);         // Read temp
+       wire_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
+                       data[i] = wire_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);
index dad8df5fc67f9b190fe926001737d5c3c202464e..ecd1b878da8925158a46c061b6d8e41075694693 100644 (file)
@@ -12,7 +12,8 @@
 struct ds18s20_dev 
 {
        uint8_t port;           // 0 for P0, 1 for P1 ...
-       uint8_t pin;
+       uint8_t pin_read;
+       uint8_t pin_write;
        int16_t temp;
 };