summaryrefslogtreecommitdiff
path: root/tmk_core/protocol/arm_atsam/usb/usb.h
blob: 9a452881a76e4a9ae74167e25431484be45bba6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/**
 * \file
 *
 * \brief SAM USB Driver
 *
 * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * 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. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL 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.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */
#ifndef USB_H_INCLUDED
#define USB_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \defgroup asfdoc_sam0_usb_group SAM Universal Serial Bus (USB)
 *
 * The Universal Serial Bus (USB) module complies with the USB 2.1 specification.
 *
 * The following peripherals are used by this module:
 *  - USB (Universal Serial Bus)
 *
 * The following devices can use this module:
 *  - Atmel | SMART SAM D51
 *
 * The USB module covers following mode:
 * \if USB_DEVICE_MODE
 *  - USB Device Mode
 * \endif
 * \if USB_HOST_MODE
 *  - USB Host Mode
 * \endif
 *
 * The USB module covers following speed:
 * \if USB_HS_MODE
 *  - USB High Speed (480Mbit/s)
 * \endif
 *  - USB Full Speed (12Mbit/s)
 * \if USB_LS_MODE
 *  - USB Low Speed (1.5Mbit/s)
 * \endif
 *
 * \if USB_LPM_MODE
 * The USB module supports Link Power Management (LPM-L1) protocol.
 * \endif
 *
 * USB support needs whole set of enumeration process, to make the device
 * recognizable and usable. The USB driver is designed to interface to the
 * USB Stack in Atmel Software Framework (ASF).
 *
 * \if USB_DEVICE_MODE
 * \section asfdoc_sam0_usb_device USB Device Mode
 * The ASF USB Device Stack has defined the USB Device Driver (UDD) interface,
 * to support USB device operations. The USB module device driver complies with
 * this interface, so that the USB Device Stack can work based on the
 * USB module.
 *
 * Refer to <a href="http://www.atmel.com/images/doc8360.pdf">
 * "ASF - USB Device Stack"</a> for more details.
 * \endif
 *
 * \if USB_HOST_MODE
 * \section adfdoc_sam0_usb_host USB Host Mode
 * The ASF USB Host Stack has defined the USB Host Driver (UHD) interface,
 * to support USB host operations. The USB module host driver complies with
 * this interface, so that the USB Host Stack can work based on the USB module.
 *
 * Refer to <a href="http://www.atmel.com/images/doc8486.pdf">
 * "ASF - USB Host Stack"</a> for more details.
 * \endif
 */

/** Enum for the speed status for the USB module */
enum usb_speed {
    USB_SPEED_LOW,
    USB_SPEED_FULL,
};

/** Enum for the possible callback types for the USB in host module */
enum usb_host_callback {
    USB_HOST_CALLBACK_SOF,
    USB_HOST_CALLBACK_RESET,
    USB_HOST_CALLBACK_WAKEUP,
    USB_HOST_CALLBACK_DNRSM,
    USB_HOST_CALLBACK_UPRSM,
    USB_HOST_CALLBACK_RAMACER,
    USB_HOST_CALLBACK_CONNECT,
    USB_HOST_CALLBACK_DISCONNECT,
    USB_HOST_CALLBACK_N,
};

/** Enum for the possible callback types for the USB pipe in host module */
enum usb_host_pipe_callback {
    USB_HOST_PIPE_CALLBACK_TRANSFER_COMPLETE,
    USB_HOST_PIPE_CALLBACK_ERROR,
    USB_HOST_PIPE_CALLBACK_SETUP,
    USB_HOST_PIPE_CALLBACK_STALL,
    USB_HOST_PIPE_CALLBACK_N,
};

/**
 * \brief Host pipe types.
 */
enum usb_host_pipe_type {
    USB_HOST_PIPE_TYPE_DISABLE,
    USB_HOST_PIPE_TYPE_CONTROL,
    USB_HOST_PIPE_TYPE_ISO,
    USB_HOST_PIPE_TYPE_BULK,
    USB_HOST_PIPE_TYPE_INTERRUPT,
    USB_HOST_PIPE_TYPE_EXTENDED,
};

/**
 * \brief Host pipe token types.
 */
enum usb_host_pipe_token {
    USB_HOST_PIPE_TOKEN_SETUP,
    USB_HOST_PIPE_TOKEN_IN,
    USB_HOST_PIPE_TOKEN_OUT,
};

/**
 * \brief Enumeration for the possible callback types for the USB in device module
 */
enum usb_device_callback {
    USB_DEVICE_CALLBACK_SOF,
    USB_DEVICE_CALLBACK_RESET,
    USB_DEVICE_CALLBACK_WAKEUP,
    USB_DEVICE_CALLBACK_RAMACER,
    USB_DEVICE_CALLBACK_SUSPEND,
    USB_DEVICE_CALLBACK_LPMNYET,
    USB_DEVICE_CALLBACK_LPMSUSP,
    USB_DEVICE_CALLBACK_N,
};

/**
 * \brief Enumeration for the possible callback types for the USB endpoint in device module
 */
enum usb_device_endpoint_callback {
    USB_DEVICE_ENDPOINT_CALLBACK_TRCPT,
    USB_DEVICE_ENDPOINT_CALLBACK_TRFAIL,
    USB_DEVICE_ENDPOINT_CALLBACK_RXSTP,
    USB_DEVICE_ENDPOINT_CALLBACK_STALL,
    USB_DEVICE_EP_CALLBACK_N,
};

/**
 * \brief Device Endpoint types.
 */
enum usb_device_endpoint_type {
    USB_DEVICE_ENDPOINT_TYPE_DISABLE,
    USB_DEVICE_ENDPOINT_TYPE_CONTROL,
    USB_DEVICE_ENDPOINT_TYPE_ISOCHRONOUS,
    USB_DEVICE_ENDPOINT_TYPE_BULK,
    USB_DEVICE_ENDPOINT_TYPE_INTERRUPT,
};

/**
 * \brief Endpoint Size
 */
enum usb_endpoint_size {
    USB_ENDPOINT_8_BYTE,
    USB_ENDPOINT_16_BYTE,
    USB_ENDPOINT_32_BYTE,
    USB_ENDPOINT_64_BYTE,
    USB_ENDPOINT_128_BYTE,
    USB_ENDPOINT_256_BYTE,
    USB_ENDPOINT_512_BYTE,
    USB_ENDPOINT_1023_BYTE,
};

/**
 * \brief Link Power Management Handshake.
 */
enum usb_device_lpm_mode {
    USB_DEVICE_LPM_NOT_SUPPORT,
    USB_DEVICE_LPM_ACK,
    USB_DEVICE_LPM_NYET,
};

/**
 * \brief Module structure
 */
struct usb_module;

/**
 * \name Host Callback Functions Types
 * @{
 */
typedef void (*usb_host_callback_t)(struct usb_module *module_inst);
typedef void (*usb_host_pipe_callback_t)(struct usb_module *module_inst, void *);
/** @} */

/**
 * \name Device Callback Functions Types
 * @{
 */
typedef void (*usb_device_callback_t)(struct usb_module *module_inst, void* pointer);
typedef void (*usb_device_endpoint_callback_t)(struct usb_module *module_inst, void* pointer);
/** @} */

/** USB configurations */
struct usb_config {
    /** \c true for host, \c false for device. */
    bool select_host_mode;
    /** When \c true the module is enabled during standby. */
    bool run_in_standby;
    /** Generic Clock Generator source channel. */
    // enum gclk_generator source_generator;
    uint8_t source_generator;
    /** Speed mode */
    //enum usb_speed speed_mode;
    uint8_t speed_mode;
};

/**
 * \brief USB software module instance structure.
 *
 * USB software module instance structure, used to retain software state
 * information of an associated hardware module instance.
 *
 */
struct usb_module {
    /** Hardware module pointer of the associated USB peripheral. */
    Usb *hw;

    /** Array to store device related callback functions */
    usb_device_callback_t device_callback[USB_DEVICE_CALLBACK_N];
    usb_device_endpoint_callback_t device_endpoint_callback[USB_EPT_NUM][USB_DEVICE_EP_CALLBACK_N];
    /** Bit mask for device callbacks registered */
    uint16_t device_registered_callback_mask;
    /** Bit mask for device callbacks enabled */
    uint16_t device_enabled_callback_mask;
    /** Bit mask for device endpoint callbacks registered */
    uint8_t device_endpoint_registered_callback_mask[USB_EPT_NUM];
    /** Bit mask for device endpoint callbacks enabled */
    uint8_t device_endpoint_enabled_callback_mask[USB_EPT_NUM];
};

/** USB device endpoint configurations */
struct usb_device_endpoint_config {
    /** device address */
    uint8_t ep_address;
    /** endpoint size */
    enum usb_endpoint_size ep_size;
    /** automatic zero length packet mode, \c true to enable */
    bool auto_zlp;
    /** type of endpoint with Bank */
    enum usb_device_endpoint_type ep_type;
};

/** USB device endpoint callback status parameter structure */
struct usb_endpoint_callback_parameter {
    uint16_t received_bytes;
    uint16_t sent_bytes;
    uint16_t out_buffer_size;
    uint8_t endpoint_address;
};

void usb_enable(struct usb_module *module_inst);
void usb_disable(struct usb_module *module_inst);

/**
 * \brief Get the status of USB module's state machine
 *
 * \param module_inst Pointer to USB module instance
 */
static inline uint8_t usb_get_state_machine_status(struct usb_module *module_inst)
{
    /* Sanity check arguments */
    Assert(module_inst);
    Assert(module_inst->hw);

    return module_inst->hw->DEVICE.FSMSTATUS.reg;
}

void usb_get_config_defaults(struct usb_config *module_config);
enum status_code usb_init(struct usb_module *module_inst, Usb *const hw,
        struct usb_config *module_config);

/**
 * \brief Attach USB device to the bus
 *
 * \param module_inst Pointer to USB device module instance
 */
static inline void usb_device_attach(struct usb_module *module_inst)
{
    module_inst->hw->DEVICE.CTRLB.reg &= ~USB_DEVICE_CTRLB_DETACH;
}

/**
 * \brief Detach USB device from the bus
 *
 * \param module_inst Pointer to USB device module instance
 */
static inline void usb_device_detach(struct usb_module *module_inst)
{
    module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_DETACH;
}

/**
 * \brief Get the speed mode of USB device
 *
 * \param module_inst Pointer to USB device module instance
 * \return USB Speed mode (\ref usb_speed).
 */
static inline enum usb_speed usb_device_get_speed(struct usb_module *module_inst)
{
    if (!(module_inst->hw->DEVICE.STATUS.reg & USB_DEVICE_STATUS_SPEED_Msk)) {
        return USB_SPEED_FULL;
    } else {
        return USB_SPEED_LOW;
    }
}

/**
 * \brief Get the address of USB device
 *
 * \param module_inst Pointer to USB device module instance
 * \return USB device address value.
 */
static inline uint8_t usb_device_get_address(struct usb_module *module_inst)
{
    return ((uint8_t)(module_inst->hw->DEVICE.DADD.bit.DADD));
}

/**
 * \brief Set the speed mode of USB device
 *
 * \param module_inst Pointer to USB device module instance
 * \param address     USB device address value
 */
static inline void usb_device_set_address(struct usb_module *module_inst, uint8_t address)
{
    module_inst->hw->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | address;
}

/**
 * \brief Get the frame number of USB device
 *
 * \param module_inst Pointer to USB device module instance
 * \return USB device frame number value.
 */
static inline uint16_t usb_device_get_frame_number(struct usb_module *module_inst)
{
    return ((uint16_t)(module_inst->hw->DEVICE.FNUM.bit.FNUM));
}

/**
 * \brief Get the micro-frame number of USB device
 *
 * \param module_inst Pointer to USB device module instance
 * \return USB device micro-frame number value.
 */
static inline uint16_t usb_device_get_micro_frame_number(struct usb_module *module_inst)
{
    return ((uint16_t)(module_inst->hw->DEVICE.FNUM.reg));
}

/**
 * \brief USB device send the resume wakeup
 *
 * \param module_inst Pointer to USB device module instance
 */
static inline void usb_device_send_remote_wake_up(struct usb_module *module_inst)
{
    module_inst->hw->DEVICE.CTRLB.reg |= USB_DEVICE_CTRLB_UPRSM;
}

/**
 * \brief USB device set the LPM mode
 *
 * \param module_inst Pointer to USB device module instance
 * \param lpm_mode    LPM mode
 */
static inline void usb_device_set_lpm_mode(struct usb_module *module_inst,
        enum usb_device_lpm_mode lpm_mode)
{
    module_inst->hw->DEVICE.CTRLB.bit.LPMHDSK = lpm_mode;
}

/**
 * \name USB Device Callback Management
 * @{
 */
enum status_code usb_device_register_callback(struct usb_module *module_inst,
        enum usb_device_callback callback_type,
        usb_device_callback_t callback_func);
enum status_code usb_device_unregister_callback(struct usb_module *module_inst,
        enum usb_device_callback callback_type);
enum status_code usb_device_enable_callback(struct usb_module *module_inst,
        enum usb_device_callback callback_type);
enum status_code usb_device_disable_callback(struct usb_module *module_inst,
        enum usb_device_callback callback_type);
/** @} */

/**
 * \name USB Device Endpoint Configuration
 * @{
 */
void usb_device_endpoint_get_config_defaults(struct usb_device_endpoint_config *ep_config);
enum status_code usb_device_endpoint_set_config(struct usb_module *module_inst,
        struct usb_device_endpoint_config *ep_config);
bool usb_device_endpoint_is_configured(struct usb_module *module_inst, uint8_t ep);
/** @} */

/**
 * \name USB Device Endpoint Callback Management
 * @{
 */
enum status_code usb_device_endpoint_register_callback(
        struct usb_module *module_inst, uint8_t ep_num,
        enum usb_device_endpoint_callback callback_type,
        usb_device_endpoint_callback_t callback_func);
enum status_code usb_device_endpoint_unregister_callback(
        struct usb_module *module_inst, uint8_t ep_num,
        enum usb_device_endpoint_callback callback_type);
enum status_code usb_device_endpoint_enable_callback(
        struct usb_module *module_inst, uint8_t ep,
        enum usb_device_endpoint_callback callback_type);
enum status_code usb_device_endpoint_disable_callback(
        struct usb_module *module_inst, uint8_t ep,
        enum usb_device_endpoint_callback callback_type);
/** @} */

/**
 * \name USB Device Endpoint Job Management
 * @{
 */
enum status_code usb_device_endpoint_write_buffer_job(struct usb_module *module_inst,uint8_t ep_num,
        uint8_t* pbuf, uint32_t buf_size);
enum status_code usb_device_endpoint_read_buffer_job(struct usb_module *module_inst,uint8_t ep_num,
        uint8_t* pbuf, uint32_t buf_size);
enum status_code usb_device_endpoint_setup_buffer_job(struct usb_module *module_inst,
        uint8_t* pbuf);
void usb_device_endpoint_abort_job(struct usb_module *module_inst, uint8_t ep);
/** @} */

/**
 * \name USB Device Endpoint Operations
 * @{
 */

bool usb_device_endpoint_is_halted(struct usb_module *module_inst, uint8_t ep);
void usb_device_endpoint_set_halt(struct usb_module *module_inst, uint8_t ep);
void usb_device_endpoint_clear_halt(struct usb_module *module_inst, uint8_t ep);

/** @} */

#ifdef __cplusplus
}
#endif

#endif /* USB_H_INCLUDED */