Ubuntu下用minicom搭建TQ2440的程序下载dnw环境

来源:互联网 发布:带英语的网络流行语 编辑:程序博客网 时间:2024/05/22 10:22
在之前就已经做了一半的工作了,也就是把minicom安装好了,而之后也不知道是为什么就不了了之了,很可能是因为dnw for linux的不成功。
今天想起上次跟哥说起为什么要在虚拟机开发而不直接在Linux系统下开发呢?同时今天也想要在ubuntu下学习,所以就索性在linux环境下开发的工作先搞定!

首先要多谢这位网友的文章,很不错很详细的:http://www.linuxidc.com/Linux/2011-03/32869.htm   Ubuntu下搭建TQ2440的程序下载环境

我这里是用ubuntu 10.10的,也差不多的。

安装minicom:sudo apt-get install minicom (这里也有网友说安装putty,有空试下http://linux.net527.cn/Ubuntu/Ubuntuanzhuangyuyingyong/140.html)

sudo minicom -s 这里是用管理员身份打开,也只能用管理员身份打开

之后检查/dev下有没有ttyUSB0驱动(这里是用串口转USB的),如果没有,就自行创建一个吧:sudo mknod /dev/ttyUSB0 c 188 0。在这里我是直接把USB口插进去自动生成,这个有关问题之后还要去探索的。

进入minicom进行配置:sudo minicom -s
在serial port setup中的A配置为/dev/ttyUSB0
再save setup as dfl,这是为了下次不用再配置

然后插上USB,重启minicom就可看到信息
Welcome to minicom 2.4

OPTIONS: I18n
Compiled on Jun  3 2010, 13:48:00.
Port /dev/ttyUSB0

Press CTRL-A Z for help on special keys

开发板在nor flash启动,插上USB host,按下选项即可出现USB host is connected. Waiting a download. 在这里一定是要先启动开发板之后再插上USB host才能检测到。

接下来是dnw for linux的问题了。

我们先新建一个名为dnw.c的文件,往里面复制以下代码:

/* dnw2 linux main file. This depends on libusb.

*

* Author: Fox <hulifox008@163.com>

* License: GPL

*

*/

#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;

}
保存之后就进行编译,这里要注意前面的信息,也就是关于usb.h的,在这里也纠结了一会,要在你的ubuntu系统中有安装libusb-dev才能认得了usb.h:sudo apt-get install libusb-dev这样在/usr/include下就有usb.h,而不要误认为/usr/include/linux下的usb.h就是你要的而把usb.h复制到对应的目录下
之后编译:gcc dnw.c -o dnw -lusb 即可生成dnw可执行文件
把dnw复制到工具目录下:sudo cp dnw /usr/local/bin
现在就可以使用dnw工具啦!

体验ubuntu环境下的下载程序:
1.连接好开发板,先连接USB转串口;
2.打开终端,在管理员身份下输入minicom,看到连接成功的提示;
3.从Nor Flash启动开发板,从minicom上看到u-boot的提示,再连接USB host,按1可看到USB host is connected. Waiting a download.的提示信息(这个是烧写u-boot程序到Nand Flash下);
4.在另一终端下运行dnw程序,并烧写u-boot.bin:sudo ./dnw u-boot.bin
到这里就烧写成功来,不过还是相对有点慢的,呵呵,没关系。好好体验下吧!


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 3岁宝宝老踢被子怎么办 4岁宝宝老踢被子怎么办 四线锁边机跳线怎么办 引流管伤口洞红怎么办 甘蔗卡在喉咙里怎么办 棉花被子生虫了怎么办 绗缝羽绒服钻毛怎么办 宝珠笔没墨水了怎么办 衣服上画的笔印怎么办 黑笔芯弄衣服上怎么办 圆珠笔油在皮上怎么办 不小心吞了水银怎么办 小孩吃了洗发露怎么办? 脸上被铅笔戳了怎么办 小孩吃了铅笔芯怎么办 小孩把橡皮吃了怎么办 用棉签掏耳朵里面疼怎么办 棉签头掉耳朵里怎么办 黑裤子老是粘毛怎么办 新买的裤子掉色怎么办 黑裤子容易粘毛怎么办 裤子粘了全部毛怎么办 纯棉裤子粘毛了怎么办 裤子粘毛怎么办怎么洗 黑裤子洗白了怎么办 新买床单有味道怎么办 新买的床单扎人怎么办 刚买的衣服皱了怎么办 橘子沾到衣服上怎么办 橘子水掉衣服上怎么办 菜汁滴在衣服上怎么办 饺子面和硬了怎么办 化纤衣服烫亮了怎么办 衣服穿久了发亮怎么办 买的毛衣长了怎么办 买的毛衣袖子长怎么办 新买的毛衣扎人怎么办 玻璃光纤线断了怎么办 家里光纤线断了怎么办 光纤线断了怎么办 找谁 光纤入户线断了怎么办