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

📄 isr.c

📁 USB接口芯片D12在STC单片机上的驱动开发
💻 C
字号:
/*
   //*************************************************************************
   //
   //                  P H I L I P S   P R O P R I E T A R Y
   //
   //           COPYRIGHT (c)   1997 BY PHILIPS SINGAPORE.
   //                     --  ALL RIGHTS RESERVED  --
   //
   // File Name:	ISR.C
   // Author:		Wenkai Du/Chen Hu
   // Created:		19 Dec 97
   // Modified:
   // Revision:		1.0
   //
   //*************************************************************************
   //
   // 98/11/25 		Added I/O mode Main endpoints access. (WK)
   //*************************************************************************
   */
#include <stdio.h>
#include <string.h>


#include <reg51.h>                /* special function register declarations   */
#include "epphal.h"
#include "d12ci.h"
#include "mainloop.h"
#include "usb100.h"

unsigned char idata GenEpBuf[EP1_PACKET_SIZE];
unsigned char idata MainEpBuf[EP2_PACKET_SIZE];

bdata unsigned char  i_st;	//interrupt state reg
sbit ctl_out_enp = i_st ^ 0;
sbit ctl_in_enp = i_st ^ 1;
sbit enp1_out = i_st ^ 2;
sbit enp1_in = i_st ^ 3;
sbit main_out_enp = i_st ^ 4;
sbit main_in_enp = i_st ^ 5;
sbit bus_rst = i_st ^ 6;
sbit sus_chg = i_st ^ 7;
//sbit dma = i_st ^ 8;
extern void bus_reset(void);

extern void ep0_txdone(void);
extern void ep0_rxdone(void);

extern void ep1_txdone(void);
extern void ep1_rxdone(void);

extern void main_txdone(void);
extern void main_rxdone(void);

extern void dma_eot(void);

/*
//*************************************************************************
//  Public static data
//*************************************************************************
*/

EPPFLAGS bEPPflags;

/* Control endpoint TX/RX buffers */
extern CONTROL_XFER ControlData;

/* ISR static vars */

IO_REQUEST idata ioRequest;

unsigned long ClockTicks = 0;


timer_isr() interrupt 1 using 1
{
	DISABLE;
	ClockTicks++;
	bEPPflags.bits.timer = 1;
	ENABLE;
}


usb_isr() interrupt 0 using 0	//use interrupt 0 for d12
{
	DISABLE;		//close all interrupt enable
	fn_usb_isr();	//usb interrupt service progame.
	ENABLE;			//open all interrupt enable
}


void fn_usb_isr()
{
//#define DEBUG
	bEPPflags.bits.in_isr = 1;

	i_st = D12_ReadInterruptRegister();
#ifdef	DEBUG
	printf("\ninterrupt register value is 0x%bx.\n", i_st);
#endif
	if(i_st != 0) {
		if(bus_rst)
		{
			printf("\nBus reset.\t\t");
			bus_reset();
			bEPPflags.bits.bus_reset = 1;

		}
		else {
/*			if(dma)
			{
				printf("\nstep 2...dma operation is completed.\t\t");
				dma_eot();
			}
*/
			if(sus_chg)
			{
				printf("\nsuspend change.\t\t");
				bEPPflags.bits.suspend = 1;
			}

			if(ctl_in_enp)
			{
//				printf("\nendpoint 0 in.\t\t");
				ep0_txdone();
			}
			if(ctl_out_enp)
			{
//				printf("\nendpoint 0 out.\t\t");
				ep0_rxdone();
			}
			if(enp1_in)
			{
//				printf("\nendpoint 1 in.\t\t");
				ep1_txdone();
			}
			if(enp1_out)
			{
//				printf("\nendpoint 1 out.\t\t");
				ep1_rxdone();
			}
			if(main_in_enp)
			{
//				printf("\nendpoint 2 in.\t\t");
				main_txdone();
			}
			if(main_out_enp)
			{
//				printf("\nendpoint 2 out.\t\t");
				main_rxdone();
			}
		}
	}

	bEPPflags.bits.in_isr = 0;
}

void bus_reset(void)
{
//	return;
}

void ep0_rxdone(void)
{
	unsigned char ep_last, i;

	ep_last = D12_ReadLastTransactionStatus(0); // Clear interrupt flag

	if (ep_last & D12_SETUPPACKET) {

		ControlData.wLength = 0;
		ControlData.wCount = 0;

		if( D12_ReadEndpoint(0, (unsigned char *)(&(ControlData.DeviceRequest)),
			sizeof(ControlData.DeviceRequest)) != sizeof(DEVICE_REQUEST) ) {

			D12_SetEndpointStatus(0, 1);
			D12_SetEndpointStatus(1, 1);
			bEPPflags.bits.control_state = USB_IDLE;
			
			return;
		}


		ControlData.DeviceRequest.wValue = SWAP(ControlData.DeviceRequest.wValue);
		ControlData.DeviceRequest.wIndex = SWAP(ControlData.DeviceRequest.wIndex);
		ControlData.DeviceRequest.wLength = SWAP(ControlData.DeviceRequest.wLength);


		// Acknowledge setup here to unlock in/out endp
		D12_AcknowledgeEndpoint(0);
		D12_AcknowledgeEndpoint(1);

		ControlData.wLength = ControlData.DeviceRequest.wLength;
		ControlData.wCount = 0;

		if (ControlData.DeviceRequest.bmRequestType & (unsigned char)USB_ENDPOINT_DIRECTION_MASK) {
			bEPPflags.bits.setup_packet = 1;
			bEPPflags.bits.control_state = USB_IDLE;		/* get command */
		}
		else {
			if (ControlData.DeviceRequest.wLength == 0) {
				bEPPflags.bits.setup_packet = 1;
				bEPPflags.bits.control_state = USB_IDLE;		/* set command */
			}
			else {
				if(ControlData.DeviceRequest.wLength > MAX_CONTROLDATA_SIZE) {
					bEPPflags.bits.control_state = USB_IDLE;
					D12_SetEndpointStatus(0, 1);
					D12_SetEndpointStatus(1, 1);
				}
				else {
					bEPPflags.bits.control_state = USB_RECEIVE;	/* set command with OUT token */
				}
			} // set command with data
		} // else set command
	} // if setup packet

	else if (bEPPflags.bits.control_state == USB_RECEIVE) {
		i =	D12_ReadEndpoint(0, ControlData.dataBuffer + ControlData.wCount,
			EP0_PACKET_SIZE);

		ControlData.wCount += i;
		if( i != EP0_PACKET_SIZE || ControlData.wCount >= ControlData.wLength) {
			bEPPflags.bits.setup_packet = 1;
			bEPPflags.bits.control_state = USB_IDLE;
		}
	}

	else {
		bEPPflags.bits.control_state = USB_IDLE;
	}
}

void ep0_txdone(void)
{
//#define DEBUG
	short i = ControlData.wLength - ControlData.wCount;

	D12_ReadLastTransactionStatus(1); // Clear interrupt flag

	if (bEPPflags.bits.control_state != USB_TRANSMIT) 
		return;
#ifdef DEBUG
	printf("\nrun here...\n");
#endif
	if( i >= EP0_PACKET_SIZE) {
		D12_WriteEndpoint(1, ControlData.pData + ControlData.wCount, EP0_PACKET_SIZE);
		ControlData.wCount += EP0_PACKET_SIZE;

		bEPPflags.bits.control_state = USB_TRANSMIT;
	}
	else if( i != 0) {
		D12_WriteEndpoint(1, ControlData.pData + ControlData.wCount, i);
		ControlData.wCount += i;

		bEPPflags.bits.control_state = USB_IDLE;
	}
	else if (i == 0){
		D12_WriteEndpoint(1, 0, 0); // Send zero packet at the end ???

		bEPPflags.bits.control_state = USB_IDLE;
	}
}

void dma_eot(void)
{
	if(bEPPflags.bits.dma_state == DMA_PENDING)
		bEPPflags.bits.setup_dma = 1;
	else
		bEPPflags.bits.dma_state = DMA_IDLE;
}

void ep1_txdone(void)
{
	D12_ReadLastTransactionStatus(3); /* Clear interrupt flag */
}

void ep1_rxdone(void)
{
#define DEBUG
	unsigned char len;

	D12_ReadLastTransactionStatus(2); /* Clear interrupt flag */

	len = D12_ReadEndpoint(2, GenEpBuf, sizeof(GenEpBuf));
	#ifdef DEBUG
		printf("\nrecive len=0x%bx.\n", len);
	#endif
	D12_AcknowledgeEndpoint(2);
	D12_AcknowledgeEndpoint(3);
	if(len != 0)
		bEPPflags.bits.ep1_rxdone = 1;
}

void main_txdone(void)
{
	D12_ReadLastTransactionStatus(5); /* Clear interrupt flag */

}

void main_rxdone(void)
{
#define DEBUG
	unsigned char len;

	D12_ReadLastTransactionStatus(4); /* Clear interrupt flag */
	
	len = D12_ReadEndpoint(4, MainEpBuf, sizeof(MainEpBuf));
	#ifdef DEBUG
		printf("\nrecive len = 0x%bx.\n", len);
	#endif
	D12_AcknowledgeEndpoint(4);
	D12_AcknowledgeEndpoint(5);
	if(len != 0)
		bEPPflags.bits.ep2_rxdone = 1;

}

⌨️ 快捷键说明

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