bootloader_firmware/usbdrv/usbdrv.c File Reference

#include "iarcompat.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "usbdrv.h"
#include "oddebug.h"

Go to the source code of this file.

Data Structures

union  converter_t

Defines

#define IAR_SECTION(arg)
#define __no_init
#define USB_FLG_TX_PACKET   (1<<0)
#define USB_FLG_MSGPTR_IS_ROM   (1<<6)
#define USB_FLG_USE_DEFAULT_RW   (1<<7)
#define PRG_RDB(addr)   pgm_read_byte(addr)

Functions

__no_init uchar usbRxBuf[2][USB_BUFSIZE] __attribute__ ((section(USB_BUFFER_SECTION)))
void usbSetInterrupt (uchar *data, uchar len)
void usbPoll (void)
void usbInit (void)

Variables

uchar usbNakBuf [1] = {USBPID_NAK}
uchar * usbMsgPtr


Define Documentation

#define __no_init

Definition at line 27 of file usbdrv.c.

#define IAR_SECTION ( arg   ) 

Definition at line 26 of file usbdrv.c.

#define PRG_RDB ( addr   )     pgm_read_byte(addr)

Definition at line 175 of file usbdrv.c.

#define USB_FLG_MSGPTR_IS_ROM   (1<<6)

Definition at line 64 of file usbdrv.c.

#define USB_FLG_TX_PACKET   (1<<0)

Definition at line 62 of file usbdrv.c.

#define USB_FLG_USE_DEFAULT_RW   (1<<7)

Definition at line 65 of file usbdrv.c.


Function Documentation

__no_init uchar usbRxBuf [2][USB_BUFSIZE] __attribute__ ( (section(USB_BUFFER_SECTION))   ) 

Definition at line 37 of file usbdrv.c.

00037                                                                                                                                         : PID, 8 bytes data, 2 bytes CRC */
00038 uchar       usbDeviceAddr;      /* assigned during enumeration, defaults to 0 */
00039 uchar       usbNewDeviceAddr;   /* device ID which should be set after status phase */
00040 uchar       usbConfiguration;   /* currently selected configuration. Administered by driver, but not used */
00041 uchar       usbInputBuf;        /* ptr to raw buffer used for receiving */
00042 uchar       usbAppBuf;          /* ptr to raw buffer passed to app for processing */
00043 volatile schar usbRxLen;        /* = 0; number of bytes in usbAppBuf; 0 means free */
00044 uchar       usbCurrentTok;      /* last token received */
00045 uchar       usbRxToken;         /* token for data we received */
00046 uchar       usbMsgLen = 0xff;   /* remaining number of bytes, no msg to send if -1 (see usbMsgPtr) */
00047 volatile schar usbTxLen = -1;   /* number of bytes to transmit with next IN token */
00048 uchar       usbTxBuf[USB_BUFSIZE];/* data to transmit with next IN, free if usbTxLen == -1 */
00049 #if USB_CFG_HAVE_INTRIN_ENDPOINT
00050 /* uchar       usbRxEndp;          endpoint which was addressed (1 bit in MSB) [not impl] */
00051 volatile schar usbTxLen1 = -1;  /* TX count for endpoint 1 */
00052 uchar       usbTxBuf1[USB_BUFSIZE];/* TX data for endpoint 1 */
00053 #endif
00054 uchar       usbAckBuf[1] = {USBPID_ACK};    /* transmit buffer for ack tokens */

void usbInit ( void   ) 

Definition at line 501 of file usbdrv.c.

Referenced by main().

00502 {
00503     usbInputBuf = (uchar)usbRxBuf[0];
00504     usbAppBuf = (uchar)usbRxBuf[1];
00505 #if USB_INTR_CFG_SET != 0
00506     USB_INTR_CFG |= USB_INTR_CFG_SET;
00507 #endif
00508 #if USB_INTR_CFG_CLR != 0
00509     USB_INTR_CFG &= ~(USB_INTR_CFG_CLR);
00510 #endif
00511     USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT);
00512 }

void usbPoll ( void   ) 

Definition at line 452 of file usbdrv.c.

Referenced by main().

00453 {
00454 uchar   len;
00455 
00456     if((len = usbRxLen) > 0){
00457 /* We could check CRC16 here -- but ACK has already been sent anyway. If you
00458  * need data integrity checks with this driver, check the CRC in your app
00459  * code and report errors back to the host. Since the ACK was already sent,
00460  * retries must be handled on application level.
00461  * unsigned crc = usbCrc16((uchar *)(unsigned)(usbAppBuf + 1), usbRxLen - 3);
00462  */
00463         len -= 3;       /* remove PID and CRC */
00464         if(len < 128){  /* no overflow */
00465             converter_t appBuf;
00466             appBuf.ptr = (uchar *)usbRxBuf;
00467             appBuf.bytes[0] = usbAppBuf;
00468             appBuf.bytes[0]++;
00469             usbProcessRx(appBuf.ptr, len);
00470         }
00471         usbRxLen = 0;   /* mark rx buffer as available */
00472     }
00473     if(usbMsgLen != 0xff){  /* transmit data pending? */
00474         if(usbTxLen < 0)    /* transmit system idle */
00475             usbBuildTxBlock();
00476     }
00477     if(isNotSE0()){ /* SE0 state */
00478         usbIsReset = 0;
00479     }else{
00480         /* check whether SE0 lasts for more than 2.5us (3.75 bit times) */
00481         if(!usbIsReset){
00482             uchar i;
00483             for(i=100;i;i--){
00484                 if(isNotSE0())
00485                     goto notUsbReset;
00486             }
00487             usbIsReset = 1;
00488             usbNewDeviceAddr = 0;
00489             usbDeviceAddr = 0;
00490 #if USB_CFG_IMPLEMENT_HALT
00491             usbHalted1 = 0;
00492 #endif
00493             DBG1(0xff, 0, 0);
00494 notUsbReset:;
00495         }
00496     }
00497 }

void usbSetInterrupt ( uchar *  data,
uchar  len 
)

Definition at line 196 of file usbdrv.c.

Referenced by main().

00197 {
00198 uchar       *p, i;
00199 
00200 #if USB_CFG_IMPLEMENT_HALT
00201     if(usbHalted1)
00202         return;
00203 #endif
00204     if(len > 8) /* interrupt transfers are limited to 8 bytes */
00205         len = 8;
00206     i = USBPID_DATA1;
00207     if(usbTxPacketCnt1 & 1)
00208         i = USBPID_DATA0;
00209     if(usbTxLen1 < 0){      /* packet buffer was empty */
00210         usbTxPacketCnt1++;
00211     }else{
00212         usbTxLen1 = -1;     /* avoid sending incomplete interrupt data */
00213     }
00214     p = usbTxBuf1;
00215     *p++ = i;
00216     for(i=len;i--;)
00217         *p++ = *data++;
00218     usbCrc16Append(&usbTxBuf1[1], len);
00219     usbTxLen1 = len + 4;    /* len must be given including sync byte */
00220 #if DEBUG_LEVEL > 1
00221     DBG2(0x21, usbTxBuf1, usbTxLen1-1);
00222 #else
00223     DBG1(0x21, usbTxBuf1 + 1, 2);
00224 #endif
00225 }


Variable Documentation

uchar* usbMsgPtr

Definition at line 58 of file usbdrv.c.

Referenced by usbFunctionSetup().

uchar usbNakBuf[1] = {USBPID_NAK}

Definition at line 55 of file usbdrv.c.


Generated on Fri Oct 30 15:43:34 2009 for USB IR HID Device by  doxygen 1.5.8