Lec17SkillTwo - Function Pointers and Callback Functions...

Info icon This preview shows pages 1–9. Sign up to view the full content.

ECE 471 17-Sk2-1 Lecture 17 Software Development Techniques II Outline Callback functions Conditional compilation File structures Summary
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

ECE 471 17-Sk2-2 Using an example of 16-bit counter building block to demonstrate the techniques Function pointer Callback function Function Pointers and Callback Functions
Image of page 2
ECE 471 17-Sk2-3 Function Pointers Definition - pointer to functions (or procedures) Function pointer type declaration typedef void (*Counter_Callback_Routine_Type) (void); typedef U16 (*Counter_Callback_Routine_Type) (U16); Variable declaration Counter_Callback_Routine_Type function_pointer; Assign function to a function pointer void function1 (void); /*declare a function*/ function_pointer = &function1; Usage - the following instruction executes function1. function_pointer();
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

ECE 471 17-Sk2-4 Callback function Definition Callback functions is a function that is called by service routines to indicate the service is done. They are usually defined in the application and called by service routines. Callback function is defined the same as a regular function Why using callback functions When the collaboration needs the ability to dynamically change the service at run-time Prevent polling in asynchronous operations. Allow the service routine to interface with different applications.
Image of page 4
ECE 471 17-Sk2-5 Implementation of Callback Functions Step 1 Define the interface of a callback function (function type, number of parameters, the type of each parameter, and the type of the return value) Step 2 Define a function pointer of the callback function type Step 3 Define an interface data structure that include the function pointer Step 4 Write the server program module that calls the callback function Step 5 Write the client program module (may be application module) that has the callback function. Associate the name of the callback function to the function pointer. Step 6 Associate the callback function to the
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

ECE 471 17-Sk2-6 Example 1 Counter Building Block Many applications need counters Microcontroller has limited hardware counters The basic functionality of counters for all applications are similar, which include Initialize a counter ---BB_Initialize_Counter Reset a counter --- BB_Reset_Counter Service a counter --- BB_Service_Counter (BB_Service_Counter notifies the user when the counter expires) Read counter value - BB_Read_Counter
Image of page 6
ECE 471 17-Sk2-7 To provide a convenient usage, the counter BB should support many independent counters simultaneously counter_1, counter_n use one function for each general operation BB_Initialize_Counter() BB_Service_Counter() allow each function to support all possible counters BB_Initialize_Counter(counter_number) BB_Service_Counter (counter_number) Example 1 (cont) Counter BB Requirement
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

ECE 471 17-Sk2-8 bb_cnt.h #ifndef _bb_cnt_h #define _bb_cnt_h 1 typedef unsigned int U16; typedef unsigned char U8; typedef void (*Counter_Callback_Routine_Type) (void); typedef const struct Counter_Block_Struct_Tag { U8 *counter_expired; U16
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '08
  • Chien
  • callback function

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern