]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/chibios/usb_driver.h
Helix-serial.c configuration improvement (#4370)
[qmk_firmware.git] / tmk_core / protocol / chibios / usb_driver.h
1 /*
2     ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8         http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16
17 /**
18  * @file    usb_driver.h
19  * @brief   Usb driver suitable for both packet and serial formats
20  *
21  * @addtogroup SERIAL_USB
22  * @{
23  */
24
25 #ifndef USB_DRIVER_H
26 #define USB_DRIVER_H
27
28 #include "hal_usb_cdc.h"
29
30 /*===========================================================================*/
31 /* Driver constants.                                                         */
32 /*===========================================================================*/
33
34 /*===========================================================================*/
35 /* Derived constants and error checks.                                       */
36 /*===========================================================================*/
37
38 #if HAL_USE_USB == FALSE
39 #error "The USB Driver requires HAL_USE_USB"
40 #endif
41
42 /*===========================================================================*/
43 /* Driver data structures and types.                                         */
44 /*===========================================================================*/
45
46 /**
47  * @brief Driver state machine possible states.
48  */
49 typedef enum {
50   QMKUSB_UNINIT = 0,                   /**< Not initialized.                   */
51   QMKUSB_STOP = 1,                     /**< Stopped.                           */
52   QMKUSB_READY = 2                     /**< Ready.                             */
53 } qmkusbstate_t;
54
55 /**
56  * @brief   Structure representing a serial over USB driver.
57  */
58 typedef struct QMKUSBDriver QMKUSBDriver;
59
60 /**
61  * @brief   Serial over USB Driver configuration structure.
62  * @details An instance of this structure must be passed to @p sduStart()
63  *          in order to configure and start the driver operations.
64  */
65 typedef struct {
66   /**
67    * @brief   USB driver to use.
68    */
69   USBDriver                 *usbp;
70   /**
71    * @brief   Bulk IN endpoint used for outgoing data transfer.
72    */
73   usbep_t                   bulk_in;
74   /**
75    * @brief   Bulk OUT endpoint used for incoming data transfer.
76    */
77   usbep_t                   bulk_out;
78   /**
79    * @brief   Interrupt IN endpoint used for notifications.
80    * @note    If set to zero then the INT endpoint is assumed to be not
81    *          present, USB descriptors must be changed accordingly.
82    */
83   usbep_t                   int_in;
84
85   /**
86    * @brief The number of buffers in the queues
87    */
88   size_t                    in_buffers;
89   size_t                    out_buffers;
90
91   /**
92    * @brief The size of each buffer in the queue, typically the same as the endpoint size
93    */
94   size_t                    in_size;
95   size_t                    out_size;
96
97   /**
98    * @brief Always send full buffers in_size (the rest is filled with zeroes)
99    */
100   bool                      fixed_size;
101
102   /* Input buffer
103    * @note needs to be initialized with a memory buffer of the right size
104    */
105   uint8_t*                  ib;
106   /* Output buffer
107    * @note needs to be initialized with a memory buffer of the right size
108    */
109   uint8_t*                  ob;
110 } QMKUSBConfig;
111
112 /**
113  * @brief   @p SerialDriver specific data.
114  */
115 #define _qmk_usb_driver_data                                                \
116   _base_asynchronous_channel_data                                           \
117   /* Driver state.*/                                                        \
118   qmkusbstate_t             state;                                          \
119   /* Input buffers queue.*/                                                 \
120   input_buffers_queue_t     ibqueue;                                        \
121   /* Output queue.*/                                                        \
122   output_buffers_queue_t    obqueue;                                        \
123   /* End of the mandatory fields.*/                                         \
124   /* Current configuration data.*/                                          \
125   const QMKUSBConfig     *config;
126
127 /**
128  * @brief   @p SerialUSBDriver specific methods.
129  */
130 #define _qmk_usb_driver_methods                                             \
131   _base_asynchronous_channel_methods
132
133 /**
134  * @extends BaseAsynchronousChannelVMT
135  *
136  * @brief   @p SerialDriver virtual methods table.
137  */
138 struct QMKUSBDriverVMT {
139   _qmk_usb_driver_methods
140 };
141
142 /**
143  * @extends BaseAsynchronousChannel
144  *
145  * @brief   Full duplex serial driver class.
146  * @details This class extends @p BaseAsynchronousChannel by adding physical
147  *          I/O queues.
148  */
149 struct QMKUSBDriver {
150   /** @brief Virtual Methods Table.*/
151   const struct QMKUSBDriverVMT *vmt;
152   _qmk_usb_driver_data
153 };
154
155 /*===========================================================================*/
156 /* Driver macros.                                                            */
157 /*===========================================================================*/
158
159 /*===========================================================================*/
160 /* External declarations.                                                    */
161 /*===========================================================================*/
162
163 #ifdef __cplusplus
164 extern "C" {
165 #endif
166   void qmkusbInit(void);
167   void qmkusbObjectInit(QMKUSBDriver *qmkusbp, const QMKUSBConfig * config);
168   void qmkusbStart(QMKUSBDriver *qmkusbp, const QMKUSBConfig *config);
169   void qmkusbStop(QMKUSBDriver *qmkusbp);
170   void qmkusbSuspendHookI(QMKUSBDriver *qmkusbp);
171   void qmkusbWakeupHookI(QMKUSBDriver *qmkusbp);
172   void qmkusbConfigureHookI(QMKUSBDriver *qmkusbp);
173   bool qmkusbRequestsHook(USBDriver *usbp);
174   void qmkusbSOFHookI(QMKUSBDriver *qmkusbp);
175   void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep);
176   void qmkusbDataReceived(USBDriver *usbp, usbep_t ep);
177   void qmkusbInterruptTransmitted(USBDriver *usbp, usbep_t ep);
178 #ifdef __cplusplus
179 }
180 #endif
181
182 #endif /* USB_DRIVER_H */
183
184 /** @} */