flatUSB/class/acm.h
Epagris 2a69c57225 - basic docs added
- doxygen styling added
- CDC -> ACM refactor
- type refactoring
- examples categorized
- flatUSB_config.h examples added
2024-11-16 21:53:01 +01:00

130 lines
4.6 KiB
C

/**
******************************************************************************
* @file cdc.h
* @brief This is the USB Communication Device Class, Abstract Control Model
* implementation module for the flatUSB project. The following standard
* requestes are implemented: `SetLineCoding`, `GetLineCoding`,
* `SetControlLineState` and `SendBreak`. Data transmission is performed by
* calling `usb_acm_write()`. Ingress data is passed to the application through
* the `usb_acm_read_callback()` function. A weak prototype is provided which
* is expected to be overridden by the application. Supplying a BlockingFifo
* is mandatory!
*
* If necessary, `USB_CDC_FIFO_MEM_SIZE` and `USB_CDC_PCKT_BUFSIZE` can be freely
* overridden in the flatUSB configuration file.
*
* For more information about the USB CDC ACM class refer to the
* <a href="https://www.usb.org/document-library/class-definitions-communication-devices-12" target="_blank">
* USB CDC PSTN subclass</a>.
*
* @copyright András Wiesner, 2024-\showdate "%Y"
******************************************************************************
*/
#ifndef CORE_USB_CLASS_CDC
#define CORE_USB_CLASS_CDC
#include "../usb_callback_event.h"
/**
* CDC request codes
*/
typedef enum {
USB_ACM_SET_COMM_FEATURE = 0x2, ///< SetCommFeature
USB_ACM_GET_COMM_FEATURE = 0x3, ///< GetCommFeature
USB_ACM_CLEAR_COMM_FEATURE = 0x4, ///< ClearCommFeature
USB_ACM_SET_AUX_LINE_STATE = 0x10, ///< SetAuxLineState
USB_ACM_SET_HOOK_STATE = 0x11, ///< SetHookState
USB_ACM_PULSE_SETUP = 0x12, ///< PulseSetup
USB_ACM_SEND_PULSE = 0x13, ///< SendPulse
USB_ACM_SET_PULSE_TIME = 0x14, ///< SetPulseTime
USB_ACM_RING_AUX_JACK = 0x15, ///< RingAuxJack
USB_ACM_SET_LINE_CODING = 0x20, ///< SetLineCoding
USB_ACM_GET_LINE_CODING = 0x21, ///< GetLineCoding
USB_ACM_SET_CONTROL_LINE_STATE = 0x22, ///< SetControlLineState
USB_ACM_SEND_BREAK = 0x23, ///< SendBreak
USB_ACM_SET_RINGER_PARMS = 0x30, ///< SetRingerParms
USB_ACM_GET_RINGER_PARMS = 0x31, ///< GetRingerParms
USB_ACM_SET_OPERATION_PARMS = 0x32, ///< SetOperationParms
USB_ACM_GET_OPERATION_PARMS = 0x33, ///< GetOperationParms
USB_ACM_SET_LINE_PARMS = 0x34, ///< GetLineParms
} Usb_Acm_RequestCodes;
/**
* ACM line coding structure
*/
typedef struct {
uint32_t dwDTERate; ///< data terminal rate, bits per second
uint8_t bCharFormat; ///< character format
uint8_t bParityType; ///< parity type
uint8_t bDataBits; ///< data bits
} Usb_Acm_LineCodingStruct;
/**
* CDC control line state struct
*/
typedef struct {
uint16_t D; ///< settings word
} Usb_Acm_ControlLineStateStruct;
// ----------------
/**
* Endpoint assignments
*/
typedef struct {
uint8_t control_ep : 4; ///< Control endpoint
uint8_t data_ep : 4; ///< Data endpoint
} Usb_Acm_EpAssignments;
typedef struct {
Usb_Acm_EpAssignments ep_assignments; ///< Endpoint assignments
Usb_Acm_LineCodingStruct line_coding; ///< Line Coding
Usb_Acm_ControlLineStateStruct control_line_state; ///< Control Line State
uint16_t interrupt_data; ///< Data sent though the next transfer on the notification element
bool interrupt_pending; ///< Interrupt data is valid and should be send in the next cycle
bool moduleInit; ///< CDC module is initialized
bool commInit; ///< Communication protocol is initialized
} Usb_AcmState;
// ----------------
#define USB_ACM_INVALID_CONTROL_LINE_STATE (0xFFFF) ///< Invalid Control Line State
// ----------------
#ifndef USB_ACM_FIFO_MEM_SIZE
#define USB_ACM_FIFO_MEM_SIZE (3072) ///< CDC ACM FIFO memory size
#endif
#ifndef USB_ACM_PCKT_BUFSIZE
#define USB_ACM_PCKT_BUFSIZE (128) ///< Buffer size assigned to the data transfer endpoint
#endif
// ----------------
/**
* Initialize CDC ACM module.
*
* @param as pointer to filled endpoint assignments object
*/
void usb_acm_init(const Usb_Acm_EpAssignments *as);
/**
* Regular USB class process and return function.
*
* @param cbevt pointer to callback event emitted by the USB core
* @return request is unprocessed if < 0
*/
int usb_acm_process_and_return(Usb_CallbackEvent *cbevt);
/**
* Write to CDC ACM interface.
*
* @param data data to be sent through the interface
* @param size length of the data
*/
void usb_acm_write(const uint8_t *data, uint32_t size);
#endif /* CORE_USB_CLASS_CDC */