usb_serial.h

00001 /*
00002  * This file is relicensed as part of the Python-on-a-Chip program.
00003  * Python-on-a-Chip is free software: you can redistribute it and/or modify
00004  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00005  * 
00006  * Python-on-a-Chip is distributed in the hope that it will be useful,
00007  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00008  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00009  * A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00010  * is seen in the file COPYING up one directory from this.
00011  */
00012 
00013 /* USB Serial Example for Teensy USB Development Board 
00014  * (usb_serial.h & usb_serial.c)
00015  * http://www.pjrc.com/teensy/
00016  * Copyright (c) 2008 PJRC.COM, LLC
00017  * 
00018  * Permission is hereby granted, free of charge, to any person obtaining a copy
00019  * of this software and associated documentation files (the "Software"), to deal
00020  * in the Software without restriction, including without limitation the rights
00021  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00022  * copies of the Software, and to permit persons to whom the Software is
00023  * furnished to do so, subject to the following conditions:
00024  * 
00025  * The above copyright notice and this permission notice shall be included in
00026  * all copies or substantial portions of the Software.
00027  * 
00028  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00029  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00030  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00031  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00032  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00033  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00034  * THE SOFTWARE.
00035  */
00036 
00037 
00038 #ifndef usb_serial_h__
00039 #define usb_serial_h__
00040 
00041 #include <stdint.h>
00042 
00043 // setup
00044 void usb_init(void);                    // initialize everything
00045 uint8_t usb_configured(void);           // is the USB port configured
00046 
00047 // receiving data
00048 int16_t usb_serial_getchar(void);       // receive a character (-1 if timeout/error)
00049 uint8_t usb_serial_available(void);     // number of bytes in receive buffer
00050 void usb_serial_flush_input(void);      // discard any buffered input
00051 
00052 // transmitting data
00053 int8_t usb_serial_putchar(uint8_t c);   // transmit a character
00054 int8_t usb_serial_putchar_nowait(uint8_t c);  // transmit a character, do not wait
00055 int8_t usb_serial_write(const uint8_t *buffer, uint16_t size); // transmit a buffer
00056 void usb_serial_flush_output(void);     // immediately transmit any buffered output
00057 
00058 // serial parameters
00059 uint32_t usb_serial_get_baud(void);     // get the baud rate
00060 uint8_t usb_serial_get_stopbits(void);  // get the number of stop bits
00061 uint8_t usb_serial_get_paritytype(void);// get the parity type
00062 uint8_t usb_serial_get_numbits(void);   // get the number of data bits
00063 uint8_t usb_serial_get_control(void);   // get the RTS and DTR signal state
00064 int8_t usb_serial_set_control(uint8_t signals); // set DSR, DCD, RI, etc
00065 
00066 // constants corresponding to the various serial parameters
00067 #define USB_SERIAL_DTR                  0x01
00068 #define USB_SERIAL_RTS                  0x02
00069 #define USB_SERIAL_1_STOP               0
00070 #define USB_SERIAL_1_5_STOP             1
00071 #define USB_SERIAL_2_STOP               2
00072 #define USB_SERIAL_PARITY_NONE          0
00073 #define USB_SERIAL_PARITY_ODD           1
00074 #define USB_SERIAL_PARITY_EVEN          2
00075 #define USB_SERIAL_PARITY_MARK          3
00076 #define USB_SERIAL_PARITY_SPACE         4
00077 #define USB_SERIAL_DCD                  0x01
00078 #define USB_SERIAL_DSR                  0x02
00079 #define USB_SERIAL_BREAK                0x04
00080 #define USB_SERIAL_RI                   0x08
00081 #define USB_SERIAL_FRAME_ERR            0x10
00082 #define USB_SERIAL_PARITY_ERR           0x20
00083 #define USB_SERIAL_OVERRUN_ERR          0x40
00084 
00085 // This file does not include the HID debug functions, so these empty
00086 // macros replace them with nothing, so users can compile code that
00087 // has calls to these functions.
00088 #define usb_debug_putchar(c)
00089 #define usb_debug_flush_output()
00090 
00091 
00092 
00093 // Everything below this point is only intended for usb_serial.c
00094 #ifdef USB_SERIAL_PRIVATE_INCLUDE
00095 #include <avr/io.h>
00096 #include <avr/pgmspace.h>
00097 #include <avr/interrupt.h>
00098 
00099 #define EP_TYPE_CONTROL                 0x00
00100 #define EP_TYPE_BULK_IN                 0x81
00101 #define EP_TYPE_BULK_OUT                0x80
00102 #define EP_TYPE_INTERRUPT_IN            0xC1
00103 #define EP_TYPE_INTERRUPT_OUT           0xC0
00104 #define EP_TYPE_ISOCHRONOUS_IN          0x41
00105 #define EP_TYPE_ISOCHRONOUS_OUT         0x40
00106 #define EP_SINGLE_BUFFER                0x02
00107 #define EP_DOUBLE_BUFFER                0x06
00108 #define EP_SIZE(s)      ((s) == 64 ? 0x30 :     \
00109                         ((s) == 32 ? 0x20 :     \
00110                         ((s) == 16 ? 0x10 :     \
00111                                      0x00)))
00112 
00113 #define MAX_ENDPOINT            4
00114 
00115 #define LSB(n) (n & 255)
00116 #define MSB(n) ((n >> 8) & 255)
00117 
00118 #if defined(__AVR_AT90USB162__)
00119 #define HW_CONFIG() 
00120 #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
00121 #define USB_CONFIG() (USBCON = (1<<USBE))
00122 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
00123 #elif defined(__AVR_ATmega32U4__)
00124 #define HW_CONFIG() (UHWCON = 0x01)
00125 #define PLL_CONFIG() (PLLCSR = 0x12)
00126 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
00127 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
00128 #elif defined(__AVR_AT90USB646__)
00129 #define HW_CONFIG() (UHWCON = 0x81)
00130 #define PLL_CONFIG() (PLLCSR = 0x1A)
00131 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
00132 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
00133 #elif defined(__AVR_AT90USB1286__)
00134 #define HW_CONFIG() (UHWCON = 0x81)
00135 #define PLL_CONFIG() (PLLCSR = 0x16)
00136 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
00137 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
00138 #endif
00139 
00140 // standard control endpoint request types
00141 #define GET_STATUS                      0
00142 #define CLEAR_FEATURE                   1
00143 #define SET_FEATURE                     3
00144 #define SET_ADDRESS                     5
00145 #define GET_DESCRIPTOR                  6
00146 #define GET_CONFIGURATION               8
00147 #define SET_CONFIGURATION               9
00148 #define GET_INTERFACE                   10
00149 #define SET_INTERFACE                   11
00150 // HID (human interface device)
00151 #define HID_GET_REPORT                  1
00152 #define HID_GET_PROTOCOL                3
00153 #define HID_SET_REPORT                  9
00154 #define HID_SET_IDLE                    10
00155 #define HID_SET_PROTOCOL                11
00156 // CDC (communication class device)
00157 #define CDC_SET_LINE_CODING             0x20
00158 #define CDC_GET_LINE_CODING             0x21
00159 #define CDC_SET_CONTROL_LINE_STATE      0x22
00160 #endif
00161 #endif

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9