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.

This commit is contained in:
Andrew J. Kroll 2014-05-22 23:36:33 -04:00
parent 0acad810b2
commit d56ed57495
10 changed files with 155 additions and 162 deletions

View file

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

10
Usb.cpp
View file

@ -327,7 +327,7 @@ uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8
regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ regWr(rHIRQ, bmHXFRDNIRQ); //clear IRQ
rcode = (regRd(rHRSL) & 0x0f); rcode = (regRd(rHRSL) & 0x0f);
while(rcode && (timeout > millis())) { while(rcode && ((long)(millis() - timeout) >= 0L)) {
switch(rcode) { switch(rcode) {
case hrNAK: case hrNAK:
nak_count++; nak_count++;
@ -380,11 +380,11 @@ uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
uint8_t retry_count = 0; uint8_t retry_count = 0;
uint16_t nak_count = 0; uint16_t nak_count = 0;
while(timeout > millis()) { while((long)(millis() - timeout) >= 0L) {
regWr(rHXFR, (token | ep)); //launch the transfer regWr(rHXFR, (token | ep)); //launch the transfer
rcode = USB_ERROR_TRANSFER_TIMEOUT; rcode = USB_ERROR_TRANSFER_TIMEOUT;
while(timeout > millis()) //wait for transfer completion while((long)(millis() - timeout) >= 0L) //wait for transfer completion
{ {
tmpdata = regRd(rHIRQ); tmpdata = regRd(rHIRQ);
@ -475,7 +475,7 @@ void USB::Task(void) //USB state machine
case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here
break; break;
case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device
if(delay < millis()) if((long)(millis() - delay) >= 0L)
usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE; usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
else break; // don't fall through else break; // don't fall through
case USB_ATTACHED_SUBSTATE_RESET_DEVICE: case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
@ -502,7 +502,7 @@ void USB::Task(void) //USB state machine
} }
break; break;
case USB_ATTACHED_SUBSTATE_WAIT_RESET: case USB_ATTACHED_SUBSTATE_WAIT_RESET:
if(delay < millis()) usb_task_state = USB_STATE_CONFIGURING; if((long)(millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING;
else break; // don't fall through else break; // don't fall through
case USB_STATE_CONFIGURING: case USB_STATE_CONFIGURING:

View file

@ -657,7 +657,7 @@ void WII::L2CAP_task() {
/* The next states are in run() */ /* The next states are in run() */
case L2CAP_INTERRUPT_DISCONNECT: case L2CAP_INTERRUPT_DISCONNECT:
if(l2cap_check_flag(L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) && millis() > timer) { if(l2cap_check_flag(L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE) && ((long)(millis() - timer) >= 0L)) {
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80); Notify(PSTR("\r\nDisconnected Interrupt Channel"), 0x80);
#endif #endif
@ -682,7 +682,7 @@ void WII::L2CAP_task() {
} }
void WII::Run() { void WII::Run() {
if(l2cap_state == L2CAP_INTERRUPT_DISCONNECT && millis() > timer) if(l2cap_state == L2CAP_INTERRUPT_DISCONNECT && ((long)(millis() - timer) >= 0L))
L2CAP_task(); // Call the rest of the disconnection routine after we have waited long enough L2CAP_task(); // Call the rest of the disconnection routine after we have waited long enough
switch(l2cap_state) { switch(l2cap_state) {

View file

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

View file

@ -10,21 +10,18 @@
#include <spi4teensy3.h> #include <spi4teensy3.h>
#endif #endif
class PLAsyncOper : public CDCAsyncOper class PLAsyncOper : public CDCAsyncOper {
{
public: public:
virtual uint8_t OnInit(ACM *pacm); virtual uint8_t OnInit(ACM *pacm);
}; };
uint8_t PLAsyncOper::OnInit(ACM *pacm) uint8_t PLAsyncOper::OnInit(ACM *pacm) {
{
uint8_t rcode; uint8_t rcode;
// Set DTR = 1 // Set DTR = 1
rcode = pacm->SetControlLineState(1); rcode = pacm->SetControlLineState(1);
if (rcode) if(rcode) {
{
ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode); ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
return rcode; return rcode;
} }
@ -37,7 +34,7 @@ uint8_t PLAsyncOper::OnInit(ACM *pacm)
rcode = pacm->SetLineCoding(&lc); rcode = pacm->SetLineCoding(&lc);
if (rcode) if(rcode)
ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode); ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
return rcode; return rcode;
@ -50,35 +47,33 @@ PL2303 Pl(&Usb, &AsyncOper);
uint32_t read_delay; uint32_t read_delay;
#define READ_DELAY 100 #define READ_DELAY 100
void setup() void setup() {
{ Serial.begin(115200);
Serial.begin( 115200 ); while(!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
Serial.println("Start"); Serial.println("Start");
if (Usb.Init() == -1) if(Usb.Init() == -1)
Serial.println("OSCOKIRQ failed to assert"); Serial.println("OSCOKIRQ failed to assert");
delay( 200 ); delay(200);
} }
void loop() void loop() {
{ uint8_t rcode;
uint8_t rcode; uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint
uint8_t buf[64]; //serial buffer equals Max.packet size of bulk-IN endpoint uint16_t rcvd = 64;
uint16_t rcvd = 64;
Usb.Task(); Usb.Task();
if( Pl.isReady()) { if(Pl.isReady()) {
/* reading the GPS */ /* reading the GPS */
if( read_delay < millis() ){ if((long)(millis() - read_delay) >= 0L) {
read_delay += READ_DELAY; read_delay += READ_DELAY;
rcode = Pl.RcvData(&rcvd, buf); rcode = Pl.RcvData(&rcvd, buf);
if ( rcode && rcode != hrNAK ) if(rcode && rcode != hrNAK)
ErrorMessage<uint8_t>(PSTR("Ret"), rcode); ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
if( rcvd ) { //more than zero bytes received if(rcvd) { //more than zero bytes received
for( uint16_t i=0; i < rcvd; i++ ) { for(uint16_t i = 0; i < rcvd; i++) {
Serial.print((char)buf[i]); //printing on the screen Serial.print((char)buf[i]); //printing on the screen
}//for( uint16_t i=0; i < rcvd; i++... }//for( uint16_t i=0; i < rcvd; i++...
}//if( rcvd }//if( rcvd

View file

@ -126,7 +126,7 @@ static int tty_std_putc(char c, FILE *t) {
} }
static int tty_std_getc(FILE *t) { static int tty_std_getc(FILE *t) {
while (!Serial.available()); while(!Serial.available());
return Serial.read(); return Serial.read();
} }
@ -140,18 +140,18 @@ extern "C" {
int _write(int fd, const char *ptr, int len) { int _write(int fd, const char *ptr, int len) {
int j; int j;
for (j = 0; j < len; j++) { for(j = 0; j < len; j++) {
if (fd == 1) if(fd == 1)
Serial.write(*ptr++); Serial.write(*ptr++);
else if (fd == 2) else if(fd == 2)
USB_HOST_SERIAL.write(*ptr++); USB_HOST_SERIAL.write(*ptr++);
} }
return len; return len;
} }
int _read(int fd, char *ptr, int len) { int _read(int fd, char *ptr, int len) {
if (len > 0 && fd == 0) { if(len > 0 && fd == 0) {
while (!Serial.available()); while(!Serial.available());
*ptr = Serial.read(); *ptr = Serial.read();
return 1; return 1;
} }
@ -175,7 +175,7 @@ extern "C" {
void setup() { void setup() {
boolean serr = false; boolean serr = false;
for (int i = 0; i < _VOLUMES; i++) { for(int i = 0; i < _VOLUMES; i++) {
Fats[i] = NULL; Fats[i] = NULL;
sto[i].private_data = new pvt_t; sto[i].private_data = new pvt_t;
((pvt_t *)sto[i].private_data)->B = 255; // impossible ((pvt_t *)sto[i].private_data)->B = 255; // impossible
@ -193,7 +193,7 @@ void setup() {
// Initialize 'debug' serial port // Initialize 'debug' serial port
USB_HOST_SERIAL.begin(115200); USB_HOST_SERIAL.begin(115200);
// Do not start primary Serial port if already started. // Do not start primary Serial port if already started.
if (bit_is_clear(UCSR0B, TXEN0)) { if(bit_is_clear(UCSR0B, TXEN0)) {
Serial.begin(115200); Serial.begin(115200);
serr = true; serr = true;
} }
@ -220,7 +220,7 @@ void setup() {
analogWrite(LED_BUILTIN, 0); analogWrite(LED_BUILTIN, 0);
delay(500); delay(500);
#else #else
while (!Serial); while(!Serial);
#endif #endif
printf_P(PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n")); printf_P(PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n"));
@ -236,7 +236,7 @@ void setup() {
"Disabled" "Disabled"
#endif #endif
"\r\n")); "\r\n"));
if (serr) { if(serr) {
fprintf_P(stderr, PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n")); fprintf_P(stderr, PSTR("\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nStart\r\n"));
fprintf_P(stderr, PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl); fprintf_P(stderr, PSTR("Current UsbDEBUGlvl %02x\r\n"), UsbDEBUGlvl);
fprintf_P(stderr, PSTR("Long filename support: " fprintf_P(stderr, PSTR("Long filename support: "
@ -274,7 +274,7 @@ void setup() {
// I want to be able to have slightly more control. // I want to be able to have slightly more control.
// Besides, it is easier to initialize stuff... // Besides, it is easier to initialize stuff...
#if WANT_HUB_TEST #if WANT_HUB_TEST
for (int i = 0; i < MAX_HUBS; i++) { for(int i = 0; i < MAX_HUBS; i++) {
Hubs[i] = new USBHub(&Usb); Hubs[i] = new USBHub(&Usb);
#if defined(AVR) #if defined(AVR)
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
@ -284,7 +284,7 @@ void setup() {
// Initialize generic storage. This must be done before USB starts. // Initialize generic storage. This must be done before USB starts.
InitStorage(); InitStorage();
while (Usb.Init(1000) == -1) { while(Usb.Init(1000) == -1) {
printf_P(PSTR("No USB HOST Shield?\r\n")); printf_P(PSTR("No USB HOST Shield?\r\n"));
Notify(PSTR("OSC did not start."), 0x40); Notify(PSTR("OSC did not start."), 0x40);
} }
@ -316,7 +316,7 @@ void setup() {
//printf("SPI_CTAR0 = %8.8X\r\n", ctar); //printf("SPI_CTAR0 = %8.8X\r\n", ctar);
uint32_t mcr = SPI0_MCR; uint32_t mcr = SPI0_MCR;
if (mcr & SPI_MCR_MDIS) { if(mcr & SPI_MCR_MDIS) {
SPI0_CTAR0 = ctar; SPI0_CTAR0 = ctar;
} else { } else {
SPI0_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT; SPI0_MCR = mcr | SPI_MCR_MDIS | SPI_MCR_HALT;
@ -334,23 +334,23 @@ void serialEvent() {
// . to increase by 16, , to decrease by 16 // . to increase by 16, , to decrease by 16
// e to flick VBUS // e to flick VBUS
// * to report debug level // * to report debug level
if (Serial.available()) { if(Serial.available()) {
int inByte = Serial.read(); int inByte = Serial.read();
switch (inByte) { switch(inByte) {
case '+': case '+':
if (UsbDEBUGlvl < 0xff) UsbDEBUGlvl++; if(UsbDEBUGlvl < 0xff) UsbDEBUGlvl++;
reportlvl = true; reportlvl = true;
break; break;
case '-': case '-':
if (UsbDEBUGlvl > 0x00) UsbDEBUGlvl--; if(UsbDEBUGlvl > 0x00) UsbDEBUGlvl--;
reportlvl = true; reportlvl = true;
break; break;
case '.': case '.':
if (UsbDEBUGlvl < 0xf0) UsbDEBUGlvl += 16; if(UsbDEBUGlvl < 0xf0) UsbDEBUGlvl += 16;
reportlvl = true; reportlvl = true;
break; break;
case ',': case ',':
if (UsbDEBUGlvl > 0x0f) UsbDEBUGlvl -= 16; if(UsbDEBUGlvl > 0x0f) UsbDEBUGlvl -= 16;
reportlvl = true; reportlvl = true;
break; break;
case '*': case '*':
@ -370,7 +370,7 @@ void serialEvent() {
#if defined(AVR) #if defined(AVR)
ISR(TIMER3_COMPA_vect) { ISR(TIMER3_COMPA_vect) {
if (millis() >= LEDnext_time) { if(millis() >= LEDnext_time) {
LEDnext_time = millis() + 30; LEDnext_time = millis() + 30;
// set the brightness of LED // set the brightness of LED
@ -380,11 +380,11 @@ ISR(TIMER3_COMPA_vect) {
brightness = brightness + fadeAmount; brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: // reverse the direction of the fading at the ends of the fade:
if (brightness <= 0) { if(brightness <= 0) {
brightness = 0; brightness = 0;
fadeAmount = -fadeAmount; fadeAmount = -fadeAmount;
} }
if (brightness >= 255) { if(brightness >= 255) {
brightness = 255; brightness = 255;
fadeAmount = -fadeAmount; fadeAmount = -fadeAmount;
} }
@ -406,8 +406,8 @@ void loop() {
#if defined(AVR) #if defined(AVR)
// Print a heap status report about every 10 seconds. // Print a heap status report about every 10 seconds.
if (millis() >= HEAPnext_time) { if(millis() >= HEAPnext_time) {
if (UsbDEBUGlvl > 0x50) { if(UsbDEBUGlvl > 0x50) {
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
} }
HEAPnext_time = millis() + 10000; HEAPnext_time = millis() + 10000;
@ -419,14 +419,14 @@ void loop() {
#endif #endif
// Horrid! This sort of thing really belongs in an ISR, not here! // 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! // We also will be needing to test each hub port, we don't do this yet!
if (!change && !usbon && millis() >= usbon_time) { if(!change && !usbon && millis() >= usbon_time) {
change = true; change = true;
usbon = true; usbon = true;
} }
if (change) { if(change) {
change = false; change = false;
if (usbon) { if(usbon) {
Usb.vbusPower(vbus_on); Usb.vbusPower(vbus_on);
printf_P(PSTR("VBUS on\r\n")); printf_P(PSTR("VBUS on\r\n"));
} else { } else {
@ -436,21 +436,21 @@ void loop() {
} }
Usb.Task(); Usb.Task();
current_state = Usb.getUsbTaskState(); current_state = Usb.getUsbTaskState();
if (current_state != last_state) { if(current_state != last_state) {
if (UsbDEBUGlvl > 0x50) if(UsbDEBUGlvl > 0x50)
printf_P(PSTR("USB state = %x\r\n"), current_state); printf_P(PSTR("USB state = %x\r\n"), current_state);
#if defined(AVR) #if defined(AVR)
if (current_state == USB_STATE_RUNNING) { if(current_state == USB_STATE_RUNNING) {
fadeAmount = 30; fadeAmount = 30;
} }
#endif #endif
if (current_state == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) { if(current_state == USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE) {
#if defined(AVR) #if defined(AVR)
fadeAmount = 80; fadeAmount = 80;
#endif #endif
partsready = false; partsready = false;
for (int i = 0; i < cpart; i++) { for(int i = 0; i < cpart; i++) {
if (Fats[i] != NULL) if(Fats[i] != NULL)
delete Fats[i]; delete Fats[i];
Fats[i] = NULL; Fats[i] = NULL;
} }
@ -462,28 +462,27 @@ void loop() {
} }
// only do any of this if usb is on // only do any of this if usb is on
if (usbon) { if(usbon) {
if (partsready && !fatready) { if(partsready && !fatready) {
if (cpart > 0) fatready = true; if(cpart > 0) fatready = true;
} }
// This is horrible, and needs to be moved elsewhere! // This is horrible, and needs to be moved elsewhere!
for (int B = 0; B < MAX_USB_MS_DRIVERS; B++) { for(int B = 0; B < MAX_USB_MS_DRIVERS; B++) {
if (!partsready && (Bulk[B]->GetAddress() != NULL)) { if(!partsready && (Bulk[B]->GetAddress() != NULL)) {
// Build a list. // Build a list.
int ML = Bulk[B]->GetbMaxLUN(); int ML = Bulk[B]->GetbMaxLUN();
//printf("MAXLUN = %i\r\n", ML); //printf("MAXLUN = %i\r\n", ML);
ML++; ML++;
for (int i = 0; i < ML; i++) { for(int i = 0; i < ML; i++) {
if (Bulk[B]->LUNIsGood(i)) { if(Bulk[B]->LUNIsGood(i)) {
partsready = true; partsready = true;
((pvt_t *)(sto[i].private_data))->lun = i; ((pvt_t *)(sto[i].private_data))->lun = i;
((pvt_t *)(sto[i].private_data))->B = B; ((pvt_t *)(sto[i].private_data))->B = B;
sto[i].Read = *PRead;
sto[i].Write = *PWrite;
sto[i].Reads = *PReads; sto[i].Reads = *PReads;
sto[i].Writes = *PWrites; sto[i].Writes = *PWrites;
sto[i].Status = *PStatus; sto[i].Status = *PStatus;
sto[i].Commit = *UHS_USB_BulkOnly_Commit;
sto[i].TotalSectors = Bulk[B]->GetCapacity(i); sto[i].TotalSectors = Bulk[B]->GetCapacity(i);
sto[i].SectorSize = Bulk[B]->GetSectorSize(i); sto[i].SectorSize = Bulk[B]->GetSectorSize(i);
printf_P(PSTR("LUN:\t\t%u\r\n"), i); printf_P(PSTR("LUN:\t\t%u\r\n"), i);
@ -492,18 +491,18 @@ void loop() {
// get the partition data... // get the partition data...
PT = new PCPartition; PT = new PCPartition;
if (!PT->Init(&sto[i])) { if(!PT->Init(&sto[i])) {
part_t *apart; part_t *apart;
for (int j = 0; j < 4; j++) { for(int j = 0; j < 4; j++) {
apart = PT->GetPart(j); apart = PT->GetPart(j);
if (apart != NULL && apart->type != 0x00) { if(apart != NULL && apart->type != 0x00) {
memcpy(&(parts[cpart]), apart, sizeof (part_t)); memcpy(&(parts[cpart]), apart, sizeof (part_t));
printf_P(PSTR("Partition %u type %#02x\r\n"), j, parts[cpart].type); printf_P(PSTR("Partition %u type %#02x\r\n"), j, parts[cpart].type);
// for now // for now
if (isfat(parts[cpart].type)) { if(isfat(parts[cpart].type)) {
Fats[cpart] = new PFAT(&sto[i], cpart, parts[cpart].firstSector); Fats[cpart] = new PFAT(&sto[i], cpart, parts[cpart].firstSector);
//int r = Fats[cpart]->Good(); //int r = Fats[cpart]->Good();
if (Fats[cpart]->MountStatus()) { if(Fats[cpart]->MountStatus()) {
delete Fats[cpart]; delete Fats[cpart];
Fats[cpart] = NULL; Fats[cpart] = NULL;
} else cpart++; } else cpart++;
@ -514,7 +513,7 @@ void loop() {
// try superblock // try superblock
Fats[cpart] = new PFAT(&sto[i], cpart, 0); Fats[cpart] = new PFAT(&sto[i], cpart, 0);
//int r = Fats[cpart]->Good(); //int r = Fats[cpart]->Good();
if (Fats[cpart]->MountStatus()) { if(Fats[cpart]->MountStatus()) {
//printf_P(PSTR("Superblock error %x\r\n"), r); //printf_P(PSTR("Superblock error %x\r\n"), r);
delete Fats[cpart]; delete Fats[cpart];
Fats[cpart] = NULL; Fats[cpart] = NULL;
@ -535,18 +534,18 @@ void loop() {
} }
} }
if (fatready) { if(fatready) {
if (Fats[0] != NULL) { if(Fats[0] != NULL) {
struct Pvt * p; struct Pvt * p;
p = ((struct Pvt *)(Fats[0]->storage->private_data)); p = ((struct Pvt *)(Fats[0]->storage->private_data));
if (!Bulk[p->B]->LUNIsGood(p->lun)) { if(!Bulk[p->B]->LUNIsGood(p->lun)) {
// media change // media change
#if defined(AVR) #if defined(AVR)
fadeAmount = 80; fadeAmount = 80;
#endif #endif
partsready = false; partsready = false;
for (int i = 0; i < cpart; i++) { for(int i = 0; i < cpart; i++) {
if (Fats[i] != NULL) if(Fats[i] != NULL)
delete Fats[i]; delete Fats[i];
Fats[cpart] = NULL; Fats[cpart] = NULL;
} }
@ -557,62 +556,61 @@ void loop() {
} }
} }
if (fatready) { if(fatready) {
FRESULT rc; /* Result code */ FRESULT rc; /* Result code */
UINT bw, br, i; UINT bw, br, i;
if (!notified) { if(!notified) {
#if defined(AVR) #if defined(AVR)
fadeAmount = 5; fadeAmount = 5;
#endif #endif
notified = true; notified = true;
printf_P(PSTR("\r\nOpen an existing file (message.txt).\r\n")); printf_P(PSTR("\r\nOpen an existing file (message.txt).\r\n"));
rc = f_open(&My_File_Object_x, "0:/MESSAGE.TXT", FA_READ); rc = f_open(&My_File_Object_x, "0:/MESSAGE.TXT", FA_READ);
if (rc) printf_P(PSTR("Error %i, message.txt not found.\r\n"), rc); if(rc) printf_P(PSTR("Error %i, message.txt not found.\r\n"), rc);
else { else {
printf_P(PSTR("\r\nType the file content.\r\n")); printf_P(PSTR("\r\nType the file content.\r\n"));
for (;;) { for(;;) {
rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &br); /* Read a chunk of file */ rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &br); /* Read a chunk of file */
if (rc || !br) break; /* Error or end of file */ if(rc || !br) break; /* Error or end of file */
for (i = 0; i < br; i++) { for(i = 0; i < br; i++) {
/* Type the data */ /* Type the data */
if (My_Buff_x[i] == '\n') if(My_Buff_x[i] == '\n')
Serial.write('\r'); Serial.write('\r');
if (My_Buff_x[i] != '\r') if(My_Buff_x[i] != '\r')
Serial.write(My_Buff_x[i]); Serial.write(My_Buff_x[i]);
Serial.flush(); Serial.flush();
} }
} }
if (rc) { if(rc) {
f_close(&My_File_Object_x); f_close(&My_File_Object_x);
goto out; goto out;
} }
printf_P(PSTR("\r\nClose the file.\r\n")); printf_P(PSTR("\r\nClose the file.\r\n"));
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) goto out; if(rc) goto out;
} }
printf_P(PSTR("\r\nCreate a new file (hello.txt).\r\n")); printf_P(PSTR("\r\nCreate a new file (hello.txt).\r\n"));
rc = f_open(&My_File_Object_x, "0:/Hello.TxT", FA_WRITE | FA_CREATE_ALWAYS); rc = f_open(&My_File_Object_x, "0:/Hello.TxT", FA_WRITE | FA_CREATE_ALWAYS);
if (rc) { if(rc) {
die(rc); die(rc);
goto outdir; goto outdir;
} }
printf_P(PSTR("\r\nWrite a text data. (Hello world!)\r\n")); printf_P(PSTR("\r\nWrite a text data. (Hello world!)\r\n"));
rc = f_write(&My_File_Object_x, "Hello world!\r\n", 14, &bw); rc = f_write(&My_File_Object_x, "Hello world!\r\n", 14, &bw);
if (rc) { if(rc) {
goto out; goto out;
} }
printf_P(PSTR("%u bytes written.\r\n"), bw); printf_P(PSTR("%u bytes written.\r\n"), bw);
printf_P(PSTR("\r\nClose the file.\r\n")); printf_P(PSTR("\r\nClose the file.\r\n"));
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) { if(rc) {
die(rc); die(rc);
goto out; goto out;
} }
outdir: outdir:{
{
#if _USE_LFN #if _USE_LFN
char lfn[_MAX_LFN + 1]; char lfn[_MAX_LFN + 1];
FILINFO My_File_Info_Object_x; /* File information object */ FILINFO My_File_Info_Object_x; /* File information object */
@ -621,7 +619,7 @@ outdir:
DIR My_Dir_Object_x; /* Directory object */ DIR My_Dir_Object_x; /* Directory object */
printf_P(PSTR("\r\nOpen root directory.\r\n")); printf_P(PSTR("\r\nOpen root directory.\r\n"));
rc = f_opendir(&My_Dir_Object_x, "0:/"); rc = f_opendir(&My_Dir_Object_x, "0:/");
if (rc) { if(rc) {
die(rc); die(rc);
goto out; goto out;
} }
@ -630,46 +628,46 @@ outdir:
#if defined(AVR) #if defined(AVR)
printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap()); printf_P(PSTR("Available heap: %u Bytes\r\n"), freeHeap());
#endif #endif
for (;;) { for(;;) {
#if _USE_LFN #if _USE_LFN
My_File_Info_Object_x.lfsize = _MAX_LFN; My_File_Info_Object_x.lfsize = _MAX_LFN;
#endif #endif
rc = f_readdir(&My_Dir_Object_x, &My_File_Info_Object_x); /* Read a directory item */ rc = f_readdir(&My_Dir_Object_x, &My_File_Info_Object_x); /* Read a directory item */
if (rc || !My_File_Info_Object_x.fname[0]) break; /* Error or end of dir */ if(rc || !My_File_Info_Object_x.fname[0]) break; /* Error or end of dir */
if (My_File_Info_Object_x.fattrib & AM_DIR) { if(My_File_Info_Object_x.fattrib & AM_DIR) {
Serial.write('d'); Serial.write('d');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
Serial.write('r'); Serial.write('r');
if (My_File_Info_Object_x.fattrib & AM_RDO) { if(My_File_Info_Object_x.fattrib & AM_RDO) {
Serial.write('-'); Serial.write('-');
} else { } else {
Serial.write('w'); Serial.write('w');
} }
if (My_File_Info_Object_x.fattrib & AM_HID) { if(My_File_Info_Object_x.fattrib & AM_HID) {
Serial.write('h'); Serial.write('h');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
if (My_File_Info_Object_x.fattrib & AM_SYS) { if(My_File_Info_Object_x.fattrib & AM_SYS) {
Serial.write('s'); Serial.write('s');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
if (My_File_Info_Object_x.fattrib & AM_ARC) { if(My_File_Info_Object_x.fattrib & AM_ARC) {
Serial.write('a'); Serial.write('a');
} else { } else {
Serial.write('-'); Serial.write('-');
} }
#if _USE_LFN #if _USE_LFN
if (*My_File_Info_Object_x.lfname) if(*My_File_Info_Object_x.lfname)
printf_P(PSTR(" %8lu %s (%s)\r\n"), My_File_Info_Object_x.fsize, My_File_Info_Object_x.fname, My_File_Info_Object_x.lfname); printf_P(PSTR(" %8lu %s (%s)\r\n"), My_File_Info_Object_x.fsize, My_File_Info_Object_x.fname, My_File_Info_Object_x.lfname);
else else
#endif #endif
@ -677,48 +675,48 @@ outdir:
} }
} }
out: out:
if (rc) die(rc); if(rc) die(rc);
printf_P(PSTR("\r\nTest completed.\r\n")); printf_P(PSTR("\r\nTest completed.\r\n"));
} }
if (runtest) { if(runtest) {
ULONG ii, wt, rt, start, end; ULONG ii, wt, rt, start, end;
runtest = false; runtest = false;
f_unlink("0:/10MB.bin"); f_unlink("0:/10MB.bin");
printf_P(PSTR("\r\nCreate a new 10MB test file (10MB.bin).\r\n")); printf_P(PSTR("\r\nCreate a new 10MB test file (10MB.bin).\r\n"));
rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_WRITE | FA_CREATE_ALWAYS); rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_WRITE | FA_CREATE_ALWAYS);
if (rc) goto failed; if(rc) goto failed;
for (bw = 0; bw < mbxs; bw++) My_Buff_x[bw] = bw & 0xff; for(bw = 0; bw < mbxs; bw++) My_Buff_x[bw] = bw & 0xff;
fflush(stdout); fflush(stdout);
start = millis(); start = millis();
while (start == millis()); while(start == millis());
for (ii = 10485760LU / mbxs; ii > 0LU; ii--) { for(ii = 10485760LU / mbxs; ii > 0LU; ii--) {
rc = f_write(&My_File_Object_x, My_Buff_x, mbxs, &bw); rc = f_write(&My_File_Object_x, My_Buff_x, mbxs, &bw);
if (rc || !bw) goto failed; if(rc || !bw) goto failed;
} }
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) goto failed; if(rc) goto failed;
end = millis(); end = millis();
wt = (end - start) - 1; wt = (end - start) - 1;
printf_P(PSTR("Time to write 10485760 bytes: %lu ms (%lu sec) \r\n"), wt, (500 + wt) / 1000UL); printf_P(PSTR("Time to write 10485760 bytes: %lu ms (%lu sec) \r\n"), wt, (500 + wt) / 1000UL);
rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_READ); rc = f_open(&My_File_Object_x, "0:/10MB.bin", FA_READ);
fflush(stdout); fflush(stdout);
start = millis(); start = millis();
while (start == millis()); while(start == millis());
if (rc) goto failed; if(rc) goto failed;
for (;;) { for(;;) {
rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &bw); /* Read a chunk of file */ rc = f_read(&My_File_Object_x, My_Buff_x, mbxs, &bw); /* Read a chunk of file */
if (rc || !bw) break; /* Error or end of file */ if(rc || !bw) break; /* Error or end of file */
} }
end = millis(); end = millis();
if (rc) goto failed; if(rc) goto failed;
rc = f_close(&My_File_Object_x); rc = f_close(&My_File_Object_x);
if (rc) goto failed; if(rc) goto failed;
rt = (end - start) - 1; rt = (end - start) - 1;
printf_P(PSTR("Time to read 10485760 bytes: %lu ms (%lu sec)\r\nDelete test file\r\n"), rt, (500 + rt) / 1000UL); printf_P(PSTR("Time to read 10485760 bytes: %lu ms (%lu sec)\r\nDelete test file\r\n"), rt, (500 + rt) / 1000UL);
failed: failed:
if (rc) die(rc); if(rc) die(rc);
printf_P(PSTR("10MB timing test finished.\r\n")); printf_P(PSTR("10MB timing test finished.\r\n"));
} }
} }

View file

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

View file

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

View file

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

View file

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