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

📄 autoroad.c

📁 I/O board control program for U.C.W./Sirael Dana robot.
💻 C
📖 第 1 页 / 共 2 页
字号:
   PCON     = 0x80;
   SCON0    = 0x50;
   T2CON    = 0x00;

   /* enable serial port 0 interrupt */
   ES0      = 1;

   /* start timer 1 */
   TR1      = 1;
}

static void setup_timer2_int()
{
   /*
    * configure timer2:
    * + timer with auto reload
    * + rate is 250 Hz
    */
   T2CON  = 0x00;
   RCAP2H = TH2 = 0xc1;
   RCAP2L = TL2 = 0x80;

   /* enable timer 2 interrupt */
   ET2    = 1;
   /* clear timer 2 interrupt */
   TF2    = 0;
   /* start timer 2 */
   TR2    = 1;
}

static Byte i2c_wait_done(Byte check)
{
 	Byte status;

	do {
		status = I2CS;
	} while (~status & I2C_DONE);

	return ((status ^ I2C_ACK) & check);
}

static void setup_pcf8591(Byte index)
{
	while (I2CS & 0x40);

	I2CS  = I2C_START;
	I2DAT = I2C_PCF8591_ADDR | (index << 1) | I2C_WRITE;
	if (i2c_wait_done(I2C_BERR | I2C_ACK))
		goto fail_s;

	I2DAT = 0x45;	/* Enable D/A, 4 SE channels, select chan 3 */
	if (i2c_wait_done(I2C_BERR | I2C_ACK))
		goto fail_s;

	I2DAT = 0x00;	/* Output 0V on D/A */
	if (i2c_wait_done(I2C_BERR | I2C_ACK))
		goto fail_s;

fail_s:

	I2CS = I2C_STOP;
}

/*
 * _sdcc_external_startup()
 *
 * Earliest possibility to disconnect for re-enumeration.
 * This is not necessary when the firmware resides in the onboard I2C EEPROM
 * where the master control is handed back to the USB core to let it talk to
 * the host. Afterwards we perform a clean re-enumeration.
 */

Byte _sdcc_external_startup()
{
#ifndef ROM_FIRMWARE
   /* disconnect immediately */
   USBCS &= ~0x04;	/* tristate the Disconnect pin */
   USBCS |= 0x08;	/* disconnect USB core */
#endif
   return(0);
}

/*
 * main()
 *
 * Main function.
 * Call initializers, trigger re-enumeration and stay busy in endless loop.
 * All functionality is handled in the interrupt service routines.
 */

void main()
{
	int loop;

#ifndef ROM_FIRMWARE
	loop = 16384;
	while (loop-- > 0);	/* wait */
#endif

	init_variables();

	setup_usb_int();
	setup_io();

	setup_pcf8591(0);
	setup_i2c_int();

	setup_ser_int();
	setup_timer2_int();

#ifndef ROM_FIRMWARE
	USBCS |= 0x02;		/* activate RENUM */
	USBCS &= ~0x08;		/* deactivate DISCON */
	USBCS |= 0x04;		/* release tristate on Disconnect pin */
#endif

	while (1) {		/* heartbeat */
		loop = 32767;
		while (loop-- > 0);
		OUTC ^= 0x10;
	}
}

/*
 * I/O board control header file for U.C.W./Sirael Dana robot.
 *
 * Version 1.0
 *
 * Copyright (c) 2002 Vojtech Pavlik
 * Copyright (c) 2001 Arnim Laeuger
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version. See also the file COPYING which came with this
 * application.
 */

/*
 * USB descriptor types
 */

#define USB_DT_DEVICE			0x01
#define USB_DT_CONFIG			0x02
#define USB_DT_STRING			0x03
#define USB_DT_INTERFACE		0x04
#define USB_DT_ENDPOINT			0x05

/* our main data type */
typedef unsigned char Byte;

/* mapping for the setup packet */
struct setup_dat {
      Byte bmRequestType;
      Byte bRequest;
      Byte wValueL;
      Byte wValueH;
      Byte wIndexL;
      Byte wIndexH;
      Byte wLengthL;
      Byte wLengthH;
};

typedef struct setup_dat setup_dat;

/*
 * i2c defines
 */

#define I2C_START		0x80
#define I2C_STOP		0x40
#define I2C_LAST		0x20
#define I2C_BERR		0x04
#define I2C_ACK			0x02
#define I2C_DONE		0x01

#define I2C_WRITE		0x00
#define I2C_READ		0x01
#define I2C_PCF8591_ADDR	0x90
#define I2C_MAX127_ADDR		0x28
#define I2C_MAX1236_ADDR	0x34

#define I2C_PCF8591		0
#define I2C_MAX127R		1
#define I2C_MAX127W		2
#define I2C_MAX1236		3

/*
 * Other defines
 */

#define MISC_BUFFER_SIZE 8
#define I2C_BUFFER_SIZE 16
#define TX_BUFFER_SIZE	32
#define IN1_BUFFER_SIZE (MISC_BUFFER_SIZE+I2C_BUFFER_SIZE)

/*
 * global variables
 */

/* i2c data buffer */

static data int beat;

static data Byte i2c_buffer[I2C_BUFFER_SIZE];
static data Byte tx_buffer[TX_BUFFER_SIZE];

static data Byte i2c_mode;
static data Byte i2c_state;
static data Byte i2c_index;

static data Byte tx_head;
static data Byte tx_tail;
static bit tx_running;

/*
 * USB related stuff
 */

/* report has changed, in1buf should be transferred on the next opportunity */
static bit in1_change;

/* busy semaphore for IN1 endpoint */
static bit in1_busy;                 /* apply_reset_values */

/* the device descriptor */
static code Byte dev_desc[] = {
   0x12, // bLength             : Length of Descriptor
   0x01, // bDescriptorType     : Descriptor Type = Device
   0x10, // bcdUSB (L)          : USB Specification Version 1.00 (L)
   0x01, // bcdUSB (H)          : USB Specification Version 1.00 (H)
   0xff, // bDeviceClass        : Device Class (0xff is Vendor Specific)
   0xff, // bDeviceSubClass     : Device Sub-Class (0xff is Vendor Specific)
   0xff, // bDeviceProtocol     : Device Protocol (0xff is Vendor Specific)
   0x40, // bMaxPacketSize0     : Maximum Packet Size for EP0
   0xbe, // idVendor (L)        : Vendor ID (L)
   0xba, // idVendor (H)        : Vendor ID (H)
   0x04, // idProduct (L)       : Product ID (L)
   0x00, // idProduct (H)       : Product ID (H)
   0x00, // bcdDevice (L)       : Device Release Number (BCD,L)
   0x01, // bcdDevice (H)       : Device Release Number (BCD,H)
   0x01, // iManufacturer       : Manufacturer Index String
   0x02, // iProduct            : Product Index String
   0x00, // iSerialNumber       : Serial Number Index String
   0x01  // bNumConfigurations  : Number of Configurations in this Device
};

/* the configuration descriptor */
static code Byte conf_desc[] = {
   0x09, // bLength             : Length of Descriptor
   0x02, // bDescriptorType     : Descriptor Type = Configuration
   0x20, // wTotalLength (L)    : Total Length (L) including Interface and Endpoint
   0x00, // wTotalLength (H)    : Total Length (H)
   0x01, // bNumInterfaces      : One Interface in this Configuration
   0x01, // bConfigurationValue : Configuration Value Used by Set_Configuration Request
         //                       to Select this Configuration
   0x00, // iConfiguration      : Index of String Describing this Configuration
   0x80, // bmAttributes        : Attributes
   0x64, // MaxPower            : Maximum Power

   //
   // The interface descriptor
   //

   // Interface 0, alternate setting 0
   0x09, // bLength             : Length of Descriptor
   0x04, // bDescriptorType     : Descriptor Type = Interface
   0x00, // bInterfaceNumber    : Zero-based index of this Interface
   0x00, // bAlternateSetting   : Alternate Setting
   0x02, // bNumEndpoints       : Number of Endpoints in this Interface
   0xff, // bInterfaceClass     : Interface Class
   0xff, // bInterfaceSubClass  : Interface Sub-Class (boot interface)
   0xff, // bInterfaceProtocol  : Interface Protocol (keyboard)
   0x00, // iInterface          : Index to String Descriptor for this Interface
   
   // Endpoint Descriptor
   0x07, // bLength             : Length of Descriptor
   0x05, // bDescriptorType     : Descriptor Type = Endpoint
   0x81, // bEndpointAddress    : Endpoint Address
   0x03, // bmAttributes        : Endpoint Attributes = INT
   0x40, // wMaxPacketSize (L)  : Maximum Packet Size (L)
   0x00, // wMaxPacketSize (H)  : Maximum Packet Size (H)
   0x05, // bInterval           : Polling intervall in Milliseconds

   // Endpoint Descriptor
   0x07, // bLength             : Length of Descriptor
   0x05, // bDescriptorType     : Descriptor Type = Endpoint
   0x01, // bEndpointAddress    : Endpoint Address
   0x03, // bmAttributes        : Endpoint Attributes = INT
   0x08, // wMaxPacketSize (L)  : Maximum Packet Size (L)
   0x00, // wMaxPacketSize (H)  : Maximum Packet Size (H)
   0x05  // bInterval           : Polling intervall in Milliseconds

};

/* language ID string descriptor */
static code Byte string_langid[] = { 0x04, 0x03, 0x00, 0x00 };

/* manufacturer string descriptor */
static code Byte string_mfg[] = {
   0x1f, 0x03,
   'U',0, '.',0, 'C',0, '.',0, 'W',0, '.',0, ' ',0, '/',0,
   ' ',0, 'S',0, 'i',0, 'r',0, 'a',0, 'e',0, 'l',0
};

/* product string descriptor */
static code Byte string_prod[] = {
   0x1c, 0x03,
   'D',0, 'a',0, 'n',0, 'a',0, ' ',0, 'I',0, '/',0, 'O',0,
   ' ',0, 'U',0, 'n',0, 'i',0, 't',0
};

#define NUM_STRING 3

static data unsigned int string_index[NUM_STRING] = {
   (unsigned int)string_langid,
   (unsigned int)string_mfg,
   (unsigned int)string_prod
};

/*
 * global pointers
 */
#define in0buf(n)  (&IN0BUF)[n]
#define in1buf(n)  (&IN1BUF)[n]
#define out0buf(n) (&OUT0BUF)[n]
#define out1buf(n) (&OUT1BUF)[n]
static xdata setup_dat * data sdat = (xdata setup_dat *)&SETUPDAT;

/*
 *
 * $Id: ezusb_reg.h,v 1.2 2003/01/10 17:03:08 vojtech Exp $
 *
 * Change History:
 *
 * $Log: ezusb_reg.h,v $
 * Revision 1.2  2003/01/10 17:03:08  vojtech
 * Versio 0.06: Input works.
 *
 * Revision 1.1  2003/01/09 15:02:13  vojtech
 * Firmware for the EzUSB I/O board.
 *
 * Revision 1.3  2002/03/03 23:35:32  arniml
 * add TOGCTL
 *
 * Revision 1.2  2001/08/15 18:24:03  arniml
 * added some register definitions for serial port 1 and IN2 endpoint
 *
 * Revision 1.1.1.1  2001/07/14 15:32:37  arniml
 * initial import
 *
 *
 */


sbit  at 0xe8 EUSB;
sbit  at 0xac ES0;
sbit  at 0xae ES1;
sbit  at 0x98 RI_0;
sbit  at 0x99 TI_0;
sbit  at 0xc0 RI_1;
sbit  at 0xc1 TI_1;
sbit  at 0xca TR2;
sbit  at 0xcf TF2;
sbit  at 0xad ET2;

sfr   at 0x91 EXIF;
sfr   at 0xe8 EIE;
sfr   at 0x98 SCON0;
sfr   at 0xc0 SCON1;
sfr   at 0x99 SBUF0;
sfr   at 0xc1 SBUF1;
sfr   at 0x8e CKCON;
sfr   at 0xc8 T2CON;
sfr   at 0xca RCAP2L;
sfr   at 0xcb RCAP2H;
sfr   at 0xcc TL2;
sfr   at 0xcd TH2;

xdata at 0x7fb4 unsigned char EP0CS;
xdata at 0x7f00 unsigned char IN0BUF;
xdata at 0x7fb5 unsigned char IN0BC;
xdata at 0x7ec0 unsigned char OUT0BUF;
xdata at 0x7fc5 unsigned char OUT0BC;

xdata at 0x7fb6 unsigned char IN1CS;
xdata at 0x7e80 unsigned char IN1BUF;
xdata at 0x7fb7 unsigned char IN1BC;
xdata at 0x7e40 unsigned char OUT1BUF;
xdata at 0x7fc6 unsigned char OUT1CS;
xdata at 0x7fc7 unsigned char OUT1BC;

xdata at 0x7e00 unsigned char IN2BUF;
xdata at 0x7fb9 unsigned char IN2BC;
xdata at 0x7fb8 unsigned char IN2CS;
xdata at 0x7fc9 unsigned char OUT2BC;
xdata at 0x7fc8 unsigned char OUT2CS;
xdata at 0x7dc0 unsigned char OUT2BUF;
xdata at 0x7d00 unsigned char IN4BUF;
xdata at 0x7fbd unsigned char IN4BC;
xdata at 0x7fbc unsigned char IN4CS;
xdata at 0x7f93 unsigned char PORTACFG;
xdata at 0x7f94 unsigned char PORTBCFG;
xdata at 0x7f95 unsigned char PORTCCFG;
xdata at 0x7f96 unsigned char OUTA;
xdata at 0x7f97 unsigned char OUTB;
xdata at 0x7f98 unsigned char OUTC;
xdata at 0x7f99 unsigned char PINSA;
xdata at 0x7f9a unsigned char PINSB;
xdata at 0x7f9b unsigned char PINSC;
xdata at 0x7f9c unsigned char OEA;
xdata at 0x7f9d unsigned char OEB;
xdata at 0x7f9e unsigned char OEC;

xdata at 0x7fa9 unsigned char IN07IRQ;
xdata at 0x7faa unsigned char OUT07IRQ;
xdata at 0x7fac unsigned char IN07IEN;
xdata at 0x7fad unsigned char OUT07IEN;
xdata at 0x7fab unsigned char USBIRQ;
xdata at 0x7fae unsigned char USBIEN;

xdata at 0x7faf unsigned char USBBAV;
xdata at 0x7fd6 unsigned char USBCS;
xdata at 0x7fd7 unsigned char TOGCTL;
xdata at 0x7fd4 unsigned char SUDPTRH;
xdata at 0x7fd5 unsigned char SUDPTRL;
xdata at 0x7fe8 unsigned char SETUPDAT;
xdata at 0x7fa5 unsigned char I2CS;
xdata at 0x7fa6 unsigned char I2DAT;
xdata at 0x7fde unsigned char IN07VAL;
xdata at 0x7fdf unsigned char OUT07VAL;
xdata at 0x7fe3 unsigned char AUTOPTRH;
xdata at 0x7fe4 unsigned char AUTOPTRL;
xdata at 0x7fe5 unsigned char AUTODATA;

⌨️ 快捷键说明

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