Data Sheet

8. Compiling a USB application with M-Stack
This section explains how to compile a USB application using M-Stack [http://www.signal11.us/oss/m-stack/], a USB
device stack developed by Signal 11 Software [http://www.signal11.us/].
On the PIC18F25K50, the hardware USB module only implements the lowest levels of the USB protocol,
and many other parts of the protocol need to be implemented in firmware. Therefore, it is usually easier to
incorporate a USB stack into your firmware instead of writing the USB code from scratch. With M-Stack, you
can write C code that uses USB without having to worry about many of the details of how USB works or how
the PIC18F25K50 USB module works.
This tutorial will explain how to use MPLAB X and XC8 to compile the demo apps that come with M-Stack and
run them on the P-Star 25K50 Micro. These apps can be used as a starting point for your own app. You can see
what demo apps are available by looking in the M-Stack apps folder on github [https://github.com/pololu/m-stack/
tree/master/apps].
1. First, install MPLAB X [http://www.microchip.com/mplabx], XC8 [http://www.microchip.com/compilers], and
XC16 [http://www.microchip.com/compilers]. We will use MPLAB X and XC8 to compile the example
code. We will not use XC16, but the MPLAB X projects we will open have configurations that use
XC16 and installing it will avoid some issues when opening those projects.
2. Next, download the Pololu fork of M-Stack [https://github.com/pololu/m-stack]. This version of M-Stack
has a few changes that we made to add support for the PIC18F25K50 and the P-Star 25K50 Micro.
(We are working with the developer of M-Stack to get these changes merged in.) If you download it as
a ZIP archive, be sure to extract the files from the ZIP archive before continuing.
3. Run the MPLAB X IDE.
4. In the “File” menu, select “Open Project…”, and navigate to apps\hid_mouse\MPLAB.X. Select the
MPLAB.X folder and click “Open Project”.
5. In the “Run” menu, under “Set Project Configuration”, select “P-Star_25K50_Micro”. This configures
the project so that it will be built for the PIC18F25K50 and use the --codeoffset 0x2000 option so
that it can be loaded with the P-Stars bootloader.
6. In the “Run” menu, select “Build Project”.
7. The “Output” pane should now show the build output from MPLAB X. One of the last lines of the
output should say “Loading code from” and have the full path to the HEX file produced during
compilation.
8. Use p-load to load this HEX file onto the P-Star using its USB bootloader, as described in Section 5.4.
9. If the app runs successfully, it should connect to the computer as a standard USB mouse and you
should see your mouse cursor moving back and forth horizontally. To regain normal use of your cursor,
you can disconnect the P-Star from USB or put it back into bootloader mode as described in Section
5.2.
These instructions will also work for any other app in M-Stack, except for the bootloader app. These apps can
be used as a starting point for your own app.
Where to find more information
We recommend reading the README.txt file that comes with M-Stack.
Pololu P-Star 25K50 Micro Users Guide © 2001–2017 Pololu Corporation
8. Compiling a USB application with M-Stack Page 29 of 38