1#include "main-header.h"
3#include "user_app_exe.h"
5#define MCULIB_SOFTIRQ_HANDLE 13
9static uint32_t real_irq_hertz;
10static uint8_t softirqs_enabled;
13 void (*callback)(uint32_t);
17 uint32_t timer_skipctr;
21static struct softirqdef softirqs[MAX_SOFTIRQS];
24 memset(softirqs, 0,
sizeof(softirqs));
32 for (i = 0; i < MAX_SOFTIRQS; i++) {
34 if (res->callback == NULL) {
41static void softirq_real_irq() {
44 for (i = 0; i < MAX_SOFTIRQS; i++) {
51 if (res->timer_skipctr <= 1) {
52 res->timer_skipctr = res->timer_skip;
63int softirq_set_real_frequency(uint32_t hertz) {
66 if (hertz == real_irq_hertz) {
70 printf(
"[softirq] disabling timer %i\r\n", hw_timer);
71 r = mculib_timer_disable(MCULIB_SOFTIRQ_HANDLE, hw_timer);
73 printf(
"[softirq] failed to disable timer %i: %i\r\n", hw_timer, r);
75 printf(
"[softirq] disabled timer %i: %i\r\n", hw_timer);
83 if (config_get_flag(CONFIG_FLAGS_LEDEXTERN)) {
88 printf(
"[softirq] changing frequency of timer %i to %iHz\r\n", hw_timer, hertz);
89 r = mculib_timer_enable_simple(MCULIB_SOFTIRQ_HANDLE, timer, hertz, &softirq_real_irq);
91 printf(
"[softirq] failed to change timer: %i\r\n", r);
95 real_irq_hertz = hertz;
100void softirqs_removeall() {
101 softirqs_enabled = 0;
104 for (i = 0; i < MAX_SOFTIRQS; i++) {
106 res->callback = NULL;
108 softirq_set_real_frequency(0);
109 softirqs_enabled = 1;
115void softirq_calc_settings() {
118 for (i = 0; i < MAX_SOFTIRQS; i++) {
120 if (res->callback == NULL) {
123 res->timer_skip = real_irq_hertz / res->hertz;
124 res->timer_skipctr = res->timer_skip;
128void print_softirqs() {
131 printf(
"[softirq] Hardware IRQ, timer %i, running at %iHz, enabled=%i\r\n", hw_timer, real_irq_hertz, softirqs_enabled);
134 for (i = 0; i < MAX_SOFTIRQS; i++) {
136 if (res->callback == NULL) {
140 printf(
"[softirq] IRQ #%i running at %iHz, skip=%i, skipctr=%i,opaque=%i,invoked=%i\r\n", i, res->hertz, res->timer_skip, res->timer_skipctr, res->opaque, res->invoke_ctr);
143 printf(
"[softirq] %i softirqs active\r\n", j);
149int softirq_timer(uint32_t hertz, uint32_t opaque,
void (*callback)(uint32_t)) {
150 printf(
"[softirq] adding irq with %iHz (hardware timer @%iHz)\r\n", hertz, real_irq_hertz);
154 printf(
"[softirq] could not add irq: maximum softirq entries (%i) exceeded\r\n", MAX_SOFTIRQS);
157 softirqs_enabled = 0;
160 if (hertz > real_irq_hertz) {
161 if (softirq_set_real_frequency(hertz) != 0) {
162 printf(
"[softirq] could not add irq: failed to set hardware irq to %iHz\r\n", sid->hertz);
163 softirqs_enabled = 1;
168 sid->callback = callback;
170 sid->opaque = opaque;
172 softirq_calc_settings();
173 softirqs_enabled = 1;
174 printf(
"[softirq] added irq with %iHz (opaque=%i)\r\n", sid->hertz, sid->opaque);