OpenCores
URL https://opencores.org/ocsvn/sockit_owm/sockit_owm/trunk

Subversion Repositories sockit_owm

[/] [sockit_owm/] [trunk/] [HAL/] [src/] [owtran.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 iztok
//---------------------------------------------------------------------------
2
// Copyright (C) 1999 Dallas Semiconductor Corporation, All Rights Reserved.
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a
5
// copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
18
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
// OTHER DEALINGS IN THE SOFTWARE.
21
//
22
// Except as contained in this notice, the name of Dallas Semiconductor
23
// shall not be used except as stated in the Dallas Semiconductor
24
// Branding Policy.
25
//---------------------------------------------------------------------------
26
//
27
//  owTran.C - Transport functions for 1-Wire devices.
28
//
29
//  Version: 2.01
30
//
31
//  History: 1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for
32
//                         multiple ports.
33
//           2.00 -> 2.01  Added support for owError library
34
//
35
 
36
#include "ownet.h"
37
 
38
//--------------------------------------------------------------------------
39
// The 'owBlock' transfers a block of data to and from the
40
// 1-Wire Net with an optional reset at the begining of communication.
41
// The result is returned in the same buffer.
42
//
43
// 'do_reset' - cause a owTouchReset to occure at the begining of
44
//              communication TRUE(1) or not FALSE(0)
45
// 'tran_buf' - pointer to a block of unsigned
46
//              chars of length 'TranferLength' that will be sent
47
//              to the 1-Wire Net
48
// 'tran_len' - length in bytes to transfer
49
// Supported devices: all
50
//
51
// Returns:   TRUE (1) : The optional reset returned a valid
52
//                       presence (do_reset == TRUE) or there
53
//                       was no reset required.
54
//            FALSE (0): The reset did not return a valid prsence
55
//                       (do_reset == TRUE).
56
//
57
//  The maximum tran_len is 160
58
//
59
SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len)
60
{
61
   uchar i;
62
 
63
   // check for a block too big
64
   if (tran_len > 160)
65
   {
66
      OWERROR(OWERROR_BLOCK_TOO_BIG);
67
      return FALSE;
68
   }
69
 
70
   // check if need to do a owTouchReset first
71
   if (do_reset)
72
   {
73
      if (!owTouchReset(portnum))
74
      {
75
         OWERROR(OWERROR_NO_DEVICES_ON_NET);
76
         return FALSE;
77
      }
78
   }
79
 
80
   // send and receive the buffer
81
   for (i = 0; i < tran_len; i++)
82
      tran_buf[i] = (uchar)owTouchByte(portnum,tran_buf[i]);
83
 
84
   return TRUE;
85
}
86
 
87
//--------------------------------------------------------------------------
88
// Write a byte to an EPROM 1-Wire device.
89
//
90
// Supported devices: crc_type=0(CRC8)
91
//                        DS1982
92
//                    crc_type=1(CRC16)
93
//                        DS1985, DS1986, DS2407
94
//
95
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number is provided to
96
//                indicate the symbolic port number.
97
// 'write_byte' - byte to program
98
// 'addr'       - address of byte to program
99
// 'write_cmd'  - command used to write (0x0F reg mem, 0x55 status)
100
// 'crc_type'   - CRC used (0 CRC8, 1 CRC16)
101
// 'do_access'  - Flag to access device for each byte
102
//                (0 skip access, 1 do the access)
103
//                WARNING, only use do_access=0 if programing the NEXT
104
//                byte immediatly after the previous byte.
105
//
106
// Returns: >=0   success, this is the resulting byte from the program
107
//                effort
108
//          -1    error, device not connected or program pulse voltage
109
//                not available
110
//
111
SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd,
112
                       SMALLINT crc_type, SMALLINT do_access)
113
{
114
   ushort lastcrc16;
115
   uchar lastcrc8;
116
 
117
   // optionally access the device
118
   if (do_access)
119
   {
120
      if (!owAccess(portnum))
121
      {
122
         OWERROR(OWERROR_ACCESS_FAILED);
123
         return -1;
124
      }
125
 
126
      // send the write command
127
      if (!owWriteByte(portnum,write_cmd))
128
      {
129
         OWERROR(OWERROR_WRITE_BYTE_FAILED);
130
         return -1;
131
      }
132
 
133
      // send the address
134
      if (!owWriteByte(portnum,addr & 0xFF) || !owWriteByte(portnum,addr >> 8))
135
      {
136
         OWERROR(OWERROR_WRITE_BYTE_FAILED);
137
         return -1;
138
      }
139
   }
140
 
141
   // send the data to write
142
   if (!owWriteByte(portnum,write_byte))
143
   {
144
      OWERROR(OWERROR_WRITE_BYTE_FAILED);
145
      return -1;
146
   }
147
 
148
   // read the CRC
149
   if (crc_type == 0)
150
   {
151
      // calculate CRC8
152
      if (do_access)
153
      {
154
         setcrc8(portnum,0);
155
         docrc8(portnum,(uchar)write_cmd);
156
         docrc8(portnum,(uchar)(addr & 0xFF));
157
         docrc8(portnum,(uchar)(addr >> 8));
158
      }
159
      else
160
         setcrc8(portnum,(uchar)(addr & 0xFF));
161
 
162
      docrc8(portnum,(uchar)write_byte);
163
      // read and calculate the read crc
164
      lastcrc8 = docrc8(portnum,(uchar)owReadByte(portnum));
165
      // crc should now be 0x00
166
      if (lastcrc8 != 0)
167
      {
168
         OWERROR(OWERROR_CRC_FAILED);
169
         return -1;
170
      }
171
   }
172
   else
173
   {
174
      // CRC16
175
      if (do_access)
176
      {
177
         setcrc16(portnum,0);
178
         docrc16(portnum,(ushort)write_cmd);
179
         docrc16(portnum,(ushort)(addr & 0xFF));
180
         docrc16(portnum,(ushort)(addr >> 8));
181
      }
182
      else
183
         setcrc16(portnum,(ushort)addr);
184
      docrc16(portnum,(ushort)write_byte);
185
      // read and calculate the read crc
186
      docrc16(portnum,(ushort)owReadByte(portnum));
187
      lastcrc16 = docrc16(portnum,(ushort)owReadByte(portnum));
188
      // crc should now be 0xB001
189
      if (lastcrc16 != 0xB001)
190
         return -1;
191
   }
192
 
193
   // send the program pulse
194
   if (!owProgramPulse(portnum))
195
   {
196
      OWERROR(OWERROR_PROGRAM_PULSE_FAILED);
197
      return -1;
198
   }
199
 
200
   // read back and return the resulting byte
201
   return owReadByte(portnum);
202
}
203
 
204
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.