Commit b20e998e authored by Wesley W. Terpstra's avatar Wesley W. Terpstra

Replaced CERN 1wire software with code capable of addressing multiple devices.

Read the MAC address either from EEPROM or sensor ID (preferring EEPROM).
parent 01aac71f
......@@ -86,6 +86,7 @@ OBJS_PLATFORM=arch/lm32/crt0.o arch/lm32/irq.o arch/lm32/debug.o
include shell/shell.mk
include tests/tests.mk
include lib/lib.mk
include sockitowm/sockitowm.mk
include softpll/softpll.mk
include dev/dev.mk
......@@ -96,7 +97,7 @@ SIZE=$(CROSS_COMPILE)size
CFLAGS= $(CFLAGS_PLATFORM) $(CFLAGS_PTPD) $(INCLUDE_DIRS) -ffunction-sections -fdata-sections -Os -Iinclude -include include/trace.h $(PTPD_CFLAGS) -I$(PTP_NOPOSIX)/PTPWRd -I. -Isoftpll
LDFLAGS= $(LDFLAGS_PLATFORM) -ffunction-sections -fdata-sections -Wl,--gc-sections -Os -Iinclude
OBJS=$(OBJS_PLATFORM) $(OBJS_WRC) $(OBJS_PTPD) $(OBJS_SHELL) $(OBJS_TESTS) $(OBJS_LIB) $(OBJS_SOFTPLL) $(OBJS_DEV)
OBJS=$(OBJS_PLATFORM) $(OBJS_WRC) $(OBJS_PTPD) $(OBJS_SHELL) $(OBJS_TESTS) $(OBJS_LIB) $(OBJS_SOCKITOWM) $(OBJS_SOFTPLL) $(OBJS_DEV)
OUTPUT=wrc
REVISION=$(shell git rev-parse HEAD)
......
......@@ -3,8 +3,8 @@ OBJS_DEV = dev/eeprom.o \
dev/ep_pfilter.o \
dev/i2c.o \
dev/minic.o \
dev/onewire.o \
dev/pps_gen.o \
dev/syscon.o \
dev/uart.o \
dev/sfp.o
dev/sfp.o \
dev/persistent_mac.o
#include <stdio.h>
#include "board.h"
#define R_CSR 0x0
#define R_CDR 0x4
#define CSR_DAT_MSK (1<<0)
#define CSR_RST_MSK (1<<1)
#define CSR_OVD_MSK (1<<2)
#define CSR_CYC_MSK (1<<3)
#define CSR_PWR_MSK (1<<4)
#define CSR_IRQ_MSK (1<<6)
#define CSR_IEN_MSK (1<<7)
#define CSR_SEL_OFS 8
#define CSR_SEL_MSK (0xF<<8)
#define CSR_POWER_OFS 16
#define CSR_POWER_MSK (0xFFFF<<16)
#define CDR_NOR_MSK (0xFFFF<<0)
#define CDR_OVD_OFS 16
#define CDR_OVD_MSK (0xFFFF<<16)
#define CLK_DIV_NOR 175 //clock divider for normal mode
#define CLK_DIV_OVD 124/2 //clock divider for overdrive mode
static inline void ow_writel(uint32_t reg, uint32_t data)
{
*(volatile uint32_t *) (BASE_ONEWIRE + reg) = data;
}
static inline uint32_t ow_readl(uint32_t reg)
{
return *(volatile uint32_t *)(BASE_ONEWIRE + reg);
}
void ow_init()
{
//set clock dividers for normal and overdrive mode
ow_writel( R_CDR, ((CLK_DIV_NOR & CDR_NOR_MSK) | (( CLK_DIV_OVD << CDR_OVD_OFS) & CDR_OVD_MSK)) );
}
static uint32_t ow_reset(uint32_t port)
{
uint32_t reg, data;
data = ( (port<<CSR_SEL_OFS) & CSR_SEL_MSK) |
CSR_CYC_MSK | CSR_RST_MSK; //start cycle, rst pulse request
ow_writel(R_CSR, data);
//wait until cycle done
while(ow_readl(R_CSR) & CSR_CYC_MSK);
reg = ow_readl(R_CSR);
return ~reg & CSR_DAT_MSK;
}
static uint32_t slot(uint32_t port, uint32_t bit)
{
uint32_t data, reg;
data = ( (port<<CSR_SEL_OFS) & CSR_SEL_MSK) |
CSR_CYC_MSK | (bit & CSR_DAT_MSK); //start cycle, write bit
ow_writel(R_CSR, data);
//wait until cycle done
while( ow_readl(R_CSR) & CSR_CYC_MSK );
reg = ow_readl(R_CSR);
return reg & CSR_DAT_MSK;
}
static inline uint32_t ow_read_bit(uint32_t port) { return slot(port, 0x1); }
static inline uint32_t ow_write_bit(uint32_t port, uint32_t bit) { return slot(port, bit); }
uint8_t ow_read_byte(uint32_t port)
{
uint32_t data = 0, i;
for(i=0;i<8;i++)
data |= ow_read_bit(port) << i;
return (uint8_t)data;
}
int8_t ow_write_byte(uint32_t port, uint32_t byte)
{
uint32_t data = 0;
uint8_t i;
uint32_t byte_old = byte;
for (i=0;i<8;i++)
{
data |= ow_write_bit(port, (byte & 0x1)) << i;
byte >>= 1;
}
return byte_old == data ? 0 : -1;
}
int ow_write_block(int port, uint8_t *block, int len)
{
uint32_t i;
for(i=0;i<len;i++)
*block++ = ow_write_byte(port, *block);
return 0;
}
int ow_read_block(int port, uint8_t *block, int len)
{
uint32_t i;
for(i=0;i<len;i++)
*block++ = ow_read_byte(port);
return 0;
}
#define ROM_SEARCH 0xF0
#define ROM_READ 0x33
#define ROM_MATCH 0x55
#define ROM_SKIP 0xCC
#define ROM_ALARM_SEARCH 0xEC
#define CONVERT_TEMP 0x44
#define WRITE_SCRATCHPAD 0x4E
#define READ_SCRATCHPAD 0xBE
#define COPY_SCRATCHPAD 0x48
#define RECALL_EEPROM 0xB8
#define READ_POWER_SUPPLY 0xB4
static uint8_t ds18x_id [8];
int8_t ds18x_read_serial(uint8_t *id)
{
uint8_t i;
if(!ow_reset(0))
return -1;
if(ow_write_byte(0, ROM_READ) < 0)
return -1;
for(i=0;i<8;i++)
{
*id = ow_read_byte(0);
id++;
}
return 0;
}
static int8_t ds18x_access(uint8_t *id)
{
int i;
if(!ow_reset(0))
return -1;
if(ow_write_byte(0, ROM_MATCH) < 0)
return -1;
for(i=0;i<8;i++)
if(ow_write_byte(0, id[i]) < 0)
return -1;
return 0;
}
int8_t ds18x_read_temp(uint8_t *id, int *temp_r)
{
int i, temp;
uint8_t data[9];
if(ds18x_access(id) < 0)
return -1;
ow_write_byte(0, READ_SCRATCHPAD);
for(i=0;i<9;i++) data[i] = ow_read_byte(0);
temp = ((int)data[1] << 8) | ((int)data[0]);
if(temp & 0x1000)
temp = -0x10000 + temp;
ds18x_access(id);
ow_write_byte(0, CONVERT_TEMP);
if(temp_r) *temp_r = temp;
return 0;
}
int ds18x_init()
{
ow_init();
if(ds18x_read_serial(ds18x_id) < 0)
return -1;
return ds18x_read_temp(ds18x_id, NULL);
}
#include "persistent_mac.h"
#include "../sockitowm/ownet.h"
#include "../sockitowm/findtype.h"
#define MAX_DEV1WIRE 8
#define DEBUG_PMAC 0
/* 0 = success, -1 = error */
int get_persistent_mac(unsigned char* mac)
{
unsigned char FamilySN[MAX_DEV1WIRE][8];
unsigned char read_buffer[32];
unsigned char portnum = ONEWIRE_PORT;
int i, devices, out;
// Find the device(s)
devices = 0;
devices += FindDevices(portnum, &FamilySN[devices], 0x28, MAX_DEV1WIRE - devices); /* Temperature 28 sensor (SPEC) */
devices += FindDevices(portnum, &FamilySN[devices], 0x42, MAX_DEV1WIRE - devices); /* Temperature 42 sensor (SCU) */
devices += FindDevices(portnum, &FamilySN[devices], 0x43, MAX_DEV1WIRE - devices); /* EEPROM */
out = -1;
for (i = 0; i < devices; ++i) {
/* If there is a temperature sensor, use it for the low three MAC values */
if (FamilySN[i][0] == 0x28 || FamilySN[i][0] == 0x42) {
mac[3] = FamilySN[i][3];
mac[4] = FamilySN[i][2];
mac[5] = FamilySN[i][1];
out = 0;
#ifdef DEBUG_PMAC
mprintf("Using temperature sensor ID: %x:%x:%x:%x:%x:%x:%x:%x\n",
FamilySN[i][7], FamilySN[i][6], FamilySN[i][5], FamilySN[i][4],
FamilySN[i][3], FamilySN[i][2], FamilySN[i][1], FamilySN[i][0]);
#endif
}
/* If there is an EEPROM, read page 0 for the MAC */
if (FamilySN[i][0] == 0x43) {
owLevel(portnum, MODE_NORMAL);
if (ReadMem43(portnum, FamilySN[i], EEPROM_MAC_PAGE, &read_buffer) == TRUE) {
if (read_buffer[0] == 0 && read_buffer[1] == 0 && read_buffer[2] == 0) {
/* Skip the EEPROM since it has not been programmed! */
#ifdef DEBUG_PMAC
mprintf("EEPROM %x:%x:%x:%x:%x:%x:%x:%x has not been programmed with a MAC\n",
FamilySN[i][7], FamilySN[i][6], FamilySN[i][5], FamilySN[i][4],
FamilySN[i][3], FamilySN[i][2], FamilySN[i][1], FamilySN[i][0]);
#endif
} else {
memcpy(mac, read_buffer, 6);
out = 0;
#ifdef DEBUG_PMAC
mprintf("Using EEPROM page: %x:%x:%x:%x:%x:%x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
#endif
}
}
}
return out;
}
/* 0 = success, -1 = error */
int set_persistent_mac(unsigned char* mac)
{
unsigned char FamilySN[MAX_DEV1WIRE][8];
unsigned char write_buffer[32];
unsigned char portnum = ONEWIRE_PORT;
int i, devices;
// Find the device(s)
devices = FindDevices(portnum, &FamilySN[0], 0x43, MAX_DEV1WIRE); /* EEPROM */
if (devices == 0) return -1;
memset(write_buffer, 0, sizeof(write_buffer));
memcpy(write_buffer, mac, 6);
/* Write the last EEPROM with the MAC */
owLevel(portnum, MODE_NORMAL);
if (Write43(portnum, FamilySN[devices-1], EEPROM_MAC_PAGE, &write_buffer) == TRUE)
return 0;
return -1;
}
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
void ow_init();
uint32_t ow_reset(uint32_t port);
uint8_t ow_read_byte(uint32_t port);
int8_t ow_write_byte(uint32_t port, uint32_t byte);
int ow_write_block(int port, uint8_t *block, int len);
int ow_read_block(int port, uint8_t *block, int len);
int8_t ds18x_read_serial(uint8_t *id);
int8_t ds18x_read_temp(uint8_t *id, int *temp_r);
int ds18x_init();
#endif
#ifndef PERSISTENT_MAC_H
#define PERSISTENT_MAC_H
#define ONEWIRE_PORT 0
#define EEPROM_MAC_PAGE 0
/* 0 = success, -1 = error */
int get_persistent_mac(unsigned char* mac);
int set_persistent_mac(unsigned char* mac);
#endif
......@@ -136,7 +136,7 @@ void shell_init()
env_init();
cmd_len = cmd_pos = 0;
state = SH_PROMPT;
mprintf("\033[2J\033[1;1H");
// mprintf("\033[2J\033[1;1H");
}
void shell_interactive()
......
//---------------------------------------------------------------------------
// 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
ushort 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, ushort 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
//
ushort docrc16(int portnum, ushort 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];
}
#include "ownet.h"
#define READ_SCRATCH_CMD 0xaa
#define WRITE_SCRATCH_CMD 0x0f
#define COPY_SCRATCH_CMD 0x55
#define READ_MEM_CMD 0xf0
#define E_READ_MEM_CMD 0xa5
int Write43(int portnum, uchar *SerialNum, int page, uchar *page_buffer)
{
uchar rt=FALSE;
ushort lastcrc16;
int i;
owSerialNum(portnum, SerialNum, FALSE);
if(owAccess(portnum))
{
mprintf(" Writing Scratchpad...\n");
if (!owWriteBytePower(portnum, WRITE_SCRATCH_CMD))
return FALSE;
setcrc16(portnum, 0);
docrc16(portnum,(ushort)WRITE_SCRATCH_CMD);
owLevel(portnum, MODE_NORMAL);
owWriteBytePower(portnum, 0x00); //write LSB of target addr
docrc16(portnum,(ushort)0x00);
owLevel(portnum, MODE_NORMAL);
owWriteBytePower(portnum, 0x00); //write MSB of target addr
docrc16(portnum,(ushort)0x00);
for(i = 0; i < 32; i++) //write 32 data bytes to scratchpad
{
owLevel(portnum,MODE_NORMAL);
owWriteBytePower(portnum, i);
lastcrc16 = docrc16(portnum,i);
// mprintf(" CRC16: %x\n", lastcrc16);
}
for(i = 0; i < 2; i++) //read two bytes CRC16
{
owLevel(portnum,MODE_NORMAL);
lastcrc16 = docrc16(portnum,(ushort)owReadBytePower(portnum));
}
mprintf(" CRC16: %x\n", lastcrc16);
if(lastcrc16 == 0xb001)
{
//copy to mem
owLevel(portnum, MODE_NORMAL);
if(Copy2Mem43(portnum, SerialNum))
rt=TRUE;
}
}
return rt;
}
int Copy2Mem43(int portnum, uchar *SerialNum)
{
uchar rt=FALSE;
ushort lastcrc16;
int i;
uchar read_data;
owSerialNum(portnum, SerialNum, FALSE);
if(owAccess(portnum))
{
if (!owWriteBytePower(portnum, COPY_SCRATCH_CMD))
return FALSE;
owLevel(portnum, MODE_NORMAL);
owWriteBytePower(portnum, 0x00); //write LSB of target addr
owLevel(portnum, MODE_NORMAL);
owWriteBytePower(portnum, 0x00); //write MSB of target addr
owLevel(portnum, MODE_NORMAL);
owWriteBytePower(portnum, 0x1f); //write E/S
msDelay(500);
owLevel(portnum,MODE_NORMAL);
read_data = owReadBytePower(portnum);
if (read_data == 0xaa)
rt=TRUE;
}
return rt;
}
// routine for reading the scratchpad of a DS28EC20P EEPROM
// 80 pages of 32byte
// 32byte scratchpad
// expects 32 byte deep buffer
int ReadScratch43(int portnum, uchar *SerialNum, uchar *page_buffer)
{
uchar rt=FALSE;
ushort lastcrc16;
int i;
ushort target_addr = 0;
uchar read_data;
owSerialNum(portnum, SerialNum, FALSE);
if(owAccess(portnum))
{
mprintf(" Reading Scratchpad...\n");
if (!owWriteBytePower(portnum, READ_SCRATCH_CMD))
return FALSE;
setcrc16(portnum, 0); //init crc
docrc16(portnum,(ushort)READ_SCRATCH_CMD);
owLevel(portnum,MODE_NORMAL); //read 2 byte address and 1 byte status
read_data = owReadBytePower(portnum);
lastcrc16 = docrc16(portnum, read_data);
target_addr = read_data;
owLevel(portnum,MODE_NORMAL);
read_data = owReadBytePower(portnum);
lastcrc16 = docrc16(portnum, read_data);
target_addr |= read_data << 8;
owLevel(portnum,MODE_NORMAL);
read_data = owReadBytePower(portnum);
lastcrc16 = docrc16(portnum, read_data);
mprintf("E/S: 0x%x\n", read_data);
for(i = 0; i < 32; i++)
{
owLevel(portnum,MODE_NORMAL);
page_buffer[i] = owReadBytePower(portnum);
lastcrc16 = docrc16(portnum, page_buffer[i]);
// mprintf("send_block[%d]: %x", i, send_block[i]);
// mprintf(" CRC16: %x", lastcrc16);
// mprintf(" CRC16i: %x\n", ~lastcrc16);
}
for(i = 0; i < 2; i++)
{
owLevel(portnum,MODE_NORMAL);
read_data = owReadBytePower(portnum);
lastcrc16 = docrc16(portnum, read_data);
}
if (lastcrc16 == 0xb001)
rt=TRUE;
}
return rt;
}
// routine for reading a memory page of a DS28EC20P EEPROM
// expects 32 byte deep buffer
int ReadMem43(int portnum, uchar *SerialNum, int page, uchar *page_buffer)
{
uchar rt=FALSE;
ushort lastcrc16;
int i;
ushort target_addr = 0;
uchar read_data;
owSerialNum(portnum, SerialNum, FALSE);
if(owAccess(portnum))
{
if (!owWriteBytePower(portnum, E_READ_MEM_CMD))
return FALSE;
setcrc16(portnum, 0); //init crc
docrc16(portnum,(ushort)E_READ_MEM_CMD);
owLevel(portnum, MODE_NORMAL);
owWriteBytePower(portnum, 0x00); //write LSB of target addr
docrc16(portnum,(ushort)0x00);
owLevel(portnum, MODE_NORMAL);
owWriteBytePower(portnum, 0x00); //write MSB of target addr
docrc16(portnum,(ushort)0x00);
for(i = 0; i < 32; i++)
{
owLevel(portnum,MODE_NORMAL);
page_buffer[i] = owReadBytePower(portnum);
lastcrc16 = docrc16(portnum, page_buffer[i]);
// mprintf("send_block[%d]: %x", i, send_block[i]);
// mprintf(" CRC16: %x", lastcrc16);
// mprintf(" CRC16i: %x\n", ~lastcrc16);
}
for(i = 0; i < 2; i++)
{
owLevel(portnum,MODE_NORMAL);
read_data = owReadBytePower(portnum);
lastcrc16 = docrc16(portnum, read_data);
}
if (lastcrc16 == 0xb001)
rt=TRUE;
}
return rt;
}
#ifndef EEP43_H
#define EEP43_H
/* returns TRUE for success */
int Write43(int portnum, uchar *SerialNum, int page, uchar *page_buffer);
int ReadMem43(int portnum, uchar *SerialNum, int page, uchar *page_buffer);
#endif
//---------------------------------------------------------------------------
// 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 "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;
}
//---------------------------------------------------------------------------
// 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);
//---------------------------------------------------------------------------
// 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.
//---------------------------------------------------------------------------
//
// owerr.c - Library functions for error handling with 1-Wire library
//
// Version: 1.00
//
#include <string.h>
//#include <stdio.h>
#include "ownet.h"
#include "uart.h"
#ifndef SIZE_OWERROR_STACK
#ifdef SOCKIT_OWM_ERR_SMALL
//for small memory, only hold 1 error
#define SIZE_OWERROR_STACK 1
#else
#define SIZE_OWERROR_STACK 10
#endif
#endif
//---------------------------------------------------------------------------
// Variables
//---------------------------------------------------------------------------
// Error Struct for holding error information.
// In DEBUG, this will also hold the line number and filename.
typedef struct
{
int owErrorNum;
#ifdef DEBUG
int lineno;
char *filename;
#endif
} owErrorStruct;
// Ring-buffer used for stack.
// In case of overflow, deepest error is over-written.
static owErrorStruct owErrorStack[SIZE_OWERROR_STACK];
// Stack pointer to top-most error.
static int owErrorPointer = 0;
//---------------------------------------------------------------------------
// Functions Definitions
//---------------------------------------------------------------------------
int owGetErrorNum(void);
void owClearError(void);
int owHasErrors(void);
#ifdef DEBUG
void owRaiseError(int,int,char*);
#else
void owRaiseError(int);
#endif
#ifndef SOCKIT_OWM_ERR_SMALL
void owPrintErrorMsg(FILE *);
void owPrintErrorMsgStd();
char *owGetErrorMsg(int);
#endif
//--------------------------------------------------------------------------
// The 'owGetErroNum' returns the error code of the top-most error on the
// error stack. NOTE: This function has the side effect of popping the
// current error off the stack. All successive calls to 'owGetErrorNum'
// will further clear the error stack.
//
// For list of error codes, see 'ownet.h'
//
// Returns: int : The error code of the top-most error on the stack
//
int owGetErrorNum(void)
{
int i = owErrorStack[ owErrorPointer ].owErrorNum;
owErrorStack[ owErrorPointer ].owErrorNum = 0;
if(!owErrorPointer)
owErrorPointer = SIZE_OWERROR_STACK - 1;
else
owErrorPointer = (owErrorPointer - 1);
return i;
}
//--------------------------------------------------------------------------
// The 'owClearError' clears all the errors.
//
void owClearError(void)
{
owErrorStack[ owErrorPointer ].owErrorNum = 0;
}
//--------------------------------------------------------------------------
// The 'owHasErrors' is a boolean test function which tests whether or not
// a valid error is waiting on the stack.
//
// Returns: TRUE (1) : When there are errors on the stack.
// FALSE (0): When stack's errors are set to 0, or NO_ERROR_SET.
//
int owHasErrors(void)
{
if(owErrorStack[ owErrorPointer ].owErrorNum)
return 1; //TRUE
else
return 0; //FALSE
}
#ifdef DEBUG
//--------------------------------------------------------------------------
// The 'owRaiseError' is the method for raising an error onto the error
// stack.
//
// Arguments: int err - the error code you wish to raise.
// int lineno - DEBUG only - the line number where it was raised
// char* filename - DEBUG only - the file name where it occured.
//
void owRaiseError(int err, int lineno, char* filename)
{
owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;
owErrorStack[ owErrorPointer ].owErrorNum = err;
owErrorStack[ owErrorPointer ].lineno = lineno;
owErrorStack[ owErrorPointer ].filename = filename;
}
#else
//--------------------------------------------------------------------------
// The 'owRaiseError' is the method for raising an error onto the error
// stack.
//
// Arguments: int err - the error code you wish to raise.
//
void owRaiseError(int err)
{
owErrorPointer = (owErrorPointer + 1) % SIZE_OWERROR_STACK;
owErrorStack[ owErrorPointer ].owErrorNum = err;
}
#endif
// SOCKIT_OWM_ERR_SMALL - embedded microcontrollers, where these
// messaging functions might not make any sense.
#ifndef SOCKIT_OWM_ERR_SMALL
//Array of meaningful error messages to associate with codes.
//Not used on targets with low memory (i.e. PIC).
static char *owErrorMsg[125] =
{
/*000*/ "No Error Was Set",
/*001*/ "No Devices found on 1-Wire Network",
/*002*/ "1-Wire Net Reset Failed",
/*003*/ "Search ROM Error: Couldn't locate next device on 1-Wire",
/*004*/ "Access Failed: Could not select device",
/*005*/ "DS2480B Adapter Not Detected",
/*006*/ "DS2480B: Wrong Baud",
/*007*/ "DS2480B: Bad Response",
/*008*/ "Open COM Failed",
/*009*/ "Write COM Failed",
/*010*/ "Read COM Failed",
/*011*/ "Data Block Too Large",
/*012*/ "Block Transfer failed",
/*013*/ "Program Pulse Failed",
/*014*/ "Program Byte Failed",
/*015*/ "Write Byte Failed",
/*016*/ "Read Byte Failed",
/*017*/ "Write Verify Failed",
/*018*/ "Read Verify Failed",
/*019*/ "Write Scratchpad Failed",
/*020*/ "Copy Scratchpad Failed",
/*021*/ "Incorrect CRC Length",
/*022*/ "CRC Failed",
/*023*/ "Failed to acquire a necessary system resource",
/*024*/ "Failed to initialize system resource",
/*025*/ "Data too long to fit on specified device.",
/*026*/ "Read exceeds memory bank end.",
/*027*/ "Write exceeds memory bank end.",
/*028*/ "Device select failed",
/*029*/ "Read Scratch Pad verify failed.",
/*030*/ "Copy scratchpad complete not found",
/*031*/ "Erase scratchpad complete not found",
/*032*/ "Address read back from scrachpad was incorrect",
/*033*/ "Read page with extra-info not supported by this memory bank",
/*034*/ "Read page packet with extra-info not supported by this memory bank",
/*035*/ "Length of packet requested exceeds page size",
/*036*/ "Invalid length in packet",
/*037*/ "Program pulse required but not available",
/*038*/ "Trying to access a read-only memory bank",
/*039*/ "Current bank is not general purpose memory",
/*040*/ "Read back from write compare is incorrect, page may be locked",
/*041*/ "Invalid page number for this memory bank",
/*042*/ "Read page with CRC not supported by this memory bank",
/*043*/ "Read page with CRC and extra-info not supported by this memory bank",
/*044*/ "Read back from write incorrect, could not lock page",
/*045*/ "Read back from write incorrect, could not lock redirect byte",
/*046*/ "The read of the status was not completed.",
/*047*/ "Page redirection not supported by this memory bank",
/*048*/ "Lock Page redirection not supported by this memory bank",
/*049*/ "Read back byte on EPROM programming did not match.",
/*050*/ "Can not write to a page that is locked.",
/*051*/ "Can not lock a redirected page that has already been locked.",
/*052*/ "Trying to redirect a locked redirected page.",
/*053*/ "Trying to lock a page that is already locked.",
/*054*/ "Trying to write to a memory bank that is write protected.",
/*055*/ "Error due to not matching MAC.",
/*056*/ "Memory Bank is write protected.",
/*057*/ "Secret is write protected, can not Load First Secret.",
/*058*/ "Error in Reading Scratchpad after Computing Next Secret.",
/*059*/ "Load Error from Loading First Secret.",
/*060*/ "Power delivery required but not available",
/*061*/ "Not a valid file name.",
/*062*/ "Unable to Create a Directory in this part.",
/*063*/ "That file already exists.",
/*064*/ "The directory is not empty.",
/*065*/ "The wrong type of part for this operation.",
/*066*/ "The max len for this file is too small.",
/*067*/ "This is not a write once bank.",
/*068*/ "The file can not be found.",
/*069*/ "There is not enough space available.",
/*070*/ "There is not a page to match that bit in the bitmap.",
/*071*/ "There are no jobs for EPROM parts.",
/*072*/ "Function not supported to modify attributes.",
/*073*/ "Handle is not in use.",
/*074*/ "Tring to read a write only file.",
/*075*/ "There is no handle available for use.",
/*076*/ "The directory provided is an invalid directory.",
/*077*/ "Handle does not exist.",
/*078*/ "Serial Number did not match with current job.",
/*079*/ "Can not program EPROM because a non-EPROM part on the network.",
/*080*/ "Write protect redirection byte is set.",
/*081*/ "There is an inappropriate directory length.",
/*082*/ "The file has already been terminated.",
/*083*/ "Failed to read memory page of iButton part.",
/*084*/ "Failed to match scratchpad of iButton part.",
/*085*/ "Failed to erase scratchpad of iButton part.",
/*086*/ "Failed to read scratchpad of iButton part.",
/*087*/ "Failed to execute SHA function on SHA iButton.",
/*088*/ "SHA iButton did not return a status completion byte.",
/*089*/ "Write data page failed.",
/*090*/ "Copy secret into secret memory pages failed.",
/*091*/ "Bind unique secret to iButton failed.",
/*092*/ "Could not install secret into user token.",
/*093*/ "Transaction Incomplete: signature did not match.",
/*094*/ "Transaction Incomplete: could not sign service data.",
/*095*/ "User token did not provide a valid authentication response.",
/*096*/ "Failed to answer a challenge on the user token.",
/*097*/ "Failed to create a challenge on the coprocessor.",
/*098*/ "Transaction Incomplete: service data was not valid.",
/*099*/ "Transaction Incomplete: service data was not updated.",
/*100*/ "Unrecoverable, catastrophic service failure occured.",
/*101*/ "Load First Secret from scratchpad data failed.",
/*102*/ "Failed to match signature of user's service data.",
/*103*/ "Subkey out of range for the DS1991.",
/*104*/ "Block ID out of range for the DS1991",
/*105*/ "Password is enabled",
/*106*/ "Password is invalid",
/*107*/ "This memory bank has no read only password",
/*108*/ "This memory bank has no read/write password",
/*109*/ "1-Wire is shorted",
/*110*/ "Error communicating with 1-Wire adapter",
/*111*/ "CopyScratchpad failed: Ending Offset must go to end of page",
/*112*/ "WriteScratchpad failed: Ending Offset must go to end of page",
/*113*/ "Mission can not be stopped while one is not in progress",
/*114*/ "Error stopping the mission",
/*115*/ "Port number is outside (0,MAX_PORTNUM) interval",
/*116*/ "Level of the 1-Wire was not changed",
/*117*/ "Both the Read Only and Read Write Passwords must be set",
/*118*/ "Failure to change latch state."
/*119*/ "Could not open usb port through libusb",
/*120*/ "Libusb DS2490 port already opened",
/*121*/ "Failed to set libusb configuration",
/*122*/ "Failed to claim libusb interface",
/*123*/ "Failed to set libusb altinterface",
/*124*/ "No adapter found at this port number"
};
char *owGetErrorMsg(int err)
{
return owErrorMsg[err];
}
//--------------------------------------------------------------------------
// The 'owPrintErrorMsg' is the method for printing an error from the stack.
// The destination for the print is specified by the argument, fileno, which
// can be stderr, stdout, or a log file. In non-debug mode, the output is
// of the form:
// Error num: Error msg
//
// In debug-mode, the output is of the form:
// Error num: filename line#: Error msg
//
// NOTE: This function has the side-effect of popping the error off the stack.
//
// Arguments: FILE*: the destination for printing.
//
void owPrintErrorMsg(FILE *filenum)
{
#ifdef DEBUG
int l = owErrorStack[ owErrorPointer ].lineno;
char *f = owErrorStack[ owErrorPointer ].filename;
int err = owGetErrorNum();
//fprintf(filenum,"Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);
#else
int err = owGetErrorNum();
//fprintf(filenum,"Error %d: %s\r\n",err,owErrorMsg[err]);
#endif
}
// Same as above, except uses default printf output
void owPrintErrorMsgStd()
{
#ifdef DEBUG
int l = owErrorStack[ owErrorPointer ].lineno;
char *f = owErrorStack[ owErrorPointer ].filename;
int err = owGetErrorNum();
mprintf("Error %d: %s line %d: %s\r\n",err,f,l,owErrorMsg[err]);
#else
int err = owGetErrorNum();
mprintf("Error %d: %s\r\n",err,owErrorMsg[err]);
#endif
}
#endif
//---------------------------------------------------------------------------
// 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.
//---------------------------------------------------------------------------
//
// TODO.C - Link Layer functions required by general 1-Wire drive
// implementation. Fill in the platform specific code.
//
// Version: 3.00
//
// History: 1.00 -> 1.01 Added function msDelay.
// 1.02 -> 1.03 Added function msGettick.
// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
// multiple ports.
// 2.10 -> 3.00 Added owReadBitPower and owWriteBytePower
//
#include "ownet.h"
#include "sockit_owm_regs.h"
#include "board.h"
//#define S_OVD 1
//#define S_PWR 0
#define S_IEN 0
#define S_OVD_E 1
//#define CLK_DIV_NOR CPU_CLOCK/357 //clock divider for normal mode
//#define CLK_DIV_OVD CPU_CLOCK/1008 //clock divider for overdrive mode
#define CLK_DIV_NOR 624
#define CLK_DIV_OVD 124
// exportable link-level functions
SMALLINT owTouchReset(int);
SMALLINT owTouchBit(int,SMALLINT);
SMALLINT owTouchByte(int,SMALLINT);
SMALLINT owWriteByte(int,SMALLINT);
SMALLINT owReadByte(int);
SMALLINT owSpeed(int,SMALLINT);
SMALLINT owLevel(int,SMALLINT);
SMALLINT owProgramPulse(int);
//void msDelay(int);
long msGettick(void);
SMALLINT owWriteBytePower(int,SMALLINT);
SMALLINT owReadBytePower(int);
SMALLINT owReadBitPower(int,SMALLINT);
SMALLINT owHasPowerDelivery(int);
SMALLINT owHasOverDrive(int);
SMALLINT owHasProgramPulse(int);
SMALLINT owInit(void);
int S_OVD = 0;
int S_PWR = 0;
// init clock divider for SCU board
SMALLINT owInit(void)
{
IOWR_SOCKIT_OWM_CDR(BASE_ONEWIRE, ((CLK_DIV_NOR & SOCKIT_OWM_CDR_N_MSK) | ((CLK_DIV_OVD << SOCKIT_OWM_CDR_O_OFST) & SOCKIT_OWM_CDR_O_MSK)));
return 0;
}
//--------------------------------------------------------------------------
// Reset all of the devices on the 1-Wire Net and return the result.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
//
// Returns: TRUE(1): presence pulse(s) detected, device(s) reset
// FALSE(0): no presence pulses detected
//
SMALLINT owTouchReset(int portnum)
{
int reg;
int ovd = (S_OVD >> portnum) & 0x1;
IOWR_SOCKIT_OWM_CTL (BASE_ONEWIRE, (S_PWR << SOCKIT_OWM_CTL_POWER_OFST )
| (portnum << SOCKIT_OWM_CTL_SEL_OFST )
| (S_IEN ? SOCKIT_OWM_CTL_IEN_MSK : 0x00)
| ( SOCKIT_OWM_CTL_CYC_MSK )
| (ovd ? SOCKIT_OWM_CTL_OVD_MSK : 0x00)
| ( SOCKIT_OWM_CTL_RST_MSK ));
// wait for STX (end of transfer cycle) and read the presence status
while ((reg = IORD_SOCKIT_OWM_CTL (BASE_ONEWIRE)) & SOCKIT_OWM_CTL_CYC_MSK);
// return negated DAT (presence detect)
return (~reg & SOCKIT_OWM_CTL_DAT_MSK); // NOTE the shortcut
}
//--------------------------------------------------------------------------
// 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 is provided to
// indicate the symbolic 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)
{
int reg;
int ovd = (S_OVD >> portnum) & 0x1;
// read/write data
IOWR_SOCKIT_OWM_CTL (BASE_ONEWIRE, (S_PWR << SOCKIT_OWM_CTL_POWER_OFST )
| (portnum << SOCKIT_OWM_CTL_SEL_OFST )
| (S_IEN ? SOCKIT_OWM_CTL_IEN_MSK : 0x00)
| ( SOCKIT_OWM_CTL_CYC_MSK )
| (ovd ? SOCKIT_OWM_CTL_OVD_MSK : 0x00)
| (sendbit & SOCKIT_OWM_CTL_DAT_MSK )); // NOTE the shortcut
// wait for STX (end of transfer cycle) and read the read data bit
while ((reg = IORD_SOCKIT_OWM_CTL (BASE_ONEWIRE)) & SOCKIT_OWM_CTL_CYC_MSK);
// return DAT (read bit)
return (reg & SOCKIT_OWM_CTL_DAT_MSK); // NOTE the shortcut
}
//--------------------------------------------------------------------------
// 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 is provided to
// indicate the symbolic port number.
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns: 8 bytes read from sendbyte
//
SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
{
int i;
SMALLINT dat = 0;
for (i=0; i<8; i++)
{
dat |= owTouchBit(portnum,sendbyte & 0x1) << i;
sendbyte >>= 1;
}
return dat;
}
//--------------------------------------------------------------------------
// 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 is provided to
// indicate the symbolic 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) == 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 is provided to
// indicate the symbolic port number.
//
// Returns: 8 bytes read from 1-Wire Net
//
SMALLINT owReadByte(int portnum)
{
return owTouchByte(portnum,0xFF);
}
//--------------------------------------------------------------------------
// Set the 1-Wire Net communication speed.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic 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)
{
int select;
select = 0x1 << portnum;
//if overdrive is implemented use it
if (S_OVD_E) {
if (new_speed == MODE_OVERDRIVE) S_OVD |= select;
if (new_speed == MODE_NORMAL ) S_OVD &= ~select;
}
// return the current port state
return (S_OVD & select) ? MODE_OVERDRIVE : MODE_NORMAL;
}
//--------------------------------------------------------------------------
// Set the 1-Wire Net line level. The values for NewLevel are
// as follows:
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'new_level' - new level defined as
// MODE_NORMAL 0x00
// MODE_STRONG5 0x02
// MODE_PROGRAM 0x04
// MODE_BREAK 0x08
//
// Returns: current 1-Wire Net level
//
SMALLINT owLevel(int portnum, SMALLINT new_level)
{
if (new_level == MODE_STRONG5) {
// set the power bit
S_PWR |= (1 << portnum);
IOWR_SOCKIT_OWM_CTL (BASE_ONEWIRE, (S_PWR << SOCKIT_OWM_CTL_POWER_OFST) | SOCKIT_OWM_CTL_PWR_MSK);
}
if (new_level == MODE_NORMAL) {
// clear the power bit
S_PWR &= ~(1 << portnum);
IOWR_SOCKIT_OWM_CTL (BASE_ONEWIRE, (S_PWR << SOCKIT_OWM_CTL_POWER_OFST));
}
// return the current port state
return ((S_PWR >> portnum) & 0x1) ? MODE_STRONG5 : MODE_NORMAL;
}
//--------------------------------------------------------------------------
// 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 is provided to
// indicate the symbolic port number.
//
// Returns: TRUE successful
// FALSE program voltage not available
//
SMALLINT owProgramPulse(int portnum)
{
return owHasProgramPulse(portnum);
}
//--------------------------------------------------------------------------
// Description:
// Delay for at least 'len' ms
//
/*void msDelay(int len)
{
#if SOCKIT_OWM_HW_DLY
int i;
// compute the number delay cycles depending on delay time
len = (len * S_F_DLY) >> 16;
for (i=0; i<len; i++) {
// create a 960us pause
IOWR_SOCKIT_OWM_CTL (BASE_ONEWIRE, ( S_PWR << SOCKIT_OWM_CTL_POWER_OFST )
| ( S_IEN ? SOCKIT_OWM_CTL_IEN_MSK : 0x00)
| ((S_PWR & 0x1) ? SOCKIT_OWM_CTL_PWR_MSK : 0x00)
| ( SOCKIT_OWM_CTL_CYC_MSK )
| ( SOCKIT_OWM_CTL_DLY_MSK ));
// wait for STX (end of transfer cycle)
while (IORD_SOCKIT_OWM_CTL (BASE_ONEWIRE) & SOCKIT_OWM_CTL_CYC_MSK);
}
#else
#ifdef UCOS_II
// uCOS-II timed delay
OSTimeDlyHMSM(0,0,0,len);
#else
// Altera HAL us delay
usleep (1000*len);
#endif
#endif
}
*/
//--------------------------------------------------------------------------
// 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)
{
#ifdef UCOS_II
// uCOS-II tick counter
OSTimeGet();
#else
// TODO add platform specific code here
return 0;
#endif
}
//--------------------------------------------------------------------------
// 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)
{
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)
{
if (!owHasPowerDelivery(portnum))
return FALSE;
if(owTouchBit(portnum,0x01) != applyPowerResponse)
return FALSE;
if(owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
return FALSE;
return TRUE;
}
//--------------------------------------------------------------------------
// Read 8 bits of communication to the 1-Wire Net and then turn on
// power delivery.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
// OpenCOM to indicate the port number.
//
// Returns: byte read
// FALSE: power delivery failed
//
SMALLINT owReadBytePower(int portnum)
{
SMALLINT getbyte;
if (!owHasPowerDelivery(portnum))
return FALSE;
getbyte = owTouchByte(portnum,0xFF);
if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
return FALSE;
return getbyte;
}
//--------------------------------------------------------------------------
// 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 if adapter is capable of delivering power.
//
SMALLINT owHasPowerDelivery(int portnum)
{
return TRUE;
}
//--------------------------------------------------------------------------
// 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 if adapter is capable of over drive.
//
SMALLINT owHasOverDrive(int portnum)
{
return S_OVD_E;
}
//--------------------------------------------------------------------------
// 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 voltage available
// FALSE program voltage not available
SMALLINT owHasProgramPulse(int portnum)
{
return FALSE;
}
//---------------------------------------------------------------------------
// 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))
{
// 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);
}
//---------------------------------------------------------------------------
// 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>
#include <stdio.h>
//--------------------------------------------------------------//
// Target Specific Information
//--------------------------------------------------------------//
// Altera Nios II + uCOS II
// configuration options available in: sockit_owm_sw.tcl
//#define SOCKIT_OWM_ERR_ENABLE
//#define SOCKIT_OWM_ERR_SMALL
//--------------------------------------------------------------//
// 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
#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
#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
#ifdef __sun__
#include <sys/types.h>
#endif
#ifdef SDCC
//intent of ushort is 2 bytes unsigned.
//for ds390 in sdcc, an int, not a short,
//is 2 bytes.
typedef unsigned int ushort;
#endif
#endif
// general defines
#define WRITE_FUNCTION 1
#define READ_FUNCTION 0
// error codes
// todo: investigate these and replace with new Error Handling library
#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
#define FALSE 0
#define TRUE 1
#ifndef MAX_PORTNUM
#define MAX_PORTNUM 1
#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
//--------------------------------------------------------------//
#ifdef SOCKIT_OWM_ERR_ENABLE
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) owRaiseError(err)
extern void owRaiseError(int);
#define OWASSERT(s,err,ret) if(!(s)){owRaiseError((err));return (ret);}
#endif
#ifdef SOCKIT_OWM_ERR_SMALL
#define OWERROR_DUMP(fileno) /*no-op*/;
#else
//Prints the stack out to the given file.
#define OWERROR_DUMP(fileno) while(owHasErrors()) owPrintErrorMsg(fileno);
extern void owPrintErrorMsg(FILE *);
extern void owPrintErrorMsgStd();
extern char *owGetErrorMsg(int);
#endif
#else
#define OWERROR_CLEAR() /*no-op*/;
#define OWERROR(err) /*no-op*/;
#define OWERROR_DUMP(fileno) /*no-op*/;
#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, ushort reset);
ushort docrc16(int portnum, ushort cdata);
void setcrc8(int portnum, uchar reset);
uchar docrc8(int portnum, uchar x);
#endif //OWNET_H
//---------------------------------------------------------------------------
// Copyright (C) 1999 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.
//---------------------------------------------------------------------------
//
// todoses.C - Acquire and release a Session Todo for general 1-Wire Net
// library
//
// Version: 2.00
// 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
// multiple ports.
//
#include "ownet.h"
#include "sockit_owm.h"
extern sockit_owm_state sockit_owm;
// local function prototypes
SMALLINT owAcquire(int,char *);
void owRelease(int);
//---------------------------------------------------------------------------
// Attempt to acquire a 1-Wire net
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'port_zstr' - zero terminated port name.
//
// Returns: TRUE - success, port opened
//
// The current implementation does not wait for a port to be freed,
// it returns FALSE.
//
SMALLINT owAcquire(int portnum, char *port_zstr)
{
// check if there are enough ports available
if (sockit_owm.own <= portnum) {
// TODO some error message might be added
return FALSE;
}
// check if port is already in use
if ((sockit_owm.use >> portnum) & 0x1) {
return FALSE;
}
// if it is unused take it
else {
sockit_owm.use |= (0x1 << portnum);
return TRUE;
}
}
//---------------------------------------------------------------------------
// Release the previously acquired a 1-Wire net.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
//
void owRelease(int portnum)
{
// check if port is already in use and release it
if ((sockit_owm.use >> portnum) & 0x1) {
sockit_owm.use &= ~(0x1 << portnum);
}
// releasing an unused port is not supported
else {
// TODO some error message might be added
}
}
//---------------------------------------------------------------------------
// Copyright (C) 1999 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.
//---------------------------------------------------------------------------
//
// owTran.C - Transport functions for 1-Wire devices.
//
// Version: 2.01
//
// History: 1.03 -> 2.00 Changed 'MLan' to 'ow'. Added support for
// multiple ports.
// 2.00 -> 2.01 Added support for owError library
//
#include "ownet.h"
//--------------------------------------------------------------------------
// 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.
//
// '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 'TranferLength' 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_len is 160
//
SMALLINT owBlock(int portnum, SMALLINT do_reset, uchar *tran_buf, SMALLINT tran_len)
{
uchar 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;
}
}
// send and receive the buffer
for (i = 0; i < tran_len; i++)
tran_buf[i] = (uchar)owTouchByte(portnum,tran_buf[i]);
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)
{
ushort 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,(ushort)write_cmd);
docrc16(portnum,(ushort)(addr & 0xFF));
docrc16(portnum,(ushort)(addr >> 8));
}
else
setcrc16(portnum,(ushort)addr);
docrc16(portnum,(ushort)write_byte);
// read and calculate the read crc
docrc16(portnum,(ushort)owReadByte(portnum));
lastcrc16 = docrc16(portnum,(ushort)owReadByte(portnum));
// crc should now be 0xB001
if (lastcrc16 != 0xB001)
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);
}
//////////////////////////////////////////////////////////////////////////////
// //
// Minimalistic 1-wire (onewire) master with Avalon MM bus interface //
// //
// Copyright (C) 2010 Iztok Jeras //
// //
//////////////////////////////////////////////////////////////////////////////
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Lesser General Public License //
// as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
// //
//////////////////////////////////////////////////////////////////////////////
#ifndef __SOCKIT_OWM_REGS_H__
#define __SOCKIT_OWM_REGS_H__
//////////////////////////////////////////////////////////////////////////////
// control status register //
//////////////////////////////////////////////////////////////////////////////
#define SOCKIT_OWM_CTL_REG 0
#define IOADDR_SOCKIT_OWM_CTL(base) IO_CALC_ADDRESS_NATIVE(base, SOCKIT_OWM_CTL_REG)
#define IORD_SOCKIT_OWM_CTL(base) (*(( volatile int*)base + SOCKIT_OWM_CTL_REG))
#define IOWR_SOCKIT_OWM_CTL(base, data) (*(( volatile int*)base + SOCKIT_OWM_CTL_REG)) = data
#define SOCKIT_OWM_CTL_DAT_MSK (0x00000001) // data bit
#define SOCKIT_OWM_CTL_DAT_OFST (0)
#define SOCKIT_OWM_CTL_RST_MSK (0x00000002) // reset
#define SOCKIT_OWM_CTL_RST_OFST (1)
#define SOCKIT_OWM_CTL_OVD_MSK (0x00000004) // overdrive
#define SOCKIT_OWM_CTL_OVD_OFST (2)
#define SOCKIT_OWM_CTL_CYC_MSK (0x00000008) // cycle
#define SOCKIT_OWM_CTL_CYC_OFST (3)
#define SOCKIT_OWM_CTL_PWR_MSK (0x00000010) // power (strong pull-up), if there is a single 1-wire line
#define SOCKIT_OWM_CTL_PWR_OFST (5)
#define SOCKIT_OWM_CTL_RSV_MSK (0x00000020) // reserved
#define SOCKIT_OWM_CTL_RSV_OFST (5)
#define SOCKIT_OWM_CTL_IRQ_MSK (0x00000040) // irq status
#define SOCKIT_OWM_CTL_IRQ_OFST (6)
#define SOCKIT_OWM_CTL_IEN_MSK (0x00000080) // irq enable
#define SOCKIT_OWM_CTL_IEN_OFST (7)
#define SOCKIT_OWM_CTL_SEL_MSK (0x00000f00) // port select number
#define SOCKIT_OWM_CTL_SEL_OFST (8)
#define SOCKIT_OWM_CTL_POWER_MSK (0xffff0000) // power (strong pull-up), if there is more than one 1-wire line
#define SOCKIT_OWM_CTL_POWER_OFST (16)
// two common commands
#define SOCKIT_OWM_CTL_DLY_MSK ( SOCKIT_OWM_CTL_RST_MSK | SOCKIT_OWM_CTL_DAT_MSK)
#define SOCKIT_OWM_CTL_IDL_MSK (SOCKIT_OWM_CTL_OVD_MSK | SOCKIT_OWM_CTL_RST_MSK | SOCKIT_OWM_CTL_DAT_MSK)
//////////////////////////////////////////////////////////////////////////////
// clock divider ratio register //
//////////////////////////////////////////////////////////////////////////////
#define SOCKIT_OWM_CDR_REG 1
#define IOADDR_SOCKIT_OWM_CDR(base) IO_CALC_ADDRESS_NATIVE(base, SOCKIT_OWM_CDR_REG)
#define IORD_SOCKIT_OWM_CDR(base) (*(( volatile int*)base + SOCKIT_OWM_CDR_REG))
#define IOWR_SOCKIT_OWM_CDR(base, data) (*(( volatile int*)base + SOCKIT_OWM_CDR_REG)) = data
#define SOCKIT_OWM_CDR_N_MSK (0x0000ffff) // normal mode
#define SOCKIT_OWM_CDR_N_OFST (0)
#define SOCKIT_OWM_CDR_O_MSK (0xffff0000) // overdrive mode
#define SOCKIT_OWM_CDR_O_OFST (16)
#endif /* __SOCKIT_OWM_REGS_H__ */
OBJS_SOCKITOWM = \
sockitowm/crcutil.o \
sockitowm/eep43.o \
sockitowm/findtype.o \
sockitowm/owerr.o \
sockitowm/owlnk.o \
sockitowm/ownet.o \
sockitowm/owtran.o \
sockitowm/temp10.o \
sockitowm/temp28.o \
sockitowm/temp42.o
//---------------------------------------------------------------------------
// 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.
//
// ---------------------------------------------------------------------------
//
//
#include "ownet.h"
#include "temp10.h"
//----------------------------------------------------------------------
// Read the temperature of a DS1920/DS1820 (family code 0x10)
//
// '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 ReadTemperature10(int portnum, uchar *SerialNum, float *Temp)
{
uchar rt=FALSE;
uchar send_block[30],lastcrc8 = 0;
int send_cnt, tsht, i, loop=0;
float tmp,cr,cpc;
int power = 0;
// set the device serial number to the counter device
owSerialNum(portnum,SerialNum,FALSE);
for (loop = 0; loop < 2; loop ++)
{
// check if the chip is connected to VDD
if (owAccess(portnum))
{
owWriteByte(portnum,0xB4);
power = owReadByte(portnum);
}
// access the device
if (owAccess(portnum))
{
// send the convert command and if nesessary start power delivery
if (power) {
if (!owWriteBytePower(portnum,0x44))
return FALSE;
} else {
if (!owWriteByte(portnum,0x44))
return FALSE;
}
// sleep for 1 second
msDelay(1000);
// turn off the 1-Wire Net strong pull-up
if (power) {
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;
if (send_block[2] & 0x01)
tsht |= -128;
tmp = (float)(tsht);
cr = send_block[7];
cpc = send_block[8];
if (((cpc - cr) == 1) && (loop == 0))
continue;
if (cpc == 0)
return FALSE;
else
tmp = tmp - (float)0.25 + (cpc - cr)/cpc;
*Temp = tmp;
// success
rt = TRUE;
break;
}
}
}
}
}
// return the result flag rt
return rt;
}
//---------------------------------------------------------------------------
// 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.
//
// ---------------------------------------------------------------------------
int ReadTemperature10(int,uchar *,float *);
//---------------------------------------------------------------------------
// 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.
// ---------------------------------------------------------------------------
//
// temp28.C - Module to read the DS18B20 - temperature measurement.
//
// ---------------------------------------------------------------------------
//
//
#include "ownet.h"
#include "temp28.h"
//----------------------------------------------------------------------
// Read the temperature of a DS18B20 (family code 0x28)
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
// OpenCOM to indicate the port number.
// 'SerialNum' - Serial Number of DS18B20 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 ReadTemperature28(int portnum, uchar *SerialNum, float *Temp)
{
uchar rt=FALSE;
uchar send_block[30],lastcrc8 = 0;
int send_cnt, tsht, i, loop=0;
int power = 0;
// set the device serial number to the counter device
owSerialNum(portnum,SerialNum,FALSE);
for (loop = 0; loop < 2; loop ++)
{
// check if the chip is connected to VDD
if (owAccess(portnum))
{
owWriteByte(portnum,0xB4);
power = owReadByte(portnum);
}
// access the device
if (owAccess(portnum))
{
// send the convert command and if nesessary start power delivery
if (power) {
if (!owWriteBytePower(portnum,0x44))
return FALSE;
} else {
if (!owWriteByte(portnum,0x44))
return FALSE;
}
// sleep for 1 second
msDelay(1000);
// turn off the 1-Wire Net strong pull-up
if (power) {
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] << 8;
tsht = tsht | send_block[1];
if (tsht & 0x00001000)
tsht = tsht | 0xffff0000;
*Temp = ((float) tsht)/16;
// success
rt = TRUE;
break;
}
}
}
}
}
// return the result flag rt
return rt;
}
//---------------------------------------------------------------------------
// 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.
// ---------------------------------------------------------------------------
//
// temp28.h - Header to read the DS18B20 - temperature measurement.
//
// ---------------------------------------------------------------------------
int ReadTemperature28(int,uchar *,float *);
//---------------------------------------------------------------------------
// 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.
// ---------------------------------------------------------------------------
//
// temp42.C - Module to read the DS28EA00 - temperature measurement.
//
// ---------------------------------------------------------------------------
//
//
#include "ownet.h"
#include "temp42.h"
//----------------------------------------------------------------------
// Read the temperature of a DS28EA00 (family code 0x42)
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
// OpenCOM to indicate the port number.
// 'SerialNum' - Serial Number of DS18B20 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 ReadTemperature42(int portnum, uchar *SerialNum, int *Temp, int *frac )
{
uchar rt=FALSE;
uchar send_block[30],lastcrc8=0;
int send_cnt, i, loop=0;
int power=0;
int val_int=0;
int is_neg=FALSE;
// set the device serial number to the counter device
owSerialNum(portnum,SerialNum,FALSE);
for (loop = 0; loop < 2; loop ++)
{
// check if the chip is connected to VDD
if (owOverdriveAccess(portnum))
{
owWriteByte(portnum,0xB4);
power = owReadByte(portnum);
}
// access the device
if (owOverdriveAccess(portnum))
{
// send the convert command and if nesessary start power delivery
// if (power) {
// if (!owWriteBytePower(portnum,0x44))
// return FALSE;
// } else {
if (!owWriteByte(portnum,0x44))
return FALSE;
// }
// sleep for 1 second
msDelay(1000);
// turn off the 1-Wire Net strong pull-up
if (power) {
if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
return FALSE;
}
// access the device
if (owOverdriveAccess(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
// from twos complement representation
val_int = send_block[1]; // low byte
val_int |= send_block[2] << 8; // high byte
if (val_int & 0x00001000) { // check sign for negative
val_int = ~val_int + 1;
is_neg = TRUE;
}
*Temp = val_int >> 4; // only integer part
*frac = val_int & 0x08; // only 1/2 bit
// success
rt = TRUE;
break;
}
}
}
}
}
// exit overdrive mode
owSpeed(portnum, MODE_NORMAL);
// return the result flag rt
return rt;
}
//---------------------------------------------------------------------------
// 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.
// ---------------------------------------------------------------------------
//
// temp42.h - Header to read the DS28EA00 - temperature measurement.
//
// ---------------------------------------------------------------------------
int ReadTemperature42(int,uchar *,int *,int *);
......@@ -12,8 +12,8 @@
#include "ptpd_netif.h"
#include "i2c.h"
//#include "eeprom.h"
#include "onewire.h"
#include "softpll_ng.h"
#include "persistent_mac.h"
#include "wrc_ptp.h"
......@@ -34,29 +34,20 @@ void wrc_initialize()
uint8_t mac_addr[6], ds18_id[8] = {0,0,0,0,0,0,0,0};
char sfp_pn[17];
uart_init();
mprintf("WR Core: starting up...\n");
timer_init(1);
#if 1
//Generate MAC address
ow_init();
if( ds18x_read_serial(ds18_id) == 0 )
TRACE_DEV("Found DS18xx sensor: %x:%x:%x:%x:%x:%x:%x:%x\n",
ds18_id[7], ds18_id[6], ds18_id[5], ds18_id[4],
ds18_id[3], ds18_id[2], ds18_id[1], ds18_id[0]);
else
TRACE_DEV("DS18B20 not found\n");
#endif
uart_init();
mprintf("WR Core: starting up...\n");
timer_init(1);
mac_addr[0] = 0x08; //
mac_addr[1] = 0x00; // CERN OUI
mac_addr[2] = 0x30; //
mac_addr[3] = ds18_id[3]; // www.maxim-ic.com
mac_addr[4] = ds18_id[2]; // APPLICATION NOTE 186
mac_addr[5] = ds18_id[1]; // Creating Global Identifiers Using 1-Wire® Devices
mac_addr[3] = 0xDE; // fallback MAC if get_persistent_mac fails
mac_addr[4] = 0xAD;
mac_addr[5] = 0x42;
if (get_persistent_mac(mac_addr) == -1) {
mprintf("Unable to determine MAC address\n");
}
TRACE_DEV("Local MAC address: %x:%x:%x:%x:%x:%x\n", mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment