aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libmaple/usb/usb.c40
-rw-r--r--libmaple/usb/usb_callbacks.c30
-rw-r--r--libmaple/usb/usb_callbacks.h1
3 files changed, 20 insertions, 51 deletions
diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c
index 396579f..448c4ff 100644
--- a/libmaple/usb/usb.c
+++ b/libmaple/usb/usb.c
@@ -340,40 +340,32 @@ uint32 usbSendBytes(uint8* sendBuf, uint32 len) {
uint16 loaded = 0;
- if (bDeviceState != CONFIGURED || (!usbGetDTR() && !usbGetRTS())) {
- // Indicates to caller to stop trying, were not configured/connected
- // The DTR and RTS lines are handled differently on major platforms, so
- // the above logic is unreliable
- return 0;
- }
+ /* any checks on connection (via dtr/rts) done upstream in wirish or by user */
- // Due to a variety of shit this is how we roll; all buffering etc is pushed
- // upstream
+ /* last xmit hasnt finished, abort */
if (countTx) {
return 0;
}
// We can only put VCOM_TX_EPSIZE bytes in the buffer
if(len > VCOM_TX_EPSIZE) {
- loaded = VCOM_TX_EPSIZE;
- } else {
- loaded = len;
+ len = VCOM_TX_EPSIZE;
}
// Try to load some bytes if we can
- if (loaded) {
- UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR + countTx, loaded);
- _SetEPTxCount(VCOM_TX_ENDP, countTx+loaded);
+ if (len) {
+ UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR, len);
+ _SetEPTxCount(VCOM_TX_ENDP, len);
_SetEPTxValid(VCOM_TX_ENDP);
- countTx += loaded;
+ countTx += len;
}
- return loaded;
+ return len;
}
/* returns the number of available bytes are in the recv FIFO */
uint32 usbBytesAvailable(void) {
- return VCOM_RX_EPSIZE - maxNewBytes;
+ return newBytes;
}
/* copies len bytes from the local recieve FIFO (not
@@ -381,22 +373,20 @@ uint32 usbBytesAvailable(void) {
will only copy the minimum of len or the available
bytes. returns the number of bytes copied */
uint32 usbReceiveBytes(uint8* recvBuf, uint32 len) {
- if (len > VCOM_RX_EPSIZE - maxNewBytes) {
- len = VCOM_RX_EPSIZE - maxNewBytes;
+ if (len > newBytes) {
+ len = newBytes;
}
int i;
for (i=0;i<len;i++) {
- recvBuf[i] = (uint8)(vcomBufferRx[recvBufOut]);
- recvBufOut = (recvBufOut + 1) % VCOM_RX_EPSIZE;
+ recvBuf[i] = (uint8)(vcomBufferRx[i]);
}
- maxNewBytes += len; /* is this the potential bug? */
- // assert(maxNewBytes < VCOM_RX_EPSIZE)
+ newBytes -= len;
/* re-enable the rx endpoint which we had set to receive 0 bytes */
- if (maxNewBytes >= VCOM_RX_EPSIZE) {
- SetEPRxCount(VCOM_RX_ENDP,maxNewBytes);
+ if (newBytes == 0) {
+ SetEPRxCount(VCOM_RX_ENDP,VCOM_RX_EPSIZE);
SetEPRxStatus(VCOM_RX_ENDP,EP_RX_VALID);
}
diff --git a/libmaple/usb/usb_callbacks.c b/libmaple/usb/usb_callbacks.c
index d04f610..4d7e57a 100644
--- a/libmaple/usb/usb_callbacks.c
+++ b/libmaple/usb/usb_callbacks.c
@@ -37,7 +37,7 @@ volatile uint32 countTx = 0;
volatile uint32 recvBufIn = 0;
volatile uint32 recvBufOut = 0;
volatile uint32 maxNewBytes = VCOM_RX_BUFLEN;
-
+volatile uint32 newBytes = 0;
RESET_STATE reset_state = DTR_UNSET;
uint8 line_dtr_rts = 0;
@@ -47,7 +47,7 @@ void vcomDataTxCb(void) {
/* allows usbSendBytes to stop blocking */
- countTx = 0;
+ countTx = 0; /* assumes tx transactions are atomic 64 bytes (nearly certain they are) */
}
/* we could get arbitrarily complicated here for speed purposes
@@ -60,9 +60,8 @@ void vcomDataRxCb(void) {
/* setEPRxCount on the previous cycle should garuntee
we havnt received more bytes than we can fit */
- uint8 newBytes = GetEPRxCount(VCOM_RX_ENDP);
+ newBytes = GetEPRxCount(VCOM_RX_ENDP);
SetEPRxStatus(VCOM_RX_ENDP,EP_RX_NAK);
- /* assert (newBytes <= maxNewBytes); */
/* todo, not checking very carefully for edge cases. USUALLY,
if we emit the reset pulse and send 4 bytes, then newBytes
@@ -117,28 +116,7 @@ void vcomDataRxCb(void) {
- if (recvBufIn + newBytes < VCOM_RX_BUFLEN) {
- PMAToUserBufferCopy(&vcomBufferRx[recvBufIn],VCOM_RX_ADDR,newBytes);
- recvBufIn += newBytes;
- } else {
- /* we have to copy the data in two chunks because we roll over
- the edge of the circular buffer */
- uint8 tailBytes = VCOM_RX_BUFLEN - recvBufIn;
- uint8 remaining = newBytes - tailBytes;
-
- PMAToUserBufferCopy(&vcomBufferRx[recvBufIn],VCOM_RX_ADDR,tailBytes);
- PMAToUserBufferCopy(&vcomBufferRx[0], VCOM_RX_ADDR,remaining);
-
- recvBufIn = (recvBufIn + newBytes ) % VCOM_RX_BUFLEN;
- }
-
- maxNewBytes -= newBytes;
-
- if (maxNewBytes >= VCOM_RX_EPSIZE) {
- SetEPRxCount(VCOM_RX_ENDP,VCOM_RX_EPSIZE);
- SetEPRxStatus(VCOM_RX_ENDP,EP_RX_VALID);
- }
-
+ PMAToUserBufferCopy(&vcomBufferRx[0],VCOM_RX_ADDR,newBytes);
}
void vcomManagementCb(void) {
diff --git a/libmaple/usb/usb_callbacks.h b/libmaple/usb/usb_callbacks.h
index 21ceb77..20d2c13 100644
--- a/libmaple/usb/usb_callbacks.h
+++ b/libmaple/usb/usb_callbacks.h
@@ -39,6 +39,7 @@ extern uint8 vcomBufferRx[VCOM_RX_BUFLEN]; /* no reason this has to be VCOM_RX_
extern volatile uint32 recvBufIn; /* the FIFO in index to the recvbuffer */
extern volatile uint32 recvBufOut; /* the FIFO out index to the recvbuffer */
extern volatile uint32 maxNewBytes;
+extern volatile uint32 newBytes;
void vcomDataTxCb(void);
void vcomDataRxCb(void);