八月份国赛比完,原计划开始的嵌入式Linux学习一直拖到了现在;由于之前所有的开发全在Windows下进行的,对各种底层完全不清楚,刚好这段时间开始学习Linux,我就在想能不能把开发环境给迁移到Linux中来,就这样,有了这篇博客的产生;本人现在是应届大四学生,这是第一次写博客,有啥问题还望各位大佬们指教。
    闲话少说,首先介绍下我的硬件环境,我使用的是天嵌科技的TQ2440开发板,软件环境是安装了交叉编译链的Ubuntu 16.04版本。
    我们使用DNW向开发板烧写程序其实需要经过两个步骤,在串口终端中根据指令选择要下载的程序,然后在使用DNW工具进行写入。我们首先安装串口工具minicom,他实现的功能跟SecureCRT类似,我们直接用sodo apt-get install minicom命令安装它。
    1.安装minicom

sudo apt-get install minicom

配置minicom:

sudo minicom -s        // 注意:安装的前面一定要sudo,不然后面配置文件会出现不能写入的问题。

回车进入minicom的配置页面,选择Serial port setup,进入后可以看到一共有ABCDEFG七项配置,我们主要设置第一项设置设备,第五项波特率,第六七相由于我们一般用的都是USB转串口设备,所有都设置为No;第一项是串口好,如果我们使用的USB转串口的话可以在另一个终端中使用ls -l  /dev/ttyUSB*指令来查看我们的USB设备(我使用的USB转串口与开发板连接,所以选择USB 设备),在minicom配置界面输入A修改成我们查到的端口。回车后在输入E修改波特率,数据位、停止位;修改完成功后回车退出修改界面。选择Save setup as df1保存。最后退出,我们的minicom就已经配置好了;输入 sudo minicom 就可以打开minicom使用了。可以在minicom中同时按Crtrl+,然后按A查看帮助,X退出。(注意:minicom必须使用sudo打开)

minicom配置

minicom打开后的界面(串口没数据过来)

minicom配置完了,接下来就该安装程序下载到目标板的最后步骤了,首先要安装libusb-dev这个库;直接在输入指令 sudo apt-get install libusb-dev 装完之后就编译下载工具,网上牛人提供的一个,代码如下:

#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> #define QQ2440_SECBULK_IDVENDOR 0x5345
#define QQ2440_SECBULK_IDPRODUCT 0x1234 struct usb_dev_handle * open_port()
{
struct usb_bus *busses, *bus; usb_init();
usb_find_busses();
usb_find_devices(); busses = usb_get_busses();
for(bus=busses;bus;bus=bus->next)
{
struct usb_device *dev;
for(dev=bus->devices;dev;dev=dev->next)
{
printf("idVendor:0x%x/t,ipProduct:0x%x/n",dev->descriptor.idVendor,dev->descriptor.idProduct);
if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
&& QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
{
printf("Target usb device found!/n");
struct usb_dev_handle *hdev = usb_open(dev);
if(!hdev)
{
perror("Cannot open device");
}
else
{
if(0!=usb_claim_interface(hdev, 0))
{
perror("Cannot claim interface");
usb_close(hdev);
hdev = NULL;
}
}
return hdev;
}
}
} printf("Target usb device not found!/n"); return NULL;
} void usage()
{
printf("Usage: dnw2 <file>/n/n");
} unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
unsigned char *write_buf = NULL;
struct stat fs; int fd = open(filename, O_RDONLY);
if(-1==fd)
{
perror("Cannot open file");
return NULL;
}
if(-1==fstat(fd, &fs))
{
perror("Cannot get file size");
goto error;
}
write_buf = (unsigned char*)malloc(fs.st_size+10);
if(NULL==write_buf)
{
perror("malloc failed");
goto error;
} if(fs.st_size != read(fd, write_buf+8, fs.st_size))
{
perror("Reading file failed");
goto error;
} printf("Filename : %s/n", filename);
printf("Filesize : %d bytes/n", fs.st_size); *((u_int32_t*)write_buf) = 0x30000000; //download address
*((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size; *len = fs.st_size + 10;
return write_buf; error:
if(fd!=-1) close(fd);
if(NULL!=write_buf) free(write_buf);
fs.st_size = 0;
return NULL; } int main(int argc, char *argv[])
{
if(2!=argc)
{
usage();
return 1;
} struct usb_dev_handle *hdev = open_port();
if(!hdev)
{
return 1;
} unsigned int len = 0;
unsigned char* write_buf = prepare_write_buf(argv[1], &len);
if(NULL==write_buf) return 1; unsigned int remain = len;
unsigned int towrite;
printf("Writing data .../n");
while(remain)
{
towrite = remain>512 ? 512 : remain;
if(towrite != usb_bulk_write(hdev, 0x03, write_buf+(len-remain), towrite, 3000))
{
perror("usb_bulk_write failed");
break;
}
remain-=towrite;
printf("/r%d%/t %d bytes ", (len-remain)*100/len, len-remain);
fflush(stdout);
}
if(0==remain) printf("Done!/n");
return 0;
}

1)将上面代码保存为文件如: dnw1.c
2)接着编译: 

gcc dnw1.c -o dnw1 -l usb

3)移动dnw1可执行文件到/usr/bin目录下

sudo mv ./dnw1 /usr/bin

4)执行

sudo dnw1 path/your_filename

这样整个下载环境就安装完了,接下来就该演示下载过程了,首先将开发板的USB口跟串口分别与电脑相连,然后上电,打开minicom

sudo minicom

按下开发板的复位键,我们就能看见开发板的下载选项了

输入下载的内容选项,我这里随便下载一个裸机程序

我们可以看见,USB已经连接成功,正在等待下载....

然后我们打开一个新的终端,输入指令 sudo dnw1 +你要发送的文件(我这里是用的DNW2  ),例如:

sudo dnw1 led.bin

可以看见下载已经完成。

最新文章

  1. cxLookupComboBox 控件
  2. Windows Store App 用户库文件分组
  3. ThroughRain第二次冲刺总结
  4. express的session函数
  5. ListView Optimization
  6. Painter 12安装教程
  7. hdu 4115 2-SAT判定
  8. Android 使用Application总结
  9. IOS7学习之路一(新UI之自定义UITableViewCell)
  10. Objective-C中的面向对象编程
  11. arm处理器
  12. 【python】函数返回值
  13. Could not create the view: An unexpected exception was thrown的解决方法
  14. 手写简单的jq雪花飘落
  15. day4 liaoxuefeng---高级特性
  16. 01-Git简介和仓库创建
  17. RabbitMQ 和 Kafka
  18. JSON Web Token(JWT)原理和用法介绍
  19. Spring MVC 中的输入验证 Vlidator
  20. 自学Aruba1.1-WLAN一些基本常识

热门文章

  1. Spring读书笔记——bean解析
  2. VS2010 c/c++ 本地化 emscripten 配置
  3. [Python]Codecombat攻略之地牢Kithgard(1-22关)
  4. php学习资料
  5. Django - - - -视图层之视图函数(views)
  6. Azure 基础:使用 powershell 创建虚拟机
  7. Linux.根据进程名关键字杀进程
  8. C#第二篇——关于C#中的正则表达式
  9. 初探 ELK - 每天5分钟玩转 Docker 容器技术(89)
  10. shell编程下 特殊变量、test / [ ]判断、循环、脚本排错