Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72633
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 87
  • 用 户 组: 普通用户
  • 注册时间: 2014-12-06 22:00
文章分类

全部博文(7)

文章存档

2019年(4)

2018年(1)

2014年(2)

我的朋友

分类: LINUX

2019-01-27 22:18:55

硬件:树莓派2B+、BIOS烧录夹、杜邦线若干、东芝笔记本Satelite L50-AT11W1(BIOS是W25Q64FVSIG)
环境:树莓派装PiLFS(其他系统也行)、WiringPI

网上下载了笔记本的对应BIOS,改了开机logo,用fptw64刷不了,只能用烧录夹刷。
W25Q64是SPI接口,可以用树莓派的GPIO模拟,测试代码如下:
  1. #include<wiringPi.h>
  2. #include<stdio.h>
  3. #include<time.h>
  4. //1 /CS GPIO.9(3)
  5. //2 DO GPIO.13(9)
  6. //3 /WP GPIO.28(20)
  7. //4 GND GND
  8. //5 DI GPIO.12(10)
  9. //6 CLK GPIO.14(11)
  10. //7 /HOLD GPIO.40(21)
  11. //8 VCC GPIO.8(2)

  12. #define    VCC    2
  13. #define    CS    3
  14. #define    DI    10
  15. #define    DO    9
  16. #define    CLK    11
  17. #define    WP    20
  18. #define    HOLD    21

  19. void delay_x(int i){
  20.     int j;
  21.     for(j=0;j<i;j++);
  22. }

  23. void delay_ms(int i){
  24.     delay(i);
  25. }

  26. unsigned int spi_read_byte(){
  27.     int i,data;
  28.     data=0;
  29.     for(i=0;i<8;i++){
  30.         digitalWrite(CLK,0);
  31.         delay_x(1000);
  32.         digitalWrite(CLK,1);
  33.         delay_x(1000);
  34.         if(digitalRead(DO)){
  35.             data|=1<<(7-i);
  36.         }
  37.     }
  38.     return data;
  39. }

  40. void spi_write_byte(unsigned int data){
  41.     int i;
  42.     for(i=0;i<8;i++){
  43.         if(data&(1<<(7-i))){
  44.             digitalWrite(DI,1);
  45.         }else{
  46.             digitalWrite(DI,0);
  47.         }
  48.         digitalWrite(CLK,0);
  49.         delay_x(1000);
  50.         digitalWrite(CLK,1);
  51.         delay_x(1000);
  52.     }
  53. }

  54. void spi_id_read(){
  55.     digitalWrite(CS,0);
  56.     spi_write_byte(0x9f);
  57.     printf("%X\n",spi_read_byte());
  58.     printf("%X\n",spi_read_byte());
  59.     printf("%X\n",spi_read_byte());
  60.     digitalWrite(CS,1);
  61. }

  62. void spi_id_read1(){
  63.     digitalWrite(CS,0);
  64.     spi_write_byte(0x90);
  65.     spi_write_byte(0x00);
  66.     spi_write_byte(0x00);
  67.     spi_write_byte(0x00);
  68.     printf("%X\n",spi_read_byte());
  69.     printf("%X\n",spi_read_byte());
  70.     digitalWrite(CS,1);
  71. }

  72. void spi_reg_read(){
  73.     digitalWrite(CS,0);
  74.     spi_write_byte(0x05);
  75.     printf("%X\n",spi_read_byte());
  76.     digitalWrite(CS,1);
  77. }

  78. void spi_reg_read1(){
  79.     digitalWrite(CS,0);
  80.     spi_write_byte(0x35);
  81.     printf("%X\n",spi_read_byte());
  82.     digitalWrite(CS,1);
  83. }

  84. unsigned int spi_data_read(unsigned int addr){
  85.     int data;
  86.     digitalWrite(CS,0);
  87.     spi_write_byte(0x03);
  88.     spi_write_byte((addr>>16)&0xff);
  89.     spi_write_byte((addr>>8)&0xff);
  90.     spi_write_byte(addr&0xff);
  91.     data=spi_read_byte();
  92.     digitalWrite(CS,1);
  93.     return data;
  94. }

  95. void spi_data_write(unsigned int addr,unsigned int data){
  96.     digitalWrite(CS,0);
  97.     spi_write_byte(0x06);
  98.     digitalWrite(CS,1);
  99.     delay_ms(1);
  100.     digitalWrite(CS,0);
  101.     spi_write_byte(0x02);
  102.     spi_write_byte((addr>>16)&0xff);
  103.     spi_write_byte((addr>>8)&0xff);
  104.     spi_write_byte(addr&0xff);
  105.     spi_write_byte(data);
  106.     digitalWrite(CS,1);
  107.     delay_ms(1);
  108. }

  109. void spi_chip_erase(){
  110.     digitalWrite(CS,0);
  111.     spi_write_byte(0x06);
  112.     digitalWrite(CS,1);
  113.     delay_ms(1);
  114.     digitalWrite(CS,0);
  115.     spi_write_byte(0x60);
  116.     digitalWrite(CS,1);
  117.     delay_ms(20000);
  118. }

  119. void spi_reg_clear(){
  120.     digitalWrite(CS,0);
  121.     spi_write_byte(0x06);
  122.     digitalWrite(CS,1);
  123.     delay_ms(1);
  124.     digitalWrite(CS,0);
  125.     spi_write_byte(0x01);
  126.     spi_write_byte(0x00);
  127.     spi_write_byte(0x02);
  128.     digitalWrite(CS,1);
  129.     delay_ms(1000);
  130. }

  131. int main(){
  132.     wiringPiSetupGpio();
  133.     pinMode(VCC,OUTPUT);
  134.     pinMode(CS,OUTPUT);
  135.     pinMode(DI,OUTPUT);
  136.     pinMode(DO,INPUT);
  137.     pinMode(CLK,OUTPUT);
  138.     pinMode(WP,OUTPUT);
  139.     pinMode(HOLD,OUTPUT);
  140.     
  141.     digitalWrite(VCC,1);
  142.     digitalWrite(CS,1);
  143.     digitalWrite(WP,1);
  144.     digitalWrite(HOLD,1);
  145.     
  146.     spi_id_read();
  147.     spi_id_read1();
  148.     
  149.     spi_reg_read();
  150.     spi_reg_read1();
  151.     
  152.     spi_reg_clear();
  153.     
  154.     return 0;
  155. }
编译:
gcc testspi.c -lwiringPi

烧录夹夹上之后,直接读W25Q64的ID发现读出来的都是0x00(试了很多次都是这样),若主板插上电源后再读,读出来的都是0xFF,过几分钟把电源去掉,读出来就正常了(可能是外部电路导致VCC供电不足)。

正式代码如下(刷了几十分钟):
  1. #include<wiringPi.h>
  2. #include<stdio.h>
  3. #include<time.h>
  4. #include<string.h>
  5. //1 /CS GPIO.9(3)
  6. //2 DO GPIO.13(9)
  7. //3 /WP GPIO.28(20)
  8. //4 GND GND
  9. //5 DI GPIO.12(10)
  10. //6 CLK GPIO.14(11)
  11. //7 /HOLD GPIO.40(21)
  12. //8 VCC GPIO.8(2)

  13. #define    VCC    2
  14. #define    CS    3
  15. #define    DI    10
  16. #define    DO    9
  17. #define    CLK    11
  18. #define    WP    20
  19. #define    HOLD    21

  20. void delay_x(int i){
  21.     int j;
  22.     for(j=0;j<i;j++);
  23. }

  24. void delay_ms(int i){
  25.     delay(i);
  26. }

  27. unsigned int spi_read_byte(){
  28.     int i,data;
  29.     data=0;
  30.     for(i=0;i<8;i++){
  31.         digitalWrite(CLK,0);
  32.         delay_x(1000);
  33.         digitalWrite(CLK,1);
  34.         delay_x(1000);
  35.         if(digitalRead(DO)){
  36.             data|=1<<(7-i);
  37.         }
  38.     }
  39.     return data;
  40. }

  41. void spi_write_byte(unsigned int data){
  42.     int i;
  43.     for(i=0;i<8;i++){
  44.         if(data&(1<<(7-i))){
  45.             digitalWrite(DI,1);
  46.         }else{
  47.             digitalWrite(DI,0);
  48.         }
  49.         digitalWrite(CLK,0);
  50.         delay_x(1000);
  51.         digitalWrite(CLK,1);
  52.         delay_x(1000);
  53.     }
  54. }

  55. unsigned int spi_data_read(unsigned int addr){
  56.     int data;
  57.     digitalWrite(CS,0);
  58.     spi_write_byte(0x03);
  59.     spi_write_byte((addr>>16)&0xff);
  60.     spi_write_byte((addr>>8)&0xff);
  61.     spi_write_byte(addr&0xff);
  62.     data=spi_read_byte();
  63.     digitalWrite(CS,1);
  64.     return data;
  65. }

  66. void spi_data_write(unsigned int addr,unsigned int data){
  67.     digitalWrite(CS,0);
  68.     spi_write_byte(0x06);
  69.     digitalWrite(CS,1);
  70.     delay_ms(1);
  71.     digitalWrite(CS,0);
  72.     spi_write_byte(0x02);
  73.     spi_write_byte((addr>>16)&0xff);
  74.     spi_write_byte((addr>>8)&0xff);
  75.     spi_write_byte(addr&0xff);
  76.     spi_write_byte(data);
  77.     digitalWrite(CS,1);
  78. }

  79. void spi_data_read_x(unsigned int addr,unsigned char *data,int size){
  80.     int i;
  81.     digitalWrite(CS,0);
  82.     spi_write_byte(0x03);
  83.     spi_write_byte((addr>>16)&0xff);
  84.     spi_write_byte((addr>>8)&0xff);
  85.     spi_write_byte(addr&0xff);
  86.     for(i=0;i<size;i++){
  87.         data[i]=spi_read_byte();
  88.     }
  89.     digitalWrite(CS,1);
  90. }

  91. void spi_data_write_x(unsigned int addr,unsigned char *data,int size){
  92.     int i;
  93.     digitalWrite(CS,0);
  94.     spi_write_byte(0x06);
  95.     digitalWrite(CS,1);
  96.     delay_ms(1);
  97.     digitalWrite(CS,0);
  98.     spi_write_byte(0x02);
  99.     spi_write_byte((addr>>16)&0xff);
  100.     spi_write_byte((addr>>8)&0xff);
  101.     spi_write_byte(addr&0xff);
  102.     for(i=0;i<size;i++){
  103.         spi_write_byte(data[i]);
  104.     }
  105.     digitalWrite(CS,1);
  106.     delay_ms(1);
  107. }

  108. void spi_chip_erase(){
  109.     digitalWrite(CS,0);
  110.     spi_write_byte(0x06);
  111.     digitalWrite(CS,1);
  112.     delay_ms(1);
  113.     digitalWrite(CS,0);
  114.     spi_write_byte(0x60);
  115.     digitalWrite(CS,1);
  116.     delay_ms(20000);
  117. }

  118. int main(){
  119.     wiringPiSetupGpio();
  120.     pinMode(VCC,OUTPUT);
  121.     pinMode(CS,OUTPUT);
  122.     pinMode(DI,OUTPUT);
  123.     pinMode(DO,INPUT);
  124.     pinMode(CLK,OUTPUT);
  125.     pinMode(WP,OUTPUT);
  126.     pinMode(HOLD,OUTPUT);
  127.     
  128.     digitalWrite(VCC,1);
  129.     digitalWrite(CS,1);
  130.     digitalWrite(WP,1);
  131.     digitalWrite(HOLD,1);
  132.     
  133.     printf("start\n");
  134.     
  135.     spi_chip_erase();
  136.     
  137.     int i,j,l;
  138.     char data[256],data1[256];
  139.     FILE *fp;
  140.     fp=fopen("x.bin","rb");
  141.     fseek(fp,0,2);
  142.     l=ftell(fp);
  143.     printf("file size: %d\n",l);
  144.     
  145.     fseek(fp,0,0);
  146.     for(i=0;i<l;i+=256){
  147.         j=(l-i<256)?(l-i):256;
  148.         fread(data,1,j,fp);
  149.         spi_data_write_x(i,data,j);
  150.         spi_data_read_x(i,data1,j);
  151.         if(memcmp(data,data1,j)!=0){
  152.             break;
  153.         }
  154.         printf("%d\n",i/256);
  155.     }
  156.     
  157.     printf("%s\n",i<l?"wa":"ac");
  158.     fclose(fp);
  159.     
  160.     return 0;
  161. }
编译:
gcc testspi_bios.c -lwiringPi

这里的代码仅供参考,若笔记本变砖了概不负责(笔记本变砖了不保修)。

阅读(3960) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~