'*
' * Project name:
'     Boot_Test (using mikroBasic PRO for PIC Bootloader on PIC16 enhanced
'      16F1xxx range of products)
' * Copyright:
'     (c) Mikroelektronika, 2009.  (Parts)
'
'     This is a mikroBasic PRO for PIC serial port Bootloader implementation.
'     Once started in PIC, the bootloader waits for a while (approx. 5 seconds)
'     to establish USART communication with the MikroBootloader application
'     (Tools menu) on the PC. If it fails to do so, the bootloader starts
'     the program already loaded in the PIC.
'     If the communication is established, the bootloader receives the hex file
'     from the PC and places it where requred (the MikroBootloader application
'     reads out the hex file and takes care of the location where hex is
'     to be placed). When hex download has finished, the user is notified by
'     the MikroBootloader to reset the PIC, and PIC enters the described
'     bootload sequence again.
' * Test configuration:
'     MCU:             PIC16F1938
'
'     Dev.Board:      mikroprog
''        http://www.mikroe.com/eng/products/view/297/easypic7-development-system/
'     Oscillator:      INTOSC 8.0000 MHz, CLK out RA6 (2MHz reference)
'     SW:              mikroBasic PRO for PIC
'              http://www.mikroe.com/eng/products/view/9/mikrobasic-pro-for-pic/

' * NOTES:
'
'RS 485 Switching added 20/10/11 for half-duplex - see RS485_E for details
'Needs 10K pull-up on rx pin of PIC for half duplex to work well.
'*******************************************************************************
'Write routines extensively modifed for erase 32 words, write 8 words x 4 via 
'latch capability of 16F1938.
'Added auto calculation of org and location for start_program routines.
'Tested with 16F1938 using internal oscillator at 8MHZ.
'Total size of this code including half duplex RS485 is 678 words.
'666 words if full duplex is used.
'*****************************************************************************************************

program boot_loader_1938


include bootfor16_E2     'use enhanced version.
                        'also calls RS485 unit where baudrate is set and half duplex if
                        'required.
main:
            org  boot_main
             orgall (boot_main)  'define location of bootloader main program (this one)
             'everything is above this place in ROM until user program is bootloaded
             'except for 0x0000 sets PCLATH to upper 2K page and 0001  = goto boot_main
             'first job is to get oscillator right. Also needs chip config to be set
             OSCCON = %01110000         'set osc to no PLL,  8MHz,   Int clock
             Uart_Begin()               'must set up Uart for bootloader to work
'*******************************************************************************
'now talk to PC and se if we get a response:
  if (Usart_Write_Loop("g","r") = 255) then   
                                              'send 256 "g"s,
                                              'if no response from PC 
                                              '(we are looking for "r")then
      start_program()                         'start user's program
      ' WARNING : If user_program uses USART pins then user must be
      'aware that these pins are already used for bootloader.
      'So user should set USART pins values and directions
      'once again at the start of the user_program.
  else                                'if "r" is received then 
                                      'PC wants to load new program so:
      start_bootload()                'start bootload
  end if
  '''Simples!!!!
end.