Use DNW to send programs to the Board under Linux. Includes driver code: SECBULK.C, Application code: DNW.C. Can only run on a 32-bit system and prompt for errors on 64-bit systems: DNW download Data size is too big.
DNW Source code:
#include <stdio.h>#include<stdlib.h>#include<malloc.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>#include<string.h>Const Char* Dev ="/dev/secbulk0";intMainintargcChar*argv[]) {unsignedChar* File_buffer =NULL; Long intAddr =0; if(3!=argc) {printf ("usage:dwn <filename> address\n"); return 1; } intFD = open (argv[1], o_rdonly); if(-1==FD) {printf ("Can not open file-%s\n", argv[1]); return 1; } addr= Strtol ((Char*) argv[2], NULL, -); printf ("addr =%lx \ n", addr); //Get File Size structstat File_stat; if( -1= = Fstat (FD, &File_stat)) {printf ("Get File Size filed!\n"); return 1; } File_buffer= (unsignedChar*) malloc (file_stat.st_size+Ten); if(NULL = =File_buffer) {printf ("malloc failed!\n"); Gotoerror; } //memset (File_buffer, ' n ', sizeof (File_buffer));//Bad code! Corrected by Qulorymemset (File_buffer,' /',sizeof(Char) * (file_stat.st_size+Ten)); //the first 8 bytes in the File_buffer are reserved, the last 2 bytes also; if(File_stat.st_size! = Read (FD, file_buffer+8, File_stat.st_size)) {printf ("Read file failed!\n"); Gotoerror; } printf ("File Name:%s\n", argv[1]); printf ("File Size:%ld bytes\n", file_stat.st_size);//off_t is long int intFd_dev =open (Dev, o_wronly); if( -1==Fd_dev) {printf ("Can not open%s\n", Dev); Gotoerror; } /** note:the first 4 bytes store the dest addr; * The following 4 bytes store the file size; * and the last 2 bytes store the sum of each bytes of the file; */* ((unsignedLong*) file_buffer) = addr;//Load Address* ((unsignedLong*) file_buffer+1) = file_stat.st_size+Ten;//File SizeUnsigned Shortsum =0; inti; for(i=8; I<file_stat.st_size+8; i++) {sum+=File_buffer[i]; } * ((unsigned Short*) (file_buffer+8+file_stat.st_size)) =sum; printf ("Start sending data...\n"); size_t remain_size= file_stat.st_size+Ten; size_t block_size= +; size_t written=0; while(Remain_size >0) {size_t to_write= remain_size > Block_size?block_size:remain_size; size_t Real_write= Write (Fd_dev, file_buffer+written, to_write); if(To_write! =real_write) {printf ("write/dev/secbulk0 failed!"); return 1; } remain_size-=To_write; Written+=To_write; printf ("\rsent%lu%% \%lu bytes!", written* -/(file_stat.st_size+Ten), written); Fflush (stdout); } printf ("ok\n"); return 0; Error:if(-1!=Fd_dev) {Close (Fd_dev); } if(FD! =-1) {close (FD); } if(NULL! =File_buffer) {free (file_buffer); } return-1;}
SECBULK.C Source code:
#include <linux/module.h>#include<linux/kernel.h>#include<linux/usb.h>#include<linux/fs.h>#include<linux/uaccess.h>#include<linux/slab.h>#defineBulkout_buffer_size 512Char*Bulkout_buffer;structUsb_device *udev;__u8 bulk_out_endaddr;Static structusb_device_id dnw_id_table [] ={{Usb_device (0x5345,0x1234) }, { } };Static intDnw_open (structinode* Inode,structFile *file) {Bulkout_buffer=Kmalloc (Bulkout_buffer_size,gfp_kernel); return 0;}Static intDnw_release (structinode* Inode,structFile *file) {Kfree (Bulkout_buffer); return 0; }Staticssize_t Dnw_write (structFile *file,Const Char__user *buf, size_t len, loff_t *POS) {size_t to_write; size_t Total_write=0; size_t Act_len; while(len>0) {To_write=min (len, (size_t) bulkout_buffer_size); Copy_from_user (Bulkout_buffer,buf+total_write,to_write); Usb_bulk_msg (Udev,usb_sndbulkpipe (UDEV,BULK_OUT_ENDADDR), Bulkout_buffer,to_write,&act_len,3*HZ); Len-=To_write; Total_write+=To_write; } returnTotal_write;}Static structFile_operations Dnw_ops ={. Owner=This_module,. Write=dnw_write,. Open=Dnw_open,. Release=Dnw_release,};Static structUsb_class_driver Dnw_class ={. Name="secbulk%d",. FoPs= &dnw_ops,. Minor_base= -,};Static intDnw_probe (structUsb_interface *intf,Const structUSB_DEVICE_ID *ID) { /*? Ó?úéè??? Èê? */ structUsb_host_interface *Interface; structUsb_endpoint_descriptor *endpoint; inti; Interface= intf->cur_altsetting; for(i=0;i<Interface->desc.bnumendpoints;i++) {Endpoint= &Interface-Endpoint[i].desc; if(Usb_endpoint_is_bulk_out (endpoint)) {bulk_out_endaddr= endpoint->bendpointaddress; Break; }} usb_register_dev (intf,&Dnw_class); Udev=Usb_get_dev (Interface_to_usbdev (intf)); }Static voidDnw_disconnect (structUsb_interface *intf) {Usb_deregister_dev (intf,&dnw_class);}structUsb_driver Dnw_driver ={. Name="DNW",/*? y?ˉ ??*/. Probe= Dnw_probe,/*2??? Oˉêy*/. Disconnect= Dnw_disconnect,/*D??? Oˉêy*/. Id_table= Dnw_id_table,/*Éè±?ád±í*/};intDnw_init (void) {Usb_register (&dnw_driver); return 0; }voidDnw_exit (void) {Usb_deregister (&dnw_driver); }module_init (Dnw_init); Module_exit (Dnw_exit); Module_license ("GPL");
Makefile Code:
Obj-m: = secbulk.okdir:=/lib/modules/' uname-r '/buildpwd:= $ (shell pwd) All: -C $ (kdir ) m=$ (PWD) Modulesclean: *.o* *.s* *.ko* *.mod.* *.k*
Application of DNW to arm Development Board in Linux system