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&nbsp;X IDE.</li> <li>In the &#8220;File&#8221; menu, select &#8220;Open Project&#8230;&#8221;, and navigate to <code>apps\hid_mouse\MPLAB.X</code>. Select the <code>MPLAB.X</code> folder and click &#8220;Open Project&#8221;.</li> <li>In the &#8220;Production&#8221; menu, set the Main Project to be the project you just opened.</li> <li>In the &#8220;Production&#8221; menu, set the project configuration to &#8220;P-Star_25K50_Micro&#8221;. 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&#8217;s bootloader.</li> <li>If your P-Star is based on the PIC18F45K50, you should reconfigure MPLAB&nbsp;X to compile for the correct microcontroller. To do this, open the &#8220;File&#8221; menu and select &#8220;Project properties&#8221;. On the left-hand side of the Project Properties window, select the &#8220;P-Star_25K50_Micro&#8221; configuration. Set the Device for this configuration to the model of the PIC on your P-Star (e.g. &#8220;PIC18F45K50&#8221;). You can also rename the configuration to avoid confusion. Click OK.</li> <li>In the &#8220;Production&#8221; menu, select &#8220;Build Main Project&#8221;.</li> <li>The &#8220;Output&#8221; pane should now show the build output from MPLAB&nbsp;X. One of the last lines of the output should say &#8220;Loading code from&#8221; 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>

Related Products

P-Star 45K50 Mini SV
P-Star 25K50 Micro
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors