在很多应用场合,我们需要将数据传输到PC端进行数据分析处理,最常用的当然就是串口了,但是大家也知道,串口有个最大的问题就是速度问题;
为解决这个问题,这个高速版本的USB2SPI就出来了;
1,USB传输采用高速USB(480Mbps);
2,两路硬件SPI,每一路最大接收数据速度可高达50Mbps;
3,SPI采用DMA环形循环接收,每一路SPI高达40K数据缓冲区,可以防止丢数据;
4,PC端采用回调函数方式接收数据,也就是启动数据接收后,若接收到了数据,会自动进入回调函数里面,用户在回调函数里面处理数据即可;
5,支持Windows,Linux,Android;
从机接收到数据之后将数据写入文件中的程序:
- /*
- ******************************************************************************
- * @file : USB2XXXSPISlaveReceiveFile.cpp
- * @Copyright: usbxyz
- * @Revision : ver 1.0
- * @Date : 2014/12/19 9:33
- * @brief : USB2XXX SPI slave receive file test demo
- ******************************************************************************
- * @attention
- *
- * Copyright 2009-2014, usbxyz.com
- * http://www.usbxyz.com/
- * All Rights Reserved
- *
- ******************************************************************************
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "usb_device.h"
- #include "usb2spi.h"
- #define USE_CALLBACK_RECEIVE_DATA 1
- FILE *pFile;
- int FileSize = 0;
- #if USE_CALLBACK_RECEIVE_DATA
- int __stdcall SlaveGetData(int DevHandle,int SPIIndex,unsigned char *pData,int DataNum)
- {
- fwrite(pData,1,DataNum,pFile);
- FileSize += DataNum;
- printf("Get data num = %d
",DataNum);
- return 0;
- }
- #endif
- int main(int argc, const char* argv[])
- {
- DEVICE_INFO DevInfo;
- SPI_CONFIG SPIConfig;
- int DevHandle[10];
- int SPIIndex = SPI1_CS0;
- bool state;
- int ret;
- unsigned char ReadBuffer[20480];
- //扫描查找设备
- ret = USB_ScanDevice(DevHandle);
- if(ret <= 0){
- printf("No device connected!
");
- getchar();
- return 0;
- }
- //打开设备
- state = USB_OpenDevice(DevHandle[0]);
- if(!state){
- printf("Open device error!
");
- getchar();
- return 0;
- }
- //输入SPI相关参数,必须和主机匹配
- int DataTemp;
- printf("Please input CPHA(0 or 1):");
- scanf("%d",&DataTemp);
- SPIConfig.CPHA = DataTemp;
- printf("Please input CPOL(0 or 1):");
- scanf("%d",&DataTemp);
- SPIConfig.CPOL = DataTemp;
- printf("SPIConfig.CPHA = %d
",SPIConfig.CPHA);
- printf("SPIConfig.CPOL = %d
",SPIConfig.CPOL);
- //配置SPI总线相关参数(配置为从机模式)
- SPIConfig.Mode = SPI_MODE_HARD_FDX;
- SPIConfig.ClockSpeedHz = 50000000;
- //SPIConfig.CPHA = 0;
- //SPIConfig.CPOL = 0;
- SPIConfig.LSBFirst = SPI_MSB;
- SPIConfig.Master = SPI_SLAVE;
- SPIConfig.SelPolarity = SPI_SEL_LOW;
- ret = SPI_Init(DevHandle[0],SPIIndex,&SPIConfig);
- if(ret != SPI_SUCCESS){
- printf("Initialize device error!
");
- getchar();
- return 0;
- }
- //输入文件名
- printf("Please input file name:");
- char FileName[512]={0};
- scanf("%s",FileName);
- printf("Start receive data to file,the file name is %s
",FileName);
- printf("Press any key to exit the data reception!
");
- pFile=fopen(FileName,"wb"); //获取文件的指针
- if(pFile == NULL){
- printf("Open file faild
");
- getchar();
- return 0;
- }
- #if USE_CALLBACK_RECEIVE_DATA
- SPI_SlaveContinueRead(DevHandle[0],SPIIndex,SlaveGetData);
- getchar();
- getchar();
- SPI_SlaveContinueReadStop(DevHandle[0],SPIIndex);
- #else
- while(true)
- {
- unsigned char DataBuffer[40960];
- int DataNum = SPI_SlaveReadBytes(DevHandle[0],SPIIndex,DataBuffer,0);
- if(DataNum > 0){
- fwrite(DataBuffer,1,DataNum,pFile);
- FileSize += DataNum;
- printf("Get data num = %d
",DataNum);
- printf("FileSize = %d Byte
",FileSize);
- }else if(DataNum < 0){
- break;
- }
- //延时
- #ifndef OS_UNIX
- Sleep(900);
- #else
- usleep(900*1000);
- #endif
- }
- #endif
- USB_CloseDevice(DevHandle[0]);
- fclose(pFile);
- printf("FileSize = %d Byte
",FileSize);
- printf("Stop receive!
");
- getchar();
- return 0;
- }
复制代码
测试工程下载:
USB2XXXSPISlaveReceiveFile.rar
(650.1 KB, 下载次数: 55)
2017-5-19 12:10 上传
点击文件名下载附件
适配器购买链接:
https://item.taobao.com/item.htm ... amp;id=545509364910
双路50Mbit就是有100Mbit了,加上超大的缓冲区。
不过ft232h是支持8Mbyte的SPI,也就是40Mbit的。并口支持40 Mbytes/Sec。但是发送和接收缓冲区分别只有1K。
一周热门 更多>