freemyipod r597 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r596‎ | r597 | r598 >
Date:20:42, 12 February 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Remove the periodic scheduler tick. Instead, wakeup and preemting IRQs are now scheduled dynamically
Modified paths:
  • /emcore/trunk/init.c (modified) (history)
  • /emcore/trunk/target/ipodnano2g/timer.c (modified) (history)
  • /emcore/trunk/target/ipodnano3g/timer.c (modified) (history)
  • /emcore/trunk/target/ipodnano4g/timer.c (modified) (history)
  • /emcore/trunk/thread.c (modified) (history)
  • /emcore/trunk/timer.h (modified) (history)

Diff [purge]

Index: emcore/trunk/init.c
@@ -273,5 +273,6 @@
274274 ib->bootalloc = bootalloc;
275275 thread_create(&(ib->initthread), initthreadname, initthread, ib->initstack,
276276 sizeof(ib->initstack), OS_THREAD, 127, true);
 277+ timer_init();
277278 interrupt_init();
278279 }
Index: emcore/trunk/target/ipodnano2g/timer.c
@@ -27,22 +27,42 @@
2828 #include "s5l8701.h"
2929
3030
31 -void setup_tick()
 31+void timer_init()
3232 {
33 - int cycles = SYSTEM_TICK / 100;
34 -
35 - /* configure timer for 10 kHz */
36 - TBCMD = (1 << 1); /* TB_CLR */
37 - TBPRE = 300 - 1; /* prescaler */
38 - TBCON = (0 << 13) | /* TB_INT1_EN */
39 - (1 << 12) | /* TB_INT0_EN */
 33+}
 34+
 35+void timer_schedule_wakeup(uint32_t usecs)
 36+{
 37+ if (usecs > 11184640)
 38+ {
 39+ TBPRE = 511;
 40+ TBDATA1 = 65535;
 41+ }
 42+ else if (usecs > 21845)
 43+ {
 44+ TBPRE = 511;
 45+ TBDATA1 = (usecs * 192) >> 15;
 46+ }
 47+ else
 48+ {
 49+ TBPRE = 0;
 50+ TBDATA1 = (usecs * 192) >> 6;
 51+ }
 52+ TBCON = (1 << 13) | /* TB_INT1_EN */
 53+ (0 << 12) | /* TB_INT0_EN */
4054 (0 << 11) | /* TB_START */
4155 (2 << 8) | /* TB_CS = PCLK / 16 */
42 - (0 << 4); /* TB_MODE_SEL = interval mode */
43 - TBDATA0 = cycles; /* set interval period */
 56+ (2 << 4); /* TB_MODE_SEL = one-shot mode */
 57+ TBCMD = (1 << 1); /* TB_CLR */
4458 TBCMD = (1 << 0); /* TB_EN */
4559 }
4660
 61+void timer_kill_wakeup()
 62+{
 63+ TBCMD = (1 << 1); /* TB_CLR */
 64+ TBCON = TBCON;
 65+}
 66+
4767 void INT_TIMERB(void)
4868 {
4969 TBCON = TBCON;
Index: emcore/trunk/target/ipodnano3g/timer.c
@@ -27,10 +27,8 @@
2828 #include "s5l8702.h"
2929
3030
31 -void setup_tick()
 31+void timer_init()
3232 {
33 - int cycles = SYSTEM_TICK / 100;
34 -
3533 TACMD = (1 << 1); /* TA_CLR */
3634 TBCMD = (1 << 1); /* TB_CLR */
3735 TCCMD = (1 << 1); /* TC_CLR */
@@ -37,18 +35,40 @@
3836 TDCMD = (1 << 1); /* TD_CLR */
3937 TGCMD = (1 << 1); /* TG_CLR */
4038 THCMD = (1 << 1); /* TH_CLR */
 39+}
4140
42 - /* configure timer for 10 kHz */
43 - TBPRE = 337 - 1; /* prescaler */
44 - TBCON = (0 << 13) | /* TB_INT1_EN */
45 - (1 << 12) | /* TB_INT0_EN */
 41+void timer_schedule_wakeup(uint32_t usecs)
 42+{
 43+ if (usecs > 9942053)
 44+ {
 45+ TBPRE = 511;
 46+ TBDATA1 = 65535;
 47+ }
 48+ else if (usecs > 19417)
 49+ {
 50+ TBPRE = 511;
 51+ TBDATA1 = (usecs * 216) >> 15;
 52+ }
 53+ else
 54+ {
 55+ TBPRE = 0;
 56+ TBDATA1 = (usecs * 216) >> 6;
 57+ }
 58+ TBCON = (1 << 13) | /* TB_INT1_EN */
 59+ (0 << 12) | /* TB_INT0_EN */
4660 (0 << 11) | /* TB_START */
4761 (3 << 8) | /* TB_CS = PCLK / 64 */
48 - (0 << 4); /* TB_MODE_SEL = interval mode */
49 - TBDATA0 = cycles; /* set interval period */
 62+ (2 << 4); /* TB_MODE_SEL = one-shot mode */
 63+ TBCMD = (1 << 1); /* TB_CLR */
5064 TBCMD = (1 << 0); /* TB_EN */
5165 }
5266
 67+void timer_kill_wakeup()
 68+{
 69+ TBCMD = (1 << 1); /* TB_CLR */
 70+ TBCON = TBCON;
 71+}
 72+
5373 void INT_TIMERB(void)
5474 {
5575 TBCON = TBCON;
Index: emcore/trunk/target/ipodnano4g/timer.c
@@ -27,10 +27,8 @@
2828 #include "s5l8720.h"
2929
3030
31 -void setup_tick()
 31+void timer_init()
3232 {
33 - int cycles = SYSTEM_TICK / 100;
34 -
3533 TACMD = (1 << 1); /* TA_CLR */
3634 TBCMD = (1 << 1); /* TB_CLR */
3735 TCCMD = (1 << 1); /* TC_CLR */
@@ -38,18 +36,40 @@
3937 TFCMD = (1 << 1); /* TF_CLR */
4038 TGCMD = (1 << 1); /* TG_CLR */
4139 THCMD = (1 << 1); /* TH_CLR */
 40+}
4241
43 - /* configure timer for 10 kHz */
44 - TBPRE = 208 - 1; /* prescaler */
45 - TBCON = (0 << 13) | /* TB_INT1_EN */
46 - (1 << 12) | /* TB_INT0_EN */
 42+void timer_schedule_wakeup(uint32_t usecs)
 43+{
 44+ if (usecs > 16146247)
 45+ {
 46+ TBPRE = 511;
 47+ TBDATA1 = 65535;
 48+ }
 49+ else if (usecs > 31535)
 50+ {
 51+ TBPRE = 511;
 52+ TBDATA1 = (usecs * 133) >> 15;
 53+ }
 54+ else
 55+ {
 56+ TBPRE = 0;
 57+ TBDATA1 = (usecs * 133) >> 6;
 58+ }
 59+ TBCON = (1 << 13) | /* TB_INT1_EN */
 60+ (0 << 12) | /* TB_INT0_EN */
4761 (0 << 11) | /* TB_START */
4862 (3 << 8) | /* TB_CS = PCLK / 64 */
49 - (0 << 4); /* TB_MODE_SEL = interval mode */
50 - TBDATA0 = cycles; /* set interval period */
 63+ (2 << 4); /* TB_MODE_SEL = one-shot mode */
 64+ TBCMD = (1 << 1); /* TB_CLR */
5165 TBCMD = (1 << 0); /* TB_EN */
5266 }
5367
 68+void timer_kill_wakeup()
 69+{
 70+ TBCMD = (1 << 1); /* TB_CLR */
 71+ TBCON = TBCON;
 72+}
 73+
5474 void INT_TIMERB(void)
5575 {
5676 TBCON = TBCON;
Index: emcore/trunk/thread.c
@@ -238,7 +238,6 @@
239239 idle_thread.type = CORE_THREAD;
240240 idle_thread.name = "idle thread";
241241 idle_thread.stack = (uint32_t*)-1;
242 - setup_tick();
243242 }
244243
245244 bool scheduler_freeze(bool value)
@@ -275,6 +274,9 @@
276275 current_thread->block_type = THREAD_DEFUNCT_STKOV;
277276 wakeup_signal(&dbgwakeup);
278277 }
 278+
 279+ timer_kill_wakeup();
 280+
279281 if (usec - last_tick > SCHEDULER_TICK)
280282 {
281283 uint32_t diff = usec - last_tick;
@@ -286,10 +288,12 @@
287289 }
288290 }
289291
 292+ uint32_t next_unblock = 0xffffffff;
290293 if (scheduler_frozen) thread = &idle_thread;
291294 else
292295 {
293296 for (t = head_thread; t; t = t->thread_next)
 297+ {
294298 if (t->state == THREAD_BLOCKED && t->timeout != -1
295299 && TIME_AFTER(usec, t->blocked_since + t->timeout))
296300 {
@@ -300,6 +304,12 @@
301305 t->blocked_by = NULL;
302306 t->timeout = 0;
303307 }
 308+ else if (t->state == THREAD_BLOCKED && t->timeout != -1)
 309+ {
 310+ uint32_t left = t->blocked_since + t->timeout - usec;
 311+ if (left < next_unblock) next_unblock = left;
 312+ }
 313+ }
304314
305315 if (!thread || thread->state != THREAD_READY)
306316 {
@@ -317,6 +327,9 @@
318328 }
319329 }
320330 }
 331+
 332+ if (thread == &idle_thread) timer_schedule_wakeup(next_unblock);
 333+ else timer_schedule_wakeup(SYSTEM_TICK);
321334 }
322335
323336 current_thread = thread;
Index: emcore/trunk/timer.h
@@ -45,7 +45,9 @@
4646 }
4747
4848
49 -void setup_tick() INITCODE_ATTR;
 49+void timer_init() INITCODE_ATTR;
 50+void timer_schedule_wakeup(uint32_t usecs) ICODE_ATTR;
 51+void timer_kill_wakeup() ICODE_ATTR;
5052 void INT_TIMERB() ICODE_ATTR;
5153
5254