bootloader for 16f88

General discussion on mikroC.
Post Reply
Author
Message
motopic
Posts: 21
Joined: 21 Jul 2006 16:10

bootloader for 16f88

#1 Post by motopic » 18 Oct 2006 15:05

Hi ME,

Can the MEC bootloader be made to work with 16f88 pic?

I tried to change baud setting and memory and recompile, but it errors with libs not found. If I add the #pragma lines from 18f776.c to the 16f88.c will that work?

If MEC just had a program offset and reset vector fix, I could use anyones bootloader. As it is it looks like CCS is going to get my money, because they do that one thing. Very frustrating because I want to use MEC but can't.

Thanks,
Joe

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

Re: bootloader for 16f88

#2 Post by rajkovic » 19 Oct 2006 15:56

motopic wrote:Hi ME,

Can the MEC bootloader be made to work with 16f88 pic?

I tried to change baud setting and memory and recompile, but it errors with libs not found. If I add the #pragma lines from 18f776.c to the 16f88.c will that work?

If MEC just had a program offset and reset vector fix, I could use anyones bootloader. As it is it looks like CCS is going to get my money, because they do that one thing. Very frustrating because I want to use MEC but can't.

Thanks,
Joe

Source code for bootloader is distributed with compiler. This allow our users to adopt it and fit it for their needs. I have done this changes for p16f88 and will post code tomorrow.

I hope that this will encourage others to write their own modifications for specific pics and post code also.

motopic
Posts: 21
Joined: 21 Jul 2006 16:10

#3 Post by motopic » 19 Oct 2006 20:22

Source came with 6.2 compiler???

Looked to me like MEC made some precompiled libs for Susart and such?
I will have to poke around some more and see.

I would like to get bootloader working for 16f88, as my HW.programmer is kinda unreliable. I would really like to understand how it works, so as to be able to help get it on other bootable pics.

Thanks in advance.
Joe

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

#4 Post by rajkovic » 20 Oct 2006 13:40

motopic wrote:Source came with 6.2 compiler???

Looked to me like MEC made some precompiled libs for Susart and such?
I will have to poke around some more and see.

I would like to get bootloader working for 16f88
Thanks in advance.
Joe
Here is the code for p16f88

Code: Select all




unsigned block[32];
unsigned rcv_1, rcv_2;

void eraseBlock(unsigned beginorg);

void sboot16_Delay_5ms() org 0xd36 {

   Delay_ms(5);

 }

void SUSART_init(char Brg_reg) org 0xd54 {

  char i;


  RCSTA = 0x90;
  TXSTA = 0x26;
  TRISB.f2 = 1;
  TRISB.f5 = 0;

  while (PIR1.RCIF == 1) i = RCREG;
  SPBRG = Brg_reg;
}

void SUSART_Write(char data) org 0xd77 {

 while (TXSTA.TRMT == 0) asm nop;
  TXREG = data;
}

char SUSART_Data_Ready()  org 0xd95     {

  return (PIR1.RCIF);
}

char SUSART_Read()      org 0xda9         {
  char r;
  r = RCREG;
  if (RCSTA.OERR = 1) {
     RCSTA.CREN = 0;
     RCSTA.CREN = 1;
     }
  return r;
}



//------------------------------------------------------------------------------}
// If this procedure is called the PIC till jump to this address (eg 0x0FA0) and
// this will be the location where the main code's original 0x0000, 0x0001 and
// 0x0002 starts.
void Start_program()     org 0xfA0  {

  asm nop;
}




//------------------------------------------------------------------------------}
// This procedure will recieve the (start address DIV 32) in the ROM where
// the 32 words will be written to.
// eg. To write to location 0x0000 - Flash_Write_Address(0x00);
//     To write to location 0x0020 - Flash_Write_Adderss(0x01);
//     To write to location 0x1FA0 - Flash_Write_Adderss(0xFD);
void Flash_Write_Address(unsigned beginorg)  org 0xe23 {

     unsigned total;
     unsigned temp;
     char loop;
     char SaveIntCon;

     eraseBlock(beginorg) ;
     loop = 0;
     while (loop != 32) {
          total = (beginorg << 5) + loop;
          temp  = block[loop];
          SaveIntCon = INTCON;
          EEADR   = (char)(total);
          EEADRH  = (char)(total >> 8);
          EEDATA  = (char)(temp);
          EEDATH  = (char)(temp >> 8);
          EECON1.EEPGD = 1;
          EECON1.WREN  = 1;
          EECON1.FREE = 0;
          INTCON.GIE   = 0;
          EECON2 = 0x55;
          EECON2 = 0xAA;
          EECON1.WR    = 1;
          asm nop;
          asm nop;
          INTCON = SaveIntCon;
          EECON1.WREN = 0;
          loop++;
     }
}
//------------------------------------------------------------------------------}
// This function will send a char over the USART until another char is recieved
// or until a timeout is reaced. If the correct char is recieved the function will
// return false else it will return true

char SUsart_Write_Loop(char send, char recieve)org 0xe9b  {

     char  stop, r;
     r    = 0;
     stop = 0;
     while (stop != 1) {
          sboot16_Delay_5ms();
          SUsart_Write(send);
          sboot16_Delay_5ms();
          r++;
          if (r == 255)
            stop = 1;
          if (!SUSART_Data_Ready())
            continue;
          if (SUsart_Read() == recieve)  stop = 1;
     }

     return  r;
     
}
//------------------------------------------------------------------------------}
// This procedure 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 0x1FA0
void Write_Begin()        org 0xed2     {
     block[2] = block[0];            // reset vector of new program
     block[0] = 0x118A;              // page
     block[1] = 0x120A;              // page
     Flash_Write_Address(0x7D);      // wrile 0x7D shl 5 = 0x0FA0
     block[0] = 0x158A;              // page
     block[1] = 0x120A;              // page
     block[2] = 0x2FD8;              // poinst to main procedure
}

//------------------------------------------------------------------------------}
// Handles comunication between PIC and PC
void Start_Bootload()       org 0xF04         {

     char     i;
     unsigned j;

    i = 0;
    j = 0;


     for ( ; ; )  {
          if (i == 32) {             // If 32 words recieved then write to flash
               if (j == 0)  Write_Begin();
               Flash_Write_Address(j);
               i = 0;
               j++;
          }

          SUsart_write('y');                        // Ask for rcv_2
          do ; while (SUsart_Data_Ready() != 1);
          rcv_2 = SUsart_Read();                        // Read rcv_2

          SUsart_write('x');                        // Ask for rcv_1
          do ; while (SUsart_Data_Ready() != 1);
          rcv_1 = SUsart_Read();                        // Read rcv_1


          block[i] = rcv_1 << 8;                     // Save rcv_1rcv_2 in block[i]
          block[i] = block[i] + rcv_2;
          i++;
                               // Next word
     }
}


void eraseBlock(unsigned beginorg) org 0xdbf  {
   unsigned total;
     unsigned temp;
     char loop;
     char SaveIntCon;

    total = (beginorg << 5);
    SaveIntCon = INTCON;
    EEADR   = (char)(total);
    EEADRH  = (char)(total >> 8);
    EECON1.EEPGD = 1;
    EECON1.WREN  = 1;
    EECON1.FREE = 1;
    INTCON.GIE   = 0;
    EECON2 = 0x55;
    EECON2 = 0xAA;
    EECON1.WR    = 1;
    asm nop;
    asm nop;
    INTCON = SaveIntCon;
    EECON1.WREN = 0;
}


and main

Code: Select all

/*
 * Project name:
     MikroC_Bootloader_Example_p16 (using mikroC Bootloader on PIC16's)
 * Test configuration:
     MCU:             PIC16F88
     Dev.Board:       EasyPic4
     Oscillator:      HS, 8.000MHz
     Ext. Modules:    None.
     SW:              mikroC v6.2
 * NOTES:
     - It is HIGHLY UNRECOMMENDED to alter the start address of the main() -
       this way you will have less space in ROM left for the application being
       bootloaded!
 */


void main() org 0x0FD8 {
  // 103 = 16 MHz  @9600 bps
  // 51 = 8 MHz
  // 25 = 4 MHz
  //TRISD = PORTD = 0;
  ansel = 0;
  Susart_Init(51);
  


  
  if (Susart_Write_Loop('g', 'r') != 255)
    Start_Bootload();
  else
    {
     Start_Program();
    }
}//~!

motopic
Posts: 21
Joined: 21 Jul 2006 16:10

#5 Post by motopic » 20 Oct 2006 14:13

Hi,

Thanks a lot! I will be testing this tonite after work. I want to understand how it works.

Thanks,
Joe

motopic
Posts: 21
Joined: 21 Jul 2006 16:10

#6 Post by motopic » 21 Oct 2006 03:30

Aw, I can't get it to work. 2 questions:

1. are RCSTA and TXSTA supposed to be 0x18 and 0x98 respectively?

2. do the function 'org 0x000' values depend on having the optimized compiler? At this point I am un-optimized. :)

I combined the main and the SUsart code into 1 .c file and compiled it. Having 2 c files just kept giving me errors about 'block[32]' being defined already.

Thanks,
Joe

motopic
Posts: 21
Joined: 21 Jul 2006 16:10

#7 Post by motopic » 23 Oct 2006 14:41

Hi,

I think I figured these out on my own, the RCSTA and TXSTA are ok, you are defining how the RX/TX will work. I read the 16f88 spec on that.

I think the memory map is ok, but I am not sure, its a lot to go over.

Still never answers the bootloader app query tho...

motopic
Posts: 21
Joined: 21 Jul 2006 16:10

#8 Post by motopic » 25 Oct 2006 04:23

Ok, bootloader hooks up now. The Tool->bootloader connects and sends the file over.

#1. Program uploaded never runs tho.

#2. Bootloader.hex should just listen for 5 sec and not broadcast 'g's. Use upper asci ctrl bytes. Let the pic reply to the bootloader tool query. This minimizes 'junk' output to lcds that are tied to the serial output.

Joe

User avatar
srdjan
mikroElektronika team
Posts: 1552
Joined: 28 Dec 2005 12:47
Location: Serbia

#9 Post by srdjan » 25 Oct 2006 11:02

motopic wrote:Ok, bootloader hooks up now. The Tool->bootloader connects and sends the file over.

#1. Program uploaded never runs tho.
I have just tried this particular code and it works fine for me. Try some smaller example and see if it works. Also be carefull to set ANSEL register so that neccesary pins are set as digital/analog properly.
motopic wrote:#2. Bootloader.hex should just listen for 5 sec and not broadcast 'g's. Use upper asci ctrl bytes. Let the pic reply to the bootloader tool query. This minimizes 'junk' output to lcds that are tied to the serial output.
The bootloader library is to be revised in the future releases; for the time being it will stay like this.

mkh
Posts: 11
Joined: 11 Oct 2011 16:57

Re: bootloader for 16f88

#10 Post by mkh » 11 Oct 2011 22:44

Hello,

do you have this code also for mikroBasic? Or at least .hex file? Code in C seems to be slightly different (not only change in addresses) than version for PIC16F887 in Basic, so I don't know how to adjusted version from PIC16F887 to PIC16F88.

Thanks for help.

Post Reply

Return to “mikroC General”