1 /* $NetBSD: ds.h,v 1.1 1997/07/06 22:22:01 is Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 Ignatios Souvatzis. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the author nor the names of contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * Definitions for access to Dallas Semiconductor chips which attach to 33 * the same 1-wire bus as the DS2404 RTC. 34 */ 35 36 #ifndef DALLAS_SEMI_CHIPS_H 37 #define DALLAS_SEMI_CHIPS_H 38 39 /* Family codes (low byte of the ROM) */ 40 41 #define DS_FAMILY_2404 0x04 /* DS2404 Econoram Time Chip */ 42 43 /* 44 * ROM access codes. These are only available from the 1-wire bus, and one 45 * of them MUST be used before a memory access code is called. If you want 46 * to detect which devices are on the bus, you have to issue the ROM search 47 * function (see data sheet). 48 * If only one device is on the BUS, and you don't want any ROM function, 49 * issue the SKIP function. 50 * READ ROM works only if only one device is on the bus. 51 */ 52 53 #define DS_ROM_MATCH 0x55 /* 55 8-bytes-of-ROM-to-select */ 54 #define DS_ROM_SEARCH 0xf0 /* see data sheet */ 55 #define DS_ROM_SKIP 0xCC /* don't do ROM function */ 56 #define DS_ROM_READ 0x33 /* 33 -> 8 bytes of ROM */ 57 58 /* 59 * Memory access codes. These are available from the 1- or 3-wire bus, and 60 * but you must use one of the ROM access codes first, if using the 1-wire 61 * bus. 62 * 63 * You can read from any starting address up to the end of the chip, or 64 * abort the read with a reset pulse. 65 * You first write 2-32 bytes beginning some address to the scratchpad. 66 * Starting address and final byte stream length are remembered by the 67 * chip. After reading data and address/length back from the scratchpad, 68 * and verifying the information, you can issue the copy scratchpad command 69 * to copy the written parts of the scratchpad to the corresponding parts 70 * of the implied (in the address) memory page. 71 */ 72 73 #define DS_MEM_WRITE_SCRATCH 0x0f /* 0F low-ads high-ads data ... */ 74 #define DS_MEM_READ_SCRATCH 0xaa /* AA -> low-ads high-ads end-ofs 75 * data ... */ 76 #define DS_MEM_COPY_SCRATCH 0x55 /* 55 low-ads high-ads end-ofs */ 77 #define DS_MEM_READ_MEMORY 0xf0 /* F0 low-ads high-ads -> data ...*/ 78 79 /* 80 * Hardware handle for access functions 81 */ 82 83 struct ds_handle { 84 int (*ds_read_bit) __P((void *)); 85 void (*ds_write_bit) __P((void *, int)); 86 void (*ds_reset) __P((void *)); 87 void *ds_hw_handle; 88 }; 89 90 /* 91 * Functions for access to Dallas Semiconductor chips which attach to 92 * the same 1-wire bus as the DS2404 RTC. 93 */ 94 95 static u_int8_t ds_read_byte __P((struct ds_handle *)); 96 static void ds_write_byte __P((struct ds_handle *, unsigned int)); 97 98 static inline u_int8_t 99 ds_read_byte(dsh) 100 struct ds_handle *dsh; 101 { 102 u_int8_t buf; 103 int i; 104 105 for (i=buf=0; i<8; ++i) 106 buf |= (dsh->ds_read_bit)(dsh->ds_hw_handle) << i; 107 108 return buf; 109 } 110 111 static inline void 112 ds_write_byte(dsh, b) 113 struct ds_handle *dsh; 114 unsigned int b; 115 { 116 int i; 117 118 for (i=0; i<8; ++i) { 119 (dsh->ds_write_bit)(dsh->ds_hw_handle, b & 1); 120 b >>= 1; 121 } 122 } 123 124 #endif /* DALLAS_SEMI_CHIPS_H */ 125