aboutsummaryrefslogtreecommitdiffstats
path: root/src/stm32/usbfs.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2021-12-19 11:18:33 -0500
committerKevin O'Connor <kevin@koconnor.net>2021-12-23 22:15:26 -0500
commit8d7a6e4ca4eac64e35a5099158f61ff27da8a0e8 (patch)
tree937d2f7c533a35fc37c19a0034b6b4a3e9635b28 /src/stm32/usbfs.c
parenta4a02e86af21303f1874555597814bebd621e011 (diff)
downloadkutter-8d7a6e4ca4eac64e35a5099158f61ff27da8a0e8.tar.gz
kutter-8d7a6e4ca4eac64e35a5099158f61ff27da8a0e8.tar.xz
kutter-8d7a6e4ca4eac64e35a5099158f61ff27da8a0e8.zip
stm32: Rework USB transfer memory layout in usbfs.c
Use a fixed layout for the USB transfer memory and remove the ep_mem struct definition. This is in preparation for stm32g0 support. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/stm32/usbfs.c')
-rw-r--r--src/stm32/usbfs.c101
1 files changed, 55 insertions, 46 deletions
diff --git a/src/stm32/usbfs.c b/src/stm32/usbfs.c
index e2e2928f..435788fb 100644
--- a/src/stm32/usbfs.c
+++ b/src/stm32/usbfs.c
@@ -1,11 +1,10 @@
// Hardware interface to "fullspeed USB controller" on stm32f1
//
-// Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2018-2021 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // NULL
-#include "autoconf.h" // CONFIG_STM32_FLASH_START_2000
#include "board/armcm_boot.h" // armcm_enable_irq
#include "board/armcm_timer.h" // udelay
#include "board/gpio.h" // gpio_out_setup
@@ -31,49 +30,64 @@
* USB transfer memory
****************************************************************/
-struct ep_desc {
- epmword_t addr_tx, count_tx, addr_rx, count_rx;
-};
+// Layout of the USB transfer memory
+#define EPM ((epmword_t*)USB_PMAADDR)
+#define EPM_EP_DESC(ep) (&EPM[(ep) * 4])
+#define EPM_BUF_OFFSET 0x10
+#define EPM_EP_BUF_SIZE (64 / 2 + 1)
+#define EPM_EP_TX_BUF(ep) (&EPM[EPM_BUF_OFFSET + (ep)*2*EPM_EP_BUF_SIZE])
+#define EPM_EP_RX_BUF(ep) (&EPM[EPM_BUF_OFFSET + (1+(ep)*2)*EPM_EP_BUF_SIZE])
-struct ep_mem {
- struct ep_desc ep0, ep_acm, ep_bulk_out, ep_bulk_in;
- epmword_t ep0_tx[USB_CDC_EP0_SIZE / 2];
- epmword_t ep0_rx[USB_CDC_EP0_SIZE / 2 + 1];
- epmword_t ep_acm_tx[USB_CDC_EP_ACM_SIZE / 2];
- epmword_t ep_bulk_out_rx[USB_CDC_EP_BULK_OUT_SIZE / 2 + 1];
- epmword_t ep_bulk_in_tx[USB_CDC_EP_BULK_IN_SIZE / 2];
-};
+// Configure the usb descriptor for an endpoint
+static void
+epm_ep_desc_setup(int ep, int rx_size)
+{
+ uint32_t addr_tx = (EPM_EP_TX_BUF(ep) - EPM) * 2, count_tx = 0;
+ uint32_t addr_rx = (EPM_EP_RX_BUF(ep) - EPM) * 2;
+ uint32_t count_rx = (rx_size <= 30 ? DIV_ROUND_UP(rx_size, 2) << 10
+ : ((DIV_ROUND_UP(rx_size, 32) - 1) << 10) | 0x8000);
+ epmword_t *desc = EPM_EP_DESC(ep);
+ desc[0] = addr_tx;
+ desc[1] = count_tx;
+ desc[2] = addr_rx;
+ desc[3] = count_rx;
+}
-#define EPM ((struct ep_mem *)USB_PMAADDR)
+// Return number of read bytes on an rx endpoint
+static uint32_t
+epm_get_ep_count_rx(int ep)
+{
+ epmword_t *desc = EPM_EP_DESC(ep);
+ return desc[3] & 0x3ff;
+}
-#define CALC_ADDR(p) (((epmword_t*)(p) - (epmword_t*)EPM) * 2)
-#define CALC_SIZE(s) ((s) > 30 ? ((DIV_ROUND_UP((s), 32) - 1) << 10) | 0x8000 \
- : DIV_ROUND_UP((s), 2) << 10)
+// Set number of bytes ready to be transmitted on a tx endpoint
+static void
+epm_set_ep_count_tx(int ep, uint32_t count)
+{
+ epmword_t *desc = EPM_EP_DESC(ep);
+ desc[1] = count;
+}
// Setup the transfer descriptors in dedicated usb memory
static void
btable_configure(void)
{
- EPM->ep0.count_tx = 0;
- EPM->ep0.addr_tx = CALC_ADDR(EPM->ep0_tx);
- EPM->ep0.count_rx = CALC_SIZE(USB_CDC_EP0_SIZE);
- EPM->ep0.addr_rx = CALC_ADDR(EPM->ep0_rx);
-
- EPM->ep_acm.count_tx = 0;
- EPM->ep_acm.addr_tx = CALC_ADDR(EPM->ep_acm_tx);
-
- EPM->ep_bulk_out.count_rx = CALC_SIZE(USB_CDC_EP_BULK_OUT_SIZE);
- EPM->ep_bulk_out.addr_rx = CALC_ADDR(EPM->ep_bulk_out_rx);
-
- EPM->ep_bulk_in.count_tx = 0;
- EPM->ep_bulk_in.addr_tx = CALC_ADDR(EPM->ep_bulk_in_tx);
+ epm_ep_desc_setup(0, USB_CDC_EP0_SIZE);
+ epm_ep_desc_setup(USB_CDC_EP_ACM, 0);
+ epm_ep_desc_setup(USB_CDC_EP_BULK_OUT, USB_CDC_EP_BULK_OUT_SIZE);
+ epm_ep_desc_setup(USB_CDC_EP_BULK_IN, 0);
}
// Read a packet stored in dedicated usb memory
-static void
-btable_read_packet(uint8_t *dest, epmword_t *src, int count)
+static uint32_t
+btable_read_packet(int ep, uint8_t *dest, int max_len)
{
- uint_fast8_t i;
+ epmword_t *src = EPM_EP_RX_BUF(ep);
+ uint32_t count = epm_get_ep_count_rx(ep);
+ if (count > max_len)
+ count = max_len;
+ int i;
for (i=0; i<(count/2); i++) {
uint32_t d = *src++;
*dest++ = d;
@@ -81,12 +95,14 @@ btable_read_packet(uint8_t *dest, epmword_t *src, int count)
}
if (count & 1)
*dest = *src;
+ return count;
}
// Write a packet to dedicated usb memory
static void
-btable_write_packet(epmword_t *dest, const uint8_t *src, int count)
+btable_write_packet(int ep, const uint8_t *src, int count)
{
+ epmword_t *dest = EPM_EP_TX_BUF(ep);
int i;
for (i=0; i<(count/2); i++) {
uint8_t b1 = *src++, b2 = *src++;
@@ -94,6 +110,7 @@ btable_write_packet(epmword_t *dest, const uint8_t *src, int count)
}
if (count & 1)
*dest = *src;
+ epm_set_ep_count_tx(ep, count);
}
@@ -137,10 +154,7 @@ usb_read_bulk_out(void *data, uint_fast8_t max_len)
if ((epr & USB_EPRX_STAT) == USB_EP_RX_VALID)
// No data ready
return -1;
- uint32_t count = EPM->ep_bulk_out.count_rx & 0x3ff;
- if (count > max_len)
- count = max_len;
- btable_read_packet(data, EPM->ep_bulk_out_rx, count);
+ uint32_t count = btable_read_packet(USB_CDC_EP_BULK_OUT, data, max_len);
USB_EPR[USB_CDC_EP_BULK_OUT] = set_stat_rx_bits(epr, USB_EP_RX_VALID);
return count;
}
@@ -152,8 +166,7 @@ usb_send_bulk_in(void *data, uint_fast8_t len)
if ((epr & USB_EPTX_STAT) != USB_EP_TX_NAK)
// No buffer space available
return -1;
- btable_write_packet(EPM->ep_bulk_in_tx, data, len);
- EPM->ep_bulk_in.count_tx = len;
+ btable_write_packet(USB_CDC_EP_BULK_IN, data, len);
USB_EPR[USB_CDC_EP_BULK_IN] = set_stat_tx_bits(epr, USB_EP_TX_VALID);
return len;
}
@@ -165,10 +178,7 @@ usb_read_ep0(void *data, uint_fast8_t max_len)
if ((epr & USB_EPRX_STAT) != USB_EP_RX_NAK)
// No data ready
return -1;
- uint32_t count = EPM->ep0.count_rx & 0x3ff;
- if (count > max_len)
- count = max_len;
- btable_read_packet(data, EPM->ep0_rx, count);
+ uint32_t count = btable_read_packet(0, data, max_len);
USB_EPR[0] = set_stat_rxtx_bits(epr, USB_EP_RX_VALID | USB_EP_TX_NAK);
return count;
}
@@ -189,8 +199,7 @@ usb_send_ep0(const void *data, uint_fast8_t len)
if ((epr & USB_EPTX_STAT) != USB_EP_TX_NAK)
// No buffer space available
return -1;
- btable_write_packet(EPM->ep0_tx, data, len);
- EPM->ep0.count_tx = len;
+ btable_write_packet(0, data, len);
USB_EPR[0] = set_stat_tx_bits(epr, USB_EP_TX_VALID);
return len;
}