Fix millis() and micros() rollover bug

Also replace long with int32_t, so it is not architecture dependent
This commit is contained in:
Kristian Sloth Lauszus 2017-02-12 15:10:07 +01:00
parent 07de430af0
commit 6fb48f48e4
20 changed files with 38 additions and 49 deletions

View file

@ -384,7 +384,7 @@ uint8_t BTD::Release() {
uint8_t BTD::Poll() {
if(!bPollEnable)
return 0;
if((long)(millis() - qNextPollTime) >= 0L) { // Don't poll if shorter than polling interval
if((int32_t)(millis() - qNextPollTime) >= 0L) { // Don't poll if shorter than polling interval
qNextPollTime = millis() + pollInterval; // Set new poll time
HCI_event_task(); // Poll the HCI event pipe
HCI_task(); // HCI state machine

View file

@ -454,7 +454,7 @@ void PS3BT::L2CAP_task() {
void PS3BT::Run() {
switch(l2cap_state) {
case PS3_ENABLE_SIXAXIS:
if(millis() - timer > 1000) { // loop 1 second before sending the command
if((int32_t)(millis() - timer) > 1000) { // loop 1 second before sending the command
memset(l2capinbuf, 0, BULK_MAXPKTSIZE); // Reset l2cap in buffer as it sometimes read it as a button has been pressed
for(uint8_t i = 15; i < 19; i++)
l2capinbuf[i] = 0x7F; // Set the analog joystick values to center position
@ -465,7 +465,7 @@ void PS3BT::Run() {
break;
case TURN_ON_LED:
if(millis() - timer > 1000) { // loop 1 second before sending the command
if((int32_t)(millis() - timer) > 1000) { // loop 1 second before sending the command
if(remote_name_first == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P')
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nDualshock 3 Controller Enabled\r\n"), 0x80);
@ -494,7 +494,7 @@ void PS3BT::Run() {
case L2CAP_DONE:
if(PS3MoveConnected) { // The Bulb and rumble values, has to be send at approximately every 5th second for it to stay on
if(millis() - timer > 4000) { // Send at least every 4th second
if((int32_t)(millis() - timer) > 4000) { // Send at least every 4th second
HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE); // The Bulb and rumble values, has to be written again and again, for it to stay turned on
timer = millis();
}
@ -510,7 +510,7 @@ void PS3BT::Run() {
// Playstation Sixaxis Dualshock and Navigation Controller commands
void PS3BT::HID_Command(uint8_t* data, uint8_t nbytes) {
if(millis() - timerHID <= 150) // Check if is has been more than 150ms since last command
if((int32_t)(millis() - timerHID) <= 150) // Check if is has been more than 150ms since last command
delay((uint32_t)(150 - (millis() - timerHID))); // There have to be a delay between commands
pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]); // Both the Navigation and Dualshock controller sends data via the control channel
timerHID = millis();
@ -595,7 +595,7 @@ void PS3BT::enable_sixaxis() { // Command used to enable the Dualshock 3 and Nav
// Playstation Move Controller commands
void PS3BT::HIDMove_Command(uint8_t* data, uint8_t nbytes) {
if(millis() - timerHID <= 150)// Check if is has been less than 150ms since last command
if((int32_t)(millis() - timerHID) <= 150)// Check if is has been less than 150ms since last command
delay((uint32_t)(150 - (millis() - timerHID))); // There have to be a delay between commands
pBtd->L2CAP_Command(hci_handle, data, nbytes, interrupt_scid[0], interrupt_scid[1]); // The Move controller sends it's data via the intterrupt channel
timerHID = millis();

View file

@ -276,14 +276,14 @@ uint8_t PS3USB::Poll() {
if(PS3Connected || PS3NavigationConnected) {
uint16_t BUFFER_SIZE = EP_MAXPKTSIZE;
pUsb->inTransfer(bAddress, epInfo[ PS3_INPUT_PIPE ].epAddr, &BUFFER_SIZE, readBuf); // input on endpoint 1
if(millis() - timer > 100) { // Loop 100ms before processing data
if((int32_t)(millis() - timer) > 100) { // Loop 100ms before processing data
readReport();
#ifdef PRINTREPORT
printReport(); // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
#endif
}
} else if(PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB
if(millis() - timer > 4000) { // Send at least every 4th second
if((int32_t)(millis() - timer) > 4000) { // Send at least every 4th second
Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE); // The Bulb and rumble values, has to be written again and again, for it to stay turned on
timer = millis();
}

View file

@ -421,7 +421,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) {
}
void SPP::Run() {
if(waitForLastCommand && (millis() - timer) > 100) { // We will only wait 100ms and see if the UIH Remote Port Negotiation Command is send, as some deviced don't send it
if(waitForLastCommand && (int32_t)(millis() - timer) > 100) { // We will only wait 100ms and see if the UIH Remote Port Negotiation Command is send, as some deviced don't send it
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nRFCOMM Connection is now established - Automatic\r\n"), 0x80);
#endif

14
Usb.cpp
View file

@ -313,7 +313,7 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
if(maxpktsize < 1 || maxpktsize > 64)
return USB_ERROR_INVALID_MAX_PKT_SIZE;
unsigned long timeout = millis() + USB_XFER_TIMEOUT;
uint32_t timeout = millis() + USB_XFER_TIMEOUT;
regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); //set toggle value
@ -328,7 +328,7 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ
rcode = (regRd(rHRSL) & 0x0f);
while(rcode && ((long)(millis() - timeout) < 0L)) {
while(rcode && ((int32_t)(millis() - timeout) < 0L)) {
switch(rcode) {
case hrNAK:
nak_count++;
@ -375,17 +375,17 @@ breakout:
/* return codes 0x00-0x0f are HRSLT( 0x00 being success ), 0xff means timeout */
uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
unsigned long timeout = millis() + USB_XFER_TIMEOUT;
uint32_t timeout = millis() + USB_XFER_TIMEOUT;
uint8_t tmpdata;
uint8_t rcode = hrSUCCESS;
uint8_t retry_count = 0;
uint16_t nak_count = 0;
while((long)(millis() - timeout) < 0L) {
while((int32_t)(millis() - timeout) < 0L) {
regWr(rHXFR, (token | ep)); //launch the transfer
rcode = USB_ERROR_TRANSFER_TIMEOUT;
while((long)(millis() - timeout) < 0L) //wait for transfer completion
while((int32_t)(millis() - timeout) < 0L) //wait for transfer completion
{
tmpdata = regRd(rHIRQ);
@ -476,7 +476,7 @@ void USB::Task(void) //USB state machine
case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here
break;
case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device
if((long)(millis() - delay) >= 0L)
if((int32_t)(millis() - delay) >= 0L)
usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
else break; // don't fall through
case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
@ -503,7 +503,7 @@ void USB::Task(void) //USB state machine
}
break;
case USB_ATTACHED_SUBSTATE_WAIT_RESET:
if((long)(millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING;
if((int32_t)(millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING;
else break; // don't fall through
case USB_STATE_CONFIGURING:

View file

@ -545,7 +545,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
Notify(wiimotePitch, 0x80);
*/
} else {
if((micros() - timer) > 1000000) { // Loop for 1 sec before resetting the values
if((int32_t)(micros() - timer) > 1000000) { // Loop for 1 sec before resetting the values
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nThe gyro values has been reset"), 0x80);
#endif
@ -698,7 +698,7 @@ void WII::L2CAP_task() {
/* The next states are in run() */
case L2CAP_INTERRUPT_DISCONNECT:
if(l2cap_check_flag(L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) && ((long)(millis() - timer) >= 0L)) {
if(l2cap_check_flag(L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) && ((int32_t)(millis() - timer) >= 0L)) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80);
#endif
@ -723,7 +723,7 @@ void WII::L2CAP_task() {
}
void WII::Run() {
if(l2cap_state == L2CAP_INTERRUPT_DISCONNECT && ((long)(millis() - timer) >= 0L))
if(l2cap_state == L2CAP_INTERRUPT_DISCONNECT && ((int32_t)(millis() - timer) >= 0L))
L2CAP_task(); // Call the rest of the disconnection routine after we have waited long enough
switch(l2cap_state) {

View file

@ -293,7 +293,7 @@ uint8_t XBOXRECV::Release() {
uint8_t XBOXRECV::Poll() {
if(!bPollEnable)
return 0;
if(!checkStatusTimer || ((millis() - checkStatusTimer) > 3000)) { // Run checkStatus every 3 seconds
if(!checkStatusTimer || ((int32_t)(millis() - checkStatusTimer) > 3000)) { // Run checkStatus every 3 seconds
checkStatusTimer = millis();
checkStatus();
}

View file

@ -33,7 +33,7 @@ void loop() {
Serial.println(srw1.srws1Data.tilt);
} else { // Show strobe light effect
static uint32_t timer;
if (millis() - timer > 12) {
if ((int32_t)(millis() - timer) > 12) {
timer = millis(); // Reset timer
static uint16_t leds = 0;

View file

@ -17,8 +17,6 @@ USB Usb;
//USBHub Hub6(&Usb);
//USBHub Hub7(&Usb);
uint32_t next_time;
void PrintAllAddresses(UsbDevice *pdev)
{
UsbDeviceAddress adr;
@ -60,8 +58,6 @@ void setup()
Serial.println("OSC did not start.");
delay( 200 );
next_time = millis() + 10000;
}
byte getdevdescr( byte addr, byte &num_conf );
@ -104,8 +100,6 @@ void loop()
Usb.Task();
if ( Usb.getUsbTaskState() == USB_STATE_RUNNING )
{
//if (millis() >= next_time)
{
Usb.ForEachUsbDevice(&PrintAllDescriptors);
Usb.ForEachUsbDevice(&PrintAllAddresses);
@ -113,7 +107,6 @@ void loop()
while ( 1 ); //stop
}
}
}
byte getdevdescr( byte addr, byte &num_conf )
{

View file

@ -68,7 +68,7 @@ void loop() {
digitalWrite(LED, msg[0] ? HIGH : LOW);
}
if (millis() - timer >= 1000) { // Send data every 1s
if ((int32_t)(millis() - timer) >= 1000) { // Send data every 1s
timer = millis();
rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer);
if (rcode && rcode != hrNAK) {

View file

@ -39,8 +39,6 @@ USB Usb;
FTDIAsync FtdiAsync;
FTDI Ftdi(&Usb, &FtdiAsync);
uint32_t next_time;
void setup()
{
Serial.begin( 115200 );
@ -53,8 +51,6 @@ void setup()
Serial.println("OSC did not start.");
delay( 200 );
next_time = millis() + 5000;
}
void loop()

View file

@ -96,7 +96,7 @@ void loop()
Usb.Task();
if ( Usb.getUsbTaskState() == USB_STATE_RUNNING ) {
if ((long)(millis() - next_time) >= 0L) {
if ((int32_t)(millis() - next_time) >= 0L) {
Usb.ForEachUsbDevice(&PrintAllDescriptors);
Usb.ForEachUsbDevice(&PrintAllAddresses);

View file

@ -71,7 +71,7 @@ void loop() {
if(Pl.isReady()) {
/* reading the GPS */
if((long)(millis() - read_delay) >= 0L) {
if((int32_t)(millis() - read_delay) >= 0L) {
read_delay += READ_DELAY;
rcode = Pl.RcvData(&rcvd, buf);
if(rcode && rcode != hrNAK)

View file

@ -94,10 +94,10 @@ void loop()
if( Pl.isReady()) {
bool newdata = false;
unsigned long start = millis();
uint32_t start = millis();
// Every 5 seconds we print an update
while (millis() - start < 5000) {
while ((int32_t)(millis() - start) < 5000) {
if( feedgps()) {
newdata = true;
}

View file

@ -371,7 +371,7 @@ void serialEvent() {
// ALL teensy versions LACK PWM ON LED
ISR(TIMER3_COMPA_vect) {
if((long)(millis() - LEDnext_time) >= 0L) {
if((int32_t)(millis() - LEDnext_time) >= 0L) {
LEDnext_time = millis() + 30;
// set the brightness of LED
@ -407,7 +407,7 @@ void loop() {
#if defined(__AVR__)
// Print a heap status report about every 10 seconds.
if((long)(millis() - HEAPnext_time) >= 0L) {
if((int32_t)(millis() - HEAPnext_time) >= 0L) {
if(UsbDEBUGlvl > 0x50) {
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
}
@ -421,7 +421,7 @@ void loop() {
#endif
// Horrid! This sort of thing really belongs in an ISR, not here!
// We also will be needing to test each hub port, we don't do this yet!
if(!change && !usbon && (long)(millis() - usbon_time) >= 0L) {
if(!change && !usbon && (int32_t)(millis() - usbon_time) >= 0L) {
change = true;
usbon = true;
}

View file

@ -578,7 +578,7 @@ template <const uint8_t BOOT_PROTOCOL>
uint8_t HIDBoot<BOOT_PROTOCOL>::Poll() {
uint8_t rcode = 0;
if(bPollEnable && ((long)(millis() - qNextPollTime) >= 0L)) {
if(bPollEnable && ((int32_t)(millis() - qNextPollTime) >= 0L)) {
// To-do: optimize manually, using the for loop only if needed.
for(int i = 0; i < epMUL(BOOT_PROTOCOL); i++) {

View file

@ -360,7 +360,7 @@ uint8_t HIDComposite::Poll() {
if(!bPollEnable)
return 0;
if((long)(millis() - qNextPollTime) >= 0L) {
if((int32_t)(millis() - qNextPollTime) >= 0L) {
qNextPollTime = millis() + pollInterval;
uint8_t buf[constBuffLen];

View file

@ -372,7 +372,7 @@ uint8_t HIDUniversal::Poll() {
if(!bPollEnable)
return 0;
if((long)(millis() - qNextPollTime) >= 0L) {
if((int32_t)(millis() - qNextPollTime) >= 0L) {
qNextPollTime = millis() + pollInterval;
uint8_t buf[constBuffLen];

View file

@ -673,7 +673,7 @@ uint8_t BulkOnly::Poll() {
if(!bPollEnable)
return 0;
if((long)(millis() - qNextPollTime) >= 0L) {
if((int32_t)(millis() - qNextPollTime) >= 0L) {
CheckMedia();
}
//rcode = 0;

View file

@ -232,7 +232,7 @@ uint8_t USBHub::Poll() {
if(!bPollEnable)
return 0;
if(((long)(millis() - qNextPollTime) >= 0L)) {
if(((int32_t)(millis() - qNextPollTime) >= 0L)) {
rcode = CheckHubStatus();
qNextPollTime = millis() + 100;
}