-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#5) Added external interrupt callback registry
- Loading branch information
1 parent
71bec38
commit d4e9964
Showing
5 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef EXTI_H | ||
#define EXTI_H | ||
|
||
#include <stdint.h> | ||
|
||
typedef void (*exti_callback_t)(void *); | ||
|
||
/** | ||
* @brief Registers callback to run when external interrupt occurs on specified pin | ||
* | ||
*/ | ||
void exti_register_callback(uint16_t pin_mask, exti_callback_t callback, void *param); | ||
|
||
#endif /* EXTI_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "exti.h" | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
|
||
#define NUM_EXTI_PINS 16 | ||
|
||
struct callback_data { | ||
exti_callback_t cb; | ||
void *param; | ||
}; | ||
|
||
static struct callback_data callbacks[NUM_EXTI_PINS] = {0}; | ||
|
||
// Overload weak EXTI callback function | ||
void HAL_GPIO_EXTI_Callback(uint16_t pin_mask) | ||
{ | ||
// loop through callbacks and call them if the bit is set | ||
for (int i = 0; i < NUM_EXTI_PINS; i++) { | ||
if (pin_mask & (1 << i) && callbacks[i].cb != NULL) { | ||
callbacks[i].cb(callbacks[i].param); | ||
} | ||
} | ||
} | ||
|
||
void exti_register_callback(uint16_t pin_mask, exti_callback_t callback, void *param) | ||
{ | ||
if (pin_mask == 0) | ||
return; | ||
|
||
// get index of set bit in pin_mask | ||
// stolen from https://stackoverflow.com/questions/14767308/how-to-compute-log-base-2-using-bitwise-operators | ||
int i = 0; | ||
while (pin_mask >>= 1) i++; | ||
|
||
callbacks[i].cb = callback; | ||
callbacks[i].param = param; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters