9. Compiling a USB application with M-Stack
The examples in Section 8 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.
This section explains how to compile a USB application using M-Stack, a USB device stack that is an alternative to the Microchip USB stack.
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.
- First, install MPLAB X, XC8, and XC16. 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.
- Next, download the Pololu fork of M-Stack. If you download it as a ZIP archive, be sure to extract the files from the ZIP archive before continuing.
- Run the MPLAB X IDE.
- In the “File” menu, select “Open Project…”, and navigate to
apps\hid_mouse\MPLAB.X. Select the
MPLAB.Xfolder and click “Open Project”.
- In the “Production” menu, set the Main Project to be the project you just opened.
- 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
--codeoffset 0x2000option so that it can be loaded with the P-Star’s bootloader.
- 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.
- In the “Production” menu, select “Build Main Project”.
- 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.
- Use p-load to load this HEX file onto the P-Star using its USB bootloader, as described in Section 5.4.
- 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 example app in M-Stack, except for the bootloader app.
Where to find more information
We recommend reading the
README.txt file that comes with M-Stack.
For information about USB, see the USB 2.0 Specification.
To write PC software to communicate with the P-Star over USB using a generic USB interface, see libusb.