Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions os/hal/ports/HT32/LLD/USBv1/hal_usb_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,26 @@ static size_t usb_packet_receive(USBDriver *usbp, usbep_t ep) {
const uint32_t cfgr = USB->EP[ep].CFGR;
volatile uint32_t *const EPSRAM = (void *)(USB_SRAM_BASE + (cfgr & 0x3ff) + ((ep == 0) ? ((cfgr >> 10) & 0x7f) : 0));

for (size_t i = 0; i < n; i += 4) {
// Reads the packet into the defined buffer. The host can send a full
// packet when less room is left, the excess is discarded.
const size_t m = n < osp->rxsize ? n : osp->rxsize;
for (size_t i = 0; i < m; i += 4) {
if(!osp->rxbuf)
break;
const uint32_t word = EPSRAM[i/4];
if (i + 0 < n)
if (i + 0 < m)
osp->rxbuf[i + 0] = word >> 0;
if (i + 1 < n)
if (i + 1 < m)
osp->rxbuf[i + 1] = word >> 8;
if (i + 2 < n)
if (i + 2 < m)
osp->rxbuf[i + 2] = word >> 16;
if (i + 3 < n)
if (i + 3 < m)
osp->rxbuf[i + 3] = word >> 24;
}

osp->rxbuf += n;
osp->rxcnt += n;
osp->rxsize -= n;
osp->rxbuf += m;
osp->rxcnt += m;
osp->rxsize -= m;
osp->rxpkts--;
return n;
}
Expand Down
7 changes: 5 additions & 2 deletions os/hal/ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ void usb_packet_receive(USBDriver *usbp, usbep_t ep, size_t n)
if (n > (size_t)epc->out_maxsize)
n = (size_t)epc->out_maxsize;

/* Copy from _usbb[] to buf */
/* Reads the packet into the defined buffer. The host can send a full
packet when less room is left, the excess is discarded.*/
const size_t m = n < osp->rxsize ? n : osp->rxsize;

size_t i=0;
for(i=0;i<n;i++)
for(i=0;i<m;i++)
osp->rxbuf[i] = bd->addr[i];

/* Update the Buffer status
Expand Down
15 changes: 9 additions & 6 deletions os/hal/ports/RP/LLD/USBDv1/hal_usb_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ static void usb_serve_endpoint(USBDriver *usbp, usbep_t ep, bool is_in) {
const USBEndpointConfig *epcp = usbp->epc[ep];
USBOutEndpointState *oesp;
USBInEndpointState *iesp;
uint16_t n;
uint16_t n, m;

if (is_in) {
/* IN endpoint */
Expand All @@ -318,14 +318,17 @@ static void usb_serve_endpoint(USBDriver *usbp, usbep_t ep, bool is_in) {
/* OUT endpoint */
oesp = usbp->epc[ep]->out_state;

/* Length received */
/* Length received. The host can send a full
packet when less room is left, the excess is discarded. */
n = BUF_CTRL(ep).OUT & USB_BUFFER_BUFFER0_TRANS_LENGTH_Msk;
m = n < oesp->rxsize ? n : oesp->rxsize;

/* Copy received data into user buffer */
memcpy((void *)oesp->rxbuf, (void *)oesp->hw_buf, n);
oesp->rxbuf += n;
oesp->rxcnt += n;
oesp->rxsize -= n;
memcpy((void *)oesp->rxbuf, (void *)oesp->hw_buf, m);

oesp->rxbuf += m;
oesp->rxcnt += m;
oesp->rxsize -= m;

oesp->rxpkts -= 1;

Expand Down
34 changes: 20 additions & 14 deletions os/hal/ports/WB32/LLD/USBv1/hal_usb_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ static void _wbusb_ep0out(USBDriver *usbp, usbep_t ep) {
*
* @param[in] ep endpoint number
* @param[out] buf buffer where to copy the packet data
* @return The size of the receivee packet.
* @param[in] max maximum number of bytes to copy, the rest of the
* packet is discarded
* @return The size of the received packet.
*
* @notapi
*/
static size_t usb_packet_read_to_buffer(usbep_t ep, uint8_t *buf) {
static size_t usb_packet_read_to_buffer(usbep_t ep, uint8_t *buf,
size_t max) {
size_t rx_cnt, n;
uint8_t ep_num;
uint32_t ep_fifo_addr;
Expand All @@ -200,7 +203,7 @@ static size_t usb_packet_read_to_buffer(usbep_t ep, uint8_t *buf) {
rx_cnt = WB32_USB->COUNT0;
}

n = rx_cnt;
n = rx_cnt < max ? rx_cnt : max;

while (n) {
*buf++ = *((volatile uint8_t *)ep_fifo_addr);
Expand Down Expand Up @@ -238,7 +241,7 @@ static void usb_packet_write_from_buffer(usbep_t ep,
* @notapi
*/
static void usb_serve_ep0_handler(USBDriver *usbp) {
size_t n;
size_t n, m;
uint8_t ControlReg;
const USBEndpointConfig *epcp = usbp->epc[0];

Expand Down Expand Up @@ -315,12 +318,13 @@ static void usb_serve_ep0_handler(USBDriver *usbp) {

if (ControlReg & USB_CSR0_OUTPKTRDY) {
/* Reads the packet into the defined buffer.*/
n = usb_packet_read_to_buffer(0, osp->rxbuf);
osp->rxbuf += n;
n = usb_packet_read_to_buffer(0, osp->rxbuf, osp->rxsize);
m = n < osp->rxsize ? n : osp->rxsize;
osp->rxbuf += m;

/* Transaction data updated.*/
osp->rxcnt += n;
osp->rxsize -= n;
osp->rxcnt += m;
osp->rxsize -= m;
osp->rxpkts -= 1;

/* The transaction is completed if the specified number of packets
Expand Down Expand Up @@ -393,7 +397,7 @@ static void usb_serve_epin_handler(USBDriver *usbp, usbep_t ep) {
* @notapi
*/
static void usb_serve_epout_handler(USBDriver *usbp, usbep_t ep) {
size_t n;
size_t n, m;
uint8_t ControlReg;
const USBEndpointConfig *epcp = usbp->epc[ep];
/* OUT endpoint, receive.*/
Expand All @@ -404,14 +408,16 @@ static void usb_serve_epout_handler(USBDriver *usbp, usbep_t ep) {
ControlReg = WB32_USB->OUTCSR1;

if (ControlReg & USB_OUTCSR1_OUTPKTRDY) {
/* Reads the packet into the defined buffer.*/
n = usb_packet_read_to_buffer(ep, osp->rxbuf);
/* Reads the packet into the defined buffer. The host can send a full
packet when less room is left, the excess is discarded.*/
n = usb_packet_read_to_buffer(ep, osp->rxbuf, osp->rxsize);
m = n < osp->rxsize ? n : osp->rxsize;

osp->rxbuf += n;
osp->rxbuf += m;

/* Transaction data updated.*/
osp->rxcnt += n;
osp->rxsize -= n;
osp->rxcnt += m;
osp->rxsize -= m;
osp->rxpkts -= 1;

/* The transaction is completed if the specified number of packets
Expand Down