From 873356a31fae8cf4e8b6a5ab609125a5a501d1c4 Mon Sep 17 00:00:00 2001 From: AJM Date: Mon, 13 Dec 2010 22:03:14 -0500 Subject: candidate bugfix for serialusb receive bug changed USB driver to nak whenever it cant fill an entire endpoint (64B) worth of new data. The old scheme was to set receive valid as long as as the endpoint buffer wasnt full, the new scheme is to nak until it is completely empty. --- libmaple/usb/usb.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libmaple/usb/usb.h') diff --git a/libmaple/usb/usb.h b/libmaple/usb/usb.h index ffba9ff..84390cb 100644 --- a/libmaple/usb/usb.h +++ b/libmaple/usb/usb.h @@ -73,9 +73,9 @@ void usb_lpIRQHandler(void); void usbWaitReset(void); /* blocking functions for send/receive */ -uint16 usbSendBytes(uint8* sendBuf,uint16 len); -uint8 usbBytesAvailable(void); -uint8 usbReceiveBytes(uint8* recvBuf, uint8 len); +uint32 usbSendBytes(uint8* sendBuf,uint16 len); +uint32 usbBytesAvailable(void); +uint32 usbReceiveBytes(uint8* recvBuf, uint8 len); uint8 usbGetDTR(void); uint8 usbGetRTS(void); uint8 usbIsConnected(void); -- cgit v1.2.3 From 125cbc814bc032ba217e8c10e0ceb43981126ce3 Mon Sep 17 00:00:00 2001 From: AJM Date: Mon, 13 Dec 2010 23:46:36 -0500 Subject: enlarged rx buffer and nak when less than 64 bytes is left in packet buf --- libmaple/usb/usb.c | 4 ++-- libmaple/usb/usb.h | 4 ++-- libmaple/usb/usb_callbacks.c | 23 ++++++++++++----------- libmaple/usb/usb_callbacks.h | 10 +++++----- libmaple/usb/usb_config.h | 1 + 5 files changed, 22 insertions(+), 20 deletions(-) (limited to 'libmaple/usb/usb.h') diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c index 2b99132..396579f 100644 --- a/libmaple/usb/usb.c +++ b/libmaple/usb/usb.c @@ -336,7 +336,7 @@ void usbWaitReset(void) { * * */ -uint32 usbSendBytes(uint8* sendBuf, uint16 len) { +uint32 usbSendBytes(uint8* sendBuf, uint32 len) { uint16 loaded = 0; @@ -380,7 +380,7 @@ uint32 usbBytesAvailable(void) { usb packet buffer) into recvBuf and deq's the fifo. will only copy the minimum of len or the available bytes. returns the number of bytes copied */ -uint32 usbReceiveBytes(uint8* recvBuf, uint8 len) { +uint32 usbReceiveBytes(uint8* recvBuf, uint32 len) { if (len > VCOM_RX_EPSIZE - maxNewBytes) { len = VCOM_RX_EPSIZE - maxNewBytes; } diff --git a/libmaple/usb/usb.h b/libmaple/usb/usb.h index 84390cb..5bc31e1 100644 --- a/libmaple/usb/usb.h +++ b/libmaple/usb/usb.h @@ -73,9 +73,9 @@ void usb_lpIRQHandler(void); void usbWaitReset(void); /* blocking functions for send/receive */ -uint32 usbSendBytes(uint8* sendBuf,uint16 len); +uint32 usbSendBytes(uint8* sendBuf,uint32 len); uint32 usbBytesAvailable(void); -uint32 usbReceiveBytes(uint8* recvBuf, uint8 len); +uint32 usbReceiveBytes(uint8* recvBuf, uint32 len); uint8 usbGetDTR(void); uint8 usbGetRTS(void); uint8 usbIsConnected(void); diff --git a/libmaple/usb/usb_callbacks.c b/libmaple/usb/usb_callbacks.c index c30d650..d04f610 100644 --- a/libmaple/usb/usb_callbacks.c +++ b/libmaple/usb/usb_callbacks.c @@ -32,11 +32,11 @@ USB_Line_Coding line_coding = { datatype: 0x08 }; -uint8 vcomBufferRx[VCOM_RX_EPSIZE]; -volatile uint8 countTx = 0; -volatile uint8 recvBufIn = 0; -volatile uint8 recvBufOut = 0; -volatile uint8 maxNewBytes = VCOM_RX_EPSIZE; +uint8 vcomBufferRx[VCOM_RX_BUFLEN]; +volatile uint32 countTx = 0; +volatile uint32 recvBufIn = 0; +volatile uint32 recvBufOut = 0; +volatile uint32 maxNewBytes = VCOM_RX_BUFLEN; RESET_STATE reset_state = DTR_UNSET; uint8 line_dtr_rts = 0; @@ -61,6 +61,7 @@ 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); + SetEPRxStatus(VCOM_RX_ENDP,EP_RX_NAK); /* assert (newBytes <= maxNewBytes); */ /* todo, not checking very carefully for edge cases. USUALLY, @@ -116,26 +117,26 @@ void vcomDataRxCb(void) { - if (recvBufIn + newBytes < VCOM_RX_EPSIZE) { + 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_EPSIZE - recvBufIn; + 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_EPSIZE; + recvBufIn = (recvBufIn + newBytes ) % VCOM_RX_BUFLEN; } maxNewBytes -= newBytes; - if (maxNewBytes > VCOM_RX_EPSIZE) { - SetEPRxCount(VCOM_RX_ENDP,0); - SetEPRxStatus(VCOM_RX_ENDP,EP_RX_NAK); /* nak until we clear the buffer */ + if (maxNewBytes >= VCOM_RX_EPSIZE) { + SetEPRxCount(VCOM_RX_ENDP,VCOM_RX_EPSIZE); + SetEPRxStatus(VCOM_RX_ENDP,EP_RX_VALID); } } diff --git a/libmaple/usb/usb_callbacks.h b/libmaple/usb/usb_callbacks.h index a94de11..21ceb77 100644 --- a/libmaple/usb/usb_callbacks.h +++ b/libmaple/usb/usb_callbacks.h @@ -34,11 +34,11 @@ typedef enum { extern RESET_STATE reset_state; /* tracks DTR/RTS */ extern uint8 line_dtr_rts; -extern volatile uint8 countTx; -extern uint8 vcomBufferRx[VCOM_RX_EPSIZE]; /* no reason this has to be VCOM_RX_EPSIZE, could be bigger */ -extern volatile uint8 recvBufIn; /* the FIFO in index to the recvbuffer */ -extern volatile uint8 recvBufOut; /* the FIFO out index to the recvbuffer */ -extern volatile uint8 maxNewBytes; +extern volatile uint32 countTx; +extern uint8 vcomBufferRx[VCOM_RX_BUFLEN]; /* no reason this has to be VCOM_RX_EPSIZE, could be bigger */ +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; void vcomDataTxCb(void); void vcomDataRxCb(void); diff --git a/libmaple/usb/usb_config.h b/libmaple/usb/usb_config.h index ba05d42..e5f3979 100644 --- a/libmaple/usb/usb_config.h +++ b/libmaple/usb/usb_config.h @@ -26,6 +26,7 @@ #define VCOM_RX_EPNUM 0x03 #define VCOM_RX_ADDR 0x110 #define VCOM_RX_EPSIZE 0x40 +#define VCOM_RX_BUFLEN (VCOM_RX_EPSIZE*3) #define bMaxPacketSize 0x40 /* 64B, maximum for USB FS Devices */ -- cgit v1.2.3 From 84fd2532a7f23d20354ff590790b3f892cb7e7d7 Mon Sep 17 00:00:00 2001 From: AJM Date: Tue, 14 Dec 2010 01:39:20 -0500 Subject: added a usbBlockingSendByte(char ch) this new function will only return AFTER the usb interrupt has been serviced and the byte is sent. Bytes should NOT fall on the floor ever with this function. (but they still do? pyserials problem?) --- libmaple/usb/usb.c | 15 ++++++++++----- libmaple/usb/usb.h | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'libmaple/usb/usb.h') diff --git a/libmaple/usb/usb.c b/libmaple/usb/usb.c index 448c4ff..4e86ed0 100644 --- a/libmaple/usb/usb.c +++ b/libmaple/usb/usb.c @@ -336,10 +336,15 @@ void usbWaitReset(void) { * * */ +void usbBlockingSendByte(char ch) { + while (countTx); + UserToPMABufferCopy(&ch,VCOM_TX_ADDR,1); + _SetEPTxCount(VCOM_TX_ENDP,1); + _SetEPTxValid(VCOM_TX_ENDP); + countTx = 1; + while (countTx); +} uint32 usbSendBytes(uint8* sendBuf, uint32 len) { - - uint16 loaded = 0; - /* any checks on connection (via dtr/rts) done upstream in wirish or by user */ /* last xmit hasnt finished, abort */ @@ -348,8 +353,8 @@ uint32 usbSendBytes(uint8* sendBuf, uint32 len) { } // We can only put VCOM_TX_EPSIZE bytes in the buffer - if(len > VCOM_TX_EPSIZE) { - len = VCOM_TX_EPSIZE; + if(len > VCOM_TX_EPSIZE/2) { + len = VCOM_TX_EPSIZE/2; } // Try to load some bytes if we can diff --git a/libmaple/usb/usb.h b/libmaple/usb/usb.h index 5bc31e1..0ed02e5 100644 --- a/libmaple/usb/usb.h +++ b/libmaple/usb/usb.h @@ -73,6 +73,7 @@ void usb_lpIRQHandler(void); void usbWaitReset(void); /* blocking functions for send/receive */ +void usbBlockingSendByte(char ch); uint32 usbSendBytes(uint8* sendBuf,uint32 len); uint32 usbBytesAvailable(void); uint32 usbReceiveBytes(uint8* recvBuf, uint32 len); -- cgit v1.2.3