pq113_6的个人空间 https://bbs.21ic.com/?51690 [收藏] [复制] [RSS]

日志

VNC2 V2DAP学习笔记 9-与FTDI USB转串口芯片的通信

已有 624 次阅读2019-1-25 15:10 |个人分类:USB桥接|系统分类:原稿与翻译

V2DAP不仅可以读写U盘,还可以作为HOSTFTDIUSB转串口的芯片进行通信。以最广泛使用的FT232R为例,即实现MCUFT232R的功能。

首先用PCUSBUARTVNC2模块,进入IPA(0x90)模式。发送的是短命令格式。

1.      FT232R模块插入VNC2模块的USB HOST2端口,并将TXDRXD短接做回路测试。

2.      发送Query Port命令查询是否有设备,USB HOST1对应的命令是QP1(0x2B)HOST2对应的接口是QP2(0x2C).

如果没有插入设备,串口返回$00 $00,第一个参数表示设备类型,第二个参数一直为0x00。设备类型定义如下:

Bit
设备类型
7
Hub设备
6
未知设备
5
BOMS类设备
4
CDC类设备
3
HID类设备
2
打印机类设备
1
保留(一直为0)
0
FTDI设备


3.      
发送Query Device查询设备信息,带一个参数,有效值为0-15,表示设备号。返回32字节,含义如下:

对应的命令是QD(0x85)

4.      发送Set Current 命令SC(0x86)选择设备

5.      发送Set Baud Rate命令FBD(0x18)设置波特率,波特率对应的数值表如下

波特率
1byte
2byte
3byte
300
0x10
0x27
0x00
600
0x88
0x13
0x00 

1200

0xC4

0x09

0x00

2400

0xE2

0x04

0x00

4800

0x71

0x02

0x00

9600

0x38

0x41

0x00

19200

0x9C

0x80

0x00
38400
0x4E
0xC0
0x00
576000x34

0xC0

0x00

115200

0x1A

0x00

0x00

230400

0x0D

0x00

0x00

460800

0x06

0x40

0x00

921600

0x03

0x80

0x00

1000000

0x03

0x00

0x00

1500000

0x02

0x00

0x00

2000000

0x01

0x00

0x00

3000000

0x00

0x00

0x00

例如,设置波特率为9600

6.      发送Set Modem Control命令FMC(0x19)设置modem控制字

功能

1byte
2byte
DTR# State 0 = off, 1 = on

0 

X

RTS# State 0 = off, 1 = on

1

X

1 = change DTR, 0 = leave DTR alone

X

0

1 = change RTS, 0 = leave RTS alone

X

1

例如,设置不使用控制字

7.      发送Set Data Characteristics命令FSD(0x1A)配置数据特性

第一个字节表示数据bit数,7表示数据是7bits8表示数据是8bits.

第二个字节每个bit的含义如下:

Bit

含义

2:0

Parity :

0 – none

1 – odd

2 – even

3 – mark

4 - space

5:3


  

Number of Stop bits :

0 - 1 stop bit

1 - 1 stop bit

2 - 2 stop bits

6

  
1 = Send break

0 = Stop break

7
保留,为0

8.      发送Device Send Data命令DSD(0x83)发送数据,第一个字节是发送数据的长度,回车开始发送数据。

发送1个字节数据0xAAFT232R

9.      发送Device Read Data命令DRD(0x84)接收数据。

因为是回路测试,发送84 0D后系统先返回OK信号3E 0D(>+回车),然后返回能接收的数据个数+回车,最后是数据0xAA

 

MCU端软件设计类似

1.      同步并设置为短命令格式

while(1)

{

         Printf("sync vnc2 start\n");

         do

         {

                  monCmdSend('E');

                   resp = monPrompt();

         }while (resp != Resp_Prompt_UE);

         Printf("sync E OK\n");

         monCmdSend('e');

         resp = monPrompt();

         if (resp == Resp_Prompt_LE)

         {

                   Printf("sync e OK\n");

                   // change to Short Command Set Mode

                   monCmdSend(CMD_SCS);

                   resp = monPrompt();

                   if (resp == Resp_Prompt_OK)

                   {

                            break;

                   }

         }

}

Printf("vnc2 sync ok\n");

2.      判断是不是FT232R设备插入USB HOST2

#define FTDI_VID                                  0x0403

#define DLP_TILT_VID                         FTDI_VID

#define DLP_TILT_PID                         0x6001

enum tLedState checkFT232R()

{

enum vResponse resp;

enum tLedState state;

unsigned char dev;

unsigned char qdBuf[MON_QD_SIZE];

char i;

for (dev = 0; dev < 16; dev++)

{

           // send query device command

           monCmdSendParam(CMD_QD, 1, &dev);

           for (i = 0; i < MON_QD_SIZE; i++)

           {

                    qdBuf[i] = monReadWait();

           }

           resp = monPrompt();

           if (resp == Resp_Prompt_OK)

           {

                    // USB device at this device number

                    if (qdBuf[0])

                   {

                             // check VID is same as DLT-TILT

                             Printf("VID:0x%04x, ", (uint16_t)qdBuf[MON_QD_VID+1] << 8 | qdBuf[MON_QD_VID]);

                             Printf("PID:0x%04x\n", (uint16_t)qdBuf[MON_QD_PID+1] << 8 | qdBuf[MON_QD_PID]);

                             if ((qdBuf[MON_QD_VID] == (DLP_TILT_VID & 0xff)) &&

                                       (qdBuf[MON_QD_VID+1] == (DLP_TILT_VID >> 8)))

                             {

                                       // check PID is same as DLT-TILT

                                       if ((qdBuf[MON_QD_PID  ] == (DLP_TILT_PID & 0xff)) &&

                                                (qdBuf[MON_QD_PID+1] == (DLP_TILT_PID >> 8)))

                                       {

                                                //found DLP-TILT

                                                //set current to device

                                                monCmdSendParam(CMD_SC, 1, &dev);

                                                resp = monPrompt();

                                                if (resp == Resp_Prompt_OK)

                                                {

                                                         state = stReady;

                                                         break;

                                                }

                                       }

                             }

                    }

           }

}

if (dev == 16)

{

           state = stDeviceError;

}

return state;

}

3.      设置波特率

param[0] = 0x38;

param[1] = 0x41;

param[2] = 0x00;

monCmdSendParam(CMD_FBD, 3, param);

resp = monPrompt();

if (resp != Resp_Prompt_OK)

{

delayms(1000);         

continue;

}

Printf("Set Baud Rate OK\n");

4.      设置modem control字节

param[0] = 0x00;

param[1] = 0x00;

monCmdSendParam(CMD_FMC, 2, param);

resp = monPrompt();

if (resp != Resp_Prompt_OK)

{

delayms(1000);         

continue;

}

Printf("Set Modem Control OK\n");

5.      设置数据特性

param[0] = 0x08;

param[1] = 0x00;

monCmdSendParam(CMD_FSD, 2, param);

resp = monPrompt();

if (resp != Resp_Prompt_OK)

{

delayms(1000);         

continue;

}

Printf("Set Data Characteristic OK\n");

6.      发送串口数据

param[0] = 0x0a;

monCmdSendParam(CMD_DSD, 1, param);

for(i = 0; i < 10; i++)

{

monSendByte(i);

}

resp = monPrompt();

if (resp != Resp_Prompt_OK)

{

delayms(1000);         

continue;

}

Printf("Send Uart Data OK\n");

7.      接收串口数据

monCmdSend(CMD_DRD);

monCmdSend(0x0a);

param[0] = monReadWait();

param[1] = monReadWait();

Printf("Read data:");

if(param[0] == 10)

{

         for(i = 0; i < 10; i++)

         {

                   param[0] = monReadWait();

                   Printf("0x%02x ", param[0]);

         }

         Printf("\n");

}

else

{

         Printf("Read Fail\n");

 }

测试结果如下:

Uart Init OK

sync vnc2 start

sync E OK

sync e OK

vnc2 sync ok

VID:0x0403, PID:0x6001

check FT232R chip ready

Set Baud Rate OK

Set Modem Control OK

Set Data Characteristic OK

Send Uart Data OK

Read data:0x00 0x01
0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09

评论 (0 个评论)