/*
* Copyright (c) 2023-2028, SpaceTouch Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date          Author      Notes
* 2023-07-18    zhixiongl       the first version
*/


#ifndef SPV_HAL_USBD_H
#define SPV_HAL_USBD_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "platform.h"

//#define DBG_HAL_USBD

#if defined(DBG_HAL_USBD)
#define HAL_USBD_LOG(fmt, ...)      rt_kprintf(fmt, ##__VA_ARGS__)
#else
#define HAL_USBD_LOG(fmt, ...)
#endif


#define HAL_USBD_EOK                          0               /**< There is no error */
#define HAL_USBD_ERROR                        1               /**< A generic error happens */


#define USB_HS_ENABLE
//#define USB_FS_ENABLE
typedef enum
{
    USB_FS,
    USB_HS
} USB_SPEED_SELECT;

/* Define maximum packet size for endpoint 0 */
#define M_EP0_MAXP  64
extern uint8_t usb_setup_data[M_EP0_MAXP];


#define TYPE_ISO     0
#define TYPE_BULK    1
#define TYPE_INT     2


void hal_usbd_set_stall(uint8_t ep_addr);
void hal_usbd_clear_stall(uint8_t ep_addr);
void hal_usbd_set_address(uint8_t ep_addr);
void hal_usbd_activate_endpoint(uint8_t ep_addr);
void hal_usbd_deactivate_endpoint(uint8_t ep_addr);
uint16_t hal_usbd_receive_count(uint8_t ep_addr);
uint16_t hal_usbd_receive(uint8_t ep_addr, uint8_t *pBuf);
void hal_usbd_send(uint8_t ep_addr, uint8_t *pBuf, uint32_t len);

void USBReset(void);
void FIFORead(int nEP, int nBytes, void *pDst);
void FIFOWrite(int nEP, int nBytes, void *pSrc);
void usbhsfc_softdiscon(void);
void usbhsfc_softcon(void);
void endpoint_IN_init(uint8_t address, uint8_t type);
void endpoint_OUT_init(uint8_t address, uint8_t type);

#define USB_EP_NUM      7


typedef void (*hal_usbd_ep_cb)(void *param, uint8_t ep_addr);
struct hal_usbd_ep_cb_struct
{
    hal_usbd_ep_cb ep_cb[USB_EP_NUM];    //用于ep中断回调函数
    void *param[USB_EP_NUM];
};
void hal_usbd_cb_register(uint8_t ep_addr, hal_usbd_ep_cb cpcb, void *param);
void hal_usbd_cb_unregister(uint8_t ep_addr);
void hal_usbd_ep_function_call(uint8_t ep_addr);

#ifdef __cplusplus
}
#endif

#endif

