I struggle to understand the way function pointers work in the code I'm working with.
The code describes the IAP (in-application programming) of Flash memory on the LPC1768 Microcontroller from NXP.
unsigned int flashParamTab[5];
unsigned int flashResultTab[5];
// IAP entry function
#define IAP_LOCATION 0x1FFF1FF1
// In Application Programming function entry
typedef void (*IAP)(unsigned int[], unsigned int[]);
IAP flashIapEntry = (IAP)IAP_LOCATION;
This code is than being used with in various functions, like these two:
// Prepare a sector for write operation.
void flashPrepareSectors(unsigned int startSec, unsigned int endSec){
flashParamTab[0] = IAP_PREPARE_WRITE;
flashParamTab[1] = startSec;
flashParamTab[2] = endSec;
flashIapEntry(flashParamTab, flashResultTab);
}
// Erase a sector.
void flashEraseSectors(unsigned int startSec, unsigned int endSec) {
flashParamTab[0] = IAP_ERASE_SECTORS;
flashParamTab[1] = startSec;
flashParamTab[2] = endSec;
flashIapEntry(flashParamTab, flashResultTab);
}
As far as I understand:
typedef void (*IAP)(unsigned int[], unsigned int[]);
defines an IAP datatype where IAP is a pointer to a function that takes two arrays of integers as arguments, and doesn't return anything.
In the next step the
IAP flashIapEntry = (IAP)IAP_LOCATION;
declares a variable flashIapEntry of type IAP, that is a pointer to a specific location in the memory, where code for a function that takes two arrays of integers is located.
I can't really figure out what happens when this function is called. In my opinion in both cases (when preparing and erasing a sector) the same function is called (the one located at 0x1fff1ff1), and the only difference is the first field of the array, the flashParamTab[0], that defines the behaviour of this function.
Am I correct with my understanding?