Skip to content

Commit

Permalink
SuperCAN: D5034-04: device is recognized by Linux driver
Browse files Browse the repository at this point in the history
Integrated GD's USB stack to handle the lower level bits of USB.
  • Loading branch information
jgressmann committed Oct 7, 2023
1 parent 0ed4fbf commit b7eb653
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 89 deletions.
86 changes: 40 additions & 46 deletions hw/bsp/d5035_04/d5035_04.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,12 @@ void dcd_init(uint8_t rhport)

NVIC_SetPriority(USBFS_IRQn, SC_ISR_PRIORITY);

LOG("1\n");

LOG("prio tim2 %u\n", NVIC_GetPriority(TIMER2_IRQn));

// __disable_irq();

/* initializes the USB core*/
/* initializes the USB core*/
(void)usb_core_init (udev->bp, &udev->regs);

// __disable_irq();

LOG("2\n");

/* set device disconnect */
/* set device disconnect */
usbd_disconnect (udev);

#ifndef USE_OTG_MODE
Expand All @@ -375,8 +367,6 @@ void dcd_init(uint8_t rhport)

usb_globalint_enable(&udev->regs);



/* set device connect */
usbd_connect (udev);

Expand Down Expand Up @@ -439,10 +429,12 @@ void dcd_disconnect(uint8_t rhport)
// Configure endpoint's registers according to descriptor
bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep)
{
LOG("dcd_edpt_open\n");
// LOG("dcd_edpt_open\n");

(void)rhport;
(void)desc_ep;
// (void)desc_ep;

usbd_ep_setup(&driver, desc_ep);

return true;
}
Expand Down Expand Up @@ -472,15 +464,22 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t t
usb_core_driver* udev = &driver;
(void)rhport;

LOG("dcd_edpt_xfer %02x\n", ep_addr);

// if (ep_addr & 0x80) {
// // always returns 0
// (void)usbd_ep_send(udev, ep_addr, buffer, total_bytes);
// } else {
// // always returns 0
// (void)usbd_ep_recev(udev, ep_addr, buffer, total_bytes);
// }
uint8_t pipe = ep_addr & ~0x80;

if (pipe) {
// LOG("dcd_edpt_xfer %02x\n", ep_addr);
if (ep_addr & 0x80) {
// always returns 0
// LOG("usbd_ep_send %02x\n", ep_addr);
(void)usbd_ep_send(udev, ep_addr, buffer, total_bytes);
} else {
// always returns 0
// LOG("usbd_ep_recev %02x\n", ep_addr);
(void)usbd_ep_recev(udev, ep_addr, buffer, total_bytes);
}
} else {
// EP0 is handled by GD USB stack
}

return true;
}
Expand Down Expand Up @@ -564,29 +563,31 @@ static uint8_t sc_core_req(usb_dev *udev, usb_req *req)
}
static uint8_t sc_core_in(usb_dev *udev, uint8_t ep_num)
{
LOG("sc_core_in %02x\n", ep_num);
dcd_event_xfer_complete(0, ep_num, udev->dev.transc_in[ep_num & ~0x80].xfer_len, XFER_RESULT_SUCCESS, true);
// LOG("sc_core_in %02x\n", 0x80 | ep_num);
// LOG("> %02x l=%u\n", 0x80 | ep_num, udev->dev.transc_in[ep_num].xfer_len);
dcd_event_xfer_complete(0, 0x80 | ep_num, udev->dev.transc_in[ep_num].xfer_len, XFER_RESULT_SUCCESS, true);
return USBD_OK;
}
static uint8_t sc_core_out(usb_dev *udev, uint8_t ep_num)
{
LOG("sc_core_out %02x\n", ep_num);
// LOG("sc_core_out %02x\n", ep_num);
// LOG("< %02x l=%u\n", ep_num, udev->dev.transc_out[ep_num].xfer_len);
dcd_event_xfer_complete(0, ep_num, udev->dev.transc_out[ep_num].xfer_len, XFER_RESULT_SUCCESS, true);
return USBD_OK;
}

static uint8_t sc_core_ctlx_in(usb_dev *udev, uint8_t ep_num)
{
LOG("sc_core_ctlx_in\n", ep_num);
// dcd_event_xfer_complete(0, ep_num, udev->dev.transc_in[ep_num & ~0x80].xfer_len, XFER_RESULT_SUCCESS, true);
return USBD_OK;
}
static uint8_t sc_core_ctlx_out(usb_dev *udev, uint8_t ep_num)
{
LOG("sc_core_out %02x\n", ep_num);
// dcd_event_xfer_complete(0, ep_num, udev->dev.transc_out[ep_num].xfer_len, XFER_RESULT_SUCCESS, true);
return USBD_OK;
}
// static uint8_t sc_core_ctlx_in(usb_dev *udev, uint8_t ep_num)
// {
// LOG("sc_core_ctlx_in\n", ep_num);
// // dcd_event_xfer_complete(0, ep_num, udev->dev.transc_in[ep_num & ~0x80].xfer_len, XFER_RESULT_SUCCESS, true);
// return USBD_OK;
// }
// static uint8_t sc_core_ctlx_out(usb_dev *udev, uint8_t ep_num)
// {
// LOG("sc_core_out %02x\n", ep_num);
// // dcd_event_xfer_complete(0, ep_num, udev->dev.transc_out[ep_num].xfer_len, XFER_RESULT_SUCCESS, true);
// return USBD_OK;
// }



Expand Down Expand Up @@ -640,14 +641,7 @@ void usb_rcu_config(void)
*/
void usb_timer_init (void)
{
/* configure the priority group to 2 bits */
// nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);

/* enable the TIM2 global interrupt */
// nvic_irq_enable((uint8_t)TIMER2_IRQn, 1U, 0U);


// DO NOT SET interrupt prio, as the RTOS hasn't
// DO NOT SET interrupt prio, as the RTOS hasn't
// set up proper handling yet
// NVIC_SetPriority(TIMER2_IRQn, SC_ISR_PRIORITY);
NVIC_EnableIRQ(TIMER2_IRQn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,8 @@ static uint8_t* (*std_desc_get[])(usb_core_driver *udev, uint8_t index, uint16_t
*/
usb_reqsta usbd_standard_request (usb_core_driver *udev, usb_req *req)
{
LOG("usbd_standard_request %x\n", req->bRequest);
// dcd_event_t ev;
// LOG("usbd_standard_request %x\n", req->bRequest);

// ev.rhport = RHPORT;
// ev.event_id = DCD_EVENT_SETUP_RECEIVED;
// ev.setup_received = *(tusb_control_request_t*)req;


// dcd_event_handler(&ev, true);
return (*_std_dev_req[req->bRequest])(udev, req);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ uint8_t usbd_setup_transc (usb_core_driver *udev)
switch (req.bmRequestType & USB_REQTYPE_MASK) {
/* standard device request */
case USB_REQTYPE_STRD:
LOG("setup\n");
// LOG("setup\n");
reqstat = usbd_standard_request (udev, &req);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,48 @@
/*
Copyright (c) 2023, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "drv_usb_core.h"
#include "drv_usb_hw.h"

#include <supercan_debug.h>

/* local function prototypes ('static') */
static void usb_core_reset (usb_core_regs *usb_regs);

/*!
\brief configure USB core basic
\brief configure USB core basic
\param[in] usb_basic: pointer to USB capabilities
\param[in] usb_regs: USB core registers
\param[in] usb_core: USB core
\param[out] none
\retval operation status
*/
usb_status usb_basic_init (usb_core_basic *usb_basic,
usb_core_regs *usb_regs,
usb_status usb_basic_init (usb_core_basic *usb_basic,
usb_core_regs *usb_regs,
usb_core_enum usb_core)
{
/* configure USB default transfer mode as FIFO mode */
Expand Down Expand Up @@ -111,7 +113,7 @@ usb_status usb_basic_init (usb_core_basic *usb_basic,
}

/*!
\brief initializes the USB controller registers and
\brief initializes the USB controller registers and
prepares the core device mode or host mode operation
\param[in] usb_basic: pointer to USB capabilities
\param[in] usb_regs: pointer to USB core registers
Expand Down Expand Up @@ -186,15 +188,17 @@ usb_status usb_core_init (usb_core_basic usb_basic, usb_core_regs *usb_regs)
\param[out] none
\retval operation status
*/
usb_status usb_txfifo_write (usb_core_regs *usb_regs,
uint8_t *src_buf,
uint8_t fifo_num,
usb_status usb_txfifo_write (usb_core_regs *usb_regs,
uint8_t *src_buf,
uint8_t fifo_num,
uint16_t byte_count)
{
uint32_t word_count = (byte_count + 3U) / 4U;

__IO uint32_t *fifo = usb_regs->DFIFO[fifo_num];

// SC_DEBUG_ASSERT((((uintptr_t)src_buf) & 3) == 0);

while (word_count-- > 0U) {
*fifo = *((__packed uint32_t *)src_buf);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,23 @@ static uint32_t usbd_int_rxfifo (usb_core_driver *udev)

case RSTAT_XFER_COMP:
/* trigger the OUT endpoint interrupt */
LOG("RSTAT_XFER_COMP\n");
if ((uint8_t)USBD_CONFIGURED == udev->dev.cur_status) {
dcd_event_xfer_complete(RHPORT, ep_num, transc->xfer_count, XFER_RESULT_SUCCESS, true);
}
// LOG("RSTAT_XFER_COMP %02x\n", ep_num);
// if ((uint8_t)USBD_CONFIGURED == udev->dev.cur_status) {
// dcd_event_xfer_complete(RHPORT, ep_num, transc->xfer_count, XFER_RESULT_SUCCESS, true);
// }
break;

case RSTAT_SETUP_COMP:
/* trigger the OUT endpoint interrupt */
LOG("RSTAT_SETUP_COMP\n");
// LOG("RSTAT_SETUP_COMP\n");
dcd_event_setup_received(RHPORT, &udev->dev.control.req, true);
break;

case RSTAT_SETUP_UPDT:
if ((0U == transc->ep_addr.num) && (8U == bcount) && (DPID_DATA0 == data_PID)) {
/* copy the setup packet received in FIFO into the setup buffer in RAM */
(void)usb_rxfifo_read (&udev->regs, (uint8_t *)&udev->dev.control.req, (uint16_t)bcount);
LOG("RSTAT_SETUP_UPDT\n");
// dcd_event_setup_received(RHPORT, &udev->dev.control.req, true);
// LOG("RSTAT_SETUP_UPDT\n");

transc->xfer_count += bcount;
}
Expand Down Expand Up @@ -464,6 +463,8 @@ static uint32_t usbd_emptytxfifo_write (usb_core_driver *udev, uint32_t ep_num)
len = transc->max_len;
}

// LOG("tx%u %u\n", ep_num, len);

word_count = (len + 3U) / 4U;

while (((udev->regs.er_in[ep_num]->DIEPTFSTAT & DIEPTFSTAT_IEPTFS) >= word_count) && \
Expand Down
10 changes: 5 additions & 5 deletions hw/bsp/d5035_04/usb_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
#define USB_FS_CORE 1



#define RX_FIFO_FS_SIZE 256
#define TX0_FIFO_FS_SIZE 64
#define TX1_FIFO_FS_SIZE 64
#define TX2_FIFO_FS_SIZE 256
// in 4 byte terms
#define RX_FIFO_FS_SIZE 128
#define TX0_FIFO_FS_SIZE 16
#define TX1_FIFO_FS_SIZE 16
#define TX2_FIFO_FS_SIZE 128
#define TX3_FIFO_FS_SIZE 0


Expand Down

0 comments on commit b7eb653

Please sign in to comment.