1-Wire implementation, using DS2480B controller interfaced with serial port, working example to read DS18B20, based on work already in progress / Dallas - Public domain code
Revision 0:1193dbfe28e2, committed 2011-03-24
- Comitter:
- pwheels
- Date:
- Thu Mar 24 17:21:29 2011 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 1193dbfe28e2 Headers/ds2480.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Headers/ds2480.h Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,220 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// DS2480.H - This file contains the DS2480B constants +// +// Version: 2.00 +// +// History: 1.02 -> 1.03 Make sure uchar is not defined twice. +// +// + + +#ifndef DS2480_H +#define DS2480_H + +#include "ownet.h" + +#ifndef __MC68K__ +#include <stdlib.h> +#ifndef _WIN32_WCE +#include <stdio.h> +#endif +#endif + +// Mode Commands +#define MODE_DATA 0xE1 +#define MODE_COMMAND 0xE3 +#define MODE_STOP_PULSE 0xF1 + +// Return byte value +#define RB_CHIPID_MASK 0x1C +#define RB_RESET_MASK 0x03 +#define RB_1WIRESHORT 0x00 +#define RB_PRESENCE 0x01 +#define RB_ALARMPRESENCE 0x02 +#define RB_NOPRESENCE 0x03 + +#define RB_BIT_MASK 0x03 +#define RB_BIT_ONE 0x03 +#define RB_BIT_ZERO 0x00 + +// Masks for all bit ranges +#define CMD_MASK 0x80 +#define FUNCTSEL_MASK 0x60 +#define BITPOL_MASK 0x10 +#define SPEEDSEL_MASK 0x0C +#define MODSEL_MASK 0x02 +#define PARMSEL_MASK 0x70 +#define PARMSET_MASK 0x0E +#define VERSION_MASK 0x1C + +// Command or config bit +#define CMD_COMM 0x81 +#define CMD_CONFIG 0x01 + +// Function select bits +#define FUNCTSEL_BIT 0x00 +#define FUNCTSEL_SEARCHON 0x30 +#define FUNCTSEL_SEARCHOFF 0x20 +#define FUNCTSEL_RESET 0x40 +#define FUNCTSEL_CHMOD 0x60 + +// Bit polarity/Pulse voltage bits +#define BITPOL_ONE 0x10 +#define BITPOL_ZERO 0x00 +#define BITPOL_5V 0x00 +#define BITPOL_12V 0x10 + +// One Wire speed bits +#define SPEEDSEL_STD 0x00 +#define SPEEDSEL_FLEX 0x04 +#define SPEEDSEL_OD 0x08 +#define SPEEDSEL_PULSE 0x0C + +// Data/Command mode select bits +#define MODSEL_DATA 0x00 +#define MODSEL_COMMAND 0x02 + +// 5V Follow Pulse select bits (If 5V pulse +// will be following the next byte or bit.) +#define PRIME5V_TRUE 0x02 +#define PRIME5V_FALSE 0x00 + +// Parameter select bits +#define PARMSEL_PARMREAD 0x00 +#define PARMSEL_SLEW 0x10 +#define PARMSEL_12VPULSE 0x20 +#define PARMSEL_5VPULSE 0x30 +#define PARMSEL_WRITE1LOW 0x40 +#define PARMSEL_SAMPLEOFFSET 0x50 +#define PARMSEL_ACTIVEPULLUPTIME 0x60 +#define PARMSEL_BAUDRATE 0x70 + +// Pull down slew rate. +#define PARMSET_Slew15Vus 0x00 +#define PARMSET_Slew2p2Vus 0x02 +#define PARMSET_Slew1p65Vus 0x04 +#define PARMSET_Slew1p37Vus 0x06 +#define PARMSET_Slew1p1Vus 0x08 +#define PARMSET_Slew0p83Vus 0x0A +#define PARMSET_Slew0p7Vus 0x0C +#define PARMSET_Slew0p55Vus 0x0E + +// 12V programming pulse time table +#define PARMSET_32us 0x00 +#define PARMSET_64us 0x02 +#define PARMSET_128us 0x04 +#define PARMSET_256us 0x06 +#define PARMSET_512us 0x08 +#define PARMSET_1024us 0x0A +#define PARMSET_2048us 0x0C +#define PARMSET_infinite 0x0E + +// 5V strong pull up pulse time table +#define PARMSET_16p4ms 0x00 +#define PARMSET_65p5ms 0x02 +#define PARMSET_131ms 0x04 +#define PARMSET_262ms 0x06 +#define PARMSET_524ms 0x08 +#define PARMSET_1p05s 0x0A +#define PARMSET_2p10s 0x0C +#define PARMSET_infinite 0x0E + +// Write 1 low time +#define PARMSET_Write8us 0x00 +#define PARMSET_Write9us 0x02 +#define PARMSET_Write10us 0x04 +#define PARMSET_Write11us 0x06 +#define PARMSET_Write12us 0x08 +#define PARMSET_Write13us 0x0A +#define PARMSET_Write14us 0x0C +#define PARMSET_Write15us 0x0E + +// Data sample offset and Write 0 recovery time +#define PARMSET_SampOff3us 0x00 +#define PARMSET_SampOff4us 0x02 +#define PARMSET_SampOff5us 0x04 +#define PARMSET_SampOff6us 0x06 +#define PARMSET_SampOff7us 0x08 +#define PARMSET_SampOff8us 0x0A +#define PARMSET_SampOff9us 0x0C +#define PARMSET_SampOff10us 0x0E + +// Active pull up on time +#define PARMSET_PullUp0p0us 0x00 +#define PARMSET_PullUp0p5us 0x02 +#define PARMSET_PullUp1p0us 0x04 +#define PARMSET_PullUp1p5us 0x06 +#define PARMSET_PullUp2p0us 0x08 +#define PARMSET_PullUp2p5us 0x0A +#define PARMSET_PullUp3p0us 0x0C +#define PARMSET_PullUp3p5us 0x0E + +// Baud rate bits +#define PARMSET_9600 0x00 +#define PARMSET_19200 0x02 +#define PARMSET_57600 0x04 +#define PARMSET_115200 0x06 + +// Baud rate bits - Inverted Read +#define PARMSET_INV_9600 0x08 +#define PARMSET_INV_19200 0x0A +#define PARMSET_INV_57600 0x0C +#define PARMSET_INV_115200 0x0E + + +// DS2480B program voltage available +#define DS2480PROG_MASK 0x20 + +// mode bit flags +#define MODE_NORMAL 0x00 +#define MODE_OVERDRIVE 0x01 +#define MODE_STRONG5 0x02 +#define MODE_PROGRAM 0x04 +#define MODE_BREAK 0x08 + +// Versions of DS2480 +#define VER_LINK 0x1C +#define VER_DS2480 0x08 +#define VER_DS2480B 0x0C + +// exportable functions defined in ds2480ut.c +SMALLINT DS2480Detect(int portnum); +SMALLINT DS2480ChangeBaud(int portnum, uchar newbaud); + +// link functions from win32lnk.c or other link files +SMALLINT OpenCOM(int portnum, char *port_zstr); +int OpenCOMEx(char *port_zstr); +void CloseCOM(int portnum); +void FlushCOM(int portnum); +SMALLINT WriteCOM(int portnum, int outlen, uchar *outbuf); +int ReadCOM(int portnum, int inlen, uchar *inbuf); +void BreakCOM(int portnum); +void SetBaudCOM(int portnum, int new_baud); + + +#endif //DS2480_H
diff -r 000000000000 -r 1193dbfe28e2 Headers/findtype.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Headers/findtype.h Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,33 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2001 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// findtype.c - Header for the module to find all devices of one type. +// +// Version: 2.00 +// +//---------------------------------------------------------------------- + +SMALLINT FindDevices(int,uchar FamilySN[][8],SMALLINT,int);
diff -r 000000000000 -r 1193dbfe28e2 Headers/owTemp.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Headers/owTemp.h Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,43 @@ + + + +#ifndef MBED_1WIRETEMP_H +#define MBED_1WIRETEMP_H + + + +/** An interface for the Dallas 1-wire temperature buttons, the DS1920 + * + * @code + * #include "mbed.h" + * #include "1-wiretemp.h" + * + * // TODO NokiaLCD lcd(p5, p7, p8, p9); // mosi, sclk, cs, rst + * + * int main() { + * lcd.printf("Hello World!"); + * } + * @endcode + */ +class owTemp { + + + +public: + + owTemp ( ); + + + void Maingettemp( void ); + + +protected: + +private: + + +}; + +#endif + +
diff -r 000000000000 -r 1193dbfe28e2 Headers/ownet.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Headers/ownet.h Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,364 @@ +/* + * ownet.h + * + * Created on: Feb 6, 2009 + * Author: scv + */ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// ownet.h - Include file for 1-Wire Net library +// +// Version: 2.10 +// +// History: 1.02 -> 1.03 Make sure uchar is not defined twice. +// 1.03 -> 2.00 Changed 'MLan' to 'ow'. +// 2.00 -> 2.01 Added error handling. Added circular-include check. +// 2.01 -> 2.10 Added raw memory error handling and SMALLINT +// 2.10 -> 3.00 Added memory bank functionality +// Added file I/O operations +// + +#ifndef OWNET_H +#define OWNET_H + + +//--------------------------------------------------------------// +// Common Includes to ownet applications +//--------------------------------------------------------------// +#include <stdlib.h> + +//--------------------------------------------------------------// +// Typedefs +//--------------------------------------------------------------// +#ifndef SMALLINT + // + // purpose of smallint is for compile-time changing of formal + // parameters and return values of functions. For each target + // machine, an integer is alleged to represent the most "simple" + // number representable by that architecture. This should, in + // most cases, produce optimal code for that particular arch. + // BUT... The majority of compilers designed for embedded + // processors actually keep an int at 16 bits, although the + // architecture might only be comfortable with 8 bits. + // The default size of smallint will be the same as that of + // an integer, but this allows for easy overriding of that size. + // + // NOTE: + // In all cases where a smallint is used, it is assumed that + // decreasing the size of this integer to something as low as + // a single byte _will_not_ change the functionality of the + // application. e.g. a loop counter that will iterate through + // several kilobytes of data should not be SMALLINT. The most + // common place you'll see smallint is for boolean return types. + // + #define SMALLINT int +#endif + +#define MAX_BAUD 0x06 + + +#ifndef OW_UCHAR + #define OW_UCHAR + typedef unsigned char uchar; + #if !defined(__MINGW32__) && (defined(__CYGWIN__) || defined(__GNUC__)) + typedef unsigned long ulong; + //ushort already defined in sys/types.h +// Commented out 11/23/2010 sv #include <sys/types.h> + #else + #if defined(_WIN32) || defined(WIN32) || defined(__MC68K__) || defined(_WIN32_WCE) || defined(_DOS) || defined(_WINDOWS) || defined(__MINGW32__) + typedef unsigned short ushort; + typedef unsigned long ulong; + #endif + #endif + +#endif + +// general defines +#define WRITE_FUNCTION 1 +#define READ_FUNCTION 0 + +// error codes +#define READ_ERROR -1 +#define INVALID_DIR -2 +#define NO_FILE -3 +#define WRITE_ERROR -4 +#define WRONG_TYPE -5 +#define FILE_TOO_BIG -6 + +// Misc +#ifndef FALSE + #define FALSE 0 +#endif + +#ifndef TRUE + #define TRUE 1 +#endif + +#ifndef MAX_PORTNUM + #define MAX_PORTNUM 16 +#endif + +// mode bit flags +#define MODE_NORMAL 0x00 +#define MODE_OVERDRIVE 0x01 +#define MODE_STRONG5 0x02 +#define MODE_PROGRAM 0x04 +#define MODE_BREAK 0x08 + +// Output flags +#define LV_ALWAYS 2 +#define LV_OPTIONAL 1 +#define LV_VERBOSE 0 + +//--------------------------------------------------------------// +// Error handling +//--------------------------------------------------------------// +extern int owGetErrorNum(void); +extern int owHasErrors(void); + + +//Clears the stack. +#define OWERROR_CLEAR() while(owHasErrors()) owGetErrorNum(); + +#ifdef DEBUG + //Raises an exception with extra debug info + #define OWERROR(err) owRaiseError(err,__LINE__,__FILE__) + extern void owRaiseError(int,int,char*); + #define OWASSERT(s,err,ret) if(!(s)){owRaiseError((err),__LINE__,__FILE__);return (ret);} +#else + //Raises an exception with just the error code + #define OWERROR(err) printf( "Debug Error: %u\r\n", err ); + extern void owRaiseError(int); + #define OWASSERT(s,err,ret) if(!(s)){owRaiseError((err));return (ret);} +#endif + +#ifdef SMALL_MEMORY_TARGET + #define OWERROR_DUMP(fileno) /*no-op*/; +#else + //Prints the stack out to the given file. +#define OWERROR_DUMP(fileno) iprintf("Debug Error: %u", fileno); +// #define OWERROR_DUMP(fileno) while(owHasErrors()) owPrintErrorMsg(fileno); +// extern void owPrintErrorMsg(FILE *); + extern void owPrintErrorMsgStd(); + extern char *owGetErrorMsg(int); +#endif + +#define OWERROR_NO_ERROR_SET 0 +#define OWERROR_NO_DEVICES_ON_NET 1 +#define OWERROR_RESET_FAILED 2 +#define OWERROR_SEARCH_ERROR 3 +#define OWERROR_ACCESS_FAILED 4 +#define OWERROR_DS2480_NOT_DETECTED 5 +#define OWERROR_DS2480_WRONG_BAUD 6 +#define OWERROR_DS2480_BAD_RESPONSE 7 +#define OWERROR_OPENCOM_FAILED 8 +#define OWERROR_WRITECOM_FAILED 9 +#define OWERROR_READCOM_FAILED 10 +#define OWERROR_BLOCK_TOO_BIG 11 +#define OWERROR_BLOCK_FAILED 12 +#define OWERROR_PROGRAM_PULSE_FAILED 13 +#define OWERROR_PROGRAM_BYTE_FAILED 14 +#define OWERROR_WRITE_BYTE_FAILED 15 +#define OWERROR_READ_BYTE_FAILED 16 +#define OWERROR_WRITE_VERIFY_FAILED 17 +#define OWERROR_READ_VERIFY_FAILED 18 +#define OWERROR_WRITE_SCRATCHPAD_FAILED 19 +#define OWERROR_COPY_SCRATCHPAD_FAILED 20 +#define OWERROR_INCORRECT_CRC_LENGTH 21 +#define OWERROR_CRC_FAILED 22 +#define OWERROR_GET_SYSTEM_RESOURCE_FAILED 23 +#define OWERROR_SYSTEM_RESOURCE_INIT_FAILED 24 +#define OWERROR_DATA_TOO_LONG 25 +#define OWERROR_READ_OUT_OF_RANGE 26 +#define OWERROR_WRITE_OUT_OF_RANGE 27 +#define OWERROR_DEVICE_SELECT_FAIL 28 +#define OWERROR_READ_SCRATCHPAD_VERIFY 29 +#define OWERROR_COPY_SCRATCHPAD_NOT_FOUND 30 +#define OWERROR_ERASE_SCRATCHPAD_NOT_FOUND 31 +#define OWERROR_ADDRESS_READ_BACK_FAILED 32 +#define OWERROR_EXTRA_INFO_NOT_SUPPORTED 33 +#define OWERROR_PG_PACKET_WITHOUT_EXTRA 34 +#define OWERROR_PACKET_LENGTH_EXCEEDS_PAGE 35 +#define OWERROR_INVALID_PACKET_LENGTH 36 +#define OWERROR_NO_PROGRAM_PULSE 37 +#define OWERROR_READ_ONLY 38 +#define OWERROR_NOT_GENERAL_PURPOSE 39 +#define OWERROR_READ_BACK_INCORRECT 40 +#define OWERROR_INVALID_PAGE_NUMBER 41 +#define OWERROR_CRC_NOT_SUPPORTED 42 +#define OWERROR_CRC_EXTRA_INFO_NOT_SUPPORTED 43 +#define OWERROR_READ_BACK_NOT_VALID 44 +#define OWERROR_COULD_NOT_LOCK_REDIRECT 45 +#define OWERROR_READ_STATUS_NOT_COMPLETE 46 +#define OWERROR_PAGE_REDIRECTION_NOT_SUPPORTED 47 +#define OWERROR_LOCK_REDIRECTION_NOT_SUPPORTED 48 +#define OWERROR_READBACK_EPROM_FAILED 49 +#define OWERROR_PAGE_LOCKED 50 +#define OWERROR_LOCKING_REDIRECTED_PAGE_AGAIN 51 +#define OWERROR_REDIRECTED_PAGE 52 +#define OWERROR_PAGE_ALREADY_LOCKED 53 +#define OWERROR_WRITE_PROTECTED 54 +#define OWERROR_NONMATCHING_MAC 55 +#define OWERROR_WRITE_PROTECT 56 +#define OWERROR_WRITE_PROTECT_SECRET 57 +#define OWERROR_COMPUTE_NEXT_SECRET 58 +#define OWERROR_LOAD_FIRST_SECRET 59 +#define OWERROR_POWER_NOT_AVAILABLE 60 +#define OWERROR_XBAD_FILENAME 61 +#define OWERROR_XUNABLE_TO_CREATE_DIR 62 +#define OWERROR_REPEAT_FILE 63 +#define OWERROR_DIRECTORY_NOT_EMPTY 64 +#define OWERROR_WRONG_TYPE 65 +#define OWERROR_BUFFER_TOO_SMALL 66 +#define OWERROR_NOT_WRITE_ONCE 67 +#define OWERROR_FILE_NOT_FOUND 68 +#define OWERROR_OUT_OF_SPACE 69 +#define OWERROR_TOO_LARGE_BITNUM 70 +#define OWERROR_NO_PROGRAM_JOB 71 +#define OWERROR_FUNC_NOT_SUP 72 +#define OWERROR_HANDLE_NOT_USED 73 +#define OWERROR_FILE_WRITE_ONLY 74 +#define OWERROR_HANDLE_NOT_AVAIL 75 +#define OWERROR_INVALID_DIRECTORY 76 +#define OWERROR_HANDLE_NOT_EXIST 77 +#define OWERROR_NONMATCHING_SNUM 78 +#define OWERROR_NON_PROGRAM_PARTS 79 +#define OWERROR_PROGRAM_WRITE_PROTECT 80 +#define OWERROR_FILE_READ_ERR 81 +#define OWERROR_ADDFILE_TERMINATED 82 +#define OWERROR_READ_MEMORY_PAGE_FAILED 83 +#define OWERROR_MATCH_SCRATCHPAD_FAILED 84 +#define OWERROR_ERASE_SCRATCHPAD_FAILED 85 +#define OWERROR_READ_SCRATCHPAD_FAILED 86 +#define OWERROR_SHA_FUNCTION_FAILED 87 +#define OWERROR_NO_COMPLETION_BYTE 88 +#define OWERROR_WRITE_DATA_PAGE_FAILED 89 +#define OWERROR_COPY_SECRET_FAILED 90 +#define OWERROR_BIND_SECRET_FAILED 91 +#define OWERROR_INSTALL_SECRET_FAILED 92 +#define OWERROR_VERIFY_SIG_FAILED 93 +#define OWERROR_SIGN_SERVICE_DATA_FAILED 94 +#define OWERROR_VERIFY_AUTH_RESPONSE_FAILED 95 +#define OWERROR_ANSWER_CHALLENGE_FAILED 96 +#define OWERROR_CREATE_CHALLENGE_FAILED 97 +#define OWERROR_BAD_SERVICE_DATA 98 +#define OWERROR_SERVICE_DATA_NOT_UPDATED 99 +#define OWERROR_CATASTROPHIC_SERVICE_FAILURE 100 +#define OWERROR_LOAD_FIRST_SECRET_FAILED 101 +#define OWERROR_MATCH_SERVICE_SIGNATURE_FAILED 102 +#define OWERROR_KEY_OUT_OF_RANGE 103 +#define OWERROR_BLOCK_ID_OUT_OF_RANGE 104 +#define OWERROR_PASSWORDS_ENABLED 105 +#define OWERROR_PASSWORD_INVALID 106 +#define OWERROR_NO_READ_ONLY_PASSWORD 107 +#define OWERROR_NO_READ_WRITE_PASSWORD 108 +#define OWERROR_OW_SHORTED 109 +#define OWERROR_ADAPTER_ERROR 110 +#define OWERROR_EOP_COPY_SCRATCHPAD_FAILED 111 +#define OWERROR_EOP_WRITE_SCRATCHPAD_FAILED 112 +#define OWERROR_HYGRO_STOP_MISSION_UNNECESSARY 113 +#define OWERROR_HYGRO_STOP_MISSION_ERROR 114 +#define OWERROR_PORTNUM_ERROR 115 +#define OWERROR_LEVEL_FAILED 116 +#define OWERROR_PASSWORD_NOT_SET 117 +#define OWERROR_LATCH_NOT_SET 118 +#define OWERROR_LIBUSB_OPEN_FAILED 119 +#define OWERROR_LIBUSB_DEVICE_ALREADY_OPENED 120 +#define OWERROR_LIBUSB_SET_CONFIGURATION_ERROR 121 +#define OWERROR_LIBUSB_CLAIM_INTERFACE_ERROR 122 +#define OWERROR_LIBUSB_SET_ALTINTERFACE_ERROR 123 +#define OWERROR_LIBUSB_NO_ADAPTER_FOUND 124 + +// One Wire functions defined in ownetu.c +SMALLINT owFirst(int portnum, SMALLINT do_reset, SMALLINT alarm_only); +SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only); +void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read); +void owFamilySearchSetup(int portnum, SMALLINT search_family); +void owSkipFamily(int portnum); +SMALLINT owAccess(int portnum); +SMALLINT owVerify(int portnum, SMALLINT alarm_only); +SMALLINT owOverdriveAccess(int portnum); + + +// external One Wire functions defined in owsesu.c + SMALLINT owAcquire(int portnum, char *port_zstr); + int owAcquireEx(char *port_zstr); + void owRelease(int portnum); + +// external One Wire functions defined in findtype.c +// SMALLINT FindDevices(int,uchar FamilySN[][8],SMALLINT,int); + +// external One Wire functions from link layer owllu.c +SMALLINT owTouchReset(int portnum); +SMALLINT owTouchBit(int portnum, SMALLINT sendbit); +SMALLINT owTouchByte(int portnum, SMALLINT sendbyte); +SMALLINT owWriteByte(int portnum, SMALLINT sendbyte); +SMALLINT owReadByte(int portnum); +SMALLINT owSpeed(int portnum, SMALLINT new_speed); +SMALLINT owLevel(int portnum, SMALLINT new_level); +SMALLINT owProgramPulse(int portnum); +SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte); +SMALLINT owReadBytePower(int portnum); +SMALLINT owHasPowerDelivery(int portnum); +SMALLINT owHasProgramPulse(int portnum); +SMALLINT owHasOverDrive(int portnum); +SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse); +// external One Wire global from owllu.c +extern SMALLINT FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE; + +// external One Wire functions from transaction layer in owtrnu.c +SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len); +SMALLINT owReadPacketStd(int portnum, SMALLINT do_access, int start_page, uchar *read_buf); +SMALLINT owWritePacketStd(int portnum, int start_page, uchar *write_buf, + SMALLINT write_len, SMALLINT is_eprom, SMALLINT crc_type); +SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd, + SMALLINT crc_type, SMALLINT do_access); + +// link functions +void msDelay(int len); +long msGettick(void); + +// ioutil.c functions prototypes +int EnterString(char *msg, char *buf, int min, int max); +int EnterNum(char *msg, int numchars, long *value, long min, long max); +int EnterHex(char *msg, int numchars, ulong *value); +int ToHex(char ch); +int getkeystroke(void); +int key_abort(void); +void ExitProg(char *msg, int exit_code); +int getData(uchar *write_buff, int max_len, SMALLINT gethex); +void PrintHex(uchar* buffer, int cnt); +void PrintChars(uchar* buffer, int cnt); +void PrintSerialNum(uchar* buffer); + +// external functions defined in crcutil.c +void setcrc16(int portnum, unsigned short reset); +unsigned short docrc16(int portnum, unsigned short cdata); +void setcrc8(int portnum, uchar reset); +uchar docrc8(int portnum, uchar x); + +#endif //OWNET_H
diff -r 000000000000 -r 1193dbfe28e2 Headers/readDS18B20.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Headers/readDS18B20.h Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,33 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +// --------------------------------------------------------------------------- +// +// temp10.h - Header to read the DS1920/DS1820 - temperature measurement. +// +// Version: 2.00 +// +// --------------------------------------------------------------------------- + +int ReadDS18B20(int,unsigned char *,float *); \ No newline at end of file
diff -r 000000000000 -r 1193dbfe28e2 Headers/readDS1920.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Headers/readDS1920.h Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,33 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +// --------------------------------------------------------------------------- +// +// temp10.h - Header to read the DS1920/DS1820 - temperature measurement. +// +// Version: 2.00 +// +// --------------------------------------------------------------------------- + +int ReadTemperature(int,unsigned char *,float *);
diff -r 000000000000 -r 1193dbfe28e2 crcutil.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crcutil.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,119 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//-------------------------------------------------------------------------- +// +// crcutil.c - Keeps track of the CRC for 16 and 8 bit operations +// version 2.00 + +// Include files +#include "ownet.h" + +// Local global variables +unsigned short utilcrc16[MAX_PORTNUM]; +uchar utilcrc8[MAX_PORTNUM]; +static short oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; +static uchar dscrc_table[] = { + 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, + 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, + 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, + 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, + 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, + 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, + 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, + 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, + 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, + 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, + 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, + 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, + 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, + 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, + 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, + 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; + +//-------------------------------------------------------------------------- +// Reset crc16 to the value passed in +// +// 'reset' - data to set crc16 to. +// +void setcrc16(int portnum, unsigned short reset) +{ + utilcrc16[portnum&0x0FF] = reset; + return; +} + +//-------------------------------------------------------------------------- +// Reset crc8 to the value passed in +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'reset' - data to set crc8 to +// +void setcrc8(int portnum, uchar reset) +{ + utilcrc8[portnum&0x0FF] = reset; + return; +} + +//-------------------------------------------------------------------------- +// Calculate a new CRC16 from the input data short. Return the current +// CRC16 and also update the global variable CRC16. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'data' - data to perform a CRC16 on +// +// Returns: the current CRC16 +// +unsigned short docrc16(int portnum, unsigned short cdata) +{ + cdata = (cdata ^ (utilcrc16[portnum&0x0FF] & 0xff)) & 0xff; + utilcrc16[portnum&0x0FF] >>= 8; + + if (oddparity[cdata & 0xf] ^ oddparity[cdata >> 4]) + utilcrc16[portnum&0x0FF] ^= 0xc001; + + cdata <<= 6; + utilcrc16[portnum&0x0FF] ^= cdata; + cdata <<= 1; + utilcrc16[portnum&0x0FF] ^= cdata; + + return utilcrc16[portnum&0x0FF]; +} + +//-------------------------------------------------------------------------- +// Update the Dallas Semiconductor One Wire CRC (utilcrc8) from the global +// variable utilcrc8 and the argument. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'x' - data byte to calculate the 8 bit crc from +// +// Returns: the updated utilcrc8. +// +uchar docrc8(int portnum, uchar x) +{ + utilcrc8[portnum&0x0FF] = dscrc_table[utilcrc8[portnum&0x0FF] ^ x]; + return utilcrc8[portnum&0x0FF]; +}
diff -r 000000000000 -r 1193dbfe28e2 ds2480ut.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds2480ut.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,238 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000v Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// ds2480ut.c - DS2480B utility functions. +// +// Version: 2.01 +// +// History: 1.00 -> 1.01 Default PDSRC changed from 0.83 to 1.37V/us +// in DS2480Detect. Changed to use msDelay instead +// of Delay. +// 1.01 -> 1.02 Changed global declarations from 'uchar' to 'int'. +// Changed DSO/WORT from 7 to 10us in DS2480Detect. +// 1.02 -> 1.03 Removed caps in #includes for Linux capatibility +// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for +// multiple ports. Changed W1LT to 8us. +// 2.00 -> 2.01 Added error handling. Added circular-include check. +// 2.01 -> 2.10 Added raw memory error handling and SMALLINT +// 2.10 -> 3.00 Added memory bank functionality +// Added file I/O operations +// 3.00 -> 3.10 Modified as to properly work with mbed and send code +// as defined by AN192 / DS2480B PDF's +// + +#include "ownet.h" +#include "./Headers/ds2480.h" + +// global DS2480B state +SMALLINT ULevel[MAX_PORTNUM]; // current DS2480B 1-Wire Net level +SMALLINT UBaud[MAX_PORTNUM]; // current DS2480B baud rate +SMALLINT UMode[MAX_PORTNUM]; // current DS2480B command or data mode state +SMALLINT USpeed[MAX_PORTNUM]; // current DS2480B 1-Wire Net communication speed +SMALLINT UVersion[MAX_PORTNUM]; // current DS2480B version + +//--------------------------------------------------------------------------- +// Attempt to resyc and detect a DS2480B +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE - DS2480B detected successfully +// FALSE - Could not detect DS2480B +// +SMALLINT DS2480Detect(int portnum) +{ + uchar sendpacket[10],readbuffer[10]; + uchar sendlen=0; + + // reset modes + UMode[portnum] = MODSEL_COMMAND; +/* has been changed, this is causing issue's when not sending RX invert using MBED + UBaud[portnum] = PARMSET_INV_9600; */ + UBaud[portnum] = PARMSET_9600; + USpeed[portnum] = SPEEDSEL_FLEX; + + // set the baud rate to 9600 + SetBaudCOM(portnum,(uchar)UBaud[portnum]); + + // send a break to reset the DS2480 + BreakCOM(portnum); + + // delay to let line settle + msDelay(2); + + // flush the buffers + FlushCOM(portnum); + + // send the reset byte + sendpacket[0] = 0xC1; + if (WriteCOM(portnum,1,sendpacket) != 1) + { + OWERROR(OWERROR_WRITECOM_FAILED); + return FALSE; + } + + // delay to let line settle + msDelay(2); + +/* has been removed as this is causing issue's when used with mbed + // send the Baud Rate byte, which also inverts RX signal + sendpacket[0] = 0x79; + if (WriteCOM(portnum,1,sendpacket) != 1) + { + OWERROR(OWERROR_WRITECOM_FAILED); + return FALSE; + } + + // delay to let line settle + msDelay(4); + */ + // set the FLEX configuration parameters + // default PDSRC = 1.37Vus + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SLEW | PARMSET_Slew1p37Vus; + // default W1LT = 10us + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_WRITE1LOW | PARMSET_Write10us; + // default DSO/WORT = 8us + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_SAMPLEOFFSET | PARMSET_SampOff8us; + // construct the command to read the baud rate (to test command block) + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3); + // also do 1 bit operation (to test 1-Wire block) + sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | UBaud[portnum] | BITPOL_ONE; + + // flush the buffers + FlushCOM(portnum); + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the response + if (ReadCOM(portnum,5,readbuffer) == 5) + { + // look at the baud rate and bit operation + // to see if the response makes sense + if (((readbuffer[3] & 0xF1) == 0x00) && + ((readbuffer[3] & 0x0E) == UBaud[portnum]) && + ((readbuffer[4] & 0xF0) == 0x90) && + ((readbuffer[4] & 0x0C) == UBaud[portnum])) + return TRUE; + else + OWERROR(OWERROR_DS2480_BAD_RESPONSE); + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + return FALSE; +} + +//--------------------------------------------------------------------------- +// Change the DS2480B from the current baud rate to the new baud rate. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'newbaud' - the new baud rate to change to, defined as: +// PARMSET_9600 0x00 +// PARMSET_19200 0x02 +// PARMSET_57600 0x04 +// PARMSET_115200 0x06 +// +// Returns: current DS2480B baud rate. +// +SMALLINT DS2480ChangeBaud(int portnum, uchar newbaud) +{ + uchar rt=FALSE; + uchar readbuffer[5],sendpacket[5],sendpacket2[5]; + uchar sendlen=0,sendlen2=0; + + // see if diffenent then current baud rate + if (UBaud[portnum] == newbaud) + return UBaud[portnum]; + else + { + // build the command packet + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + // build the command + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_BAUDRATE | newbaud; + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (!WriteCOM(portnum,sendlen,sendpacket)) + { + OWERROR(OWERROR_WRITECOM_FAILED); + rt = FALSE; + } + else + { + // make sure buffer is flushed + msDelay(5); + + // change our baud rate + SetBaudCOM(portnum,newbaud); + UBaud[portnum] = newbaud; + + // wait for things to settle + msDelay(5); + + // build a command packet to read back baud rate + sendpacket2[sendlen2++] = CMD_CONFIG | PARMSEL_PARMREAD | (PARMSEL_BAUDRATE >> 3); + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen2,sendpacket2)) + { + // read back the 1 byte response + if (ReadCOM(portnum,1,readbuffer) == 1) + { + // verify correct baud + if (((readbuffer[0] & 0x0E) == (sendpacket[sendlen-1] & 0x0E))) + rt = TRUE; + else + OWERROR(OWERROR_DS2480_WRONG_BAUD); + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + } + } + + // if lost communication with DS2480 then reset + if (rt != TRUE) + DS2480Detect(portnum); + + return UBaud[portnum]; +}
diff -r 000000000000 -r 1193dbfe28e2 findtype.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/findtype.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,78 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// findtype.c - Test module to find all devices of one type. +// +// Version: 2.00 +// +//---------------------------------------------------------------------- +// +// +#include "ownet.h" +#include "./Headers/findtype.h" + +//---------------------------------------------------------------------- +// Search for devices +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'FamilySN' - an array of all the serial numbers with the matching +// family code +// 'family_code' - the family code of the devices to search for on the +// 1-Wire Net +// 'MAXDEVICES' - the maximum number of devices to look for with the +// family code passed. +// +// Returns: TRUE(1) success, device type found +// FALSE(0) device not found +// +SMALLINT FindDevices(int portnum, uchar FamilySN[][8], SMALLINT family_code, int MAXDEVICES) +{ + int NumDevices=0; + + // find the devices + // set the search to first find that family code + owFamilySearchSetup(portnum,family_code); + + // loop to find all of the devices up to MAXDEVICES + NumDevices = 0; + do + { + // perform the search + if (!owNext(portnum,TRUE, FALSE)) + break; + + owSerialNum(portnum,FamilySN[NumDevices], TRUE); + if ((FamilySN[NumDevices][0] & 0x7F) == (family_code & 0x7F)) + { + NumDevices++; + } + } + while (NumDevices < (MAXDEVICES - 1)); + + // check if not at least 1 device + return NumDevices; +}
diff -r 000000000000 -r 1193dbfe28e2 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,43 @@ +#include "mbed.h" +#include "ds2480.h" +#include "ownet.h" +#include "readDS18B20.h" +#include "findtype.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); + +// defines +#define MAXDEVICES 20 +// global serial numbers +uchar FamilySN1[MAXDEVICES][8]; + +int main() { + + float temp = 0; + int NumDevices = 0; + + if ( DS2480Detect(3) ) { + + NumDevices = FindDevices(3, &FamilySN1[0], 0x10, MAXDEVICES); + + printf("dev: %d\r\n", NumDevices); + printf("sn: "); + PrintSerialNum(FamilySN1[0]); + printf("\r\n"); + led1 = 0; + while(1) { + + if (ReadDS18B20(3, FamilySN1[0],&temp)) { + printf(" %7.3f %cC\r\n", temp, 0xf8); + } else { + printf(" Error, reading temperature\r\n"); + } + + led1 != led1; + wait(1); + } + } else { + printf(" Error, no DS2480 controller found\r\n"); + } +} \ No newline at end of file
diff -r 000000000000 -r 1193dbfe28e2 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
diff -r 000000000000 -r 1193dbfe28e2 mbedlnk.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbedlnk.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,493 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// TODO.C - COM functions required to communicate with the DS2480 based +// Universal Serial Adapter 'U'. Fill in the platform specific code. +// +// Version: 2.00 +// +// History: 1.00 -> 1.01 Added function msDelay. +// +// 1.01 -> 1.02 Changed to generic OpenCOM/CloseCOM for easier +// use with other platforms. +// +// 1.02 -> 1.03 Removed caps in #includes for Linux capatibility +// Add function msGettick() +// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for +// multiple ports. +// 2.00 -> 2.10 some changes made as to allow MBED usage and multiple ports +// +// work to be done, evaluate functions at been removed / flagged +// as text if migrate to mbed is meaningfull / needed +// + +//#include "onewire_config.h" // missing include file, handled locally now + +#include "ownet.h" +#include "mbed.h" +#include <string> +using namespace std; + +#define ONEWIRE_TX p9 +#define ONEWIRE_RX p10 + +Serial com1( ONEWIRE_TX , ONEWIRE_RX ); // uart 1 = p13_p14, tx, rx +Serial com2( ONEWIRE_TX , ONEWIRE_RX ); // uart 2 = p28_p27, tx, rx +Serial com3( ONEWIRE_TX , ONEWIRE_RX ); // uart 3 = p9_p10, tx, rx + +// exportable functions required +SMALLINT OpenCOM(int,char *); +SMALLINT WriteCOM(int,int,uchar *); +void FlushCOM(int); +int ReadCOM(int,int,uchar *); +void BreakCOM(int); +void SetBaudCOM(int,int); +void CloseCOM(int); +long msGettick(void); +void msDelay(int); +SMALLINT owHasPowerDelivery(int); +SMALLINT owHasOverDrive(int); +SMALLINT owHasProgramPulse(int); +SMALLINT owWriteBytePower(int,SMALLINT); +SMALLINT owReadBitPower(int,SMALLINT); + + +//--------------------------------------------------------------------------- +// Attempt to open a com port. Keep the handle in ComID. +// Set the starting baud rate to 9600. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number provided will +// be used to indicate the port number desired when calling +// all other functions in this library. +// +// 'port_zstr' - zero terminate port name. For this platform +// use format COMX where X is the port number. +// NOTE: not being used by MBED +// +// +// Returns: TRUE(1) - success, COM port opened +// FALSE(0) - failure, could not open specified port +// +SMALLINT OpenCOM(int portnum, char *port_zstr) { + + switch (portnum) { + case 1: + com1.baud( 9600); + break; + case 2: + com2.baud( 9600); + break; + case 3: + com3.baud( 9600); + break; + default: + return 0; + } + return 1; +} + +//--------------------------------------------------------------------------- +// Attempt to open a com port. Keep the handle in ComID. +// Set the starting baud rate to 9600. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number provided will +// be used to indicate the port number desired when calling +// all other functions in this library. +// +// +// Returns: the port number if it was successful otherwise -1 +// +int OpenCOMEx(char *port_zstr) { + + int portnum = -1; + + if ( port_zstr[0] == '1') { + portnum = 1; + } + + if ( port_zstr[0] == '2') { + portnum = 2; + } + + if ( port_zstr[0] == '3') { + portnum = 3; + } + + if(!OpenCOM(portnum, port_zstr)) { + return -1; + } + return portnum; +} + +//--------------------------------------------------------------------------- +// Closes the connection to the port. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +void CloseCOM(int portnum) { + // no need to implement, mbed doesn't support close ! +} + +//--------------------------------------------------------------------------- +// Flush the rx and tx buffers +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +void FlushCOM(int portnum) { + + switch (portnum) { + case 1: + // Flush TX & RX FIFO buffer + LPC_UART1->FCR |= 0x06; + break; + case 2: + // Flush TX & RX FIFO buffer + LPC_UART2->FCR |= 0x06; + break; + case 3: + // Flush TX & RX FIFO buffer + LPC_UART3->FCR |= 0x06; + break; + } +} + +//-------------------------------------------------------------------------- +// Write an array of bytes to the COM port, verify that it was +// sent out. Assume that baud rate has been set. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'outlen' - number of bytes to write to COM port +// 'outbuf' - pointer ot an array of bytes to write +// +// Returns: TRUE(1) - success +// FALSE(0) - failure +// +SMALLINT WriteCOM(int portnum, int outlen, uchar *outbuf) { + + //printf("port-%d\r\n",portnum); + for ( int x=0; x < outlen; x++ ) + { + switch (portnum) { + case 1: + if (com1.writeable()) { + com1.putc( outbuf[x]); + } + break; + case 2: + if (com2.writeable()) { + com2.putc( outbuf[x]); + } + break; + case 3: + if (com3.writeable()) { + com3.putc( outbuf[x]); + //printf("idx-%d, char-%2x\r\n", x, outbuf[x]); + } + break; + } + wait_ms(1); + } + + switch (portnum) { + case 1: + while ( (LPC_UART1->LSR & 0x40) == 0 ) {} + break; + case 2: + while ( (LPC_UART2->LSR & 0x40) == 0 ) {} + break; + case 3: + while ( (LPC_UART3->LSR & 0x40) == 0 ) {} + break; + } + return 1; +} + +//-------------------------------------------------------------------------- +// Read an array of bytes from the COM port, verify that it was +// received. Assume that baud rate has been set. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'inlen' - number of bytes to read from COM port +// 'inbuf' - pointer to a buffer to hold the incoming bytes +// +// Returns: number of characters read +// +int ReadCOM(int portnum, int inlen, unsigned char *inbuf) { + + string buffer (""); + Timer t; + int numchar = 0; + int read =0; + + t.start(); // Start timeout timer + + while ( t.read_ms() < 1000 ) { // If we haven't timed out.... + switch (portnum) { + case 1: + if (com1.readable()) { + read = com1.getc(); + buffer += read; + numchar++; + t.reset(); + } + break; + case 2: + if (com2.readable()) { + read = com2.getc(); + buffer += read; + numchar++; + t.reset(); + } + break; + case 3: + if (com3.readable()) { + read = com3.getc(); + buffer += read; + numchar++; + t.reset(); + } + break; + } + + if (numchar == inlen) + { + break; + } + } + + buffer.copy( (char*)inbuf, numchar ); + + return numchar; +} + +//-------------------------------------------------------------------------- +// Send a break on the com port for at least 2 ms +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +void BreakCOM(int portnum) { + int BREAK_time = 2; + + switch (portnum) { + case 1: + // Send Line Break + LPC_UART1->LCR |= 0x40; + wait_ms( BREAK_time ); + // Mark after break + LPC_UART1->LCR &= ~(0x40); + break; + case 2: + // Send Line Break + LPC_UART2->LCR |= 0x40; + wait_ms( BREAK_time ); + // Mark after break + LPC_UART2->LCR &= ~(0x40); + break; + case 3: + // Send Line Break + LPC_UART3->LCR |= 0x40; + wait_ms( BREAK_time ); + // Mark after break + LPC_UART3->LCR &= ~(0x40); + break; + default: + break; + } + + wait_us( BREAK_time ); +} + +//-------------------------------------------------------------------------- +// Set the baud rate on the com port. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'new_baud' - new baud rate defined as +// PARMSET_9600 0x00 +// PARMSET_19200 0x02 +// PARMSET_57600 0x04 +// PARMSET_115200 0x06 +// +void SetBaudCOM(int portnum, int new_baud) { + + int baud_rate = 9600; + + switch (new_baud) { + case 0: + baud_rate = 9600; + break; + case 2: + baud_rate = 19200; + break; + case 4: + baud_rate = 57600; + break; + case 6: + baud_rate = 115200; + break; + } + + switch (portnum) { + + case 1: + com1.baud(baud_rate); + break; + case 2: + com2.baud(baud_rate); + break; + case 3: + com2.baud(baud_rate); + break; + } +} + +//-------------------------------------------------------------------------- +// Description: +// Delay for at least 'len' ms +// +void msDelay(int len) +{ + wait_ms( len ); +} + +//-------------------------------------------------------------------------- +// Get the current millisecond tick count. Does not have to represent +// an actual time, it just needs to be an incrementing timer. +// +long msGettick(void) { + // add platform specific code here + return 0; +} + +/* from here onwards functions have been removed, further evaluation needed +//-------------------------------------------------------------------------- +// This procedure indicates wether the adapter can deliver power. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE if adapter is capable of delivering power. +// +SMALLINT owHasPowerDelivery(int portnum) { + + // add adapter specific code here + return TRUE; +} + +//-------------------------------------------------------------------------- +// This procedure indicates wether the adapter can deliver power. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE if adapter is capable of over drive. +// +SMALLINT owHasOverDrive(int portnum) { + + // add adapter specific code here + return TRUE; +} + +//-------------------------------------------------------------------------- +// This procedure creates a fixed 480 microseconds 12 volt pulse +// on the 1-Wire Net for programming EPROM iButtons. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE program volatage available +// FALSE program voltage not available +SMALLINT owHasProgramPulse(int portnum) +{ + // add adapter specific code here + return TRUE; +} + + +//-------------------------------------------------------------------------- +// Send 8 bits of communication to the 1-Wire Net and verify that the +// 8 bits read from the 1-Wire Net is the same (write operation). +// The parameter 'sendbyte' least significant 8 bits are used. After the +// 8 bits are sent change the level of the 1-Wire net. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'sendbyte' - 8 bits to send (least significant byte) +// +// Returns: TRUE: bytes written and echo was the same +// FALSE: echo was not the same +// +SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte) +{ + // replace if platform has better implementation (faster response) + if (!owHasPowerDelivery(portnum)) + return FALSE; + + if(owTouchByte(portnum,sendbyte) != sendbyte) + return FALSE; + + if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5) + return FALSE; + + return TRUE; +} + + +//-------------------------------------------------------------------------- +// Send 1 bit of communication to the 1-Wire Net and verify that the +// response matches the 'applyPowerResponse' bit and apply power delivery +// to the 1-Wire net. Note that some implementations may apply the power +// first and then turn it off if the response is incorrect. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'applyPowerResponse' - 1 bit response to check, if correct then start +// power delivery +// +// Returns: TRUE: bit written and response correct, strong pullup now on +// FALSE: response incorrect +// +SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse) +{ + // replace if platform has better implementation (faster response) + if (!owHasPowerDelivery(portnum)) + return FALSE; + + if(owTouchBit(portnum,0x01) != applyPowerResponse) + return FALSE; + + if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5) + return FALSE; + + return TRUE; +} + +*/ \ No newline at end of file
diff -r 000000000000 -r 1193dbfe28e2 owTemp.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/owTemp.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,141 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// temp.c - Application to find and read the 1-Wire Net +// DS1920/DS1820/DS18S20 - temperature measurement. +// +// This application uses the files from the 'Public Domain' +// 1-Wire Net libraries ('general' and 'userial'). +// +// +// Version: 2.00 +// + +#include <stdlib.h> +#include <stdio.h> +#include "owTemp.h" +#include "ownet.h" +#include "readDS1920.h" +#include "findtype.h" + +// defines +#define MAXDEVICES 20 + +// global serial numbers +uchar FamilySN[MAXDEVICES][8]; + +// variables +int family_code; + + + +owTemp::owTemp () +{ + + +} + + +void PrintSerialNum(uchar* buffer) +{ + int i; + for(i=7; i>=0; i--) + { + printf("%02X", buffer[i]); + } +} + + +//---------------------------------------------------------------------- +// Main Test for DS1920/DS1820 temperature measurement +// +void owTemp::Maingettemp( void ) +{ + float current_temp; + int i = 0; + int NumDevices=0; + int portnum = 0; + + + //---------------------------------------- + // Introduction header + printf("\r\n/---------------------------------------------\r\n"); + printf(" Temperature application DS1920/DS1820 - Version 1.00 \r\n" + " The following is a test to exercise a DS1920/DS1820.\r\n" + " Temperature Find and Read from a: \r\n" + " DS1920/DS1820 (at least 1)\r\n\r\n"); + + printf(" Press any CTRL-C to stop this program.\r\n\r\n"); + printf(" Output [Serial Number(s) ........ Temp1(F)] \r\n\r\n"); + + // attempt to acquire the 1-Wire Net + portnum = owAcquireEx("1"); + + // success + printf("Port opened: %i\r\n", portnum); + + // Find the device(s) + NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES); + + if (NumDevices>0) + { + printf("\r\n"); + printf("Device(s) Found: \r\n"); + for (i = 0; i < NumDevices; i++) + { + PrintSerialNum(FamilySN[i]); + printf("\r\n"); + } + printf("\r\n\r\n"); + + while (1) + { + // read the temperature and print serial number and temperature + for (i = 0; i < NumDevices; i++) + { + if (ReadTemperature(portnum, FamilySN[i],¤t_temp)) + { + float far = current_temp * 9.0 / 5.0 + 32.0; + PrintSerialNum(FamilySN[i]); + printf(" %5.1f Degrees Farenheight\r\n", far); + // converting temperature from Celsius to Fahrenheit + } + else + printf(" Error reading temperature, verify device present:%d\r\n", + (int)owVerify(portnum, FALSE)); + } + printf("\r\n"); + } + + } + else + printf("\r\n\r\n\r\nERROR, device DS1920/DS1820 not found!\r\n"); + + // release the 1-Wire Net + owRelease(portnum); +} +
diff -r 000000000000 -r 1193dbfe28e2 owllu.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/owllu.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,838 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// owLLU.C - Link Layer 1-Wire Net functions using the DS2480/DS2480B (U) +// serial interface chip. +// +// Version: 3.00 +// +// History: 1.00 -> 1.01 DS2480 version number now ignored in +// owTouchReset. +// 1.02 -> 1.03 Removed caps in #includes for Linux capatibility +// Removed #include <windows.h> +// Add #include "ownet.h" to define TRUE,FALSE +// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for +// multiple ports. +// 2.00 -> 2.01 Added error handling. Added circular-include check. +// 2.01 -> 2.10 Added raw memory error handling and SMALLINT +// 2.10 -> 3.00 Added memory bank functionality +// Added file I/O operations +// Added owReadBitPower and owWriteBytePower +// Added support for THE LINK +// Updated owLevel to match AN192 +// + +#include "ownet.h" +#include "ds2480.h" + +int dodebug=0; + +// external globals +extern SMALLINT ULevel[MAX_PORTNUM]; // current DS2480B 1-Wire Net level +extern SMALLINT UBaud[MAX_PORTNUM]; // current DS2480B baud rate +extern SMALLINT UMode[MAX_PORTNUM]; // current DS2480B command or data mode state +extern SMALLINT USpeed[MAX_PORTNUM]; // current DS2480B 1-Wire Net communication speed +extern SMALLINT UVersion[MAX_PORTNUM]; // current DS2480B version + +// new global for DS1994/DS2404/DS1427. If TRUE, puts a delay in owTouchReset to compensate for alarming clocks. +SMALLINT FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE = FALSE; // default owTouchReset to quickest response. + +// local varable flag, true if program voltage available +static SMALLINT ProgramAvailable[MAX_PORTNUM]; + +//-------------------------------------------------------------------------- +// Reset all of the devices on the 1-Wire Net and return the result. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE(1): presense pulse(s) detected, device(s) reset +// FALSE(0): no presense pulses detected +// +// WARNING: Without setting the above global (FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE) +// to TRUE, this routine will not function correctly on some +// Alarm reset types of the DS1994/DS1427/DS2404 with +// Rev 1,2, and 3 of the DS2480/DS2480B. +// +// +SMALLINT owTouchReset(int portnum) +{ + uchar readbuffer[10],sendpacket[10]; + uchar sendlen=0; + + if (dodebug) + printf("\nRST ");//?????????????? + + // make sure normal level + owLevel(portnum,MODE_NORMAL); + + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // construct the command + sendpacket[sendlen++] = (uchar)(CMD_COMM | FUNCTSEL_RESET | USpeed[portnum]); + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 1 byte response + if (ReadCOM(portnum,1,readbuffer) == 1) + { + // make sure this byte looks like a reset byte + if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) || + ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE)) + { + // check if programming voltage available + ProgramAvailable[portnum] = ((readbuffer[0] & 0x20) == 0x20); + UVersion[portnum] = (readbuffer[0] & VERSION_MASK); + + // only check for alarm pulse if DS2404 present and not using THE LINK + if ((FAMILY_CODE_04_ALARM_TOUCHRESET_COMPLIANCE) && + (UVersion[portnum] != VER_LINK)) + { + msDelay(5); // delay 5 ms to give DS1994 enough time + FlushCOM(portnum); + } + return TRUE; + } + else + OWERROR(OWERROR_RESET_FAILED); + + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // an error occured so re-sync with DS2480 + DS2480Detect(portnum); + + return FALSE; +} + +//-------------------------------------------------------------------------- +// Send 1 bit of communication to the 1-Wire Net and return the +// result 1 bit read from the 1-Wire Net. The parameter 'sendbit' +// least significant bit is used and the least significant bit +// of the result is the return bit. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'sendbit' - the least significant bit is the bit to send +// +// Returns: 0: 0 bit read from sendbit +// 1: 1 bit read from sendbit +// +SMALLINT owTouchBit(int portnum, SMALLINT sendbit) +{ + uchar readbuffer[10],sendpacket[10]; + uchar sendlen=0; + + // make sure normal level + owLevel(portnum,MODE_NORMAL); + + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // construct the command + sendpacket[sendlen] = (sendbit != 0) ? BITPOL_ONE : BITPOL_ZERO; + sendpacket[sendlen++] |= CMD_COMM | FUNCTSEL_BIT | USpeed[portnum]; + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the response + if (ReadCOM(portnum,1,readbuffer) == 1) + { + // interpret the response + if (((readbuffer[0] & 0xE0) == 0x80) && + ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE)) + return 1; + else + return 0; + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // an error occured so re-sync with DS2480 + DS2480Detect(portnum); + + return 0; +} + +//-------------------------------------------------------------------------- +// Send 8 bits of communication to the 1-Wire Net and verify that the +// 8 bits read from the 1-Wire Net is the same (write operation). +// The parameter 'sendbyte' least significant 8 bits are used. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'sendbyte' - 8 bits to send (least significant byte) +// +// Returns: TRUE: bytes written and echo was the same +// FALSE: echo was not the same +// +SMALLINT owWriteByte(int portnum, SMALLINT sendbyte) +{ + return (owTouchByte(portnum,sendbyte) == (0xff & sendbyte)) ? TRUE : FALSE; +} + + +//-------------------------------------------------------------------------- +// Send 8 bits of read communication to the 1-Wire Net and and return the +// result 8 bits read from the 1-Wire Net. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: 8 bits read from 1-Wire Net +// +SMALLINT owReadByte(int portnum) +{ + return owTouchByte(portnum,(SMALLINT)0xFF); +} + +//-------------------------------------------------------------------------- +// Send 8 bits of communication to the 1-Wire Net and return the +// result 8 bits read from the 1-Wire Net. The parameter 'sendbyte' +// least significant 8 bits are used and the least significant 8 bits +// of the result is the return byte. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'sendbyte' - 8 bits to send (least significant byte) +// +// Returns: 8 bits read from sendbyte +// +SMALLINT owTouchByte(int portnum, SMALLINT sendbyte) +{ + uchar readbuffer[10],sendpacket[10]; + uchar sendlen=0; + + // make sure normal level + owLevel(portnum,MODE_NORMAL); + + // check if correct mode + if (UMode[portnum] != MODSEL_DATA) + { + UMode[portnum] = MODSEL_DATA; + sendpacket[sendlen++] = MODE_DATA; + } + + // add the byte to send + sendpacket[sendlen++] = (uchar)sendbyte; + + // check for duplication of data that looks like COMMAND mode + if (sendbyte ==(SMALLINT)MODE_COMMAND) + sendpacket[sendlen++] = (uchar)sendbyte; + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 1 byte response + if (ReadCOM(portnum,1,readbuffer) == 1) + { + if (dodebug) + printf("%02X ",readbuffer[0]);//?????????????? + + // return the response + return (int)readbuffer[0]; + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // an error occured so re-sync with DS2480 + DS2480Detect(portnum); + + return 0; +} + +//-------------------------------------------------------------------------- +// Set the 1-Wire Net communucation speed. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'new_speed' - new speed defined as +// MODE_NORMAL 0x00 +// MODE_OVERDRIVE 0x01 +// +// Returns: current 1-Wire Net speed +// +SMALLINT owSpeed(int portnum, SMALLINT new_speed) +{ + uchar sendpacket[5]; + uchar sendlen=0; + uchar rt = FALSE; + + // check if change from current mode + if (((new_speed == MODE_OVERDRIVE) && + (USpeed[portnum] != SPEEDSEL_OD)) || + ((new_speed == MODE_NORMAL) && + (USpeed[portnum] != SPEEDSEL_FLEX))) + { + if (new_speed == MODE_OVERDRIVE) + { + // check for unsupported mode in THE LINK + if (UVersion[portnum] == VER_LINK) + { + OWERROR(OWERROR_FUNC_NOT_SUP); + // if overdrive then switch to higher baud + } + else + { + if (DS2480ChangeBaud(portnum,MAX_BAUD) == MAX_BAUD) + { + USpeed[portnum] = SPEEDSEL_OD; + rt = TRUE; + } + } + } + else if (new_speed == MODE_NORMAL) + { + // else normal so set to 9600 baud + if (DS2480ChangeBaud(portnum,PARMSET_9600) == PARMSET_9600) + { + USpeed[portnum] = SPEEDSEL_FLEX; + rt = TRUE; + } + + } + + // if baud rate is set correctly then change DS2480 speed + if (rt) + { + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // proceed to set the DS2480 communication speed + sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_SEARCHOFF | USpeed[portnum]; + + // send the packet + if (!WriteCOM(portnum,sendlen,sendpacket)) + { + OWERROR(OWERROR_WRITECOM_FAILED); + rt = FALSE; + // lost communication with DS2480 then reset + DS2480Detect(portnum); + } + } + } + + // return the current speed + return (USpeed[portnum] == SPEEDSEL_OD) ? MODE_OVERDRIVE : MODE_NORMAL; +} + +//-------------------------------------------------------------------------- +// Set the 1-Wire Net line level. The values for new_level are +// as follows: +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'new_level' - new level defined as +// MODE_NORMAL 0x00 +// MODE_STRONG5 0x02 +// MODE_PROGRAM 0x04 +// MODE_BREAK 0x08 (not supported) +// +// Returns: current 1-Wire Net level +// +SMALLINT owLevel(int portnum, SMALLINT new_level) +{ + uchar sendpacket[10],readbuffer[10]; + uchar sendlen=0; + uchar rt=FALSE; + + // check if need to change level + if (new_level != ULevel[portnum]) + { + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // check if just putting back to normal + if (new_level == MODE_NORMAL) + { + // stop pulse command + sendpacket[sendlen++] = MODE_STOP_PULSE; + + // add the command to begin the pulse WITHOUT prime + sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V | PRIME5V_FALSE; + + // stop pulse command + sendpacket[sendlen++] = MODE_STOP_PULSE; + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 2 byte response + if (ReadCOM(portnum,2,readbuffer) == 2) + { + // check response byte + if (((readbuffer[0] & 0xE0) == 0xE0) && + ((readbuffer[1] & 0xE0) == 0xE0)) + { + rt = TRUE; + ULevel[portnum] = MODE_NORMAL; + } + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + } + // set new level + else + { + // strong 5 volts + if (new_level == MODE_STRONG5) + { + // set the SPUD time value + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; + // add the command to begin the pulse + sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V; + } + // 12 volts + else if (new_level == MODE_PROGRAM) + { + // check if programming voltage available + if (!ProgramAvailable[portnum]) + return MODE_NORMAL; + + // set the PPD time value + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_infinite; + // add the command to begin the pulse + sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_12V; + } + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 1 byte response from setting time limit + if (ReadCOM(portnum,1,readbuffer) == 1) + { + // check response byte + if ((readbuffer[0] & 0x81) == 0) + { + ULevel[portnum] = new_level; + rt = TRUE; + } + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + } + + // if lost communication with DS2480 then reset + if (rt != TRUE) + DS2480Detect(portnum); + } + + // return the current level + return ULevel[portnum]; +} + +//-------------------------------------------------------------------------- +// This procedure creates a fixed 480 microseconds 12 volt pulse +// on the 1-Wire Net for programming EPROM iButtons. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE successful +// FALSE program voltage not available +// +SMALLINT owProgramPulse(int portnum) +{ + uchar sendpacket[10],readbuffer[10]; + uchar sendlen=0; + + // check if programming voltage available + if (!ProgramAvailable[portnum]) + return FALSE; + + // make sure normal level + owLevel(portnum,MODE_NORMAL); + + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // set the SPUD time value + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us; + + // pulse command + sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE; + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 2 byte response + if (ReadCOM(portnum,2,readbuffer) == 2) + { + // check response byte + if (((readbuffer[0] | CMD_CONFIG) == + (CMD_CONFIG | PARMSEL_12VPULSE | PARMSET_512us)) && + ((readbuffer[1] & 0xFC) == + (0xFC & (CMD_COMM | FUNCTSEL_CHMOD | BITPOL_12V | SPEEDSEL_PULSE)))) + return TRUE; + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // an error occured so re-sync with DS2480 + DS2480Detect(portnum); + + return FALSE; +} + +//-------------------------------------------------------------------------- +// Send 8 bits of communication to the 1-Wire Net and verify that the +// 8 bits read from the 1-Wire Net is the same (write operation). +// The parameter 'sendbyte' least significant 8 bits are used. After the +// 8 bits are sent change the level of the 1-Wire net. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'sendbyte' - 8 bits to send (least significant bit) +// +// Returns: TRUE: bytes written and echo was the same, strong pullup now on +// FALSE: echo was not the same +// +SMALLINT owWriteBytePower(int portnum, SMALLINT sendbyte) +{ + uchar sendpacket[10],readbuffer[10]; + uchar sendlen=0; + uchar rt=FALSE; + uchar i, temp_byte; + + if (dodebug) + printf("P%02X ",sendbyte);//?????????????? + + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // set the SPUD time value + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; + + // construct the stream to include 8 bit commands with the last one + // enabling the strong-pullup + temp_byte = sendbyte; + for (i = 0; i < 8; i++) + { + sendpacket[sendlen++] = ((temp_byte & 0x01) ? BITPOL_ONE : BITPOL_ZERO) + | CMD_COMM | FUNCTSEL_BIT | USpeed[portnum] | + ((i == 7) ? PRIME5V_TRUE : PRIME5V_FALSE); + temp_byte >>= 1; + } + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 9 byte response from setting time limit + if (ReadCOM(portnum,9,readbuffer) == 9) + { + // check response + if ((readbuffer[0] & 0x81) == 0) + { + // indicate the port is now at power delivery + ULevel[portnum] = MODE_STRONG5; + + // reconstruct the echo byte + temp_byte = 0; + for (i = 0; i < 8; i++) + { + temp_byte >>= 1; + temp_byte |= (readbuffer[i + 1] & 0x01) ? 0x80 : 0; + } + + if (temp_byte == sendbyte) + rt = TRUE; + } + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // if lost communication with DS2480 then reset + if (rt != TRUE) + DS2480Detect(portnum); + + return rt; +} + +//-------------------------------------------------------------------------- +// Send 8 bits of communication to the 1-Wire Net and verify that the +// 8 bits read from the 1-Wire Net is the same (write operation). +// The parameter 'sendbyte' least significant 8 bits are used. After the +// 8 bits are sent change the level of the 1-Wire net. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'sendbyte' - 8 bits to send (least significant bit) +// +// Returns: TRUE: bytes written and echo was the same, strong pullup now on +// FALSE: echo was not the same +// +SMALLINT owReadBytePower(int portnum) +{ + uchar sendpacket[10],readbuffer[10]; + uchar sendlen=0; + uchar rt=FALSE; + uchar i, temp_byte; + + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // set the SPUD time value + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; + + // construct the stream to include 8 bit commands with the last one + // enabling the strong-pullup + temp_byte = 0xFF; + for (i = 0; i < 8; i++) + { + sendpacket[sendlen++] = ((temp_byte & 0x01) ? BITPOL_ONE : BITPOL_ZERO) + | CMD_COMM | FUNCTSEL_BIT | USpeed[portnum] | + ((i == 7) ? PRIME5V_TRUE : PRIME5V_FALSE); + temp_byte >>= 1; + } + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 9 byte response from setting time limit + if (ReadCOM(portnum,9,readbuffer) == 9) + { + // check response + if ((readbuffer[0] & 0x81) == 0) + { + // indicate the port is now at power delivery + ULevel[portnum] = MODE_STRONG5; + + // reconstruct the return byte + temp_byte = 0; + for (i = 0; i < 8; i++) + { + temp_byte >>= 1; + temp_byte |= (readbuffer[i + 1] & 0x01) ? 0x80 : 0; + } + + rt = TRUE; + } + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // if lost communication with DS2480 then reset + if (rt != TRUE) + DS2480Detect(portnum); + + if (dodebug) + printf("PFF%02X ",temp_byte);//?????????????? + + return temp_byte; +} + + +//-------------------------------------------------------------------------- +// Send 1 bit of communication to the 1-Wire Net and verify that the +// response matches the 'applyPowerResponse' bit and apply power delivery +// to the 1-Wire net. Note that some implementations may apply the power +// first and then turn it off if the response is incorrect. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'applyPowerResponse' - 1 bit response to check, if correct then start +// power delivery +// +// Returns: TRUE: bit written and response correct, strong pullup now on +// FALSE: response incorrect +// +SMALLINT owReadBitPower(int portnum, SMALLINT applyPowerResponse) +{ + uchar sendpacket[3],readbuffer[3]; + uchar sendlen=0; + uchar rt=FALSE; + + // check if correct mode + if (UMode[portnum] != MODSEL_COMMAND) + { + UMode[portnum] = MODSEL_COMMAND; + sendpacket[sendlen++] = MODE_COMMAND; + } + + // set the SPUD time value + sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; + + // enabling the strong-pullup after bit + sendpacket[sendlen++] = BITPOL_ONE + | CMD_COMM | FUNCTSEL_BIT | USpeed[portnum] | + PRIME5V_TRUE; + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the 2 byte response from setting time limit + if (ReadCOM(portnum,2,readbuffer) == 2) + { + // check response to duration set + if ((readbuffer[0] & 0x81) == 0) + { + // indicate the port is now at power delivery + ULevel[portnum] = MODE_STRONG5; + + // check the response bit + if ((readbuffer[1] & 0x01) == applyPowerResponse) + rt = TRUE; + else + owLevel(portnum,MODE_NORMAL); + + return rt; + } + } + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // if lost communication with DS2480 then reset + if (rt != TRUE) + DS2480Detect(portnum); + + return rt; +} + + +//-------------------------------------------------------------------------- +// This procedure indicates whether the adapter can deliver power. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE because all userial adapters have over drive. +// +SMALLINT owHasPowerDelivery(int portnum) +{ + return TRUE; +} + +//-------------------------------------------------------------------------- +// This procedure indicates wether the adapter can deliver power. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE because all userial adapters have over drive. +// +SMALLINT owHasOverDrive(int portnum) +{ + return TRUE; +} +//-------------------------------------------------------------------------- +// This procedure creates a fixed 480 microseconds 12 volt pulse +// on the 1-Wire Net for programming EPROM iButtons. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +// Returns: TRUE program volatage available +// FALSE program voltage not available +SMALLINT owHasProgramPulse(int portnum) +{ + return ProgramAvailable[portnum]; +} +
diff -r 000000000000 -r 1193dbfe28e2 ownet.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ownet.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,538 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// ownet.C - Network functions for 1-Wire net devices. +// +// Version: 3.00 +// +// History: 1.00 -> 1.01 Change to owFamilySearchSetup, LastDiscrepancy[portnum] +// was set to 64 instead of 8 to enable devices with +// early contention to go in the '0' direction first. +// 1.02 -> 1.03 Initialized goodbits in owVerify +// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for +// multiple ports. +// 2.00 -> 2.01 Added support for owError library +// 2.01 -> 3.00 Make search functions consistent with AN187 +// + +#include <stdio.h> +#include "ownet.h" + +// exportable functions defined in ownet.c +SMALLINT bitacc(SMALLINT,SMALLINT,SMALLINT,uchar *); + +// global variables for this module to hold search state information +static SMALLINT LastDiscrepancy[MAX_PORTNUM]; +static SMALLINT LastFamilyDiscrepancy[MAX_PORTNUM]; +static SMALLINT LastDevice[MAX_PORTNUM]; +uchar SerialNum[MAX_PORTNUM][8]; + +//-------------------------------------------------------------------------- +// The 'owFirst' finds the first device on the 1-Wire Net This function +// contains one parameter 'alarm_only'. When +// 'alarm_only' is TRUE (1) the find alarm command 0xEC is +// sent instead of the normal search command 0xF0. +// Using the find alarm command 0xEC will limit the search to only +// 1-Wire devices that are in an 'alarm' state. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not +// perform reset before search. +// 'alarm_only' - TRUE (1) the find alarm command 0xEC is +// sent instead of the normal search command 0xF0 +// +// Returns: TRUE (1) : when a 1-Wire device was found and it's +// Serial Number placed in the global SerialNum[portnum] +// FALSE (0): There are no devices on the 1-Wire Net. +// +SMALLINT owFirst(int portnum, SMALLINT do_reset, SMALLINT alarm_only) +{ + // reset the search state + LastDiscrepancy[portnum] = 0; + LastDevice[portnum] = FALSE; + LastFamilyDiscrepancy[portnum] = 0; + + return owNext(portnum,do_reset,alarm_only); +} + +//-------------------------------------------------------------------------- +// The 'owNext' function does a general search. This function +// continues from the previos search state. The search state +// can be reset by using the 'owFirst' function. +// This function contains one parameter 'alarm_only'. +// When 'alarm_only' is TRUE (1) the find alarm command +// 0xEC is sent instead of the normal search command 0xF0. +// Using the find alarm command 0xEC will limit the search to only +// 1-Wire devices that are in an 'alarm' state. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'do_reset' - TRUE (1) perform reset before search, FALSE (0) do not +// perform reset before search. +// 'alarm_only' - TRUE (1) the find alarm command 0xEC is +// sent instead of the normal search command 0xF0 +// +// Returns: TRUE (1) : when a 1-Wire device was found and it's +// Serial Number placed in the global SerialNum[portnum] +// FALSE (0): when no new device was found. Either the +// last search was the last device or there +// are no devices on the 1-Wire Net. +// +SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only) +{ + uchar bit_test, search_direction, bit_number; + uchar last_zero, serial_byte_number, next_result; + uchar serial_byte_mask; + uchar lastcrc8=0; + + // initialize for search + bit_number = 1; + last_zero = 0; + serial_byte_number = 0; + serial_byte_mask = 1; + next_result = 0; + setcrc8(portnum,0); + + // if the last call was not the last one + if (!LastDevice[portnum]) + { + // check if reset first is requested + if (do_reset) + { + // reset the 1-wire + // if there are no parts on 1-wire, return FALSE + if (!owTouchReset(portnum)) + { + // printf("owTouchReset failed\r\n"); + // reset the search + LastDiscrepancy[portnum] = 0; + LastFamilyDiscrepancy[portnum] = 0; + OWERROR(OWERROR_NO_DEVICES_ON_NET); + return FALSE; + } + } + + // If finding alarming devices issue a different command + if (alarm_only) + owWriteByte(portnum,0xEC); // issue the alarming search command + else + owWriteByte(portnum,0xF0); // issue the search command + + //pause before beginning the search + //usDelay(100); + + // loop to do the search + do + { + // read a bit and its compliment + bit_test = owTouchBit(portnum,1) << 1; + bit_test |= owTouchBit(portnum,1); + + // check for no devices on 1-wire + if (bit_test == 3) + break; + else + { + // all devices coupled have 0 or 1 + if (bit_test > 0) + search_direction = !(bit_test & 0x01); // bit write value for search + else + { + // if this discrepancy if before the Last Discrepancy + // on a previous next then pick the same as last time + if (bit_number < LastDiscrepancy[portnum]) + search_direction = ((SerialNum[portnum][serial_byte_number] & serial_byte_mask) > 0); + else + // if equal to last pick 1, if not then pick 0 + search_direction = (bit_number == LastDiscrepancy[portnum]); + + // if 0 was picked then record its position in LastZero + if (search_direction == 0) + { + last_zero = bit_number; + + // check for Last discrepancy in family + if (last_zero < 9) + LastFamilyDiscrepancy[portnum] = last_zero; + } + } + + // set or clear the bit in the SerialNum[portnum] byte serial_byte_number + // with mask serial_byte_mask + if (search_direction == 1) + SerialNum[portnum][serial_byte_number] |= serial_byte_mask; + else + SerialNum[portnum][serial_byte_number] &= ~serial_byte_mask; + + // serial number search direction write bit + owTouchBit(portnum,search_direction); + + // increment the byte counter bit_number + // and shift the mask serial_byte_mask + bit_number++; + serial_byte_mask <<= 1; + + // if the mask is 0 then go to new SerialNum[portnum] byte serial_byte_number + // and reset mask + if (serial_byte_mask == 0) + { + // The below has been added to accomidate the valid CRC with the + // possible changing serial number values of the DS28E04. + if (((SerialNum[portnum][0] & 0x7F) == 0x1C) && (serial_byte_number == 1)) + lastcrc8 = docrc8(portnum,0x7F); + else + lastcrc8 = docrc8(portnum,SerialNum[portnum][serial_byte_number]); // accumulate the CRC + + serial_byte_number++; + serial_byte_mask = 1; + } + } + } + while(serial_byte_number < 8); // loop until through all SerialNum[portnum] bytes 0-7 + + // if the search was successful then + if (!((bit_number < 65) || lastcrc8)) + { + // search successful so set LastDiscrepancy[portnum],LastDevice[portnum],next_result + LastDiscrepancy[portnum] = last_zero; + LastDevice[portnum] = (LastDiscrepancy[portnum] == 0); + next_result = TRUE; + } + } + + // if no device found then reset counters so next 'next' will be + // like a first + if (!next_result || !SerialNum[portnum][0]) + { + LastDiscrepancy[portnum] = 0; + LastDevice[portnum] = FALSE; + LastFamilyDiscrepancy[portnum] = 0; + next_result = FALSE; + } + + return next_result; +} + +//-------------------------------------------------------------------------- +// The 'owSerialNum' function either reads or sets the SerialNum buffer +// that is used in the search functions 'owFirst' and 'owNext'. +// This function contains two parameters, 'serialnum_buf' is a pointer +// to a buffer provided by the caller. 'serialnum_buf' should point to +// an array of 8 unsigned chars. The second parameter is a flag called +// 'do_read' that is TRUE (1) if the operation is to read and FALSE +// (0) if the operation is to set the internal SerialNum buffer from +// the data in the provided buffer. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'serialnum_buf' - buffer to that contains the serial number to set +// when do_read = FALSE (0) and buffer to get the serial +// number when do_read = TRUE (1). +// 'do_read' - flag to indicate reading (1) or setting (0) the current +// serial number. +// +void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read) +{ + uchar i; + + // read the internal buffer and place in 'serialnum_buf' + if (do_read) + { + for (i = 0; i < 8; i++) + serialnum_buf[i] = SerialNum[portnum][i]; + } + // set the internal buffer from the data in 'serialnum_buf' + else + { + for (i = 0; i < 8; i++) + SerialNum[portnum][i] = serialnum_buf[i]; + } +} + +//-------------------------------------------------------------------------- +// Setup the search algorithm to find a certain family of devices +// the next time a search function is called 'owNext'. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'search_family' - family code type to set the search algorithm to find +// next. +// +void owFamilySearchSetup(int portnum, SMALLINT search_family) +{ + uchar i; + + // set the search state to find SearchFamily type devices + SerialNum[portnum][0] = search_family; + for (i = 1; i < 8; i++) + SerialNum[portnum][i] = 0; + LastDiscrepancy[portnum] = 64; + LastDevice[portnum] = FALSE; +} + +//-------------------------------------------------------------------------- +// Set the current search state to skip the current family code. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// +void owSkipFamily(int portnum) +{ + // set the Last discrepancy to last family discrepancy + LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum]; + LastFamilyDiscrepancy[portnum] = 0; + + // check for end of list + if (LastDiscrepancy[portnum] == 0) + LastDevice[portnum] = TRUE; +} + +//-------------------------------------------------------------------------- +// The 'owAccess' function resets the 1-Wire and sends a MATCH Serial +// Number command followed by the current SerialNum code. After this +// function is complete the 1-Wire device is ready to accept device-specific +// commands. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// +// Returns: TRUE (1) : reset indicates present and device is ready +// for commands. +// FALSE (0): reset does not indicate presence or echos 'writes' +// are not correct. +// +SMALLINT owAccess(int portnum) +{ + uchar sendpacket[9]; + uchar i; + + // reset the 1-wire + if (owTouchReset(portnum)) + { + // create a buffer to use with block function + // match Serial Number command 0x55 + sendpacket[0] = 0x55; + // Serial Number + for (i = 1; i < 9; i++) + sendpacket[i] = SerialNum[portnum][i-1]; + + // send/recieve the transfer buffer + if (owBlock(portnum,FALSE,sendpacket,9)) + { + // verify that the echo of the writes was correct + for (i = 1; i < 9; i++) + if (sendpacket[i] != SerialNum[portnum][i-1]) + return FALSE; + if (sendpacket[0] != 0x55) + { + OWERROR(OWERROR_WRITE_VERIFY_FAILED); + return FALSE; + } + else + return TRUE; + } + else + OWERROR(OWERROR_BLOCK_FAILED); + } + else + OWERROR(OWERROR_NO_DEVICES_ON_NET); + + // reset or match echo failed + return FALSE; +} + +//---------------------------------------------------------------------- +// The function 'owVerify' verifies that the current device +// is in contact with the 1-Wire Net. +// Using the find alarm command 0xEC will verify that the device +// is in contact with the 1-Wire Net and is in an 'alarm' state. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'alarm_only' - TRUE (1) the find alarm command 0xEC +// is sent instead of the normal search +// command 0xF0. +// +// Returns: TRUE (1) : when the 1-Wire device was verified +// to be on the 1-Wire Net +// with alarm_only == FALSE +// or verified to be on the 1-Wire Net +// AND in an alarm state when +// alarm_only == TRUE. +// FALSE (0): the 1-Wire device was not on the +// 1-Wire Net or if alarm_only +// == TRUE, the device may be on the +// 1-Wire Net but in a non-alarm state. +// +SMALLINT owVerify(int portnum, SMALLINT alarm_only) +{ + uchar i,sendlen=0,goodbits=0,cnt=0,s,tst; + uchar sendpacket[50]; + + // construct the search + if (alarm_only) + sendpacket[sendlen++] = 0xEC; // issue the alarming search command + else + sendpacket[sendlen++] = 0xF0; // issue the search command + // set all bits at first + for (i = 1; i <= 24; i++) + sendpacket[sendlen++] = 0xFF; + // now set or clear apropriate bits for search + for (i = 0; i < 64; i++) + bitacc(WRITE_FUNCTION,bitacc(READ_FUNCTION,0,i,&SerialNum[portnum][0]),(int)((i+1)*3-1),&sendpacket[1]); + + // send/recieve the transfer buffer + if (owBlock(portnum,TRUE,sendpacket,sendlen)) + { + // check results to see if it was a success + for (i = 0; i < 192; i += 3) + { + tst = (bitacc(READ_FUNCTION,0,i,&sendpacket[1]) << 1) | + bitacc(READ_FUNCTION,0,(int)(i+1),&sendpacket[1]); + + s = bitacc(READ_FUNCTION,0,cnt++,&SerialNum[portnum][0]); + + if (tst == 0x03) // no device on line + { + goodbits = 0; // number of good bits set to zero + break; // quit + } + + if (((s == 0x01) && (tst == 0x02)) || + ((s == 0x00) && (tst == 0x01)) ) // correct bit + goodbits++; // count as a good bit + } + + // check too see if there were enough good bits to be successful + if (goodbits >= 8) + return TRUE; + } + else + OWERROR(OWERROR_BLOCK_FAILED); + + // block fail or device not present + return FALSE; +} + +//---------------------------------------------------------------------- +// Perform a overdrive MATCH command to select the 1-Wire device with +// the address in the ID data register. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// +// Returns: TRUE: If the device is present on the 1-Wire Net and +// can do overdrive then the device is selected. +// FALSE: Device is not present or not capable of overdrive. +// +// *Note: This function could be converted to send DS2480 +// commands in one packet. +// +SMALLINT owOverdriveAccess(int portnum) +{ + uchar sendpacket[8]; + uchar i, bad_echo = FALSE; + + // make sure normal level + owLevel(portnum,MODE_NORMAL); + + // force to normal communication speed + owSpeed(portnum,MODE_NORMAL); + + // call the 1-Wire Net reset function + if (owTouchReset(portnum)) + { + // send the match command 0x69 + if (owWriteByte(portnum,0x69)) + { + // switch to overdrive communication speed + owSpeed(portnum,MODE_OVERDRIVE); + + // create a buffer to use with block function + // Serial Number + for (i = 0; i < 8; i++) + sendpacket[i] = SerialNum[portnum][i]; + + // send/recieve the transfer buffer + if (owBlock(portnum,FALSE,sendpacket,8)) + { + // verify that the echo of the writes was correct + for (i = 0; i < 8; i++) + if (sendpacket[i] != SerialNum[portnum][i]) + bad_echo = TRUE; + // if echo ok then success + if (!bad_echo) + return TRUE; + else + OWERROR(OWERROR_WRITE_VERIFY_FAILED); + } + else + OWERROR(OWERROR_BLOCK_FAILED); + } + else + OWERROR(OWERROR_WRITE_BYTE_FAILED); + } + else + OWERROR(OWERROR_NO_DEVICES_ON_NET); + + // failure, force back to normal communication speed + owSpeed(portnum,MODE_NORMAL); + + return FALSE; +} + +//-------------------------------------------------------------------------- +// Bit utility to read and write a bit in the buffer 'buf'. +// +// 'op' - operation (1) to set and (0) to read +// 'state' - set (1) or clear (0) if operation is write (1) +// 'loc' - bit number location to read or write +// 'buf' - pointer to array of bytes that contains the bit +// to read or write +// +// Returns: 1 if operation is set (1) +// 0/1 state of bit number 'loc' if operation is reading +// +SMALLINT bitacc(SMALLINT op, SMALLINT state, SMALLINT loc, uchar *buf) +{ + SMALLINT nbyt,nbit; + + nbyt = (loc / 8); + nbit = loc - (nbyt * 8); + + if (op == WRITE_FUNCTION) + { + if (state) + buf[nbyt] |= (0x01 << nbit); + else + buf[nbyt] &= ~(0x01 << nbit); + + return 1; + } + else + return ((buf[nbyt] >> nbit) & 0x01); +}
diff -r 000000000000 -r 1193dbfe28e2 owsesu.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/owsesu.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,117 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// owSesU.C - Acquire and release a Session on the 1-Wire Net. +// +// Version: 2.01 +// +// History: 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for +// multiple ports. +// 2.00 -> 2.01 Added error handling. Added circular-include check. +// 2.01 -> 2.10 Added raw memory error handling and SMALLINT +// 2.10 -> 3.00 Added memory bank functionality +// Added file I/O operations +// + +#include "mbed.h" +#include "ownet.h" +#include "./Headers/ds2480.h" + +//--------------------------------------------------------------------------- +// Attempt to acquire a 1-Wire net using a com port and a DS2480 based +// adapter. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'port_zstr' - zero terminated port name. For this platform +// use format COMX where X is the port number. +// +// Returns: TRUE - success, COM port opened +// +// exportable functions defined in ownetu.c +SMALLINT owAcquire(int portnum, char *port_zstr) +{ + // attempt to open the communications port + if (OpenCOM(portnum,port_zstr) < 0) + { + OWERROR(OWERROR_OPENCOM_FAILED); + return FALSE; + } + + // detect DS2480 + if (!DS2480Detect(portnum)) + { + CloseCOM(portnum); + OWERROR(OWERROR_DS2480_NOT_DETECTED); + return FALSE; + } + + return TRUE; +} + +//--------------------------------------------------------------------------- +// Attempt to acquire a 1-Wire net using a com port and a DS2480 based +// adapter. +// +// 'port_zstr' - zero terminated port name. For this platform +// use format COMX where X is the port number. +// +// Returns: valid handle, or -1 if an error occurred +// +// exportable functions defined in ownetu.c +// +int owAcquireEx(char *port_zstr) +{ + int portnum; + + // attempt to open the communications port + if ((portnum = OpenCOMEx(port_zstr)) < 0) + { + OWERROR(OWERROR_OPENCOM_FAILED); + return -1; + } + + // detect DS2480 + if (!DS2480Detect(portnum)) + { + CloseCOM(portnum); + OWERROR(OWERROR_DS2480_NOT_DETECTED); + return -1; + } + + return portnum; +} + +//--------------------------------------------------------------------------- +// Release the previously acquired a 1-Wire net. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// +void owRelease(int portnum) +{ + CloseCOM(portnum); +}
diff -r 000000000000 -r 1193dbfe28e2 owtrnu.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/owtrnu.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,598 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +//--------------------------------------------------------------------------- +// +// owTranU.C - Transport functions for 1-Wire Net +// using the DS2480B (U) serial interface chip. +// +// Version: 2.01 +// +// History: 1.02 -> 1.03 Removed caps in #includes for Linux capatibility +// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for +// multiple ports. +// 2.00 -> 2.01 Added support for owError library +// 2.01 -> 2.10 Added SMALLINT for small processors and error +// handling plus the raw memory utilities. +// 2.10 -> 3.00 Added memory bank functionality +// Added file I/O operations +// + +#include "ownet.h" +#include "./Headers/ds2480.h" +// external defined in ds2480ut.c +extern SMALLINT UBaud[MAX_PORTNUM]; +extern SMALLINT UMode[MAX_PORTNUM]; +extern SMALLINT USpeed[MAX_PORTNUM]; +extern uchar SerialNum[MAX_PORTNUM][8]; + +// local static functions +static SMALLINT Write_Scratchpad(int,uchar *,int,SMALLINT); +static SMALLINT Copy_Scratchpad(int,int,SMALLINT); + +//-------------------------------------------------------------------------- +// The 'owBlock' transfers a block of data to and from the +// 1-Wire Net with an optional reset at the begining of communication. +// The result is returned in the same buffer. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'do_reset' - cause a owTouchReset to occure at the begining of +// communication TRUE(1) or not FALSE(0) +// 'tran_buf' - pointer to a block of unsigned +// chars of length 'tran_len' that will be sent +// to the 1-Wire Net +// 'tran_len' - length in bytes to transfer + +// Supported devices: all +// +// Returns: TRUE (1) : The optional reset returned a valid +// presence (do_reset == TRUE) or there +// was no reset required. +// FALSE (0): The reset did not return a valid prsence +// (do_reset == TRUE). +// +// The maximum tran_length is (160) +// +SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len) +{ + uchar sendpacket[320]; + uchar sendlen=0,i; + + // check for a block too big + if (tran_len > 160) + { + OWERROR(OWERROR_BLOCK_TOO_BIG); + return FALSE; + } + + // check if need to do a owTouchReset first + if (do_reset) + { + if (!owTouchReset(portnum)) + { + OWERROR(OWERROR_NO_DEVICES_ON_NET); + return FALSE; + } + } + + // construct the packet to send to the DS2480 + // check if correct mode + if (UMode[portnum] != MODSEL_DATA) + { + UMode[portnum] = MODSEL_DATA; + sendpacket[sendlen++] = MODE_DATA; + } + + // add the bytes to send +// pos = sendlen; + for (i = 0; i < tran_len; i++) + { + sendpacket[sendlen++] = tran_buf[i]; + + // check for duplication of data that looks like COMMAND mode + if (tran_buf[i] == MODE_COMMAND) + sendpacket[sendlen++] = tran_buf[i]; + } + + // flush the buffers + FlushCOM(portnum); + + // send the packet + if (WriteCOM(portnum,sendlen,sendpacket)) + { + // read back the response + if (ReadCOM(portnum,tran_len,tran_buf) == tran_len) + return TRUE; + else + OWERROR(OWERROR_READCOM_FAILED); + } + else + OWERROR(OWERROR_WRITECOM_FAILED); + + // an error occured so re-sync with DS2480 + DS2480Detect(portnum); + + return FALSE; +} + +//-------------------------------------------------------------------------- +// Read a Universal Data Packet from a standard NVRAM iButton +// and return it in the provided buffer. The page that the +// packet resides on is 'start_page'. Note that this function is limited +// to single page packets. The buffer 'read_buf' must be at least +// 29 bytes long. +// +// The Universal Data Packet always start on page boundaries but +// can end anywhere. The length is the number of data bytes not +// including the length byte and the CRC16 bytes. There is one +// length byte. The CRC16 is first initialized to the starting +// page number. This provides a check to verify the page that +// was intended is being read. The CRC16 is then calculated over +// the length and data bytes. The CRC16 is then inverted and stored +// low byte first followed by the high byte. +// +// Supported devices: DS1992, DS1993, DS1994, DS1995, DS1996, DS1982, +// DS1985, DS1986, DS2407, and DS1971. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'do_access' - flag to indicate if an 'owAccess' should be +// peformed at the begining of the read. This may +// be FALSE (0) if the previous call was to read the +// previous page (start_page-1). +// 'start_page' - page number to start the read from +// 'read_buf' - pointer to a location to store the data read +// +// Returns: >=0 success, number of data bytes in the buffer +// -1 failed to read a valid UDP +// +// +SMALLINT owReadPacketStd(int portnum, SMALLINT do_access, int start_page, uchar *read_buf) +{ + uchar i,length,sendlen=0,head_len=0; + uchar sendpacket[50]; + unsigned short lastcrc16; + + // check if access header is done + // (only use if in sequention read with one access at begining) + if (do_access) + { + // match command + sendpacket[sendlen++] = 0x55; + for (i = 0; i < 8; i++) + sendpacket[sendlen++] = SerialNum[portnum][i]; + // read memory command + sendpacket[sendlen++] = 0xF0; + // write the target address + sendpacket[sendlen++] = ((start_page << 5) & 0xFF); + sendpacket[sendlen++] = (start_page >> 3); + // check for DS1982 exception (redirection byte) + if (SerialNum[portnum][0] == 0x09) + sendpacket[sendlen++] = 0xFF; + // record the header length + head_len = sendlen; + } + // read the entire page length byte + for (i = 0; i < 32; i++) + sendpacket[sendlen++] = 0xFF; + + // send/recieve the transfer buffer + if (owBlock(portnum,do_access,sendpacket,sendlen)) + { + // seed crc with page number + setcrc16(portnum,(unsigned short)start_page); + + // attempt to read UDP from sendpacket + length = sendpacket[head_len]; + docrc16(portnum,(unsigned short)length); + + // verify length is not too large + if (length <= 29) + { + // loop to read packet including CRC + for (i = 0; i < length; i++) + { + read_buf[i] = sendpacket[i+1+head_len]; + docrc16(portnum,read_buf[i]); + } + + // read and compute the CRC16 + docrc16(portnum,sendpacket[i+1+head_len]); + lastcrc16 = docrc16(portnum,sendpacket[i+2+head_len]); + + // verify the CRC16 is correct + if (lastcrc16 == 0xB001) + return length; // return number of byte in record + else + OWERROR(OWERROR_CRC_FAILED); + } + else + OWERROR(OWERROR_INCORRECT_CRC_LENGTH); + } + else + OWERROR(OWERROR_BLOCK_FAILED); + + // failed block or incorrect CRC + return -1; +} + +//-------------------------------------------------------------------------- +// Write a Universal Data Packet onto a standard NVRAM 1-Wire device +// on page 'start_page'. This function is limited to UDPs that +// fit on one page. The data to write is provided as a buffer +// 'write_buf' with a length 'write_len'. +// +// The Universal Data Packet always start on page boundaries but +// can end anywhere. The length is the number of data bytes not +// including the length byte and the CRC16 bytes. There is one +// length byte. The CRC16 is first initialized to the starting +// page number. This provides a check to verify the page that +// was intended is being read. The CRC16 is then calculated over +// the length and data bytes. The CRC16 is then inverted and stored +// low byte first followed by the high byte. +// +// Supported devices: is_eprom=0 +// DS1992, DS1993, DS1994, DS1995, DS1996 +// is_eprom=1, crc_type=0(CRC8) +// DS1982 +// is_eprom=1, crc_type=1(CRC16) +// DS1985, DS1986, DS2407 +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'start_page' - page number to write packet to +// 'write_buf' - pointer to buffer containing data to write +// 'write_len' - number of data byte in write_buf +// 'is_eprom' - flag set if device is an EPROM (1 EPROM, 0 NVRAM) +// 'crc_type' - if is_eprom=1 then indicates CRC type +// (0 CRC8, 1 CRC16) +// +// Returns: TRUE(1) success, packet written +// FALSE(0) failure to write, contact lost or device locked +// +SMALLINT owWritePacketStd(int portnum, int start_page, uchar *write_buf, + SMALLINT write_len, SMALLINT is_eprom, SMALLINT crc_type) +{ + uchar construct_buffer[32]; + uchar i,buffer_cnt=0,start_address,do_access; + unsigned short lastcrc16=0; + + // check to see if data too long to fit on device + if (write_len > 29) + return FALSE; + + // seed crc with page number + setcrc16(portnum,(unsigned short)start_page); + + // set length byte + construct_buffer[buffer_cnt++] = (uchar)(write_len); + docrc16(portnum,(unsigned short)write_len); + + // fill in the data to write + for (i = 0; i < write_len; i++) + { + lastcrc16 = docrc16(portnum,write_buf[i]); + construct_buffer[buffer_cnt++] = write_buf[i]; + } + + // add the crc + construct_buffer[buffer_cnt++] = (uchar)(~(lastcrc16 & 0xFF)); + construct_buffer[buffer_cnt++] = (uchar)(~((lastcrc16 & 0xFF00) >> 8)); + + // check if not EPROM + if (!is_eprom) + { + // write the page + if (!Write_Scratchpad(portnum,construct_buffer,start_page,buffer_cnt)) + { + OWERROR(OWERROR_WRITE_SCRATCHPAD_FAILED); + return FALSE; + } + + // copy the scratchpad + if (!Copy_Scratchpad(portnum,start_page,buffer_cnt)) + { + OWERROR(OWERROR_COPY_SCRATCHPAD_FAILED); + return FALSE; + } + + // copy scratch pad was good then success + return TRUE; + } + // is EPROM + else + { + // calculate the start address + start_address = ((start_page >> 3) << 8) | ((start_page << 5) & 0xFF); + do_access = TRUE; + // loop to program each byte + for (i = 0; i < buffer_cnt; i++) + { + if (owProgramByte(portnum,construct_buffer[i], start_address + i, + 0x0F, crc_type, do_access) != construct_buffer[i]) + { + OWERROR(OWERROR_PROGRAM_BYTE_FAILED); + return FALSE; + } + do_access = FALSE; + } + return TRUE; + } +} + +//-------------------------------------------------------------------------- +// Write a byte to an EPROM 1-Wire device. +// +// Supported devices: crc_type=0(CRC8) +// DS1982 +// crc_type=1(CRC16) +// DS1985, DS1986, DS2407 +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'write_byte' - byte to program +// 'addr' - address of byte to program +// 'write_cmd' - command used to write (0x0F reg mem, 0x55 status) +// 'crc_type' - CRC used (0 CRC8, 1 CRC16) +// 'do_access' - Flag to access device for each byte +// (0 skip access, 1 do the access) +// WARNING, only use do_access=0 if programing the NEXT +// byte immediatly after the previous byte. +// +// Returns: >=0 success, this is the resulting byte from the program +// effort +// -1 error, device not connected or program pulse voltage +// not available +// +SMALLINT owProgramByte(int portnum, SMALLINT write_byte, int addr, SMALLINT write_cmd, + SMALLINT crc_type, SMALLINT do_access) +{ + unsigned short lastcrc16; + uchar lastcrc8; + + // optionally access the device + if (do_access) + { + if (!owAccess(portnum)) + { + OWERROR(OWERROR_ACCESS_FAILED); + return -1; + } + + // send the write command + if (!owWriteByte(portnum,write_cmd)) + { + OWERROR(OWERROR_WRITE_BYTE_FAILED); + return -1; + } + + // send the address + if (!owWriteByte(portnum,addr & 0xFF) || !owWriteByte(portnum,addr >> 8)) + { + OWERROR(OWERROR_WRITE_BYTE_FAILED); + return -1; + } + } + + // send the data to write + if (!owWriteByte(portnum,write_byte)) + { + OWERROR(OWERROR_WRITE_BYTE_FAILED); + return -1; + } + + // read the CRC + if (crc_type == 0) + { + // calculate CRC8 + if (do_access) + { + setcrc8(portnum,0); + docrc8(portnum,(uchar)write_cmd); + docrc8(portnum,(uchar)(addr & 0xFF)); + docrc8(portnum,(uchar)(addr >> 8)); + } + else + setcrc8(portnum,(uchar)(addr & 0xFF)); + + docrc8(portnum,(uchar)write_byte); + // read and calculate the read crc + lastcrc8 = docrc8(portnum,(uchar)owReadByte(portnum)); + // crc should now be 0x00 + if (lastcrc8 != 0) + { + OWERROR(OWERROR_CRC_FAILED); + return -1; + } + } + else + { + // CRC16 + if (do_access) + { + setcrc16(portnum,0); + docrc16(portnum,(unsigned short)write_cmd); + docrc16(portnum,(unsigned short)(addr & 0xFF)); + docrc16(portnum,(unsigned short)(addr >> 8)); + } + else + setcrc16(portnum,(unsigned short)addr); + docrc16(portnum,(unsigned short)write_byte); + // read and calculate the read crc + docrc16(portnum,( unsigned short)owReadByte(portnum)); + lastcrc16 = docrc16(portnum,( unsigned short)owReadByte(portnum)); + // crc should now be 0xB001 + if (lastcrc16 != 0xB001) + { + OWERROR(OWERROR_CRC_FAILED); + return -1; + } + } + + // send the program pulse + if (!owProgramPulse(portnum)) + { + OWERROR(OWERROR_PROGRAM_PULSE_FAILED); + return -1; + } + + // read back and return the resulting byte + return owReadByte(portnum); +} + +//-------------------------------------------------------------------------- +// Write the scratchpad of a standard NVRam device such as the DS1992,3,4 +// and verify its contents. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'write_buf' - pointer to buffer containing data to write +// 'start_page' - page number to write packet to +// 'write_len' - number of data byte in write_buf +// +// Returns: TRUE(1) success, the data was written and verified +// FALSE(0) failure, the data could not be written +// +// +SMALLINT Write_Scratchpad(int portnum, uchar *write_buf, int start_page, SMALLINT write_len) +{ + uchar i,sendlen=0; + uchar sendpacket[50]; + + // match command + sendpacket[sendlen++] = 0x55; + for (i = 0; i < 8; i++) + sendpacket[sendlen++] = SerialNum[portnum][i]; + // write scratchpad command + sendpacket[sendlen++] = 0x0F; + // write the target address + sendpacket[sendlen++] = ((start_page << 5) & 0xFF); + sendpacket[sendlen++] = (start_page >> 3); + + // write packet bytes + for (i = 0; i < write_len; i++) + sendpacket[sendlen++] = write_buf[i]; + + // send/recieve the transfer buffer + if (owBlock(portnum,TRUE,sendpacket,sendlen)) + { + // now attempt to read back to check + sendlen = 0; + // match command + sendpacket[sendlen++] = 0x55; + for (i = 0; i < 8; i++) + sendpacket[sendlen++] = SerialNum[portnum][i]; + // read scratchpad command + sendpacket[sendlen++] = 0xAA; + // read the target address, offset and data + for (i = 0; i < (write_len + 3); i++) + sendpacket[sendlen++] = 0xFF; + + // send/recieve the transfer buffer + if (owBlock(portnum,TRUE,sendpacket,sendlen)) + { + // check address and offset of scratchpad read + if ((sendpacket[10] != ((start_page << 5) & 0xFF)) || + (sendpacket[11] != (start_page >> 3)) || + (sendpacket[12] != (write_len - 1))) + { + OWERROR(OWERROR_READ_VERIFY_FAILED); + return FALSE; + } + + // verify each data byte + for (i = 0; i < write_len; i++) + if (sendpacket[i+13] != write_buf[i]) + { + OWERROR(OWERROR_WRITE_VERIFY_FAILED); + return FALSE; + } + + // must have verified + return TRUE; + } + else + OWERROR(OWERROR_BLOCK_FAILED); + } + else + OWERROR(OWERROR_BLOCK_FAILED); + + // failed a block tranfer + return FALSE; +} + +//-------------------------------------------------------------------------- +// Copy the contents of the scratchpad to its intended nv ram page. The +// page and length of the data is needed to build the authorization bytes +// to copy. +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to +// indicate the symbolic port number. +// 'start_page' - page number to write packet to +// 'write_len' - number of data bytes that are being copied +// +// Returns: TRUE(1) success +// FALSE(0) failure +// +SMALLINT Copy_Scratchpad(int portnum, int start_page, SMALLINT write_len) +{ + uchar i,sendlen=0; + uchar sendpacket[50]; + + // match command + sendpacket[sendlen++] = 0x55; + for (i = 0; i < 8; i++) + sendpacket[sendlen++] = SerialNum[portnum][i]; + // copy scratchpad command + sendpacket[sendlen++] = 0x55; + // write the target address + sendpacket[sendlen++] = ((start_page << 5) & 0xFF); + sendpacket[sendlen++] = (start_page >> 3); + sendpacket[sendlen++] = write_len - 1; + // read copy result + sendpacket[sendlen++] = 0xFF; + + // send/recieve the transfer buffer + if (owBlock(portnum,TRUE,sendpacket,sendlen)) + { + // check address and offset of scratchpad read + if ((sendpacket[10] != ((start_page << 5) & 0xFF)) || + (sendpacket[11] != (start_page >> 3)) || + (sendpacket[12] != (write_len - 1)) || + (sendpacket[13] & 0xF0)) + { + OWERROR(OWERROR_READ_VERIFY_FAILED); + return FALSE; + } + else + return TRUE; + } + else + OWERROR(OWERROR_BLOCK_FAILED); + + // failed a block tranfer + return FALSE; +} +
diff -r 000000000000 -r 1193dbfe28e2 readDS18B20.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/readDS18B20.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,108 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +// --------------------------------------------------------------------------- +// +// temp10.C - Module to read the DS1920/DS1820 - temperature measurement. +// +// Version: 2.00 +// +// --------------------------------------------------------------------------- +// +// +#include "mbed.h" +#include "ownet.h" +#include "./Headers/readDS1920.h" + +//---------------------------------------------------------------------- +// Read the temperature of a DS1920/DS1820 +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'SerialNum' - Serial Number of DS1920/DS1820 to read temperature from +// 'Temp ' - pointer to variable where that temperature will be +// returned +// +// Returns: TRUE(1) temperature has been read and verified +// FALSE(0) could not read the temperature, perhaps device is not +// in contact +// + +int ReadDS18B20(int portnum, uchar *SerialNum, float *Temp) +{ + uchar rt=FALSE; + uchar send_block[30],lastcrc8; + int send_cnt, tsht, i; + float tmp; + + // set the device serial number to the counter device + owSerialNum(portnum,SerialNum,FALSE); + + // access the device + if (owAccess(portnum)) { + // send the convert command and start power delivery + if (!owWriteBytePower(portnum,0x44)) + return FALSE; + + // sleep for 1 second + msDelay(1000); + + // turn off the 1-Wire Net strong pull-up + if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL) + return FALSE; + + // access the device + if (owAccess(portnum)) { + // create a block to send that reads the temperature + // read scratchpad command + send_cnt = 0; + send_block[send_cnt++] = 0xBE; + // now add the read bytes for data bytes and crc8 + for (i = 0; i < 9; i++) + send_block[send_cnt++] = 0xFF; + + // now send the block + if (owBlock(portnum,FALSE,send_block,send_cnt)) { + // initialize the CRC8 + setcrc8(portnum,0); + // perform the CRC8 on the last 8 bytes of packet + for (i = send_cnt - 9; i < send_cnt; i++) + lastcrc8 = docrc8(portnum,send_block[i]); + + // verify CRC8 is correct + if (lastcrc8 == 0x00) { + // calculate the high-res temperature + tsht = send_block[2]*256 + send_block[1]; + tmp = (float)(tsht); + tmp = tmp / 16; + *Temp = tmp; + // success + rt = TRUE; + } + } + } + } + // return the result flag rt + return rt; +} \ No newline at end of file
diff -r 000000000000 -r 1193dbfe28e2 readDS1920.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/readDS1920.cpp Thu Mar 24 17:21:29 2011 +0000 @@ -0,0 +1,133 @@ +//--------------------------------------------------------------------------- +// Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES +// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// Except as contained in this notice, the name of Dallas Semiconductor +// shall not be used except as stated in the Dallas Semiconductor +// Branding Policy. +// --------------------------------------------------------------------------- +// +// temp10.C - Module to read the DS1920/DS1820 - temperature measurement. +// +// Version: 2.00 +// +// --------------------------------------------------------------------------- +// +// +#include "mbed.h" +#include "ownet.h" +#include "./Headers/readDS1920.h" + +//---------------------------------------------------------------------- +// Read the temperature of a DS1920/DS1820 +// +// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to +// OpenCOM to indicate the port number. +// 'SerialNum' - Serial Number of DS1920/DS1820 to read temperature from +// 'Temp ' - pointer to variable where that temperature will be +// returned +// +// Returns: TRUE(1) temperature has been read and verified +// FALSE(0) could not read the temperature, perhaps device is not +// in contact +// + + +int ReadTemperature(int portnum, uchar *SerialNum, float *Temp) +{ + uchar rt=FALSE; + uchar send_block[30],lastcrc8; + int send_cnt, tsht, i, loop=0; + float tmp,cr,cpc; + + // set the device serial number to the counter device + owSerialNum(portnum,SerialNum,FALSE); + + for (loop = 0; loop < 2; loop ++) + { + // access the device + if (owAccess(portnum)) + { + // send the convert command and start power delivery + if (!owWriteBytePower(portnum,0x44)) + return FALSE; + + // sleep for 1 second + msDelay(1000); + + // turn off the 1-Wire Net strong pull-up + if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL) + return FALSE; + + // access the device + if (owAccess(portnum)) + { + // create a block to send that reads the temperature + // read scratchpad command + send_cnt = 0; + send_block[send_cnt++] = 0xBE; + // now add the read bytes for data bytes and crc8 + for (i = 0; i < 9; i++) + send_block[send_cnt++] = 0xFF; + + // now send the block + if (owBlock(portnum,FALSE,send_block,send_cnt)) + { + // initialize the CRC8 + setcrc8(portnum,0); + // perform the CRC8 on the last 8 bytes of packet + for (i = send_cnt - 9; i < send_cnt; i++) + lastcrc8 = docrc8(portnum,send_block[i]); + + // verify CRC8 is correct + if (lastcrc8 == 0x00) + { + // calculate the high-res temperature + tsht = send_block[1]/2; + printf ("t1: %2x\r\n", tsht); + if (send_block[2] & 0x01) + tsht |= -128; + printf ("t2: %2x\r\n", tsht); + tmp = (float)(tsht); + printf ("t3: %f\r\n", tmp); + cr = send_block[7]; + cpc = send_block[8]; + if (((cpc - cr) == 1) && (loop == 0)) + continue; + if (cpc == 0) + return FALSE; + else + printf ("t4: %f\r\n", tmp); + tmp = tmp - (float)0.25 + (cpc - cr)/cpc; + printf ("t5: %f\r\n", tmp); + *Temp = tmp; + // success + rt = TRUE; + break; + } + } + } + } + + } + + // return the result flag rt + return rt; +}