PIC16F15214 Bootloader
Macros | Functions | Variables
main.c File Reference
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>

Macros

#define WRITE_FLASH_BLOCKSIZE   32
 Write Size of a line of flash in words. More...
 
#define ERASE_FLASH_BLOCKSIZE   32
 ERASE Size of a line of flash in words. More...
 
#define END_FLASH   0x1000
 End address of flash programming area (exclusive) More...
 
#define NEW_RESET_VECTOR   0x140
 The address (in words) in flash where the Application's reset vector will be placed. More...
 
#define NEW_INTERRUPT_VECTOR   (NEW_RESET_VECTOR + 4)
 The address (in words) in flash where the Application's interrupt handler or interrupt handler goto statement will be placed. More...
 
#define _str(x)   #x
 
#define str(x)   _str(x)
 

Functions

 asm ("psect intentry,global,class=CODE,delta=2")
 
 asm ("pagesel " str(NEW_INTERRUPT_VECTOR))
 
 asm ("GOTO " str(NEW_INTERRUPT_VECTOR))
 
void StartWrite ()
 Unlock and start the write or erase sequence. See the PIC16F15214 datasheet. More...
 
uint8_t EUSART1_Read (void)
 Wait until a byte is avaialble from the UART and return it. More...
 
void EUSART1_Write (uint8_t txData)
 Wait until buffer space is avaialble in the UART and transmit a byte. More...
 
uint8_t Bootload_Required ()
 Determine whether we should stay in the bootloader or jump to app Jump to app will happen unless one of the following criteria is present: More...
 
void Run_Bootloader ()
 The bootloader programming executable. This function is called if the Bootload_Required function indicates a bootload is required. More...
 
void main (void)
 Main Program The main program contains a flattening of a number of functions created for initialization inside SYSTEM_Initialize by the Microchip code configurator (MCC) tool. The function calls were removed to save flash space since most functions simply assign values to registers. The original function name is commented out but left in to help line comparison software in the case that a comparison is done between this main and a new MCC generation. The Bootloader runs at 16MHz so that it can run down to 1.8V. More...
 

Variables

uint8_t bootloadReason = 0
 Global variable storing reason that the bootloader stayed in boot rather than jumping to the application. More...
 
uint8_t startBytes [4]
 An array of bytes that is treated like a FIFO by the code. This array stores the last 4 bytes received and compares them to a magic value to enter the bootloader. More...
 

Macro Definition Documentation

◆ _str

#define _str (   x)    #x

◆ END_FLASH

#define END_FLASH   0x1000

End address of flash programming area (exclusive)

◆ ERASE_FLASH_BLOCKSIZE

#define ERASE_FLASH_BLOCKSIZE   32

ERASE Size of a line of flash in words.

◆ NEW_INTERRUPT_VECTOR

#define NEW_INTERRUPT_VECTOR   (NEW_RESET_VECTOR + 4)

The address (in words) in flash where the Application's interrupt handler or interrupt handler goto statement will be placed.

◆ NEW_RESET_VECTOR

#define NEW_RESET_VECTOR   0x140

The address (in words) in flash where the Application's reset vector will be placed.

◆ str

#define str (   x)    _str(x)

◆ WRITE_FLASH_BLOCKSIZE

#define WRITE_FLASH_BLOCKSIZE   32

Write Size of a line of flash in words.

Function Documentation

◆ asm() [1/3]

asm ( "GOTO "   strNEW_INTERRUPT_VECTOR)

◆ asm() [2/3]

asm ( "pagesel "   strNEW_INTERRUPT_VECTOR)

◆ asm() [3/3]

asm ( "psect  intentry,
global  ,
class  = CODE 
)

◆ Bootload_Required()

uint8_t Bootload_Required ( )

Determine whether we should stay in the bootloader or jump to app Jump to app will happen unless one of the following criteria is present:

  • The first location in App space is unprogrammed (0x3FFF)
  • The last location is App space is not the magic number 0x14B7
  • The last reset was caused by a hardware stack overflow (indication from App that we should stay in boot)
  • The voltage on Vdd is less than 2.2V across 2 samples 100ms Apart

◆ EUSART1_Read()

uint8_t EUSART1_Read ( void  )

Wait until a byte is avaialble from the UART and return it.

◆ EUSART1_Write()

void EUSART1_Write ( uint8_t  txData)

Wait until buffer space is avaialble in the UART and transmit a byte.

◆ main()

void main ( void  )

Main Program The main program contains a flattening of a number of functions created for initialization inside SYSTEM_Initialize by the Microchip code configurator (MCC) tool. The function calls were removed to save flash space since most functions simply assign values to registers. The original function name is commented out but left in to help line comparison software in the case that a comparison is done between this main and a new MCC generation. The Bootloader runs at 16MHz so that it can run down to 1.8V.

LATx registers

TRISx registers

ANSELx registers

WPUx registers

ODx registers

SLRCONx registers

INLVLx registers

◆ Run_Bootloader()

void Run_Bootloader ( void  )

The bootloader programming executable. This function is called if the Bootload_Required function indicates a bootload is required.

◆ StartWrite()

void StartWrite ( void  )

Unlock and start the write or erase sequence. See the PIC16F15214 datasheet.

Variable Documentation

◆ bootloadReason

uint8_t bootloadReason = 0

Global variable storing reason that the bootloader stayed in boot rather than jumping to the application.

Valid values are:

  • 'L' - The low byte of the flash end programming magic value was not correct
  • 'H' - The high byte of the flash end programming magic value was not correct
  • 'F' - The first byte of flash in application was not programmed to non-erased value
  • 'S' - The Stack Overflow indicator is set (application request to stay in boot)
  • 'V' - The system voltage was less than 2.2 volts, indicating an external request to stay in boot.

◆ startBytes

uint8_t startBytes[4]

An array of bytes that is treated like a FIFO by the code. This array stores the last 4 bytes received and compares them to a magic value to enter the bootloader.