/*!
    \file  usbd_lld_core.c
    \brief USB device low level driver core

    \version 2018-06-01, V1.0.0, firmware for GD32 USBD
*/

/*
    Copyright (c) 2018, GigaDevice Semiconductor Inc.

    All rights reserved.

    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 
       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 
       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 
       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 
OF SUCH DAMAGE.
*/

#include "usb_common.h"
#include "usbd_core.h"

#define USB_EPTYPE_MASK           0x03U

#if defined (__CC_ARM)         /* ARM Compiler */
    usbd_ep_ram btable_ep[EP_COUNT]__attribute__((at(USBD_RAM + BTABLE_OFFSET)));
#elif defined (__ICCARM__)     /* IAR Compiler */
    __no_init usbd_ep_ram btable_ep[EP_COUNT] @(USBD_RAM + BTABLE_OFFSET);
#endif

const uint32_t ep_type[] = {
    [USB_EP_ATTR_CONTROL]  = EP_CONTROL,
    [USB_EP_ATTR_BULK] = EP_BULK,
    [USB_EP_ATTR_INT]  = EP_INTERRUPT,
    [USB_EP_ATTR_ISOC]  = EP_ISO
};

usb_dev udev;

/*!
    \brief      device core register initialization
    \param[in]  none
    \param[out] none
    \retval     none
*/
void usbd_core_reset (void)
{
    udev.dev_addr = 0U;

    /* reset the CLOSE bit */
    USBD_CTL = CTL_SETRST;

    /* may be need wait some time(tSTARTUP) ... */

    /* clear SETRST bit in USBD_CTL register */
    USBD_CTL = 0U;

    /* clear all pending interrupts */
    USBD_INTF = 0U;

    /* set descriptors table offset in USB dedicated SRAM */
    USBD_BADDR = BTABLE_OFFSET & 0xFFF8U;

#ifdef LPM_ENABLED
    /* enable L1REQ interrupt */
    USBD_CTL = CTL_L1REQIE;

    USBD_LPMCS = LPMCS_LPMACK | LPMCS_LPMEN;
#endif /* LPM_ENABLED */

    /* enable all interrupts mask bits */
    USBD_CTL |= CTL_STIE | CTL_WKUPIE | CTL_SPSIE | CTL_SOFIE | CTL_ESOFIE | CTL_RSTIE;
}

/*!
    \brief      free buffer used from application by toggling the SW_BUF byte
    \param[in]  ep_num: endpoint identifier (0..7)
    \param[in]  dir: endpoint direction which can be OUT(0) or IN(1)
    \param[out] none
    \retval     None
*/
void user_buffer_free (uint8_t ep_num, uint8_t dir)
{
    if (DBUF_EP_OUT == dir) {
        USBD_SWBUF_TOGGLE(ep_num, TX);
    } else if (DBUF_EP_IN == dir) {
        USBD_SWBUF_TOGGLE(ep_num, RX);
    } else {
        /* no operation */
    }
}

/*!
    \brief      device core register configure when stop device
    \param[in]  none
    \param[out] none
    \retval     none
*/
void usbd_core_stop (void)
{
    /* disable all interrupts and set USB reset */
    USBD_CTL = CTL_SETRST;

    /* clear all interrupt flags */
    USBD_INTF = 0U;

    /* close device */
    USBD_CTL = CTL_SETRST | CTL_CLOSE;
}

/*!
    \brief      set device address
    \param[in]  udev: pointer to USB device instance
    \param[out] none
    \retval     none
*/
void usbd_set_address (uint8_t dev_addr)
{
    udev.dev_addr = dev_addr;

    if (dev_addr == 0) {
        USBD_DADDR = DADDR_USBEN;
    }
}

/*!
    \brief      handle USB reset event
    \param[in]  udev: pointer to USB device instance
    \param[out] none
    \retval     none
*/
void usbd_ep_reset (void)
{
    uint8_t i = 0U, ep_max_len = USBD_EP0_MAX_SIZE;

    btable_ep[0].tx_addr = EP0_TX_ADDR;
    btable_ep[0].tx_count = 0U;

    btable_ep[0].rx_addr = EP0_RX_ADDR;

    if (ep_max_len > 62U) {
        btable_ep[0].rx_count = ((ep_max_len << 5) - 1U) | 0x8000U;
    } else {
        btable_ep[0].rx_count = ((ep_max_len + 1U) & ~1U) << 9;
    }

    /* reset non-control endpoints */
    for (i = 1U; i < EP_COUNT; i++) {
        USBD_EPxCS(i) = (USBD_EPxCS(i) & EPCS_MASK) | i;
    }

    /* clear endpoint 0 register */
    USBD_EPxCS(0)= (uint16_t)(USBD_EPxCS(0));

    USBD_EPxCS(0) = EP_CONTROL | EPRX_VALID | EPTX_NAK;

    /* set device address as default address 0 */
    USBD_DADDR = DADDR_USBEN;
}

/*!
    \brief      endpoint initialization
    \param[in]  udev: pointer to USB core instance
    \param[in]  buf_kind: endpoint buffer kind
    \param[in]  buf_addr: endpoint buffer address
    \param[in]  ep_desc: pointer to endpoint descriptor
    \param[out] none
    \retval     none
*/
void usbd_ep_setup (uint8_t buf_kind, uint16_t buf_addr, void *ep_desc)
{
    uint8_t ep_addr = ((uep_desc_t)ep_desc)->bEndpointAddress;
    uint8_t max_len = ((uep_desc_t)ep_desc)->wMaxPacketSize;
    uint8_t ep_num = EP_ID(ep_addr);

    /* set the endpoint type */
    USBD_EPxCS(ep_num) = ep_type[((uep_desc_t)ep_desc)->bmAttributes & USB_EPTYPE_MASK] | ep_num;

    if (EP_DIR(ep_addr)) {

        if (EP_BUF_SNG == buf_kind) {
            btable_ep[ep_num].tx_addr = buf_addr;

            /* configure the endpoint status as NAK status */
            USBD_EP_STAT_SET(ep_num, TX, NAK);
        } else if (EP_BUF_DBL == buf_kind) {
            USBD_EP_DBL_BUF_SET(ep_num);

            btable_ep[ep_num].tx_addr = BYTE_LOW(buf_addr);
            btable_ep[ep_num].rx_addr = BYTE_HIGH(buf_addr);

            USBD_EP_STAT_SET(ep_num, TX, VALID);
            USBD_EP_STAT_SET(ep_num, RX, DISABLED);
        } else {
            /* error operation */
        }
    } else {
        if (EP_BUF_SNG == buf_kind) {
            btable_ep[ep_num].rx_addr = buf_addr;
        } else if (EP_BUF_DBL == buf_kind) {
            USBD_EP_DBL_BUF_SET(ep_num);

            USBD_DTG_TOGGLE(ep_num, TX);

            btable_ep[ep_num].tx_addr = BYTE_LOW(buf_addr);
            btable_ep[ep_num].rx_addr = BYTE_HIGH(buf_addr);

            if (max_len > 62U) {
                btable_ep[ep_num].tx_count = ((max_len << 5) - 1U) | 0x8000U;
            } else {
                btable_ep[ep_num].tx_count = ((max_len + 1U) & ~1U) << 9;
            }
        } else {
            /* error operation */
        }

        if (max_len > 62U) {
            btable_ep[ep_num].rx_count = ((max_len << 5U) - 1U) | 0x8000U;
        } else {
            btable_ep[ep_num].rx_count = ((max_len + 1U) & ~1U) << 9;
        }

        if (EP_BUF_SNG == buf_kind) {
            /* configure the endpoint status as NAK status */
            USBD_EP_STAT_SET(ep_num, RX, VALID);
        } else if (EP_BUF_DBL == buf_kind) {
            USBD_EP_STAT_SET(ep_num, RX, DISABLED);
            USBD_EP_STAT_SET(ep_num, TX, NAK);
        } else {
            /* error operation */
        }
    }
}

/*!
    \brief      configure the endpoint when it is disabled
    \param[in]  udev: pointer to USB core instance
    \param[in]  ep_addr: endpoint address
                  in this parameter:
                    bit0..bit6: endpoint number (0..7)
                    bit7: endpoint direction which can be IN(1) or OUT(0)
    \param[out] none
    \retval     none
*/
void usbd_ep_disable (uint8_t ep_addr)
{
    uint8_t ep_num = EP_ID(ep_addr);

    if (EP_DIR(ep_addr)) {
        USBD_DTG_CLEAR(ep_num, TX);

        /* configure the endpoint status as DISABLED */
        USBD_EP_STAT_SET(ep_num, TX, DISABLED);
    } else {
        USBD_DTG_CLEAR(ep_num, RX);

        /* configure the endpoint status as DISABLED */
        USBD_EP_STAT_SET(ep_num, RX, DISABLED);
    }
}

/*!
    \brief      enable endpoint to receive
    \param[in]  udev: pointer to USB core instance
    \param[in]  ep_addr: endpoint address
                  in this parameter:
                    bit0..bit6: endpoint number (0..7)
                    bit7: endpoint direction which can be IN(1) or OUT(0)
    \param[out] none
    \retval     none
*/
void usbd_ep_rx_enable (uint8_t ep_addr, uint8_t* buffer)
{
    udev.xfer_buf = buffer;

    /* enable endpoint to receive */
    USBD_EP_STAT_SET(EP_ID(ep_addr), RX, VALID);
}

/*!
    \brief      set an endpoint to STALL status
    \param[in]  udev: pointer to USB core instance
    \param[in]  ep_addr: endpoint address
                  in this parameter:
                    bit0..bit6: endpoint number (0..7)
                    bit7: endpoint direction which can be IN(1) or OUT(0)
    \param[out] none
    \retval     none
*/
void usbd_ep_stall_set (uint8_t ep_addr)
{
    uint8_t ep_num = EP_ID(ep_addr);

    if (0U == ep_num) {
        USBD_EP_STAT_SET(0U, TX, STALL);
        USBD_EP_STAT_SET(0U, RX, STALL);
    } else {
        if (EP_DIR(ep_addr)) {
            USBD_EP_STAT_SET(ep_num, TX, STALL);
        } else {
            USBD_EP_STAT_SET(ep_num, RX, STALL);
        }
    }
}

/*!
    \brief      clear endpoint stalled status
    \param[in]  udev: pointer to USB core instance
    \param[in]  ep_addr: endpoint address
                  in this parameter:
                    bit0..bit6: endpoint number (0..7)
                    bit7: endpoint direction which can be IN(1) or OUT(0)
    \param[out] none
    \retval     none
*/
void usbd_ep_stall_clear (uint8_t ep_addr)
{
    uint8_t ep_num = EP_ID(ep_addr);

    if (EP_DIR(ep_addr)) {
        /* clear endpoint data toggle bit */
        USBD_DTG_CLEAR(ep_num, TX);

        /* clear endpoint stall status */
        USBD_EP_STAT_SET(ep_num, TX, VALID);
    } else {
        /* clear endpoint data toggle bit */
        USBD_DTG_CLEAR(ep_num, RX);

        /* clear endpoint stall status */
        USBD_EP_STAT_SET(ep_num, RX, VALID);
    }
}

/*!
    \brief      get the endpoint status
    \param[in]  udev: pointer to USB core instance
    \param[in]  ep_addr: endpoint address
                  in this parameter:
                    bit0..bit6: endpoint number (0..7)
                    bit7: endpoint direction which can be IN(1) or OUT(0)
    \param[out] none
    \retval     endpoint status
*/
uint8_t usbd_ep_status_get (uint8_t ep_addr)
{
    uint8_t epcs = USBD_EPxCS(EP_ID(ep_addr));

    if (EP_DIR(ep_addr)) {
        return epcs & EPxCS_TX_STA;
    } else {
        return epcs & EPxCS_RX_STA;
    }
}

/*!
    \brief      write data from user FIFO to USB RAM
    \param[in]  user_fifo: pointer to user FIFO
    \param[in]  ep_num: endpoint number
    \param[in]  bytes: the bytes count of the write data
    \param[out] none
    \retval     none
*/
void usbd_ep_data_write (uint8_t *user_fifo, uint8_t ep_num, uint16_t bytes)
{
    if (bytes != 0U) {
        uint32_t n;
        uint32_t *write_addr = (uint32_t *)(btable_ep[ep_num].tx_addr * 2U + USBD_RAM);

        for (n = 0U; n < (bytes + 1U) / 2U; n++) {
            *write_addr++ = *((__packed uint16_t*)user_fifo);
            user_fifo += 2U;
        }
    }

    btable_ep[ep_num].tx_count = bytes;

    USBD_EP_STAT_SET(ep_num, TX, VALID);
}

/*!
    \brief      read data from USBRAM to user FIFO
    \param[in]  user_fifo: pointer to user FIFO
    \param[in]  ep_num: endpoint number
    \param[in]  buf_kind: endpoint buffer kind
    \param[out] none
    \retval     none
*/
uint16_t usbd_ep_data_read (uint8_t *user_fifo, uint8_t ep_num, uint8_t buf_kind)
{
    uint16_t n = 0U, bytes = 0U;
    uint32_t *read_addr = NULL;

    if (EP_BUF_SNG == buf_kind) {
        bytes = btable_ep[ep_num].rx_count & EPRCNT_CNT;

        read_addr = (uint32_t *)(btable_ep[ep_num].rx_addr * 2U + USBD_RAM);
    } else if (EP_BUF_DBL == buf_kind) {
        if (USBD_EPxCS(ep_num) & EPxCS_TX_DTG) {
            bytes = btable_ep[ep_num].tx_count & EPRCNT_CNT;

            read_addr = (uint32_t *)(btable_ep[ep_num].tx_addr * 2U + USBD_RAM);
        } else {
            bytes = btable_ep[ep_num].rx_count & EPRCNT_CNT;

            read_addr = (uint32_t *)(btable_ep[ep_num].rx_addr * 2U + USBD_RAM);
        }
    } else {
        return 0U;
    }

    for (n = 0U; n < (bytes + 1U) / 2U; n++) {
        *((__packed uint16_t*)user_fifo) = (uint16_t)*read_addr++;
        user_fifo += 2U;
    }

    return bytes;
}

#ifdef USBD_LOWPWR_MODE_ENABLE

/*!
    \brief      restore system clocks and power while exiting suspend mode
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void lowpower_mode_exit (void)
{
    /* restore system clock */

    /* enable HXTAL */
    rcu_osci_on(RCU_HXTAL);

    /* wait till HXTAL is ready */
    while(RESET == rcu_flag_get(RCU_FLAG_HXTALSTB));

    /* enable PLL */
    rcu_osci_on(RCU_PLL_CK);

    /* wait till PLL is ready */
    while(RESET == rcu_flag_get(RCU_FLAG_PLLSTB));

    /* select PLL as system clock source */
    rcu_system_clock_source_config(RCU_CKSYSSRC_PLL);

    /* wait till PLL is used as system clock source */
    while(0x08U != rcu_system_clock_source_get());

    /* low power sleep on exit disabled */
    system_lowpower_reset(SCB_LPM_DEEPSLEEP);

#ifdef USE_IRC48M
    /* enable IRC48M clock */
    rcu_osci_on(RCU_IRC48M);

    /* wait till IRC48M is ready */
    while (SUCCESS != rcu_osci_stab_wait(RCU_IRC48M)) {
    }

    rcu_ck48m_clock_config(RCU_CK48MSRC_IRC48M);
#endif
}

#endif /* USBD_LOWPWR_MODE_ENABLE */

/*!
    \brief      resume the USB device
    \param[in]  none
    \param[out] none
    \retval     none
*/
void usbd_resume (void)
{
#ifdef LPM_ENABLED
    USBD_CTL |= CTL_L1RSREQ;
#else
    /* make USB resume */
    USBD_CTL |= CTL_RSREQ;
#endif /* LPM_ENABLED */
}

/*!
    \brief      set USB device to leave mode
    \param[in]  none
    \param[out] none
    \retval     none
*/
void usbd_leave_suspend (void)
{
    /* clear low_power mode bit in USBD_CTL */
   USBD_CTL &= ~CTL_LOWM;

#ifdef USBD_LOWPWR_MODE_ENABLE

    /* restore normal operations */
    lowpower_mode_exit();

#endif /* USBD_LOWPWR_MODE_ENABLE */

    /* clear SETSPS bit */
    USBD_CTL &= ~CTL_SETSPS;
}

/*!
    \brief      set USB device to enter suspend mode
    \param[in]  none
    \param[out] none
    \retval     none
*/
void usbd_suspend (void)
{
    /* set USB module to suspend and low-power mode */
    USBD_CTL |= CTL_SETSPS | CTL_LOWM;

#ifdef USBD_LOWPWR_MODE_ENABLE

    /* check wakeup flag is set */
    if (0U == (USBD_INTF & INTF_WKUPIF)) {
        /* enter DEEP_SLEEP mode with LDO in low power mode */
        pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
    } else {
        /* clear wakeup interrupt flag */
        CLR(WKUPIF);

        /* clear set_suspend flag */
        USBD_CTL &= ~CTL_SETSPS;
    }

#endif /* USBD_LOWPWR_MODE_ENABLE */
}
