From 5e355a4f0588438632d56e2f9fef5a75b949bd24 Mon Sep 17 00:00:00 2001 From: Christian Starkjohann Date: Wed, 5 Dec 2012 22:57:13 +0100 Subject: usbMsgPtr now has a separate type so that it can be defined to an 8 bit type for tiny memory model --- usbdrv/usbconfig-prototype.h | 9 +++++++++ usbdrv/usbdrv.c | 12 +++++++----- usbdrv/usbdrv.h | 13 ++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/usbdrv/usbconfig-prototype.h b/usbdrv/usbconfig-prototype.h index 826f026..f9cdd91 100644 --- a/usbdrv/usbconfig-prototype.h +++ b/usbdrv/usbconfig-prototype.h @@ -356,6 +356,15 @@ section at the end of this file). #define USB_CFG_DESCR_PROPS_HID_REPORT 0 #define USB_CFG_DESCR_PROPS_UNKNOWN 0 + +#define usbMsgPtr_t unsigned short +/* If usbMsgPtr_t is not defined, it defaults to 'uchar *'. We define it to + * a scalar type here because gcc generates slightly shorter code for scalar + * arithmetics than for pointer arithmetics. Remove this define for backward + * type compatibility or define it to an 8 bit type if you use data in RAM only + * and all RAM is below 256 bytes (tiny memory model in IAR CC). + */ + /* ----------------------- Optional MCU Description ------------------------ */ /* The following configurations have working defaults in usbdrv.h. You diff --git a/usbdrv/usbdrv.c b/usbdrv/usbdrv.c index 7a125f2..e58d764 100644 --- a/usbdrv/usbdrv.c +++ b/usbdrv/usbdrv.c @@ -45,7 +45,7 @@ uchar usbCurrentDataToken;/* when we check data toggling to ignore duplica #endif /* USB status registers / not shared with asm code */ -uchar *usbMsgPtr; /* data to transmit next -- ROM or RAM address */ +usbMsgPtr_t usbMsgPtr; /* data to transmit next -- ROM or RAM address */ static usbMsgLen_t usbMsgLen = USB_NO_MSG; /* remaining number of bytes */ static uchar usbMsgFlags; /* flag values see below */ @@ -301,7 +301,7 @@ USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len) len = usbFunctionDescriptor(rq); \ }else{ \ len = USB_PROP_LENGTH(cfgProp); \ - usbMsgPtr = (uchar *)(staticName); \ + usbMsgPtr = (usbMsgPtr_t)(staticName); \ } \ } @@ -409,7 +409,7 @@ uchar index = rq->wIndex.bytes[0]; SWITCH_DEFAULT /* 7=SET_DESCRIPTOR, 12=SYNC_FRAME */ /* Should we add an optional hook here? */ SWITCH_END - usbMsgPtr = dataPtr; + usbMsgPtr = (usbMsgPtr_t)dataPtr; skipMsgPtrAssignment: return len; } @@ -499,7 +499,8 @@ static uchar usbDeviceRead(uchar *data, uchar len) }else #endif { - uchar i = len, *r = usbMsgPtr; + uchar i = len; + usbMsgPtr_t r = usbMsgPtr; if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */ do{ uchar c = USB_READ_FLASH(r); /* assign to char size variable to enforce byte ops */ @@ -508,7 +509,8 @@ static uchar usbDeviceRead(uchar *data, uchar len) }while(--i); }else{ /* RAM data */ do{ - *data++ = *r++; + *data++ = *((uchar *)r); + r++; }while(--i); } usbMsgPtr = r; diff --git a/usbdrv/usbdrv.h b/usbdrv/usbdrv.h index 6d1ad50..b377c43 100644 --- a/usbdrv/usbdrv.h +++ b/usbdrv/usbdrv.h @@ -163,6 +163,17 @@ USB messages, even if they address another (low-speed) device on the same bus. */ #define USB_NO_MSG ((usbMsgLen_t)-1) /* constant meaning "no message" */ +#ifndef usbMsgPtr_t +#define usbMsgPtr_t uchar * +#endif +/* Making usbMsgPtr_t a define allows the user of this library to define it to + * an 8 bit type on tiny devices. This reduces code size, especially if the + * compiler supports a tiny memory model. + * The type can be a pointer or scalar type, casts are made where necessary. + * Although it's paradoxical, Gcc 4 generates slightly better code for scalar + * types than for pointers. + */ + struct usbRequest; /* forward declaration */ USB_PUBLIC void usbInit(void); @@ -178,7 +189,7 @@ USB_PUBLIC void usbPoll(void); * Please note that debug outputs through the UART take ~ 0.5ms per byte * at 19200 bps. */ -extern uchar *usbMsgPtr; +extern usbMsgPtr_t usbMsgPtr; /* This variable may be used to pass transmit data to the driver from the * implementation of usbFunctionWrite(). It is also used internally by the * driver for standard control requests. -- cgit v1.2.3