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);
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
//---------------------------------------------------------------------------
// 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