Support » Pololu P-Star User’s Guide »
9. Compiling a USB application with M-Stack
<p class="note">The examples in <a href="/docs/0J62/8">Section 8</a> show how to use the Microchip USB stack on the P-Star. M-Stack is an alternative USB stack for PICs that we have modified to work on the P-Star.</p>
<p>This section explains how to compile a USB application using <a href="https://github.com/signal11/m-stack">M-Stack</a>, a USB device stack that is an alternative to the Microchip USB stack.</p>
<p>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 <a href="https://github.com/pololu/m-stack/tree/master/apps">M-Stack apps folder on github</a>.</p>
<ol>
<li>First, install <a href="https://www.microchip.com/en-us/tools-resources/develop/mplab-x-ide">MPLAB X</a>, <a href="https://www.microchip.com/en-us/tools-resources/develop/mplab-xc-compilers">XC8</a>, and <a href="https://www.microchip.com/en-us/tools-resources/develop/mplab-xc-compilers">XC16</a>. 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.</li>
<li>Next, download the <a href="https://github.com/pololu/m-stack">Pololu fork of M-Stack</a>. If you download it as a ZIP archive, be sure to extract the files from the ZIP archive before continuing.</li>
<li>Run the MPLAB X IDE.</li>
<li>In the “File” menu, select “Open Project…”, and navigate to <code>apps\hid_mouse\MPLAB.X</code>. Select the <code>MPLAB.X</code> folder and click “Open Project”.</li>
<li>In the “Production” menu, set the Main Project to be the project you just opened.</li>
<li>In the “Production” menu, set the project configuration to “P-Star_25K50_Micro”. This configures the project so that it will be built for the PIC18F25K50 and use the <code>--codeoffset 0x2000</code> option so that it can be loaded with the P-Star’s bootloader.</li>
<li>If your P-Star is based on the PIC18F45K50, you should reconfigure MPLAB X to compile for the correct microcontroller. To do this, open the “File” menu and select “Project properties”. On the left-hand side of the Project Properties window, select the “P-Star_25K50_Micro” configuration. Set the Device for this configuration to the model of the PIC on your P-Star (e.g. “PIC18F45K50”). You can also rename the configuration to avoid confusion. Click OK.</li>
<li>In the “Production” menu, select “Build Main Project”.</li>
<li>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.</li>
<li>Use p-load to load this HEX file onto the P-Star using its USB bootloader, as described in <a href="/docs/0J62/5.4">Section 5.4</a>.</li>
<li>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 <a href="/docs/0J62/5.2">Section 5.2</a>.</li>
</ol>
<p>These instructions will also work for any other example app in M-Stack, except for the bootloader app.</p>
<h4>Where to find more information</h4>
<p>We recommend reading the <code>README.txt</code> file that comes with M-Stack.</p>
<p>For information about USB, see the <a href="https://www.usb.org/document-library/usb-20-specification">USB 2.0 Specification</a>.</p>
<p>For information about the USB module on the PIC, see the <a href="https://www.microchip.com/en-us/product/PIC18F25K50">PIC18F25K50</a>/<a href="https://www.microchip.com/en-us/product/PIC18F45K50">PIC18F45K50</a> datasheet.</p>
<p>To write PC software to communicate with the P-Star over USB using a generic USB interface, see <a href="https://libusb.info/">libusb</a>.</p>