⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dk3200_iap.cpp

📁 尽量朝“单片”方向设计硬件系统。系统器件越多
💻 CPP
📖 第 1 页 / 共 3 页
字号:
   }
   return(comm_port_open);
}
//---------------------------------------------------------------------------

/////////////////// deinitCommPort()
//
// Closes the COM port.
//
int deinitCommPort()
{
   if(comm_port_open)
   {
      comm_port_open = 0;
      com->Close();
   }
   return 1;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//              PACKET PREPARAION AND SENDING ROUTINES
//---------------------------------------------------------------------------

#define PACKET_HEADER 0x21

/////////////////// encapsulate_packet()
//
// Prepare the packet for sending via RS232 line.
// This function sets the header and computes the checksum.
//
// The function returns the size of encapsulated packet (number of bytes).
//
int encapsulate_packet(char *buffer, int data_length)
{
   buffer[0] = PACKET_HEADER;
   buffer[1] = (data_length + 6) / 256;
   buffer[2] = (data_length + 6) % 256;
   buffer[3] = -buffer[1] - buffer[2];
   unsigned char csum1=0,csum2=0;
   for(int i=0; i<data_length; i++){
      csum1 += buffer[i+4];
      csum2 ^= buffer[i+4];
   }
   buffer[data_length+4] = csum1;
   buffer[data_length+4+1] = csum2;
   return(data_length + 6);
}
//---------------------------------------------------------------------------

/////////////////// encapsulate_packet()
//
// Send the packet via RS232 line and receive the answer from the board.
// Function returns 1 on success, 0 if failed.
// The retry attempts can be adjusted by parameter, default is 10.
// The length of answer received is stored in variable.
//
int SendCommand_DK3200(char *command, int command_length, char *result_buffer, int result_buffer_size, int *bytes_received, int TX_RETRIES)
{
   char tx_buf[LONG_BUFFER_SIZE],rx_buf[LONG_BUFFER_SIZE];
   int packet_length;
   int result_success;
   int retry = 0;
   char tmp_print[100];
   int read_count,buff_ptr,k;

   if(!command_length)command_length=strlen(command);
   memcpy(tx_buf+4,command,command_length);
   packet_length = encapsulate_packet(tx_buf,command_length);

   TDateTime t1,t2;
   t1 = Now();

   do {
      com->Write(tx_buf,packet_length);
      buff_ptr = 0;
      do {
         Sleep(30);
         buff_ptr+=(read_count = com->Read(rx_buf+buff_ptr, LONG_BUFFER_SIZE-buff_ptr));
      }
      while(read_count);

      read_count=buff_ptr;
      if(result_buffer_size&&(read_count>result_buffer_size))read_count=result_buffer_size;
      if(read_count>0)
      {
         rx_buf[read_count] = '\0';  // for null terminated string operations
         if(
         (!strncmp(rx_buf,"WFT ",4))|| // Write Flash toggle
         (!strncmp(rx_buf,"WFP ",4))|| // Write Flash poll
         (!strncmp(rx_buf,"WRC ",4))|| // Write RAM code
         (!strncmp(rx_buf,"WRD ",4))|| // Write RAM data
         (!strncmp(rx_buf,"RRC ",4))|| // Read RAM code
         (!strncmp(rx_buf,"RRD ",4))|| // Read RAM data
//       (!strncmp(rx_buf,"RFC ",4))|| // Read Flash code (unused, identical to RRC)
//       (!strncmp(rx_buf,"RFD ",4))|| // Read Flash data (unused, identical to RRD)
         (!strncmp(rx_buf,"RST ",4))|| // Board Reset
         (!strncmp(rx_buf,"BRD ",4))|| // Board name request
         (!strncmp(rx_buf,"MIR ",4))|| // LCD content request
         (!strncmp(rx_buf,"PG ",3))||  // PAGE Register set
         (!strncmp(rx_buf,"VM ",3))||  // VM Register set
         (!strncmp(rx_buf,"LCD ",4))|| // Write text to LCD
         (!strncmp(rx_buf,"EM ",3))||  // Erase Main Flash sector
         (!strncmp(rx_buf,"EB ",3))    // Erase Boot Flash sector
         ){
            if(bytes_received!=NULL)bytes_received[0] = read_count;
            result_success=1;
         }
         else {
            result_success=0;
            retry++;
         }
      }
   else {
      result_success=0;
      retry++;
      }
   }
   while((retry<=TX_RETRIES)&&(result_success!=1));

   t2 = Now();
//   int time_consumed = MilliSecondsBetween(t1, t2);
   if(result_buffer!=NULL)
   {
      memcpy(result_buffer,rx_buf,result_buffer_size);
   }
   if(retry>TX_RETRIES)
   {
      if(result_buffer!=NULL)
      {
         if(bytes_received!=NULL) bytes_received[0] = 0;
         strcpy(result_buffer,"RETRY Timeout");
      }
      return 0;
   }
   return 1;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//              MAIN IAP DEMO COMMANDS IMPLEMENTATION
//---------------------------------------------------------------------------

/////////////////// ResetBoard()
//
// Sends a command to reset the chip.
//
// Function returns 1 on success, 0 on error.
//
int ResetBoard()
{
   // Send command
   if(!SendCommand_DK3200("RST", NULL, 0, NULL, NULL))
   {
      OnError("Error sending RESET command.");
      return 0;
   }
   return 1;
}
//---------------------------------------------------------------------------

/////////////////// SetPage()
//
// Sets the page register.
//
// The function takes one argument - value which will be set
// to PAGE register of uPSD.
//
// Function returns 1 if successful, 0 on error.
//
int SetPage(BYTE page)
{
   char command[SHORT_BUFFER_SIZE],reply[SHORT_BUFFER_SIZE];
   strcpy(command,"PG ");
   sprintf_2hex(command+3,page);
   command[5]=0;
   if(!SendCommand_DK3200(command, 0, reply, SHORT_BUFFER_SIZE, NULL))
   {
      OnError("Error sending SET PAGE command.");
      return 0;
    }
    return 1;
}
//---------------------------------------------------------------------------

/////////////////// SetVM()
//
// Sets the VM register.
//
// The function takes one argument - value which will be set
// to VM register of uPSD.
//
// Function returns 1 if successful, 0 on error.
//
int SetVM(BYTE vm)
{
   char command[SHORT_BUFFER_SIZE],reply[SHORT_BUFFER_SIZE];
   strcpy(command,"VM ");
   sprintf_2hex(command+3,vm);
   command[5]=0;
   if(!SendCommand_DK3200(command, 0, reply, SHORT_BUFFER_SIZE, NULL))
   {
      OnError("Error sending SET VM command.");
      return 0;
   }
   return 1;
}
//---------------------------------------------------------------------------

/////////////////// SelectFlash()
//
// Selects primary or secondary flash for manipulation in data space.
//
// list and meaning of parameters:
// BYTE flash    - flash selector, 0 = Main Flash (primary), 1 = Boot Flash (secondary)
// BYTE sector   - sector selector, 0..7 for Main Flash, 0..3 for Boot flash
//
// Function returns 1 if successful, 0 on error.
//
int SelectFlash(BYTE flash, BYTE sector)
{
   if(flash == PRIMARY_FLASH)
   {
      // Put primary in data space; secondary/boot in code space
      if(!SetVM(PRIMARY_FLASH_VM))return 0;
      if(!SetPage(sector))return 0;
   }
   else if(flash == SECONDARY_FLASH)
   {
      if(sector >= 4)
      {
         OnError("Secondary/boot flash sector can not be > 3.");
         return 0;
      }
      // Put secondary/boot flash in data space
      if(!SetVM(SECONDARY_FLASH_VM))return 0;
      if(!SetPage(0))return 0;
   }
   return 1;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//              FLASH READ & WRITE ROUTINES IMPLEMENTATION
//---------------------------------------------------------------------------

/////////////////// ReadFlash()
//
// Reads and returns a number of bytes from the
// actual flash sector starting at a specified offset.
//
// list and meaning of parameters:
// int offset   - offset address where to write the data, valid values are
//                0x0000-0x7fff for Main Flash, 0x0000-0x1fff for Boot Flash
// BYTE *buffer - destination buffer where the read data will be stored
// int nBytes   - amount of data to read
//
// Function returns 1 on success, 0 on error.
//
int ReadFlash(int offset, BYTE *buffer, int nBytes)
{
   char command[SHORT_BUFFER_SIZE],reply[LONG_BUFFER_SIZE];
   int bytes_read;
   strcpy(command, "RRD");
   command[3]=offset/256;
   command[4]=offset%256;
   command[5]=nBytes;
   if(!SendCommand_DK3200(command, 6, reply, LONG_BUFFER_SIZE, &bytes_read))
   {
      OnError("Error sending READ FLASH command.");
      return 0;
   }
   memcpy(buffer,reply,bytes_read);
   return 1;
}
//---------------------------------------------------------------------------

/////////////////// WriteFlash()
//
// Writes a number of bytes to the actual flash sector,
// starting at a specified offset.
//
// list and meaning of parameters:
// int offset   - offset address where to write the data, valid values are
//                0x0000-0x7fff for Main Flash, 0x0000-0x1fff for Boot Flash
// BYTE *buffer - source buffer containing data
// int nBytes   - amount of data to write
//
// Function returns 1 on success, 0 on error.
//
int WriteFlash(int offset, BYTE *buffer, int nBytes)
{
   char command[LONG_BUFFER_SIZE],reply[SHORT_BUFFER_SIZE];
   strcpy(command, "WFP");
   command[3]=offset/256;
   command[4]=offset%256;
   command[5]=nBytes;
   memcpy(command+6, buffer, nBytes);
   if(!SendCommand_DK3200(command, 6+nBytes, reply, SHORT_BUFFER_SIZE, NULL))
   {
      OnError("Error sending WRITE FLASH command.");
      return 0;
   }
   int bytes_written = 0;
   sscanf(reply+15,"%02x",&bytes_written);
   if(strncmp(reply+18,"B WRITTEN",9)||(bytes_written!=nBytes)){
      OnError("Error writing data, target memory space probably not blank.");
      return 0;
   }
   else return 1;
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
//              FLASH ERASE & BLANK CHECK ROUTINES IMPLEMENTATION
//---------------------------------------------------------------------------

/////////////////// SectorErase()
//
// Sends a command to erase the selected flash sector.
//
// list and meaning of parameters:
// int flash   - flash selector, 0 = Main Flash (primary), 1 = Boot Flash (secondary)
// int sector  - sector selector, 0..7 for Main Flash, 0..3 for Boot flash
// int offset  - offset parameter, specifies the memory block to erase
//
// The offset parameter is needed because in some memory map configurations
// the more physical flash sectors can be mapped in one logical block sequentially.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -