RISC-V/V203/usb/cdc/ch32v203/cdc_class.h

95 lines
3.6 KiB
C
Raw Normal View History

2024-10-11 10:39:42 +02:00
#ifndef CDC_CLASS_H
#define CDC_CLASS_H
#include "baselayer.h"
#include "gpio.h"
2024-10-12 15:33:16 +02:00
#include "fifo.h"
2024-10-11 10:39:42 +02:00
#include "system.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include "usb_desc.h"
#include "ch32v20x_usb.h"
/******************************************************************************/
/* Global Define */
/* end-point number */
#define DEF_UEP_IN 0x80
#define DEF_UEP_OUT 0x00
#define DEF_UEP0 0x00
#define DEF_UEP1 0x01
#define DEF_UEP2 0x02
#define DEF_UEP3 0x03
#define DEF_UEP4 0x04
#define DEF_UEP5 0x05
#define DEF_UEP6 0x06
#define DEF_UEP7 0x07
#define DEF_UEP_NUM 8
#define USBFSD_UEP_MOD_BASE 0x5000000C
#define USBFSD_UEP_DMA_BASE 0x50000010
#define USBFSD_UEP_LEN_BASE 0x50000030
#define USBFSD_UEP_CTL_BASE 0x50000032
#define USBFSD_UEP_RX_EN 0x08
#define USBFSD_UEP_TX_EN 0x04
#define USBFSD_UEP_BUF_MOD 0x01
#define DEF_UEP_DMA_LOAD 0 /* Direct the DMA address to the data to be processed */
#define DEF_UEP_CPY_LOAD 1 /* Use memcpy to move data to a buffer */
#define USBFSD_UEP_MOD(n) (*((volatile uint8_t *)(USBFSD_UEP_MOD_BASE+n)))
#define USBFSD_UEP_CTRL(n) (*((volatile uint8_t *)(USBFSD_UEP_CTL_BASE+n*0x04)))
#define USBFSD_UEP_DMA(n) (*((volatile uint32_t *)(USBFSD_UEP_DMA_BASE+n*0x04)))
#define USBFSD_UEP_BUF(n) ((uint8_t *)(*((volatile uint32_t *)(USBFSD_UEP_DMA_BASE+n*0x04)))+0x20000000)
#define USBFSD_UEP_TLEN(n) (*((volatile uint16_t *)(USBFSD_UEP_LEN_BASE+n*0x04)))
/* Setup Request Packets */
#define pUSBFS_SetupReqPak ((PUSB_SETUP_REQ)USBFS_EP0_Buf)
#ifdef __cplusplus
};
#endif
2024-10-11 11:39:44 +02:00
/** @class cdc_class
* V postatě je to převzato z originálního balíku od wch.nc, ale ten
* je napsán dost "špagetově", interface je nesrozumitelné, takže je tam dost věcí
2024-10-12 15:33:16 +02:00
* přidáno odhadem.
2024-10-11 10:39:42 +02:00
*/
class cdc_class : public BaseLayer {
GpioClass dtr;
2024-10-12 15:33:16 +02:00
FIFO<char, 128> TxRing;
bool Ready;
2024-10-11 10:39:42 +02:00
const uint8_t * pUSBFS_Descr;
/* Setup Request */
volatile uint8_t USBFS_SetupReqCode;
volatile uint8_t USBFS_SetupReqType;
volatile uint16_t USBFS_SetupReqValue;
volatile uint16_t USBFS_SetupReqIndex;
volatile uint16_t USBFS_SetupReqLen;
/* USB Device Status */
volatile uint8_t USBFS_DevConfig;
volatile uint8_t USBFS_DevAddr;
volatile uint8_t USBFS_DevSleepStatus;
volatile uint8_t USBFS_DevEnumStatus;
/* Endpoint Buffer */
__attribute__ ((aligned(4))) uint8_t USBFS_EP0_Buf[ DEF_USBD_UEP0_SIZE ];
__attribute__ ((aligned(4))) uint8_t USBFS_EP1_Buf[ DEF_USBD_ENDP1_SIZE ];
__attribute__ ((aligned(4))) uint8_t USBFS_EP2_Buf[ DEF_USBD_ENDP2_SIZE ];
__attribute__ ((aligned(4))) uint8_t USBFS_EP3_Buf[ DEF_USBD_ENDP3_SIZE ];
/* USB IN Endpoint Busy Flag */
volatile uint8_t USBFS_Endp_Busy[ DEF_UEP_NUM ];
public:
explicit cdc_class () noexcept;
void init ();
void cdc_irq ();
2024-10-12 15:33:16 +02:00
/// Předěláno na neblokující, data se vyměňují přes FIFO, rychlejší
2024-10-11 10:39:42 +02:00
uint32_t Down(const char * data, const uint32_t len) override;
protected:
void USBFS_Device_Init( bool sta );
void USBFS_Device_Endp_Init ();
2024-10-12 15:33:16 +02:00
void TxDataDeal ();
2024-10-12 13:14:24 +02:00
void TransferHandler ();
void InTokenHandler (const uint8_t intst);
void OutTokenHandler (const uint8_t intst);
void SetupTokenHandler ();
2024-10-11 10:39:42 +02:00
};
#endif // CDC_CLASS_H