Mikrobootloader for PIC18F-J- No EEPROM needs update

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
igeorge
Posts: 593
Joined: 28 Dec 2005 09:15

Mikrobootloader for PIC18F-J- No EEPROM needs update

#1 Post by igeorge » 09 Feb 2012 04:02

HI,
I had to spend some time to figure out why the program which had included Boot18_128K_G does not compile
What i found is that the boot loader refer to a Pic with EEprom and my PIC18F87J90 does not have EEprom, so bit 7 and 6 on the EECON1 does not exist.
For this reason the compiler reported an error all the time
See the code and the commented two lines
I suggest ME make a boot loader for 128K flash P18 chips without these two bits , so others can have the right boot loader

Code: Select all

sub procedure Flash_Write_Sector(dim address as longword, dim sdata as ^byte)
  dim saveintcon, i, j as byte
  saveintcon = INTCON

  '--- erase memory
  TBLPTRL = Lo(address)
  TBLPTRH = Hi(address)
  TBLPTRU = Higher(address)
  '--- required erase sequence
'''''''''''''''''''''''''''''''''''''''''  EECON1.EEPGD = 1   'Not used for pic18f87j90 without eeprom
'''''''''''''''''''''''''''''''''''''''''  EECON1.CFGS = 0    'Not used for pic18f87j90 without eeprom
  EECON1.WREN = 1
  EECON1.FREE = 1
  INTCON.GIE = 0
  EECON2 = 0x55
  EECON2 = 0xAA
  EECON1.WR = 1
  INTCON.GIE = 1
  asm
    TBLRD*-
  end asm
  '--- write memory
  FSR0L = Lo(sdata)
  FSR0H = Hi(sdata)
  j = 0
  while (j < _FLASH_ERASE/_FLASH_WRITE_LATCH)
    i = 0
    while (i < _FLASH_WRITE_LATCH)
      TABLAT = POSTINC0
        asm
          TBLWT+*
        end asm
      inc(i)
    wend
''''''''''''''''''''''''''''''''''''''''   EECON1.EEPGD = 1   'Not used for pic18f87j90
'''''''''''''''''''''''''''''''''''''''''  EECON1.CFGS = 0    'Not used for pic18f87j90
    EECON1.WREN = 1
    INTCON.GIE = 0
    EECON2 = 0x55
    EECON2 = 0xAA
    EECON1.WR = 1
    inc(j)
  wend
  INTCON.GIE = 1
  EECON1.WREN = 0
  '--- restore interrupt
  INTCON = saveintcon
end sub
Experience is something you don't get until just after you need it

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Mikrobootloader for PIC18F-J- No EEPROM needs update

#2 Post by janko.kaljevic » 14 Feb 2012 18:09

Hello,

Thanks for the info.
We will publish appropriate source code for bootloader for PIC18FJ family.

Best regards.

bdenkov
Posts: 1
Joined: 13 Mar 2016 11:16

Re: Mikrobootloader for PIC18F-J- No EEPROM needs update

#3 Post by bdenkov » 13 Mar 2016 11:29

Hello,
I am trying to modify the bootloader code for PIC128K (from the examples directory for MikroC) and make it suitable for PIC18F97J60, but I have the following problem. It starts to transfer the hex file and it seems it the first 64 bytes are transfered but after that afer several seconds the bootloader software shows "Error: Timeout waiting for MCU. Disconnected.".

I am using 25MHZ clock. Baudrate is 2400.
The bootloader PC software is the embedded one in MikroC (version 6.6.2).
The only modification I have made are that I changed the baudrate value and removed the instructions EECON1.EEPGD = 1; and EECON1.CFGS = 0;

Boot_Loader.c:

Code: Select all

void Susart_Init(char Brg_reg);
void Susart_Write(char data_);
void Start_Bootload();
void Start_Program();
char Susart_Write_Loop(char send, char recieve);

//-------------- Boootloader main for 128K PIC18's
void main() org 130424 {
    // 16Mhz --> 103           look for asynchronous high speed table
    // 10Mhz -->  64
    //  8Mhz -->  51
    //  4Mhz -->  25
    //            |
    //            |
    //            |
    //           \ /
    //            |

    Susart_Init(162);                    // Init USART1 at 2600
    if (Susart_Write_Loop('g','r')) {   // Send 'g' for ~5 sec, if 'r'
      Start_Bootload();                 //   received start bootload
    }
    else {
      Start_Program();                  //   else start program
    }

}
boot18_128K_G.c.:

Code: Select all

#include "built_in.h"

/*
 * constants :
 *  _FLASH_WRITE_LATCH - flash write latch size
 *  _FLASH_ERASE       - flash erase block size
 * are related to mcu flash memory write and erase block sizes
 * and as such are defined in appropriate flash library file.
 * Be aware that these constants are MCU dependent.
 * Compiler will link them from appropriate flash library,
 * so the user does not have to take care of them.
 *
 */

static char block[64];

void Susart_Init(unsigned short brg_reg) org 130768 {
  unsigned short i;

  TXSTA1.TXEN = 1;
  RCSTA1 = 0x90;
  TXSTA1.TX9D = 0; //8-bit transmission
  TXSTA1.BRGH = 0;
  SPBRG1 = brg_reg;
  TXSTA1.SYNC = 0;
  BAUDCON1.BRG16 = 0;
  TRISC.B7 = 1;
  TRISC.B6 = 0;

  while (PIR1.RCIF)
    i = RCREG1;
}


void Susart_Write(unsigned short data_) org 130728 {

  while (!TXSTA.TRMT)
    ;
  TXREG = data_;
}

unsigned short Susart_Data_Ready() org 130754 {
  return (PIR1.RCIF);
}

unsigned short Susart_Read() org 130696 {
  unsigned short rslt;
  rslt = RCREG1;

  if (RCSTA1.OERR) {
     RCSTA1.CREN = 0;
     RCSTA1.CREN = 1;
  }
  return rslt;
}

//-------------- Empty function, this is where the start address of 'real'
//               program will be placed (by Write_Begin()). That is why
//               this function must have at least 4 bytes of ROM available
//               after its org address.
void Start_Program() org 0x1FFC0 {
}

void Flash_Write_Sector(long address, char *sdata) org 130518 {
  unsigned short saveintcon, i, j;

  saveintcon = INTCON;

  //--- erase memory
  TBLPTRL = Lo(address);
  TBLPTRH = Hi(address);
  TBLPTRU = Higher(address);
  //--- required erase sequence
  //EECON1.EEPGD = 1;
  //EECON1.CFGS = 0;
  EECON1.WREN = 1;
  EECON1.FREE = 1;
  INTCON.GIE = 0;
  EECON2 = 0x55;
  EECON2 = 0xAA;
  EECON1.WR = 1;
  INTCON.GIE = 1;
  asm TBLRD*- ;
  //--- write memory                             `
  FSR0L = Lo(sdata);
  FSR0H = Hi(sdata);
  j = 0;
  while (j < _FLASH_ERASE/_FLASH_WRITE_LATCH) {
    i = 0;
    while (i < _FLASH_WRITE_LATCH) {
      TABLAT  = POSTINC0;
      asm {
        TBLWT+*
      }
      i++;
    }
   //EECON1.EEPGD = 1;
    //EECON1.CFGS = 0;
    EECON1.WREN = 1;
    INTCON.GIE = 0;
    EECON2 = 0x55;
    EECON2 = 0xAA;
    EECON1.WR = 1;
    j++;
  }
  INTCON.GIE = 1;
  EECON1.WREN = 0;
  //--- restore interrupt
  INTCON = saveintcon;
}

unsigned short Susart_Write_Loop(char send, char receive) org 130460 {
  unsigned short rslt = 0;

  LBL_BOOT18_128_01:
    ___Boot_Delay128k();
    Susart_Write(send);
    ___Boot_Delay128k();

    rslt++;
    if (rslt == 255u)
      return 0;
    if (Susart_Read() == receive)
      return 1;
  goto LBL_BOOT18_128_01;
}

//-------------- This void will recover the bootlocation 0x0000, 0x0001 and
//               0x0002 to point to the bootloaer's main. It will also move
//               the reset vector of the program that is uploaded to a new
//               location, in this case 130424
void Write_Begin() org 130642 {

  Flash_Write_Sector(0x01FFC0, block);
  //--- goto main 130424
  block[0] = 0xBC;
  block[1] = 0xEF;
  block[2] = 0xFE;
  block[3] = 0xF0;
}

//-------------- Starts with bootload
void Start_Bootload() org 130804 {

  unsigned short i = 0, xx, yy;
  long j = 0;
  while (1) {
    if (i == 64u) {
      //--- If 32 words (64 bytes) recieved then write to flash
      if (!j)
        Write_Begin();
      Flash_Write_Sector(j, block);

      i = 0;
      j += 0x40;
    }
    //--- Ask for yy
    Susart_Write('y');
    while (!Susart_Data_Ready()) ;
    //--- Read yy
    yy = Susart_Read();
    //--- Ask for xx
    Susart_Write('x');
    while (!Susart_Data_Ready()) ;
    //--- Read xx
    xx = Susart_Read();
    //--- Save xxyy in block[i]
    block[i++] = yy;
    block[i++] = xx;
  }
}
Does anybody see where it might be the problem ?
Thank you in advance!

User avatar
biljana.nedeljkovic
mikroElektronika team
Posts: 1043
Joined: 30 Jun 2015 15:15

Re: Mikrobootloader for PIC18F-J- No EEPROM needs update

#4 Post by biljana.nedeljkovic » 22 Mar 2016 15:28

Hello,

Have you tried to use another frequency?
Please double check the datasheet in order to set baud rate:
http://ww1.microchip.com/downloads/en/D ... 39762f.pdf

Best regards,
Biljana

Post Reply

Return to “mikroBasic PRO for PIC32 General”