Page 1 of 1

Bootloader code is not working for pic18f67k40 in mikroC pro for pic

Posted: 11 Jan 2024 05:39
by kumar123
Hi,
I had tried to compile the bootloader code from the mikroE example code for pic18f67k40 microcontroller in mikroC pro for pic. The below code I have tried-

Code: Select all

#include <built_in.h>
#pragma orgall 0x1C000
#define BOOTLOADER_START_ADDR 0x1C000
#define START_PROGRAM_ADDR 0x01FFFF
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 == 0x0200)
      return 0;
    if (UART1_Read() == receive)
      return 1;
  }
}
void Write_Begin(){
  FLASH_Write(START_PROGRAM_ADDR, block);
  //--- goto main
  block[0] = 0x60;  //0xF03EEF60
  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);
      }

      i = 0;
      j += 0x40;
    }
    //--- Ask for yy
    UART1_Write('y');
    while (!UART1_Data_Ready()) ;
    //--- Read yy
    yy = UART1_Read();
    //--- Ask for xx
    UART1_Write('x');
    while (!UART1_Data_Ready()) ;
    //--- Read xx
    xx = UART1_Read();
    //--- Save xxyy in block[i]
    block[i++] = yy;
    block[i++] = xx;
  }
}
void main() {
    /*LATC  = 0;
    LATB = 0;
    PORTC = 0;
    TRISC = 0;
    PORTB = 0;
    TRISB = 0;*/
    UART1_Init(9600);
    if (UART_Write_Loop('g','r')) {
         Start_Bootload();
    }
    else {
         Start_Program();
    }
}
I am getting this error -
orgall1.png
orgall1.png (186.83 KiB) Viewed 614 times
Regards,
Kumar

Re: Bootloader code is not working for pic18f67k40 in mikroC pro for pic

Posted: 15 Jan 2024 12:12
by IvanJeremic
Hi,

Try something like this:

#pragma orgall 0x1C000

#define BOOTLOADER_START_ADDR 0x1C000
#define START_PROGRAM_ADDR 0x1C300

Regards,

Ivan.

Re: Bootloader code is not working for pic18f67k40 in mikroC pro for pic

Posted: 17 Jan 2024 06:18
by kumar123
Hi,
I had tried according to your suggestion, the code is compiling and it is not giving any error, but when I tried to connect with micro controller it display a message Disconnected.
I am using MCU clock is 64 Mhz, baud rate is 9600. connection configuration PC<---usb---->UART<------juper wires---->header of pcb
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 == 0x0200)
      return 0;
    if (UART1_Read() == receive)
      return 1;
  }
}
void Write_Begin(){
  FLASH_Write(START_PROGRAM_ADDR, block);
  //FLASH_Write_64(START_PROGRAM_ADDR, block);
  //--- goto main
  block[0] = 0x60;  //0xF03EEF60
  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_Write_64(j, block);
      }

      i = 0;
      j += 0x40;
    }
    //--- Ask for yy
    UART1_Write('y');
    while (!UART1_Data_Ready()) ;
    //--- Read yy
    yy = UART1_Read();
    //--- Ask for xx
    UART1_Write('x');
    while (!UART1_Data_Ready()) ;
    //--- Read xx
    xx = UART1_Read();
    //--- Save xxyy in block[i]
    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();
    }
}
Cable I am using :
Busb.png
Busb.png (101.61 KiB) Viewed 534 times
message I am getting:
img.png
img.png (28.74 KiB) Viewed 534 times
Regards,
Kumar

Re: Bootloader code is not working for pic18f67k40 in mikroC pro for pic

Posted: 19 Jan 2024 11:23
by IvanJeremic
Hi,

Try changing the cod back to 115200 baud rate and change it accordingly in the bootloader application as well.

Regards,

Ivan.

Re: Bootloader code is not working for pic18f67k40 in mikroC pro for pic

Posted: 19 Jan 2024 12:10
by kumar123
Hi,
I have tried according to your suggestion, still it is giving same error,
And one thing, I am using FLASH_Write() instead of FLASH_Erase_Write_64(), because when I tried to compile code using FLASH_Erase_Write_64(), It is displaying some error like:- Undeclared Identifier

Code: Select all

#include "built_in.h"
#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 == 0x0200)
      return 0;
    if (UART1_Read() == receive)
      return 1;
  }
}
void Write_Begin(){
  FLASH_Write(START_PROGRAM_ADDR, block);
  //FLASH_Write_64(START_PROGRAM_ADDR, block);
  //FLASH_Erase_Write_64(START_PROGRAM_ADDR, block);
  //--- goto main
  block[0] = 0x60;  //0xF03EEF60
  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_Write_64(j, block);
           //FLASH_Erase_Write_64(j, block);
      }

      i = 0;
      j += 0x40;

    }

    //--- Ask for yy
    UART1_Write('y');
    while (!UART1_Data_Ready()) ;
    //--- Read yy
    yy = UART1_Read();
    //--- Ask for xx
    UART1_Write('x');
    while (!UART1_Data_Ready()) ;
    //--- Read xx
    xx = UART1_Read();
    //--- Save xxyy in block[i]
    block[i++] = yy;
    block[i++] = xx;
  }
}
void main() org BOOTLOADER_START_ADDR{
     //1,31,072
    UART1_Init(115200);
    if (UART_Write_Loop('g','r')) {
         Start_Bootload();
    }
    else {
         Start_Program();
    }
}
sdfj.png
sdfj.png (53.64 KiB) Viewed 501 times
hj.png
hj.png (38.01 KiB) Viewed 501 times
Regards,
Kumar

Re: Bootloader code is not working for pic18f67k40 in mikroC pro for pic

Posted: 23 Jan 2024 11:58
by IvanJeremic
Hi,

Perhaps the clock scheme is not correct, try using one of the UART examples in the compiler, and try it with your MCU, if it works correctly then copy that clock scheme into the bootloader project that you are using.

Regards,

Ivan.