USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL; (usb host shield 2.0)

Hello,

At the moment, I am engaged in the development of product which is based on Arduino mega adk + tusb2036 Hub IC. I have to drive keyboard, Flash drive and a printer through hub IC. I am using the library Usb Host Shield 2.0 (GitHub - felis/USB_Host_Shield_2.0: Revision 2.0 of USB Host Library for Arduino.) from Github. I am successful in driving any of the two devices which are declared first. When I try to make something out of device which is declared at last, I am getting the error "USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;".

Here is my code:

#include <hidboot.h>
#include <usbh_midi.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <printer_font.h>
#include <SPI.h>
#include <UsbFat.h>
#include <masstorage.h>
#include "DigitalIO.h"

#define PRINTER_CLEAR_COMMAND_LEN    65
#define PRINTER_HEADER_COMMAND_LEN    30

#define MIDI_MAX_ENDPOINTS 5
/*----------------------------------------------------------------------------*/

static unsigned char PRINTER_status;
/*----------------------------------------------------------------------------*/
const char clear_command[] PROGMEM = 
{
  
 0x0C,
 0X0D,
 0x1B, 
};

const size_t BUFFER_SZ = 200;
char Buffer[BUFFER_SZ];
char Buffer1[BUFFER_SZ];
char file_name[80];
size_t pos = 0, iIndex1 = 0, iIndex2 = 0;    // current write position in buffer
int c, g =0, iCounter = 0,drive_mode = 1;
int count = 0;
int ret;
char input[12];
/*--------------------------------------------------------------------------*/
const uint8_t SOFT_SPI_MISO_PIN = 32;
const uint8_t SOFT_SPI_MOSI_PIN = 31;
const uint8_t SOFT_SPI_SCK_PIN  = 33;
const uint8_t SPI_MODE = 0;



const byte numChars = 32;
String receivedChars,s2,s3;   // an array to store the received data
String dataTokens1[20];
String dataTokens2[20];
String _oString;
boolean newData = false;
uint32_t next_time;
int fVariable1 = 0;
int fVariable2 = 0;



bool isFirst = true;
unsigned char *rxBuffer;
int bytescount=0;
String s;
bool isReady = false;


class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);
        uint8_t mapExtraAsciiKeys(uint8_t);

protected:
        virtual void OnControlKeysChanged(uint8_t before, uint8_t after);

  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
  virtual void OnKeyUp  (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};

void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
    MODIFIERKEYS mod;
    *((uint8_t*)&mod) = m;
    Serial.print((mod.bmLeftCtrl   == 1) ? "C" : " ");
    Serial.print((mod.bmLeftShift  == 1) ? "S" : " ");
    Serial.print((mod.bmLeftAlt    == 1) ? "A" : " ");
    Serial.print((mod.bmLeftGUI    == 1) ? "G" : " ");

   // Serial.print(" >");
  //  PrintHex<uint8_t>(key, 0x80);
  //  Serial.print("< ");

    Serial.print((mod.bmRightCtrl   == 1) ? "C" : " ");
    Serial.print((mod.bmRightShift  == 1) ? "S" : " ");
    Serial.print((mod.bmRightAlt    == 1) ? "A" : " ");
    Serial.println((mod.bmRightGUI    == 1) ? "G" : " ");
};

// Map some of the keys currently not handled by OemToAscii
uint8_t KbdRptParser::mapExtraAsciiKeys(uint8_t key) {

  switch(key) {
      case UHS_HID_BOOT_KEY_SPACE: return ' ';
    //  case UHS_HID_BOOT_KEY_ZERO2: return '0';
     // case UHS_HID_BOOT_KEY_PERIOD: return '.';
    //  case UHS_HID_BOOT_KEY_ESC: return 'e';
     // case UHS_HID_BOOT_KEY_DEL: return 'd';
      case UHS_HID_BOOT_KEY_BACKSPACE: return 42;
    //  case UHS_HID_BOOT_KEY_TAB: return ;
  }

  return (0);
}


void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  //  Serial.print("DN ");
  //  PrintKey(mod, key);
  if((key <= 82 && key >= 79) || (key >= 58 && key <= 69) || key == 41 || key == 76 || key == 99 || key == 40 || key == 88 || key == 42 || key == 43 || key == 75 || key == 74 || key == 78 || key == 77 || key == 72 || key == 70)
{  
 // String S0 = "K";
 // String S1 = String(key);
  Serial1.println(key); 
  }
  else{
    uint8_t c = OemToAscii(mod, key);
    
    if (!c) {
      c = mapExtraAsciiKeys(key);
    }

    if (c) {
        OnKeyPressed(c);
    }
    }

}

void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {

    MODIFIERKEYS beforeMod;
    *((uint8_t*)&beforeMod) = before;

    MODIFIERKEYS afterMod;
    *((uint8_t*)&afterMod) = after;

    if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
        Serial.println("LeftCtrl changed");
    }
    if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
        Serial.println("LeftShift changed");
    }
    if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
        Serial.println("LeftAlt changed");
    }
    if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
        Serial.println("LeftGUI changed");
    }

    if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
        Serial.println("RightCtrl changed");
    }
    if (beforeMod.bmRightShift != afterMod.bmRightShift) {
        Serial.println("RightShift changed");
    }
    if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
        Serial.println("RightAlt changed");
    }
    if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
        Serial.println("RightGUI changed");
    }

}

void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
  //  Serial.print("UP ");
  //  PrintKey(mod, key);
}

void KbdRptParser::OnKeyPressed(uint8_t key)
{
  //  Serial.print("ASCII: ");
    Serial1.println((char)key);
};


USB Usb;
USBHub  Hub(&Usb);



HIDBoot<HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);  //Keyboard
KbdRptParser Prs;



USBH_MIDI  Midi(&Usb);  //Printer


BulkOnly bulk(&Usb);   //Flash drive
UsbFat key1(&bulk);  

FatFile  file;
File file2;
File file3;




bool iRun1 = false;
bool iRun2 = false;




USB_DEVICE_DESCRIPTOR buf;
USB_DEVICE_DESCRIPTOR buf2;
bool isVariable = false;



SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> spi1;

void setup()
{
  Serial.begin(115200);  
  Serial1.begin(115200);
  Serial2.begin(9600);  
  Serial.println("Start");
  if (!initUSB(&Usb)) {}
  HidKeyboard.SetReportParser(0, &Prs);     

  
   initLCD();
   
}

void loop()
{    
   
    Usb.Task();
    delayMicroseconds(10);
    fLCD1();
}

void fLCD1(){
Usb.Task();
 while(Serial1.available())
   {
   s = Serial1.readStringUntil('\n');
   s= s.substring(0,s.length()-1);
   if(s.length()>0)
   {
     if(s == "^flash")
     {
       FlashFunc();
      isReady=false;
     }else{
      isReady=true;
     }
   }
   else {
   isReady=false;
   }
    if(isReady)
    {
      switch(s[0])
      {        
         case '

Could any one of you please help me regarding this?

Thank You
Vamshi: // LCD        
        textprintf(s[1],s[2],s.substring(3));      
        break;
        case '^':
         if(drive_mode == 1) {
       
        fDrive(s.substring(1));
       
         }
         
        break;
        case '`':
     
        PRINTER_request(s.substring(1));
       
        break;
       
        default:
         Usb.Task();                              
        break;
     }
 
  isReady=false;
   }
  }
  if(Serial2.available())
  {
     count = 0;
     while(Serial2.available() && count < 12)          // Read 12 characters and store them in input array
     {
        input[count] = Serial2.read();
        count++;
        delay(5);
     }
     Serial1.print(input);
  }
}


Could any one of you please help me regarding this?

Thank You
Vamshi

When I try to make something out of device which is declared at last, I am getting the error "USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;".

can you identify which line of code you believe is causing the error and where whatever is "declared at last"?

Thank you for the quick response.

The first three declarations are as follows:

USB Usb;
USBHub Hub(&Usb);

HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb); //Keyboard
KbdRptParser Prs;

USBH_MIDI Midi(&Usb); //Printer

BulkOnly bulk(&Usb); //Flash drive
UsbFat key1(&bulk);

FatFile file;
File file2;
File file3;

Here the first two declarations keyboard and printer are working fine. When it comes to flash drive, the third declaration, the function Usb.outTransfer() is returning "USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;".