реклама на сайте
подробности

 
 
> Рабочий пример работы SL811 в режиме host'a
Hexxx
сообщение Apr 10 2007, 15:14
Сообщение #1


Участник
*

Группа: Свой
Сообщений: 49
Регистрация: 29-03-06
Пользователь №: 15 592



Тема поднималась милион раз, но никто так и не удосужился запостить пример. Значит буду первым:
Код
#define MASTER_MODE             0x80

void SL811_Init(void)
{    
    SL811Write(cDATASet, 0xe0); //SOF Timeout Value
    SL811Write(cSOFcnt, 0x2e | MASTER_MODE);
    SL811Write(CtrlReg, 0x5);            
    SL811Write(EP0Status, PID_SOF);
    SL811Write(EP0Counter, 0);              //Set Endpoint Zero
    SL811Write(EP0Control, 0x01);            //Start the SOF
    SL811Write(IntEna, INSERT_REMOVE);      //USB-A, Insert/Remove, USB_Resume.
    SL811Write(IntStatus, INT_CLEAR);    //Clear Interrupt enable status
    
}

#define NO_ERROR 0
#define ERROR_TIMEOUT 1
#define ERROR_STALL 2
#define ERROR_OVERFLOW 3
#define ERROR_EP0_ERROR 4
#define ERROR_UNKNOWN 5


int SendData(void * pData, int DataSize)
{
  unsigned char result, intr, remainder,timeout;
  SL811BufWrite(EP0_Buf,pData,DataSize);

  SL811Write(EP0Status,(PID_SETUP | 0));  // PID + EP address
  SL811Write(EP0Counter,0);               // USB address
  SL811Write(EP0Address,EP0_Buf);         // buffer address, start with "data0"
  SL811Write(EP0XferLen,DataSize);        // data transfer length
  SL811Write(IntStatus,INT_CLEAR);        // clear interrupt status
  SL811Write(EP0Control,DATA0_WR);  

  timeout=10;
  while(1)
  {
    do
    {                                      
      intr = SL811Read(IntStatus);          // wait for interrupt to be done
      if((intr & USB_RESET) || (intr & INSERT_REMOVE))  //leave if device is removed
        return 1;                  
              
    } while (!(intr & USB_A_DONE)); // interrupt done !!!
  
    SL811Write(IntStatus,INT_CLEAR);         // clear interrupt status
    result = SL811Read(EP0Status);           // read EP0status register
    remainder = SL811Read(EP0Counter);       // remainder value in last pkt xfer
  
    if (result & EP0_ACK)
      return 0;  
    
    if (result & EP0_NAK)                  // NAK Detected
    {                            
        SL811Write(IntStatus,INT_CLEAR);        // clear interrupt status, need to
        SL811Write(EP0Control,DATA0_WR);            // re-arm and request for last cmd, IN token
        result = 0;                                     // respond to NAK status only
    }
    
    if (result & EP0_TIMEOUT)                // TIMEOUT Detected
    {                            
        if(timeout)
        {  
            timeout--;
        }
        else
          return ERROR_TIMEOUT;                    // exit on the timeout detected  
        
        SL811Write(IntStatus,INT_CLEAR);        // clear interrupt status, need to
        SL811Write(EP0Control,DATA0_WR);            // re-arm and request for last cmd again
    }
    
    if (result & EP0_STALL)                  // STALL detected
      return ERROR_STALL;                    // for unsupported request.
    
    if (result & EP0_OVERFLOW)                // OVERFLOW detected
      return ERROR_OVERFLOW;  
    
    if (result & EP0_ERROR)                  // ERROR detected
      return ERROR_EP0_ERROR;
  }
  
  
  return ERROR_UNKNOWN;
}

int GetReply(void * pData, int DataSize)
{
  unsigned char result, intr, remainder,timeout;
  
  SL811Write(EP0Status,(PID_IN | 0));  // PID + EP address
  SL811Write(EP0Counter,0);               // USB address
  SL811Write(EP0Address,EP0_Buf);         // buffer address, start with "data0"
  SL811Write(EP0XferLen,DataSize);        // data transfer length
  SL811Write(IntStatus,INT_CLEAR);        // clear interrupt status
  SL811Write(EP0Control,DATA0_RD);  
  
  timeout=10;
  while(1)
  {
    do
    {                                      
      intr = SL811Read(IntStatus);          // wait for interrupt to be done
      if((intr & USB_RESET) || (intr & INSERT_REMOVE))  //leave if device is removed
        return 1;                  
              
    } while (!(intr & USB_A_DONE)); // interrupt done !!!
  
    SL811Write(IntStatus,INT_CLEAR);         // clear interrupt status
    result = SL811Read(EP0Status);           // read EP0status register
    remainder = SL811Read(EP0Counter);       // remainder value in last pkt xfer
  
    if (result & EP0_ACK)
    {
      if(remainder!=DataSize)              
      {
        SL811BufRead(EP0_Buf, pData, DataSize);
        return 0;
      }
    }
    
    if (result & EP0_NAK)                  // NAK Detected
    {                            
        SL811Write(IntStatus,INT_CLEAR);        // clear interrupt status, need to
        SL811Write(EP0Control,DATA0_WR);            // re-arm and request for last cmd, IN token
        result = 0;                                     // respond to NAK status only
    }
    
    if (result & EP0_TIMEOUT)                // TIMEOUT Detected
    {                            
        if(timeout)
        {  
            timeout--;
        }
        else
          return ERROR_TIMEOUT;                    // exit on the timeout detected  
        
        SL811Write(IntStatus,INT_CLEAR);        // clear interrupt status, need to
        SL811Write(EP0Control,DATA0_WR);            // re-arm and request for last cmd again
    }
    
    if (result & EP0_STALL)                  // STALL detected
      return ERROR_STALL;                    // for unsupported request.
    
    if (result & EP0_OVERFLOW)                // OVERFLOW detected
      return ERROR_OVERFLOW;  
    
    if (result & EP0_ERROR)                  // ERROR detected
      return ERROR_EP0_ERROR;
  }
  
  return ERROR_UNKNOWN;
}

unsigned char Buffer[0x64];
void testSL811(void)
{
  int error;
  SetupPKG pkg;
  pkg.bmRequest=0x80;
  pkg.bRequest=0x06;
  pkg.wValue=0x200;
  pkg.wIndex=0x00;
  pkg.wLength=0xFF00;
  
  SL811ClockInit();
  SL811_Init();
  
  error=SendData(&pkg,sizeof(pkg));
  if (!error)
  {
    error=GetReply(Buffer,0x9);  
  }

}


Код не идеальный, надо доделывать функцию чтения. Он инитит хост в режиме low-speed и позволяет вам послать SETUP пакет GET_DESCRIPTOR и вычитать 9-байтов ответа от Slave устройства. Все нехватающие функции и define есть в cy3662/EZ811 development kit'e. Его можно взять вот тут
Go to the top of the page
 
+Quote Post
 
Start new topic
Ответов
Hexxx
сообщение May 11 2007, 13:17
Сообщение #2


Участник
*

Группа: Свой
Сообщений: 49
Регистрация: 29-03-06
Пользователь №: 15 592



Я тут тоже по этому вопросу мучаю разных людей. Один мне на примере АТ команд для мобил вот что ответил:

> If the data of one package is less than payload size, it means cellphone has no other data to return.

т.е. если в количество непрочитаных байт(remainder) != 0, значит телефон еще не начал отвечать.

> If the data of one package is equal to payload size. It means cellphone has more data, or has no more data. You should read the endpoint 0x8a again.

Если количество непрочитаных байт == 0 (remainder), значит надо вычитать те данные, которые есть IN endpoint'е, а потом пытаться читать еще, чтобы проверить это все или нет.

> If cellphone return ACK, you should receive the following data.
Если когда мы прочитали повторно, девайс сказал ACK - вычитываем то что есть, и читаем снова.

> If cellphone return NAK, it means cellphone has no other data to return by now.
Если девайс сказал NAK - данные закончились.
Go to the top of the page
 
+Quote Post

Сообщений в этой теме
- Hexxx   Рабочий пример работы SL811 в режиме host'a   Apr 10 2007, 15:14
- - Hexxx   Я понял как заставить работать пример из EZ811 DK...   Apr 18 2007, 17:28
- - Hexxx   И все таки он глючит на чтении. Люди помогите, уже...   Apr 19 2007, 14:59
- - Hexxx   Так странно, куча людей. У многих уже проблемы с в...   Apr 20 2007, 11:51
- - Hexxx   Отвечает Александр Друзь! Payload - это масим...   Apr 23 2007, 12:40
- - Hexxx   Эпопея продолжается. Bulk-transfer не пашет. Отпра...   May 3 2007, 13:07
|- - misyachniy   Не знаю что и посоветовать. У меня был затык в том...   May 3 2007, 16:06
- - Hexxx   А меня опять оказался баг с переключением DATA0/DA...   May 4 2007, 12:08
- - Hexxx   Начал пробовать подключать разные устройства. Кто-...   May 7 2007, 08:32
- - Hexxx   А вообще когда проставляется TIMEOUT?   May 7 2007, 13:49
- - Hexxx   ап   May 8 2007, 13:41
|- - Ailinor   Я решил так: int Read(unsigned char *buff) { ...   May 10 2007, 15:18
- - Hexxx   Кое-что еще выяснилось на счет таймаутов. Если де...   May 17 2007, 05:05
- - Hexxx   А фишка оказалась была в том, что я не делал SetAd...   Jun 18 2007, 12:58


Reply to this topicStart new topic
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 


RSS Текстовая версия Сейчас: 25th July 2025 - 11:02
Рейтинг@Mail.ru


Страница сгенерированна за 0.01305 секунд с 7
ELECTRONIX ©2004-2016