SHARE
TWEET

ft5316 fw upgrade

a guest Jan 27th, 2015 926 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <SD.h>
  3.  
  4. #define VCC_PIN 3
  5. #define RST_PIN 2
  6. #define DEV_ADDR 0x38
  7. #define FTS_PACKET_LENGTH 2
  8.  
  9. File myFile;
  10.  
  11. void initSD(){
  12.  
  13.   Serial.print("Initializing SD card...");
  14.   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  15.   // Note that even if it's not used as the CS pin, the hardware SS pin
  16.   // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  17.   // or the SD library functions will not work.
  18.    pinMode(10, OUTPUT);
  19.    
  20.   if (!SD.begin(4)) {Serial.println("initialization failed!");return;}
  21.   Serial.println("initialization done.");
  22.  
  23.   myFile = SD.open("fw.bin");
  24.   if (myFile) {
  25.         fts_ctpm_fw_upgrade();
  26.     // read from the file until there's nothing else in it:
  27.     myFile.close();
  28.   } else {
  29.         // if the file didn't open, print an error:
  30.     Serial.println("error opening");
  31.   }
  32.  
  33.  
  34. }
  35.  
  36. void setup(){
  37.         Wire.begin();
  38.         Serial.begin(9600);
  39.         init_device();
  40.         initSD();
  41.        
  42. }
  43.  
  44. void loop(){}
  45.  
  46. void init_device() {
  47.   pinMode(VCC_PIN, OUTPUT);
  48.   pinMode(RST_PIN, OUTPUT);
  49.   digitalWrite(RST_PIN, HIGH);
  50.   digitalWrite(VCC_PIN, HIGH);
  51. }
  52.  
  53. int cmd_write(byte *a, int n){
  54.         Wire.beginTransmission(DEV_ADDR);
  55.         Wire.write(a,n);
  56.         assert(Wire.endTransmission(DEV_ADDR));
  57. }
  58.  
  59. int byte_read(byte* pbt_buf, int bt_len){
  60.         int r=0;
  61.         byte d=0xFF;
  62.         Wire.requestFrom(DEV_ADDR, bt_len);
  63.         while(Wire.available()){
  64.                 d=Wire.read();
  65.                 pbt_buf[r]=d;
  66.                 Serial.print(d,HEX);
  67.                 r++;
  68.         }
  69. }
  70.  
  71. byte i2c_smbus_read_i2c_block_data(byte command, int length, byte *values)
  72. {
  73.         byte r=0, d=0xFF;
  74.   Wire.beginTransmission(DEV_ADDR);
  75.   Wire.write(command);
  76.   assert(Wire.endTransmission(DEV_ADDR));
  77.   Wire.requestFrom(DEV_ADDR, length);
  78.   while (Wire.available()) {
  79.         d=Wire.read();
  80.         values[r]=d;
  81.     Serial.print(d, HEX);
  82.     r++;
  83.   }
  84. }
  85.  
  86. void assert(int e){ if(e){ Serial.print("[E");Serial.print(e);Serial.print("]"); } }
  87.  
  88. /*
  89. while (myFile.available() && i++<10) {
  90.         Serial.print(myFile.read(),HEX);Serial.println(" ");
  91.     }
  92.     // close the file:
  93.    
  94. */
  95.  
  96. boolean  fts_ctpm_fw_upgrade(/*byte* pbt_buf, unsigned int dw_lenth*/){
  97.         unsigned int dw_lenth = myFile.size();
  98.     byte reg_val[2] = {0};
  99.     byte tmpData[4] = {0,0,0,0};
  100.     unsigned int i = 0;
  101.  
  102.     unsigned int  packet_number;
  103.     unsigned int  j;
  104.     unsigned int  temp;
  105.     unsigned int  lenght;
  106.     byte  packet_buf[FTS_PACKET_LENGTH + 6];
  107.     byte  auc_i2c_write_buf[10];
  108.     byte bt_ecc;
  109.     int i_ret;
  110.         int ret;
  111.         unsigned char ver;
  112.  
  113.     /*********Step 1:Reset  CTPM *****/
  114.         pinMode(RST_PIN, OUTPUT);
  115.     digitalWrite(RST_PIN, LOW);  
  116.     delay(10);  
  117.     pinMode(RST_PIN, OUTPUT);
  118.     digitalWrite(RST_PIN, HIGH);
  119.     Serial.println("[TSP] Step 1: Reset CTPM test");
  120.    
  121.     delay(50);  
  122.     /*********Step 2:Enter upgrade mode *****/
  123.     auc_i2c_write_buf[0] = 0x55;
  124.     auc_i2c_write_buf[1] = 0xaa;
  125.     do{
  126.         i ++;
  127.         i_ret = cmd_write(auc_i2c_write_buf, 2);
  128.         delay(5);
  129.     }while(i_ret <= 0 && i < 5 );
  130.     Serial.println("[TSP] Step 2: Enter upgrade mode");
  131.  
  132.     /*********Step 3:check READ-ID***********************/
  133.     tmpData[0]=0x90;
  134.     cmd_write(tmpData,4);
  135.         i=0;
  136.         i_ret=0;
  137.     do{
  138.         i ++;
  139.         i_ret = byte_read(reg_val,2);
  140.         delay(10);
  141.     }while(i_ret <= 0 && i < 5 );
  142.     Serial.print("[TSP] Step 3: check ID: ");
  143.     Serial.print(reg_val[0],HEX);
  144.     Serial.print(", ");
  145.     Serial.print(reg_val[1],HEX);
  146.         Serial.println(" ");
  147.  
  148.     /*********Step 4:erase app*******************************/
  149.     tmpData[0]=0x61;
  150.     ret = cmd_write(tmpData,1);
  151.    
  152.     delay(1500);
  153.     Serial.print("[TSP] Step 4: erase.ret=");
  154.         Serial.print(ret);
  155.         Serial.println(" ");
  156.  
  157.     /*********Step 5:write firmware(FW) to ctpm flash*********/
  158.     bt_ecc = 0;
  159.         Serial.println("[TSP] Step 5: start upgrade.");
  160.     dw_lenth = dw_lenth - 8;
  161.     packet_number = (dw_lenth) / FTS_PACKET_LENGTH;
  162.     packet_buf[0] = 0xbf;
  163.     packet_buf[1] = 0x00;
  164.     for (j=0;j<packet_number;j++){
  165.         temp = j * FTS_PACKET_LENGTH;
  166.         packet_buf[2] = (byte)(temp>>8);
  167.         packet_buf[3] = (byte)temp;
  168.         lenght = FTS_PACKET_LENGTH;
  169.         packet_buf[4] = (byte)(lenght>>8);
  170.         packet_buf[5] = (byte)lenght;
  171.  
  172.         for (i=0;i<FTS_PACKET_LENGTH;i++){
  173.                 myFile.seek(j*FTS_PACKET_LENGTH + i);
  174.             packet_buf[6+i] = myFile.read();
  175.             bt_ecc ^= packet_buf[6+i];
  176.         }
  177.        
  178.         ret=cmd_write( &packet_buf[0],FTS_PACKET_LENGTH + 6);
  179.         if ((j * FTS_PACKET_LENGTH % 1024) == 0){
  180.               Serial.print("[TSP] upgrade the 0x");
  181.               Serial.print(((unsigned int)j) * FTS_PACKET_LENGTH);
  182.               Serial.println(" th byte.");
  183.         }
  184.     }
  185.  
  186.     if ((dw_lenth) % FTS_PACKET_LENGTH > 0){
  187.         temp = packet_number * FTS_PACKET_LENGTH;
  188.         packet_buf[2] = (byte)(temp>>8);
  189.         packet_buf[3] = (byte)temp;
  190.  
  191.         temp = (dw_lenth) % FTS_PACKET_LENGTH;
  192.         packet_buf[4] = (byte)(temp>>8);
  193.         packet_buf[5] = (byte)temp;
  194.  
  195.         for (i=0;i<temp;i++){
  196.                 myFile.seek(packet_number*FTS_PACKET_LENGTH + i);
  197.             packet_buf[6+i] = myFile.read();
  198.             bt_ecc ^= packet_buf[6+i];
  199.         }
  200.              // printk("[TSP]temp 0x%x \n", temp);
  201.         ret = cmd_write( &packet_buf[0],temp+6);    
  202.               //printk("[TSP] 222 ret 0x%x \n", ret);
  203.         delay(20);
  204.     }
  205.  
  206.     //send the last six byte
  207.     for (i = 0; i<6; i++){
  208.         temp = 0x6ffa + i;
  209.         packet_buf[2] = (byte)(temp>>8);
  210.         packet_buf[3] = (byte)temp;
  211.         temp =1;
  212.         packet_buf[4] = (byte)(temp>>8);
  213.         packet_buf[5] = (byte)temp;
  214.         myFile.seek(dw_lenth + i);
  215.         packet_buf[6] = myFile.read();
  216.  
  217.         bt_ecc ^= packet_buf[6];
  218.  
  219.         cmd_write(&packet_buf[0],7);  
  220.         delay(20);
  221.     }
  222.  
  223.     /*********Step 6: read out checksum***********************/
  224.  
  225.     i2c_smbus_read_i2c_block_data(0xcc, 1, &(reg_val[0]));
  226.     Serial.print("[TSP] Step 6:  ecc read 0x");
  227.     Serial.print(reg_val[0]);
  228.     Serial.print(", new firmware 0x");
  229.     Serial.print(bt_ecc);
  230.     Serial.println(".");
  231.     if(reg_val[0] != bt_ecc){
  232.         //return ERR_ECC;
  233.     }
  234.  
  235.     /*********Step 7: reset the new FW***********************/
  236.     pinMode(RST_PIN, OUTPUT);
  237.     digitalWrite(RST_PIN, LOW);  
  238.     delay(1);  
  239.     pinMode(RST_PIN, OUTPUT);
  240.     digitalWrite(RST_PIN, HIGH);
  241.  
  242.     return true;
  243. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top