/*!
    \file  usbd_lld_core.h
    \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.
*/

#ifndef __USBD_LLD_CORE_H
#define __USBD_LLD_CORE_H

#include "usbd_regs.h"

#define USBD_EP0_MAX_SIZE    64U

#define EP_IN(x)              ((uint8_t)(0x80U | (x)))
#define EP_OUT(x)             ((uint8_t)(x))

#define EP_DIR(x)            ((uint8_t)((x) >> 7U))
#define EP_ID(x)             ((uint8_t)((x) & 0x7FU))

#define BYTE_LOW(x)          ((uint8_t)((x) & 0x00FFU))
#define BYTE_HIGH(x)         ((uint8_t)(((x) & 0xFF00U) >> 8U))

/* double buffer endpoint direction */
enum dbuf_ep_dir {
    DBUF_EP_IN,               /*!< double buffer IN direction */
    DBUF_EP_OUT,              /*!< double buffer OUT direction */
    DBUF_EP_ERR,              /*!< double buffer error direction */
};

typedef struct {
    __IO uint32_t tx_addr;    /*!< transmission address */
    __IO uint32_t tx_count;   /*!< transmission count */
    __IO uint32_t rx_addr;    /*!< reception address */
    __IO uint32_t rx_count;   /*!< reception count */
} usbd_ep_ram;

/* USB device endpoint kind */
enum usbd_ep_kind {
    EP_BUF_SNG = 0U,          /*!< single buffer endpoint type value */
    EP_BUF_DBL                /*!< double buffer endpoint type value */
};

/* USB core driver struct */
typedef struct _usb_dev
{
    /* basic parameters */
    uint8_t         config;
    uint8_t         dev_addr;

    uint8_t         setup_packet[8];
    uint8_t         *xfer_buf;

    uint16_t         xfer_count;
}usb_dev;

/* function declarations */

/* device core register initialization */
void usbd_core_reset (void);

/* device core register configure when stop device */
void usbd_core_stop (void);

/* set device address */
void usbd_set_address (uint8_t dev_addr);

/* free buffer used from application by toggling the SW_BUF byte */
void user_buffer_free (uint8_t ep_num, uint8_t dir);

/* handle USB reset event */
void usbd_ep_reset (void);

/* endpoint initialization */
void usbd_ep_setup (uint8_t buf_kind, uint16_t buf_addr, void *ep_desc);

/* enable endpoint to receive */
void usbd_ep_rx_enable (uint8_t ep_addr, uint8_t* buffer);

/* configure the endpoint when it is disabled */
void usbd_ep_disable (uint8_t ep_addr);

/* set an endpoint to STALL status */
void usbd_ep_stall_set (uint8_t ep_addr);

/* clear endpoint stalled status */
void usbd_ep_stall_clear (uint8_t ep_addr);

/* write data from user FIFO to USBRAM */
void usbd_ep_data_write (uint8_t *fifo, uint8_t ep_num, uint16_t bytes);

/* read data from USBRAM to user FIFO */
uint16_t usbd_ep_data_read (uint8_t *fifo, uint8_t ep_num, uint8_t buf_kind);

/* get the endpoint status */
uint8_t usbd_ep_status_get (uint8_t ep_addr);

/* resume the USB device */
void usbd_resume (void);

/* set USB device to enter suspend mode */
void usbd_suspend (void);

/* set USB device to leave suspend mode */
void usbd_leave_suspend (void);

#endif /* __USBD_LLD_CORE_H */
