- 17. January 2012: Atypical Curiosity
- 4. January 2012: PIC32MX7 PIM, RB5 pin conflict (solved)
- 30. December 2011: MikroE Mini-32 Board
- 29. December 2011: Donate to Wikipedia
- 28. December 2011: PIC32 Interrupt Nesting (update)
- 20. December 2011: Graphics Library 3.02
- 2. December 2011: Home Brewed IDE for PIC32 assembly development
- 30. November 2011: Yoda Conditions, Egyptian brackets and more...
- 9. November 2011: AVI Player Project (for the uMMB)
- 21. March 2011: More Multimedia Boards
- January 2012
- December 2011
- November 2011
- March 2011
- February 2011
- January 2011
- March 2010
- January 2010
- December 2009
- November 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- January 2009
- December 2008
- November 2008
- October 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
Optimizing the graphic library
While working on the porting to the PIC32 of the code developed for the original PIC24 book it occurred to me that there were some obvious optimizations I had yet to explore.
For a starter, the graphic.c library was using several resources including Timer3, the Output Compare 3 module, the Output Compare 4 module and the SPI1 port in addition to one general purpose I/O (RG0 in this case). The OC3 module was used to generate the Horizontal Synchronization pulse portion of the composite video signal, but the module output pin RD2 (active as soon as the module is enabled) was not used. Rather RG0 was “manually” set during the OC3 interrupt service routine and reset during the Timer3 interrupt service routine. This added unnecessary overhead to the application as the OC3 module could easily be configured to do it all by itself…
In fact, if instead of configuring the OC3 module to perform single pulses we have it configured for continuous mode (OC3CON=0×000d), we can now remove completely the corresponding interrupt service routine and simplify the code inside the Timer3 ISR so to vary the duration of the pulses as needed to perform a vertical sync (long) pulse or an horizontal sync (short) pulse.
The code simplification is only one of the benefits:
- one general purpose I/O (RG0) pin can now be freed,
- the continuous operation of OC3 assures an even more accurate and glitch-less horizontal sync timing, and most importantly
- a number of cycles has been shaved off from the graphic module total overhead.
The main disadvantage is that now we need to modify the circuit assembled in the prototyping area (or the AV16 board, if you got the PCB or the kit…). The HSYNC signal must now be connected to the RD2 pin.
Looking at the simulation plots (obtained with the Logic Analyzer tool) and comparing the results with what published in chapter 12 of the book, you will see a further reduction of approximately 20 cycles out of 200 we used to count (a 10% relative improvement).
But the main limitation of the profiling technique used (setting an I/O each time we enter an ISR) makes us underestimate the benefits of this simple optimization. In fact, by omitting completely the OC3 ISR, we have eliminated a few more cycles required to the PIC24 to enter and exit the interrupt– 6 to be more precise.
Finally since we are talking about optimizations, let’s not forget that the MPLAB C30 compiler v3.02 Student Edition allows optimization level 1 to be used even after the initial 60 days period!
By using the -O1 compiler switch (or simply opening the Project Options dialog box with Project>Build Options>Project, selecting the MPLAB C30 pane, selecting Optimizations in the Categories combo box, and finally selecting Level 1 ) and recompiling we can get a significantly more compact code and a total cycle count of just 100 cycles. That is, the graphic module offers now only half the overhead of the previous revision!
Leave a Reply
You must be logged in to post a comment.