You are here

PSoC Creator 3.3 New Features: Patch code into generated source with macro callbacks | Cypress Semiconductor

PSoC Creator 3.3 New Features: Patch code into generated source with macro callbacks

I asked Mark Saunders to talk about a new feature in PSoC Creator 3.3, macro callbacks.  Here's what he had to say.

Matt, people often ask me how to execute user code inside generated API functions without modifying the generated source code. It is usually a requirement when you are working with a third-party IDE, like Eclipse, and you want to keep "your" code (the application) and Cypress code (the component API) separate or when you maintain your source in a revision control system and need to know you have the right files checked in.

Cypress components already support merge regions, where you can write your own code directly in the generated files, but editing generated code is a little scary when you do not know exactly how the tool works. In PSoC Creator 3.3 we have introduced macro callbacks, and they allow you to inject code into the component API functions without editing the source files. It's an elegant solution to the problem that uses the C preprocessor to do macro substitutions inside the target functions.

In the component source code we have embedded callbacks and, if you have a component called "CapSense_1", they look like this in the CapSense_1_INT.c file.

#ifdef CapSense_1_ISR_ENTRY_CALLBACK

     CapSense_1_ISR_EntryCallback();

#endif /* CapSense_1_ISR_ENTRY_CALLBACK */

To use this callback you just need to declare the macro and the function. PSoC Creator 3.3 puts a new header file in your projects called cyapicallbacks.h. That header is included in the generated source files automatically and so all you need to do is put your declaration into cyapicallbacks.h, as follows.

#define CapSense_1_ISR_ENTRY_CALLBACK

This macro will cause the CapSense_1_ISR_EntryCallback function call to be executed from the generated source. You have a lot of choice over how to implement that code. For example, if you just want to add a single line of code just use a function macro.

#define CapSense_1_ISR_ENTRY_CALLBACK

extern int isr_count;

#define CapSense_1_ISR_ENTRY_CALLBACK isr_count++;

There is no limit to the number of instructions in the macro and you can use the back slash character to concatenate multiple lines. Alternatively, you can call an actual function. To do that, simply prototype the function in cyapicallbacks.h and write the definition elsewhere (e.g. in main.c).

#define CapSense_1_ISR_ENTRY_CALLBACK

void CapSense_1_ISR_EntryCallback( void );

We are sprinkling macro callbacks liberally throughout our component code to give you plenty of flexibility in where you can insert your own code. One of the most important uses is with a real-time operation system (RTOS). Many RTOS support system calls from an ISR and perform task switching after all ISRs have completed execution. In order for this to work, the OS has to keep track of nested interrupts (and only do the task switching once they have all completed) and so most RTOS include ISR entry and exit routines that you "wrap around" your ISR code. All component ISRs now have entry and exit callbacks which makes working with an RTOS easier and gives you more flexibility with your interrupt priority selection.

If you have ever wanted to jam some of your own code into generated source please give macro callbacks a try and, if you devise an ingenious solution to a previously difficult problem, please let us know via psoc_creator_feedback@cypress.com - we're always interested to hear about your experiences because it's a great source of ideas for making the PSoC experience even better.

-----------------------

Thanks, Mark!

Comments

mfroberts's picture

I just had someone ask me how to implement this, so the timing is great.  It would be nice to have an app note or tech brief describing this.  It is explained in the components themselves, but a separate document would be helpful to explain the concept to users unfamiliar with this technique.

Mike Roberts
Arrow Cypress Specialist
 

MattLandrum's picture

Thanks for the feedback, Mike.

I'll be huddling with Mark and some of the developers today.

--Matt

jordanss123 jordanss123's picture

fake yeezy boost 750 I appreciate such people conversing on such type of blogs.

 

ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.