Implemented an optional callback function that is used instead of tu_edpt_number in the stm32 device driver as a crude tool to control mapping of the endpoint address to actual endpoint register

This commit is contained in:
Simon Kueppers 2022-10-24 22:07:45 +02:00 committed by Mengsk
parent 6d65eaf7a1
commit d9b2c9934a
2 changed files with 35 additions and 22 deletions

View File

@ -400,7 +400,7 @@ void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
(void) dev_addr; (void) dev_addr;
// Respond with status // Respond with status
dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0); dcd_edpt_xfer(rhport, TUSB_DIR_IN_MASK | 0x00, NULL, 0);
// DCD can only set address after status for this request is complete. // DCD can only set address after status for this request is complete.
// do it at dcd_edpt0_status_complete() // do it at dcd_edpt0_status_complete()
@ -479,7 +479,8 @@ static void dcd_ep_ctr_tx_handler(uint32_t wIstr)
} }
else /* TX Complete */ else /* TX Complete */
{ {
dcd_event_xfer_complete(0, (uint8_t)(0x80 + EPindex), xfer->total_len, XFER_RESULT_SUCCESS, true); uint8_t ep_addr = wEPRegVal & USB_EPADDR_FIELD;
dcd_event_xfer_complete(0, (uint8_t)(TUSB_DIR_IN_MASK | ep_addr), xfer->total_len, XFER_RESULT_SUCCESS, true);
} }
} }
@ -543,7 +544,8 @@ static void dcd_ep_ctr_rx_handler(uint32_t wIstr)
if ((count < xfer->max_packet_size) || (xfer->queued_len == xfer->total_len)) if ((count < xfer->max_packet_size) || (xfer->queued_len == xfer->total_len))
{ {
/* RX COMPLETE */ /* RX COMPLETE */
dcd_event_xfer_complete(0, EPindex, xfer->queued_len, XFER_RESULT_SUCCESS, true); uint8_t ep_addr = wEPRegVal & USB_EPADDR_FIELD;
dcd_event_xfer_complete(0, ep_addr, xfer->queued_len, XFER_RESULT_SUCCESS, true);
// Though the host could still send, we don't know. // Though the host could still send, we don't know.
// Does the bulk pipe need to be reset to valid to allow for a ZLP? // Does the bulk pipe need to be reset to valid to allow for a ZLP?
} }
@ -706,7 +708,7 @@ static void dcd_pma_alloc_reset(void)
*/ */
static uint16_t dcd_pma_alloc(uint8_t ep_addr, size_t length) static uint16_t dcd_pma_alloc(uint8_t ep_addr, size_t length)
{ {
uint8_t const epnum = tu_edpt_number(ep_addr); uint8_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(ep_addr) : tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const dir = tu_edpt_dir(ep_addr);
xfer_ctl_t* epXferCtl = xfer_ctl_ptr(epnum,dir); xfer_ctl_t* epXferCtl = xfer_ctl_ptr(epnum,dir);
@ -737,7 +739,7 @@ static uint16_t dcd_pma_alloc(uint8_t ep_addr, size_t length)
*/ */
static void dcd_pma_free(uint8_t ep_addr) static void dcd_pma_free(uint8_t ep_addr)
{ {
uint8_t const epnum = tu_edpt_number(ep_addr); uint8_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(ep_addr) : tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const dir = tu_edpt_dir(ep_addr);
// Presently, this should never be called for EP0 IN/OUT // Presently, this should never be called for EP0 IN/OUT
@ -768,7 +770,7 @@ static void dcd_pma_free(uint8_t ep_addr)
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc) bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
{ {
(void)rhport; (void)rhport;
uint8_t const epnum = tu_edpt_number(p_endpoint_desc->bEndpointAddress); uint8_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(p_endpoint_desc->bEndpointAddress) : tu_edpt_number(p_endpoint_desc->bEndpointAddress);
uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress); uint8_t const dir = tu_edpt_dir(p_endpoint_desc->bEndpointAddress);
const uint16_t epMaxPktSize = tu_edpt_packet_size(p_endpoint_desc); const uint16_t epMaxPktSize = tu_edpt_packet_size(p_endpoint_desc);
uint16_t pma_addr; uint16_t pma_addr;
@ -798,7 +800,7 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc
} }
pcd_set_eptype(USB, epnum, wType); pcd_set_eptype(USB, epnum, wType);
pcd_set_ep_address(USB, epnum, epnum); pcd_set_ep_address(USB, epnum, tu_edpt_number(p_endpoint_desc->bEndpointAddress));
// Be normal, for now, instead of only accepting zero-byte packets (on control endpoint) // Be normal, for now, instead of only accepting zero-byte packets (on control endpoint)
// or being double-buffered (bulk endpoints) // or being double-buffered (bulk endpoints)
pcd_clear_ep_kind(USB,0); pcd_clear_ep_kind(USB,0);
@ -861,7 +863,7 @@ void dcd_edpt_close_all (uint8_t rhport)
void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr) void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr)
{ {
(void)rhport; (void)rhport;
uint32_t const epnum = tu_edpt_number(ep_addr); uint32_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(ep_addr) : tu_edpt_number(ep_addr);
uint32_t const dir = tu_edpt_dir(ep_addr); uint32_t const dir = tu_edpt_dir(ep_addr);
if(dir == TUSB_DIR_IN) if(dir == TUSB_DIR_IN)
@ -907,7 +909,7 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
{ {
(void) rhport; (void) rhport;
uint8_t const epnum = tu_edpt_number(ep_addr); uint8_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(ep_addr) : tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const dir = tu_edpt_dir(ep_addr);
xfer_ctl_t * xfer = xfer_ctl_ptr(epnum,dir); xfer_ctl_t * xfer = xfer_ctl_ptr(epnum,dir);
@ -945,7 +947,7 @@ bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16
{ {
(void) rhport; (void) rhport;
uint8_t const epnum = tu_edpt_number(ep_addr); uint8_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(ep_addr) : tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr); uint8_t const dir = tu_edpt_dir(ep_addr);
xfer_ctl_t * xfer = xfer_ctl_ptr(epnum,dir); xfer_ctl_t * xfer = xfer_ctl_ptr(epnum,dir);
@ -976,13 +978,16 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr)
{ {
(void)rhport; (void)rhport;
if (ep_addr & 0x80) uint8_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(ep_addr) : tu_edpt_number(ep_addr);
uint8_t const dir = tu_edpt_dir(ep_addr);
if (dir == TUSB_DIR_IN)
{ // IN { // IN
pcd_set_ep_tx_status(USB, ep_addr & 0x7F, USB_EP_TX_STALL); pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_STALL);
} }
else else
{ // OUT { // OUT
pcd_set_ep_rx_status(USB, ep_addr, USB_EP_RX_STALL); pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_STALL);
} }
} }
@ -990,24 +995,23 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
{ {
(void)rhport; (void)rhport;
if (ep_addr & 0x80) uint8_t const epnum = tu_stm32_edpt_number_cb ? tu_stm32_edpt_number_cb(ep_addr) : tu_edpt_number(ep_addr);
{ // IN uint8_t const dir = tu_edpt_dir(ep_addr);
ep_addr &= 0x7F;
uint8_t const epnum = tu_edpt_number(ep_addr); if (dir == TUSB_DIR_IN)
{ // IN
if (pcd_get_eptype(USB, epnum) != USB_EP_ISOCHRONOUS) { if (pcd_get_eptype(USB, epnum) != USB_EP_ISOCHRONOUS) {
pcd_set_ep_tx_status(USB,ep_addr, USB_EP_TX_NAK); pcd_set_ep_tx_status(USB, epnum, USB_EP_TX_NAK);
} }
/* Reset to DATA0 if clearing stall condition. */ /* Reset to DATA0 if clearing stall condition. */
pcd_clear_tx_dtog(USB,ep_addr); pcd_clear_tx_dtog(USB, epnum);
} }
else else
{ // OUT { // OUT
/* Reset to DATA0 if clearing stall condition. */ /* Reset to DATA0 if clearing stall condition. */
pcd_clear_rx_dtog(USB,ep_addr); pcd_clear_rx_dtog(USB, epnum);
pcd_set_ep_rx_status(USB, epnum, USB_EP_RX_NAK);
pcd_set_ep_rx_status(USB,ep_addr, USB_EP_RX_NAK);
} }
} }

View File

@ -117,6 +117,15 @@
// Volatile is also needed to prevent the optimizer from changing access to 32-bit (as 32-bit access is forbidden) // Volatile is also needed to prevent the optimizer from changing access to 32-bit (as 32-bit access is forbidden)
static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR; static __IO uint16_t * const pma = (__IO uint16_t*)USB_PMAADDR;
// This array maps the endpoint address to a endpoint register in hardware
// By default the tinyusb stack chooses the endpoint register using the endpoint number
// However when using ISOCHRONOUS endpoints, the entire endpoint register (RX _and_ TX)
// are used for a double buffer in a single direction. If you now want to use the same endpoint number with
// different directions (e.g. 0x02 and 0x82), they would be mapped to the same register
// In this case, this serves as a crude helper to overcome this limitation by implementing
// this callback and defining a custom mapping in your application
TU_ATTR_WEAK uint8_t tu_stm32_edpt_number_cb(uint8_t addr);
// prototypes // prototypes
static inline __IO uint16_t* pcd_ep_rx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum); static inline __IO uint16_t* pcd_ep_rx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum);
static inline __IO uint16_t* pcd_ep_tx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum); static inline __IO uint16_t* pcd_ep_tx_cnt_ptr(USB_TypeDef * USBx, uint32_t bEpNum);