-
Notifications
You must be signed in to change notification settings - Fork 12
/
USBPause.h
69 lines (55 loc) · 1.86 KB
/
USBPause.h
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
// Copyright Pololu Corporation. For more information, see https://www.pololu.com/
/*! \file USBPause.h
*
* This is the main file for the USBPause library.
*
* For an overview of this library, see
* https://github.com/pololu/usb-pause-arduino. That is the main repository for
* this library, though copies may exist in other repositories. */
#pragma once
#include <avr/io.h>
/*! This class disables USB interrupts in its constructor when it is created and
* restores them to their previous state in its destructor when it is
* destroyed. This class is tailored to the behavior of the Arduino core USB
* code, so it might have to change if that code changes.
*
* This class assumes that the only USB interrupts enabled are general device
* interrupts and endpoint 0 interrupts.
*
* It also assumes that the endpoint 0 interrupts will not enable or disable any
* of the general device interrupts.
*/
class USBPause
{
/// The saved value of the UDIEN register.
uint8_t savedUDIEN;
/// The saved value of the UENUM register.
uint8_t savedUENUM;
/// The saved value of the UEIENX register for endpoint 0.
uint8_t savedUEIENX0;
public:
USBPause()
{
// Disable the general USB interrupt. This must be done
// first, because the general USB interrupt might change the
// state of the EP0 interrupt, but not the other way around.
savedUDIEN = UDIEN;
UDIEN = 0;
// Select endpoint 0.
savedUENUM = UENUM;
UENUM = 0;
// Disable endpoint 0 interrupts.
savedUEIENX0 = UEIENX;
UEIENX = 0;
}
~USBPause()
{
// Restore endpoint 0 interrupts.
UENUM = 0;
UEIENX = savedUEIENX0;
// Restore endpoint selection.
UENUM = savedUENUM;
// Restore general device interrupt.
UDIEN = savedUDIEN;
}
};