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

📄 sclk.c

📁 一个两碟控制的VCD的代码,两碟之间的转动及连续播放,已大量生产的CODE.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright 1996, ESS Technology, Inc. *//* SCCSID @(#)sclk.c	1.4 6/25/98 *//* Routines for TVM module (loader/servo) communication */#ifdef TVM_MODULE#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include "buffer.h"#include "common.h"#include "const.h"#include "vcxi.h"#include "timedef.h"#include "ioport.h"#include "dsc.h"#include "dsa8.h"#include "config.h"#include "util.h"#define TIMER1_COEF 6#define TIMER1_PERIOD -405000#ifdef CUST8_5DISKvoid IR_timer(void);int disk_position, disk_position2;int motor_fault_error, PI;int six_msec_counter , motor_timer ;int changer_control_register;  /* carousel rotation control and status*/int how_many_disk; /* number of position for carousel to turn */int have_disk;  /* 1: current position has a disk, 		   0: no disk in current position */volatile int motor_control_register;  /* tray and pickup control and status */extern volatile int loader_state;#endif /* CUST8_5DISK */#define SCLK_TO_VALUE   32/* defines for operation */#define SET_FALLING									\  {													\  	DETECT_FALLING_EDGE;	\        falling = 1;                                        \        SCLK_count = SCLK_TO_VALUE;                         \  }#define SET_RISING									\  {													\        DETECT_RISING_EDGE;                                  \        falling = 0;                                        \        SCLK_count = SCLK_TO_VALUE;                                     \  }/* define for interrupt state machine   because the interrupt needs quick response, we use switch statement   instead of if else */#define	SDA_IDLE	0#define SDA_RECEIVING 	1#define SDA_SENDING	2#define SDA_TIMEOUT 	3/* for transmission_state*//* for command_state*/#define SDA_INIT	0#define SDA_ARBITRATE	1#define SDA_COMMAND	2#define SDA_PARA	3#define SDA_CHECKSUM	4#define SDA_ACK		5#define SDA_RESET	6/* private variables */PRIVATE DBG_failcount = 0;PRIVATE DBG_rtocnt    = 0;PRIVATE DBG_ftocnt    = 0;PRIVATE volatile int send_receive;PRIVATE volatile int falling;PRIVATE int command_state;                                                 PRIVATE unsigned char CheckSum ; /* set up the check sum first */PRIVATE unsigned char buffer;PRIVATE int bitCount,byteCount,success = 0;PRIVATE int dbg_en;volatile unsigned int * waitptr= (unsigned int *) 0x1c060000;   /* EPROM access, for dummy cycle *//* global variables */PUBLIC unsigned char sda_cmd_msg;PUBLIC unsigned char sda_para[MAX_SDA_PARAMETERS];PUBLIC volatile unsigned char SCLK_data_ready;PRIVATE volatile int SCLK_count;/* imported variables */IMPORT unsigned char dsa_para[];/* function declarations */void		receiving_state(void);void		sending_state(void);void		init_sclk(void);int		trans_dsa(unsigned char);void		SCLK_timer(void);void SCLK_timer(void){    if(send_receive == SDA_IDLE) return;    if(SCLK_count){	--SCLK_count;	if(!SCLK_count)                 /* timed out */	{	    if(falling){		command_state = SDA_INIT;		send_receive = SDA_IDLE;		success = 0;		DBG_ftocnt++;		SET_SDA;	    }	    else{		send_receive = SDA_TIMEOUT;		DBG_rtocnt++;	    }	}    }}void init_sclk(void){    SET_FALLING;    send_receive = SDA_IDLE;    command_state = SDA_INIT;    bitCount = 0;    SET_SDA;                                   /* set SDA line to high */}/* ** sclk_isr * * *  PARAMETERS: * *  DESCRIPTION: * *  RETURNS: * */void sclk_isr(void){    switch (send_receive)    {      case SDA_IDLE:	SET_FALLING;	send_receive = SDA_RECEIVING;	command_state = SDA_INIT;	receiving_state();	break;      case SDA_RECEIVING:	receiving_state();	break;      case SDA_SENDING:	sending_state();        break;      case SDA_TIMEOUT:        SET_FALLING;        command_state = SDA_INIT;        send_receive = SDA_IDLE;        SET_SDA;        success = 0;        break;    }}void receiving_state(){        int i;    switch(command_state)    {      case SDA_INIT:	for (i = 0; i < 80; i++) (void) *waitptr; 	/* have to wait 18 us */	CheckSum = 0xff;	SET_RISING; /* falling edge trigger */	CLEAR_SDA;	command_state = SDA_ARBITRATE; /* wait for rising edge */	break;      case SDA_ARBITRATE:	SET_SDA; /* if sda is pulled up then the bus should be high */	SET_FALLING;	bitCount = 8;	command_state = SDA_COMMAND;	break;      case SDA_COMMAND:	if(falling)	{	    sda_cmd_msg >>= 1;	    if(SDA_HIGH) 		sda_cmd_msg |= 0x80;	    else		sda_cmd_msg &= 0x7f;	    	    SET_RISING;	    bitCount--;	}	else	{	    SET_FALLING;	    if(!bitCount) 	    {		CheckSum ^=  sda_cmd_msg;		bitCount = 8;		byteCount = sda_cmd_msg & 0x0f;		if(byteCount) {                    byteCount--;		    command_state = SDA_PARA;		    buffer = 0; /* initialize to zero */		} else {		    command_state = SDA_CHECKSUM;		}	    } 	}	break;      case SDA_PARA:	if(falling)	{	    buffer >>= 1;	    if(SDA_HIGH) 		buffer |= 0x80;	    else		buffer &= 0x7f;	    SET_RISING;	    bitCount--;			    	}	else	{	    SET_FALLING;	    if (!bitCount)	    {		CheckSum ^= buffer;		if(!byteCount) 		{		    bitCount = 8;                    sda_para[byteCount] = buffer;		    command_state = SDA_CHECKSUM;		    buffer = CheckSum;		}		else		{		    bitCount = 8;                    sda_para[byteCount--] = buffer;		}	    }	    	}	break;      case SDA_CHECKSUM:	if(falling)	{	    buffer >>= 1;	    if(SDA_HIGH) 		buffer |= 0x80;	    else		buffer &= 0x7F;	    SET_RISING;	    bitCount --;	}	else	{	    SET_FALLING;	    if(!bitCount) 	    {		command_state = SDA_ACK;		if (CheckSum != buffer) {		    success = 0;                    DBG_failcount ++;		} else {		    success = 1;		}	    }	}	break;      case SDA_ACK:	if(falling) 	{	    SET_RISING;	    if(success) {		CLEAR_SDA;	    } else {		SET_SDA;	    }	}	else	{	    SET_FALLING;	    command_state = SDA_RESET;	}	break;      case SDA_RESET:	if(falling)	{	    SET_RISING;	}	else	{	    SET_FALLING;	    SET_SDA;	    if(success) SCLK_data_ready = 1;	    command_state = SDA_INIT;	    send_receive = SDA_IDLE;	    	}	break;    }    }void sending_state(){        switch(command_state)    {      case SDA_INIT:	CheckSum = 0xff;	SET_RISING; /* falling edge trigger */	SET_SDA;	command_state = SDA_ARBITRATE; /* wait for rising edge */	break;      case SDA_ARBITRATE:	SET_FALLING;	bitCount = 8;        buffer = sda_cmd_msg;        CheckSum ^= sda_cmd_msg;	command_state = SDA_COMMAND;	break;      case SDA_COMMAND:	if(falling)	{	    if(buffer & 0x01)	    {SET_SDA;}	    else	    {CLEAR_SDA;}	    buffer >>= 1;	    SET_RISING;	    bitCount--;	}	else	{	    SET_FALLING;	    if(!bitCount) 	    {		bitCount = 8;                byteCount = (sda_cmd_msg & 0x07);		if(byteCount)		{		    byteCount--;		    command_state = SDA_PARA;		    buffer = sda_para[byteCount];		    CheckSum ^= buffer;		}		else		{		    command_state = SDA_CHECKSUM;		    buffer = CheckSum;		}	    }	}	break;      case SDA_PARA:	if(falling)	{	    if(buffer & 0x01)	    {SET_SDA;}	    else	    {CLEAR_SDA;}	    buffer >>= 1;	    SET_RISING;	    bitCount--;	    	}	else	{	    SET_FALLING;	    if (!bitCount)	    {		if(!byteCount) 		{		    bitCount = 8;		    command_state = SDA_CHECKSUM;		    buffer = CheckSum;		}		else		{		    byteCount--;		    buffer = sda_para[byteCount];		    CheckSum ^= buffer;		    bitCount = 8;		}	    }	}	break;      case SDA_CHECKSUM:	if(falling)	{	    if(buffer & 0x01)	    {SET_SDA;}	    else	    {CLEAR_SDA;}	    buffer >>= 1;	    SET_RISING;	    bitCount--;	}	else	{	    SET_FALLING;	    if(!bitCount) 	    {		command_state = SDA_ACK;	    }	}	break;      case SDA_ACK:	if(falling) 	{	    TRISTATE_SDA;	    SET_RISING;	}	else	{	    SET_FALLING;	    command_state = SDA_RESET;	}	break;      case SDA_RESET:	if(falling)	{	    SET_RISING;            if(SDA_HIGH) {success = 0;DBG_failcount ++;}	    else	success = 1;	}	else	{	    SET_FALLING;	    SET_SDA;            /* reset bus anyway */	    if(success)	    {		command_state = SDA_INIT;		send_receive = SDA_IDLE;	    }	    else	    {		command_state = SDA_INIT;		send_receive = SDA_IDLE; /* retry if fail */	    }	    	}	break;    }}int trans_dsa(unsigned char cmd){    int i, num_para, time_to_go;    /* wait until SDA_IDLE (0) */    time_to_go = glbTimer + TWO_SECOND;    do {        if (time_to_go < glbTimer) return (0); /* Too long..get out */        VCX_service();    } while (send_receive);    /* Initiate sending */        sda_cmd_msg = cmd;    num_para = (int)(cmd & 0x7);    for (i = 0; i < num_para; i++)     {        sda_para[i] = dsa_para[i];    }        SET_FALLING;    SCLK_data_ready = 0;    CLEAR_SDA;    command_state = SDA_INIT;    send_receive = SDA_SENDING;    time_to_go = glbTimer + TWO_SECOND;    do {        if (glbTimer > time_to_go) return (0); /* Too long..get out */        VCX_service();    } while (send_receive);        return (1);}/******************************    Cust8 Disk Changer Carousel State Machine  N: 		1	2	3	4	5	6	7 Drawer		out  	transit	in	in	in	transig	out Pickup		down	down	down	transit up	up	up State(N)	1	2	3	4	5	6	7 PI1		0	0	0	1	1	1	0 PI2		0	1	0	0	1	0	0 Carousel turn 	OK	OK	OK	NO	NO	NO	Toggle Motor Rotaion 	CW	CW	CQ	CCW	CCW	CCW	CW								then CCW Init(N)	3	3 	3	3	3	3	5 Playing	NO	NO	NO	NO	Yes	Yes	Yes Resting(N)	5	5	5	5	5	5	5 ****************************** *//* set Timer_1 to be a 1 msec counter.  67500/67.5Mhz = 1msec *//* int timer1_period = -67500;*//* 6 milisecond would be -67500 * 6 = 405000 */void RISC_timer1_interrupt_service(void){                                          mvd[riface_timer1] = TIMER1_PERIOD;       mvd[riface_clear_timer1] = 0;  /* clearing interrupt flag */    SCLK_timer();#ifdef CUST8_5DISK                                                 six_msec_counter++;                             motor_timer++;                 if ((changer_control_register & 0x8000) != 0) {	rotate_carousel_and_find_position();            }    rotate_drawer_motor();    IR_timer();#endif}void RISC_start_timer1(void){        mvd[riface_clear_timer1] = 0; 	/* clear timer irq */    mvd[riface_timer1] = TIMER1_PERIOD;    enable_int(tim1); }#ifdef CUST8_5DISK                                             /* void rotate_carousel_and_find_position(void) * Rotates Carousel and updates the current position

⌨️ 快捷键说明

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