Created
November 16, 2011 16:25
-
-
Save sw17ch/1370540 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <string.h> | |
#include <stdint.h> | |
uint32_t THE_STATUS_REGISTER = 0; | |
typedef void (*handler) (void); | |
typedef struct { | |
handler hdl; | |
uint32_t mask; | |
} HandlerEntry_T; | |
HandlerEntry_T handlers[16]; | |
void init(void); | |
void single_interrupt(void); | |
void register_handler(handler hdl, uint32_t mask); | |
void handler_1(void); | |
void handler_2(void); | |
void handler_3(void); | |
void init(void) | |
{ | |
memset(handlers, 0, sizeof(handlers)); | |
register_handler(handler_1, 0x00000001); | |
register_handler(handler_2, 0x00000002); | |
register_handler(handler_3, 0x00000004); | |
} | |
void single_interrupt(void) | |
{ | |
int i; | |
for (i = 0; i < sizeof(handlers) / sizeof(HandlerEntry_T); i++) | |
{ | |
if (handlers[i].mask & THE_STATUS_REGISTER) | |
{ | |
handlers[i].hdl(); | |
} | |
} | |
} | |
void register_handler(handler hdl, uint32_t mask) | |
{ | |
int i; | |
for (i = 0; i < sizeof(handlers) / sizeof(HandlerEntry_T); i++) | |
{ | |
if (0 == handlers[i].hdl) | |
{ | |
handlers[i].hdl = hdl; | |
handlers[i].mask = mask; | |
break; | |
} | |
} | |
} | |
void handler_1(void) | |
{ | |
printf("Handler 1!\n"); | |
} | |
void handler_2(void) | |
{ | |
printf("Handler 2!\n"); | |
} | |
void handler_3(void) | |
{ | |
printf("Handler 3!\n"); | |
} | |
int main(int argc, char * argv[]) | |
{ | |
init(); | |
THE_STATUS_REGISTER = 1; | |
single_interrupt(); | |
THE_STATUS_REGISTER = 2; | |
single_interrupt(); | |
THE_STATUS_REGISTER = 4; | |
single_interrupt(); | |
THE_STATUS_REGISTER = 1 | 2 | 4; | |
single_interrupt(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ ./single_interrupt
Handler 1!
Handler 2!
Handler 3!
Handler 1!
Handler 2!
Handler 3!