Using CodeLite with the LPC1343: Part 2
How to setup a new project and build your firmware using GCC and the CodeLite IDE
This second part of the tutorial on using CodeLite with the LPC1343 covers deploying the firmware that we built in part one. Two methods of deploying the firmware will be presented: using the LPC1343's built-in USB bootloader, and using a Segger J-Link for ARM and GDB Server.
Deploying the Firmware with the USB Bootloader
The LPC1343 has a very convenient USB bootloader built into the ROM that allows you to quickly (and inexpensively) flash your device by simply dragging and dropping your compiled binary (.bin) file onto the device in Windows.
You can enter the USB bootloader mode by pressing and releasing the "BOOTLOADER" button on the board, which will cause the device to reset and enter the USB bootloade. Once the LPC1343 has enumerated on your PC as a mass storage device (it may take a few seconds to appear for the first time in Windows),you should see a drive with 32KB of storage space, and the current firmware shown as firmware.bin.
We've already mentionned the USB Bootloader in this blog entry, amongst others. To update the firmware, simply delete the current 'firmware.bin' file and replace it with the new .bin, such as the file we generated in the previous tutorial.
Unfortunately, the checksum generated by GCC is not correct and unless the checksum of the .bin file is modified, the firmware will be rejected by the USB bootloader. Thankfully, this is relatively easy to fix. Roel Verdult has provided a free utility to fix the checksum, and the source code is available in the 'tools/lpcrc' folder of the LPC1343 Code Base. A pre-compiled version for Windows (named lpcrc.exe) is also located in the root folder. To fix the checksum, simply go into the command-line and go to the root folder where both the lpcrc.exe tool and your .bin file are located (the file will be named 'firmware.bin' unless you have modified the Makefile), and enter the following command:
Fixing the firmware.bin Checksum
You should see the following message:
At this point, you can enter the USB bootloader by clicking the BOOTLOADER button on the LPC1343 Reference Board and -- assuming the USB cable is connected -- delete the firmware.bin file on the drive that appears and replace it with the .bin file you just created and corrected. (For reference sake, the file can have any name, but it will always appear as firmware.bin in the USB bootloader.)
Reset your board with the RESET button, and your program should start executing.
Using lpcrc in Linux
A precompiled binary of lpcrc for Linux is also included in the '/tools/lpcrc/bin' folder, but you may need to rebuild from source depending on your OS and HW configuration. This is easily accomplished with the following commands (assuming you are already in the 'tools/lpcrc' folder):
To run the lpcrc utility in Linux, simply move the compiled binary (lpcrc) to the root folder where firmware.bin is located, and enter the following command (be sure to include the preceding './' before lpcrc):
If you are seeing "make: execvp: ./lpcrc: Permission denied
" when you run the makefile or try to run lpcrc directly in Linux, you will need to give execute permissions to the pre-compiled version of lpcrc that is included with the lpc1343 code base. Simply execute the following command in the root folder of the code base:
chmod a+x lpcrc
Tip: In recent version of the lpc1343 code base, lpcrc has been setup to automatically run in the makefile after every build. This ensures that a valid binary is generated every time, and you can skip directly to copy and pasting the file onto the development board.
Deploying the Firmware with a Segger J-Link and SWD
- If you wish to flash your device using a Segger J-Link for ARM via SWD you will obviously need to have a J-Link at your disposal, and have already installed the latest drivers from Segger.
A Segger J-Link for ARM can also be used to automatically flash the device from within CodeLite, without having to drop down to the command-prompt or use the USB Bootloader. To do this, you will need to start the Segger GDB Server ('Start > SEGGER > J-Link ARM v4.14a > J-Link GDB Server' for example). Since the software defaults to JTAG mode and the LPC1343 only support the newer SWD, you will need to start the GDB server with the '-if swd' option, which will force it to start in SWD mode. The easiest way to do this is to simply modify the shortcut in your start menu to include the extra -if argument, as seen below:
After this, just restart the GDB Server, and adjust it to have the following settings (see the checkboxes on the right-hand side):
Once Segger J-Link GDB Server is started, you can go back to CodeLite and make sure that the CodeLite project is properly configured to work with GDB Server. This can be verified by right-clicking on the project name, and in the 'Project Settings' popup selecting the 'Debugger' tab, as seen below:
Make sure that the debugger path is set to 'arm-none-eabi-gdb.exe' (unless you are using a different toolchain), that the 'Debugging remote target' checkbox is checked, and that the host address is set to 'localhost' at port '2331'.
The most important part is the set of commands that you will send to the J-Link after a connection is made over TCP/IP. The following commands are provided by default, which will flash the device with the compiled firmware (firmware.elf), and then reset the device to start executing the program:
Commands for Segger J-Link GDB Server and the LPC1343
# Make sure that we are using SWD
monitor interface SWD
# Set monitor to little endian
monitor endian little
# Set monitor speed
monitor speed 1000
# Reset device
# Set device ID to LPC1343
monitor flash device = LPC1343
# Enable flash download
monitor flash download = 1
# Transfer the firmware to the device
# Initializing PC and stack pointer
monitor reg r13 = (0x00000000)
monitor reg pc = (0x00000004)
Flashing the device in CodeLite
To flash the device using the J-Link, you simply need to press the F5 key from within CodeLite, or select the 'Debug > Start/Continue Debugger' menu item. If the build is succesful, this should cause the J-Link to update the firmware and reset the board, which will begin the execution of the firmware: