+++ /dev/null
-
-/**
- * @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
- *
- */
-
-
-#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;
-
-
-
-
-
-/**
- * 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;
-}
-
-
-/**
- * Sets the output pin to output and drive it low
- *
- */
-static inline void wire_low (void)
-{
- *((reg_size *)dir) |= (1<<pin_write);
- *((reg_size *)clr) = (1<<pin_write);
-}
-
-/**
- * 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_write);
-}
-
-/**
- * Setrs the pin as input and return its value
- *
- */
-static inline uint8_t wire_read (void)
-{
-
- *((reg_size *)dir) &= ~(1<<pin_read);
- return (unsigned char) ((*((reg_size *)port_wire) >> pin_read) & 1);
-}
-
-/**
- * Reset the 1wire bus, read and return the presence bit
- * @return presence detect state
- */
-uint8_t wire_reset(void)
-
-{
- uint8_t presence_detect = 0;;
-
- 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
-
- return presence_detect;
-}
-
-/**
- * Write one bit to 1-wire
- * @param write_bit value to write
- */
-static void wire_write_bit (uint8_t write_bit)
-{
- if (write_bit)
- {
- wire_low(); // Drive the bus low
- wire_delay(6);
- wire_high (); // Release the bus
- wire_delay(64);
- }
- else
- {
- 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)
- }
-}
-
-
-/**
- * Read one bit from 1-wire
- * @return readed bit state
- */
-static uint8_t wire_read_bit (void)
-{
- uint8_t read_data;
- wire_low();
- wire_delay(6);
- wire_high ();
- wire_delay(9);
- read_data = wire_read();
- wire_delay(55);
- return read_data;
-}
-
-/**
- * Write one byte to 1-wire
- * @param write_data value to write
- */
-void wire_write_byte(uint8_t write_data)
-{
- uint8_t loop;
-
- for (loop = 0; loop < 8; loop++)
- {
- wire_write_bit(write_data & 0x01); //Sending LS-bit first
- write_data >>= 1; // shift the data byte for the next bit to send
- }
-}
-
-/**
- * Read one byte from 1-wire
- * @return readed byte from 1wire
- */
-uint8_t wire_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 (wire_read_bit())
- result |= 0x80; // if result is one, then set MS-bit
- }
- return result;
-}
-
-
-/**
- * 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 + 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_tim / 1000000;
-}
-
-
-/**
- * Reset 1-wire and test for presence bit
- * @return 1 if no device found
- */
-uint8_t wire_detect_device(void)
-{
- if (wire_reset())
- return 1;
- else
- return 0;
-}
-
+++ /dev/null
-// 1-wire driver
-
-#ifndef __1_WIRE_DRW_H
-#define __1_WIRE_DRW_H
-
-#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);
-
-/**
- * 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
+++ /dev/null
-
-
-#include "ds18s20.h"
-#include "1-wire-drv.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_read, dev->pin_write, 72000000/4);
-
- if( wire_detect_device())
- return DEVICE_NOT_FOUND;
-
-
- wire_reset();
- wire_write_byte (0xCC); // Send a command to prepare read temp
- wire_write_byte (0x44);
-
- wire_delay(6);
-
- wire_reset();
- wire_write_byte (0xCC); // Read temp
- wire_write_byte (0xBE);
-
- for(i = 0; i < 9; i++)
- 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);
-
-
- 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;
-}
-
-
-