Time out problem with mikro bootloader GUI

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
kumar123
Posts: 68
Joined: 17 Oct 2023 07:32

Time out problem with mikro bootloader GUI

#1 Post by kumar123 » 19 Feb 2024 08:00

Hi,
I was working with this given code. I configure this code for my pic18f67k40 microcontroller.
It is giving time out error when I upload .hex file of my application code.

Code: Select all

#pragma orgall 0x1C000
#define BOOTLOADER_START_ADDR 0x1C000
#define START_PROGRAM_ADDR 0x1C300

static char block[64];
void Start_Program() org START_PROGRAM_ADDR{}

unsigned short UART_Write_Loop(char send, char receive) {
  
  unsigned int rslt = 0;
  while(1){
    Delay_5ms();
    UART1_Write(send);
    Delay_5ms();
    
    rslt++;
    if (rslt == 512){
      return 0;
    }
    if (UART1_Read() == receive){
      return 1;
    }
  }

}

void Write_Begin(){

  FLASH_Write(START_PROGRAM_ADDR, block);
  //FLASH_Erase_Write_64(START_PROGRAM_ADDR, block);
  block[0] = 0x60;
  block[1] = 0xEF;
  block[2] = 0x3E;
  block[3] = 0xF0;

}

void Start_Bootload()  {
  char i = 0, xx, yy;
  long j = 0;

  while (1) {

    if (i == 64) {
      //--- If 32 words (64 bytes) recieved then write to flash
      if (!j)
        Write_Begin();
      if (j<BOOTLOADER_START_ADDR){
           FLASH_Write(j, block);
           //FLASH_Erase_Write_64(j, block);
      }
      
      i = 0;
      j += 0x40;

    }

    UART1_Write('y');
    while (!UART1_Data_Ready());
    yy = UART1_Read();

    UART1_Write('x');
    while (!UART1_Data_Ready());
    xx = UART1_Read();

    block[i++] = yy;
    block[i++] = xx;
  }
}

void main() org BOOTLOADER_START_ADDR{

    UART1_Init(9600);

    if (UART_Write_Loop('g','r')) {
         Start_Bootload();
    }
    else {
         Start_Program();
    }
}
Is that code I configured right or wrong?
If this code is wrong, suggest new code for pic18f67k40 microcontroller.

Regards,
Kumar

mikasa
Posts: 1
Joined: 21 Mar 2024 09:13

Re: Time out problem with mikro bootloader GUI

#2 Post by mikasa » 21 Mar 2024 09:16

Instead of a hardcoded limit, you can consider implementing a timeout mechanism based on a timer interrupt. Below is a modified version addressing the timeout issue and adding comments

Code: Select all

#pragma orgall 0x1C000
#define BOOTLOADER_START_ADDR 0x1C000
#define START_PROGRAM_ADDR 0x1C300

static char block[64];

void Start_Program() org START_PROGRAM_ADDR {}

// Function to write a block of data to flash with timeout handling
unsigned short UART_Write_Loop_With_Timeout(char send, char receive, unsigned int timeout_ms) {
  unsigned int rslt = 0;
  unsigned int start_time = TickGet(); // Replace with appropriate timer function

  while (1) {
    Delay_5ms();
    UART1_Write(send);
    Delay_5ms();

    rslt++;
    if (rslt >= timeout_ms / 5) { // Check timeout based on timer and Delay_5ms
      return 0;
    }
    if (UART1_Read() == receive) {
      return 1;
    }
  }
}

void Write_Begin() {
  FLASH_Write(START_PROGRAM_ADDR, block);
  //FLASH_Erase_Write_64(START_PROGRAM_ADDR, block);  // Consider using for large writes
  block[0] = 0x60;
  block[1] = 0xEF;
  block[2] = 0x3E;
  block[3] = 0xF0;
}

void Start_Bootload() {
  char i = 0, xx, yy;
  long j = 0;

  while (1) {
    if (i == 64) {
      // Write to flash if 32 words (64 bytes) received
      if (!j) Write_Begin();
      if (j < BOOTLOADER_START_ADDR) FLASH_Write(j, block);

      i = 0;
      j += 0x40;
    }

    UART1_Write('y');
    while (!UART1_Data_Ready());
    yy = UART1_Read();

    UART1_Write('x');
    while (!UART1_Data_Ready());
    xx = UART1_Read();

    block[i++] = yy;
    block[i++] = xx;
  }
}

void main() org BOOTLOADER_START_ADDR {
  UART1_Init(9600);

  // Increase timeout value if necessary based on your communication setup
  if (UART_Write_Loop_With_Timeout('g', 'r', 1000)) { // 1 second timeout
    Start_Bootload();
  } else {
    Start_Program();
  }
}

Post Reply

Return to “mikroC PRO for PIC General”