Wiki Sirius

Aggiornamento firmware via CAN

1. Programmazione

graph TB
START(Start)
START_PRG[SDO_START_PRG<br><br>idx = 0x5003<br>Subidx = 0x01<br>Value = 0x1<br>Data len = 0x04]
POS_PBIOS_INFO[SDO_POS_PBIOS_INFO<br><br>idx = 5000<br>Subidx = 1<br>value = 1<br>len = 0x04]
CHECK{Fine blocchi?}
POS_ADDR_TO_PROG[SDO_POS_ADDR_TO_PROG<br><br>idx = 5001<br>Subidx = 0<br>Value = indirizzo blocco di programmazione<br>len = 0x04]
CHECK2{Fine blocco?}
PDO4[PDO4<br><br>8 Byte di cui<br>1 Byte = indice incrementale azzerato ad ogni blocco<br>2..7 Byte = data n..n+7]
POS_CHECKSUM[SDO_POS_CHECKSUM<br><br>idx = 5002<br>Subidx = 0<br>Value = #lt;Checksum#gt;<br>len = 0x01]
END(End)

START --> START_PRG 
START_PRG --> POS_PBIOS_INFO
POS_PBIOS_INFO --> CHECK
CHECK --> |No|POS_ADDR_TO_PROG
CHECK -----> |Si|END
POS_ADDR_TO_PROG --> CHECK2
CHECK2 --> |No|PDO4
CHECK2 --> |Si|POS_CHECKSUM
PDO4 --> CHECK2
POS_CHECKSUM --> CHECK
Elenco dei comandi di programmazione
Comando Index Len (byte) Subidx Dati
SDO_POS_PBIOS_INFO 0x5000 4 1 0 = dati non cifrati
1 = dati cifrati
inizializza per la programmazione
2
3
4
SDO_POS_ADDR_TO_PROG 0x5001 4 0 Indirizzo letto dal file bin
Quando viene inviato azzera PDO4.data0

Risposta di errore per indirizzo non valido
SOD_POS_CHECKSUM 0x5002 1 0 Somma tutti i dati in una variabile a 8 bit senza riporto

Risposta di errore per checksum non valido
SDO_POS_START_PRG 0x5003 4 0 1= Avvia il programma
1 1= entra o resta nel loader
PDO4 0x500 + ID
ID = 0 broadcast
2..8 ___ Data0 = 0 al primo frame, incrementato a ogni frame
Data1 = dato n+0 letto dal file
Data2 = dato n+1 letto dal file
Data3 = dato n+2 letto dal file
Data4 = dato n+3 letto dal file
Data5 = dato n+4 letto dal file
Data6 = dato n+5 letto dal file
Data7 = dato n+6 letto dal file
In the CanOpen protocol, SDO errors are indicated by a specific format of the SDO message. The format of an SDO error message is as follows:

The first byte of the message (byte 0) is the SDO command specifier (SCS). The SCS for an SDO error message is 0x80.

The second byte of the message (byte 1) is the SDO error code. This code indicates the specific error that occurred. Some common error codes include:

0x060x000x - "Toggle bit not alternated"
0x050x000x - "Client/server command specifier not valid or unknown"
0x080x000x - "Out of memory"
The third and fourth bytes of the message (bytes 2 and 3) are the object index and sub-index of the object that caused the error.

The fifth byte of the message (byte 4) is not used in an error message.

1.1 Esempio di traffico CAN

Esempio di invio del primo e del secondo blocco.

[000000] 08-     601    2f 00 50 01 01 00 00 00 
[000001] 08-     581    60 00 50 01 00 00 00 00 
[000002] 08-     601    23 01 50 00 00 00 01 00 
[000003] 08-     581    60 01 50 00 00 00 00 00 
[000004] 08-     501    00 40 51 33 5f 69 39 03 
[000005] 08-     501    01 84 3e 65 8e 37 86 b3 
[000006] 08-     501    02 78 b6 ce 82 d8 70 95 
[000007] 08-     501    03 b4 60 7d 50 f1 5f f8 

...........
...........

[000038] 08-     501    22 fd d0 09 eb 14 52 03 
[000039] 08-     501    23 5e d3 db 58 8d 05 fd 
[000040] 05-     501    24 0d 33 7e 77          
[000041] 08-     601    2f 02 50 00 be 00 00 00 
[000042] 08-     581    60 02 50 00 00 00 00 00 
[000043] 08-     601    23 01 50 00 00 01 01 00 
[000044] 08-     581    60 01 50 00 00 00 00 00 
[000045] 08-     501    00 49 64 d1 9d 8a 71 5d 
[000046] 08-     501    01 69 e8 a8 a5 93 72 b0 
[000047] 08-     501    02 10 c0 6a 67 72 ca ee 
[000048] 08-     501    03 f6 6c 46 36 6e 8c 1a 
...........
...........

[000079] 08-     501    22 90 b0 3a ea 49 3f 4f 
[000080] 08-     501    23 49 93 e5 56 d2 b3 4f 
[000081] 05-     501    24 26 aa 30 01          
[000082] 08-     601    2f 02 50 00 16 00 00 00 
[000083] 08-     581    60 02 50 00 00 00 00 00

2. Descrizione del file binario


Descrizione dell'header
Voce Valore Descrizione
<HEAD> Tag di inizio header
version 1.1 Versione dell'header
block_size 128 dimensione blocco = block_size * word_for_line *2
cpu_type XMC4400 Il binario รจ dedicato alla cpu_type
str_addr 65536 Indirizzo del primo blocco
end_addr_com 385792 Indirizzo dell'ultimo blocco
word_for_line 1 2 byte per word
crypt 1 0 = dati non cifrati
1 = dati cifrati
</HEAD> Tag di fine header

Note

Numero di blocchi da inviare = (end_addr_com - str_addr) / dimensione blocco


Descrizione del blocco
<ADDR> address(ASCII) </ADDR> \n (0x0a) blocco di dati binari di dimensione pari alla dimensione blocco

3. Esempio di codice per la programmazione via CAN

int IDX_version;
int IDX_block_size;
int IDX_cpu_type;
int IDX_str_addr;
int IDX_end_addr;
int IDX_word_for_line;
int IDX_crypt;

void __fastcall TForm21::Timer1Timer(TObject *Sender) {
  int isXMC = 0;
  int tIdx;
  int nLine;

  Form1->CanOpen1->Checked = 1; //Deve essere a uno per funzionare
  Timer1->Enabled = false;

  switch (SttUpdate) {
  case 0:
       if (handle)
         fclose (handle);
       handle = NULL;

       if ((handle = fopen(FileName.c_str(), "rb")) == NULL) {
         perror("Error:");
         Timer1->Enabled = false;
       }

       hi = -1;

       /*Leggo Header*/
       for (int nLine = 0; nLine < 16; nLine++) {
         fgets(line, MAX_LINE, handle);
         if (strstr(line, "<HEAD>")) {
           hi=0;
         }
         else if (strstr(line, "</HEAD>")) {
           break;
         }
         else if (hi >= 0) {
           sscanf(line, "%s\t%s\n", &head[hi].desc, &head[hi].value);
           hi++;
         }
       }

       if (hi <= 0)
         return;

       for (int i = 0; i < hi; i++) {
          if (!stricmp (head[i].desc, "version")) {
            IDX_version = i;
          }
          else if (!stricmp (head[i].desc, "block_size")) {
            IDX_block_size = i;
          }
          else if (!stricmp (head[i].desc, "cpu_type")) {
            IDX_cpu_type = i;
          }
          else if (!stricmp (head[i].desc, "str_addr")) {
            IDX_str_addr = i;
          }
          else if (!stricmp (head[i].desc, "end_addr_com")) {
            IDX_end_addr = i;
          }
          else if (!stricmp (head[i].desc, "word_for_line")) {
            IDX_word_for_line = i;
          }
          else if (!stricmp (head[i].desc, "crypt")) {
            IDX_crypt = i;
          }
       }

       if (strstr(head[IDX_cpu_type].value, "XMC"))
         isXMC = 1;

       MAX_BUFF = atoi(head[IDX_block_size].value) * atoi(head[IDX_word_for_line].value) * 2;

        if (head[IDX_end_addr].value[0] == '0' && head[IDX_end_addr].value[1] == 'x')
          end_addr = (unsigned long)charHexToVal(head[IDX_end_addr].value, NULL, 0);
        else
         end_addr = atol(head[IDX_end_addr].value);

        if (head[IDX_str_addr].value[0] == '0' && head[IDX_str_addr].value[1] == 'x')
          str_addr = (unsigned long)charHexToVal(head[IDX_str_addr].value, NULL, 0);
        else
          str_addr = atol(head[IDX_str_addr].value);


       ProgressBar1->Position = 0;
       ProgressBar1->Max = (end_addr-str_addr)/MAX_BUFF;
       ProgressBar1->Min = 0;

       sprintf(appo, "0x%x", end_addr);
       Edit3->Text = appo;


       ProgressBar1->Position = ProgressBar1->Min;
       actAddr = str_addr;

       for (int firstStr = 0; firstStr < 0x7f; firstStr++)
         nInfo[firstStr].idPresent = false;

       SttUpdate=100;
       break;

    case 100:  //Fix type of prog
       clearAnsw(idx);
       SDOWriteRequest(idx, POS_PBIOS_INFO , 0x01, (char)atoi(head[IDX_crypt].value), 1);  //Crypt

       firstTmo = 0;
       SttUpdate=101;  //Verifico quanti elementi sono presenti
       break;

    case 101:
       if ((firstTmo+=Timer1->Interval) > 500) {
         SttUpdate=1;
         Edit4->Text = (int)getNumDevice(idx);
       }
       break;

    case 1:
       if (!feof(handle)) {

         fgets(line, MAX_LINE, handle);
         if (sscanf(line, "<ADDR>%d</ADDR>\n", &readAddr))
         {
           sprintf(appo, "0x%x", readAddr);
           Edit2->Text = appo;
           Edit5->Text = appo;

           letti = fread(bf2Prg,1, MAX_BUFF, handle);
           if (letti) {
             if (readAddr == 0x0000) {
               /*First Jmp*/
               bf2Prg[0] = 0x00;
               bf2Prg[1] = 0x08; //0x04;
               bf2Prg[2] = 0x04;
               bf2Prg[3] = 0x00;
             }
             SttUpdate++;
           }
           else
             enabled = false;
         }
         else
           enabled = false;
       }
       else {
         enabled = false;
       }
       break;


    case 2:  //Fix Addr
       actAddr = readAddr;
       clearAnsw(idx);
       SDOWriteRequest(idx, POS_ADDR_TO_PROG, 0x00, actAddr, 4);
       ProgressBar1->Position++;
       SttUpdate++;
       break;

       //SEND DATA
    case 3:
       if (!checkAnsw(POS_ADDR_TO_PROG, 0x60, idx)) {
         char btx[8] = {0,0,0,0,0,0,0};
         int i;

         pdoCnt=0;
         bi = 0;
         chk = 0;

         for (;bi < MAX_BUFF;){
           for (i=1; i < 8; i++){
             unsigned char tmp = 0xff;
             if (bi < letti)
               tmp = (unsigned char)(bf2Prg[bi]);
             if (bi >= MAX_BUFF)
               break;

             chk += tmp;
             btx[i] = tmp;
             bi++;
             actAddr++;
           }

           btx[0] = pdoCnt++;
           CO_CANSendMessage(CAN_ID_RPDO4+idx,  btx, i);

           if (!(pdoCnt % 15)) {
             Application->ProcessMessages();
             Sleep(1);
           }
         }
         SttUpdate++;
       }
       else if (checkTmo(Timer1->Interval, 3000))
        SttUpdate--;   //Reset a startAddr
       break;

    case 4:
       clearAnsw(idx);
       SDOWriteRequest(idx, POS_CHECKSUM, 0x00, (chk&0x00ff), 1);
       SttUpdate++;
       break;

    case 5:
       if (!checkAnsw(POS_CHECKSUM, 0x60, 0x00)) {
         idx = atoi(AnsiString(Edit1->Text).c_str());
         SttUpdate=1; //Send New Block
       }
       else if (tIdx = getAnsw(0x80)) {    // Error Go to send Data
         idx = tIdx;
         SttUpdate=2; //Retray a block From Addr to chk
       }
       else if (tIdx = checkTmo(Timer1->Interval, 3000)) {
        idx = tIdx;   //Resend a checkSum
        SttUpdate--;
       }
       break;
  }


  if (enabled) {
    Timer1->Enabled = true;
  }
  else {
    ProgressBar1->Min = 0;
    ProgressBar1->Position = 0;
    ProgressBar1->Max = 0;
    Timer1->Enabled = false;
    if (handle)
      fclose (handle);
  }
}


void clearAnsw(int idx) {
  for (int i = 0; i < 0x7f; i++)
    nInfo[i].TmoCnt = 0;

  if (idx) {
    nInfo[idx].answ = -1;
    nInfo[idx].answOk = 0;
    nInfo[idx].index = 0;
    nInfo[idx].subIndex = 0;
    return;
  }

  for (int i = 0; i < 0x7f; i++) {
    nInfo[i].answ = -1;
    nInfo[i].answOk = 0;
    nInfo[i].index = 0;
    nInfo[i].subIndex = 0;
  }
}

char checkAnsw(unsigned int index, char chk, int idx) {
  char ret = -1;
  if (idx > 0) {
    if (nInfo[idx].idPresent) {
      ret = 0;
      if (nInfo[idx].index != index)
        return idx;
      else if (nInfo[idx].answ != chk)
        return idx;
      else
        nInfo[idx].answOk = true;
    }
  }
  else {
    for (int i = 1; i < 0x7f; i++) {
      if (nInfo[i].idPresent) {
        ret = 0;
        if (nInfo[i].index != index)
          return i;
        else if (nInfo[i].answ != chk)
          return i;
        else
          nInfo[i].answOk = true;
      }
    }
  }
  return ret;
}

char getAnsw(char chk) {
  for (int i = 0; i < 0x7f; i++)  {
    if (nInfo[i].idPresent) {
      if (nInfo[i].answ == chk)
        return i;
    }
  }
  return 0;
}


char checkTmo(int elap, int tmo) {
  for (int i = 0; i < 0x7f; i++) {
    if (nInfo[i].idPresent) {
      if (!nInfo[i].answOk) {
        if ((nInfo[i].TmoCnt +=elap) > tmo)
          return i;
      }
    }
  }
  return 0;
}