设置成executable
file就可以了。
参考部分测试代码:
BF548部分升级程序代码:
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
int main(int argc,char *argv[])
{
int
spi_fd;
int
i = 0;
int
iRet;
unsigned
char spiData[4];
int
mode;
unsigned
int fileFd;
struct stat fileStat;
int
filep;
spi_fd
= open("/dev/spidev2.33",O_RDWR);
if(spi_fd
< 0){
printf("can't
open spi_device\r\n");
return
-1;
}
mode
= 1;
iRet
= ioctl(spi_fd,SPI_IOC_WR_LSB_FIRST,&mode);
if (iRet == -1)
printf("can't set bits
lsb_first\n");
printf("read
spi device ok\n");
printf("raw
data is : 0x%08x\r\n",spiData);
filep
= 0;
fileFd
= open ( "/app/dsp12489.ldr", O_RDONLY);
if(fileFd
< 0){
printf("can't
open fileFd\r\n");
return
-1;
}
printf("read
fileFd ok\n");
fstat
(fileFd, &fileStat);
printf("/app/dsp12489.ldr
file size = %d\n",(int)(fileStat.st_size));
filep
= 0;
while(filep<fileStat.st_size)
{
//
boot kernel code
//app
code
read(fileFd,spiData,4);
write(spi_fd,spiData,4);
filep
+= 4;
}
printf("write
end\n");
return
0;
}
BF548测试程序代码:
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#if 1
int main(int argc,char *argv[])
{
int
spi_fd;
int
iRet;
int
mode;
unsigned
char spiData[4] = {0x12,0x34,0x56,0x78};
unsigned
char buf[5]={0};
spi_fd
= open("/dev/spidev2.33",O_RDWR);
if(spi_fd
< 0){
printf("can't
open spi_device\r\n");
return
-1;
}
printf("read
spi device ok\n");
mode
= 0;
iRet
= ioctl(spi_fd,SPI_IOC_WR_LSB_FIRST,&mode);
if (iRet == -1)
printf("can't set bits
msb_first per word");
while(1)
{
#if
1
if
(write(spi_fd,spiData,4) < 0) {
perror("write
Error");
return
-1;
}
#endif
if (read(spi_fd,buf,4) < 0) {
perror("read
Error");
return
-1;
}
printf("buf[0]=0x%02x,buf[1]=0x%02x,buf[2]=0x%02x,buf[3]=0x%02x\r\n",
buf[0],buf[1],buf[2],buf[3]);
sleep(1);
}
return
0;
}
#endif
注:测试代码速度会比较慢,40K的程序大概4s,修改程序即可。
3个spi升级程序代码示例,速度能达到秒速,45.2KB的程序。
升级程序:
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <sys/mman.h>
#include <string.h>
unsigned char *p[]={
"/dev/spidev2.33","/dev/spidev2.34","/dev/spidev2.35"
};
#if 1
int main(int argc,char *argv[])
{
int
spi_fd;
int
i = 0;
int
iRet;
unsigned
char spiData[256];
int
mode;
unsigned
int fileFd;
struct stat fileStat;
int
filep;
unsigned
char *mmapMem;
for(i
= 0 ;i < 3 ; i ++)
{
//spi_fd
= open("/dev/spidev2.33",O_RDWR);
spi_fd
= open(p[i],O_RDWR);
if(spi_fd
< 0){
printf("can't
open spi_device\r\n");
return
-1;
}
mode
= 1;
iRet
= ioctl(spi_fd,SPI_IOC_WR_LSB_FIRST,&mode);
if
(iRet == -1)
printf("can't
set bits lsb_first\n");
printf("read
spi device ok\n");
printf("raw
data is : 0x%08x\r\n",spiData);
filep
= 0;
fileFd
= open ( "/app/dsp12489.ldr", O_RDONLY);
if(fileFd
< 0){
printf("can't
open fileFd\r\n");
return
-1;
}
printf("read
fileFd ok\n");
fstat
(fileFd, &fileStat);
printf("/app/dsp12489.ldr
file size = %d\n",(int)(fileStat.st_size));
mmapMem
= mmap(NULL, fileStat.st_size, PROT_READ, MAP_PRIVATE, fileFd, 0);
filep
= 0;
while(filep<(fileStat.st_size
/ 256 * 256))
{
//
boot kernel code
//app
code
#if
0
//loop
again and again too slow,just read a big block to be fast.
read(fileFd,spiData,4);
write(spi_fd,spiData,4);
filep
+= 4;
#endif
memcpy(spiData,&mmapMem[filep],256);
write(spi_fd,spiData,256);
filep
+= 256;
}
if(filep
!= fileStat.st_size)
{
memcpy(spiData,&mmapMem[filep],fileStat.st_size
- filep);
write(spi_fd,spiData,fileStat.st_size
- filep);
}
printf("write
end\n");
close(fileFd);
munmap(mmapMem,
fileStat.st_size);
}
return
0;
}
#endif
spi的测试程序:
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <sys/mman.h>
#include <string.h>
unsigned char *p[]={
"/dev/spidev2.33","/dev/spidev2.34","/dev/spidev2.35"
};
int main(int argc,char *argv[])
{
int
spi_fd;
int
iRet;
int
mode;
int
i = 0;
unsigned
char spiData[4] = {0x12,0x34,0x56,0x78};
unsigned
char buf[5]={0};
for(i
= 0 ; i < 3 ;i ++)
{
spi_fd
= open(p[i],O_RDWR);
if(spi_fd
< 0){
printf("can't
open spi_device\r\n");
return
-1;
}
printf("read
spi device ok\n");
mode
= 0;
iRet
= ioctl(spi_fd,SPI_IOC_WR_LSB_FIRST,&mode);
if
(iRet == -1)
printf("can't
set bits msb_first per word");
if
(write(spi_fd,spiData,4) < 0) {
perror("write
Error");
return
-1;
}
if
(read(spi_fd,buf,4) < 0) {
perror("read
Error");
return
-1;
}
printf("%s
",p[i]);
printf("buf[0]=0x%02x,buf[1]=0x%02x,buf[2]=0x%02x,buf[3]=0x%02x\r\n",
buf[0],buf[1],buf[2],buf[3]);
memset(buf,0,sizeof(buf));
sleep(1);
}
return
0;
}