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

📄 main.c

📁 upsd 3200序列的usb驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
// main.c
//
// Main application file.
//
// Author: Jon Moore
//
// Revision History:
//
// 07/22/02 (JAM) V1.0.0 - Initial coding.
// 11/02/02 (JAM) V1.0.5 - Added display of selected flash in LCD.
// 01/21/03 (JAM) V1.0.7 - Fixed '0 length packet' bug.

/*---------------------------------------------------------------------------
Copyright (c) 2002-2003 ST Microelectronics
This example demo code is provided as is and has no warranty,
implied or otherwise.  You are free to use/modify any of the provided
code at your own risk in your applications with the expressed limitation
of liability (see below) so long as your product using the code contains
at least one uPSD products (device).

LIMITATION OF LIABILITY:   NEITHER STMicroelectronics NOR ITS VENDORS OR 
AGENTS SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
--------------------------------------------------------------------------*/

#include <string.h>
#include "general.h"
#include "upsd3200.h"
#include "upsd_xreg.h"
#include "upsd_usb.h"
#include "upsd_cfg.h"

#include "lcd_io.h"
#include "timer.h"

#include "app_intr.h"
#include "ISD51_U1.h"
/////////////////// Prototypes

extern uchar flash_write_with_poll(volatile uchar xdata *addr, uchar dat);
extern uchar flash_boot_write_with_poll(volatile uchar xdata *addr, uchar dat);
extern uchar flash_erase_sector(volatile uchar xdata* addr);
extern uchar flash_boot_erase_sector(volatile uchar xdata* addr);
extern void flash_reset();
extern void flash_boot_reset();

extern void initXREG();

/////////////////// Globals

#define ISD51_DEBUG     0
#define DEMO_TITLE_STR  "USB DEMO V1.0.7\r\n"

extern char xdata LCD_buffer[]; // LCD mirror buffer (lcd_io.c)
extern PSD_REGS UPSD_xreg;      // UPSD registers (upsd_cfg.c)
extern int counter;             // Incremented in do_timer0 (timer_func.c)

MCU_CMD status;         // Status from last command executed
uchar   returnStatus;   // Set TRUE if we should return status record to host
MCU_CMD currentCmd;     // Current command we're working on
MCU_CMD rcvReport;      // Incoming report
uchar   rcvIndex;       // Current byte position in incoming report
MCU_CMD txReport;       // Outgoing report
uchar   txIndex;        // Current byte position in outgoing report

uchar   g_debug0;
uchar   g_debug1;
uchar   g_debug2;

xdata uchar testBuf[256];

/////////////////// initISD()
//
// Initializes serial port for Keil ISD debugger.
//
// NOTE: RCAP2L is currently set for a 24 Mhz DK3000 board frequency.

#if (ISD51_DEBUG)
static void initISD(void) 
{
	T2CON   = 0x34;      // Use Timer 2 as baudrate generator
	RCAP2H  = 0xFF;
    RCAP2L  = 0xC6;      // 9600 baud with 24 MHz clock
	SCON    = 0x50;      // Enable serial uart & receiver
	RI=0;
	TI=0;
	EA = 1;              // Enable global interrupt flag


}
#endif

/////////////////// OnTransmitEP1()
//
// Handler for successful data transmission on endpoint EP1 IN.
//
// Sends next segment of LCD buffer to PC as a HID input report.

static void OnTransmitEP1()
{
    static int bufIndex = 0;        // Current position in LCD buffer
    char txBuf[INPUT_REPORT_SIZE];  // Buffer to send back to PC
    int nBytes = 7;                 // Num bytes of LCD data

    // Store current index into LCD buffer in first byte of tx buffer
    txBuf[0] = bufIndex;
    if (nBytes > (LCD_BUFFER_SIZE - bufIndex))
    {
        nBytes = LCD_BUFFER_SIZE - bufIndex;
    }
    memcpy(txBuf + 1, LCD_buffer + bufIndex, nBytes);

    // Transmit input report to host
    TransmitDataEPx(1, txBuf, INPUT_REPORT_SIZE);

    if ((bufIndex += nBytes) >= LCD_BUFFER_SIZE)
    {
        // Wrap around to start of LCD buffer for next transmission
        bufIndex = 0;
    }
}

/////////////////// initIPs()

static void initIPs()
{
    UsbInitialize();
}

/////////////////// main()

void main()
{
    // Initialize globals
    g_debug0 = 0;
    g_debug1 = 0;
    g_debug2 = 0;

    memset((uchar*)&status, 0, sizeof(status));
    memset((uchar*)&currentCmd, 0, sizeof(currentCmd));
    counter = 0;

    // We are not currently transmitting or receiving feature/input reports
    rcvIndex = CMD_SIZE;
    txIndex = CMD_SIZE;

    // Disable watchdog    
    WDKEY=WD_OFF;

    #if (ISD51_DEBUG)
    initISD();		// init In-system debugger
    //ISDwait();		// initialize uVision2 Debugger and continue program run
    #endif

    initIPs();		// init IP blocks in uPSD
    initXREG();		// init extended regs in xdata
	
    timer0_initialize();
    initLCD();
    printfLCD(DEMO_TITLE_STR);

    while (1)
    {
		//ISDcheck(); 
        g_debug0 = currentCmd.u.cmd;

        // Indicate which flash we are running out of: main (M) or boot (B)

        g_debug2 = ((UPSD_xreg.VM == 0x92) ? 'B' : 'M');
        printfLCD("%x %x %x %c\r", (uchar)(counter >> 8), g_debug0, g_debug1, g_debug2);

        switch (currentCmd.u.cmd)
        {
        case CMD_ERASE:

            if (currentCmd.u.erase.flash == PRIMARY_FLASH)
            {
                status.u.status.ret = flash_erase_sector(
                    (volatile uchar xdata*) currentCmd.u.erase.address);
            }
            else
            {
                status.u.status.ret = flash_boot_erase_sector(
                    (volatile uchar xdata*) currentCmd.u.erase.address);
            }

            // Done
            currentCmd.u.cmd = 0;
            break;

        default:

            break;
        }
    }
}

/////////////////// WriteBufferToFlash()

static void WriteBufferToFlash(uchar flash, uint16 address, uchar* buf, uint16 cb)
{
    // Assume desired flash segment is mapped into high 32k of xdata space
    volatile uchar xdata* p = (volatile uchar xdata*) address;

    if (flash == PRIMARY_FLASH)
    {
        while (cb--)
        {
            flash_write_with_poll(p, *buf++);
            status.u.status.checkSum += *p++;
        }
    }
    else
    {
        while (cb--)
        {
            flash_boot_write_with_poll(p, *buf++);
            status.u.status.checkSum += *p++;
        }
    }
}

/////////////////// ReadBufferFromFlash()

static void ReadBufferFromFlash(uint16 address, uchar* buf, uint16 cb)
{
    // Assume desired flash segment is mapped into high 32k of xdata space
    volatile uchar xdata* p = (volatile uchar xdata*) address;

    while (cb--)
    {
        *buf++ = *p++;
    }
}

/////////////////// OnDeviceConfigured()
//
// Called after device is completely configured.

void OnDeviceConfigured()
{
	// Set up first tx on EP1
	OnTransmitEP1();

    // Disable EP0 IN
    UCON0 &= ~uTX0E;
}

/////////////////// PrepareTransmitSegment()
//
// Prepare next segment of feature report for transmission.
//
// index - current byte index into txReport.

static void PrepareTransmitSegment(uchar index)
{
    uchar cbData;
    uchar i;

    if (returnStatus)
    {
        // Prepare the whole status report on first call
        if (index == 0)
        {
            status.u.cmd               = CMD_STATUS;
            status.u.status.currentCmd = currentCmd.u.cmd;
            status.u.status.page       = UPSD_xreg.PAGE;
            status.u.status.vm         = UPSD_xreg.VM;

            memcpy(&txReport, &status, CMD_SIZE);
        }

        return;
    }

    switch (currentCmd.u.cmd)
    {
    case CMD_READ:

        // First segment needs 0 command byte to indicate data
        if (index == 0)
        {
            cbData = min(currentCmd.u.rw.nBytes, EP0_SIZE - 1);
            index = 1;
            txReport.u.cmd = 0;
        }
        else
        {
            cbData = min(currentCmd.u.rw.nBytes, EP0_SIZE);
        }

        ReadBufferFromFlash(
            currentCmd.u.rw.address,
            txReport.u.buffer + index,
            cbData);

        currentCmd.u.rw.address += cbData;
        if ((currentCmd.u.rw.nBytes -= cbData) == 0)
        {
            // All done
            currentCmd.u.cmd = 0;
        }

        break;

    default:

        for (i = 0; i < EP0_SIZE; i++)
        {
            txReport.u.buffer[index + i] = 0;
        }
        break;
    }
}

/////////////////// OnReportSegmentReceived()
//
// Called as each EP0_SIZE segment of a report is received.

static void OnReportSegmentReceived(uchar cbReceived)
{
    uchar cbData;

⌨️ 快捷键说明

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