Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino Mega ADK and NAK bug #238

Open
mark-mb opened this issue May 1, 2016 · 1 comment
Open

Arduino Mega ADK and NAK bug #238

mark-mb opened this issue May 1, 2016 · 1 comment
Labels

Comments

@mark-mb
Copy link

mark-mb commented May 1, 2016

Hi

We're working on a project with Android and Arduino Mega ADK, and we're hitting the NAK bug when using USB Host Shield 2 libraries.

We use a very simple protocol: Android writes a request, Arduino processes, writes back OK or error, Android reads and continues. In the Android side, the application is a C application running on top of the kernel reading and writing directly to the /dev/usb_accessory file, no Java or JNI involved.

With this configuration, I tried a simple LED blink test, and this happens:

  • Android writes a pin high request
  • Arduino reads and processes
  • Arduino sends OK (format for OK is 5 bytes: 1 1 1 1 1)
  • Android reads
  • Android writes a pin low request
  • Arduino reads and processes
  • Arduino sends OK (1 1 1 1 1)
  • Android reads. But, at this point, it reads 0 0 0 0 0

After looking for the error in our code and failing, we looked at the Arduino libraries. This line was the issue (adk.cpp L.50):
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
It looks like the first device doesn't repeat transmission in case of hitting the NAK bug. I don't know why the code is this way, but we changed the line to:
epInfo[i].bmNakPower = USB_NAK_MAX_POWER;
This solved our issue.
Why is there a special case for the first device? Can it be removed? Did anyone hit the same problem before?

Thanks

@Benjamin410
Copy link

Hello,

I have a similar problem, that I am getting a lot of NAK. I did my testing with several examples and I am having always the NAK Issue when I am using this lib. (If I am using the follwoing lib the communication is more stable: https://github.com/aoabook/Arduino)

To give a better understandig what I did so far:

1.) modived the example [https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/adk/ArduinoBlinkLED/ArduinoBlinkLED.ino]

if (millis() - timer >= 300) { // Send data every 1s
      timer = millis();
      timer2 = timer;
      rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer);
      timer = millis();
      Serial.print(F("\r\nTimer: "));
      Serial.print(timer2);
      Serial.print(F("  -  "));
      Serial.print(timer);
      Serial.print(F("  Delta: "));
      Serial.print(timer - timer2);
      if (rcode == hrNAK) {
        Serial.print(F("\r\nNACK: "));
      }
    }

2.) Then I get the following output on the Serial Monitor:

Timer: 104950  -  104950  Delta: 0
Timer: 105250  -  105250  Delta: 0
NACK: 
Timer: 105550  -  105550  Delta: 0
Timer: 105850  -  105850  Delta: 0
NACK: 
Timer: 106150  -  106150  Delta: 0
Timer: 106450  -  106450  Delta: 0
NACK: 
Timer: 106750  -  106750  Delta: 0
Timer: 107051  -  107051  Delta: 0
NACK: 
Timer: 107351  -  107351  Delta: 0
Timer: 107651  -  107651  Delta: 0
NACK: 
Timer: 107951  -  107951  Delta: 0
Timer: 108251  -  108251  Delta: 0
NACK: 
Timer: 108551  -  108551  Delta: 0
Timer: 108851  -  108851  Delta: 0

3.) at the same time I am missing data on my android device. (Which is more or less in line with the NACK Outputs)

10-03 14:45:13.324 ArduinoBlinkLEDActivity: Counter10Data Received 104950
10-03 14:45:13.924 ArduinoBlinkLEDActivity: Counter11Data Received 105250
10-03 14:45:14.529 ArduinoBlinkLEDActivity: Counter12Data Received 106150
10-03 14:45:15.129 ArduinoBlinkLEDActivity: Counter13Data Received 106450
10-03 14:45:15.729 ArduinoBlinkLEDActivity: Counter14Data Received 107351
10-03 14:45:16.329 ArduinoBlinkLEDActivity: Counter15Data Received 107651
10-03 14:45:16.929 ArduinoBlinkLEDActivity: Counter16Data Received 108551
10-03 14:45:17.529 ArduinoBlinkLEDActivity: Counter17Data Received 108851

When I am using the change provided here I am get in the Serial Monitor and Android the same amount of data, but It takes very long to process on the Arduino side. Which leads into very high delay regarding the timestamps (way more than the defined 300ms), Also some how the data which is received on the Android side not matches 100% to the Arduino side:

Timer: 7494  -  7494  Delta: 0
Timer: 9197  -  9197  Delta: 0
Timer: 10901  -  10901  Delta: 0
Timer: 12604  -  12604  Delta: 0
Timer: 14307  -  14307  Delta: 0
Timer: 16010  -  16010  Delta: 0
10-03 14:51:17.649 ArduinoBlinkLEDActivity: Counter0Data Received 7424
10-03 14:51:19.354 ArduinoBlinkLEDActivity: Counter1Data Received 8960
10-03 14:51:21.059 ArduinoBlinkLEDActivity: Counter2Data Received 10752
10-03 14:51:22.764 ArduinoBlinkLEDActivity: Counter3Data Received 12544
10-03 14:51:24.469 ArduinoBlinkLEDActivity: Counter4Data Received 14080
10-03 14:51:26.174 ArduinoBlinkLEDActivity: Counter5Data Received 15872

Addtional Hint: When I am removing the read data function on Arduino side the delay is gone and the execution is again on a regular base.

It would be great if somebody could look into that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants