sio_usbtest.c
上传用户:dahaojd
上传日期:2008-01-29
资源大小:14357k
文件大小:7k
- /*
- * Copyright 2003 by Texas Instruments Incorporated.
- * All rights reserved. Property of Texas Instruments Incorporated.
- * Restricted rights to use, duplicate or disclose this code are
- * granted through contract.
- *
- */
- /* "@(#) DDK 1.10.00.23 07-02-03 (ddk-b12)" */
- /*
- * ======== sio_usbtest.c ========
- *
- * This example demonstrates the use of the USB IOM driver with SIO APIs by
- * using the DIO class driver with a User-Defined device USB mini-driver.
- * This is the loopback application where data is read
- * from an input SIO stream, then sent back via an output SIO stream.
- *
- * The following objects need to be created in the DSP/BIOS
- * configuration for this application:
- *
- * * A UDEV object, which links in a user device driver. In this
- * case the UDEV is coded based IOM device driver.
- * * DIO objects, which links the UDEV object. Channel Parameters are
- * configured here for each USB channel used.
- * * A TSK object, with the function to run set to the function myUsbTask
- * defined in this file.
- * * A LOG named trace for debug and status output.
- * * A SEM object used to block until the USB bus is connected.
- */
- #include <std.h>
- #include <c5509_usb.h>
- #include <log.h>
- #include <sio.h>
- #include <sys.h>
- #define SIOBUFSIZE 256 /* SIO buffer size */
- /*
- * USB buffer size is one word greater than data size because the first word
- * is used to save actual transfer length(in bytes) for the USB peripheral.
- * If needed, applications can used this first field to read the USB byte
- * count sent from the host.
- */
- #define USBBUFSIZE (SIOBUFSIZE + 1) /* USB actual buffer size */
- extern LOG_Obj trace;
- extern SEM_Obj usbDeviceConnect;
- /* Function prototype */
- static Void createStreams();
- /*
- * Stream handles.
- */
- static SIO_Handle inStream, outStream;
- /*
- * Application function that gets called when USB bus is connected.
- */
- static C5509_USB_AppCallback deviceConnectCb = {
- (C5509_USB_TappCallback)SEM_post, /* fxn */
- &usbDeviceConnect /* arg. semaphore handle to post when/if connected */
- };
- Void main()
- {
- LOG_printf(&trace, "test started");
- }
- /*
- * ======== createStreams ========
- */
- static Void createStreams()
- {
- SIO_Attrs attrs;
- attrs = SIO_ATTRS;
- attrs.model = SIO_ISSUERECLAIM;
- /* create bulk type dsp input stream for USB endpoint #2 */
- inStream = SIO_create("/dioUsb2", SIO_INPUT, SIOBUFSIZE, &attrs);
- if (inStream == NULL) {
- SYS_abort("Create input stream FAILED.");
- }
- /* create bulk type dsp output stream for USB endpoint #2 */
- outStream = SIO_create("/dioUsb2", SIO_OUTPUT, SIOBUFSIZE, &attrs);
- if (outStream == NULL) {
- SYS_abort("Create output stream FAILED.");
- }
- /* connect USB device to the host */
- SIO_ctrl(outStream, C5509_USB_DEVICECONNECT, &deviceConnectCb );
-
- SEM_pend(&usbDeviceConnect, SYS_FOREVER);/* block until bus is connected */
- }
- /*
- * ======== myUsbTask ========
- * This function copies from the input stream to the output stream.
- */
- Void myUsbTask()
- {
- Int nmadus;
- Ptr inbuf0, inbuf1, outbuf0, outbuf1;
- /* create I/O streams */
- createStreams();
-
- /*
- * Both input and output streams are created double buffered for performance.
- * Initially, allocate stream buffers and issue 2 empty buffers to input stream. Then
- * reclaim two full input buffers and issue them to the output stream.
- */
- inbuf0 = MEM_calloc(0, USBBUFSIZE, 0);
- inbuf1 = MEM_calloc(0, USBBUFSIZE, 0);
- outbuf0 = MEM_calloc(0, USBBUFSIZE, 0);
- outbuf1 = MEM_calloc(0, USBBUFSIZE, 0);
- if (inbuf0 == NULL || inbuf1 == NULL ||
- outbuf0 == NULL || outbuf1 == NULL) {
- SYS_abort("MEM_calloc failed.");
- }
- /* Prime the input stream with two empty buffers */
- if (SIO_issue(inStream, inbuf0, SIO_bufsize(inStream), NULL) != SYS_OK) {
- SYS_abort("Error issuing buffer to the input stream");
- }
- if (SIO_issue(inStream, inbuf1, SIO_bufsize(inStream), NULL) != SYS_OK) {
- SYS_abort("Error issuing buffer to the input stream");
- }
- /* Reclaim 1st full buffer from input stream and process */
- if ((nmadus = SIO_reclaim(inStream, (Ptr *)&inbuf0, NULL)) < 0) {
- SYS_abort("Error reclaiming full buffer from the input stream");
- }
-
- /* Issue the full buffer to the output stream */
- if (SIO_issue(outStream, inbuf0, nmadus, NULL) != SYS_OK) {
- SYS_abort("Error issuing buffer to the output stream");
- }
- /* Issue another empty buffer to input stream. */
- if (SIO_issue(inStream, outbuf0, SIO_bufsize(inStream), NULL) != SYS_OK) {
- SYS_abort("Error issuing buffer to the input stream");
- }
- /* Reclaim 2nd full buffer from input stream and process */
- if ((nmadus = SIO_reclaim(inStream, (Ptr *)&inbuf0, NULL)) < 0) {
- SYS_abort("Error reclaiming full buffer from the input stream");
- }
- /* Issue another empty buffer to input stream. */
- if (SIO_issue(inStream, outbuf1, SIO_bufsize(inStream), NULL) != SYS_OK) {
- SYS_abort("Error issuing buffer to the input stream");
- }
- /* Issue the full buffer to the output stream. */
- if (SIO_issue(outStream, inbuf0, nmadus, NULL) != SYS_OK) {
- SYS_abort("Error issuing buffer to the output stream");
- }
- /*
- * Now, both streams have two buffers and we have echod back
- * two full buffers of data.
- * Loop forever .
- */
- for (;;) {
- /* Reclaim buffer with data from the input stream */
- if ((nmadus = SIO_reclaim(inStream, (Ptr *)&inbuf0, NULL)) < 0) {
- SYS_abort("Error reclaiming full buffer from the input stream");
- }
- /* Reclaim empty buffer from the output stream to be reused */
- if (SIO_reclaim(outStream, (Ptr *)&outbuf0, NULL) < 0) {
- SYS_abort("Error reclaiming empty buffer from the output stream");
- }
- /*
- * Do any data processing here.
- * Note: The first word of inbuf0 buffer contains the actual
- * USB data byte count.
- */
- /* Issue buffer to the output stream */
- if (SIO_issue(outStream, inbuf0, nmadus, NULL) != SYS_OK) {
-
- SYS_abort("Error issuing full buffer to the output stream");
- }
- /* Issue an empty buffer to the input stream */
- if (SIO_issue(inStream, outbuf0, SIO_bufsize(inStream), NULL) != SYS_OK) {
- SYS_abort("Error issuing empty buffer to the input stream");
- }
- }
- }