PIC24EP Mikromedia boards replace the original PIC24FJ256GB110 micro controller with a larger and faster model of the PIC24EP series: the PIC24EP512GU810 which is pin to pin compatible but >4x faster (up to 70MIPS) and with 2x the Flash memory and RAM. Most all other features of the Mikromedia board were left unchanged providing for an excellent compatibility out of the box and a nice (upward) migration path.
Porting Notes, Tips and Tricks
Often the journey is more interesting than the destination, here a short summary of the steps (forward and occasionally backward) I took and the notes I made along the way.
First Things First
The first step when porting an MLA project to a new hardware configuration is naturally to update the HardwareProfile.h file.
Testament of the extreme compatibility between the two generations of PIC24 processors, the few and only modifications to the PIC24 original profile proved to be related to the Analog to Digital Converter, of which the PIC24EP series has multiple (4 units) and the novel approach to the input channel multiplexers control registers. Input pins are now set to be Analog or Digital via a set of Special Function Registers named ANSELx (one per each PORTx) similarly to the latest 8-bit PIC micro controller generations rather than the PIC24 (and dsPIC) traditional ADxPCFG single control register.
The tricky part was to realise that beside the different name/location the control bits now respond to the inverse logic: setting a bit to 1 means setting the corresponding pin to ANALOG mode!
Since there are many more input channels/pins available on the PIC24EP, you will notice that the SPI2 port (used for the SD card interface, the MP3 codec and the Serial Flash) is using four pins that are by default set as Analog at power up. The MLA File System module will configure them appropriately if the correct macros are set in the HardwareProfile:
// dsPIC33EP and PIC24EP specific Analog input de-selection for SPI pin #define SD_CS_ANSEL ANSELGbits.ANSG9 #define SD_SCK_ANSEL ANSELGbits.ANSG6 #define SD_SDI_ANSEL ANSELGbits.ANSG7 #define SD_SDO_ANSEL ANSELGbits.ANSG8
Note that when not using the File System (not calling FSInit()) we will need to set those pin input type to digital (0) before using the SPI port with the Serial Flash and the MP3 Codec. We will add this later when introducing and customising the uMedia.c support module (Chapter 4) and in particular the board initialisation function uMBInit().
Chapter 1. Hello World
The first exercise proved to be extremely valuable during the porting phase to verify the assumptions about the PIC24EP oscillator and its correct operation. First, the configuration bits settings had to be re-generated (using the MPLAB X Config bit Window) to use the external 8MHz crystal and to enable the primary PLL. Then the settings were copied and pasted into the source. Only then the backlight flashing frequency could be checked to verify the default clock speed and resulting instruction/peripheral clock.
As documented in the device data sheet the PLL defaults to a x50 multiplier (M) and the two pre and post- dividers default respectively to the value 2 and 4 (N1 and N2) giving a resulting 50MHz system clock and a 25MHz peripheral clock (and 25MIPS, core instructions execution). Basically a PIC24EP will be almost 1.5x faster than a PIC24F device right out of the box. But this is only the beginning… don’t write these values in the HardwareProfile.h just yet, we will return on this shortly…
In the second project/challenge of the first chapter, the reader is invited to use the RTCC module to generate a 1/2 second interval instead of a timer derived from the primary system clock. The solution offered for the PIC24F project (1.Solution.x) hinted to two peripheral library modules: rtcc.h and PwrMgmt.h. But as it turns out, none of the two could be found for the PIC24EP.
The rtcc.h module is actually available but, for reasons yet unknown to me, it cannot be reached by the compiler unless a path to it is added to the “Include Dirs Path” option in the xc16-gcc project configurations. This path is dependent on the compiler installation and the specific operating system, in my case (OSX) it turned out to be: “/Applications/microchip/xc16/v1.21/support/peripheral_30F_24H_33F” (Windows users will likely have to replace the “/Applications” part with “C:/Program Files”)
The PwrMgmt.h file turns out to be actually unnecessary (obsoleted) if the Sleep() macro, now included in the built-in set of assembly macros of the xc16 compiler, is used to send the PIC24 device into low power mode!
Note this is applicable to the PIC24F models as well and will be included in future book updates.
Chapter 2. Hello MLA
No changes were required.
Chapter 3. BitMaps and Fonts
No changes were required.
Chapter 4. Touch
When the uMedia.c support file is introduced, a small section dedicated to configuring the oscillator and PLL of the PIC24EP was first added. Initalizing the PPS module turned out not to be necessary as the SPI2 pins are not multiplexed through it anymore.
See considerations below in Chapter 6 for the optimal choice of the frequency settings.
Chapter 5. Storage
No changes were required.
Chapter 6. Sound
When I started experimenting more with the oscillator options of the PIC24EP, as I was curious to identify where and which of the subsystems of the Mikromedia board would prove to limit (if any) the maximum clock speed and therefore the performance of the micro controller, it turned out that the MP3 codec was the primary suspect. The PIC24EP primary PLL can operate at frequencies in excess of 300MHz and through the pre- and post- dividers it can be made to generate a large variety of system clock frequencies all the way up to 120MHz and peripheral bus frequencies up to 60MHz.
On a Mikromedia board though, since a single SPI port is shared among the Serial Flash, SD card and MP3 decoder a common/compatible port setting and bit rate must be identified to avoid having to continuously reinitialise the peripheral as the three interfaces are alternating during normal use. For example an MP3 player application (such as project 6.3) will use the SPI port initially to read from the serial Flash a set of touch panel calibration values. Later it will alternate use of the same SPI module between reading the sound samples from the SD card (File System) and sending them to the MP3 decoder.
Luckily one such compatible configuration is found in the setting: CKP=1, CKE=0, SMP=0, mode=0. It’s the bit rate selection though that turns out to be a bit more tricky. Granted the SPI minimum peripheral clock divider is 1:2 (the primary and secondary prescalers cannot be set both to 1:1 mode) but the MLA File System will actually select the 1:4 divider for FAST_MODE operation. If we were to select the (120MHz clock) 60MHz peripheral clock, this would produce a maximum SPI port bit rate of 15MHz which appears to be compatible with most/all SD cards (to my knowledge) and the Serial Flash. Unfortunately the MP3 decoder (VS1053) data sheet indicates that the maximum input clock allowed is 12MHz (or 1/2 of the codec own clock x PLL).
This brings us to the (safe) choice of 80MHz as the ideal maximum (compromised) system clock speed for the PIC24EP Mikromedia resulting in a 40MHz peripheral clock and 10MHz SPI port maximum speed. (these are the values I eventually set in the appropriate HardwareProfile.h section),
This leaves us with a device that operates at 40MIPS which is already representing a 250% speed improvement over the original PIC24.
Higher performance figures (close to 400%) can be obtained by carefully balancing the benefit vs. the cost/complexity of dynamically changing the SPI port bit-rate during use and customising parts of the MLA library, but this is beside the scope of the book and the set of examples offered.
Chapter 7. GOL
Porting the first two examples of use of the GOL library was easy and required no changes beside updating the PPS configuration macro (strangely the order of parameters passed to the PPSOutput() function has been inverted!).
The last two examples using the GDD tool will require more work as the tool is currently not including support for the PIC24EP Mikromedia, although all the necessary elements are already there or can be found in the /uMB support folder. I will investigate the amount of effort required for this and other Mikromedia variants…
Chapter 8. USB
Confiiguring appropriately the secondary PLL is very simple and, once enabled, it provides the stable clock required for the Full Speed USB module.
Both USB class demonstrations (CDC and HID) work as expected!
Both Alarm.X and Pond.X worked right out of the box without any modification.
Same money, same code, 250% to 400% the performance, sounds like a very good deal to me…