Bluetooth (RN42) bootloader

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Bluetooth (RN42) bootloader

Post by SHughes_Fusion » Fri Nov 14, 2014 10:48 am

I've been developing a couple of products that communicate with a Smartphone via the Microchip RN42 bluetooth module. With help and advice from Jerry I wrote a bootloader for these so I can update the firmware over the bluetooth link. This has proved reliable so I've stripped out the product-specific code and created what I hope will be a useful module for others who want to do the same.

A few pointers that will be helpful:
This module is currently optimised for the 2xK22 and 4xK22 series. There is no reason why it won't work on others but the original had to be optimised as much as possible to fit in the 2k bootblock of the chip I was using. This meant I wasn't able to use many compiler functions and existing subs and had to write optimised versions. Therefore, if you are intending to use this with a different PIC then you will need to check things like register definitions.

The module assumes the hardware will have at least one button and one LED. The button is used to force the bootloader to run (to allow faulty firmware to be recovered) and the LED indicates various operation states. There is plenty of scope to extend the output options - for example, the original bootloader could output basic information to a 128x64 LCD and another version had red / green / amber LEDs to indicate status. I've created this as a 'minimal hardware' version that should be suitable for most applications, feel free to adapt it to suit!

I run the PIC at 64MHz, partly to generate the 115200 baud necessary to talk to an 'unmodified' RN42. There are various ways to slow down the RN42 baud rate if you can't reliably generate 115200 baud at your chosen clock speed.

I've modified my System.bas to include the ClearRAM function. This ensures all variables are zeroed and saves a bit of code space over initialising them directly. You will get warnings that variables have not been initialised, these can be ignored safely.

The code plus the necessary library files (including Jerry's SetAllDigital routine) are attached.
Attachments
BT Bootloader.zip
Bluetooth Bootloader with all necessary library files.
(11.5 KiB) Downloaded 237 times

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: Bluetooth (RN42) bootloader

Post by SHughes_Fusion » Fri Nov 14, 2014 11:14 am

Four things I forgot to mention!!!
The code uses EEPROM location 0. This is used to confirm the bootloaded firmware runs - your code will need to write the value 39 to this location for the bootloader to attempt to run the code.

Your code needs offsetting above the bootloader - add the following at the start of your code to achieve this:

Code: Select all

#option org_reset = 2048
When connected to your hardware over Bluetooth, you can send a hash character (#) to query to status of the bootloader. By default this just returns the version of the bootloader. If your firmware uses a version string and / or a serial number string then these can be reported also. For this to work you need to store them somewhere in the EEPROM and uncomment the appropriate lines in the code, along with setting VersionLocation, EndOfVersion, SerialLocation and EndOfSerial to point to the strings.

Lastly, if there is an error the unit will return
BTBOOT, ERR
and a number. The number shows the actual error type, these are:

Error 1: CRC check failed on passed data.
Error 2: Write attempted to an address which does not exist.
Error 3: A write has been attempted which would over-write the bootloader.
Error 4: Framing error on the received data.
Error 5: Invalid data sent.

I should also have said that to use this bootloader you use a serial terminal connected to the device over the Bluetooth link and just send the complete HEX file. I use CoolTerm and Connection>Send Text File.

Post Reply