Skip to content

Commit d56ed57

Browse files
committed
Fix 1 month rollover bug -- Lei Shi found this one in one place, I found the problem all over the library and patched them all.
1 parent 0acad81 commit d56ed57

File tree

10 files changed

+154
-161
lines changed

10 files changed

+154
-161
lines changed

BTD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ uint8_t BTD::Release() {
371371
uint8_t BTD::Poll() {
372372
if(!bPollEnable)
373373
return 0;
374-
if(qNextPollTime <= millis()) { // Don't poll if shorter than polling interval
374+
if((long)(millis() - qNextPollTime) >= 0L) { // Don't poll if shorter than polling interval
375375
qNextPollTime = millis() + pollInterval; // Set new poll time
376376
HCI_event_task(); // Poll the HCI event pipe
377377
HCI_task(); // HCI state machine

Usb.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
327327
regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ
328328
rcode = (regRd(rHRSL) & 0x0f);
329329

330-
while(rcode && (timeout > millis())) {
330+
while(rcode && ((long)(millis() - timeout) >= 0L)) {
331331
switch(rcode) {
332332
case hrNAK:
333333
nak_count++;
@@ -380,11 +380,11 @@ uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
380380
uint8_t retry_count = 0;
381381
uint16_t nak_count = 0;
382382

383-
while(timeout > millis()) {
383+
while((long)(millis() - timeout) >= 0L) {
384384
regWr(rHXFR, (token | ep)); //launch the transfer
385385
rcode = USB_ERROR_TRANSFER_TIMEOUT;
386386

387-
while(timeout > millis()) //wait for transfer completion
387+
while((long)(millis() - timeout) >= 0L) //wait for transfer completion
388388
{
389389
tmpdata = regRd(rHIRQ);
390390

@@ -475,7 +475,7 @@ void USB::Task(void) //USB state machine
475475
case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here
476476
break;
477477
case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device
478-
if(delay < millis())
478+
if((long)(millis() - delay) >= 0L)
479479
usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
480480
else break; // don't fall through
481481
case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
@@ -502,7 +502,7 @@ void USB::Task(void) //USB state machine
502502
}
503503
break;
504504
case USB_ATTACHED_SUBSTATE_WAIT_RESET:
505-
if(delay < millis()) usb_task_state = USB_STATE_CONFIGURING;
505+
if((long)(millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING;
506506
else break; // don't fall through
507507
case USB_STATE_CONFIGURING:
508508

Wii.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ void WII::L2CAP_task() {
657657
/* The next states are in run() */
658658

659659
case L2CAP_INTERRUPT_DISCONNECT:
660-
if(l2cap_check_flag(L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) && millis() > timer) {
660+
if(l2cap_check_flag(L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) && ((long)(millis() - timer) >= 0L)) {
661661
#ifdef DEBUG_USB_HOST
662662
Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80);
663663
#endif
@@ -682,7 +682,7 @@ void WII::L2CAP_task() {
682682
}
683683

684684
void WII::Run() {
685-
if(l2cap_state == L2CAP_INTERRUPT_DISCONNECT && millis() > timer)
685+
if(l2cap_state == L2CAP_INTERRUPT_DISCONNECT && ((long)(millis() - timer) >= 0L))
686686
L2CAP_task(); // Call the rest of the disconnection routine after we have waited long enough
687687

688688
switch(l2cap_state) {

examples/hub_demo/hub_demo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void loop()
9797

9898
if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
9999
{
100-
if (millis() >= next_time)
100+
if ((next_time - millis()) >= 0L)
101101
{
102102
Usb.ForEachUsbDevice(&PrintAllDescriptors);
103103
Usb.ForEachUsbDevice(&PrintAllAddresses);

examples/pl2303/pl2303_gps/pl2303_gps.ino

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,80 +10,75 @@
1010
#include <spi4teensy3.h>
1111
#endif
1212

13-
class PLAsyncOper : public CDCAsyncOper
14-
{
13+
class PLAsyncOper : public CDCAsyncOper {
1514
public:
16-
virtual uint8_t OnInit(ACM *pacm);
15+
virtual uint8_t OnInit(ACM *pacm);
1716
};
1817

19-
uint8_t PLAsyncOper::OnInit(ACM *pacm)
20-
{
21-
uint8_t rcode;
18+
uint8_t PLAsyncOper::OnInit(ACM *pacm) {
19+
uint8_t rcode;
2220

23-
// Set DTR = 1
24-
rcode = pacm->SetControlLineState(1);
21+
// Set DTR = 1
22+
rcode = pacm->SetControlLineState(1);
2523

26-
if (rcode)
27-
{
28-
ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
29-
return rcode;
30-
}
24+
if(rcode) {
25+
ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
26+
return rcode;
27+
}
3128

32-
LINE_CODING lc;
33-
lc.dwDTERate = 4800; //default serial speed of GPS unit
34-
lc.bCharFormat = 0;
35-
lc.bParityType = 0;
36-
lc.bDataBits = 8;
29+
LINE_CODING lc;
30+
lc.dwDTERate = 4800; //default serial speed of GPS unit
31+
lc.bCharFormat = 0;
32+
lc.bParityType = 0;
33+
lc.bDataBits = 8;
3734

38-
rcode = pacm->SetLineCoding(&lc);
35+
rcode = pacm->SetLineCoding(&lc);
3936

40-
if (rcode)
41-
ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
37+
if(rcode)
38+
ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
4239

43-
return rcode;
40+
return rcode;
4441
}
4542

46-
USB Usb;
47-
USBHub Hub(&Usb);
48-
PLAsyncOper AsyncOper;
49-
PL2303 Pl(&Usb, &AsyncOper);
43+
USB Usb;
44+
USBHub Hub(&Usb);
45+
PLAsyncOper AsyncOper;
46+
PL2303 Pl(&Usb, &AsyncOper);
5047
uint32_t read_delay;
5148
#define READ_DELAY 100
5249

53-
void setup()
54-
{
55-
Serial.begin( 115200 );
56-
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
57-
Serial.println("Start");
50+
void setup() {
51+
Serial.begin(115200);
52+
while(!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
53+
Serial.println("Start");
5854

59-
if (Usb.Init() == -1)
60-
Serial.println("OSCOKIRQ failed to assert");
55+
if(Usb.Init() == -1)
56+
Serial.println("OSCOKIRQ failed to assert");
6157

62-
delay( 200 );
58+
delay(200);
6359
}
6460

65-
void loop()
66-
{
67-
uint8_t rcode;
68-
uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint
69-
uint16_t rcvd = 64;
70-
71-
Usb.Task();
72-
73-
if( Pl.isReady()) {
74-
/* reading the GPS */
75-
if( read_delay < millis() ){
76-
read_delay += READ_DELAY;
77-
rcode = Pl.RcvData(&rcvd, buf);
78-
if ( rcode && rcode != hrNAK )
79-
ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
80-
if( rcvd ) { //more than zero bytes received
81-
for( uint16_t i=0; i < rcvd; i++ ) {
82-
Serial.print((char)buf[i]); //printing on the screen
83-
}//for( uint16_t i=0; i < rcvd; i++...
84-
}//if( rcvd
85-
}//if( read_delay > millis()...
86-
}//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
61+
void loop() {
62+
uint8_t rcode;
63+
uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint
64+
uint16_t rcvd = 64;
65+
66+
Usb.Task();
67+
68+
if(Pl.isReady()) {
69+
/* reading the GPS */
70+
if((long)(millis() - read_delay) >= 0L) {
71+
read_delay += READ_DELAY;
72+
rcode = Pl.RcvData(&rcvd, buf);
73+
if(rcode && rcode != hrNAK)
74+
ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
75+
if(rcvd) { //more than zero bytes received
76+
for(uint16_t i = 0; i < rcvd; i++) {
77+
Serial.print((char)buf[i]); //printing on the screen
78+
}//for( uint16_t i=0; i < rcvd; i++...
79+
}//if( rcvd
80+
}//if( read_delay > millis()...
81+
}//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
8782
}
8883

8984

0 commit comments

Comments
 (0)