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.
#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.
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.
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).
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 firstname.lastname@example.org - we're always interested to hear about your experiences because it's a great source of ideas for making the PSoC experience even better.