⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds2401.c

📁 1-wire ds2401 driver for vxworks
💻 C
字号:
/*********************************************************************************
**  File name  : ds2401.c
**  Created By : He jianguo
**	Discription
** 		DS2401 driver
**	Log
**	2006/09/01	original created
***********************************************************************************/
#include "vxWorks.h"
#include "stdio.h"
#include "drv/multi/ppc860Siu.h"

#include "ds2401.h"

#define DS2401_PIN			0x0001			/* Pc15 */

#define READ_TIMEBASE(x)   __asm__ volatile (" mftb %0": "=r" (x))

static VINT32 immr_base = 0xFF000000;

#define DS_READ_PIN				(*PCDAT(immr_base) &   DS2401_PIN)
#define DS_SET_PIN_LOW			*PCDIR(immr_base) |=  DS2401_PIN; *PCDAT(immr_base) &= ~DS2401_PIN
#define DS_RELEASE_PIN			(*PCDIR(immr_base) &= ~DS2401_PIN)

LOCAL unsigned long get_tbclk (void)
{
	/*VINT32 immr_base = (vxImmrIsbGet() & 0xFFFF0000);*/
	
	if(*SCCR(immr_base)& SCCR_TBS){
		return (50000000 / 16);				/* GCLK2(50MHz)/16 */
		}
	
	return (10000000 / 4);					/* OSCCLK(10MHz)/4 */
}

LOCAL unsigned long usec2ticks(unsigned long usec)
{
	unsigned long ticks;

	if (usec < 1000) {
		ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
	} else {
		ticks = ((usec / 10) * (get_tbclk() / 100000));
	}

	return (ticks);
}

void udelay(unsigned long usec)
{
  register unsigned timer, end_timer;

  READ_TIMEBASE(end_timer);
  end_timer += usec2ticks(usec);

  do {
    READ_TIMEBASE(timer);
  } while(timer < end_timer);
}

LOCAL INT16 calc_crc(INT16 crc, INT16 byte)
{
	INT16 i;

	for(i = 0; i < 8; i++) {
		if((byte ^ crc) & 1)
			crc ^= 0x0118;				/* x^8 + x^5 + x^4 + 1 */

		crc >>= 1;
		byte >>= 1;
		}
	
  return crc;
}

LOCAL void ds_pin_setup(void)
{
	*PCPAR(immr_base) &= ~DS2401_PIN;		/* GPIO */
	*PCDIR(immr_base) |=  DS2401_PIN;		/* Output */
	/**PCODR(immr_base) |=  DS2401_PIN;	*/	/* Open-drain */
	*PCSO(immr_base) &=  ~DS2401_PIN;     	/* Open-drain */
	*PCDAT(immr_base) |=  DS2401_PIN;		/* Data: 1 */
}
/*
static void ds_drv_bus_low(void)
{
	*PADAT(immr_base) &= ~DS2401_PIN;
}

static void ds_release_bus(void)
{
	*PADAT(immr_base) |= DS2401_PIN;
}

static UINT8 ds_read_pin(void)
{
	*PADAT(immr_base) &= ~DS2401_PIN;
}

*/

LOCAL int ds_reset(void)
{
	int i; 
	UINT16 ret_val = DS2401_PIN;
	
	DS_SET_PIN_LOW;
	udelay(520);

	DS_RELEASE_PIN;
	udelay(60);
 
	for(i = 0; i< 60; i++){
  		ret_val = (UINT16)(DS_READ_PIN);
		if (ret_val == 0)	
			break;
		else				
			udelay(5);
		}
  
	udelay(500);
  
	if (ret_val)	
		return DS_FAILURE;
	else			
		return DS_SUCCESS;
}

LOCAL int ds_write_byte(UINT8 byte)
{
	int i;
	
	for(i = 0; i < 8; i++, byte >>= 1) {
		if(byte & 1){
			DS_SET_PIN_LOW;
			udelay(1);
			DS_RELEASE_PIN;
			udelay(85);
			}
		else{
			DS_SET_PIN_LOW;
			udelay(75);
			DS_RELEASE_PIN;
			udelay(15);			
			}
		
		if(DS_READ_PIN == 0)
			return DS_FAILURE;
			
		}
	return DS_SUCCESS;
}

LOCAL INT16 ds_read_byte(void)
{
	int i;
	UINT8	byte = 0, bit = 1;
	
	for(i = 0; i < 8; i++, bit <<= 1) {
		DS_SET_PIN_LOW;
		udelay(1);
		DS_RELEASE_PIN;
		udelay(8);
		if(DS_READ_PIN)
			byte |= bit;
		udelay(110);
		if(DS_READ_PIN == 0)
			return DS_FAILURE;
		}
	return (INT16)byte;
}

int get_ds2401_sn(ds_serial_t *pSn)
{
	int i, retry_ctr = 10, sn_byte_cnt = 8;
	UINT8 *pData;
	UINT16 crc;
	INT16 data;
	
	ds_pin_setup();

	while(retry_ctr--) {
		if(ds_reset() != DS_SUCCESS){
			/*printf("Reset ds2401 error\n");*/
			continue;
			}
		/* Write the command to read the ROM */
		if(ds_write_byte(0x33) != DS_SUCCESS){
			/*printf("Write ds2401 error\n");*/
			continue;
			}

		/* Read all eight bytes */
		for(i = 0, crc = 0, pData = (UINT8 *)pSn; i < sn_byte_cnt; i++) {
			if((data = ds_read_byte()) == DS_FAILURE)
        		break;

			*pData++ = (UINT8)data;                              /* Store the byte */
			crc = calc_crc(crc, (UINT16)data);
			}

		if(i == sn_byte_cnt && crc == 0)			
			return DS_SUCCESS;
		else{
			if(retry_ctr > 0)
				continue;
			else
				return DS_FAILURE;
			}
			
		}

	return DS_FAILURE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -